msgpackr 1.7.0-alpha6 → 1.7.0-alpha7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/test.js CHANGED
@@ -29,7 +29,7 @@
29
29
  C1.name = 'MessagePack 0xC1';
30
30
  var sequentialMode = false;
31
31
  var inlineObjectReadThreshold = 2;
32
- var readStruct, onLoadedStructures;
32
+ var readStruct, onLoadedStructures, onSaveState;
33
33
  try {
34
34
  new Function('');
35
35
  } catch(error) {
@@ -204,7 +204,7 @@
204
204
  // else more to read, but we are reading sequentially, so don't clear source yet
205
205
  return result
206
206
  } catch(error) {
207
- if (currentStructures.restoreStructures)
207
+ if (currentStructures?.restoreStructures)
208
208
  restoreStructures();
209
209
  clearSource();
210
210
  if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
@@ -988,6 +988,8 @@
988
988
  // currentExtensions[0x52] = () =>
989
989
 
990
990
  function saveState(callback) {
991
+ if (onSaveState)
992
+ onSaveState();
991
993
  let savedSrcEnd = srcEnd;
992
994
  let savedPosition = position;
993
995
  let savedSrcStringStart = srcStringStart;
@@ -1029,9 +1031,10 @@
1029
1031
  mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1030
1032
  }
1031
1033
  var defaultUnpackr = new Unpackr({ useRecords: false });
1032
- function setReadStruct(updatedReadStruct, loadedStructs) {
1034
+ function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1033
1035
  readStruct = updatedReadStruct;
1034
1036
  onLoadedStructures = loadedStructs;
1037
+ onSaveState = saveState;
1035
1038
  }
1036
1039
 
1037
1040
  let textEncoder;
@@ -1981,11 +1984,13 @@
1981
1984
  const NUMBER = 0;
1982
1985
  const UTF8 = 2;
1983
1986
  const OBJECT_DATA = 1;
1987
+ const DATE = 16;
1984
1988
  const TYPE_NAMES = ['num', 'object', 'string', 'ascii'];
1989
+ TYPE_NAMES[DATE] = 'date';
1985
1990
  const float32Headers = [false, true, true, false, false, true, true, false];
1986
1991
  let updatedPosition;
1987
1992
  const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
1988
- let textEncoder$1;
1993
+ let textEncoder$1, currentSource;
1989
1994
  try {
1990
1995
  textEncoder$1 = new TextEncoder();
1991
1996
  } catch (error) {}
@@ -2026,6 +2031,7 @@
2026
2031
  return 0;
2027
2032
  position += headerSize;
2028
2033
  let queuedReferences = [];
2034
+ let usedAscii0;
2029
2035
  let keyIndex = 0;
2030
2036
  for (let key in object) {
2031
2037
  let value = object[key];
@@ -2041,7 +2047,8 @@
2041
2047
  string16: null,
2042
2048
  object16: null,
2043
2049
  num32: null,
2044
- float64: null
2050
+ float64: null,
2051
+ date64: null
2045
2052
  };
2046
2053
  }
2047
2054
  if (position > safeEnd) {
@@ -2134,25 +2141,47 @@
2134
2141
  refPosition += encodeUtf8(target, value, refPosition);
2135
2142
  isNotAscii = refPosition - strStart > strLength;
2136
2143
  }
2137
- if (refOffset < 0xf6) {
2138
- if (isNotAscii)
2139
- transition = nextTransition.string8 || createTypeTransition(nextTransition, UTF8, 1);
2140
- else
2141
- transition = nextTransition.ascii8 || createTypeTransition(nextTransition, ASCII, 1);
2144
+ if (refOffset < 0xa0 || (refOffset < 0xf6 && (nextTransition.ascii8 || nextTransition.string8))) {
2145
+ // short strings
2146
+ if (isNotAscii) {
2147
+ if (!(transition = nextTransition.string8)) {
2148
+ if (typedStructs.length > 10 && (transition = nextTransition.ascii8)) {
2149
+ // we can safely change ascii to utf8 in place since they are compatible
2150
+ transition.__type = UTF8;
2151
+ nextTransition.ascii8 = null;
2152
+ nextTransition.string8 = transition;
2153
+ pack(null, 0, true); // special call to notify that structures have been updated
2154
+ } else {
2155
+ transition = createTypeTransition(nextTransition, UTF8, 1);
2156
+ }
2157
+ }
2158
+ } else if (refOffset === 0 && !usedAscii0) {
2159
+ usedAscii0 = true;
2160
+ transition = nextTransition.ascii0 || createTypeTransition(nextTransition, ASCII, 0);
2161
+ break; // don't increment position
2162
+ }// else ascii:
2163
+ else if (!(transition = nextTransition.ascii8) && !(typedStructs.length > 10 && (transition = nextTransition.string8)))
2164
+ transition = createTypeTransition(nextTransition, ASCII, 1);
2142
2165
  target[position++] = refOffset;
2143
2166
  } else {
2144
- if (isNotAscii)
2167
+ // TODO: Enable ascii16 at some point, but get the logic right
2168
+ //if (isNotAscii)
2145
2169
  transition = nextTransition.string16 || createTypeTransition(nextTransition, UTF8, 2);
2146
- else
2147
- transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
2170
+ //else
2171
+ //transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
2148
2172
  targetView.setUint16(position, refOffset, true);
2149
2173
  position += 2;
2150
2174
  }
2151
2175
  break;
2152
2176
  case 'object':
2153
2177
  if (value) {
2154
- //transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
2155
- queuedReferences.push(key, value, keyIndex);
2178
+ if (value.constructor === Date) {
2179
+ transition = nextTransition.date64 || createTypeTransition(nextTransition, DATE, 8);
2180
+ targetView.setFloat64(position, value.getTime(), true);
2181
+ position += 8;
2182
+ } else {
2183
+ queuedReferences.push(key, value, keyIndex);
2184
+ }
2156
2185
  break;
2157
2186
  } else { // null
2158
2187
  nextTransition = anyType(nextTransition, position, targetView, -10); // match CBOR with this
@@ -2370,7 +2399,8 @@
2370
2399
  string16: null,
2371
2400
  object16: null,
2372
2401
  num32: null,
2373
- float64: null
2402
+ float64: null,
2403
+ date64: null,
2374
2404
  };
2375
2405
  }
2376
2406
  transition = createTypeTransition(nextTransition, type, size);
@@ -2548,7 +2578,12 @@
2548
2578
  if (type === UTF8) {
2549
2579
  return src.toString('utf8', ref + refStart, end + refStart);
2550
2580
  } else {
2551
- return unpackr.unpack(src, { start: ref + refStart, end: end + refStart }); // could reuse this object
2581
+ currentSource = source;
2582
+ try {
2583
+ return unpackr.unpack(src, { start: ref + refStart, end: end + refStart });
2584
+ } finally {
2585
+ currentSource = null;
2586
+ }
2552
2587
  }
2553
2588
  };
2554
2589
  break;
@@ -2596,6 +2631,16 @@
2596
2631
  };
2597
2632
  break;
2598
2633
  }
2634
+ break;
2635
+ case DATE:
2636
+ get = function () {
2637
+ let source = this[sourceSymbol];
2638
+ let src = source.src;
2639
+ let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2640
+ return new Date(dataView.getFloat64(source.position + property.offset, true));
2641
+ };
2642
+ break;
2643
+
2599
2644
  }
2600
2645
  property.get = get;
2601
2646
  }
@@ -2620,6 +2665,14 @@
2620
2665
  }
2621
2666
  throw new Error('Unknown constant');
2622
2667
  }
2668
+
2669
+ function saveState$1() {
2670
+ if (currentSource) {
2671
+ currentSource.src = Uint8Array.prototype.slice.call(currentSource.src, currentSource.position, currentSource.srcEnd);
2672
+ currentSource.position = 0;
2673
+ currentSource.srcEnd = currentSource.src.length;
2674
+ }
2675
+ }
2623
2676
  function prepareStructures$1(structures, packr) {
2624
2677
  if (packr.typedStructs) {
2625
2678
  let structMap = new Map();
@@ -2649,12 +2702,17 @@
2649
2702
  return structures;
2650
2703
  }
2651
2704
 
2652
- setReadStruct(readStruct$1, onLoadedStructures$1);
2705
+ setReadStruct(readStruct$1, onLoadedStructures$1, saveState$1);
2653
2706
 
2654
2707
  let allSampleData = [];
2655
2708
  for (let i = 1; i < 6; i++) {
2656
2709
  allSampleData.push(JSON.parse(fs.readFileSync(new URL(`./example${i > 1 ? i : ''}.json`, (document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href)))));
2657
2710
  }
2711
+ allSampleData.push({
2712
+ name: 'some other types',
2713
+ date: new Date(),
2714
+ empty: '',
2715
+ });
2658
2716
  const sampleData = allSampleData[3];
2659
2717
  function tryRequire(module) {
2660
2718
  try {
@@ -2833,7 +2891,6 @@
2833
2891
  var deserialized = packr.unpack(serialized);
2834
2892
  assert.deepEqual(deserialized, data);
2835
2893
  });
2836
- break;
2837
2894
  }
2838
2895
 
2839
2896
  test('pack/unpack empty data with bundled strings', function(){