msgpackr 1.10.2 → 1.11.0

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/index.d.cts CHANGED
@@ -38,22 +38,23 @@ interface Extension {
38
38
  Class?: Function
39
39
  type?: number
40
40
  pack?(value: any): Buffer | Uint8Array
41
- unpack?(messagePack: Buffer | Uint8Array): any
41
+ unpack?(messagePack: Buffer | Uint8Array): any
42
42
  read?(datum: any): any
43
43
  write?(instance: any): any
44
44
  }
45
+ export type UnpackOptions = { start?: number; end?: number; lazy?: boolean; } | number;
45
46
  export class Unpackr {
46
47
  constructor(options?: Options)
47
- unpack(messagePack: Buffer | Uint8Array): any
48
- decode(messagePack: Buffer | Uint8Array): any
48
+ unpack(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any
49
+ decode(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any
49
50
  unpackMultiple(messagePack: Buffer | Uint8Array): any[]
50
51
  unpackMultiple(messagePack: Buffer | Uint8Array, forEach: (value: any, start?: number, end?: number) => any): void
51
52
  }
52
53
  export class Decoder extends Unpackr {}
53
- export function unpack(messagePack: Buffer | Uint8Array): any
54
+ export function unpack(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any
54
55
  export function unpackMultiple(messagePack: Buffer | Uint8Array): any[]
55
56
  export function unpackMultiple(messagePack: Buffer | Uint8Array, forEach: (value: any, start?: number, end?: number) => any): void
56
- export function decode(messagePack: Buffer | Uint8Array): any
57
+ export function decode(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any
57
58
  export function addExtension(extension: Extension): void
58
59
  export function clearSource(): void
59
60
  export function roundFloat32(float32Number: number): number
@@ -61,12 +62,20 @@ export const C1: {}
61
62
  export let isNativeAccelerationEnabled: boolean
62
63
 
63
64
  export class Packr extends Unpackr {
64
- pack(value: any): Buffer
65
- encode(value: any): Buffer
65
+ offset: number;
66
+ position: number;
67
+ pack(value: any, encodeOptions?: number): Buffer
68
+ encode(value: any, encodeOptions?: number): Buffer
69
+ useBuffer(buffer: Buffer | Uint8Array): void;
70
+ clearSharedData(): void;
66
71
  }
67
72
  export class Encoder extends Packr {}
68
- export function pack(value: any): Buffer
69
- export function encode(value: any): Buffer
73
+ export function pack(value: any, encodeOptions?: number): Buffer
74
+ export function encode(value: any, encodeOptions?: number): Buffer
75
+
76
+ export const REUSE_BUFFER_MODE: number;
77
+ export const RESET_BUFFER_MODE: number;
78
+ export const RESERVE_START_SPACE: number;
70
79
 
71
80
  import { Transform, Readable } from 'stream'
72
81
 
package/index.d.ts CHANGED
@@ -38,22 +38,23 @@ interface Extension {
38
38
  Class?: Function
39
39
  type?: number
40
40
  pack?(value: any): Buffer | Uint8Array
41
- unpack?(messagePack: Buffer | Uint8Array): any
41
+ unpack?(messagePack: Buffer | Uint8Array): any
42
42
  read?(datum: any): any
43
43
  write?(instance: any): any
44
44
  }
45
+ export type UnpackOptions = { start?: number; end?: number; lazy?: boolean; } | number;
45
46
  export class Unpackr {
46
47
  constructor(options?: Options)
47
- unpack(messagePack: Buffer | Uint8Array): any
48
- decode(messagePack: Buffer | Uint8Array): any
48
+ unpack(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any
49
+ decode(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any
49
50
  unpackMultiple(messagePack: Buffer | Uint8Array): any[]
50
51
  unpackMultiple(messagePack: Buffer | Uint8Array, forEach: (value: any, start?: number, end?: number) => any): void
51
52
  }
52
53
  export class Decoder extends Unpackr {}
53
- export function unpack(messagePack: Buffer | Uint8Array): any
54
+ export function unpack(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any
54
55
  export function unpackMultiple(messagePack: Buffer | Uint8Array): any[]
55
56
  export function unpackMultiple(messagePack: Buffer | Uint8Array, forEach: (value: any, start?: number, end?: number) => any): void
56
- export function decode(messagePack: Buffer | Uint8Array): any
57
+ export function decode(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any
57
58
  export function addExtension(extension: Extension): void
58
59
  export function clearSource(): void
59
60
  export function roundFloat32(float32Number: number): number
@@ -61,12 +62,20 @@ export const C1: {}
61
62
  export let isNativeAccelerationEnabled: boolean
62
63
 
63
64
  export class Packr extends Unpackr {
64
- pack(value: any): Buffer
65
- encode(value: any): Buffer
65
+ offset: number;
66
+ position: number;
67
+ pack(value: any, encodeOptions?: number): Buffer
68
+ encode(value: any, encodeOptions?: number): Buffer
69
+ useBuffer(buffer: Buffer | Uint8Array): void;
70
+ clearSharedData(): void;
66
71
  }
67
72
  export class Encoder extends Packr {}
68
- export function pack(value: any): Buffer
69
- export function encode(value: any): Buffer
73
+ export function pack(value: any, encodeOptions?: number): Buffer
74
+ export function encode(value: any, encodeOptions?: number): Buffer
75
+
76
+ export const REUSE_BUFFER_MODE: number;
77
+ export const RESET_BUFFER_MODE: number;
78
+ export const RESERVE_START_SPACE: number;
70
79
 
71
80
  import { Transform, Readable } from 'stream'
72
81
 
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { Packr, Encoder, addExtension, pack, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './pack.js'
1
+ export { Packr, Encoder, addExtension, pack, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE, RESET_BUFFER_MODE, RESERVE_START_SPACE } from './pack.js'
2
2
  export { Unpackr, Decoder, C1, unpack, unpackMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './unpack.js'
3
3
  export { decodeIter, encodeIter } from './iterators.js'
4
4
  export const useRecords = false
package/pack.js CHANGED
@@ -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
  }
@@ -572,9 +572,19 @@ export class Packr extends Unpackr {
572
572
  }
573
573
  }
574
574
 
575
- const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
575
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
576
576
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
577
- let keys = Object.keys(object)
577
+ let keys;
578
+ if (this.skipValues) {
579
+ keys = [];
580
+ for (let key in object) {
581
+ if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
582
+ !this.skipValues.includes(object[key]))
583
+ keys.push(key);
584
+ }
585
+ } else {
586
+ keys = Object.keys(object)
587
+ }
578
588
  let length = keys.length
579
589
  if (length < 0x10) {
580
590
  target[position++] = 0x80 | length
@@ -693,9 +703,9 @@ export class Packr extends Unpackr {
693
703
  }
694
704
  }
695
705
 
696
- // craete reference to useRecords if useRecords is a function
706
+ // create reference to useRecords if useRecords is a function
697
707
  const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
698
-
708
+
699
709
  const writeObject = checkUseRecords ? (object) => {
700
710
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object)
701
711
  } : writeRecord
@@ -823,9 +833,15 @@ export class Packr extends Unpackr {
823
833
  useBuffer(buffer) {
824
834
  // this means we are finished using our own buffer and we can write over it safely
825
835
  target = buffer
826
- targetView = new DataView(target.buffer, target.byteOffset, target.byteLength)
836
+ target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength))
827
837
  position = 0
828
838
  }
839
+ set position (value) {
840
+ position = value;
841
+ }
842
+ get position() {
843
+ return position;
844
+ }
829
845
  clearSharedData() {
830
846
  if (this.structures)
831
847
  this.structures = []
@@ -834,12 +850,6 @@ export class Packr extends Unpackr {
834
850
  }
835
851
  }
836
852
 
837
- function copyBinary(source, target, targetOffset, offset, endOffset) {
838
- while (offset < endOffset) {
839
- target[targetOffset++] = source[offset++]
840
- }
841
- }
842
-
843
853
  extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, C1Type ]
844
854
  extensions = [{
845
855
  pack(date, allocateForWrite, pack) {
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.0",
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
@@ -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');