msgpackr 1.9.0 → 1.9.2

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
@@ -120,10 +120,10 @@
120
120
  let size = source.length;
121
121
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
122
122
  if (forEach) {
123
- if (forEach(value) === false) return;
123
+ if (forEach(value, lastPosition, position$1) === false) return;
124
124
  while(position$1 < size) {
125
125
  lastPosition = position$1;
126
- if (forEach(checkedRead()) === false) {
126
+ if (forEach(checkedRead(), lastPosition, position$1) === false) {
127
127
  return
128
128
  }
129
129
  }
@@ -197,6 +197,10 @@
197
197
  position$1 = bundledStrings$1.postBundlePosition;
198
198
  bundledStrings$1 = null;
199
199
  }
200
+ if (sequentialMode)
201
+ // we only need to restore the structures if there was an error, but if we completed a read,
202
+ // we can clear this out and keep the structures we read
203
+ currentStructures.restoreStructures = null;
200
204
 
201
205
  if (position$1 == srcEnd) {
202
206
  // finished reading this source, cleanup references
@@ -967,7 +971,10 @@
967
971
  structure.highByte = highByte;
968
972
  }
969
973
  let existingStructure = currentStructures[id];
970
- if (existingStructure && existingStructure.isShared) {
974
+ // If it is a shared structure, we need to restore any changes after reading.
975
+ // Also in sequential mode, we may get incomplete reads and thus errors, and we need to restore
976
+ // to the state prior to an incomplete read in order to properly resume.
977
+ if (existingStructure && (existingStructure.isShared || sequentialMode)) {
971
978
  (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
972
979
  }
973
980
  currentStructures[id] = structure;
@@ -977,10 +984,10 @@
977
984
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
978
985
  currentExtensions[0].noBuffer = true;
979
986
 
980
- let global = typeof globalThis === 'object' ? globalThis : window;
987
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
981
988
  currentExtensions[0x65] = () => {
982
989
  let data = read();
983
- return (global[data[0]] || Error)(data[1])
990
+ return (glbl[data[0]] || Error)(data[1])
984
991
  };
985
992
 
986
993
  currentExtensions[0x69] = (data) => {
@@ -1024,7 +1031,7 @@
1024
1031
  if (!typedArrayName)
1025
1032
  throw new Error('Could not find typed array for code ' + typeCode)
1026
1033
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
1027
- return new global[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
1034
+ return new glbl[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
1028
1035
  };
1029
1036
  currentExtensions[0x78] = () => {
1030
1037
  let data = read();
@@ -1314,7 +1321,7 @@
1314
1321
  if (serializationsSinceTransitionRebuild < 10)
1315
1322
  serializationsSinceTransitionRebuild++;
1316
1323
  let sharedLength = structures.sharedLength || 0;
1317
- if (structures.length > sharedLength)
1324
+ if (structures.length > sharedLength && !isSequential)
1318
1325
  structures.length = sharedLength;
1319
1326
  if (transitionsCount > 10000) {
1320
1327
  // force a rebuild occasionally after a lot of transitions so it can get cleaned up
@@ -1949,7 +1956,10 @@
1949
1956
  }
1950
1957
  }, {
1951
1958
  pack(set, allocateForWrite, pack) {
1952
- if (this.setAsEmptyObject) return pack({})
1959
+ if (this.setAsEmptyObject) {
1960
+ allocateForWrite(0);
1961
+ return pack({})
1962
+ }
1953
1963
  let array = Array.from(set);
1954
1964
  let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1955
1965
  if (this.moreTypes) {
@@ -3053,7 +3063,6 @@
3053
3063
  var deserialized = packr.unpack(serialized);
3054
3064
  assert.deepEqual(deserialized, data);
3055
3065
  });
3056
-
3057
3066
  test('255 chars', function () {
3058
3067
  const data = 'RRZG9A6I7xupPeOZhxcOcioFsuhszGOdyDUcbRf4Zef2kdPIfC9RaLO4jTM5JhuZvTsF09fbRHMGtqk7YAgu3vespeTe9l61ziZ6VrMnYu2CamK96wCkmz0VUXyqaiUoTPgzk414LS9yYrd5uh7w18ksJF5SlC2e91rukWvNqAZJjYN3jpkqHNOFchCwFrhbxq2Lrv1kSJPYCx9blRg2hGmYqTbElLTZHv20iNqwZeQbRMgSBPT6vnbCBPnOh1W';
3059
3068
  var serialized = pack(data);
@@ -3150,8 +3159,24 @@
3150
3159
  var deserialized = packr.unpack(serialized);
3151
3160
  assert.deepEqual(deserialized, data);
3152
3161
  });
3162
+
3153
3163
  }
3164
+ test('mapAsEmptyObject combination', function () {
3165
+ const msgpackr = new Packr({ useRecords: false, encodeUndefinedAsNil: true, variableMapSize: true, mapAsEmptyObject: true, setAsEmptyObject: true });
3154
3166
 
3167
+ const map = new Map();
3168
+ map.set('a', 1);
3169
+ map.set('b', 2);
3170
+ const set = new Set();
3171
+ set.add('a');
3172
+ set.add('b');
3173
+ const input = { map, set };
3174
+
3175
+ const packed = msgpackr.pack(input);
3176
+ const unpacked = msgpackr.unpack(packed);
3177
+ assert.deepEqual(unpacked.map, {});
3178
+ assert.deepEqual(unpacked.set, {});
3179
+ });
3155
3180
  test('pack/unpack empty data with bundled strings', function () {
3156
3181
  var data = {};
3157
3182
  let packr = new Packr({bundleStrings: true});
@@ -3961,6 +3986,14 @@
3961
3986
  assert.deepEqual(values, [1, 2, 3, 4]);
3962
3987
  });
3963
3988
 
3989
+ test('unpackMultiple with positions', () => {
3990
+ let values = unpackMultiple(new Uint8Array([1, 2, 3, 4]));
3991
+ assert.deepEqual(values, [1, 2, 3, 4]);
3992
+ values = [];
3993
+ unpackMultiple(new Uint8Array([1, 2, 3, 4]), (value,start,end) => values.push([value,start,end]));
3994
+ assert.deepEqual(values, [[1,0,1], [2,1,2], [3,2,3], [4,3,4]]);
3995
+ });
3996
+
3964
3997
  });
3965
3998
  suite('msgpackr performance tests', function(){
3966
3999
  test('performance JSON.parse', function() {