msgpackr 2.0.0 → 2.0.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
@@ -94,7 +94,7 @@
94
94
  currentUnpackr = this;
95
95
  if (this.structures) {
96
96
  currentStructures = this.structures;
97
- return checkedRead();
97
+ return checkedRead(options);
98
98
  } else if (!currentStructures || currentStructures.length > 0) {
99
99
  currentStructures = [];
100
100
  }
@@ -103,7 +103,7 @@
103
103
  if (!currentStructures || currentStructures.length > 0)
104
104
  currentStructures = [];
105
105
  }
106
- return checkedRead();
106
+ return checkedRead(options);
107
107
  }
108
108
  unpackMultiple(source, forEach) {
109
109
  let values, lastPosition = 0;
@@ -138,6 +138,8 @@
138
138
  }
139
139
  }
140
140
  _mergeStructures(loadedStructures, existingStructures) {
141
+ if (this._onLoadedStructures)
142
+ loadedStructures = this._onLoadedStructures(loadedStructures);
141
143
  loadedStructures = loadedStructures || [];
142
144
  if (Object.isFrozen(loadedStructures))
143
145
  loadedStructures = loadedStructures.map(structure => structure.slice(0));
@@ -174,7 +176,15 @@
174
176
  if (sharedLength < currentStructures.length)
175
177
  currentStructures.length = sharedLength;
176
178
  }
177
- let result = read();
179
+ let result;
180
+ if (currentUnpackr._readStruct && src[position$1] < 0x40 && src[position$1] >= 0x20) {
181
+ result = currentUnpackr._readStruct(src, position$1, srcEnd);
182
+ src = null; // dispose of this so that recursive unpack calls don't save state
183
+ if (!(options && options.lazy) && result)
184
+ result = result.toJSON();
185
+ position$1 = srcEnd;
186
+ } else
187
+ result = read();
178
188
  if (bundledStrings$1) { // bundled strings to skip past
179
189
  position$1 = bundledStrings$1.postBundlePosition;
180
190
  bundledStrings$1 = null;
@@ -1137,6 +1147,8 @@
1137
1147
  // currentExtensions[0x52] = () =>
1138
1148
 
1139
1149
  function saveState(callback) {
1150
+ if (currentUnpackr && currentUnpackr._onSaveState)
1151
+ currentUnpackr._onSaveState();
1140
1152
  let savedSrcEnd = srcEnd;
1141
1153
  let savedPosition = position$1;
1142
1154
  let savedStringPosition = stringPosition;
@@ -1205,6 +1217,10 @@
1205
1217
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1206
1218
  return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
1207
1219
  }
1220
+ // Marker for downstream libraries (e.g. structon) to detect per-instance
1221
+ // struct-decoding hooks (this._readStruct, this._onLoadedStructures,
1222
+ // this._onSaveState). See `checkedRead` for the dispatch.
1223
+ Unpackr$1.SUPPORTS_STRUCT_HOOKS = true;
1208
1224
 
1209
1225
  let textEncoder;
1210
1226
  try {
@@ -1328,7 +1344,14 @@
1328
1344
  hasSharedUpdate = false;
1329
1345
  let encodingError;
1330
1346
  try {
1331
- pack(value);
1347
+ if (packr._writeStruct && value && typeof value === 'object') {
1348
+ if (value.constructor === Object) writeStruct(value); // simple object
1349
+ else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
1350
+ // allow user classes, if they don't need special handling (but do use toJSON if available)
1351
+ writeStruct(value.toJSON ? value.toJSON() : value);
1352
+ } else pack(value);
1353
+ } else
1354
+ pack(value);
1332
1355
  let lastBundle = bundledStrings;
1333
1356
  if (bundledStrings)
1334
1357
  writeBundles(start, pack, 0);
@@ -1384,7 +1407,7 @@
1384
1407
  let sharedLength = structures.sharedLength || 0;
1385
1408
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1386
1409
  let returnBuffer = target.subarray(start, position);
1387
- let newSharedData = prepareStructures(structures, packr);
1410
+ let newSharedData = (packr._prepareStructures || prepareStructures)(structures, packr);
1388
1411
  if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
1389
1412
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1390
1413
  // get updated structures and try again if the update failed
@@ -1553,7 +1576,7 @@
1553
1576
  } else if (type === 'number') {
1554
1577
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1555
1578
  // positive uint
1556
- if (value < 0x40 || (value < 0x80 && this.useRecords === false)) {
1579
+ if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this._writeStruct)) {
1557
1580
  target[position++] = value;
1558
1581
  } else if (value < 0x100) {
1559
1582
  target[position++] = 0xcc;
@@ -1944,6 +1967,24 @@
1944
1967
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1945
1968
  } : writeRecord;
1946
1969
 
1970
+ const writeStruct = (object) => {
1971
+ let newPosition = packr._writeStruct(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1972
+ if (notifySharedUpdate)
1973
+ return hasSharedUpdate = true;
1974
+ position = newPosition;
1975
+ let startTarget = target;
1976
+ pack(value);
1977
+ resetStructures();
1978
+ if (startTarget !== target) {
1979
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1980
+ }
1981
+ return position;
1982
+ });
1983
+ if (newPosition === 0) // bail and go to a msgpack object
1984
+ return writeObject(object);
1985
+ position = newPosition;
1986
+ };
1987
+
1947
1988
  const makeRoom = (end) => {
1948
1989
  let newSize;
1949
1990
  if (end > 0x1000000) {
@@ -2063,6 +2104,8 @@
2063
2104
  clearSharedData() {
2064
2105
  if (this.structures)
2065
2106
  this.structures = [];
2107
+ if (this.typedStructs)
2108
+ this.typedStructs = [];
2066
2109
  }
2067
2110
  };
2068
2111
 
@@ -2304,6 +2347,11 @@
2304
2347
  return structures;
2305
2348
  }
2306
2349
 
2350
+ // Marker for downstream libraries (e.g. structon) to detect that this Packr
2351
+ // supports per-instance struct-encoding hooks (this._writeStruct,
2352
+ // this._prepareStructures). See `pack` for the dispatch.
2353
+ Packr$1.SUPPORTS_STRUCT_HOOKS = true;
2354
+
2307
2355
  let defaultPackr = new Packr$1({ useRecords: false });
2308
2356
  const pack$1 = defaultPackr.pack;
2309
2357
  defaultPackr.pack;