msgpackr 2.0.0 → 2.0.1

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/index.js CHANGED
@@ -92,7 +92,7 @@
92
92
  currentUnpackr = this;
93
93
  if (this.structures) {
94
94
  currentStructures = this.structures;
95
- return checkedRead();
95
+ return checkedRead(options);
96
96
  } else if (!currentStructures || currentStructures.length > 0) {
97
97
  currentStructures = [];
98
98
  }
@@ -101,7 +101,7 @@
101
101
  if (!currentStructures || currentStructures.length > 0)
102
102
  currentStructures = [];
103
103
  }
104
- return checkedRead();
104
+ return checkedRead(options);
105
105
  }
106
106
  unpackMultiple(source, forEach) {
107
107
  let values, lastPosition = 0;
@@ -136,6 +136,8 @@
136
136
  }
137
137
  }
138
138
  _mergeStructures(loadedStructures, existingStructures) {
139
+ if (this._onLoadedStructures)
140
+ loadedStructures = this._onLoadedStructures(loadedStructures);
139
141
  loadedStructures = loadedStructures || [];
140
142
  if (Object.isFrozen(loadedStructures))
141
143
  loadedStructures = loadedStructures.map(structure => structure.slice(0));
@@ -172,7 +174,15 @@
172
174
  if (sharedLength < currentStructures.length)
173
175
  currentStructures.length = sharedLength;
174
176
  }
175
- let result = read();
177
+ let result;
178
+ if (currentUnpackr._readStruct && src[position$1] < 0x40 && src[position$1] >= 0x20) {
179
+ result = currentUnpackr._readStruct(src, position$1, srcEnd);
180
+ src = null; // dispose of this so that recursive unpack calls don't save state
181
+ if (!(options && options.lazy) && result)
182
+ result = result.toJSON();
183
+ position$1 = srcEnd;
184
+ } else
185
+ result = read();
176
186
  if (bundledStrings$1) { // bundled strings to skip past
177
187
  position$1 = bundledStrings$1.postBundlePosition;
178
188
  bundledStrings$1 = null;
@@ -1097,6 +1107,8 @@
1097
1107
  // currentExtensions[0x52] = () =>
1098
1108
 
1099
1109
  function saveState(callback) {
1110
+ if (currentUnpackr && currentUnpackr._onSaveState)
1111
+ currentUnpackr._onSaveState();
1100
1112
  let savedSrcEnd = srcEnd;
1101
1113
  let savedPosition = position$1;
1102
1114
  let savedSrcStringStart = srcStringStart;
@@ -1162,6 +1174,10 @@
1162
1174
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1163
1175
  return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
1164
1176
  }
1177
+ // Marker for downstream libraries (e.g. structon) to detect per-instance
1178
+ // struct-decoding hooks (this._readStruct, this._onLoadedStructures,
1179
+ // this._onSaveState). See `checkedRead` for the dispatch.
1180
+ Unpackr.SUPPORTS_STRUCT_HOOKS = true;
1165
1181
 
1166
1182
  let textEncoder;
1167
1183
  try {
@@ -1285,7 +1301,14 @@
1285
1301
  hasSharedUpdate = false;
1286
1302
  let encodingError;
1287
1303
  try {
1288
- pack(value);
1304
+ if (packr._writeStruct && value && typeof value === 'object') {
1305
+ if (value.constructor === Object) writeStruct(value); // simple object
1306
+ else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
1307
+ // allow user classes, if they don't need special handling (but do use toJSON if available)
1308
+ writeStruct(value.toJSON ? value.toJSON() : value);
1309
+ } else pack(value);
1310
+ } else
1311
+ pack(value);
1289
1312
  let lastBundle = bundledStrings;
1290
1313
  if (bundledStrings)
1291
1314
  writeBundles(start, pack, 0);
@@ -1341,7 +1364,7 @@
1341
1364
  let sharedLength = structures.sharedLength || 0;
1342
1365
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1343
1366
  let returnBuffer = target.subarray(start, position);
1344
- let newSharedData = prepareStructures(structures, packr);
1367
+ let newSharedData = (packr._prepareStructures || prepareStructures)(structures, packr);
1345
1368
  if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
1346
1369
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1347
1370
  // get updated structures and try again if the update failed
@@ -1510,7 +1533,7 @@
1510
1533
  } else if (type === 'number') {
1511
1534
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1512
1535
  // positive uint
1513
- if (value < 0x40 || (value < 0x80 && this.useRecords === false)) {
1536
+ if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this._writeStruct)) {
1514
1537
  target[position++] = value;
1515
1538
  } else if (value < 0x100) {
1516
1539
  target[position++] = 0xcc;
@@ -1901,6 +1924,24 @@
1901
1924
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1902
1925
  } : writeRecord;
1903
1926
 
1927
+ const writeStruct = (object) => {
1928
+ let newPosition = packr._writeStruct(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1929
+ if (notifySharedUpdate)
1930
+ return hasSharedUpdate = true;
1931
+ position = newPosition;
1932
+ let startTarget = target;
1933
+ pack(value);
1934
+ resetStructures();
1935
+ if (startTarget !== target) {
1936
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1937
+ }
1938
+ return position;
1939
+ });
1940
+ if (newPosition === 0) // bail and go to a msgpack object
1941
+ return writeObject(object);
1942
+ position = newPosition;
1943
+ };
1944
+
1904
1945
  const makeRoom = (end) => {
1905
1946
  let newSize;
1906
1947
  if (end > 0x1000000) {
@@ -2020,6 +2061,8 @@
2020
2061
  clearSharedData() {
2021
2062
  if (this.structures)
2022
2063
  this.structures = [];
2064
+ if (this.typedStructs)
2065
+ this.typedStructs = [];
2023
2066
  }
2024
2067
  }
2025
2068
 
@@ -2261,6 +2304,11 @@
2261
2304
  return structures;
2262
2305
  }
2263
2306
 
2307
+ // Marker for downstream libraries (e.g. structon) to detect that this Packr
2308
+ // supports per-instance struct-encoding hooks (this._writeStruct,
2309
+ // this._prepareStructures). See `pack` for the dispatch.
2310
+ Packr.SUPPORTS_STRUCT_HOOKS = true;
2311
+
2264
2312
  let defaultPackr = new Packr({ useRecords: false });
2265
2313
  const pack = defaultPackr.pack;
2266
2314
  const encode = defaultPackr.pack;