msgpackr 1.5.3 → 1.5.6

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
@@ -23,7 +23,6 @@ export class Packr extends Unpackr {
23
23
  this.offset = 0
24
24
  let typeBuffer
25
25
  let start
26
- let sharedStructures
27
26
  let hasSharedUpdate
28
27
  let structures
29
28
  let referenceMap
@@ -45,10 +44,13 @@ export class Packr extends Unpackr {
45
44
  maxSharedStructures = hasSharedStructures ? 32 : 0
46
45
  if (maxSharedStructures > 8160)
47
46
  throw new Error('Maximum maxSharedStructure is 8160')
47
+ if (options.structuredClone && options.moreTypes == undefined) {
48
+ options.moreTypes = true
49
+ }
48
50
  let maxOwnStructures = options.maxOwnStructures
49
51
  if (maxOwnStructures == null)
50
52
  maxOwnStructures = hasSharedStructures ? 32 : 64
51
- if (isSequential && !options.saveStructures)
53
+ if (!this.structures && options.useRecords != false)
52
54
  this.structures = []
53
55
  // two byte record ids for shared structures
54
56
  let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64)
@@ -83,23 +85,23 @@ export class Packr extends Unpackr {
83
85
  bundledStrings.size = Infinity // force a new bundle start on first string
84
86
  } else
85
87
  bundledStrings = null
86
- sharedStructures = packr.structures
87
- if (sharedStructures) {
88
- if (sharedStructures.uninitialized)
89
- sharedStructures = packr._mergeStructures(packr.getStructures())
90
- let sharedLength = sharedStructures.sharedLength || 0
88
+ structures = packr.structures
89
+ if (structures) {
90
+ if (structures.uninitialized)
91
+ structures = packr._mergeStructures(packr.getStructures())
92
+ let sharedLength = structures.sharedLength || 0
91
93
  if (sharedLength > maxSharedStructures) {
92
- //if (maxSharedStructures <= 32 && sharedStructures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
93
- throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + sharedStructures.sharedLength)
94
+ //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
95
+ throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
94
96
  }
95
- if (!sharedStructures.transitions) {
97
+ if (!structures.transitions) {
96
98
  // rebuild our structure transitions
97
- sharedStructures.transitions = Object.create(null)
99
+ structures.transitions = Object.create(null)
98
100
  for (let i = 0; i < sharedLength; i++) {
99
- let keys = sharedStructures[i]
101
+ let keys = structures[i]
100
102
  if (!keys)
101
103
  continue
102
- let nextTransition, transition = sharedStructures.transitions
104
+ let nextTransition, transition = structures.transitions
103
105
  for (let j = 0, l = keys.length; j < l; j++) {
104
106
  let key = keys[j]
105
107
  nextTransition = transition[key]
@@ -113,12 +115,11 @@ export class Packr extends Unpackr {
113
115
  lastSharedStructuresLength = sharedLength
114
116
  }
115
117
  if (!isSequential) {
116
- sharedStructures.nextId = sharedLength + 0x40
118
+ structures.nextId = sharedLength + 0x40
117
119
  }
118
120
  }
119
121
  if (hasSharedUpdate)
120
122
  hasSharedUpdate = false
121
- structures = sharedStructures || (packr.structures = [])
122
123
  try {
123
124
  pack(value)
124
125
  if (bundledStrings) {
@@ -141,14 +142,15 @@ export class Packr extends Unpackr {
141
142
  }
142
143
  return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
143
144
  } finally {
144
- if (sharedStructures) {
145
+ if (structures) {
145
146
  if (serializationsSinceTransitionRebuild < 10)
146
147
  serializationsSinceTransitionRebuild++
147
- if (sharedStructures.length > maxSharedStructures)
148
- sharedStructures.length = maxSharedStructures
148
+ let sharedLength = structures.sharedLength || maxSharedStructures
149
+ if (structures.length > sharedLength)
150
+ structures.length = sharedLength
149
151
  if (transitionsCount > 10000) {
150
152
  // force a rebuild occasionally after a lot of transitions so it can get cleaned up
151
- sharedStructures.transitions = null
153
+ structures.transitions = null
152
154
  serializationsSinceTransitionRebuild = 0
153
155
  transitionsCount = 0
154
156
  if (recordIdsToRemove.length > 0)
@@ -160,13 +162,9 @@ export class Packr extends Unpackr {
160
162
  recordIdsToRemove = []
161
163
  }
162
164
  if (hasSharedUpdate && packr.saveStructures) {
163
- let sharedLength = sharedStructures.sharedLength || maxSharedStructures
164
- if (sharedStructures.length > sharedLength) {
165
- sharedStructures = sharedStructures.slice(0, sharedLength)
166
- }
167
165
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
168
166
  let returnBuffer = target.subarray(start, position)
169
- if (packr.saveStructures(sharedStructures, lastSharedStructuresLength) === false) {
167
+ if (packr.saveStructures(structures, lastSharedStructuresLength) === false) {
170
168
  // get updated structures and try again if the update failed
171
169
  packr._mergeStructures(packr.getStructures())
172
170
  return packr.pack(value)
@@ -746,8 +744,8 @@ extensions = [{
746
744
  }, {
747
745
  pack(set, allocateForWrite, pack) {
748
746
  let array = Array.from(set)
749
- let { target, position} = allocateForWrite(this.structuredClone ? 3 : 0)
750
- if (this.structuredClone) {
747
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
748
+ if (this.moreTypes) {
751
749
  target[position++] = 0xd4
752
750
  target[position++] = 0x73 // 's' for Set
753
751
  target[position++] = 0
@@ -756,8 +754,8 @@ extensions = [{
756
754
  }
757
755
  }, {
758
756
  pack(error, allocateForWrite, pack) {
759
- let { target, position} = allocateForWrite(this.structuredClone ? 3 : 0)
760
- if (this.structuredClone) {
757
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
758
+ if (this.moreTypes) {
761
759
  target[position++] = 0xd4
762
760
  target[position++] = 0x65 // 'e' for error
763
761
  target[position++] = 0
@@ -766,8 +764,8 @@ extensions = [{
766
764
  }
767
765
  }, {
768
766
  pack(regex, allocateForWrite, pack) {
769
- let { target, position} = allocateForWrite(this.structuredClone ? 3 : 0)
770
- if (this.structuredClone) {
767
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
768
+ if (this.moreTypes) {
771
769
  target[position++] = 0xd4
772
770
  target[position++] = 0x78 // 'x' for regeXp
773
771
  target[position++] = 0
@@ -776,7 +774,7 @@ extensions = [{
776
774
  }
777
775
  }, {
778
776
  pack(arrayBuffer, allocateForWrite) {
779
- if (this.structuredClone)
777
+ if (this.moreTypes)
780
778
  writeExtBuffer(arrayBuffer, 0x10, allocateForWrite)
781
779
  else
782
780
  writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite)
@@ -784,7 +782,7 @@ extensions = [{
784
782
  }, {
785
783
  pack(typedArray, allocateForWrite) {
786
784
  let constructor = typedArray.constructor
787
- if (constructor !== ByteArray && this.structuredClone)
785
+ if (constructor !== ByteArray && this.moreTypes)
788
786
  writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite)
789
787
  else
790
788
  writeBuffer(typedArray, allocateForWrite)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpackr",
3
3
  "author": "Kris Zyp",
4
- "version": "1.5.3",
4
+ "version": "1.5.6",
5
5
  "description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning",
6
6
  "license": "MIT",
7
7
  "types": "./index.d.ts",
@@ -56,7 +56,7 @@
56
56
  "/*.ts"
57
57
  ],
58
58
  "optionalDependencies": {
59
- "msgpackr-extract": "^1.0.14"
59
+ "msgpackr-extract": "^1.1.4"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@rollup/plugin-json": "^4.1.0",
package/unpack.d.ts CHANGED
@@ -9,6 +9,7 @@ export interface Options {
9
9
  useFloat32?: FLOAT32_OPTIONS
10
10
  useRecords?: boolean
11
11
  structures?: {}[]
12
+ moreTypes?: boolean
12
13
  structuredClone?: boolean
13
14
  mapsAsObjects?: boolean
14
15
  variableMapSize?: boolean
@@ -19,6 +20,7 @@ export interface Options {
19
20
  encodeUndefinedAsNil?: boolean
20
21
  maxSharedStructures?: number
21
22
  maxOwnStructures?: number
23
+ int64AsNumber?: boolean
22
24
  shouldShareStructure?: (keys: string[]) => boolean
23
25
  getStructures?(): {}[]
24
26
  saveStructures?(structures: {}[]): boolean | void
package/unpack.js CHANGED
@@ -27,6 +27,13 @@ export class C1Type {}
27
27
  export const C1 = new C1Type()
28
28
  C1.name = 'MessagePack 0xC1'
29
29
  var sequentialMode = false
30
+ var inlineObjectReadThreshold = 2
31
+ try {
32
+ new Function('')
33
+ } catch(error) {
34
+ // if eval variants are not supported, do not create inline object readers ever
35
+ inlineObjectReadThreshold = Infinity
36
+ }
30
37
 
31
38
  export class Unpackr {
32
39
  constructor(options) {
@@ -440,7 +447,7 @@ const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/
440
447
  function createStructureReader(structure, firstId) {
441
448
  function readObject() {
442
449
  // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
443
- if (readObject.count++ > 2) {
450
+ if (readObject.count++ > inlineObjectReadThreshold) {
444
451
  let readObject = structure.read = (new Function('r', 'return function(){return {' + structure.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}}'))(read)
445
452
  if (structure.highByte === 0)
446
453
  structure.read = createSecondByteReader(firstId, structure.read)