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/node.cjs CHANGED
@@ -33,7 +33,7 @@ const C1 = new C1Type();
33
33
  C1.name = 'MessagePack 0xC1';
34
34
  var sequentialMode = false;
35
35
  var inlineObjectReadThreshold = 2;
36
- var readStruct, onLoadedStructures;
36
+ var readStruct, onLoadedStructures, onSaveState;
37
37
  try {
38
38
  new Function('');
39
39
  } catch(error) {
@@ -210,7 +210,7 @@ function checkedRead(options) {
210
210
  // else more to read, but we are reading sequentially, so don't clear source yet
211
211
  return result
212
212
  } catch(error) {
213
- if (currentStructures.restoreStructures)
213
+ if (currentStructures?.restoreStructures)
214
214
  restoreStructures();
215
215
  clearSource();
216
216
  if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
@@ -1034,6 +1034,8 @@ currentExtensions[0xff] = (data) => {
1034
1034
  // currentExtensions[0x52] = () =>
1035
1035
 
1036
1036
  function saveState(callback) {
1037
+ if (onSaveState)
1038
+ onSaveState();
1037
1039
  let savedSrcEnd = srcEnd;
1038
1040
  let savedPosition = position;
1039
1041
  let savedStringPosition = stringPosition;
@@ -1103,9 +1105,10 @@ function roundFloat32(float32Number) {
1103
1105
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1104
1106
  return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1105
1107
  }
1106
- function setReadStruct(updatedReadStruct, loadedStructs) {
1108
+ function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1107
1109
  readStruct = updatedReadStruct;
1108
1110
  onLoadedStructures = loadedStructs;
1111
+ onSaveState = saveState;
1109
1112
  }
1110
1113
 
1111
1114
  let textEncoder;
@@ -2071,11 +2074,13 @@ const ASCII = 3; // the MIBenum from https://www.iana.org/assignments/character-
2071
2074
  const NUMBER = 0;
2072
2075
  const UTF8 = 2;
2073
2076
  const OBJECT_DATA = 1;
2077
+ const DATE = 16;
2074
2078
  const TYPE_NAMES = ['num', 'object', 'string', 'ascii'];
2079
+ TYPE_NAMES[DATE] = 'date';
2075
2080
  const float32Headers = [false, true, true, false, false, true, true, false];
2076
2081
  let updatedPosition;
2077
2082
  const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
2078
- let textEncoder$1;
2083
+ let textEncoder$1, currentSource;
2079
2084
  try {
2080
2085
  textEncoder$1 = new TextEncoder();
2081
2086
  } catch (error) {}
@@ -2116,6 +2121,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2116
2121
  return 0;
2117
2122
  position += headerSize;
2118
2123
  let queuedReferences = [];
2124
+ let usedAscii0;
2119
2125
  let keyIndex = 0;
2120
2126
  for (let key in object) {
2121
2127
  let value = object[key];
@@ -2131,7 +2137,8 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2131
2137
  string16: null,
2132
2138
  object16: null,
2133
2139
  num32: null,
2134
- float64: null
2140
+ float64: null,
2141
+ date64: null
2135
2142
  };
2136
2143
  }
2137
2144
  if (position > safeEnd) {
@@ -2224,25 +2231,47 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2224
2231
  refPosition += encodeUtf8(target, value, refPosition);
2225
2232
  isNotAscii = refPosition - strStart > strLength;
2226
2233
  }
2227
- if (refOffset < 0xf6) {
2228
- if (isNotAscii)
2229
- transition = nextTransition.string8 || createTypeTransition(nextTransition, UTF8, 1);
2230
- else
2231
- transition = nextTransition.ascii8 || createTypeTransition(nextTransition, ASCII, 1);
2234
+ if (refOffset < 0xa0 || (refOffset < 0xf6 && (nextTransition.ascii8 || nextTransition.string8))) {
2235
+ // short strings
2236
+ if (isNotAscii) {
2237
+ if (!(transition = nextTransition.string8)) {
2238
+ if (typedStructs.length > 10 && (transition = nextTransition.ascii8)) {
2239
+ // we can safely change ascii to utf8 in place since they are compatible
2240
+ transition.__type = UTF8;
2241
+ nextTransition.ascii8 = null;
2242
+ nextTransition.string8 = transition;
2243
+ pack(null, 0, true); // special call to notify that structures have been updated
2244
+ } else {
2245
+ transition = createTypeTransition(nextTransition, UTF8, 1);
2246
+ }
2247
+ }
2248
+ } else if (refOffset === 0 && !usedAscii0) {
2249
+ usedAscii0 = true;
2250
+ transition = nextTransition.ascii0 || createTypeTransition(nextTransition, ASCII, 0);
2251
+ break; // don't increment position
2252
+ }// else ascii:
2253
+ else if (!(transition = nextTransition.ascii8) && !(typedStructs.length > 10 && (transition = nextTransition.string8)))
2254
+ transition = createTypeTransition(nextTransition, ASCII, 1);
2232
2255
  target[position++] = refOffset;
2233
2256
  } else {
2234
- if (isNotAscii)
2257
+ // TODO: Enable ascii16 at some point, but get the logic right
2258
+ //if (isNotAscii)
2235
2259
  transition = nextTransition.string16 || createTypeTransition(nextTransition, UTF8, 2);
2236
- else
2237
- transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
2260
+ //else
2261
+ //transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
2238
2262
  targetView.setUint16(position, refOffset, true);
2239
2263
  position += 2;
2240
2264
  }
2241
2265
  break;
2242
2266
  case 'object':
2243
2267
  if (value) {
2244
- //transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
2245
- queuedReferences.push(key, value, keyIndex);
2268
+ if (value.constructor === Date) {
2269
+ transition = nextTransition.date64 || createTypeTransition(nextTransition, DATE, 8);
2270
+ targetView.setFloat64(position, value.getTime(), true);
2271
+ position += 8;
2272
+ } else {
2273
+ queuedReferences.push(key, value, keyIndex);
2274
+ }
2246
2275
  break;
2247
2276
  } else { // null
2248
2277
  nextTransition = anyType(nextTransition, position, targetView, -10); // match CBOR with this
@@ -2460,7 +2489,8 @@ function onLoadedStructures$1(sharedData) {
2460
2489
  string16: null,
2461
2490
  object16: null,
2462
2491
  num32: null,
2463
- float64: null
2492
+ float64: null,
2493
+ date64: null,
2464
2494
  };
2465
2495
  }
2466
2496
  transition = createTypeTransition(nextTransition, type, size);
@@ -2638,7 +2668,12 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2638
2668
  if (type === UTF8) {
2639
2669
  return src.toString('utf8', ref + refStart, end + refStart);
2640
2670
  } else {
2641
- return unpackr.unpack(src, { start: ref + refStart, end: end + refStart }); // could reuse this object
2671
+ currentSource = source;
2672
+ try {
2673
+ return unpackr.unpack(src, { start: ref + refStart, end: end + refStart });
2674
+ } finally {
2675
+ currentSource = null;
2676
+ }
2642
2677
  }
2643
2678
  };
2644
2679
  break;
@@ -2686,6 +2721,16 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2686
2721
  };
2687
2722
  break;
2688
2723
  }
2724
+ break;
2725
+ case DATE:
2726
+ get = function () {
2727
+ let source = this[sourceSymbol];
2728
+ let src = source.src;
2729
+ let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2730
+ return new Date(dataView.getFloat64(source.position + property.offset, true));
2731
+ };
2732
+ break;
2733
+
2689
2734
  }
2690
2735
  property.get = get;
2691
2736
  }
@@ -2710,6 +2755,14 @@ function toConstant(code) {
2710
2755
  }
2711
2756
  throw new Error('Unknown constant');
2712
2757
  }
2758
+
2759
+ function saveState$1() {
2760
+ if (currentSource) {
2761
+ currentSource.src = Uint8Array.prototype.slice.call(currentSource.src, currentSource.position, currentSource.srcEnd);
2762
+ currentSource.position = 0;
2763
+ currentSource.srcEnd = currentSource.src.length;
2764
+ }
2765
+ }
2713
2766
  function prepareStructures$1(structures, packr) {
2714
2767
  if (packr.typedStructs) {
2715
2768
  let structMap = new Map();
@@ -2739,7 +2792,7 @@ function prepareStructures$1(structures, packr) {
2739
2792
  return structures;
2740
2793
  }
2741
2794
 
2742
- setReadStruct(readStruct$1, onLoadedStructures$1);
2795
+ setReadStruct(readStruct$1, onLoadedStructures$1, saveState$1);
2743
2796
 
2744
2797
  class PackrStream extends stream.Transform {
2745
2798
  constructor(options) {