msgpackr 1.9.7 → 1.9.9

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
@@ -175,8 +175,8 @@ class Unpackr {
175
175
  }
176
176
  return this.structures = loadedStructures
177
177
  }
178
- decode(source, end) {
179
- return this.unpack(source, end)
178
+ decode(source, options) {
179
+ return this.unpack(source, options)
180
180
  }
181
181
  }
182
182
  function checkedRead(options) {
@@ -1231,6 +1231,7 @@ class Packr extends Unpackr {
1231
1231
  } else
1232
1232
  position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1233
1233
  start = position;
1234
+ if (encodeOptions & RESERVE_START_SPACE) position += (encodeOptions & 0xff);
1234
1235
  referenceMap = packr.structuredClone ? new Map() : null;
1235
1236
  if (packr.bundleStrings && typeof value !== 'string') {
1236
1237
  bundledStrings = [];
@@ -1332,7 +1333,7 @@ class Packr extends Unpackr {
1332
1333
  let newSharedData = prepareStructures$1(structures, packr);
1333
1334
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1334
1335
  // get updated structures and try again if the update failed
1335
- return packr.pack(value)
1336
+ return packr.pack(value, encodeOptions)
1336
1337
  }
1337
1338
  packr.lastNamedStructuresLength = sharedLength;
1338
1339
  return returnBuffer
@@ -1926,7 +1927,7 @@ class Packr extends Unpackr {
1926
1927
  }
1927
1928
  };
1928
1929
  const writeStruct = (object, safePrototype) => {
1929
- let newPosition = writeStructSlots(object, target, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1930
+ let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1930
1931
  if (notifySharedUpdate)
1931
1932
  return hasSharedUpdate = true;
1932
1933
  position = newPosition;
@@ -2198,6 +2199,7 @@ const Encoder = Packr;
2198
2199
  const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
2199
2200
  const REUSE_BUFFER_MODE = 512;
2200
2201
  const RESET_BUFFER_MODE = 1024;
2202
+ const RESERVE_START_SPACE = 2048;
2201
2203
 
2202
2204
  const ASCII = 3; // the MIBenum from https://www.iana.org/assignments/character-sets/character-sets.xhtml (and other character encodings could be referenced by MIBenum)
2203
2205
  const NUMBER = 0;
@@ -2228,7 +2230,7 @@ const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
2228
2230
  return textEncoder.encodeInto(string, target.subarray(position)).written
2229
2231
  } : false;
2230
2232
  setWriteStructSlots(writeStruct, prepareStructures);
2231
- function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
2233
+ function writeStruct(object, target, encodingStart, position, structures, makeRoom, pack, packr) {
2232
2234
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2233
2235
  // note that we rely on pack.js to load stored structures before we get to this point
2234
2236
  let targetView = target.dataView;
@@ -2236,12 +2238,12 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2236
2238
  let safeEnd = target.length - 10;
2237
2239
  let start = position;
2238
2240
  if (position > safeEnd) {
2239
- let lastStart = start;
2240
2241
  target = makeRoom(position);
2241
2242
  targetView = target.dataView;
2242
- position -= lastStart;
2243
- refsStartPosition -= lastStart;
2244
- start = 0;
2243
+ position -= encodingStart;
2244
+ start -= encodingStart;
2245
+ refsStartPosition -= encodingStart;
2246
+ encodingStart = 0;
2245
2247
  safeEnd = target.length - 10;
2246
2248
  }
2247
2249
 
@@ -2279,13 +2281,13 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2279
2281
  };
2280
2282
  }
2281
2283
  if (position > safeEnd) {
2282
- let lastStart = start;
2283
2284
  target = makeRoom(position);
2284
2285
  targetView = target.dataView;
2285
- position -= lastStart;
2286
- refsStartPosition -= lastStart;
2287
- refPosition -= lastStart;
2288
- start = 0;
2286
+ position -= encodingStart;
2287
+ start -= encodingStart;
2288
+ refsStartPosition -= encodingStart;
2289
+ refPosition -= encodingStart;
2290
+ encodingStart = 0;
2289
2291
  safeEnd = target.length - 10;
2290
2292
  }
2291
2293
  switch (typeof value) {
@@ -2324,13 +2326,13 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2324
2326
  let strLength = value.length;
2325
2327
  refOffset = refPosition - refsStartPosition;
2326
2328
  if ((strLength << 2) + refPosition > safeEnd) {
2327
- let lastStart = start;
2328
2329
  target = makeRoom((strLength << 2) + refPosition);
2329
2330
  targetView = target.dataView;
2330
- position -= lastStart;
2331
- refsStartPosition -= lastStart;
2332
- refPosition -= lastStart;
2333
- start = 0;
2331
+ position -= encodingStart;
2332
+ start -= encodingStart;
2333
+ refsStartPosition -= encodingStart;
2334
+ refPosition -= encodingStart;
2335
+ encodingStart = 0;
2334
2336
  safeEnd = target.length - 10;
2335
2337
  }
2336
2338
  if (strLength > ((0xff00 + refOffset) >> 2)) {
@@ -2489,9 +2491,10 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2489
2491
  refPosition = newPosition.position;
2490
2492
  targetView = newPosition.targetView;
2491
2493
  target = newPosition.target;
2492
- refsStartPosition -= start;
2493
- position -= start;
2494
- start = 0;
2494
+ refsStartPosition -= encodingStart;
2495
+ position -= encodingStart;
2496
+ start -= encodingStart;
2497
+ encodingStart = 0;
2495
2498
  } else
2496
2499
  refPosition = newPosition;
2497
2500
  if (size === 2) {
@@ -2565,7 +2568,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2565
2568
  if (refsStartPosition === refPosition)
2566
2569
  return position; // no refs
2567
2570
  typedStructs.lastStringStart = position - start;
2568
- return writeStruct(object, target, start, structures, makeRoom, pack, packr);
2571
+ return writeStruct(object, target, encodingStart, start, structures, makeRoom, pack, packr);
2569
2572
  }
2570
2573
  return refPosition;
2571
2574
  }