msgpackr 1.11.11 → 2.0.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/README.md +16 -0
- package/dist/index-no-eval.cjs +209 -245
- package/dist/index-no-eval.cjs.map +1 -1
- package/dist/index-no-eval.min.js +1 -1
- package/dist/index-no-eval.min.js.map +1 -1
- package/dist/index.js +209 -245
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +245 -1072
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +246 -1137
- package/dist/test.js.map +1 -1
- package/dist/unpack-no-eval.cjs +167 -186
- package/dist/unpack-no-eval.cjs.map +1 -1
- package/index.js +5 -5
- package/iterators.js +28 -28
- package/node-index.js +12 -13
- package/pack.js +580 -611
- package/package.json +1 -1
- package/stream.js +37 -32
- package/unpack.js +612 -630
- package/struct.js +0 -815
package/pack.js
CHANGED
|
@@ -1,139 +1,131 @@
|
|
|
1
|
-
import { Unpackr, mult10, C1Type, typedArrays, addExtension as unpackAddExtension } from './unpack.js'
|
|
2
|
-
let textEncoder
|
|
1
|
+
import { Unpackr, mult10, C1Type, typedArrays, addExtension as unpackAddExtension } from './unpack.js';
|
|
2
|
+
let textEncoder;
|
|
3
3
|
try {
|
|
4
|
-
textEncoder = new TextEncoder()
|
|
4
|
+
textEncoder = new TextEncoder();
|
|
5
5
|
} catch (error) {}
|
|
6
|
-
let extensions, extensionClasses
|
|
7
|
-
const hasNodeBuffer = typeof Buffer !== 'undefined'
|
|
6
|
+
let extensions, extensionClasses;
|
|
7
|
+
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
8
8
|
const ByteArrayAllocate = hasNodeBuffer ?
|
|
9
|
-
function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array
|
|
10
|
-
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array
|
|
11
|
-
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000
|
|
12
|
-
let target, keysTarget
|
|
13
|
-
let targetView
|
|
14
|
-
let position = 0
|
|
15
|
-
let safeEnd
|
|
16
|
-
let bundledStrings = null
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
export const RECORD_SYMBOL = Symbol('record-id')
|
|
9
|
+
function(length) { return Buffer.allocUnsafeSlow(length); } : Uint8Array;
|
|
10
|
+
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
11
|
+
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
12
|
+
let target, keysTarget;
|
|
13
|
+
let targetView;
|
|
14
|
+
let position = 0;
|
|
15
|
+
let safeEnd;
|
|
16
|
+
let bundledStrings = null;
|
|
17
|
+
const MAX_BUNDLE_SIZE = 0x5500; // maximum characters such that the encoded bytes fits in 16 bits.
|
|
18
|
+
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
19
|
+
export const RECORD_SYMBOL = Symbol('record-id');
|
|
21
20
|
export class Packr extends Unpackr {
|
|
22
21
|
constructor(options) {
|
|
23
|
-
super(options)
|
|
24
|
-
this.offset = 0
|
|
25
|
-
let typeBuffer
|
|
26
|
-
let start
|
|
27
|
-
let hasSharedUpdate
|
|
28
|
-
let structures
|
|
29
|
-
let referenceMap
|
|
22
|
+
super(options);
|
|
23
|
+
this.offset = 0;
|
|
24
|
+
let typeBuffer;
|
|
25
|
+
let start;
|
|
26
|
+
let hasSharedUpdate;
|
|
27
|
+
let structures;
|
|
28
|
+
let referenceMap;
|
|
30
29
|
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
|
|
31
|
-
return target.utf8Write(string, position, target.byteLength - position)
|
|
30
|
+
return target.utf8Write(string, position, target.byteLength - position);
|
|
32
31
|
} : (textEncoder && textEncoder.encodeInto) ?
|
|
33
32
|
function(string, position) {
|
|
34
|
-
return textEncoder.encodeInto(string, target.subarray(position)).written
|
|
35
|
-
} : false
|
|
33
|
+
return textEncoder.encodeInto(string, target.subarray(position)).written;
|
|
34
|
+
} : false;
|
|
36
35
|
|
|
37
|
-
let packr = this
|
|
36
|
+
let packr = this;
|
|
38
37
|
if (!options)
|
|
39
|
-
options = {}
|
|
40
|
-
let isSequential = options && options.sequential
|
|
41
|
-
let hasSharedStructures = options.structures || options.saveStructures
|
|
42
|
-
let maxSharedStructures = options.maxSharedStructures
|
|
38
|
+
options = {};
|
|
39
|
+
let isSequential = options && options.sequential;
|
|
40
|
+
let hasSharedStructures = options.structures || options.saveStructures;
|
|
41
|
+
let maxSharedStructures = options.maxSharedStructures;
|
|
43
42
|
if (maxSharedStructures == null)
|
|
44
|
-
maxSharedStructures = hasSharedStructures ? 32 : 0
|
|
43
|
+
maxSharedStructures = hasSharedStructures ? 32 : 0;
|
|
45
44
|
if (maxSharedStructures > 8160)
|
|
46
|
-
throw new Error('Maximum maxSharedStructure is 8160')
|
|
45
|
+
throw new Error('Maximum maxSharedStructure is 8160');
|
|
47
46
|
if (options.structuredClone && options.moreTypes == undefined) {
|
|
48
|
-
this.moreTypes = true
|
|
47
|
+
this.moreTypes = true;
|
|
49
48
|
}
|
|
50
|
-
let maxOwnStructures = options.maxOwnStructures
|
|
49
|
+
let maxOwnStructures = options.maxOwnStructures;
|
|
51
50
|
if (maxOwnStructures == null)
|
|
52
|
-
maxOwnStructures = hasSharedStructures ? 32 : 64
|
|
51
|
+
maxOwnStructures = hasSharedStructures ? 32 : 64;
|
|
53
52
|
if (!this.structures && options.useRecords != false)
|
|
54
|
-
this.structures = []
|
|
53
|
+
this.structures = [];
|
|
55
54
|
// two byte record ids for shared structures
|
|
56
|
-
let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64)
|
|
57
|
-
let sharedLimitId = maxSharedStructures + 0x40
|
|
58
|
-
let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40
|
|
55
|
+
let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
|
|
56
|
+
let sharedLimitId = maxSharedStructures + 0x40;
|
|
57
|
+
let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
|
|
59
58
|
if (maxStructureId > 8256) {
|
|
60
|
-
throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
|
|
59
|
+
throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192');
|
|
61
60
|
}
|
|
62
|
-
let recordIdsToRemove = []
|
|
63
|
-
let transitionsCount = 0
|
|
64
|
-
let serializationsSinceTransitionRebuild = 0
|
|
61
|
+
let recordIdsToRemove = [];
|
|
62
|
+
let transitionsCount = 0;
|
|
63
|
+
let serializationsSinceTransitionRebuild = 0;
|
|
65
64
|
|
|
66
65
|
this.pack = this.encode = function(value, encodeOptions) {
|
|
67
66
|
if (!target) {
|
|
68
|
-
target = new ByteArrayAllocate(8192)
|
|
69
|
-
targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192))
|
|
70
|
-
position = 0
|
|
67
|
+
target = new ByteArrayAllocate(8192);
|
|
68
|
+
targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
|
|
69
|
+
position = 0;
|
|
71
70
|
}
|
|
72
|
-
safeEnd = target.length - 10
|
|
71
|
+
safeEnd = target.length - 10;
|
|
73
72
|
if (safeEnd - position < 0x800) {
|
|
74
73
|
// don't start too close to the end,
|
|
75
|
-
target = new ByteArrayAllocate(target.length)
|
|
76
|
-
targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))
|
|
77
|
-
safeEnd = target.length - 10
|
|
78
|
-
position = 0
|
|
74
|
+
target = new ByteArrayAllocate(target.length);
|
|
75
|
+
targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
|
|
76
|
+
safeEnd = target.length - 10;
|
|
77
|
+
position = 0;
|
|
79
78
|
} else
|
|
80
|
-
position = (position + 7) & 0x7ffffff8 // Word align to make any future copying of this buffer faster
|
|
81
|
-
start = position
|
|
82
|
-
if (encodeOptions & RESERVE_START_SPACE) position += (encodeOptions & 0xff)
|
|
83
|
-
referenceMap = packr.structuredClone ? new Map() : null
|
|
79
|
+
position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
|
|
80
|
+
start = position;
|
|
81
|
+
if (encodeOptions & RESERVE_START_SPACE) position += (encodeOptions & 0xff);
|
|
82
|
+
referenceMap = packr.structuredClone ? new Map() : null;
|
|
84
83
|
if (packr.bundleStrings && typeof value !== 'string') {
|
|
85
|
-
bundledStrings = []
|
|
86
|
-
bundledStrings.size = Infinity // force a new bundle start on first string
|
|
84
|
+
bundledStrings = [];
|
|
85
|
+
bundledStrings.size = Infinity; // force a new bundle start on first string
|
|
87
86
|
} else
|
|
88
|
-
bundledStrings = null
|
|
89
|
-
structures = packr.structures
|
|
87
|
+
bundledStrings = null;
|
|
88
|
+
structures = packr.structures;
|
|
90
89
|
if (structures) {
|
|
91
90
|
if (structures.uninitialized)
|
|
92
|
-
structures = packr._mergeStructures(packr.getStructures())
|
|
93
|
-
let sharedLength = structures.sharedLength || 0
|
|
91
|
+
structures = packr._mergeStructures(packr.getStructures());
|
|
92
|
+
let sharedLength = structures.sharedLength || 0;
|
|
94
93
|
if (sharedLength > maxSharedStructures) {
|
|
95
94
|
//if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
|
|
96
|
-
throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
|
|
95
|
+
throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength);
|
|
97
96
|
}
|
|
98
97
|
if (!structures.transitions) {
|
|
99
98
|
// rebuild our structure transitions
|
|
100
|
-
structures.transitions = Object.create(null)
|
|
99
|
+
structures.transitions = Object.create(null);
|
|
101
100
|
for (let i = 0; i < sharedLength; i++) {
|
|
102
|
-
let keys = structures[i]
|
|
101
|
+
let keys = structures[i];
|
|
103
102
|
if (!keys)
|
|
104
|
-
continue
|
|
105
|
-
let nextTransition, transition = structures.transitions
|
|
103
|
+
continue;
|
|
104
|
+
let nextTransition, transition = structures.transitions;
|
|
106
105
|
for (let j = 0, l = keys.length; j < l; j++) {
|
|
107
|
-
let key = keys[j]
|
|
108
|
-
nextTransition = transition[key]
|
|
106
|
+
let key = keys[j];
|
|
107
|
+
nextTransition = transition[key];
|
|
109
108
|
if (!nextTransition) {
|
|
110
|
-
nextTransition = transition[key] = Object.create(null)
|
|
109
|
+
nextTransition = transition[key] = Object.create(null);
|
|
111
110
|
}
|
|
112
|
-
transition = nextTransition
|
|
111
|
+
transition = nextTransition;
|
|
113
112
|
}
|
|
114
|
-
transition[RECORD_SYMBOL] = i + 0x40
|
|
113
|
+
transition[RECORD_SYMBOL] = i + 0x40;
|
|
115
114
|
}
|
|
116
|
-
this.lastNamedStructuresLength = sharedLength
|
|
115
|
+
this.lastNamedStructuresLength = sharedLength;
|
|
117
116
|
}
|
|
118
117
|
if (!isSequential) {
|
|
119
|
-
structures.nextId = sharedLength + 0x40
|
|
118
|
+
structures.nextId = sharedLength + 0x40;
|
|
120
119
|
}
|
|
121
120
|
}
|
|
122
121
|
if (hasSharedUpdate)
|
|
123
|
-
hasSharedUpdate = false
|
|
122
|
+
hasSharedUpdate = false;
|
|
124
123
|
let encodingError;
|
|
125
124
|
try {
|
|
126
|
-
|
|
127
|
-
if (value.constructor === Object) writeStruct(value); // simple object
|
|
128
|
-
else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
|
|
129
|
-
// allow user classes, if they don't need special handling (but do use toJSON if available)
|
|
130
|
-
writeStruct(value.toJSON ? value.toJSON() : value);
|
|
131
|
-
} else pack(value)
|
|
132
|
-
} else
|
|
133
|
-
pack(value)
|
|
125
|
+
pack(value);
|
|
134
126
|
let lastBundle = bundledStrings;
|
|
135
127
|
if (bundledStrings)
|
|
136
|
-
writeBundles(start, pack, 0)
|
|
128
|
+
writeBundles(start, pack, 0);
|
|
137
129
|
if (referenceMap && referenceMap.idsToInsert) {
|
|
138
130
|
let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
|
|
139
131
|
let i = idsToInsert.length;
|
|
@@ -149,7 +141,7 @@ export class Packr extends Unpackr {
|
|
|
149
141
|
if (incrementPosition >= 0) {
|
|
150
142
|
// update the bundle reference now
|
|
151
143
|
targetView.setUint32(lastBundle.position + start,
|
|
152
|
-
targetView.getUint32(lastBundle.position + start) + incrementPosition)
|
|
144
|
+
targetView.getUint32(lastBundle.position + start) + incrementPosition);
|
|
153
145
|
incrementPosition = -1; // reset
|
|
154
146
|
}
|
|
155
147
|
lastBundle = lastBundle.previous;
|
|
@@ -159,23 +151,23 @@ export class Packr extends Unpackr {
|
|
|
159
151
|
if (incrementPosition >= 0 && lastBundle) {
|
|
160
152
|
// update the bundle reference now
|
|
161
153
|
targetView.setUint32(lastBundle.position + start,
|
|
162
|
-
targetView.getUint32(lastBundle.position + start) + incrementPosition)
|
|
154
|
+
targetView.getUint32(lastBundle.position + start) + incrementPosition);
|
|
163
155
|
}
|
|
164
156
|
position += idsToInsert.length * 6;
|
|
165
157
|
if (position > safeEnd)
|
|
166
|
-
makeRoom(position)
|
|
167
|
-
packr.offset = position
|
|
168
|
-
let serialized = insertIds(target.subarray(start, position), idsToInsert)
|
|
169
|
-
referenceMap = null
|
|
170
|
-
return serialized
|
|
158
|
+
makeRoom(position);
|
|
159
|
+
packr.offset = position;
|
|
160
|
+
let serialized = insertIds(target.subarray(start, position), idsToInsert);
|
|
161
|
+
referenceMap = null;
|
|
162
|
+
return serialized;
|
|
171
163
|
}
|
|
172
|
-
packr.offset = position // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
164
|
+
packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
173
165
|
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
174
|
-
target.start = start
|
|
175
|
-
target.end = position
|
|
176
|
-
return target
|
|
166
|
+
target.start = start;
|
|
167
|
+
target.end = position;
|
|
168
|
+
return target;
|
|
177
169
|
}
|
|
178
|
-
return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
|
|
170
|
+
return target.subarray(start, position); // position can change if we call pack again in saveStructures, so we get the buffer now
|
|
179
171
|
} catch(error) {
|
|
180
172
|
encodingError = error;
|
|
181
173
|
throw error;
|
|
@@ -183,336 +175,336 @@ export class Packr extends Unpackr {
|
|
|
183
175
|
if (structures) {
|
|
184
176
|
resetStructures();
|
|
185
177
|
if (hasSharedUpdate && packr.saveStructures) {
|
|
186
|
-
let sharedLength = structures.sharedLength || 0
|
|
178
|
+
let sharedLength = structures.sharedLength || 0;
|
|
187
179
|
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
188
|
-
let returnBuffer = target.subarray(start, position)
|
|
180
|
+
let returnBuffer = target.subarray(start, position);
|
|
189
181
|
let newSharedData = prepareStructures(structures, packr);
|
|
190
182
|
if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
|
|
191
183
|
if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
|
|
192
184
|
// get updated structures and try again if the update failed
|
|
193
|
-
return packr.pack(value, encodeOptions)
|
|
185
|
+
return packr.pack(value, encodeOptions);
|
|
194
186
|
}
|
|
195
|
-
packr.lastNamedStructuresLength = sharedLength
|
|
187
|
+
packr.lastNamedStructuresLength = sharedLength;
|
|
196
188
|
// don't keep large buffers around
|
|
197
|
-
if (target.length > 0x40000000) target = null
|
|
198
|
-
return returnBuffer
|
|
189
|
+
if (target.length > 0x40000000) target = null;
|
|
190
|
+
return returnBuffer;
|
|
199
191
|
}
|
|
200
192
|
}
|
|
201
193
|
}
|
|
202
194
|
// don't keep large buffers around, they take too much memory and cause problems (limit at 1GB)
|
|
203
|
-
if (target.length > 0x40000000) target = null
|
|
195
|
+
if (target.length > 0x40000000) target = null;
|
|
204
196
|
if (encodeOptions & RESET_BUFFER_MODE)
|
|
205
|
-
position = start
|
|
197
|
+
position = start;
|
|
206
198
|
}
|
|
207
|
-
}
|
|
199
|
+
};
|
|
208
200
|
const resetStructures = () => {
|
|
209
201
|
if (serializationsSinceTransitionRebuild < 10)
|
|
210
|
-
serializationsSinceTransitionRebuild
|
|
211
|
-
let sharedLength = structures.sharedLength || 0
|
|
202
|
+
serializationsSinceTransitionRebuild++;
|
|
203
|
+
let sharedLength = structures.sharedLength || 0;
|
|
212
204
|
if (structures.length > sharedLength && !isSequential)
|
|
213
|
-
structures.length = sharedLength
|
|
205
|
+
structures.length = sharedLength;
|
|
214
206
|
if (transitionsCount > 10000) {
|
|
215
207
|
// force a rebuild occasionally after a lot of transitions so it can get cleaned up
|
|
216
|
-
structures.transitions = null
|
|
217
|
-
serializationsSinceTransitionRebuild = 0
|
|
218
|
-
transitionsCount = 0
|
|
208
|
+
structures.transitions = null;
|
|
209
|
+
serializationsSinceTransitionRebuild = 0;
|
|
210
|
+
transitionsCount = 0;
|
|
219
211
|
if (recordIdsToRemove.length > 0)
|
|
220
|
-
recordIdsToRemove = []
|
|
212
|
+
recordIdsToRemove = [];
|
|
221
213
|
} else if (recordIdsToRemove.length > 0 && !isSequential) {
|
|
222
214
|
for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
|
|
223
|
-
recordIdsToRemove[i][RECORD_SYMBOL] = 0
|
|
215
|
+
recordIdsToRemove[i][RECORD_SYMBOL] = 0;
|
|
224
216
|
}
|
|
225
|
-
recordIdsToRemove = []
|
|
217
|
+
recordIdsToRemove = [];
|
|
226
218
|
}
|
|
227
|
-
}
|
|
219
|
+
};
|
|
228
220
|
const packArray = (value) => {
|
|
229
|
-
var length = value.length
|
|
221
|
+
var length = value.length;
|
|
230
222
|
if (length < 0x10) {
|
|
231
|
-
target[position++] = 0x90 | length
|
|
223
|
+
target[position++] = 0x90 | length;
|
|
232
224
|
} else if (length < 0x10000) {
|
|
233
|
-
target[position++] = 0xdc
|
|
234
|
-
target[position++] = length >> 8
|
|
235
|
-
target[position++] = length & 0xff
|
|
225
|
+
target[position++] = 0xdc;
|
|
226
|
+
target[position++] = length >> 8;
|
|
227
|
+
target[position++] = length & 0xff;
|
|
236
228
|
} else {
|
|
237
|
-
target[position++] = 0xdd
|
|
238
|
-
targetView.setUint32(position, length)
|
|
239
|
-
position += 4
|
|
229
|
+
target[position++] = 0xdd;
|
|
230
|
+
targetView.setUint32(position, length);
|
|
231
|
+
position += 4;
|
|
240
232
|
}
|
|
241
233
|
for (let i = 0; i < length; i++) {
|
|
242
|
-
pack(value[i])
|
|
234
|
+
pack(value[i]);
|
|
243
235
|
}
|
|
244
|
-
}
|
|
236
|
+
};
|
|
245
237
|
const pack = (value) => {
|
|
246
238
|
if (position > safeEnd)
|
|
247
|
-
target = makeRoom(position)
|
|
239
|
+
target = makeRoom(position);
|
|
248
240
|
|
|
249
|
-
var type = typeof value
|
|
250
|
-
var length
|
|
241
|
+
var type = typeof value;
|
|
242
|
+
var length;
|
|
251
243
|
if (type === 'string') {
|
|
252
|
-
let strLength = value.length
|
|
244
|
+
let strLength = value.length;
|
|
253
245
|
if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
|
|
254
246
|
if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
255
|
-
let extStart
|
|
256
|
-
let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10
|
|
247
|
+
let extStart;
|
|
248
|
+
let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
|
|
257
249
|
if (position + maxBytes > safeEnd)
|
|
258
|
-
target = makeRoom(position + maxBytes)
|
|
259
|
-
let lastBundle
|
|
250
|
+
target = makeRoom(position + maxBytes);
|
|
251
|
+
let lastBundle;
|
|
260
252
|
if (bundledStrings.position) { // here we use the 0x62 extension to write the last bundle and reserve space for the reference pointer to the next/current bundle
|
|
261
|
-
lastBundle = bundledStrings
|
|
262
|
-
target[position] = 0xc8 // ext 16
|
|
263
|
-
position += 3 // reserve for the writing bundle size
|
|
264
|
-
target[position++] = 0x62 // 'b'
|
|
265
|
-
extStart = position - start
|
|
266
|
-
position += 4 // reserve for writing bundle reference
|
|
267
|
-
writeBundles(start, pack, 0) // write the last bundles
|
|
268
|
-
targetView.setUint16(extStart + start - 3, position - start - extStart)
|
|
253
|
+
lastBundle = bundledStrings;
|
|
254
|
+
target[position] = 0xc8; // ext 16
|
|
255
|
+
position += 3; // reserve for the writing bundle size
|
|
256
|
+
target[position++] = 0x62; // 'b'
|
|
257
|
+
extStart = position - start;
|
|
258
|
+
position += 4; // reserve for writing bundle reference
|
|
259
|
+
writeBundles(start, pack, 0); // write the last bundles
|
|
260
|
+
targetView.setUint16(extStart + start - 3, position - start - extStart);
|
|
269
261
|
} else { // here we use the 0x62 extension just to reserve the space for the reference pointer to the bundle (will be updated once the bundle is written)
|
|
270
|
-
target[position++] = 0xd6 // fixext 4
|
|
271
|
-
target[position++] = 0x62 // 'b'
|
|
272
|
-
extStart = position - start
|
|
273
|
-
position += 4 // reserve for writing bundle reference
|
|
262
|
+
target[position++] = 0xd6; // fixext 4
|
|
263
|
+
target[position++] = 0x62; // 'b'
|
|
264
|
+
extStart = position - start;
|
|
265
|
+
position += 4; // reserve for writing bundle reference
|
|
274
266
|
}
|
|
275
|
-
bundledStrings = ['', ''] // create new ones
|
|
267
|
+
bundledStrings = ['', '']; // create new ones
|
|
276
268
|
bundledStrings.previous = lastBundle;
|
|
277
|
-
bundledStrings.size = 0
|
|
278
|
-
bundledStrings.position = extStart
|
|
269
|
+
bundledStrings.size = 0;
|
|
270
|
+
bundledStrings.position = extStart;
|
|
279
271
|
}
|
|
280
|
-
let twoByte = hasNonLatin.test(value)
|
|
281
|
-
bundledStrings[twoByte ? 0 : 1] += value
|
|
282
|
-
target[position++] = 0xc1
|
|
272
|
+
let twoByte = hasNonLatin.test(value);
|
|
273
|
+
bundledStrings[twoByte ? 0 : 1] += value;
|
|
274
|
+
target[position++] = 0xc1;
|
|
283
275
|
pack(twoByte ? -strLength : strLength);
|
|
284
|
-
return
|
|
276
|
+
return;
|
|
285
277
|
}
|
|
286
|
-
let headerSize
|
|
278
|
+
let headerSize;
|
|
287
279
|
// first we estimate the header size, so we can write to the correct location
|
|
288
280
|
if (strLength < 0x20) {
|
|
289
|
-
headerSize = 1
|
|
281
|
+
headerSize = 1;
|
|
290
282
|
} else if (strLength < 0x100) {
|
|
291
|
-
headerSize = 2
|
|
283
|
+
headerSize = 2;
|
|
292
284
|
} else if (strLength < 0x10000) {
|
|
293
|
-
headerSize = 3
|
|
285
|
+
headerSize = 3;
|
|
294
286
|
} else {
|
|
295
|
-
headerSize = 5
|
|
287
|
+
headerSize = 5;
|
|
296
288
|
}
|
|
297
|
-
let maxBytes = strLength * 3
|
|
289
|
+
let maxBytes = strLength * 3;
|
|
298
290
|
if (position + maxBytes > safeEnd)
|
|
299
|
-
target = makeRoom(position + maxBytes)
|
|
291
|
+
target = makeRoom(position + maxBytes);
|
|
300
292
|
|
|
301
293
|
if (strLength < 0x40 || !encodeUtf8) {
|
|
302
|
-
let i, c1, c2, strPosition = position + headerSize
|
|
294
|
+
let i, c1, c2, strPosition = position + headerSize;
|
|
303
295
|
for (i = 0; i < strLength; i++) {
|
|
304
|
-
c1 = value.charCodeAt(i)
|
|
296
|
+
c1 = value.charCodeAt(i);
|
|
305
297
|
if (c1 < 0x80) {
|
|
306
|
-
target[strPosition++] = c1
|
|
298
|
+
target[strPosition++] = c1;
|
|
307
299
|
} else if (c1 < 0x800) {
|
|
308
|
-
target[strPosition++] = c1 >> 6 | 0xc0
|
|
309
|
-
target[strPosition++] = c1 & 0x3f | 0x80
|
|
300
|
+
target[strPosition++] = c1 >> 6 | 0xc0;
|
|
301
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
310
302
|
} else if (
|
|
311
303
|
(c1 & 0xfc00) === 0xd800 &&
|
|
312
304
|
((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
|
313
305
|
) {
|
|
314
|
-
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff)
|
|
315
|
-
i
|
|
316
|
-
target[strPosition++] = c1 >> 18 | 0xf0
|
|
317
|
-
target[strPosition++] = c1 >> 12 & 0x3f | 0x80
|
|
318
|
-
target[strPosition++] = c1 >> 6 & 0x3f | 0x80
|
|
319
|
-
target[strPosition++] = c1 & 0x3f | 0x80
|
|
306
|
+
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
307
|
+
i++;
|
|
308
|
+
target[strPosition++] = c1 >> 18 | 0xf0;
|
|
309
|
+
target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
|
|
310
|
+
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
311
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
320
312
|
} else {
|
|
321
|
-
target[strPosition++] = c1 >> 12 | 0xe0
|
|
322
|
-
target[strPosition++] = c1 >> 6 & 0x3f | 0x80
|
|
323
|
-
target[strPosition++] = c1 & 0x3f | 0x80
|
|
313
|
+
target[strPosition++] = c1 >> 12 | 0xe0;
|
|
314
|
+
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
315
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
324
316
|
}
|
|
325
317
|
}
|
|
326
|
-
length = strPosition - position - headerSize
|
|
318
|
+
length = strPosition - position - headerSize;
|
|
327
319
|
} else {
|
|
328
|
-
length = encodeUtf8(value, position + headerSize)
|
|
320
|
+
length = encodeUtf8(value, position + headerSize);
|
|
329
321
|
}
|
|
330
322
|
|
|
331
323
|
if (length < 0x20) {
|
|
332
|
-
target[position++] = 0xa0 | length
|
|
324
|
+
target[position++] = 0xa0 | length;
|
|
333
325
|
} else if (length < 0x100) {
|
|
334
326
|
if (headerSize < 2) {
|
|
335
|
-
target.copyWithin(position + 2, position + 1, position + 1 + length)
|
|
327
|
+
target.copyWithin(position + 2, position + 1, position + 1 + length);
|
|
336
328
|
}
|
|
337
|
-
target[position++] = 0xd9
|
|
338
|
-
target[position++] = length
|
|
329
|
+
target[position++] = 0xd9;
|
|
330
|
+
target[position++] = length;
|
|
339
331
|
} else if (length < 0x10000) {
|
|
340
332
|
if (headerSize < 3) {
|
|
341
|
-
target.copyWithin(position + 3, position + 2, position + 2 + length)
|
|
333
|
+
target.copyWithin(position + 3, position + 2, position + 2 + length);
|
|
342
334
|
}
|
|
343
|
-
target[position++] = 0xda
|
|
344
|
-
target[position++] = length >> 8
|
|
345
|
-
target[position++] = length & 0xff
|
|
335
|
+
target[position++] = 0xda;
|
|
336
|
+
target[position++] = length >> 8;
|
|
337
|
+
target[position++] = length & 0xff;
|
|
346
338
|
} else {
|
|
347
339
|
if (headerSize < 5) {
|
|
348
|
-
target.copyWithin(position + 5, position + 3, position + 3 + length)
|
|
340
|
+
target.copyWithin(position + 5, position + 3, position + 3 + length);
|
|
349
341
|
}
|
|
350
|
-
target[position++] = 0xdb
|
|
351
|
-
targetView.setUint32(position, length)
|
|
352
|
-
position += 4
|
|
342
|
+
target[position++] = 0xdb;
|
|
343
|
+
targetView.setUint32(position, length);
|
|
344
|
+
position += 4;
|
|
353
345
|
}
|
|
354
|
-
position += length
|
|
346
|
+
position += length;
|
|
355
347
|
} else if (type === 'number') {
|
|
356
348
|
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
357
349
|
// positive uint
|
|
358
|
-
if (value <
|
|
359
|
-
target[position++] = value
|
|
350
|
+
if (value < 0x40 || (value < 0x80 && this.useRecords === false)) {
|
|
351
|
+
target[position++] = value;
|
|
360
352
|
} else if (value < 0x100) {
|
|
361
|
-
target[position++] = 0xcc
|
|
362
|
-
target[position++] = value
|
|
353
|
+
target[position++] = 0xcc;
|
|
354
|
+
target[position++] = value;
|
|
363
355
|
} else if (value < 0x10000) {
|
|
364
|
-
target[position++] = 0xcd
|
|
365
|
-
target[position++] = value >> 8
|
|
366
|
-
target[position++] = value & 0xff
|
|
356
|
+
target[position++] = 0xcd;
|
|
357
|
+
target[position++] = value >> 8;
|
|
358
|
+
target[position++] = value & 0xff;
|
|
367
359
|
} else {
|
|
368
|
-
target[position++] = 0xce
|
|
369
|
-
targetView.setUint32(position, value)
|
|
370
|
-
position += 4
|
|
360
|
+
target[position++] = 0xce;
|
|
361
|
+
targetView.setUint32(position, value);
|
|
362
|
+
position += 4;
|
|
371
363
|
}
|
|
372
364
|
} else if (value >> 0 === value) { // negative integer
|
|
373
365
|
if (value >= -0x20) {
|
|
374
|
-
target[position++] = 0x100 + value
|
|
366
|
+
target[position++] = 0x100 + value;
|
|
375
367
|
} else if (value >= -0x80) {
|
|
376
|
-
target[position++] = 0xd0
|
|
377
|
-
target[position++] = value + 0x100
|
|
368
|
+
target[position++] = 0xd0;
|
|
369
|
+
target[position++] = value + 0x100;
|
|
378
370
|
} else if (value >= -0x8000) {
|
|
379
|
-
target[position++] = 0xd1
|
|
380
|
-
targetView.setInt16(position, value)
|
|
381
|
-
position += 2
|
|
371
|
+
target[position++] = 0xd1;
|
|
372
|
+
targetView.setInt16(position, value);
|
|
373
|
+
position += 2;
|
|
382
374
|
} else {
|
|
383
|
-
target[position++] = 0xd2
|
|
384
|
-
targetView.setInt32(position, value)
|
|
385
|
-
position += 4
|
|
375
|
+
target[position++] = 0xd2;
|
|
376
|
+
targetView.setInt32(position, value);
|
|
377
|
+
position += 4;
|
|
386
378
|
}
|
|
387
379
|
} else {
|
|
388
|
-
let useFloat32
|
|
380
|
+
let useFloat32;
|
|
389
381
|
if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
|
|
390
|
-
target[position++] = 0xca
|
|
391
|
-
targetView.setFloat32(position, value)
|
|
392
|
-
let xShifted
|
|
382
|
+
target[position++] = 0xca;
|
|
383
|
+
targetView.setFloat32(position, value);
|
|
384
|
+
let xShifted;
|
|
393
385
|
if (useFloat32 < 4 ||
|
|
394
386
|
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
395
387
|
((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
|
|
396
|
-
position += 4
|
|
397
|
-
return
|
|
388
|
+
position += 4;
|
|
389
|
+
return;
|
|
398
390
|
} else
|
|
399
|
-
position
|
|
391
|
+
position--; // move back into position for writing a double
|
|
400
392
|
}
|
|
401
|
-
target[position++] = 0xcb
|
|
402
|
-
targetView.setFloat64(position, value)
|
|
403
|
-
position += 8
|
|
393
|
+
target[position++] = 0xcb;
|
|
394
|
+
targetView.setFloat64(position, value);
|
|
395
|
+
position += 8;
|
|
404
396
|
}
|
|
405
397
|
} else if (type === 'object' || type === 'function') {
|
|
406
398
|
if (!value)
|
|
407
|
-
target[position++] = 0xc0
|
|
399
|
+
target[position++] = 0xc0;
|
|
408
400
|
else {
|
|
409
401
|
if (referenceMap) {
|
|
410
|
-
let referee = referenceMap.get(value)
|
|
402
|
+
let referee = referenceMap.get(value);
|
|
411
403
|
if (referee) {
|
|
412
404
|
if (!referee.id) {
|
|
413
|
-
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = [])
|
|
414
|
-
referee.id = idsToInsert.push(referee)
|
|
405
|
+
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
|
|
406
|
+
referee.id = idsToInsert.push(referee);
|
|
415
407
|
}
|
|
416
|
-
target[position++] = 0xd6 // fixext 4
|
|
417
|
-
target[position++] = 0x70 // "p" for pointer
|
|
418
|
-
targetView.setUint32(position, referee.id)
|
|
419
|
-
position += 4
|
|
420
|
-
return
|
|
408
|
+
target[position++] = 0xd6; // fixext 4
|
|
409
|
+
target[position++] = 0x70; // "p" for pointer
|
|
410
|
+
targetView.setUint32(position, referee.id);
|
|
411
|
+
position += 4;
|
|
412
|
+
return;
|
|
421
413
|
} else
|
|
422
|
-
referenceMap.set(value, { offset: position - start })
|
|
414
|
+
referenceMap.set(value, { offset: position - start });
|
|
423
415
|
}
|
|
424
|
-
let constructor = value.constructor
|
|
416
|
+
let constructor = value.constructor;
|
|
425
417
|
if (constructor === Object) {
|
|
426
|
-
writeObject(value)
|
|
418
|
+
writeObject(value);
|
|
427
419
|
} else if (constructor === Array) {
|
|
428
|
-
packArray(value)
|
|
420
|
+
packArray(value);
|
|
429
421
|
} else if (constructor === Map) {
|
|
430
|
-
if (this.mapAsEmptyObject) target[position++] = 0x80
|
|
422
|
+
if (this.mapAsEmptyObject) target[position++] = 0x80;
|
|
431
423
|
else {
|
|
432
|
-
length = value.size
|
|
424
|
+
length = value.size;
|
|
433
425
|
if (length < 0x10) {
|
|
434
|
-
target[position++] = 0x80 | length
|
|
426
|
+
target[position++] = 0x80 | length;
|
|
435
427
|
} else if (length < 0x10000) {
|
|
436
|
-
target[position++] = 0xde
|
|
437
|
-
target[position++] = length >> 8
|
|
438
|
-
target[position++] = length & 0xff
|
|
428
|
+
target[position++] = 0xde;
|
|
429
|
+
target[position++] = length >> 8;
|
|
430
|
+
target[position++] = length & 0xff;
|
|
439
431
|
} else {
|
|
440
|
-
target[position++] = 0xdf
|
|
441
|
-
targetView.setUint32(position, length)
|
|
442
|
-
position += 4
|
|
432
|
+
target[position++] = 0xdf;
|
|
433
|
+
targetView.setUint32(position, length);
|
|
434
|
+
position += 4;
|
|
443
435
|
}
|
|
444
436
|
for (let [key, entryValue] of value) {
|
|
445
|
-
pack(key)
|
|
446
|
-
pack(entryValue)
|
|
437
|
+
pack(key);
|
|
438
|
+
pack(entryValue);
|
|
447
439
|
}
|
|
448
440
|
}
|
|
449
441
|
} else {
|
|
450
442
|
for (let i = 0, l = extensions.length; i < l; i++) {
|
|
451
|
-
let extensionClass = extensionClasses[i]
|
|
443
|
+
let extensionClass = extensionClasses[i];
|
|
452
444
|
if (value instanceof extensionClass) {
|
|
453
|
-
let extension = extensions[i]
|
|
445
|
+
let extension = extensions[i];
|
|
454
446
|
if (extension.write) {
|
|
455
447
|
if (extension.type) {
|
|
456
|
-
target[position++] = 0xd4 // one byte "tag" extension
|
|
457
|
-
target[position++] = extension.type
|
|
458
|
-
target[position++] = 0
|
|
448
|
+
target[position++] = 0xd4; // one byte "tag" extension
|
|
449
|
+
target[position++] = extension.type;
|
|
450
|
+
target[position++] = 0;
|
|
459
451
|
}
|
|
460
|
-
let writeResult = extension.write.call(this, value)
|
|
452
|
+
let writeResult = extension.write.call(this, value);
|
|
461
453
|
if (writeResult === value) { // avoid infinite recursion
|
|
462
454
|
if (Array.isArray(value)) {
|
|
463
|
-
packArray(value)
|
|
455
|
+
packArray(value);
|
|
464
456
|
} else {
|
|
465
|
-
writeObject(value)
|
|
457
|
+
writeObject(value);
|
|
466
458
|
}
|
|
467
459
|
} else {
|
|
468
|
-
pack(writeResult)
|
|
460
|
+
pack(writeResult);
|
|
469
461
|
}
|
|
470
|
-
return
|
|
462
|
+
return;
|
|
471
463
|
}
|
|
472
|
-
let currentTarget = target
|
|
473
|
-
let currentTargetView = targetView
|
|
474
|
-
let currentPosition = position
|
|
475
|
-
target = null
|
|
476
|
-
let result
|
|
464
|
+
let currentTarget = target;
|
|
465
|
+
let currentTargetView = targetView;
|
|
466
|
+
let currentPosition = position;
|
|
467
|
+
target = null;
|
|
468
|
+
let result;
|
|
477
469
|
try {
|
|
478
470
|
result = extension.pack.call(this, value, (size) => {
|
|
479
471
|
// restore target and use it
|
|
480
|
-
target = currentTarget
|
|
481
|
-
currentTarget = null
|
|
482
|
-
position += size
|
|
472
|
+
target = currentTarget;
|
|
473
|
+
currentTarget = null;
|
|
474
|
+
position += size;
|
|
483
475
|
if (position > safeEnd)
|
|
484
|
-
makeRoom(position)
|
|
476
|
+
makeRoom(position);
|
|
485
477
|
return {
|
|
486
478
|
target, targetView, position: position - size
|
|
487
|
-
}
|
|
488
|
-
}, pack)
|
|
479
|
+
};
|
|
480
|
+
}, pack);
|
|
489
481
|
} finally {
|
|
490
482
|
// restore current target information (unless already restored)
|
|
491
483
|
if (currentTarget) {
|
|
492
|
-
target = currentTarget
|
|
493
|
-
targetView = currentTargetView
|
|
494
|
-
position = currentPosition
|
|
495
|
-
safeEnd = target.length - 10
|
|
484
|
+
target = currentTarget;
|
|
485
|
+
targetView = currentTargetView;
|
|
486
|
+
position = currentPosition;
|
|
487
|
+
safeEnd = target.length - 10;
|
|
496
488
|
}
|
|
497
489
|
}
|
|
498
490
|
if (result) {
|
|
499
491
|
if (result.length + position > safeEnd)
|
|
500
|
-
makeRoom(result.length + position)
|
|
501
|
-
position = writeExtensionData(result, target, position, extension.type)
|
|
492
|
+
makeRoom(result.length + position);
|
|
493
|
+
position = writeExtensionData(result, target, position, extension.type);
|
|
502
494
|
}
|
|
503
|
-
return
|
|
495
|
+
return;
|
|
504
496
|
}
|
|
505
497
|
}
|
|
506
498
|
// check isArray after extensions, because extensions can extend Array
|
|
507
499
|
if (Array.isArray(value)) {
|
|
508
|
-
packArray(value)
|
|
500
|
+
packArray(value);
|
|
509
501
|
} else {
|
|
510
502
|
// use this as an alternate mechanism for expressing how to serialize
|
|
511
503
|
if (value.toJSON) {
|
|
512
|
-
const json = value.toJSON()
|
|
504
|
+
const json = value.toJSON();
|
|
513
505
|
// if for some reason value.toJSON returns itself it'll loop forever
|
|
514
506
|
if (json !== value)
|
|
515
|
-
return pack(json)
|
|
507
|
+
return pack(json);
|
|
516
508
|
}
|
|
517
509
|
|
|
518
510
|
// if there is a writeFunction, use it, otherwise just encode as undefined
|
|
@@ -520,89 +512,89 @@ export class Packr extends Unpackr {
|
|
|
520
512
|
return pack(this.writeFunction && this.writeFunction(value));
|
|
521
513
|
|
|
522
514
|
// no extension found, write as plain object
|
|
523
|
-
writeObject(value)
|
|
515
|
+
writeObject(value);
|
|
524
516
|
}
|
|
525
517
|
}
|
|
526
518
|
}
|
|
527
519
|
} else if (type === 'boolean') {
|
|
528
|
-
target[position++] = value ? 0xc3 : 0xc2
|
|
520
|
+
target[position++] = value ? 0xc3 : 0xc2;
|
|
529
521
|
} else if (type === 'bigint') {
|
|
530
522
|
if (value < 0x8000000000000000 && value >= -0x8000000000000000) {
|
|
531
523
|
// use a signed int as long as it fits
|
|
532
|
-
target[position++] = 0xd3
|
|
533
|
-
targetView.setBigInt64(position, value)
|
|
524
|
+
target[position++] = 0xd3;
|
|
525
|
+
targetView.setBigInt64(position, value);
|
|
534
526
|
} else if (value < 0x10000000000000000 && value > 0) {
|
|
535
527
|
// if we can fit an unsigned int, use that
|
|
536
|
-
target[position++] = 0xcf
|
|
537
|
-
targetView.setBigUint64(position, value)
|
|
528
|
+
target[position++] = 0xcf;
|
|
529
|
+
targetView.setBigUint64(position, value);
|
|
538
530
|
} else {
|
|
539
531
|
// overflow
|
|
540
532
|
if (this.largeBigIntToFloat) {
|
|
541
|
-
target[position++] = 0xcb
|
|
542
|
-
targetView.setFloat64(position, Number(value))
|
|
533
|
+
target[position++] = 0xcb;
|
|
534
|
+
targetView.setFloat64(position, Number(value));
|
|
543
535
|
} else if (this.largeBigIntToString) {
|
|
544
536
|
return pack(value.toString());
|
|
545
537
|
} else if (this.useBigIntExtension || this.moreTypes) {
|
|
546
|
-
let empty = value < 0 ? BigInt(-1) : BigInt(0)
|
|
538
|
+
let empty = value < 0 ? BigInt(-1) : BigInt(0);
|
|
547
539
|
|
|
548
|
-
let array
|
|
540
|
+
let array;
|
|
549
541
|
if (value >> BigInt(0x10000) === empty) {
|
|
550
|
-
let mask = BigInt(0x10000000000000000) - BigInt(1) // literal would overflow
|
|
551
|
-
let chunks = []
|
|
542
|
+
let mask = BigInt(0x10000000000000000) - BigInt(1); // literal would overflow
|
|
543
|
+
let chunks = [];
|
|
552
544
|
while (true) {
|
|
553
|
-
chunks.push(value & mask)
|
|
554
|
-
if ((value >> BigInt(63)) === empty) break
|
|
555
|
-
value >>= BigInt(64)
|
|
545
|
+
chunks.push(value & mask);
|
|
546
|
+
if ((value >> BigInt(63)) === empty) break;
|
|
547
|
+
value >>= BigInt(64);
|
|
556
548
|
}
|
|
557
549
|
|
|
558
|
-
array = new Uint8Array(new BigUint64Array(chunks).buffer)
|
|
559
|
-
array.reverse()
|
|
550
|
+
array = new Uint8Array(new BigUint64Array(chunks).buffer);
|
|
551
|
+
array.reverse();
|
|
560
552
|
} else {
|
|
561
|
-
let invert = value < 0
|
|
562
|
-
let string = (invert ? ~value : value).toString(16)
|
|
553
|
+
let invert = value < 0;
|
|
554
|
+
let string = (invert ? ~value : value).toString(16);
|
|
563
555
|
if (string.length % 2) {
|
|
564
|
-
string = '0' + string
|
|
556
|
+
string = '0' + string;
|
|
565
557
|
} else if (parseInt(string.charAt(0), 16) >= 8) {
|
|
566
|
-
string = '00' + string
|
|
558
|
+
string = '00' + string;
|
|
567
559
|
}
|
|
568
560
|
|
|
569
561
|
if (hasNodeBuffer) {
|
|
570
|
-
array = Buffer.from(string, 'hex')
|
|
562
|
+
array = Buffer.from(string, 'hex');
|
|
571
563
|
} else {
|
|
572
|
-
array = new Uint8Array(string.length / 2)
|
|
564
|
+
array = new Uint8Array(string.length / 2);
|
|
573
565
|
for (let i = 0; i < array.length; i++) {
|
|
574
|
-
array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16)
|
|
566
|
+
array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
|
|
575
567
|
}
|
|
576
568
|
}
|
|
577
569
|
|
|
578
570
|
if (invert) {
|
|
579
|
-
for (let i = 0; i < array.length; i++) array[i] = ~array[i]
|
|
571
|
+
for (let i = 0; i < array.length; i++) array[i] = ~array[i];
|
|
580
572
|
}
|
|
581
573
|
}
|
|
582
574
|
|
|
583
575
|
if (array.length + position > safeEnd)
|
|
584
|
-
makeRoom(array.length + position)
|
|
585
|
-
position = writeExtensionData(array, target, position, 0x42)
|
|
586
|
-
return
|
|
576
|
+
makeRoom(array.length + position);
|
|
577
|
+
position = writeExtensionData(array, target, position, 0x42);
|
|
578
|
+
return;
|
|
587
579
|
} else {
|
|
588
580
|
throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
|
|
589
581
|
' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
|
|
590
|
-
' largeBigIntToString to convert to string')
|
|
582
|
+
' largeBigIntToString to convert to string');
|
|
591
583
|
}
|
|
592
584
|
}
|
|
593
|
-
position += 8
|
|
585
|
+
position += 8;
|
|
594
586
|
} else if (type === 'undefined') {
|
|
595
587
|
if (this.encodeUndefinedAsNil)
|
|
596
|
-
target[position++] = 0xc0
|
|
588
|
+
target[position++] = 0xc0;
|
|
597
589
|
else {
|
|
598
|
-
target[position++] = 0xd4 // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
|
|
599
|
-
target[position++] = 0
|
|
600
|
-
target[position++] = 0
|
|
590
|
+
target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
|
|
591
|
+
target[position++] = 0;
|
|
592
|
+
target[position++] = 0;
|
|
601
593
|
}
|
|
602
594
|
} else {
|
|
603
|
-
throw new Error('Unknown type: ' + type)
|
|
595
|
+
throw new Error('Unknown type: ' + type);
|
|
604
596
|
}
|
|
605
|
-
}
|
|
597
|
+
};
|
|
606
598
|
|
|
607
599
|
const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
|
|
608
600
|
// this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
|
|
@@ -615,263 +607,246 @@ export class Packr extends Unpackr {
|
|
|
615
607
|
keys.push(key);
|
|
616
608
|
}
|
|
617
609
|
} else {
|
|
618
|
-
keys = Object.keys(object)
|
|
610
|
+
keys = Object.keys(object);
|
|
619
611
|
}
|
|
620
|
-
let length = keys.length
|
|
612
|
+
let length = keys.length;
|
|
621
613
|
if (length < 0x10) {
|
|
622
|
-
target[position++] = 0x80 | length
|
|
614
|
+
target[position++] = 0x80 | length;
|
|
623
615
|
} else if (length < 0x10000) {
|
|
624
|
-
target[position++] = 0xde
|
|
625
|
-
target[position++] = length >> 8
|
|
626
|
-
target[position++] = length & 0xff
|
|
616
|
+
target[position++] = 0xde;
|
|
617
|
+
target[position++] = length >> 8;
|
|
618
|
+
target[position++] = length & 0xff;
|
|
627
619
|
} else {
|
|
628
|
-
target[position++] = 0xdf
|
|
629
|
-
targetView.setUint32(position, length)
|
|
630
|
-
position += 4
|
|
620
|
+
target[position++] = 0xdf;
|
|
621
|
+
targetView.setUint32(position, length);
|
|
622
|
+
position += 4;
|
|
631
623
|
}
|
|
632
|
-
let key
|
|
624
|
+
let key;
|
|
633
625
|
if (this.coercibleKeyAsNumber) {
|
|
634
626
|
for (let i = 0; i < length; i++) {
|
|
635
|
-
key = keys[i]
|
|
636
|
-
let num = Number(key)
|
|
637
|
-
pack(isNaN(num) ? key : num)
|
|
638
|
-
pack(object[key])
|
|
627
|
+
key = keys[i];
|
|
628
|
+
let num = Number(key);
|
|
629
|
+
pack(isNaN(num) ? key : num);
|
|
630
|
+
pack(object[key]);
|
|
639
631
|
}
|
|
640
632
|
|
|
641
633
|
} else {
|
|
642
634
|
for (let i = 0; i < length; i++) {
|
|
643
|
-
pack(key = keys[i])
|
|
644
|
-
pack(object[key])
|
|
635
|
+
pack(key = keys[i]);
|
|
636
|
+
pack(object[key]);
|
|
645
637
|
}
|
|
646
638
|
}
|
|
647
639
|
} :
|
|
648
640
|
(object) => {
|
|
649
|
-
target[position++] = 0xde // always using map 16, so we can preallocate and set the length afterwards
|
|
650
|
-
let objectOffset = position - start
|
|
651
|
-
position += 2
|
|
652
|
-
let size = 0
|
|
641
|
+
target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
|
|
642
|
+
let objectOffset = position - start;
|
|
643
|
+
position += 2;
|
|
644
|
+
let size = 0;
|
|
653
645
|
for (let key in object) {
|
|
654
646
|
if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
655
|
-
pack(key)
|
|
656
|
-
pack(object[key])
|
|
657
|
-
size
|
|
647
|
+
pack(key);
|
|
648
|
+
pack(object[key]);
|
|
649
|
+
size++;
|
|
658
650
|
}
|
|
659
651
|
}
|
|
660
652
|
if (size > 0xffff) {
|
|
661
653
|
throw new Error('Object is too large to serialize with fast 16-bit map size,' +
|
|
662
654
|
' use the "variableMapSize" option to serialize this object');
|
|
663
655
|
}
|
|
664
|
-
target[objectOffset++ + start] = size >> 8
|
|
665
|
-
target[objectOffset + start] = size & 0xff
|
|
666
|
-
}
|
|
656
|
+
target[objectOffset++ + start] = size >> 8;
|
|
657
|
+
target[objectOffset + start] = size & 0xff;
|
|
658
|
+
};
|
|
667
659
|
|
|
668
660
|
const writeRecord = this.useRecords === false ? writePlainObject :
|
|
669
661
|
(options.progressiveRecords && !useTwoByteRecords) ? // this is about 2% faster for highly stable structures, since it only requires one for-in loop (but much more expensive when new structure needs to be written)
|
|
670
662
|
(object) => {
|
|
671
|
-
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null))
|
|
672
|
-
let objectOffset = position++ - start
|
|
673
|
-
let wroteKeys
|
|
663
|
+
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
664
|
+
let objectOffset = position++ - start;
|
|
665
|
+
let wroteKeys;
|
|
674
666
|
for (let key in object) {
|
|
675
667
|
if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
676
|
-
nextTransition = transition[key]
|
|
668
|
+
nextTransition = transition[key];
|
|
677
669
|
if (nextTransition)
|
|
678
|
-
transition = nextTransition
|
|
670
|
+
transition = nextTransition;
|
|
679
671
|
else {
|
|
680
672
|
// record doesn't exist, create full new record and insert it
|
|
681
|
-
let keys = Object.keys(object)
|
|
682
|
-
let lastTransition = transition
|
|
683
|
-
transition = structures.transitions
|
|
684
|
-
let newTransitions = 0
|
|
673
|
+
let keys = Object.keys(object);
|
|
674
|
+
let lastTransition = transition;
|
|
675
|
+
transition = structures.transitions;
|
|
676
|
+
let newTransitions = 0;
|
|
685
677
|
for (let i = 0, l = keys.length; i < l; i++) {
|
|
686
|
-
let key = keys[i]
|
|
687
|
-
nextTransition = transition[key]
|
|
678
|
+
let key = keys[i];
|
|
679
|
+
nextTransition = transition[key];
|
|
688
680
|
if (!nextTransition) {
|
|
689
|
-
nextTransition = transition[key] = Object.create(null)
|
|
690
|
-
newTransitions
|
|
681
|
+
nextTransition = transition[key] = Object.create(null);
|
|
682
|
+
newTransitions++;
|
|
691
683
|
}
|
|
692
|
-
transition = nextTransition
|
|
684
|
+
transition = nextTransition;
|
|
693
685
|
}
|
|
694
686
|
if (objectOffset + start + 1 == position) {
|
|
695
687
|
// first key, so we don't need to insert, we can just write record directly
|
|
696
|
-
position
|
|
697
|
-
newRecord(transition, keys, newTransitions)
|
|
688
|
+
position--;
|
|
689
|
+
newRecord(transition, keys, newTransitions);
|
|
698
690
|
} else // otherwise we need to insert the record, moving existing data after the record
|
|
699
|
-
insertNewRecord(transition, keys, objectOffset, newTransitions)
|
|
700
|
-
wroteKeys = true
|
|
701
|
-
transition = lastTransition[key]
|
|
691
|
+
insertNewRecord(transition, keys, objectOffset, newTransitions);
|
|
692
|
+
wroteKeys = true;
|
|
693
|
+
transition = lastTransition[key];
|
|
702
694
|
}
|
|
703
|
-
pack(object[key])
|
|
695
|
+
pack(object[key]);
|
|
704
696
|
}
|
|
705
697
|
}
|
|
706
698
|
if (!wroteKeys) {
|
|
707
|
-
let recordId = transition[RECORD_SYMBOL]
|
|
699
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
708
700
|
if (recordId)
|
|
709
|
-
target[objectOffset + start] = recordId
|
|
701
|
+
target[objectOffset + start] = recordId;
|
|
710
702
|
else
|
|
711
|
-
insertNewRecord(transition, Object.keys(object), objectOffset, 0)
|
|
703
|
+
insertNewRecord(transition, Object.keys(object), objectOffset, 0);
|
|
712
704
|
}
|
|
713
705
|
} :
|
|
714
706
|
(object) => {
|
|
715
|
-
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null))
|
|
716
|
-
let newTransitions = 0
|
|
707
|
+
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
708
|
+
let newTransitions = 0;
|
|
717
709
|
for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
718
|
-
nextTransition = transition[key]
|
|
710
|
+
nextTransition = transition[key];
|
|
719
711
|
if (!nextTransition) {
|
|
720
|
-
nextTransition = transition[key] = Object.create(null)
|
|
721
|
-
newTransitions
|
|
712
|
+
nextTransition = transition[key] = Object.create(null);
|
|
713
|
+
newTransitions++;
|
|
722
714
|
}
|
|
723
|
-
transition = nextTransition
|
|
715
|
+
transition = nextTransition;
|
|
724
716
|
}
|
|
725
|
-
let recordId = transition[RECORD_SYMBOL]
|
|
717
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
726
718
|
if (recordId) {
|
|
727
719
|
if (recordId >= 0x60 && useTwoByteRecords) {
|
|
728
|
-
target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60
|
|
729
|
-
target[position++] = recordId >> 5
|
|
720
|
+
target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
|
|
721
|
+
target[position++] = recordId >> 5;
|
|
730
722
|
} else
|
|
731
|
-
target[position++] = recordId
|
|
723
|
+
target[position++] = recordId;
|
|
732
724
|
} else {
|
|
733
|
-
newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions)
|
|
725
|
+
newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
|
|
734
726
|
}
|
|
735
727
|
// now write the values
|
|
736
728
|
for (let key in object)
|
|
737
729
|
if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
738
|
-
pack(object[key])
|
|
730
|
+
pack(object[key]);
|
|
739
731
|
}
|
|
740
|
-
}
|
|
732
|
+
};
|
|
741
733
|
|
|
742
734
|
// create reference to useRecords if useRecords is a function
|
|
743
735
|
const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
|
|
744
736
|
|
|
745
737
|
const writeObject = checkUseRecords ? (object) => {
|
|
746
|
-
checkUseRecords(object) ? writeRecord(object) : writePlainObject(object)
|
|
747
|
-
} : writeRecord
|
|
738
|
+
checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
|
|
739
|
+
} : writeRecord;
|
|
748
740
|
|
|
749
741
|
const makeRoom = (end) => {
|
|
750
|
-
let newSize
|
|
742
|
+
let newSize;
|
|
751
743
|
if (end > 0x1000000) {
|
|
752
744
|
// special handling for really large buffers
|
|
753
745
|
if ((end - start) > MAX_BUFFER_SIZE)
|
|
754
|
-
throw new Error('Packed buffer would be larger than maximum buffer size')
|
|
746
|
+
throw new Error('Packed buffer would be larger than maximum buffer size');
|
|
755
747
|
newSize = Math.min(MAX_BUFFER_SIZE,
|
|
756
|
-
Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000)
|
|
748
|
+
Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
|
|
757
749
|
} else // faster handling for smaller buffers
|
|
758
|
-
newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12
|
|
759
|
-
let newBuffer = new ByteArrayAllocate(newSize)
|
|
760
|
-
targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize))
|
|
761
|
-
end = Math.min(end, target.length)
|
|
750
|
+
newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
|
|
751
|
+
let newBuffer = new ByteArrayAllocate(newSize);
|
|
752
|
+
targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
|
|
753
|
+
end = Math.min(end, target.length);
|
|
762
754
|
if (target.copy)
|
|
763
|
-
target.copy(newBuffer, 0, start, end)
|
|
755
|
+
target.copy(newBuffer, 0, start, end);
|
|
764
756
|
else
|
|
765
|
-
newBuffer.set(target.slice(start, end))
|
|
766
|
-
position -= start
|
|
767
|
-
start = 0
|
|
768
|
-
safeEnd = newBuffer.length - 10
|
|
769
|
-
return target = newBuffer
|
|
770
|
-
}
|
|
757
|
+
newBuffer.set(target.slice(start, end));
|
|
758
|
+
position -= start;
|
|
759
|
+
start = 0;
|
|
760
|
+
safeEnd = newBuffer.length - 10;
|
|
761
|
+
return target = newBuffer;
|
|
762
|
+
};
|
|
771
763
|
const newRecord = (transition, keys, newTransitions) => {
|
|
772
|
-
let recordId = structures.nextId
|
|
764
|
+
let recordId = structures.nextId;
|
|
773
765
|
if (!recordId)
|
|
774
|
-
recordId = 0x40
|
|
766
|
+
recordId = 0x40;
|
|
775
767
|
if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
|
|
776
|
-
recordId = structures.nextOwnId
|
|
768
|
+
recordId = structures.nextOwnId;
|
|
777
769
|
if (!(recordId < maxStructureId))
|
|
778
|
-
recordId = sharedLimitId
|
|
779
|
-
structures.nextOwnId = recordId + 1
|
|
770
|
+
recordId = sharedLimitId;
|
|
771
|
+
structures.nextOwnId = recordId + 1;
|
|
780
772
|
} else {
|
|
781
773
|
if (recordId >= maxStructureId)// cycle back around
|
|
782
|
-
recordId = sharedLimitId
|
|
783
|
-
structures.nextId = recordId + 1
|
|
774
|
+
recordId = sharedLimitId;
|
|
775
|
+
structures.nextId = recordId + 1;
|
|
784
776
|
}
|
|
785
|
-
let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1
|
|
786
|
-
transition[RECORD_SYMBOL] = recordId
|
|
787
|
-
transition.__keys__ = keys
|
|
788
|
-
structures[recordId - 0x40] = keys
|
|
777
|
+
let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1;
|
|
778
|
+
transition[RECORD_SYMBOL] = recordId;
|
|
779
|
+
transition.__keys__ = keys;
|
|
780
|
+
structures[recordId - 0x40] = keys;
|
|
789
781
|
|
|
790
782
|
if (recordId < sharedLimitId) {
|
|
791
|
-
keys.isShared = true
|
|
792
|
-
structures.sharedLength = recordId - 0x3f
|
|
793
|
-
hasSharedUpdate = true
|
|
783
|
+
keys.isShared = true;
|
|
784
|
+
structures.sharedLength = recordId - 0x3f;
|
|
785
|
+
hasSharedUpdate = true;
|
|
794
786
|
if (highByte >= 0) {
|
|
795
|
-
target[position++] = (recordId & 0x1f) + 0x60
|
|
796
|
-
target[position++] = highByte
|
|
787
|
+
target[position++] = (recordId & 0x1f) + 0x60;
|
|
788
|
+
target[position++] = highByte;
|
|
797
789
|
} else {
|
|
798
|
-
target[position++] = recordId
|
|
790
|
+
target[position++] = recordId;
|
|
799
791
|
}
|
|
800
792
|
} else {
|
|
801
793
|
if (highByte >= 0) {
|
|
802
|
-
target[position++] = 0xd5 // fixext 2
|
|
803
|
-
target[position++] = 0x72 // "r" record defintion extension type
|
|
804
|
-
target[position++] = (recordId & 0x1f) + 0x60
|
|
805
|
-
target[position++] = highByte
|
|
794
|
+
target[position++] = 0xd5; // fixext 2
|
|
795
|
+
target[position++] = 0x72; // "r" record defintion extension type
|
|
796
|
+
target[position++] = (recordId & 0x1f) + 0x60;
|
|
797
|
+
target[position++] = highByte;
|
|
806
798
|
} else {
|
|
807
|
-
target[position++] = 0xd4 // fixext 1
|
|
808
|
-
target[position++] = 0x72 // "r" record defintion extension type
|
|
809
|
-
target[position++] = recordId
|
|
799
|
+
target[position++] = 0xd4; // fixext 1
|
|
800
|
+
target[position++] = 0x72; // "r" record defintion extension type
|
|
801
|
+
target[position++] = recordId;
|
|
810
802
|
}
|
|
811
803
|
|
|
812
804
|
if (newTransitions)
|
|
813
|
-
transitionsCount += serializationsSinceTransitionRebuild * newTransitions
|
|
805
|
+
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
814
806
|
// record the removal of the id, we can maintain our shared structure
|
|
815
807
|
if (recordIdsToRemove.length >= maxOwnStructures)
|
|
816
|
-
recordIdsToRemove.shift()[RECORD_SYMBOL] = 0 // we are cycling back through, and have to remove old ones
|
|
817
|
-
recordIdsToRemove.push(transition)
|
|
818
|
-
pack(keys)
|
|
808
|
+
recordIdsToRemove.shift()[RECORD_SYMBOL] = 0; // we are cycling back through, and have to remove old ones
|
|
809
|
+
recordIdsToRemove.push(transition);
|
|
810
|
+
pack(keys);
|
|
819
811
|
}
|
|
820
|
-
}
|
|
812
|
+
};
|
|
821
813
|
const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
|
|
822
|
-
let mainTarget = target
|
|
823
|
-
let mainPosition = position
|
|
824
|
-
let mainSafeEnd = safeEnd
|
|
825
|
-
let mainStart = start
|
|
826
|
-
target = keysTarget
|
|
827
|
-
position = 0
|
|
828
|
-
start = 0
|
|
814
|
+
let mainTarget = target;
|
|
815
|
+
let mainPosition = position;
|
|
816
|
+
let mainSafeEnd = safeEnd;
|
|
817
|
+
let mainStart = start;
|
|
818
|
+
target = keysTarget;
|
|
819
|
+
position = 0;
|
|
820
|
+
start = 0;
|
|
829
821
|
if (!target)
|
|
830
|
-
keysTarget = target = new ByteArrayAllocate(8192)
|
|
831
|
-
safeEnd = target.length - 10
|
|
832
|
-
newRecord(transition, keys, newTransitions)
|
|
833
|
-
keysTarget = target
|
|
834
|
-
let keysPosition = position
|
|
835
|
-
target = mainTarget
|
|
836
|
-
position = mainPosition
|
|
837
|
-
safeEnd = mainSafeEnd
|
|
838
|
-
start = mainStart
|
|
822
|
+
keysTarget = target = new ByteArrayAllocate(8192);
|
|
823
|
+
safeEnd = target.length - 10;
|
|
824
|
+
newRecord(transition, keys, newTransitions);
|
|
825
|
+
keysTarget = target;
|
|
826
|
+
let keysPosition = position;
|
|
827
|
+
target = mainTarget;
|
|
828
|
+
position = mainPosition;
|
|
829
|
+
safeEnd = mainSafeEnd;
|
|
830
|
+
start = mainStart;
|
|
839
831
|
if (keysPosition > 1) {
|
|
840
|
-
let newEnd = position + keysPosition - 1
|
|
832
|
+
let newEnd = position + keysPosition - 1;
|
|
841
833
|
if (newEnd > safeEnd)
|
|
842
|
-
makeRoom(newEnd)
|
|
843
|
-
let insertionPosition = insertionOffset + start
|
|
844
|
-
target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position)
|
|
845
|
-
target.set(keysTarget.slice(0, keysPosition), insertionPosition)
|
|
846
|
-
position = newEnd
|
|
834
|
+
makeRoom(newEnd);
|
|
835
|
+
let insertionPosition = insertionOffset + start;
|
|
836
|
+
target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
|
|
837
|
+
target.set(keysTarget.slice(0, keysPosition), insertionPosition);
|
|
838
|
+
position = newEnd;
|
|
847
839
|
} else {
|
|
848
|
-
target[insertionOffset + start] = keysTarget[0]
|
|
840
|
+
target[insertionOffset + start] = keysTarget[0];
|
|
849
841
|
}
|
|
850
|
-
}
|
|
851
|
-
const writeStruct = (object) => {
|
|
852
|
-
let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
|
|
853
|
-
if (notifySharedUpdate)
|
|
854
|
-
return hasSharedUpdate = true;
|
|
855
|
-
position = newPosition;
|
|
856
|
-
let startTarget = target;
|
|
857
|
-
pack(value);
|
|
858
|
-
resetStructures();
|
|
859
|
-
if (startTarget !== target) {
|
|
860
|
-
return { position, targetView, target }; // indicate the buffer was re-allocated
|
|
861
|
-
}
|
|
862
|
-
return position;
|
|
863
|
-
}, this);
|
|
864
|
-
if (newPosition === 0) // bail and go to a msgpack object
|
|
865
|
-
return writeObject(object);
|
|
866
|
-
position = newPosition;
|
|
867
|
-
}
|
|
842
|
+
};
|
|
868
843
|
}
|
|
869
844
|
useBuffer(buffer) {
|
|
870
845
|
// this means we are finished using our own buffer and we can write over it safely
|
|
871
|
-
target = buffer
|
|
872
|
-
target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength))
|
|
846
|
+
target = buffer;
|
|
847
|
+
target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
|
|
873
848
|
targetView = target.dataView;
|
|
874
|
-
position = 0
|
|
849
|
+
position = 0;
|
|
875
850
|
}
|
|
876
851
|
set position (value) {
|
|
877
852
|
position = value;
|
|
@@ -881,261 +856,255 @@ export class Packr extends Unpackr {
|
|
|
881
856
|
}
|
|
882
857
|
clearSharedData() {
|
|
883
858
|
if (this.structures)
|
|
884
|
-
this.structures = []
|
|
885
|
-
if (this.typedStructs)
|
|
886
|
-
this.typedStructs = []
|
|
859
|
+
this.structures = [];
|
|
887
860
|
}
|
|
888
861
|
}
|
|
889
862
|
|
|
890
|
-
extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, DataView, C1Type ]
|
|
863
|
+
extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, DataView, C1Type ];
|
|
891
864
|
extensions = [{
|
|
892
865
|
pack(date, allocateForWrite, pack) {
|
|
893
|
-
let seconds = date.getTime() / 1000
|
|
866
|
+
let seconds = date.getTime() / 1000;
|
|
894
867
|
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
|
|
895
868
|
// Timestamp 32
|
|
896
|
-
let { target, targetView, position} = allocateForWrite(6)
|
|
897
|
-
target[position++] = 0xd6
|
|
898
|
-
target[position++] = 0xff
|
|
899
|
-
targetView.setUint32(position, seconds)
|
|
869
|
+
let { target, targetView, position} = allocateForWrite(6);
|
|
870
|
+
target[position++] = 0xd6;
|
|
871
|
+
target[position++] = 0xff;
|
|
872
|
+
targetView.setUint32(position, seconds);
|
|
900
873
|
} else if (seconds > 0 && seconds < 0x100000000) {
|
|
901
874
|
// Timestamp 64
|
|
902
|
-
let { target, targetView, position} = allocateForWrite(10)
|
|
903
|
-
target[position++] = 0xd7
|
|
904
|
-
target[position++] = 0xff
|
|
905
|
-
targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0))
|
|
906
|
-
targetView.setUint32(position + 4, seconds)
|
|
875
|
+
let { target, targetView, position} = allocateForWrite(10);
|
|
876
|
+
target[position++] = 0xd7;
|
|
877
|
+
target[position++] = 0xff;
|
|
878
|
+
targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0));
|
|
879
|
+
targetView.setUint32(position + 4, seconds);
|
|
907
880
|
} else if (isNaN(seconds)) {
|
|
908
881
|
if (this.onInvalidDate) {
|
|
909
|
-
allocateForWrite(0)
|
|
910
|
-
return pack(this.onInvalidDate())
|
|
882
|
+
allocateForWrite(0);
|
|
883
|
+
return pack(this.onInvalidDate());
|
|
911
884
|
}
|
|
912
885
|
// Intentionally invalid timestamp
|
|
913
|
-
let { target, targetView, position} = allocateForWrite(3)
|
|
914
|
-
target[position++] = 0xd4
|
|
915
|
-
target[position++] = 0xff
|
|
916
|
-
target[position++] = 0xff
|
|
886
|
+
let { target, targetView, position} = allocateForWrite(3);
|
|
887
|
+
target[position++] = 0xd4;
|
|
888
|
+
target[position++] = 0xff;
|
|
889
|
+
target[position++] = 0xff;
|
|
917
890
|
} else {
|
|
918
891
|
// Timestamp 96
|
|
919
|
-
let { target, targetView, position} = allocateForWrite(15)
|
|
920
|
-
target[position++] = 0xc7
|
|
921
|
-
target[position++] = 12
|
|
922
|
-
target[position++] = 0xff
|
|
923
|
-
targetView.setUint32(position, date.getMilliseconds() * 1000000)
|
|
924
|
-
targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)))
|
|
892
|
+
let { target, targetView, position} = allocateForWrite(15);
|
|
893
|
+
target[position++] = 0xc7;
|
|
894
|
+
target[position++] = 12;
|
|
895
|
+
target[position++] = 0xff;
|
|
896
|
+
targetView.setUint32(position, date.getMilliseconds() * 1000000);
|
|
897
|
+
targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)));
|
|
925
898
|
}
|
|
926
899
|
}
|
|
927
900
|
}, {
|
|
928
901
|
pack(set, allocateForWrite, pack) {
|
|
929
902
|
if (this.setAsEmptyObject) {
|
|
930
903
|
allocateForWrite(0);
|
|
931
|
-
return pack({})
|
|
904
|
+
return pack({});
|
|
932
905
|
}
|
|
933
|
-
let array = Array.from(set)
|
|
934
|
-
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
|
|
906
|
+
let array = Array.from(set);
|
|
907
|
+
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
935
908
|
if (this.moreTypes) {
|
|
936
|
-
target[position++] = 0xd4
|
|
937
|
-
target[position++] = 0x73 // 's' for Set
|
|
938
|
-
target[position++] = 0
|
|
909
|
+
target[position++] = 0xd4;
|
|
910
|
+
target[position++] = 0x73; // 's' for Set
|
|
911
|
+
target[position++] = 0;
|
|
939
912
|
}
|
|
940
|
-
pack(array)
|
|
913
|
+
pack(array);
|
|
941
914
|
}
|
|
942
915
|
}, {
|
|
943
916
|
pack(error, allocateForWrite, pack) {
|
|
944
|
-
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
|
|
917
|
+
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
945
918
|
if (this.moreTypes) {
|
|
946
|
-
target[position++] = 0xd4
|
|
947
|
-
target[position++] = 0x65 // 'e' for error
|
|
948
|
-
target[position++] = 0
|
|
919
|
+
target[position++] = 0xd4;
|
|
920
|
+
target[position++] = 0x65; // 'e' for error
|
|
921
|
+
target[position++] = 0;
|
|
949
922
|
}
|
|
950
|
-
pack([ error.name, error.message, error.cause ])
|
|
923
|
+
pack([ error.name, error.message, error.cause ]);
|
|
951
924
|
}
|
|
952
925
|
}, {
|
|
953
926
|
pack(regex, allocateForWrite, pack) {
|
|
954
|
-
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
|
|
927
|
+
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
955
928
|
if (this.moreTypes) {
|
|
956
|
-
target[position++] = 0xd4
|
|
957
|
-
target[position++] = 0x78 // 'x' for regeXp
|
|
958
|
-
target[position++] = 0
|
|
929
|
+
target[position++] = 0xd4;
|
|
930
|
+
target[position++] = 0x78; // 'x' for regeXp
|
|
931
|
+
target[position++] = 0;
|
|
959
932
|
}
|
|
960
|
-
pack([ regex.source, regex.flags ])
|
|
933
|
+
pack([ regex.source, regex.flags ]);
|
|
961
934
|
}
|
|
962
935
|
}, {
|
|
963
936
|
pack(arrayBuffer, allocateForWrite) {
|
|
964
937
|
if (this.moreTypes)
|
|
965
|
-
writeExtBuffer(arrayBuffer, 0x10, allocateForWrite)
|
|
938
|
+
writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
|
|
966
939
|
else
|
|
967
|
-
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite)
|
|
940
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
968
941
|
}
|
|
969
942
|
}, {
|
|
970
943
|
pack(typedArray, allocateForWrite) {
|
|
971
|
-
let constructor = typedArray.constructor
|
|
944
|
+
let constructor = typedArray.constructor;
|
|
972
945
|
if (constructor !== ByteArray && this.moreTypes)
|
|
973
|
-
writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite)
|
|
946
|
+
writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
|
|
974
947
|
else
|
|
975
|
-
writeBuffer(typedArray, allocateForWrite)
|
|
948
|
+
writeBuffer(typedArray, allocateForWrite);
|
|
976
949
|
}
|
|
977
950
|
}, {
|
|
978
951
|
pack(arrayBuffer, allocateForWrite) {
|
|
979
952
|
if (this.moreTypes)
|
|
980
|
-
writeExtBuffer(arrayBuffer, 0x11, allocateForWrite)
|
|
953
|
+
writeExtBuffer(arrayBuffer, 0x11, allocateForWrite);
|
|
981
954
|
else
|
|
982
|
-
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite)
|
|
955
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
983
956
|
}
|
|
984
957
|
}, {
|
|
985
958
|
pack(c1, allocateForWrite) { // specific 0xC1 object
|
|
986
|
-
let { target, position} = allocateForWrite(1)
|
|
987
|
-
target[position] = 0xc1
|
|
959
|
+
let { target, position} = allocateForWrite(1);
|
|
960
|
+
target[position] = 0xc1;
|
|
988
961
|
}
|
|
989
|
-
}]
|
|
962
|
+
}];
|
|
990
963
|
|
|
991
964
|
function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
|
|
992
|
-
let length = typedArray.byteLength
|
|
965
|
+
let length = typedArray.byteLength;
|
|
993
966
|
if (length + 1 < 0x100) {
|
|
994
|
-
var { target, position } = allocateForWrite(4 + length)
|
|
995
|
-
target[position++] = 0xc7
|
|
996
|
-
target[position++] = length + 1
|
|
967
|
+
var { target, position } = allocateForWrite(4 + length);
|
|
968
|
+
target[position++] = 0xc7;
|
|
969
|
+
target[position++] = length + 1;
|
|
997
970
|
} else if (length + 1 < 0x10000) {
|
|
998
|
-
var { target, position } = allocateForWrite(5 + length)
|
|
999
|
-
target[position++] = 0xc8
|
|
1000
|
-
target[position++] = (length + 1) >> 8
|
|
1001
|
-
target[position++] = (length + 1) & 0xff
|
|
971
|
+
var { target, position } = allocateForWrite(5 + length);
|
|
972
|
+
target[position++] = 0xc8;
|
|
973
|
+
target[position++] = (length + 1) >> 8;
|
|
974
|
+
target[position++] = (length + 1) & 0xff;
|
|
1002
975
|
} else {
|
|
1003
|
-
var { target, position, targetView } = allocateForWrite(7 + length)
|
|
1004
|
-
target[position++] = 0xc9
|
|
1005
|
-
targetView.setUint32(position, length + 1) // plus one for the type byte
|
|
1006
|
-
position += 4
|
|
976
|
+
var { target, position, targetView } = allocateForWrite(7 + length);
|
|
977
|
+
target[position++] = 0xc9;
|
|
978
|
+
targetView.setUint32(position, length + 1); // plus one for the type byte
|
|
979
|
+
position += 4;
|
|
1007
980
|
}
|
|
1008
|
-
target[position++] = 0x74 // "t" for typed array
|
|
1009
|
-
target[position++] = type
|
|
1010
|
-
if (!typedArray.buffer) typedArray = new Uint8Array(typedArray)
|
|
1011
|
-
target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position)
|
|
981
|
+
target[position++] = 0x74; // "t" for typed array
|
|
982
|
+
target[position++] = type;
|
|
983
|
+
if (!typedArray.buffer) typedArray = new Uint8Array(typedArray);
|
|
984
|
+
target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position);
|
|
1012
985
|
}
|
|
1013
986
|
function writeBuffer(buffer, allocateForWrite) {
|
|
1014
|
-
let length = buffer.byteLength
|
|
1015
|
-
var target, position
|
|
987
|
+
let length = buffer.byteLength;
|
|
988
|
+
var target, position;
|
|
1016
989
|
if (length < 0x100) {
|
|
1017
|
-
var { target, position } = allocateForWrite(length + 2)
|
|
1018
|
-
target[position++] = 0xc4
|
|
1019
|
-
target[position++] = length
|
|
990
|
+
var { target, position } = allocateForWrite(length + 2);
|
|
991
|
+
target[position++] = 0xc4;
|
|
992
|
+
target[position++] = length;
|
|
1020
993
|
} else if (length < 0x10000) {
|
|
1021
|
-
var { target, position } = allocateForWrite(length + 3)
|
|
1022
|
-
target[position++] = 0xc5
|
|
1023
|
-
target[position++] = length >> 8
|
|
1024
|
-
target[position++] = length & 0xff
|
|
994
|
+
var { target, position } = allocateForWrite(length + 3);
|
|
995
|
+
target[position++] = 0xc5;
|
|
996
|
+
target[position++] = length >> 8;
|
|
997
|
+
target[position++] = length & 0xff;
|
|
1025
998
|
} else {
|
|
1026
|
-
var { target, position, targetView } = allocateForWrite(length + 5)
|
|
1027
|
-
target[position++] = 0xc6
|
|
1028
|
-
targetView.setUint32(position, length)
|
|
1029
|
-
position += 4
|
|
999
|
+
var { target, position, targetView } = allocateForWrite(length + 5);
|
|
1000
|
+
target[position++] = 0xc6;
|
|
1001
|
+
targetView.setUint32(position, length);
|
|
1002
|
+
position += 4;
|
|
1030
1003
|
}
|
|
1031
|
-
target.set(buffer, position)
|
|
1004
|
+
target.set(buffer, position);
|
|
1032
1005
|
}
|
|
1033
1006
|
|
|
1034
1007
|
function writeExtensionData(result, target, position, type) {
|
|
1035
|
-
let length = result.length
|
|
1008
|
+
let length = result.length;
|
|
1036
1009
|
switch (length) {
|
|
1037
1010
|
case 1:
|
|
1038
|
-
target[position++] = 0xd4
|
|
1039
|
-
break
|
|
1011
|
+
target[position++] = 0xd4;
|
|
1012
|
+
break;
|
|
1040
1013
|
case 2:
|
|
1041
|
-
target[position++] = 0xd5
|
|
1042
|
-
break
|
|
1014
|
+
target[position++] = 0xd5;
|
|
1015
|
+
break;
|
|
1043
1016
|
case 4:
|
|
1044
|
-
target[position++] = 0xd6
|
|
1045
|
-
break
|
|
1017
|
+
target[position++] = 0xd6;
|
|
1018
|
+
break;
|
|
1046
1019
|
case 8:
|
|
1047
|
-
target[position++] = 0xd7
|
|
1048
|
-
break
|
|
1020
|
+
target[position++] = 0xd7;
|
|
1021
|
+
break;
|
|
1049
1022
|
case 16:
|
|
1050
|
-
target[position++] = 0xd8
|
|
1051
|
-
break
|
|
1023
|
+
target[position++] = 0xd8;
|
|
1024
|
+
break;
|
|
1052
1025
|
default:
|
|
1053
1026
|
if (length < 0x100) {
|
|
1054
|
-
target[position++] = 0xc7
|
|
1055
|
-
target[position++] = length
|
|
1027
|
+
target[position++] = 0xc7;
|
|
1028
|
+
target[position++] = length;
|
|
1056
1029
|
} else if (length < 0x10000) {
|
|
1057
|
-
target[position++] = 0xc8
|
|
1058
|
-
target[position++] = length >> 8
|
|
1059
|
-
target[position++] = length & 0xff
|
|
1030
|
+
target[position++] = 0xc8;
|
|
1031
|
+
target[position++] = length >> 8;
|
|
1032
|
+
target[position++] = length & 0xff;
|
|
1060
1033
|
} else {
|
|
1061
|
-
target[position++] = 0xc9
|
|
1062
|
-
target[position++] = length >> 24
|
|
1063
|
-
target[position++] = (length >> 16) & 0xff
|
|
1064
|
-
target[position++] = (length >> 8) & 0xff
|
|
1065
|
-
target[position++] = length & 0xff
|
|
1034
|
+
target[position++] = 0xc9;
|
|
1035
|
+
target[position++] = length >> 24;
|
|
1036
|
+
target[position++] = (length >> 16) & 0xff;
|
|
1037
|
+
target[position++] = (length >> 8) & 0xff;
|
|
1038
|
+
target[position++] = length & 0xff;
|
|
1066
1039
|
}
|
|
1067
1040
|
}
|
|
1068
|
-
target[position++] = type
|
|
1069
|
-
target.set(result, position)
|
|
1070
|
-
position += length
|
|
1071
|
-
return position
|
|
1041
|
+
target[position++] = type;
|
|
1042
|
+
target.set(result, position);
|
|
1043
|
+
position += length;
|
|
1044
|
+
return position;
|
|
1072
1045
|
}
|
|
1073
1046
|
|
|
1074
1047
|
function insertIds(serialized, idsToInsert) {
|
|
1075
1048
|
// insert the ids that need to be referenced for structured clones
|
|
1076
|
-
let nextId
|
|
1077
|
-
let distanceToMove = idsToInsert.length * 6
|
|
1078
|
-
let lastEnd = serialized.length - distanceToMove
|
|
1049
|
+
let nextId;
|
|
1050
|
+
let distanceToMove = idsToInsert.length * 6;
|
|
1051
|
+
let lastEnd = serialized.length - distanceToMove;
|
|
1079
1052
|
while (nextId = idsToInsert.pop()) {
|
|
1080
|
-
let offset = nextId.offset
|
|
1081
|
-
let id = nextId.id
|
|
1082
|
-
serialized.copyWithin(offset + distanceToMove, offset, lastEnd)
|
|
1083
|
-
distanceToMove -= 6
|
|
1084
|
-
let position = offset + distanceToMove
|
|
1085
|
-
serialized[position++] = 0xd6
|
|
1086
|
-
serialized[position++] = 0x69 // 'i'
|
|
1087
|
-
serialized[position++] = id >> 24
|
|
1088
|
-
serialized[position++] = (id >> 16) & 0xff
|
|
1089
|
-
serialized[position++] = (id >> 8) & 0xff
|
|
1090
|
-
serialized[position++] = id & 0xff
|
|
1091
|
-
lastEnd = offset
|
|
1053
|
+
let offset = nextId.offset;
|
|
1054
|
+
let id = nextId.id;
|
|
1055
|
+
serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
|
|
1056
|
+
distanceToMove -= 6;
|
|
1057
|
+
let position = offset + distanceToMove;
|
|
1058
|
+
serialized[position++] = 0xd6;
|
|
1059
|
+
serialized[position++] = 0x69; // 'i'
|
|
1060
|
+
serialized[position++] = id >> 24;
|
|
1061
|
+
serialized[position++] = (id >> 16) & 0xff;
|
|
1062
|
+
serialized[position++] = (id >> 8) & 0xff;
|
|
1063
|
+
serialized[position++] = id & 0xff;
|
|
1064
|
+
lastEnd = offset;
|
|
1092
1065
|
}
|
|
1093
|
-
return serialized
|
|
1066
|
+
return serialized;
|
|
1094
1067
|
}
|
|
1095
1068
|
|
|
1096
1069
|
function writeBundles(start, pack, incrementPosition) {
|
|
1097
1070
|
if (bundledStrings.length > 0) {
|
|
1098
|
-
targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start)
|
|
1071
|
+
targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
|
|
1099
1072
|
bundledStrings.stringsPosition = position - start;
|
|
1100
|
-
let writeStrings = bundledStrings
|
|
1101
|
-
bundledStrings = null
|
|
1102
|
-
pack(writeStrings[0])
|
|
1103
|
-
pack(writeStrings[1])
|
|
1073
|
+
let writeStrings = bundledStrings;
|
|
1074
|
+
bundledStrings = null;
|
|
1075
|
+
pack(writeStrings[0]);
|
|
1076
|
+
pack(writeStrings[1]);
|
|
1104
1077
|
}
|
|
1105
1078
|
}
|
|
1106
1079
|
|
|
1107
1080
|
export function addExtension(extension) {
|
|
1108
1081
|
if (extension.Class) {
|
|
1109
1082
|
if (!extension.pack && !extension.write)
|
|
1110
|
-
throw new Error('Extension has no pack or write function')
|
|
1083
|
+
throw new Error('Extension has no pack or write function');
|
|
1111
1084
|
if (extension.pack && !extension.type)
|
|
1112
|
-
throw new Error('Extension has no type (numeric code to identify the extension)')
|
|
1113
|
-
extensionClasses.unshift(extension.Class)
|
|
1114
|
-
extensions.unshift(extension)
|
|
1085
|
+
throw new Error('Extension has no type (numeric code to identify the extension)');
|
|
1086
|
+
extensionClasses.unshift(extension.Class);
|
|
1087
|
+
extensions.unshift(extension);
|
|
1115
1088
|
}
|
|
1116
|
-
unpackAddExtension(extension)
|
|
1089
|
+
unpackAddExtension(extension);
|
|
1117
1090
|
}
|
|
1118
1091
|
function prepareStructures(structures, packr) {
|
|
1119
1092
|
structures.isCompatible = (existingStructures) => {
|
|
1120
|
-
let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length)
|
|
1093
|
+
let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
|
|
1121
1094
|
if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
|
|
1122
1095
|
packr._mergeStructures(existingStructures);
|
|
1123
1096
|
return compatible;
|
|
1124
|
-
}
|
|
1125
|
-
return structures
|
|
1126
|
-
}
|
|
1127
|
-
export function setWriteStructSlots(writeSlots, makeStructures) {
|
|
1128
|
-
writeStructSlots = writeSlots;
|
|
1129
|
-
prepareStructures = makeStructures;
|
|
1097
|
+
};
|
|
1098
|
+
return structures;
|
|
1130
1099
|
}
|
|
1131
1100
|
|
|
1132
|
-
let defaultPackr = new Packr({ useRecords: false })
|
|
1133
|
-
export const pack = defaultPackr.pack
|
|
1134
|
-
export const encode = defaultPackr.pack
|
|
1135
|
-
export const Encoder = Packr
|
|
1136
|
-
export { FLOAT32_OPTIONS } from './unpack.js'
|
|
1137
|
-
import { FLOAT32_OPTIONS } from './unpack.js'
|
|
1138
|
-
export const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS
|
|
1139
|
-
export const REUSE_BUFFER_MODE = 512
|
|
1140
|
-
export const RESET_BUFFER_MODE = 1024
|
|
1141
|
-
export const RESERVE_START_SPACE = 2048
|
|
1101
|
+
let defaultPackr = new Packr({ useRecords: false });
|
|
1102
|
+
export const pack = defaultPackr.pack;
|
|
1103
|
+
export const encode = defaultPackr.pack;
|
|
1104
|
+
export const Encoder = Packr;
|
|
1105
|
+
export { FLOAT32_OPTIONS } from './unpack.js';
|
|
1106
|
+
import { FLOAT32_OPTIONS } from './unpack.js';
|
|
1107
|
+
export const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
|
|
1108
|
+
export const REUSE_BUFFER_MODE = 512;
|
|
1109
|
+
export const RESET_BUFFER_MODE = 1024;
|
|
1110
|
+
export const RESERVE_START_SPACE = 2048;
|