msgpackr 1.10.2 → 1.11.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/pack.js CHANGED
@@ -28,7 +28,7 @@ export class Packr extends Unpackr {
28
28
  let structures
29
29
  let referenceMap
30
30
  let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
31
- return target.utf8Write(string, position, 0xffffffff)
31
+ return target.utf8Write(string, position, target.byteLength - position)
32
32
  } : (textEncoder && textEncoder.encodeInto) ?
33
33
  function(string, position) {
34
34
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -53,7 +53,7 @@ export class Packr extends Unpackr {
53
53
  if (!this.structures && options.useRecords != false)
54
54
  this.structures = []
55
55
  // two byte record ids for shared structures
56
- let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64)
56
+ let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64)
57
57
  let sharedLimitId = maxSharedStructures + 0x40
58
58
  let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40
59
59
  if (maxStructureId > 8256) {
@@ -71,7 +71,7 @@ export class Packr extends Unpackr {
71
71
  }
72
72
  safeEnd = target.length - 10
73
73
  if (safeEnd - position < 0x800) {
74
- // don't start too close to the end,
74
+ // don't start too close to the end,
75
75
  target = new ByteArrayAllocate(target.length)
76
76
  targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))
77
77
  safeEnd = target.length - 10
@@ -414,7 +414,7 @@ export class Packr extends Unpackr {
414
414
  targetView.setUint32(position, referee.id)
415
415
  position += 4
416
416
  return
417
- } else
417
+ } else
418
418
  referenceMap.set(value, { offset: position - start })
419
419
  }
420
420
  let constructor = value.constructor
@@ -442,7 +442,7 @@ export class Packr extends Unpackr {
442
442
  pack(entryValue)
443
443
  }
444
444
  }
445
- } else {
445
+ } else {
446
446
  for (let i = 0, l = extensions.length; i < l; i++) {
447
447
  let extensionClass = extensionClasses[i]
448
448
  if (value instanceof extensionClass) {
@@ -510,11 +510,11 @@ export class Packr extends Unpackr {
510
510
  if (json !== value)
511
511
  return pack(json)
512
512
  }
513
-
513
+
514
514
  // if there is a writeFunction, use it, otherwise just encode as undefined
515
515
  if (type === 'function')
516
516
  return pack(this.writeFunction && this.writeFunction(value));
517
-
517
+
518
518
  // no extension found, write as plain object
519
519
  writeObject(value)
520
520
  }
@@ -536,18 +536,20 @@ export class Packr extends Unpackr {
536
536
  if (this.largeBigIntToFloat) {
537
537
  target[position++] = 0xcb
538
538
  targetView.setFloat64(position, Number(value))
539
- } else if (this.useBigIntExtension && value < 2n**(1023n) && value > -(2n**(1023n))) {
539
+ } else if (this.largeBigIntToString) {
540
+ return pack(value.toString());
541
+ } else if (this.useBigIntExtension && value < BigInt(2)**BigInt(1023) && value > -(BigInt(2)**BigInt(1023))) {
540
542
  target[position++] = 0xc7
541
543
  position++;
542
544
  target[position++] = 0x42 // "B" for BigInt
543
545
  let bytes = [];
544
546
  let alignedSign;
545
547
  do {
546
- let byte = value & 0xffn;
547
- alignedSign = (byte & 0x80n) === (value < 0n ? 0x80n : 0n);
548
+ let byte = value & BigInt(0xff);
549
+ alignedSign = (byte & BigInt(0x80)) === (value < BigInt(0) ? BigInt(0x80) : BigInt(0));
548
550
  bytes.push(byte);
549
- value >>= 8n;
550
- } while (!((value === 0n || value === -1n) && alignedSign));
551
+ value >>= BigInt(8);
552
+ } while (!((value === BigInt(0) || value === BigInt(-1)) && alignedSign));
551
553
  target[position-2] = bytes.length;
552
554
  for (let i = bytes.length; i > 0;) {
553
555
  target[position++] = Number(bytes[--i]);
@@ -555,7 +557,8 @@ export class Packr extends Unpackr {
555
557
  return
556
558
  } else {
557
559
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
558
- ' useBigIntExtension or set largeBigIntToFloat to convert to float-64')
560
+ ' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
561
+ ' largeBigIntToString to convert to string')
559
562
  }
560
563
  }
561
564
  position += 8
@@ -572,9 +575,19 @@ export class Packr extends Unpackr {
572
575
  }
573
576
  }
574
577
 
575
- const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
578
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
576
579
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
577
- let keys = Object.keys(object)
580
+ let keys;
581
+ if (this.skipValues) {
582
+ keys = [];
583
+ for (let key in object) {
584
+ if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
585
+ !this.skipValues.includes(object[key]))
586
+ keys.push(key);
587
+ }
588
+ } else {
589
+ keys = Object.keys(object)
590
+ }
578
591
  let length = keys.length
579
592
  if (length < 0x10) {
580
593
  target[position++] = 0x80 | length
@@ -615,6 +628,10 @@ export class Packr extends Unpackr {
615
628
  size++
616
629
  }
617
630
  }
631
+ if (size > 0xffff) {
632
+ throw new Error('Object is too large to serialize with fast 16-bit map size,' +
633
+ ' use the "variableMapSize" option to serialize this object');
634
+ }
618
635
  target[objectOffset++ + start] = size >> 8
619
636
  target[objectOffset + start] = size & 0xff
620
637
  }
@@ -693,9 +710,9 @@ export class Packr extends Unpackr {
693
710
  }
694
711
  }
695
712
 
696
- // craete reference to useRecords if useRecords is a function
713
+ // create reference to useRecords if useRecords is a function
697
714
  const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
698
-
715
+
699
716
  const writeObject = checkUseRecords ? (object) => {
700
717
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object)
701
718
  } : writeRecord
@@ -823,9 +840,15 @@ export class Packr extends Unpackr {
823
840
  useBuffer(buffer) {
824
841
  // this means we are finished using our own buffer and we can write over it safely
825
842
  target = buffer
826
- targetView = new DataView(target.buffer, target.byteOffset, target.byteLength)
843
+ target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength))
827
844
  position = 0
828
845
  }
846
+ set position (value) {
847
+ position = value;
848
+ }
849
+ get position() {
850
+ return position;
851
+ }
829
852
  clearSharedData() {
830
853
  if (this.structures)
831
854
  this.structures = []
@@ -834,12 +857,6 @@ export class Packr extends Unpackr {
834
857
  }
835
858
  }
836
859
 
837
- function copyBinary(source, target, targetOffset, offset, endOffset) {
838
- while (offset < endOffset) {
839
- target[targetOffset++] = source[offset++]
840
- }
841
- }
842
-
843
860
  extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, C1Type ]
844
861
  extensions = [{
845
862
  pack(date, allocateForWrite, pack) {
@@ -969,7 +986,7 @@ function writeBuffer(buffer, allocateForWrite) {
969
986
  target[position++] = length >> 8
970
987
  target[position++] = length & 0xff
971
988
  } else {
972
- var { target, position, targetView } = allocateForWrite(length + 5)
989
+ let { target, position, targetView } = allocateForWrite(length + 5)
973
990
  target[position++] = 0xc6
974
991
  targetView.setUint32(position, length)
975
992
  position += 4
@@ -1084,4 +1101,4 @@ import { FLOAT32_OPTIONS } from './unpack.js'
1084
1101
  export const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS
1085
1102
  export const REUSE_BUFFER_MODE = 512
1086
1103
  export const RESET_BUFFER_MODE = 1024
1087
- export const RESERVE_START_SPACE = 2048
1104
+ export const RESERVE_START_SPACE = 2048
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpackr",
3
3
  "author": "Kris Zyp",
4
- "version": "1.10.2",
4
+ "version": "1.11.1",
5
5
  "description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning",
6
6
  "license": "MIT",
7
7
  "types": "./index.d.ts",
package/struct.js CHANGED
@@ -60,7 +60,7 @@ try {
60
60
  textEncoder = new TextEncoder()
61
61
  } catch (error) {}
62
62
  const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
63
- return target.utf8Write(string, position, 0xffffffff)
63
+ return target.utf8Write(string, position, target.byteLength - position)
64
64
  } : (textEncoder && textEncoder.encodeInto) ?
65
65
  function(target, string, position) {
66
66
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -504,6 +504,8 @@ function readStruct(src, position, srcEnd, unpackr) {
504
504
  src = Uint8Array.prototype.slice.call(src, position, srcEnd);
505
505
  srcEnd -= position;
506
506
  position = 0;
507
+ if (!unpackr.getStructures)
508
+ throw new Error(`Reference to shared structure ${recordId} without getStructures method`);
507
509
  unpackr._mergeStructures(unpackr.getStructures());
508
510
  if (!unpackr.typedStructs)
509
511
  throw new Error('Could not find any shared typed structures');
package/unpack.js CHANGED
@@ -1001,7 +1001,7 @@ currentExtensions[0x42] = (data) => {
1001
1001
  let length = data.length;
1002
1002
  let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
1003
1003
  for (let i = 1; i < length; i++) {
1004
- value <<= 8n;
1004
+ value <<= BigInt(8);
1005
1005
  value += BigInt(data[i]);
1006
1006
  }
1007
1007
  return value;