msgpackr 1.9.6 → 1.9.8

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 = [];
@@ -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;
@@ -2291,25 +2293,28 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2291
2293
  switch (typeof value) {
2292
2294
  case 'number':
2293
2295
  let number = value;
2294
- if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
2295
- if (number < 0xf6 && number >= 0 && (nextTransition.num8 || number < 0x20 && !nextTransition.num32)) {
2296
- transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
2297
- target[position++] = number;
2298
- } else {
2299
- transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2300
- targetView.setUint32(position, number, true);
2301
- position += 4;
2302
- }
2303
- break;
2304
- } else if (number < 0x100000000 && number >= -0x80000000) {
2305
- targetView.setFloat32(position, number, true);
2306
- if (float32Headers[target[position + 3] >>> 5]) {
2307
- let xShifted;
2308
- // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2309
- if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
2296
+ // first check to see if we are using a lot of ids and should default to wide/common format
2297
+ if (nextId < 200 || !nextTransition.num64) {
2298
+ if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
2299
+ if (number < 0xf6 && number >= 0 && (nextTransition.num8 && !(nextId > 200 && nextTransition.num32) || number < 0x20 && !nextTransition.num32)) {
2300
+ transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
2301
+ target[position++] = number;
2302
+ } else {
2310
2303
  transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2304
+ targetView.setUint32(position, number, true);
2311
2305
  position += 4;
2312
- break;
2306
+ }
2307
+ break;
2308
+ } else if (number < 0x100000000 && number >= -0x80000000) {
2309
+ targetView.setFloat32(position, number, true);
2310
+ if (float32Headers[target[position + 3] >>> 5]) {
2311
+ let xShifted;
2312
+ // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2313
+ if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
2314
+ transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2315
+ position += 4;
2316
+ break;
2317
+ }
2313
2318
  }
2314
2319
  }
2315
2320
  }