msgpackr 1.6.2 → 1.7.0-alpha1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +30 -6
- package/dist/index.min.js +45 -45
- package/dist/node.cjs +275 -6
- package/dist/test.js +2231 -49
- package/node-index.js +1 -0
- package/pack.js +27 -5
- package/package.json +83 -83
- package/struct.js +260 -0
- package/unpack.js +19 -2
package/dist/test.js
CHANGED
|
@@ -3,7 +3,2172 @@
|
|
|
3
3
|
|
|
4
4
|
chai = chai && Object.prototype.hasOwnProperty.call(chai, 'default') ? chai['default'] : chai;
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
var decoder;
|
|
7
|
+
try {
|
|
8
|
+
decoder = new TextDecoder();
|
|
9
|
+
} catch(error) {}
|
|
10
|
+
var src;
|
|
11
|
+
var srcEnd;
|
|
12
|
+
var position = 0;
|
|
13
|
+
var currentUnpackr = {};
|
|
14
|
+
var currentStructures;
|
|
15
|
+
var srcString;
|
|
16
|
+
var srcStringStart = 0;
|
|
17
|
+
var srcStringEnd = 0;
|
|
18
|
+
var bundledStrings;
|
|
19
|
+
var referenceMap;
|
|
20
|
+
var currentExtensions = [];
|
|
21
|
+
var dataView;
|
|
22
|
+
var defaultOptions = {
|
|
23
|
+
useRecords: false,
|
|
24
|
+
mapsAsObjects: true
|
|
25
|
+
};
|
|
26
|
+
class C1Type {}
|
|
27
|
+
const C1 = new C1Type();
|
|
28
|
+
C1.name = 'MessagePack 0xC1';
|
|
29
|
+
var sequentialMode = false;
|
|
30
|
+
var inlineObjectReadThreshold = 2;
|
|
31
|
+
var readStruct;
|
|
32
|
+
try {
|
|
33
|
+
new Function('');
|
|
34
|
+
} catch(error) {
|
|
35
|
+
// if eval variants are not supported, do not create inline object readers ever
|
|
36
|
+
inlineObjectReadThreshold = Infinity;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class Unpackr {
|
|
40
|
+
constructor(options) {
|
|
41
|
+
if (options) {
|
|
42
|
+
if (options.useRecords === false && options.mapsAsObjects === undefined)
|
|
43
|
+
options.mapsAsObjects = true;
|
|
44
|
+
if (options.sequential && options.trusted !== false) {
|
|
45
|
+
options.trusted = true;
|
|
46
|
+
if (!options.structures && options.useRecords != false) {
|
|
47
|
+
options.structures = [];
|
|
48
|
+
if (!options.maxSharedStructures)
|
|
49
|
+
options.maxSharedStructures = 0;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (options.structures)
|
|
53
|
+
options.structures.sharedLength = options.structures.length;
|
|
54
|
+
else if (options.getStructures) {
|
|
55
|
+
(options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
|
|
56
|
+
options.structures.sharedLength = 0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
Object.assign(this, options);
|
|
60
|
+
}
|
|
61
|
+
unpack(source, end) {
|
|
62
|
+
if (src) {
|
|
63
|
+
// re-entrant execution, save the state and restore it after we do this unpack
|
|
64
|
+
return saveState(() => {
|
|
65
|
+
clearSource();
|
|
66
|
+
return this ? this.unpack(source, end) : Unpackr.prototype.unpack.call(defaultOptions, source, end)
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
srcEnd = end > -1 ? end : source.length;
|
|
70
|
+
position = 0;
|
|
71
|
+
srcStringEnd = 0;
|
|
72
|
+
srcString = null;
|
|
73
|
+
bundledStrings = null;
|
|
74
|
+
src = source;
|
|
75
|
+
// this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
|
|
76
|
+
// technique for getting data from a database where it can be copied into an existing buffer instead of creating
|
|
77
|
+
// new ones
|
|
78
|
+
try {
|
|
79
|
+
dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
|
|
80
|
+
} catch(error) {
|
|
81
|
+
// if it doesn't have a buffer, maybe it is the wrong type of object
|
|
82
|
+
src = null;
|
|
83
|
+
if (source instanceof Uint8Array)
|
|
84
|
+
throw error
|
|
85
|
+
throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
|
|
86
|
+
}
|
|
87
|
+
if (this instanceof Unpackr) {
|
|
88
|
+
currentUnpackr = this;
|
|
89
|
+
if (this.structures) {
|
|
90
|
+
currentStructures = this.structures;
|
|
91
|
+
return checkedRead()
|
|
92
|
+
} else if (!currentStructures || currentStructures.length > 0) {
|
|
93
|
+
currentStructures = [];
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
currentUnpackr = defaultOptions;
|
|
97
|
+
if (!currentStructures || currentStructures.length > 0)
|
|
98
|
+
currentStructures = [];
|
|
99
|
+
}
|
|
100
|
+
return checkedRead()
|
|
101
|
+
}
|
|
102
|
+
unpackMultiple(source, forEach) {
|
|
103
|
+
let values, lastPosition = 0;
|
|
104
|
+
try {
|
|
105
|
+
sequentialMode = true;
|
|
106
|
+
let size = source.length;
|
|
107
|
+
let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
|
|
108
|
+
if (forEach) {
|
|
109
|
+
forEach(value);
|
|
110
|
+
while(position < size) {
|
|
111
|
+
lastPosition = position;
|
|
112
|
+
if (forEach(checkedRead()) === false) {
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
values = [ value ];
|
|
119
|
+
while(position < size) {
|
|
120
|
+
lastPosition = position;
|
|
121
|
+
values.push(checkedRead());
|
|
122
|
+
}
|
|
123
|
+
return values
|
|
124
|
+
}
|
|
125
|
+
} catch(error) {
|
|
126
|
+
error.lastPosition = lastPosition;
|
|
127
|
+
error.values = values;
|
|
128
|
+
throw error
|
|
129
|
+
} finally {
|
|
130
|
+
sequentialMode = false;
|
|
131
|
+
clearSource();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
_mergeStructures(loadedStructures, existingStructures) {
|
|
135
|
+
loadedStructures = loadedStructures || [];
|
|
136
|
+
for (let i = 0, l = loadedStructures.length; i < l; i++) {
|
|
137
|
+
let structure = loadedStructures[i];
|
|
138
|
+
if (structure) {
|
|
139
|
+
structure.isShared = true;
|
|
140
|
+
if (i >= 32)
|
|
141
|
+
structure.highByte = (i - 32) >> 5;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
loadedStructures.sharedLength = loadedStructures.length;
|
|
145
|
+
for (let id in existingStructures || []) {
|
|
146
|
+
if (id >= 0) {
|
|
147
|
+
let structure = loadedStructures[id];
|
|
148
|
+
let existing = existingStructures[id];
|
|
149
|
+
if (existing) {
|
|
150
|
+
if (structure)
|
|
151
|
+
(loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
|
|
152
|
+
loadedStructures[id] = existing;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return this.structures = loadedStructures
|
|
157
|
+
}
|
|
158
|
+
decode(source, end) {
|
|
159
|
+
return this.unpack(source, end)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function checkedRead() {
|
|
163
|
+
try {
|
|
164
|
+
if (!currentUnpackr.trusted && !sequentialMode) {
|
|
165
|
+
let sharedLength = currentStructures.sharedLength || 0;
|
|
166
|
+
if (sharedLength < currentStructures.length)
|
|
167
|
+
currentStructures.length = sharedLength;
|
|
168
|
+
}
|
|
169
|
+
let result;
|
|
170
|
+
if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && readStruct) {
|
|
171
|
+
let id = (src[position++] << 8) + src[position++];
|
|
172
|
+
result = readStruct(src, position, srcEnd, currentStructures[id - 0x40] || loadStructures()[id - 0x40], currentUnpackr);
|
|
173
|
+
position = srcEnd;
|
|
174
|
+
} else
|
|
175
|
+
result = read();
|
|
176
|
+
if (bundledStrings) // bundled strings to skip past
|
|
177
|
+
position = bundledStrings.postBundlePosition;
|
|
178
|
+
|
|
179
|
+
if (position == srcEnd) {
|
|
180
|
+
// finished reading this source, cleanup references
|
|
181
|
+
if (currentStructures.restoreStructures)
|
|
182
|
+
restoreStructures();
|
|
183
|
+
currentStructures = null;
|
|
184
|
+
src = null;
|
|
185
|
+
if (referenceMap)
|
|
186
|
+
referenceMap = null;
|
|
187
|
+
} else if (position > srcEnd) {
|
|
188
|
+
// over read
|
|
189
|
+
throw new Error('Unexpected end of MessagePack data')
|
|
190
|
+
} else if (!sequentialMode) {
|
|
191
|
+
throw new Error('Data read, but end of buffer not reached ' + JSON.stringify(result).slice(0, 100))
|
|
192
|
+
}
|
|
193
|
+
// else more to read, but we are reading sequentially, so don't clear source yet
|
|
194
|
+
return result
|
|
195
|
+
} catch(error) {
|
|
196
|
+
if (currentStructures.restoreStructures)
|
|
197
|
+
restoreStructures();
|
|
198
|
+
clearSource();
|
|
199
|
+
if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
|
|
200
|
+
error.incomplete = true;
|
|
201
|
+
}
|
|
202
|
+
throw error
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function restoreStructures() {
|
|
207
|
+
for (let id in currentStructures.restoreStructures) {
|
|
208
|
+
currentStructures[id] = currentStructures.restoreStructures[id];
|
|
209
|
+
}
|
|
210
|
+
currentStructures.restoreStructures = null;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function read() {
|
|
214
|
+
let token = src[position++];
|
|
215
|
+
if (token < 0xa0) {
|
|
216
|
+
if (token < 0x80) {
|
|
217
|
+
if (token < 0x40)
|
|
218
|
+
return token
|
|
219
|
+
else {
|
|
220
|
+
let structure = currentStructures[token & 0x3f] ||
|
|
221
|
+
currentUnpackr.getStructures && loadStructures()[token & 0x3f];
|
|
222
|
+
if (structure) {
|
|
223
|
+
if (!structure.read) {
|
|
224
|
+
structure.read = createStructureReader(structure, token & 0x3f);
|
|
225
|
+
}
|
|
226
|
+
return structure.read()
|
|
227
|
+
} else
|
|
228
|
+
return token
|
|
229
|
+
}
|
|
230
|
+
} else if (token < 0x90) {
|
|
231
|
+
// map
|
|
232
|
+
token -= 0x80;
|
|
233
|
+
if (currentUnpackr.mapsAsObjects) {
|
|
234
|
+
let object = {};
|
|
235
|
+
for (let i = 0; i < token; i++) {
|
|
236
|
+
object[readKey()] = read();
|
|
237
|
+
}
|
|
238
|
+
return object
|
|
239
|
+
} else {
|
|
240
|
+
let map = new Map();
|
|
241
|
+
for (let i = 0; i < token; i++) {
|
|
242
|
+
map.set(read(), read());
|
|
243
|
+
}
|
|
244
|
+
return map
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
token -= 0x90;
|
|
248
|
+
let array = new Array(token);
|
|
249
|
+
for (let i = 0; i < token; i++) {
|
|
250
|
+
array[i] = read();
|
|
251
|
+
}
|
|
252
|
+
if (currentUnpackr.freezeData)
|
|
253
|
+
return Object.freeze(array)
|
|
254
|
+
return array
|
|
255
|
+
}
|
|
256
|
+
} else if (token < 0xc0) {
|
|
257
|
+
// fixstr
|
|
258
|
+
let length = token - 0xa0;
|
|
259
|
+
if (srcStringEnd >= position) {
|
|
260
|
+
return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
|
|
261
|
+
}
|
|
262
|
+
if (srcStringEnd == 0 && srcEnd < 140) {
|
|
263
|
+
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
264
|
+
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
|
|
265
|
+
if (string != null)
|
|
266
|
+
return string
|
|
267
|
+
}
|
|
268
|
+
return readFixedString(length)
|
|
269
|
+
} else {
|
|
270
|
+
let value;
|
|
271
|
+
switch (token) {
|
|
272
|
+
case 0xc0: return null
|
|
273
|
+
case 0xc1:
|
|
274
|
+
if (bundledStrings) {
|
|
275
|
+
value = read(); // followed by the length of the string in characters (not bytes!)
|
|
276
|
+
if (value > 0)
|
|
277
|
+
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
|
|
278
|
+
else
|
|
279
|
+
return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
|
|
280
|
+
}
|
|
281
|
+
return C1; // "never-used", return special object to denote that
|
|
282
|
+
case 0xc2: return false
|
|
283
|
+
case 0xc3: return true
|
|
284
|
+
case 0xc4:
|
|
285
|
+
// bin 8
|
|
286
|
+
value = src[position++];
|
|
287
|
+
if (value === undefined)
|
|
288
|
+
throw new Error('Unexpected end of buffer')
|
|
289
|
+
return readBin(value)
|
|
290
|
+
case 0xc5:
|
|
291
|
+
// bin 16
|
|
292
|
+
value = dataView.getUint16(position);
|
|
293
|
+
position += 2;
|
|
294
|
+
return readBin(value)
|
|
295
|
+
case 0xc6:
|
|
296
|
+
// bin 32
|
|
297
|
+
value = dataView.getUint32(position);
|
|
298
|
+
position += 4;
|
|
299
|
+
return readBin(value)
|
|
300
|
+
case 0xc7:
|
|
301
|
+
// ext 8
|
|
302
|
+
return readExt(src[position++])
|
|
303
|
+
case 0xc8:
|
|
304
|
+
// ext 16
|
|
305
|
+
value = dataView.getUint16(position);
|
|
306
|
+
position += 2;
|
|
307
|
+
return readExt(value)
|
|
308
|
+
case 0xc9:
|
|
309
|
+
// ext 32
|
|
310
|
+
value = dataView.getUint32(position);
|
|
311
|
+
position += 4;
|
|
312
|
+
return readExt(value)
|
|
313
|
+
case 0xca:
|
|
314
|
+
value = dataView.getFloat32(position);
|
|
315
|
+
if (currentUnpackr.useFloat32 > 2) {
|
|
316
|
+
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
317
|
+
let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
|
|
318
|
+
position += 4;
|
|
319
|
+
return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
320
|
+
}
|
|
321
|
+
position += 4;
|
|
322
|
+
return value
|
|
323
|
+
case 0xcb:
|
|
324
|
+
value = dataView.getFloat64(position);
|
|
325
|
+
position += 8;
|
|
326
|
+
return value
|
|
327
|
+
// uint handlers
|
|
328
|
+
case 0xcc:
|
|
329
|
+
return src[position++]
|
|
330
|
+
case 0xcd:
|
|
331
|
+
value = dataView.getUint16(position);
|
|
332
|
+
position += 2;
|
|
333
|
+
return value
|
|
334
|
+
case 0xce:
|
|
335
|
+
value = dataView.getUint32(position);
|
|
336
|
+
position += 4;
|
|
337
|
+
return value
|
|
338
|
+
case 0xcf:
|
|
339
|
+
if (currentUnpackr.int64AsNumber) {
|
|
340
|
+
value = dataView.getUint32(position) * 0x100000000;
|
|
341
|
+
value += dataView.getUint32(position + 4);
|
|
342
|
+
} else
|
|
343
|
+
value = dataView.getBigUint64(position);
|
|
344
|
+
position += 8;
|
|
345
|
+
return value
|
|
346
|
+
|
|
347
|
+
// int handlers
|
|
348
|
+
case 0xd0:
|
|
349
|
+
return dataView.getInt8(position++)
|
|
350
|
+
case 0xd1:
|
|
351
|
+
value = dataView.getInt16(position);
|
|
352
|
+
position += 2;
|
|
353
|
+
return value
|
|
354
|
+
case 0xd2:
|
|
355
|
+
value = dataView.getInt32(position);
|
|
356
|
+
position += 4;
|
|
357
|
+
return value
|
|
358
|
+
case 0xd3:
|
|
359
|
+
if (currentUnpackr.int64AsNumber) {
|
|
360
|
+
value = dataView.getInt32(position) * 0x100000000;
|
|
361
|
+
value += dataView.getUint32(position + 4);
|
|
362
|
+
} else
|
|
363
|
+
value = dataView.getBigInt64(position);
|
|
364
|
+
position += 8;
|
|
365
|
+
return value
|
|
366
|
+
|
|
367
|
+
case 0xd4:
|
|
368
|
+
// fixext 1
|
|
369
|
+
value = src[position++];
|
|
370
|
+
if (value == 0x72) {
|
|
371
|
+
return recordDefinition(src[position++] & 0x3f)
|
|
372
|
+
} else {
|
|
373
|
+
let extension = currentExtensions[value];
|
|
374
|
+
if (extension) {
|
|
375
|
+
if (extension.read) {
|
|
376
|
+
position++; // skip filler byte
|
|
377
|
+
return extension.read(read())
|
|
378
|
+
} else if (extension.noBuffer) {
|
|
379
|
+
position++; // skip filler byte
|
|
380
|
+
return extension()
|
|
381
|
+
} else
|
|
382
|
+
return extension(src.subarray(position, ++position))
|
|
383
|
+
} else
|
|
384
|
+
throw new Error('Unknown extension ' + value)
|
|
385
|
+
}
|
|
386
|
+
case 0xd5:
|
|
387
|
+
// fixext 2
|
|
388
|
+
value = src[position];
|
|
389
|
+
if (value == 0x72) {
|
|
390
|
+
position++;
|
|
391
|
+
return recordDefinition(src[position++] & 0x3f, src[position++])
|
|
392
|
+
} else
|
|
393
|
+
return readExt(2)
|
|
394
|
+
case 0xd6:
|
|
395
|
+
// fixext 4
|
|
396
|
+
return readExt(4)
|
|
397
|
+
case 0xd7:
|
|
398
|
+
// fixext 8
|
|
399
|
+
return readExt(8)
|
|
400
|
+
case 0xd8:
|
|
401
|
+
// fixext 16
|
|
402
|
+
return readExt(16)
|
|
403
|
+
case 0xd9:
|
|
404
|
+
// str 8
|
|
405
|
+
value = src[position++];
|
|
406
|
+
if (srcStringEnd >= position) {
|
|
407
|
+
return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
|
|
408
|
+
}
|
|
409
|
+
return readString8(value)
|
|
410
|
+
case 0xda:
|
|
411
|
+
// str 16
|
|
412
|
+
value = dataView.getUint16(position);
|
|
413
|
+
position += 2;
|
|
414
|
+
if (srcStringEnd >= position) {
|
|
415
|
+
return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
|
|
416
|
+
}
|
|
417
|
+
return readString16(value)
|
|
418
|
+
case 0xdb:
|
|
419
|
+
// str 32
|
|
420
|
+
value = dataView.getUint32(position);
|
|
421
|
+
position += 4;
|
|
422
|
+
if (srcStringEnd >= position) {
|
|
423
|
+
return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
|
|
424
|
+
}
|
|
425
|
+
return readString32(value)
|
|
426
|
+
case 0xdc:
|
|
427
|
+
// array 16
|
|
428
|
+
value = dataView.getUint16(position);
|
|
429
|
+
position += 2;
|
|
430
|
+
return readArray(value)
|
|
431
|
+
case 0xdd:
|
|
432
|
+
// array 32
|
|
433
|
+
value = dataView.getUint32(position);
|
|
434
|
+
position += 4;
|
|
435
|
+
return readArray(value)
|
|
436
|
+
case 0xde:
|
|
437
|
+
// map 16
|
|
438
|
+
value = dataView.getUint16(position);
|
|
439
|
+
position += 2;
|
|
440
|
+
return readMap(value)
|
|
441
|
+
case 0xdf:
|
|
442
|
+
// map 32
|
|
443
|
+
value = dataView.getUint32(position);
|
|
444
|
+
position += 4;
|
|
445
|
+
return readMap(value)
|
|
446
|
+
default: // negative int
|
|
447
|
+
if (token >= 0xe0)
|
|
448
|
+
return token - 0x100
|
|
449
|
+
if (token === undefined) {
|
|
450
|
+
let error = new Error('Unexpected end of MessagePack data');
|
|
451
|
+
error.incomplete = true;
|
|
452
|
+
throw error
|
|
453
|
+
}
|
|
454
|
+
throw new Error('Unknown MessagePack token ' + token)
|
|
455
|
+
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
|
|
460
|
+
function createStructureReader(structure, firstId) {
|
|
461
|
+
function readObject() {
|
|
462
|
+
// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
|
|
463
|
+
if (readObject.count++ > inlineObjectReadThreshold) {
|
|
464
|
+
let readObject = structure.read = (new Function('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
|
|
465
|
+
'({' + structure.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
|
|
466
|
+
if (structure.highByte === 0)
|
|
467
|
+
structure.read = createSecondByteReader(firstId, structure.read);
|
|
468
|
+
return readObject() // second byte is already read, if there is one so immediately read object
|
|
469
|
+
}
|
|
470
|
+
let object = {};
|
|
471
|
+
for (let i = 0, l = structure.length; i < l; i++) {
|
|
472
|
+
let key = structure[i];
|
|
473
|
+
object[key] = read();
|
|
474
|
+
}
|
|
475
|
+
if (currentUnpackr.freezeData)
|
|
476
|
+
return Object.freeze(object);
|
|
477
|
+
return object
|
|
478
|
+
}
|
|
479
|
+
readObject.count = 0;
|
|
480
|
+
if (structure.highByte === 0) {
|
|
481
|
+
return createSecondByteReader(firstId, readObject)
|
|
482
|
+
}
|
|
483
|
+
return readObject
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const createSecondByteReader = (firstId, read0) => {
|
|
487
|
+
return function() {
|
|
488
|
+
let highByte = src[position++];
|
|
489
|
+
if (highByte === 0)
|
|
490
|
+
return read0()
|
|
491
|
+
let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
|
|
492
|
+
let structure = currentStructures[id] || loadStructures()[id];
|
|
493
|
+
if (!structure) {
|
|
494
|
+
throw new Error('Record id is not defined for ' + id)
|
|
495
|
+
}
|
|
496
|
+
if (!structure.read)
|
|
497
|
+
structure.read = createStructureReader(structure, firstId);
|
|
498
|
+
return structure.read()
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
function loadStructures() {
|
|
503
|
+
let loadedStructures = saveState(() => {
|
|
504
|
+
// save the state in case getStructures modifies our buffer
|
|
505
|
+
src = null;
|
|
506
|
+
return currentUnpackr.getStructures()
|
|
507
|
+
});
|
|
508
|
+
return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
var readFixedString = readStringJS;
|
|
512
|
+
var readString8 = readStringJS;
|
|
513
|
+
var readString16 = readStringJS;
|
|
514
|
+
var readString32 = readStringJS;
|
|
515
|
+
function readStringJS(length) {
|
|
516
|
+
let result;
|
|
517
|
+
if (length < 16) {
|
|
518
|
+
if (result = shortStringInJS(length))
|
|
519
|
+
return result
|
|
520
|
+
}
|
|
521
|
+
if (length > 64 && decoder)
|
|
522
|
+
return decoder.decode(src.subarray(position, position += length))
|
|
523
|
+
const end = position + length;
|
|
524
|
+
const units = [];
|
|
525
|
+
result = '';
|
|
526
|
+
while (position < end) {
|
|
527
|
+
const byte1 = src[position++];
|
|
528
|
+
if ((byte1 & 0x80) === 0) {
|
|
529
|
+
// 1 byte
|
|
530
|
+
units.push(byte1);
|
|
531
|
+
} else if ((byte1 & 0xe0) === 0xc0) {
|
|
532
|
+
// 2 bytes
|
|
533
|
+
const byte2 = src[position++] & 0x3f;
|
|
534
|
+
units.push(((byte1 & 0x1f) << 6) | byte2);
|
|
535
|
+
} else if ((byte1 & 0xf0) === 0xe0) {
|
|
536
|
+
// 3 bytes
|
|
537
|
+
const byte2 = src[position++] & 0x3f;
|
|
538
|
+
const byte3 = src[position++] & 0x3f;
|
|
539
|
+
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
|
|
540
|
+
} else if ((byte1 & 0xf8) === 0xf0) {
|
|
541
|
+
// 4 bytes
|
|
542
|
+
const byte2 = src[position++] & 0x3f;
|
|
543
|
+
const byte3 = src[position++] & 0x3f;
|
|
544
|
+
const byte4 = src[position++] & 0x3f;
|
|
545
|
+
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
|
546
|
+
if (unit > 0xffff) {
|
|
547
|
+
unit -= 0x10000;
|
|
548
|
+
units.push(((unit >>> 10) & 0x3ff) | 0xd800);
|
|
549
|
+
unit = 0xdc00 | (unit & 0x3ff);
|
|
550
|
+
}
|
|
551
|
+
units.push(unit);
|
|
552
|
+
} else {
|
|
553
|
+
units.push(byte1);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
if (units.length >= 0x1000) {
|
|
557
|
+
result += fromCharCode.apply(String, units);
|
|
558
|
+
units.length = 0;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (units.length > 0) {
|
|
563
|
+
result += fromCharCode.apply(String, units);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
return result
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function readArray(length) {
|
|
570
|
+
let array = new Array(length);
|
|
571
|
+
for (let i = 0; i < length; i++) {
|
|
572
|
+
array[i] = read();
|
|
573
|
+
}
|
|
574
|
+
if (currentUnpackr.freezeData)
|
|
575
|
+
return Object.freeze(array)
|
|
576
|
+
return array
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function readMap(length) {
|
|
580
|
+
if (currentUnpackr.mapsAsObjects) {
|
|
581
|
+
let object = {};
|
|
582
|
+
for (let i = 0; i < length; i++) {
|
|
583
|
+
object[readKey()] = read();
|
|
584
|
+
}
|
|
585
|
+
return object
|
|
586
|
+
} else {
|
|
587
|
+
let map = new Map();
|
|
588
|
+
for (let i = 0; i < length; i++) {
|
|
589
|
+
map.set(read(), read());
|
|
590
|
+
}
|
|
591
|
+
return map
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
var fromCharCode = String.fromCharCode;
|
|
596
|
+
function longStringInJS(length) {
|
|
597
|
+
let start = position;
|
|
598
|
+
let bytes = new Array(length);
|
|
599
|
+
for (let i = 0; i < length; i++) {
|
|
600
|
+
const byte = src[position++];
|
|
601
|
+
if ((byte & 0x80) > 0) {
|
|
602
|
+
position = start;
|
|
603
|
+
return
|
|
604
|
+
}
|
|
605
|
+
bytes[i] = byte;
|
|
606
|
+
}
|
|
607
|
+
return fromCharCode.apply(String, bytes)
|
|
608
|
+
}
|
|
609
|
+
function shortStringInJS(length) {
|
|
610
|
+
if (length < 4) {
|
|
611
|
+
if (length < 2) {
|
|
612
|
+
if (length === 0)
|
|
613
|
+
return ''
|
|
614
|
+
else {
|
|
615
|
+
let a = src[position++];
|
|
616
|
+
if ((a & 0x80) > 1) {
|
|
617
|
+
position -= 1;
|
|
618
|
+
return
|
|
619
|
+
}
|
|
620
|
+
return fromCharCode(a)
|
|
621
|
+
}
|
|
622
|
+
} else {
|
|
623
|
+
let a = src[position++];
|
|
624
|
+
let b = src[position++];
|
|
625
|
+
if ((a & 0x80) > 0 || (b & 0x80) > 0) {
|
|
626
|
+
position -= 2;
|
|
627
|
+
return
|
|
628
|
+
}
|
|
629
|
+
if (length < 3)
|
|
630
|
+
return fromCharCode(a, b)
|
|
631
|
+
let c = src[position++];
|
|
632
|
+
if ((c & 0x80) > 0) {
|
|
633
|
+
position -= 3;
|
|
634
|
+
return
|
|
635
|
+
}
|
|
636
|
+
return fromCharCode(a, b, c)
|
|
637
|
+
}
|
|
638
|
+
} else {
|
|
639
|
+
let a = src[position++];
|
|
640
|
+
let b = src[position++];
|
|
641
|
+
let c = src[position++];
|
|
642
|
+
let d = src[position++];
|
|
643
|
+
if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
|
|
644
|
+
position -= 4;
|
|
645
|
+
return
|
|
646
|
+
}
|
|
647
|
+
if (length < 6) {
|
|
648
|
+
if (length === 4)
|
|
649
|
+
return fromCharCode(a, b, c, d)
|
|
650
|
+
else {
|
|
651
|
+
let e = src[position++];
|
|
652
|
+
if ((e & 0x80) > 0) {
|
|
653
|
+
position -= 5;
|
|
654
|
+
return
|
|
655
|
+
}
|
|
656
|
+
return fromCharCode(a, b, c, d, e)
|
|
657
|
+
}
|
|
658
|
+
} else if (length < 8) {
|
|
659
|
+
let e = src[position++];
|
|
660
|
+
let f = src[position++];
|
|
661
|
+
if ((e & 0x80) > 0 || (f & 0x80) > 0) {
|
|
662
|
+
position -= 6;
|
|
663
|
+
return
|
|
664
|
+
}
|
|
665
|
+
if (length < 7)
|
|
666
|
+
return fromCharCode(a, b, c, d, e, f)
|
|
667
|
+
let g = src[position++];
|
|
668
|
+
if ((g & 0x80) > 0) {
|
|
669
|
+
position -= 7;
|
|
670
|
+
return
|
|
671
|
+
}
|
|
672
|
+
return fromCharCode(a, b, c, d, e, f, g)
|
|
673
|
+
} else {
|
|
674
|
+
let e = src[position++];
|
|
675
|
+
let f = src[position++];
|
|
676
|
+
let g = src[position++];
|
|
677
|
+
let h = src[position++];
|
|
678
|
+
if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
|
|
679
|
+
position -= 8;
|
|
680
|
+
return
|
|
681
|
+
}
|
|
682
|
+
if (length < 10) {
|
|
683
|
+
if (length === 8)
|
|
684
|
+
return fromCharCode(a, b, c, d, e, f, g, h)
|
|
685
|
+
else {
|
|
686
|
+
let i = src[position++];
|
|
687
|
+
if ((i & 0x80) > 0) {
|
|
688
|
+
position -= 9;
|
|
689
|
+
return
|
|
690
|
+
}
|
|
691
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i)
|
|
692
|
+
}
|
|
693
|
+
} else if (length < 12) {
|
|
694
|
+
let i = src[position++];
|
|
695
|
+
let j = src[position++];
|
|
696
|
+
if ((i & 0x80) > 0 || (j & 0x80) > 0) {
|
|
697
|
+
position -= 10;
|
|
698
|
+
return
|
|
699
|
+
}
|
|
700
|
+
if (length < 11)
|
|
701
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j)
|
|
702
|
+
let k = src[position++];
|
|
703
|
+
if ((k & 0x80) > 0) {
|
|
704
|
+
position -= 11;
|
|
705
|
+
return
|
|
706
|
+
}
|
|
707
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
|
|
708
|
+
} else {
|
|
709
|
+
let i = src[position++];
|
|
710
|
+
let j = src[position++];
|
|
711
|
+
let k = src[position++];
|
|
712
|
+
let l = src[position++];
|
|
713
|
+
if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
|
|
714
|
+
position -= 12;
|
|
715
|
+
return
|
|
716
|
+
}
|
|
717
|
+
if (length < 14) {
|
|
718
|
+
if (length === 12)
|
|
719
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
|
|
720
|
+
else {
|
|
721
|
+
let m = src[position++];
|
|
722
|
+
if ((m & 0x80) > 0) {
|
|
723
|
+
position -= 13;
|
|
724
|
+
return
|
|
725
|
+
}
|
|
726
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
|
|
727
|
+
}
|
|
728
|
+
} else {
|
|
729
|
+
let m = src[position++];
|
|
730
|
+
let n = src[position++];
|
|
731
|
+
if ((m & 0x80) > 0 || (n & 0x80) > 0) {
|
|
732
|
+
position -= 14;
|
|
733
|
+
return
|
|
734
|
+
}
|
|
735
|
+
if (length < 15)
|
|
736
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
|
|
737
|
+
let o = src[position++];
|
|
738
|
+
if ((o & 0x80) > 0) {
|
|
739
|
+
position -= 15;
|
|
740
|
+
return
|
|
741
|
+
}
|
|
742
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
function readOnlyJSString() {
|
|
750
|
+
let token = src[position++];
|
|
751
|
+
let length;
|
|
752
|
+
if (token < 0xc0) {
|
|
753
|
+
// fixstr
|
|
754
|
+
length = token - 0xa0;
|
|
755
|
+
} else {
|
|
756
|
+
switch(token) {
|
|
757
|
+
case 0xd9:
|
|
758
|
+
// str 8
|
|
759
|
+
length = src[position++];
|
|
760
|
+
break
|
|
761
|
+
case 0xda:
|
|
762
|
+
// str 16
|
|
763
|
+
length = dataView.getUint16(position);
|
|
764
|
+
position += 2;
|
|
765
|
+
break
|
|
766
|
+
case 0xdb:
|
|
767
|
+
// str 32
|
|
768
|
+
length = dataView.getUint32(position);
|
|
769
|
+
position += 4;
|
|
770
|
+
break
|
|
771
|
+
default:
|
|
772
|
+
throw new Error('Expected string')
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
return readStringJS(length)
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
function readBin(length) {
|
|
780
|
+
return currentUnpackr.copyBuffers ?
|
|
781
|
+
// specifically use the copying slice (not the node one)
|
|
782
|
+
Uint8Array.prototype.slice.call(src, position, position += length) :
|
|
783
|
+
src.subarray(position, position += length)
|
|
784
|
+
}
|
|
785
|
+
function readExt(length) {
|
|
786
|
+
let type = src[position++];
|
|
787
|
+
if (currentExtensions[type]) {
|
|
788
|
+
return currentExtensions[type](src.subarray(position, position += length))
|
|
789
|
+
}
|
|
790
|
+
else
|
|
791
|
+
throw new Error('Unknown extension type ' + type)
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
var keyCache = new Array(4096);
|
|
795
|
+
function readKey() {
|
|
796
|
+
let length = src[position++];
|
|
797
|
+
if (length >= 0xa0 && length < 0xc0) {
|
|
798
|
+
// fixstr, potentially use key cache
|
|
799
|
+
length = length - 0xa0;
|
|
800
|
+
if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
|
|
801
|
+
return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
|
|
802
|
+
else if (!(srcStringEnd == 0 && srcEnd < 180))
|
|
803
|
+
return readFixedString(length)
|
|
804
|
+
} else { // not cacheable, go back and do a standard read
|
|
805
|
+
position--;
|
|
806
|
+
return read()
|
|
807
|
+
}
|
|
808
|
+
let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
|
|
809
|
+
let entry = keyCache[key];
|
|
810
|
+
let checkPosition = position;
|
|
811
|
+
let end = position + length - 3;
|
|
812
|
+
let chunk;
|
|
813
|
+
let i = 0;
|
|
814
|
+
if (entry && entry.bytes == length) {
|
|
815
|
+
while (checkPosition < end) {
|
|
816
|
+
chunk = dataView.getUint32(checkPosition);
|
|
817
|
+
if (chunk != entry[i++]) {
|
|
818
|
+
checkPosition = 0x70000000;
|
|
819
|
+
break
|
|
820
|
+
}
|
|
821
|
+
checkPosition += 4;
|
|
822
|
+
}
|
|
823
|
+
end += 3;
|
|
824
|
+
while (checkPosition < end) {
|
|
825
|
+
chunk = src[checkPosition++];
|
|
826
|
+
if (chunk != entry[i++]) {
|
|
827
|
+
checkPosition = 0x70000000;
|
|
828
|
+
break
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
if (checkPosition === end) {
|
|
832
|
+
position = checkPosition;
|
|
833
|
+
return entry.string
|
|
834
|
+
}
|
|
835
|
+
end -= 3;
|
|
836
|
+
checkPosition = position;
|
|
837
|
+
}
|
|
838
|
+
entry = [];
|
|
839
|
+
keyCache[key] = entry;
|
|
840
|
+
entry.bytes = length;
|
|
841
|
+
while (checkPosition < end) {
|
|
842
|
+
chunk = dataView.getUint32(checkPosition);
|
|
843
|
+
entry.push(chunk);
|
|
844
|
+
checkPosition += 4;
|
|
845
|
+
}
|
|
846
|
+
end += 3;
|
|
847
|
+
while (checkPosition < end) {
|
|
848
|
+
chunk = src[checkPosition++];
|
|
849
|
+
entry.push(chunk);
|
|
850
|
+
}
|
|
851
|
+
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
852
|
+
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
|
|
853
|
+
if (string != null)
|
|
854
|
+
return entry.string = string
|
|
855
|
+
return entry.string = readFixedString(length)
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// the registration of the record definition extension (as "r")
|
|
859
|
+
const recordDefinition = (id, highByte) => {
|
|
860
|
+
var structure = read();
|
|
861
|
+
let firstByte = id;
|
|
862
|
+
if (highByte !== undefined) {
|
|
863
|
+
id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
|
|
864
|
+
structure.highByte = highByte;
|
|
865
|
+
}
|
|
866
|
+
let existingStructure = currentStructures[id];
|
|
867
|
+
if (existingStructure && existingStructure.isShared) {
|
|
868
|
+
(currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
|
|
869
|
+
}
|
|
870
|
+
currentStructures[id] = structure;
|
|
871
|
+
structure.read = createStructureReader(structure, firstByte);
|
|
872
|
+
return structure.read()
|
|
873
|
+
};
|
|
874
|
+
currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
|
|
875
|
+
currentExtensions[0].noBuffer = true;
|
|
876
|
+
|
|
877
|
+
currentExtensions[0x65] = () => {
|
|
878
|
+
let data = read();
|
|
879
|
+
return (globalThis[data[0]] || Error)(data[1])
|
|
880
|
+
};
|
|
881
|
+
|
|
882
|
+
currentExtensions[0x69] = (data) => {
|
|
883
|
+
// id extension (for structured clones)
|
|
884
|
+
let id = dataView.getUint32(position - 4);
|
|
885
|
+
if (!referenceMap)
|
|
886
|
+
referenceMap = new Map();
|
|
887
|
+
let token = src[position];
|
|
888
|
+
let target;
|
|
889
|
+
// TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
|
|
890
|
+
// ahead past references to record structure definitions
|
|
891
|
+
if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
|
|
892
|
+
target = [];
|
|
893
|
+
else
|
|
894
|
+
target = {};
|
|
895
|
+
|
|
896
|
+
let refEntry = { target }; // a placeholder object
|
|
897
|
+
referenceMap.set(id, refEntry);
|
|
898
|
+
let targetProperties = read(); // read the next value as the target object to id
|
|
899
|
+
if (refEntry.used) // there is a cycle, so we have to assign properties to original target
|
|
900
|
+
return Object.assign(target, targetProperties)
|
|
901
|
+
refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
|
|
902
|
+
return targetProperties // no cycle, can just use the returned read object
|
|
903
|
+
};
|
|
904
|
+
|
|
905
|
+
currentExtensions[0x70] = (data) => {
|
|
906
|
+
// pointer extension (for structured clones)
|
|
907
|
+
let id = dataView.getUint32(position - 4);
|
|
908
|
+
let refEntry = referenceMap.get(id);
|
|
909
|
+
refEntry.used = true;
|
|
910
|
+
return refEntry.target
|
|
911
|
+
};
|
|
912
|
+
|
|
913
|
+
currentExtensions[0x73] = () => new Set(read());
|
|
914
|
+
|
|
915
|
+
const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
|
|
916
|
+
|
|
917
|
+
currentExtensions[0x74] = (data) => {
|
|
918
|
+
let typeCode = data[0];
|
|
919
|
+
let typedArrayName = typedArrays[typeCode];
|
|
920
|
+
if (!typedArrayName)
|
|
921
|
+
throw new Error('Could not find typed array for code ' + typeCode)
|
|
922
|
+
// we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
|
|
923
|
+
return new globalThis[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
|
|
924
|
+
};
|
|
925
|
+
currentExtensions[0x78] = () => {
|
|
926
|
+
let data = read();
|
|
927
|
+
return new RegExp(data[0], data[1])
|
|
928
|
+
};
|
|
929
|
+
const TEMP_BUNDLE = [];
|
|
930
|
+
currentExtensions[0x62] = (data) => {
|
|
931
|
+
let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
|
|
932
|
+
let dataPosition = position;
|
|
933
|
+
position += dataSize - data.length;
|
|
934
|
+
bundledStrings = TEMP_BUNDLE;
|
|
935
|
+
bundledStrings = [readOnlyJSString(), readOnlyJSString()];
|
|
936
|
+
bundledStrings.position0 = 0;
|
|
937
|
+
bundledStrings.position1 = 0;
|
|
938
|
+
bundledStrings.postBundlePosition = position;
|
|
939
|
+
position = dataPosition;
|
|
940
|
+
return read()
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
currentExtensions[0xff] = (data) => {
|
|
944
|
+
// 32-bit date extension
|
|
945
|
+
if (data.length == 4)
|
|
946
|
+
return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
|
|
947
|
+
else if (data.length == 8)
|
|
948
|
+
return new Date(
|
|
949
|
+
((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
|
|
950
|
+
((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
|
|
951
|
+
else if (data.length == 12)// TODO: Implement support for negative
|
|
952
|
+
return new Date(
|
|
953
|
+
((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
|
|
954
|
+
(((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
|
|
955
|
+
else
|
|
956
|
+
return new Date('invalid')
|
|
957
|
+
}; // notepack defines extension 0 to mean undefined, so use that as the default here
|
|
958
|
+
// registration of bulk record definition?
|
|
959
|
+
// currentExtensions[0x52] = () =>
|
|
960
|
+
|
|
961
|
+
function saveState(callback) {
|
|
962
|
+
let savedSrcEnd = srcEnd;
|
|
963
|
+
let savedPosition = position;
|
|
964
|
+
let savedSrcStringStart = srcStringStart;
|
|
965
|
+
let savedSrcStringEnd = srcStringEnd;
|
|
966
|
+
let savedSrcString = srcString;
|
|
967
|
+
let savedReferenceMap = referenceMap;
|
|
968
|
+
let savedBundledStrings = bundledStrings;
|
|
969
|
+
|
|
970
|
+
// TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
|
|
971
|
+
let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
|
|
972
|
+
let savedStructures = currentStructures;
|
|
973
|
+
let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
|
|
974
|
+
let savedPackr = currentUnpackr;
|
|
975
|
+
let savedSequentialMode = sequentialMode;
|
|
976
|
+
let value = callback();
|
|
977
|
+
srcEnd = savedSrcEnd;
|
|
978
|
+
position = savedPosition;
|
|
979
|
+
srcStringStart = savedSrcStringStart;
|
|
980
|
+
srcStringEnd = savedSrcStringEnd;
|
|
981
|
+
srcString = savedSrcString;
|
|
982
|
+
referenceMap = savedReferenceMap;
|
|
983
|
+
bundledStrings = savedBundledStrings;
|
|
984
|
+
src = savedSrc;
|
|
985
|
+
sequentialMode = savedSequentialMode;
|
|
986
|
+
currentStructures = savedStructures;
|
|
987
|
+
currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
|
|
988
|
+
currentUnpackr = savedPackr;
|
|
989
|
+
dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
|
|
990
|
+
return value
|
|
991
|
+
}
|
|
992
|
+
function clearSource() {
|
|
993
|
+
src = null;
|
|
994
|
+
referenceMap = null;
|
|
995
|
+
currentStructures = null;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
|
|
999
|
+
for (let i = 0; i < 256; i++) {
|
|
1000
|
+
mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
|
|
1001
|
+
}
|
|
1002
|
+
var defaultUnpackr = new Unpackr({ useRecords: false });
|
|
1003
|
+
function setReadStruct(func) {
|
|
1004
|
+
readStruct = func;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
let textEncoder;
|
|
1008
|
+
try {
|
|
1009
|
+
textEncoder = new TextEncoder();
|
|
1010
|
+
} catch (error) {}
|
|
1011
|
+
let extensions, extensionClasses;
|
|
1012
|
+
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
1013
|
+
const ByteArrayAllocate = hasNodeBuffer ?
|
|
1014
|
+
function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
|
|
1015
|
+
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
1016
|
+
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1017
|
+
let target, keysTarget;
|
|
1018
|
+
let targetView;
|
|
1019
|
+
let position$1 = 0;
|
|
1020
|
+
let safeEnd;
|
|
1021
|
+
let bundledStrings$1 = null;
|
|
1022
|
+
let writeStructSlots;
|
|
1023
|
+
const MAX_BUNDLE_SIZE = 0xf000;
|
|
1024
|
+
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1025
|
+
const RECORD_SYMBOL = Symbol('record-id');
|
|
1026
|
+
class Packr extends Unpackr {
|
|
1027
|
+
constructor(options) {
|
|
1028
|
+
super(options);
|
|
1029
|
+
this.offset = 0;
|
|
1030
|
+
let start;
|
|
1031
|
+
let hasSharedUpdate;
|
|
1032
|
+
let structures;
|
|
1033
|
+
let referenceMap;
|
|
1034
|
+
let lastSharedStructuresLength = 0;
|
|
1035
|
+
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
|
|
1036
|
+
return target.utf8Write(string, position, 0xffffffff)
|
|
1037
|
+
} : (textEncoder && textEncoder.encodeInto) ?
|
|
1038
|
+
function(string, position) {
|
|
1039
|
+
return textEncoder.encodeInto(string, target.subarray(position)).written
|
|
1040
|
+
} : false;
|
|
1041
|
+
|
|
1042
|
+
let packr = this;
|
|
1043
|
+
if (!options)
|
|
1044
|
+
options = {};
|
|
1045
|
+
let isSequential = options && options.sequential;
|
|
1046
|
+
let hasSharedStructures = options.structures || options.saveStructures;
|
|
1047
|
+
let maxSharedStructures = options.maxSharedStructures;
|
|
1048
|
+
if (maxSharedStructures == null)
|
|
1049
|
+
maxSharedStructures = hasSharedStructures ? 32 : 0;
|
|
1050
|
+
if (maxSharedStructures > 8160)
|
|
1051
|
+
throw new Error('Maximum maxSharedStructure is 8160')
|
|
1052
|
+
if (options.structuredClone && options.moreTypes == undefined) {
|
|
1053
|
+
options.moreTypes = true;
|
|
1054
|
+
}
|
|
1055
|
+
let maxOwnStructures = options.maxOwnStructures;
|
|
1056
|
+
if (maxOwnStructures == null)
|
|
1057
|
+
maxOwnStructures = hasSharedStructures ? 32 : 64;
|
|
1058
|
+
if (!this.structures && options.useRecords != false)
|
|
1059
|
+
this.structures = [];
|
|
1060
|
+
// two byte record ids for shared structures
|
|
1061
|
+
let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
|
|
1062
|
+
let sharedLimitId = maxSharedStructures + 0x40;
|
|
1063
|
+
let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
|
|
1064
|
+
if (maxStructureId > 8256) {
|
|
1065
|
+
throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
|
|
1066
|
+
}
|
|
1067
|
+
let recordIdsToRemove = [];
|
|
1068
|
+
let transitionsCount = 0;
|
|
1069
|
+
let serializationsSinceTransitionRebuild = 0;
|
|
1070
|
+
|
|
1071
|
+
this.pack = this.encode = function(value, encodeOptions) {
|
|
1072
|
+
if (!target) {
|
|
1073
|
+
target = new ByteArrayAllocate(8192);
|
|
1074
|
+
targetView = target.dataView = new DataView(target.buffer, 0, 8192);
|
|
1075
|
+
position$1 = 0;
|
|
1076
|
+
}
|
|
1077
|
+
safeEnd = target.length - 10;
|
|
1078
|
+
if (safeEnd - position$1 < 0x800) {
|
|
1079
|
+
// don't start too close to the end,
|
|
1080
|
+
target = new ByteArrayAllocate(target.length);
|
|
1081
|
+
targetView = target.dataView = new DataView(target.buffer, 0, target.length);
|
|
1082
|
+
safeEnd = target.length - 10;
|
|
1083
|
+
position$1 = 0;
|
|
1084
|
+
} else
|
|
1085
|
+
position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
|
|
1086
|
+
start = position$1;
|
|
1087
|
+
referenceMap = packr.structuredClone ? new Map() : null;
|
|
1088
|
+
if (packr.bundleStrings && typeof value !== 'string') {
|
|
1089
|
+
bundledStrings$1 = [];
|
|
1090
|
+
bundledStrings$1.size = Infinity; // force a new bundle start on first string
|
|
1091
|
+
} else
|
|
1092
|
+
bundledStrings$1 = null;
|
|
1093
|
+
structures = packr.structures;
|
|
1094
|
+
if (structures) {
|
|
1095
|
+
if (structures.uninitialized)
|
|
1096
|
+
structures = packr._mergeStructures(packr.getStructures());
|
|
1097
|
+
let sharedLength = structures.sharedLength || 0;
|
|
1098
|
+
if (sharedLength > maxSharedStructures) {
|
|
1099
|
+
//if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
|
|
1100
|
+
throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
|
|
1101
|
+
}
|
|
1102
|
+
if (!structures.transitions) {
|
|
1103
|
+
// rebuild our structure transitions
|
|
1104
|
+
structures.transitions = Object.create(null);
|
|
1105
|
+
for (let i = 0; i < sharedLength; i++) {
|
|
1106
|
+
let keys = structures[i];
|
|
1107
|
+
if (!keys)
|
|
1108
|
+
continue
|
|
1109
|
+
let nextTransition, transition = structures.transitions;
|
|
1110
|
+
for (let j = 0, l = keys.length; j < l; j++) {
|
|
1111
|
+
let key = keys[j];
|
|
1112
|
+
nextTransition = transition[key];
|
|
1113
|
+
if (!nextTransition) {
|
|
1114
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1115
|
+
}
|
|
1116
|
+
transition = nextTransition;
|
|
1117
|
+
}
|
|
1118
|
+
transition[RECORD_SYMBOL] = i + 0x40;
|
|
1119
|
+
}
|
|
1120
|
+
lastSharedStructuresLength = sharedLength;
|
|
1121
|
+
}
|
|
1122
|
+
if (!isSequential) {
|
|
1123
|
+
structures.nextId = sharedLength + 0x40;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
if (hasSharedUpdate)
|
|
1127
|
+
hasSharedUpdate = false;
|
|
1128
|
+
try {
|
|
1129
|
+
if (packr.randomAccessStructure)
|
|
1130
|
+
writeStruct(value);
|
|
1131
|
+
else
|
|
1132
|
+
pack(value);
|
|
1133
|
+
if (bundledStrings$1) {
|
|
1134
|
+
writeBundles(start, pack);
|
|
1135
|
+
}
|
|
1136
|
+
packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1137
|
+
if (referenceMap && referenceMap.idsToInsert) {
|
|
1138
|
+
position$1 += referenceMap.idsToInsert.length * 6;
|
|
1139
|
+
if (position$1 > safeEnd)
|
|
1140
|
+
makeRoom(position$1);
|
|
1141
|
+
packr.offset = position$1;
|
|
1142
|
+
let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
|
|
1143
|
+
referenceMap = null;
|
|
1144
|
+
return serialized
|
|
1145
|
+
}
|
|
1146
|
+
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1147
|
+
target.start = start;
|
|
1148
|
+
target.end = position$1;
|
|
1149
|
+
return target
|
|
1150
|
+
}
|
|
1151
|
+
return target.subarray(start, position$1) // position can change if we call pack again in saveStructures, so we get the buffer now
|
|
1152
|
+
} finally {
|
|
1153
|
+
if (structures) {
|
|
1154
|
+
if (serializationsSinceTransitionRebuild < 10)
|
|
1155
|
+
serializationsSinceTransitionRebuild++;
|
|
1156
|
+
let sharedLength = structures.sharedLength || maxSharedStructures;
|
|
1157
|
+
if (structures.length > sharedLength)
|
|
1158
|
+
structures.length = sharedLength;
|
|
1159
|
+
if (transitionsCount > 10000) {
|
|
1160
|
+
// force a rebuild occasionally after a lot of transitions so it can get cleaned up
|
|
1161
|
+
structures.transitions = null;
|
|
1162
|
+
serializationsSinceTransitionRebuild = 0;
|
|
1163
|
+
transitionsCount = 0;
|
|
1164
|
+
if (recordIdsToRemove.length > 0)
|
|
1165
|
+
recordIdsToRemove = [];
|
|
1166
|
+
} else if (recordIdsToRemove.length > 0 && !isSequential) {
|
|
1167
|
+
for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
|
|
1168
|
+
recordIdsToRemove[i][RECORD_SYMBOL] = 0;
|
|
1169
|
+
}
|
|
1170
|
+
recordIdsToRemove = [];
|
|
1171
|
+
}
|
|
1172
|
+
if (hasSharedUpdate && packr.saveStructures) {
|
|
1173
|
+
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1174
|
+
let returnBuffer = target.subarray(start, position$1);
|
|
1175
|
+
if (packr.saveStructures(structures, lastSharedStructuresLength) === false) {
|
|
1176
|
+
// get updated structures and try again if the update failed
|
|
1177
|
+
packr._mergeStructures(packr.getStructures());
|
|
1178
|
+
return packr.pack(value)
|
|
1179
|
+
}
|
|
1180
|
+
lastSharedStructuresLength = sharedLength;
|
|
1181
|
+
return returnBuffer
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
if (encodeOptions & RESET_BUFFER_MODE)
|
|
1185
|
+
position$1 = start;
|
|
1186
|
+
}
|
|
1187
|
+
};
|
|
1188
|
+
const pack = (value) => {
|
|
1189
|
+
if (position$1 > safeEnd)
|
|
1190
|
+
target = makeRoom(position$1);
|
|
1191
|
+
|
|
1192
|
+
var type = typeof value;
|
|
1193
|
+
var length;
|
|
1194
|
+
if (type === 'string') {
|
|
1195
|
+
let strLength = value.length;
|
|
1196
|
+
if (bundledStrings$1 && strLength >= 4 && strLength < 0x1000) {
|
|
1197
|
+
if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
1198
|
+
let extStart;
|
|
1199
|
+
let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
|
|
1200
|
+
if (position$1 + maxBytes > safeEnd)
|
|
1201
|
+
target = makeRoom(position$1 + maxBytes);
|
|
1202
|
+
if (bundledStrings$1.position) { // here we use the 0x62 extension to write the last bundle and reserve sapce for the reference pointer to the next/current bundle
|
|
1203
|
+
target[position$1] = 0xc8; // ext 16
|
|
1204
|
+
position$1 += 3; // reserve for the writing bundle size
|
|
1205
|
+
target[position$1++] = 0x62; // 'b'
|
|
1206
|
+
extStart = position$1 - start;
|
|
1207
|
+
position$1 += 4; // reserve for writing bundle reference
|
|
1208
|
+
writeBundles(start, pack); // write the last bundles
|
|
1209
|
+
targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
|
|
1210
|
+
} 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)
|
|
1211
|
+
target[position$1++] = 0xd6; // fixext 4
|
|
1212
|
+
target[position$1++] = 0x62; // 'b'
|
|
1213
|
+
extStart = position$1 - start;
|
|
1214
|
+
position$1 += 4; // reserve for writing bundle reference
|
|
1215
|
+
}
|
|
1216
|
+
bundledStrings$1 = ['', '']; // create new ones
|
|
1217
|
+
bundledStrings$1.size = 0;
|
|
1218
|
+
bundledStrings$1.position = extStart;
|
|
1219
|
+
}
|
|
1220
|
+
let twoByte = hasNonLatin.test(value);
|
|
1221
|
+
bundledStrings$1[twoByte ? 0 : 1] += value;
|
|
1222
|
+
target[position$1++] = 0xc1;
|
|
1223
|
+
pack(twoByte ? -strLength : strLength);
|
|
1224
|
+
return
|
|
1225
|
+
}
|
|
1226
|
+
let headerSize;
|
|
1227
|
+
// first we estimate the header size, so we can write to the correct location
|
|
1228
|
+
if (strLength < 0x20) {
|
|
1229
|
+
headerSize = 1;
|
|
1230
|
+
} else if (strLength < 0x100) {
|
|
1231
|
+
headerSize = 2;
|
|
1232
|
+
} else if (strLength < 0x10000) {
|
|
1233
|
+
headerSize = 3;
|
|
1234
|
+
} else {
|
|
1235
|
+
headerSize = 5;
|
|
1236
|
+
}
|
|
1237
|
+
let maxBytes = strLength * 3;
|
|
1238
|
+
if (position$1 + maxBytes > safeEnd)
|
|
1239
|
+
target = makeRoom(position$1 + maxBytes);
|
|
1240
|
+
|
|
1241
|
+
if (strLength < 0x40 || !encodeUtf8) {
|
|
1242
|
+
let i, c1, c2, strPosition = position$1 + headerSize;
|
|
1243
|
+
for (i = 0; i < strLength; i++) {
|
|
1244
|
+
c1 = value.charCodeAt(i);
|
|
1245
|
+
if (c1 < 0x80) {
|
|
1246
|
+
target[strPosition++] = c1;
|
|
1247
|
+
} else if (c1 < 0x800) {
|
|
1248
|
+
target[strPosition++] = c1 >> 6 | 0xc0;
|
|
1249
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1250
|
+
} else if (
|
|
1251
|
+
(c1 & 0xfc00) === 0xd800 &&
|
|
1252
|
+
((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
|
1253
|
+
) {
|
|
1254
|
+
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
1255
|
+
i++;
|
|
1256
|
+
target[strPosition++] = c1 >> 18 | 0xf0;
|
|
1257
|
+
target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
|
|
1258
|
+
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
1259
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1260
|
+
} else {
|
|
1261
|
+
target[strPosition++] = c1 >> 12 | 0xe0;
|
|
1262
|
+
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
1263
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
length = strPosition - position$1 - headerSize;
|
|
1267
|
+
} else {
|
|
1268
|
+
length = encodeUtf8(value, position$1 + headerSize);
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
if (length < 0x20) {
|
|
1272
|
+
target[position$1++] = 0xa0 | length;
|
|
1273
|
+
} else if (length < 0x100) {
|
|
1274
|
+
if (headerSize < 2) {
|
|
1275
|
+
target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
|
|
1276
|
+
}
|
|
1277
|
+
target[position$1++] = 0xd9;
|
|
1278
|
+
target[position$1++] = length;
|
|
1279
|
+
} else if (length < 0x10000) {
|
|
1280
|
+
if (headerSize < 3) {
|
|
1281
|
+
target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
|
|
1282
|
+
}
|
|
1283
|
+
target[position$1++] = 0xda;
|
|
1284
|
+
target[position$1++] = length >> 8;
|
|
1285
|
+
target[position$1++] = length & 0xff;
|
|
1286
|
+
} else {
|
|
1287
|
+
if (headerSize < 5) {
|
|
1288
|
+
target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
|
|
1289
|
+
}
|
|
1290
|
+
target[position$1++] = 0xdb;
|
|
1291
|
+
targetView.setUint32(position$1, length);
|
|
1292
|
+
position$1 += 4;
|
|
1293
|
+
}
|
|
1294
|
+
position$1 += length;
|
|
1295
|
+
} else if (type === 'number') {
|
|
1296
|
+
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1297
|
+
// positive uint
|
|
1298
|
+
if (value < 0x40 || (value < 0x80 && this.useRecords === false)) {
|
|
1299
|
+
target[position$1++] = value;
|
|
1300
|
+
} else if (value < 0x100) {
|
|
1301
|
+
target[position$1++] = 0xcc;
|
|
1302
|
+
target[position$1++] = value;
|
|
1303
|
+
} else if (value < 0x10000) {
|
|
1304
|
+
target[position$1++] = 0xcd;
|
|
1305
|
+
target[position$1++] = value >> 8;
|
|
1306
|
+
target[position$1++] = value & 0xff;
|
|
1307
|
+
} else {
|
|
1308
|
+
target[position$1++] = 0xce;
|
|
1309
|
+
targetView.setUint32(position$1, value);
|
|
1310
|
+
position$1 += 4;
|
|
1311
|
+
}
|
|
1312
|
+
} else if (value >> 0 === value) { // negative integer
|
|
1313
|
+
if (value >= -0x20) {
|
|
1314
|
+
target[position$1++] = 0x100 + value;
|
|
1315
|
+
} else if (value >= -0x80) {
|
|
1316
|
+
target[position$1++] = 0xd0;
|
|
1317
|
+
target[position$1++] = value + 0x100;
|
|
1318
|
+
} else if (value >= -0x8000) {
|
|
1319
|
+
target[position$1++] = 0xd1;
|
|
1320
|
+
targetView.setInt16(position$1, value);
|
|
1321
|
+
position$1 += 2;
|
|
1322
|
+
} else {
|
|
1323
|
+
target[position$1++] = 0xd2;
|
|
1324
|
+
targetView.setInt32(position$1, value);
|
|
1325
|
+
position$1 += 4;
|
|
1326
|
+
}
|
|
1327
|
+
} else {
|
|
1328
|
+
let useFloat32;
|
|
1329
|
+
if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
|
|
1330
|
+
target[position$1++] = 0xca;
|
|
1331
|
+
targetView.setFloat32(position$1, value);
|
|
1332
|
+
let xShifted;
|
|
1333
|
+
if (useFloat32 < 4 ||
|
|
1334
|
+
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1335
|
+
((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
|
|
1336
|
+
position$1 += 4;
|
|
1337
|
+
return
|
|
1338
|
+
} else
|
|
1339
|
+
position$1--; // move back into position for writing a double
|
|
1340
|
+
}
|
|
1341
|
+
target[position$1++] = 0xcb;
|
|
1342
|
+
targetView.setFloat64(position$1, value);
|
|
1343
|
+
position$1 += 8;
|
|
1344
|
+
}
|
|
1345
|
+
} else if (type === 'object') {
|
|
1346
|
+
if (!value)
|
|
1347
|
+
target[position$1++] = 0xc0;
|
|
1348
|
+
else {
|
|
1349
|
+
if (referenceMap) {
|
|
1350
|
+
let referee = referenceMap.get(value);
|
|
1351
|
+
if (referee) {
|
|
1352
|
+
if (!referee.id) {
|
|
1353
|
+
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
|
|
1354
|
+
referee.id = idsToInsert.push(referee);
|
|
1355
|
+
}
|
|
1356
|
+
target[position$1++] = 0xd6; // fixext 4
|
|
1357
|
+
target[position$1++] = 0x70; // "p" for pointer
|
|
1358
|
+
targetView.setUint32(position$1, referee.id);
|
|
1359
|
+
position$1 += 4;
|
|
1360
|
+
return
|
|
1361
|
+
} else
|
|
1362
|
+
referenceMap.set(value, { offset: position$1 - start });
|
|
1363
|
+
}
|
|
1364
|
+
let constructor = value.constructor;
|
|
1365
|
+
if (constructor === Object) {
|
|
1366
|
+
writeObject(value, true);
|
|
1367
|
+
} else if (constructor === Array) {
|
|
1368
|
+
length = value.length;
|
|
1369
|
+
if (length < 0x10) {
|
|
1370
|
+
target[position$1++] = 0x90 | length;
|
|
1371
|
+
} else if (length < 0x10000) {
|
|
1372
|
+
target[position$1++] = 0xdc;
|
|
1373
|
+
target[position$1++] = length >> 8;
|
|
1374
|
+
target[position$1++] = length & 0xff;
|
|
1375
|
+
} else {
|
|
1376
|
+
target[position$1++] = 0xdd;
|
|
1377
|
+
targetView.setUint32(position$1, length);
|
|
1378
|
+
position$1 += 4;
|
|
1379
|
+
}
|
|
1380
|
+
for (let i = 0; i < length; i++) {
|
|
1381
|
+
pack(value[i]);
|
|
1382
|
+
}
|
|
1383
|
+
} else if (constructor === Map) {
|
|
1384
|
+
length = value.size;
|
|
1385
|
+
if (length < 0x10) {
|
|
1386
|
+
target[position$1++] = 0x80 | length;
|
|
1387
|
+
} else if (length < 0x10000) {
|
|
1388
|
+
target[position$1++] = 0xde;
|
|
1389
|
+
target[position$1++] = length >> 8;
|
|
1390
|
+
target[position$1++] = length & 0xff;
|
|
1391
|
+
} else {
|
|
1392
|
+
target[position$1++] = 0xdf;
|
|
1393
|
+
targetView.setUint32(position$1, length);
|
|
1394
|
+
position$1 += 4;
|
|
1395
|
+
}
|
|
1396
|
+
for (let [ key, entryValue ] of value) {
|
|
1397
|
+
pack(key);
|
|
1398
|
+
pack(entryValue);
|
|
1399
|
+
}
|
|
1400
|
+
} else {
|
|
1401
|
+
for (let i = 0, l = extensions.length; i < l; i++) {
|
|
1402
|
+
let extensionClass = extensionClasses[i];
|
|
1403
|
+
if (value instanceof extensionClass) {
|
|
1404
|
+
let extension = extensions[i];
|
|
1405
|
+
if (extension.write) {
|
|
1406
|
+
if (extension.type) {
|
|
1407
|
+
target[position$1++] = 0xd4; // one byte "tag" extension
|
|
1408
|
+
target[position$1++] = extension.type;
|
|
1409
|
+
target[position$1++] = 0;
|
|
1410
|
+
}
|
|
1411
|
+
pack(extension.write.call(this, value));
|
|
1412
|
+
return
|
|
1413
|
+
}
|
|
1414
|
+
let currentTarget = target;
|
|
1415
|
+
let currentTargetView = targetView;
|
|
1416
|
+
let currentPosition = position$1;
|
|
1417
|
+
target = null;
|
|
1418
|
+
let result;
|
|
1419
|
+
try {
|
|
1420
|
+
result = extension.pack.call(this, value, (size) => {
|
|
1421
|
+
// restore target and use it
|
|
1422
|
+
target = currentTarget;
|
|
1423
|
+
currentTarget = null;
|
|
1424
|
+
position$1 += size;
|
|
1425
|
+
if (position$1 > safeEnd)
|
|
1426
|
+
makeRoom(position$1);
|
|
1427
|
+
return {
|
|
1428
|
+
target, targetView, position: position$1 - size
|
|
1429
|
+
}
|
|
1430
|
+
}, pack);
|
|
1431
|
+
} finally {
|
|
1432
|
+
// restore current target information (unless already restored)
|
|
1433
|
+
if (currentTarget) {
|
|
1434
|
+
target = currentTarget;
|
|
1435
|
+
targetView = currentTargetView;
|
|
1436
|
+
position$1 = currentPosition;
|
|
1437
|
+
safeEnd = target.length - 10;
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
if (result) {
|
|
1441
|
+
if (result.length + position$1 > safeEnd)
|
|
1442
|
+
makeRoom(result.length + position$1);
|
|
1443
|
+
position$1 = writeExtensionData(result, target, position$1, extension.type);
|
|
1444
|
+
}
|
|
1445
|
+
return
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
// no extension found, write as object
|
|
1449
|
+
writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
} else if (type === 'boolean') {
|
|
1453
|
+
target[position$1++] = value ? 0xc3 : 0xc2;
|
|
1454
|
+
} else if (type === 'bigint') {
|
|
1455
|
+
if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
|
|
1456
|
+
// use a signed int as long as it fits
|
|
1457
|
+
target[position$1++] = 0xd3;
|
|
1458
|
+
targetView.setBigInt64(position$1, value);
|
|
1459
|
+
} else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
|
|
1460
|
+
// if we can fit an unsigned int, use that
|
|
1461
|
+
target[position$1++] = 0xcf;
|
|
1462
|
+
targetView.setBigUint64(position$1, value);
|
|
1463
|
+
} else {
|
|
1464
|
+
// overflow
|
|
1465
|
+
if (this.largeBigIntToFloat) {
|
|
1466
|
+
target[position$1++] = 0xcb;
|
|
1467
|
+
targetView.setFloat64(position$1, Number(value));
|
|
1468
|
+
} else {
|
|
1469
|
+
throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
position$1 += 8;
|
|
1473
|
+
} else if (type === 'undefined') {
|
|
1474
|
+
if (this.encodeUndefinedAsNil)
|
|
1475
|
+
target[position$1++] = 0xc0;
|
|
1476
|
+
else {
|
|
1477
|
+
target[position$1++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
|
|
1478
|
+
target[position$1++] = 0;
|
|
1479
|
+
target[position$1++] = 0;
|
|
1480
|
+
}
|
|
1481
|
+
} else if (type === 'function') {
|
|
1482
|
+
pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
|
|
1483
|
+
} else {
|
|
1484
|
+
throw new Error('Unknown type: ' + type)
|
|
1485
|
+
}
|
|
1486
|
+
};
|
|
1487
|
+
|
|
1488
|
+
const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
|
|
1489
|
+
// this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
|
|
1490
|
+
let keys = Object.keys(object);
|
|
1491
|
+
let length = keys.length;
|
|
1492
|
+
if (length < 0x10) {
|
|
1493
|
+
target[position$1++] = 0x80 | length;
|
|
1494
|
+
} else if (length < 0x10000) {
|
|
1495
|
+
target[position$1++] = 0xde;
|
|
1496
|
+
target[position$1++] = length >> 8;
|
|
1497
|
+
target[position$1++] = length & 0xff;
|
|
1498
|
+
} else {
|
|
1499
|
+
target[position$1++] = 0xdf;
|
|
1500
|
+
targetView.setUint32(position$1, length);
|
|
1501
|
+
position$1 += 4;
|
|
1502
|
+
}
|
|
1503
|
+
let key;
|
|
1504
|
+
for (let i = 0; i < length; i++) {
|
|
1505
|
+
pack(key = keys[i]);
|
|
1506
|
+
pack(object[key]);
|
|
1507
|
+
}
|
|
1508
|
+
} :
|
|
1509
|
+
(object, safePrototype) => {
|
|
1510
|
+
target[position$1++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
|
|
1511
|
+
let objectOffset = position$1 - start;
|
|
1512
|
+
position$1 += 2;
|
|
1513
|
+
let size = 0;
|
|
1514
|
+
for (let key in object) {
|
|
1515
|
+
if (safePrototype || object.hasOwnProperty(key)) {
|
|
1516
|
+
pack(key);
|
|
1517
|
+
pack(object[key]);
|
|
1518
|
+
size++;
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
target[objectOffset++ + start] = size >> 8;
|
|
1522
|
+
target[objectOffset + start] = size & 0xff;
|
|
1523
|
+
} :
|
|
1524
|
+
(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)
|
|
1525
|
+
(object, safePrototype) => {
|
|
1526
|
+
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
1527
|
+
let objectOffset = position$1++ - start;
|
|
1528
|
+
let wroteKeys;
|
|
1529
|
+
for (let key in object) {
|
|
1530
|
+
if (safePrototype || object.hasOwnProperty(key)) {
|
|
1531
|
+
nextTransition = transition[key];
|
|
1532
|
+
if (nextTransition)
|
|
1533
|
+
transition = nextTransition;
|
|
1534
|
+
else {
|
|
1535
|
+
// record doesn't exist, create full new record and insert it
|
|
1536
|
+
let keys = Object.keys(object);
|
|
1537
|
+
let lastTransition = transition;
|
|
1538
|
+
transition = structures.transitions;
|
|
1539
|
+
let newTransitions = 0;
|
|
1540
|
+
for (let i = 0, l = keys.length; i < l; i++) {
|
|
1541
|
+
let key = keys[i];
|
|
1542
|
+
nextTransition = transition[key];
|
|
1543
|
+
if (!nextTransition) {
|
|
1544
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1545
|
+
newTransitions++;
|
|
1546
|
+
}
|
|
1547
|
+
transition = nextTransition;
|
|
1548
|
+
}
|
|
1549
|
+
if (objectOffset + start + 1 == position$1) {
|
|
1550
|
+
// first key, so we don't need to insert, we can just write record directly
|
|
1551
|
+
position$1--;
|
|
1552
|
+
newRecord(transition, keys, newTransitions);
|
|
1553
|
+
} else // otherwise we need to insert the record, moving existing data after the record
|
|
1554
|
+
insertNewRecord(transition, keys, objectOffset, newTransitions);
|
|
1555
|
+
wroteKeys = true;
|
|
1556
|
+
transition = lastTransition[key];
|
|
1557
|
+
}
|
|
1558
|
+
pack(object[key]);
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
if (!wroteKeys) {
|
|
1562
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
1563
|
+
if (recordId)
|
|
1564
|
+
target[objectOffset + start] = recordId;
|
|
1565
|
+
else
|
|
1566
|
+
insertNewRecord(transition, Object.keys(object), objectOffset, 0);
|
|
1567
|
+
}
|
|
1568
|
+
} :
|
|
1569
|
+
(object, safePrototype) => {
|
|
1570
|
+
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
1571
|
+
let newTransitions = 0;
|
|
1572
|
+
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
1573
|
+
nextTransition = transition[key];
|
|
1574
|
+
if (!nextTransition) {
|
|
1575
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1576
|
+
newTransitions++;
|
|
1577
|
+
}
|
|
1578
|
+
transition = nextTransition;
|
|
1579
|
+
}
|
|
1580
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
1581
|
+
if (recordId) {
|
|
1582
|
+
if (recordId >= 0x60 && useTwoByteRecords) {
|
|
1583
|
+
target[position$1++] = ((recordId -= 0x60) & 0x1f) + 0x60;
|
|
1584
|
+
target[position$1++] = recordId >> 5;
|
|
1585
|
+
} else
|
|
1586
|
+
target[position$1++] = recordId;
|
|
1587
|
+
} else {
|
|
1588
|
+
newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
|
|
1589
|
+
}
|
|
1590
|
+
// now write the values
|
|
1591
|
+
for (let key in object)
|
|
1592
|
+
if (safePrototype || object.hasOwnProperty(key))
|
|
1593
|
+
pack(object[key]);
|
|
1594
|
+
};
|
|
1595
|
+
const makeRoom = (end) => {
|
|
1596
|
+
let newSize;
|
|
1597
|
+
if (end > 0x1000000) {
|
|
1598
|
+
// special handling for really large buffers
|
|
1599
|
+
if ((end - start) > MAX_BUFFER_SIZE)
|
|
1600
|
+
throw new Error('Packed buffer would be larger than maximum buffer size')
|
|
1601
|
+
newSize = Math.min(MAX_BUFFER_SIZE,
|
|
1602
|
+
Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
|
|
1603
|
+
} else // faster handling for smaller buffers
|
|
1604
|
+
newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
|
|
1605
|
+
let newBuffer = new ByteArrayAllocate(newSize);
|
|
1606
|
+
targetView = newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize);
|
|
1607
|
+
end = Math.min(end, target.length);
|
|
1608
|
+
if (target.copy)
|
|
1609
|
+
target.copy(newBuffer, 0, start, end);
|
|
1610
|
+
else
|
|
1611
|
+
newBuffer.set(target.slice(start, end));
|
|
1612
|
+
position$1 -= start;
|
|
1613
|
+
start = 0;
|
|
1614
|
+
safeEnd = newBuffer.length - 10;
|
|
1615
|
+
return target = newBuffer
|
|
1616
|
+
};
|
|
1617
|
+
const newRecord = (transition, keys, newTransitions) => {
|
|
1618
|
+
let recordId = structures.nextId;
|
|
1619
|
+
if (!recordId)
|
|
1620
|
+
recordId = 0x40;
|
|
1621
|
+
if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
|
|
1622
|
+
recordId = structures.nextOwnId;
|
|
1623
|
+
if (!(recordId < maxStructureId))
|
|
1624
|
+
recordId = sharedLimitId;
|
|
1625
|
+
structures.nextOwnId = recordId + 1;
|
|
1626
|
+
} else {
|
|
1627
|
+
if (recordId >= maxStructureId)// cycle back around
|
|
1628
|
+
recordId = sharedLimitId;
|
|
1629
|
+
structures.nextId = recordId + 1;
|
|
1630
|
+
}
|
|
1631
|
+
let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1;
|
|
1632
|
+
transition[RECORD_SYMBOL] = recordId;
|
|
1633
|
+
transition.__keys__ = keys;
|
|
1634
|
+
structures[recordId - 0x40] = keys;
|
|
1635
|
+
|
|
1636
|
+
if (recordId < sharedLimitId) {
|
|
1637
|
+
keys.isShared = true;
|
|
1638
|
+
structures.sharedLength = recordId - 0x3f;
|
|
1639
|
+
hasSharedUpdate = true;
|
|
1640
|
+
if (highByte >= 0) {
|
|
1641
|
+
target[position$1++] = (recordId & 0x1f) + 0x60;
|
|
1642
|
+
target[position$1++] = highByte;
|
|
1643
|
+
} else {
|
|
1644
|
+
target[position$1++] = recordId;
|
|
1645
|
+
}
|
|
1646
|
+
} else {
|
|
1647
|
+
if (highByte >= 0) {
|
|
1648
|
+
target[position$1++] = 0xd5; // fixext 2
|
|
1649
|
+
target[position$1++] = 0x72; // "r" record defintion extension type
|
|
1650
|
+
target[position$1++] = (recordId & 0x1f) + 0x60;
|
|
1651
|
+
target[position$1++] = highByte;
|
|
1652
|
+
} else {
|
|
1653
|
+
target[position$1++] = 0xd4; // fixext 1
|
|
1654
|
+
target[position$1++] = 0x72; // "r" record defintion extension type
|
|
1655
|
+
target[position$1++] = recordId;
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
if (newTransitions)
|
|
1659
|
+
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
1660
|
+
// record the removal of the id, we can maintain our shared structure
|
|
1661
|
+
if (recordIdsToRemove.length >= maxOwnStructures)
|
|
1662
|
+
recordIdsToRemove.shift()[RECORD_SYMBOL] = 0; // we are cycling back through, and have to remove old ones
|
|
1663
|
+
recordIdsToRemove.push(transition);
|
|
1664
|
+
pack(keys);
|
|
1665
|
+
}
|
|
1666
|
+
};
|
|
1667
|
+
const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
|
|
1668
|
+
let mainTarget = target;
|
|
1669
|
+
let mainPosition = position$1;
|
|
1670
|
+
let mainSafeEnd = safeEnd;
|
|
1671
|
+
let mainStart = start;
|
|
1672
|
+
target = keysTarget;
|
|
1673
|
+
position$1 = 0;
|
|
1674
|
+
start = 0;
|
|
1675
|
+
if (!target)
|
|
1676
|
+
keysTarget = target = new ByteArrayAllocate(8192);
|
|
1677
|
+
safeEnd = target.length - 10;
|
|
1678
|
+
newRecord(transition, keys, newTransitions);
|
|
1679
|
+
keysTarget = target;
|
|
1680
|
+
let keysPosition = position$1;
|
|
1681
|
+
target = mainTarget;
|
|
1682
|
+
position$1 = mainPosition;
|
|
1683
|
+
safeEnd = mainSafeEnd;
|
|
1684
|
+
start = mainStart;
|
|
1685
|
+
if (keysPosition > 1) {
|
|
1686
|
+
let newEnd = position$1 + keysPosition - 1;
|
|
1687
|
+
if (newEnd > safeEnd)
|
|
1688
|
+
makeRoom(newEnd);
|
|
1689
|
+
let insertionPosition = insertionOffset + start;
|
|
1690
|
+
target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position$1);
|
|
1691
|
+
target.set(keysTarget.slice(0, keysPosition), insertionPosition);
|
|
1692
|
+
position$1 = newEnd;
|
|
1693
|
+
} else {
|
|
1694
|
+
target[insertionOffset + start] = keysTarget[0];
|
|
1695
|
+
}
|
|
1696
|
+
};
|
|
1697
|
+
const writeStruct = (object, safePrototype) => {
|
|
1698
|
+
let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition) => {
|
|
1699
|
+
position$1 = newPosition;
|
|
1700
|
+
if (start > 0) {
|
|
1701
|
+
pack(value);
|
|
1702
|
+
if (start == 0)
|
|
1703
|
+
return { position: position$1, targetView }; // indicate the buffer was re-allocated
|
|
1704
|
+
} else
|
|
1705
|
+
pack(value);
|
|
1706
|
+
return position$1;
|
|
1707
|
+
});
|
|
1708
|
+
if (newPosition === 0) // bail and go to a msgpack object
|
|
1709
|
+
return writeObject(object, true);
|
|
1710
|
+
position$1 = newPosition;
|
|
1711
|
+
};
|
|
1712
|
+
}
|
|
1713
|
+
useBuffer(buffer) {
|
|
1714
|
+
// this means we are finished using our own buffer and we can write over it safely
|
|
1715
|
+
target = buffer;
|
|
1716
|
+
targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
|
|
1717
|
+
position$1 = 0;
|
|
1718
|
+
}
|
|
1719
|
+
clearSharedData() {
|
|
1720
|
+
if (this.structures)
|
|
1721
|
+
this.structures = [];
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, C1Type ];
|
|
1726
|
+
extensions = [{
|
|
1727
|
+
pack(date, allocateForWrite, pack) {
|
|
1728
|
+
let seconds = date.getTime() / 1000;
|
|
1729
|
+
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
|
|
1730
|
+
// Timestamp 32
|
|
1731
|
+
let { target, targetView, position} = allocateForWrite(6);
|
|
1732
|
+
target[position++] = 0xd6;
|
|
1733
|
+
target[position++] = 0xff;
|
|
1734
|
+
targetView.setUint32(position, seconds);
|
|
1735
|
+
} else if (seconds > 0 && seconds < 0x100000000) {
|
|
1736
|
+
// Timestamp 64
|
|
1737
|
+
let { target, targetView, position} = allocateForWrite(10);
|
|
1738
|
+
target[position++] = 0xd7;
|
|
1739
|
+
target[position++] = 0xff;
|
|
1740
|
+
targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0));
|
|
1741
|
+
targetView.setUint32(position + 4, seconds);
|
|
1742
|
+
} else if (isNaN(seconds)) {
|
|
1743
|
+
if (this.onInvalidDate) {
|
|
1744
|
+
allocateForWrite(0);
|
|
1745
|
+
return pack(this.onInvalidDate())
|
|
1746
|
+
}
|
|
1747
|
+
// Intentionally invalid timestamp
|
|
1748
|
+
let { target, targetView, position} = allocateForWrite(3);
|
|
1749
|
+
target[position++] = 0xd4;
|
|
1750
|
+
target[position++] = 0xff;
|
|
1751
|
+
target[position++] = 0xff;
|
|
1752
|
+
} else {
|
|
1753
|
+
// Timestamp 96
|
|
1754
|
+
let { target, targetView, position} = allocateForWrite(15);
|
|
1755
|
+
target[position++] = 0xc7;
|
|
1756
|
+
target[position++] = 12;
|
|
1757
|
+
target[position++] = 0xff;
|
|
1758
|
+
targetView.setUint32(position, date.getMilliseconds() * 1000000);
|
|
1759
|
+
targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)));
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
}, {
|
|
1763
|
+
pack(set, allocateForWrite, pack) {
|
|
1764
|
+
let array = Array.from(set);
|
|
1765
|
+
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
1766
|
+
if (this.moreTypes) {
|
|
1767
|
+
target[position++] = 0xd4;
|
|
1768
|
+
target[position++] = 0x73; // 's' for Set
|
|
1769
|
+
target[position++] = 0;
|
|
1770
|
+
}
|
|
1771
|
+
pack(array);
|
|
1772
|
+
}
|
|
1773
|
+
}, {
|
|
1774
|
+
pack(error, allocateForWrite, pack) {
|
|
1775
|
+
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
1776
|
+
if (this.moreTypes) {
|
|
1777
|
+
target[position++] = 0xd4;
|
|
1778
|
+
target[position++] = 0x65; // 'e' for error
|
|
1779
|
+
target[position++] = 0;
|
|
1780
|
+
}
|
|
1781
|
+
pack([ error.name, error.message ]);
|
|
1782
|
+
}
|
|
1783
|
+
}, {
|
|
1784
|
+
pack(regex, allocateForWrite, pack) {
|
|
1785
|
+
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
1786
|
+
if (this.moreTypes) {
|
|
1787
|
+
target[position++] = 0xd4;
|
|
1788
|
+
target[position++] = 0x78; // 'x' for regeXp
|
|
1789
|
+
target[position++] = 0;
|
|
1790
|
+
}
|
|
1791
|
+
pack([ regex.source, regex.flags ]);
|
|
1792
|
+
}
|
|
1793
|
+
}, {
|
|
1794
|
+
pack(arrayBuffer, allocateForWrite) {
|
|
1795
|
+
if (this.moreTypes)
|
|
1796
|
+
writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
|
|
1797
|
+
else
|
|
1798
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
1799
|
+
}
|
|
1800
|
+
}, {
|
|
1801
|
+
pack(typedArray, allocateForWrite) {
|
|
1802
|
+
let constructor = typedArray.constructor;
|
|
1803
|
+
if (constructor !== ByteArray && this.moreTypes)
|
|
1804
|
+
writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
|
|
1805
|
+
else
|
|
1806
|
+
writeBuffer(typedArray, allocateForWrite);
|
|
1807
|
+
}
|
|
1808
|
+
}, {
|
|
1809
|
+
pack(c1, allocateForWrite) { // specific 0xC1 object
|
|
1810
|
+
let { target, position} = allocateForWrite(1);
|
|
1811
|
+
target[position] = 0xc1;
|
|
1812
|
+
}
|
|
1813
|
+
}];
|
|
1814
|
+
|
|
1815
|
+
function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
|
|
1816
|
+
let length = typedArray.byteLength;
|
|
1817
|
+
if (length + 1 < 0x100) {
|
|
1818
|
+
var { target, position } = allocateForWrite(4 + length);
|
|
1819
|
+
target[position++] = 0xc7;
|
|
1820
|
+
target[position++] = length + 1;
|
|
1821
|
+
} else if (length + 1 < 0x10000) {
|
|
1822
|
+
var { target, position } = allocateForWrite(5 + length);
|
|
1823
|
+
target[position++] = 0xc8;
|
|
1824
|
+
target[position++] = (length + 1) >> 8;
|
|
1825
|
+
target[position++] = (length + 1) & 0xff;
|
|
1826
|
+
} else {
|
|
1827
|
+
var { target, position, targetView } = allocateForWrite(7 + length);
|
|
1828
|
+
target[position++] = 0xc9;
|
|
1829
|
+
targetView.setUint32(position, length + 1); // plus one for the type byte
|
|
1830
|
+
position += 4;
|
|
1831
|
+
}
|
|
1832
|
+
target[position++] = 0x74; // "t" for typed array
|
|
1833
|
+
target[position++] = type;
|
|
1834
|
+
target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position);
|
|
1835
|
+
}
|
|
1836
|
+
function writeBuffer(buffer, allocateForWrite) {
|
|
1837
|
+
let length = buffer.byteLength;
|
|
1838
|
+
var target, position;
|
|
1839
|
+
if (length < 0x100) {
|
|
1840
|
+
var { target, position } = allocateForWrite(length + 2);
|
|
1841
|
+
target[position++] = 0xc4;
|
|
1842
|
+
target[position++] = length;
|
|
1843
|
+
} else if (length < 0x10000) {
|
|
1844
|
+
var { target, position } = allocateForWrite(length + 3);
|
|
1845
|
+
target[position++] = 0xc5;
|
|
1846
|
+
target[position++] = length >> 8;
|
|
1847
|
+
target[position++] = length & 0xff;
|
|
1848
|
+
} else {
|
|
1849
|
+
var { target, position, targetView } = allocateForWrite(length + 5);
|
|
1850
|
+
target[position++] = 0xc6;
|
|
1851
|
+
targetView.setUint32(position, length);
|
|
1852
|
+
position += 4;
|
|
1853
|
+
}
|
|
1854
|
+
target.set(buffer, position);
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
function writeExtensionData(result, target, position, type) {
|
|
1858
|
+
let length = result.length;
|
|
1859
|
+
switch (length) {
|
|
1860
|
+
case 1:
|
|
1861
|
+
target[position++] = 0xd4;
|
|
1862
|
+
break
|
|
1863
|
+
case 2:
|
|
1864
|
+
target[position++] = 0xd5;
|
|
1865
|
+
break
|
|
1866
|
+
case 4:
|
|
1867
|
+
target[position++] = 0xd6;
|
|
1868
|
+
break
|
|
1869
|
+
case 8:
|
|
1870
|
+
target[position++] = 0xd7;
|
|
1871
|
+
break
|
|
1872
|
+
case 16:
|
|
1873
|
+
target[position++] = 0xd8;
|
|
1874
|
+
break
|
|
1875
|
+
default:
|
|
1876
|
+
if (length < 0x100) {
|
|
1877
|
+
target[position++] = 0xc7;
|
|
1878
|
+
target[position++] = length;
|
|
1879
|
+
} else if (length < 0x10000) {
|
|
1880
|
+
target[position++] = 0xc8;
|
|
1881
|
+
target[position++] = length >> 8;
|
|
1882
|
+
target[position++] = length & 0xff;
|
|
1883
|
+
} else {
|
|
1884
|
+
target[position++] = 0xc9;
|
|
1885
|
+
target[position++] = length >> 24;
|
|
1886
|
+
target[position++] = (length >> 16) & 0xff;
|
|
1887
|
+
target[position++] = (length >> 8) & 0xff;
|
|
1888
|
+
target[position++] = length & 0xff;
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
target[position++] = type;
|
|
1892
|
+
target.set(result, position);
|
|
1893
|
+
position += length;
|
|
1894
|
+
return position
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
function insertIds(serialized, idsToInsert) {
|
|
1898
|
+
// insert the ids that need to be referenced for structured clones
|
|
1899
|
+
let nextId;
|
|
1900
|
+
let distanceToMove = idsToInsert.length * 6;
|
|
1901
|
+
let lastEnd = serialized.length - distanceToMove;
|
|
1902
|
+
idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
|
|
1903
|
+
while (nextId = idsToInsert.pop()) {
|
|
1904
|
+
let offset = nextId.offset;
|
|
1905
|
+
let id = nextId.id;
|
|
1906
|
+
serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
|
|
1907
|
+
distanceToMove -= 6;
|
|
1908
|
+
let position = offset + distanceToMove;
|
|
1909
|
+
serialized[position++] = 0xd6;
|
|
1910
|
+
serialized[position++] = 0x69; // 'i'
|
|
1911
|
+
serialized[position++] = id >> 24;
|
|
1912
|
+
serialized[position++] = (id >> 16) & 0xff;
|
|
1913
|
+
serialized[position++] = (id >> 8) & 0xff;
|
|
1914
|
+
serialized[position++] = id & 0xff;
|
|
1915
|
+
lastEnd = offset;
|
|
1916
|
+
}
|
|
1917
|
+
return serialized
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
function writeBundles(start, pack) {
|
|
1921
|
+
if (bundledStrings$1.length > 0) {
|
|
1922
|
+
targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start);
|
|
1923
|
+
let writeStrings = bundledStrings$1;
|
|
1924
|
+
bundledStrings$1 = null;
|
|
1925
|
+
pack(writeStrings[0]);
|
|
1926
|
+
pack(writeStrings[1]);
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
function setWriteStructSlots(func) {
|
|
1930
|
+
writeStructSlots = func;
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
let defaultPackr = new Packr({ useRecords: false });
|
|
1934
|
+
const REUSE_BUFFER_MODE = 512;
|
|
1935
|
+
const RESET_BUFFER_MODE = 1024;
|
|
1936
|
+
|
|
1937
|
+
// first four bits
|
|
1938
|
+
const hasNonLatin$1 = /[\u0080-\uFFFF]/;
|
|
1939
|
+
const float32Headers = [false, true, true, false, false, true, true, false];
|
|
1940
|
+
setWriteStructSlots(writeStruct);
|
|
1941
|
+
function writeStruct(object, target, position, structures, makeRoom, pack) {
|
|
1942
|
+
let transition = structures.transitions || false;
|
|
1943
|
+
let start = position;
|
|
1944
|
+
position += 4;
|
|
1945
|
+
let queuedReferences = [];
|
|
1946
|
+
let uint32 = target.uint32 || (target.uint32 = new Uint32Array(target.buffer));
|
|
1947
|
+
let targetView = target.dataView;
|
|
1948
|
+
let encoded;
|
|
1949
|
+
let stringData = '';
|
|
1950
|
+
let safeEnd = target.length - 10;
|
|
1951
|
+
for (let key in object) {
|
|
1952
|
+
let nextTransition = transition[key];
|
|
1953
|
+
if (!nextTransition) {
|
|
1954
|
+
return 0; // bail
|
|
1955
|
+
//nextTransition = transition[key] = Object.create(null)
|
|
1956
|
+
//newTransitions++
|
|
1957
|
+
}
|
|
1958
|
+
if (position > safeEnd) {
|
|
1959
|
+
let newPosition = position - start;
|
|
1960
|
+
target = makeRoom(position);
|
|
1961
|
+
position = newPosition;
|
|
1962
|
+
start = 0;
|
|
1963
|
+
safeEnd = target.length - 10;
|
|
1964
|
+
}
|
|
1965
|
+
transition = nextTransition;
|
|
1966
|
+
let value = object[key];
|
|
1967
|
+
switch (typeof value) {
|
|
1968
|
+
case 'number':
|
|
1969
|
+
if (value >>> 0 === value && value < 0x20000000) {
|
|
1970
|
+
encoded = value;
|
|
1971
|
+
break;
|
|
1972
|
+
} else if (value < 0x100000000 && value >= -0x80000000) {
|
|
1973
|
+
targetView.setFloat32(position, value, true);
|
|
1974
|
+
if (float32Headers[target[position + 3] >>> 5]) {
|
|
1975
|
+
let xShifted;
|
|
1976
|
+
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1977
|
+
if (((xShifted = value * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
|
|
1978
|
+
position += 4;
|
|
1979
|
+
continue;
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
// fall back to msgpack encoding
|
|
1984
|
+
queuedReferences.push(value, position - start);
|
|
1985
|
+
position += 4;
|
|
1986
|
+
continue;
|
|
1987
|
+
case 'string':
|
|
1988
|
+
if (hasNonLatin$1.test(value)) {
|
|
1989
|
+
queuedReferences.push(value, position - start);
|
|
1990
|
+
position += 4;
|
|
1991
|
+
continue;
|
|
1992
|
+
}
|
|
1993
|
+
if (value.length < 4) { // we can inline really small strings
|
|
1994
|
+
encoded = 0xf8000000 + (value.length << 24) + (value.charCodeAt(0) << 16) + (value.charCodeAt(1) << 8) + (value.charCodeAt(2) || 0);
|
|
1995
|
+
// TODO: determining remaining and make max value be a ratio of that (probably 1/256th)
|
|
1996
|
+
} else if (value.length < 256 && stringData.length < 61440) {
|
|
1997
|
+
// bundle these strings
|
|
1998
|
+
encoded = 0x60000000 | (value.length << 16) | stringData.length;
|
|
1999
|
+
stringData += value;
|
|
2000
|
+
} else { // else queue it
|
|
2001
|
+
queuedReferences.push(value, position - start);
|
|
2002
|
+
position += 4;
|
|
2003
|
+
continue;
|
|
2004
|
+
}
|
|
2005
|
+
break;
|
|
2006
|
+
case 'object':
|
|
2007
|
+
if (value) {
|
|
2008
|
+
queuedReferences.push(value, position - start);
|
|
2009
|
+
position += 4;
|
|
2010
|
+
continue;
|
|
2011
|
+
} else { // null
|
|
2012
|
+
encoded = 0xe0000000;
|
|
2013
|
+
}
|
|
2014
|
+
break;
|
|
2015
|
+
case 'boolean':
|
|
2016
|
+
encoded = value ? 0xe3000000 : 0xe2000000;
|
|
2017
|
+
break;
|
|
2018
|
+
case 'undefined':
|
|
2019
|
+
encoded = 0xe1000000;
|
|
2020
|
+
break;
|
|
2021
|
+
}
|
|
2022
|
+
targetView.setUint32(position, encoded, true);
|
|
2023
|
+
position += 4;
|
|
2024
|
+
}
|
|
2025
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
2026
|
+
if (!(recordId < 1024)) {
|
|
2027
|
+
// for now just punt and go back to writeObject
|
|
2028
|
+
return 0;
|
|
2029
|
+
// newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions, true)
|
|
2030
|
+
}
|
|
2031
|
+
let stringLength = stringData.length;
|
|
2032
|
+
if (stringData) {
|
|
2033
|
+
if (position + stringLength > safeEnd) {
|
|
2034
|
+
target = makeRoom(position + stringLength);
|
|
2035
|
+
}
|
|
2036
|
+
position += target.latin1Write(stringData, position, 0xffffffff);
|
|
2037
|
+
}
|
|
2038
|
+
target[start] = recordId >> 8;
|
|
2039
|
+
target[start + 1] = recordId & 0xff;
|
|
2040
|
+
target[start + 2] = stringLength >> 8;
|
|
2041
|
+
target[start + 3] = stringLength & 0xff;
|
|
2042
|
+
let queued32BitReferences;
|
|
2043
|
+
for (let i = 0, l = queuedReferences.length; i < l;) {
|
|
2044
|
+
let value = queuedReferences[i++];
|
|
2045
|
+
let slotOffset = queuedReferences[i++] + start;
|
|
2046
|
+
let offset = position - slotOffset;
|
|
2047
|
+
if (offset < 0x1f000000) {
|
|
2048
|
+
targetView.setUint32(slotOffset, 0x80000000 | (offset), true);
|
|
2049
|
+
} else {
|
|
2050
|
+
if (!queued32BitReferences)
|
|
2051
|
+
queued32BitReferences = [];
|
|
2052
|
+
queued32BitReferences.push({slotOffset, offset: position - start});
|
|
2053
|
+
}
|
|
2054
|
+
let newPosition = pack(value, position);
|
|
2055
|
+
if (typeof newPosition === 'object') {
|
|
2056
|
+
// re-allocated
|
|
2057
|
+
position = newPosition.position;
|
|
2058
|
+
targetView = newPosition.targetView;
|
|
2059
|
+
start = 0;
|
|
2060
|
+
} else
|
|
2061
|
+
position = newPosition;
|
|
2062
|
+
}
|
|
2063
|
+
if (queued32BitReferences) {
|
|
2064
|
+
// TODO: makeRoom
|
|
2065
|
+
for (let i = 0, l = queued32BitReferences.length; i < l; i++) {
|
|
2066
|
+
let ref = queued32BitReferences[i];
|
|
2067
|
+
targetView.setUint32(ref.slotOffset, 0xa0000000 - ((l - i) << 2), true);
|
|
2068
|
+
targetView.setUint32(position, ref.offset, true);
|
|
2069
|
+
position += 4;
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
|
|
2073
|
+
return position;
|
|
2074
|
+
}
|
|
2075
|
+
var sourceSymbol = Symbol('source');
|
|
2076
|
+
function readStruct$1(src, position, srcEnd, structure, unpackr) {
|
|
2077
|
+
var stringLength = (src[position++] << 8) | src[position++];
|
|
2078
|
+
var construct = structure.construct;
|
|
2079
|
+
var srcString;
|
|
2080
|
+
if (!construct) {
|
|
2081
|
+
construct = structure.construct = function() {
|
|
2082
|
+
};
|
|
2083
|
+
var prototype = construct.prototype;
|
|
2084
|
+
Object.defineProperty(prototype, 'toJSON', {
|
|
2085
|
+
get() {
|
|
2086
|
+
// return an enumerable object with own properties to JSON stringify
|
|
2087
|
+
let resolved = {};
|
|
2088
|
+
for (let i = 0, l = structure.length; i < l; i++) {
|
|
2089
|
+
let key = structure[i];
|
|
2090
|
+
resolved[key] = this[key];
|
|
2091
|
+
}
|
|
2092
|
+
return resolved;
|
|
2093
|
+
},
|
|
2094
|
+
// not enumerable or anything
|
|
2095
|
+
});
|
|
2096
|
+
for (let i = 0, l = structure.length; i < l; i++) {
|
|
2097
|
+
let key = structure[i];
|
|
2098
|
+
Object.defineProperty(prototype, key, {
|
|
2099
|
+
get() {
|
|
2100
|
+
let source = this[sourceSymbol];
|
|
2101
|
+
let src = source.src;
|
|
2102
|
+
//let uint32 = src.uint32 || (src.uint32 = new Uint32Array(src.buffer, src.byteOffset, src.byteLength));
|
|
2103
|
+
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2104
|
+
let position = source.position + (i << 2);
|
|
2105
|
+
let value = dataView.getUint32(position, true);
|
|
2106
|
+
let start;
|
|
2107
|
+
switch (value >>> 29) {
|
|
2108
|
+
case 0:
|
|
2109
|
+
return value;
|
|
2110
|
+
case 3:
|
|
2111
|
+
if (value & 0x10000000) {
|
|
2112
|
+
start = (value & 0xffff) + position;
|
|
2113
|
+
return src.toString('utf8', start, start + ((value >> 16) & 0x7ff));
|
|
2114
|
+
} else {
|
|
2115
|
+
if (!srcString) {
|
|
2116
|
+
start = source.position + (l << 2);
|
|
2117
|
+
srcString = src.toString('latin1', start, start + stringLength);
|
|
2118
|
+
}
|
|
2119
|
+
start = value & 0xffff;
|
|
2120
|
+
return srcString.slice(start, start + ((value >> 16) & 0x7ff));
|
|
2121
|
+
}
|
|
2122
|
+
case 4:
|
|
2123
|
+
start = (0x1fffffff & value) + position;
|
|
2124
|
+
let end = srcEnd;
|
|
2125
|
+
for (let next = i + 1; next < l; next++) {
|
|
2126
|
+
position = source.position + (next << 2);
|
|
2127
|
+
let nextValue = dataView.getUint32(position, true); if ((nextValue & 0xe0000000) == -0x80000000) {
|
|
2128
|
+
end = (0x1fffffff & nextValue) + position;
|
|
2129
|
+
break;
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
return unpackr.unpack(src.slice(start, end));
|
|
2133
|
+
case 1: case 2: case 5: case 6:
|
|
2134
|
+
let fValue = dataView.getFloat32(position, true);
|
|
2135
|
+
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
2136
|
+
let multiplier = mult10[((src[position + 3] & 0x7f) << 1) | (src[position + 2] >> 7)];
|
|
2137
|
+
return ((multiplier * fValue + (fValue > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
|
|
2138
|
+
case 7:
|
|
2139
|
+
switch((value >> 24) & 0x1f) {
|
|
2140
|
+
case 0: return null;
|
|
2141
|
+
case 1: return undefined;
|
|
2142
|
+
case 2: return false;
|
|
2143
|
+
case 3: return true;
|
|
2144
|
+
case 8: return dataView.getFloat64(position + (value & 0x3ffffff), true);
|
|
2145
|
+
case 0x18: return '';
|
|
2146
|
+
case 0x19: return String.fromCharCode((value >> 16) & 0xff);
|
|
2147
|
+
case 0x20: return String.fromCharCode((value >> 16) & 0xff, (value >> 8) & 0xff);
|
|
2148
|
+
case 0x21: return String.fromCharCode((value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff);
|
|
2149
|
+
default: throw new Error('Unknown constant');
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
},
|
|
2153
|
+
enumerable: true,
|
|
2154
|
+
});
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
var instance = new construct();
|
|
2158
|
+
instance[sourceSymbol] = {
|
|
2159
|
+
src,
|
|
2160
|
+
uint32: src.uint32,
|
|
2161
|
+
position,
|
|
2162
|
+
};
|
|
2163
|
+
return instance;
|
|
2164
|
+
}
|
|
2165
|
+
setReadStruct(readStruct$1);
|
|
2166
|
+
|
|
2167
|
+
const allSampleData = [];
|
|
2168
|
+
for (let i = 1; i < 6; i++) {
|
|
2169
|
+
allSampleData.push(JSON.parse(fs.readFileSync(new URL(`./example${i > 1 ? i : ''}.json`, (document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href)))));
|
|
2170
|
+
}
|
|
2171
|
+
const sampleData = allSampleData[3];
|
|
7
2172
|
function tryRequire(module) {
|
|
8
2173
|
try {
|
|
9
2174
|
return require(module)
|
|
@@ -14,8 +2179,8 @@
|
|
|
14
2179
|
//if (typeof chai === 'undefined') { chai = require('chai') }
|
|
15
2180
|
var assert = chai.assert;
|
|
16
2181
|
//if (typeof msgpackr === 'undefined') { msgpackr = require('..') }
|
|
17
|
-
var Packr = msgpackr.Packr;
|
|
18
|
-
var Unpackr = msgpackr.Unpackr;
|
|
2182
|
+
var Packr$1 = msgpackr.Packr;
|
|
2183
|
+
var Unpackr$1 = msgpackr.Unpackr;
|
|
19
2184
|
var unpack = msgpackr.unpack;
|
|
20
2185
|
var unpackMultiple = msgpackr.unpackMultiple;
|
|
21
2186
|
var roundFloat32 = msgpackr.roundFloat32;
|
|
@@ -56,7 +2221,7 @@
|
|
|
56
2221
|
]
|
|
57
2222
|
};
|
|
58
2223
|
let structures = [];
|
|
59
|
-
let packr = new Packr({ structures });
|
|
2224
|
+
let packr = new Packr$1({ structures });
|
|
60
2225
|
var serialized = packr.pack(data);
|
|
61
2226
|
serialized = packr.pack(data);
|
|
62
2227
|
serialized = packr.pack(data);
|
|
@@ -69,7 +2234,7 @@
|
|
|
69
2234
|
let data2 = { a: 1, b: 2, d: 4 };
|
|
70
2235
|
let data3 = { a: 1, b: 2, e: 5 };
|
|
71
2236
|
let structures = [];
|
|
72
|
-
let packr = new Packr({ structures });
|
|
2237
|
+
let packr = new Packr$1({ structures });
|
|
73
2238
|
var serialized = packr.pack(data1);
|
|
74
2239
|
var deserialized = packr.unpack(serialized);
|
|
75
2240
|
assert.deepEqual(deserialized, data1);
|
|
@@ -97,7 +2262,7 @@
|
|
|
97
2262
|
]
|
|
98
2263
|
];
|
|
99
2264
|
let structures = [];
|
|
100
|
-
let packr = new Packr({ structures });
|
|
2265
|
+
let packr = new Packr$1({ structures });
|
|
101
2266
|
var serialized = packr.pack(data);
|
|
102
2267
|
var deserialized = packr.unpack(serialized);
|
|
103
2268
|
assert.deepEqual(deserialized, data);
|
|
@@ -109,42 +2274,47 @@
|
|
|
109
2274
|
var deserialized = unpack(serialized);
|
|
110
2275
|
assert.equal(deserialized, data);
|
|
111
2276
|
});
|
|
2277
|
+
for (let sampleData of allSampleData) {
|
|
2278
|
+
let snippet = JSON.stringify(sampleData).slice(0, 20) + '...';
|
|
2279
|
+
test('pack/unpack sample data ' + snippet, function(){
|
|
2280
|
+
var data = sampleData;
|
|
2281
|
+
var serialized = pack(data);
|
|
2282
|
+
var deserialized = unpack(serialized);
|
|
2283
|
+
assert.deepEqual(deserialized, data);
|
|
2284
|
+
var serialized = pack(data);
|
|
2285
|
+
var deserialized = unpack(serialized);
|
|
2286
|
+
assert.deepEqual(deserialized, data);
|
|
2287
|
+
});
|
|
2288
|
+
test('pack/unpack sample data with random access structures ' + snippet, function() {
|
|
2289
|
+
var data = sampleData;
|
|
2290
|
+
let structures = [];
|
|
2291
|
+
let packr = new Packr$1({ structures, useRecords: true, randomAccessStructure: true, freezeData: true });
|
|
2292
|
+
for (let i = 0; i < 20; i++) {
|
|
2293
|
+
var serialized = packr.pack(data);
|
|
2294
|
+
var deserialized = packr.unpack(serialized);
|
|
2295
|
+
assert.deepEqual(deserialized, data);
|
|
2296
|
+
}
|
|
2297
|
+
});
|
|
2298
|
+
test('pack/unpack sample data with bundled strings ' + snippet, function(){
|
|
2299
|
+
var data = sampleData;
|
|
2300
|
+
let packr = new Packr$1({ /*structures,*/ useRecords: false, bundleStrings: true });
|
|
2301
|
+
var serialized = packr.pack(data);
|
|
2302
|
+
var deserialized = packr.unpack(serialized);
|
|
2303
|
+
assert.deepEqual(deserialized, data);
|
|
2304
|
+
});
|
|
2305
|
+
}
|
|
112
2306
|
|
|
113
|
-
test('pack/unpack sample data', function(){
|
|
114
|
-
var data = sampleData;
|
|
115
|
-
var serialized = pack(data);
|
|
116
|
-
var deserialized = unpack(serialized);
|
|
117
|
-
assert.deepEqual(deserialized, data);
|
|
118
|
-
var serialized = pack(data);
|
|
119
|
-
var deserialized = unpack(serialized);
|
|
120
|
-
assert.deepEqual(deserialized, data);
|
|
121
|
-
});
|
|
122
|
-
test('pack/unpack sample data with records', function(){
|
|
123
|
-
var data = sampleData;
|
|
124
|
-
let structures = [];
|
|
125
|
-
let packr = new Packr({ structures, useRecords: true });
|
|
126
|
-
var serialized = packr.pack(data);
|
|
127
|
-
var deserialized = packr.unpack(serialized);
|
|
128
|
-
assert.deepEqual(deserialized, data);
|
|
129
|
-
});
|
|
130
|
-
test('pack/unpack sample data with bundled strings', function(){
|
|
131
|
-
var data = sampleData;
|
|
132
|
-
let packr = new Packr({ /*structures,*/ useRecords: false, bundleStrings: true });
|
|
133
|
-
var serialized = packr.pack(data);
|
|
134
|
-
var deserialized = packr.unpack(serialized);
|
|
135
|
-
assert.deepEqual(deserialized, data);
|
|
136
|
-
});
|
|
137
2307
|
test('pack/unpack empty data with bundled strings', function(){
|
|
138
2308
|
var data = {};
|
|
139
|
-
let packr = new Packr({ bundleStrings: true });
|
|
2309
|
+
let packr = new Packr$1({ bundleStrings: true });
|
|
140
2310
|
var serialized = packr.pack(data);
|
|
141
2311
|
var deserialized = packr.unpack(serialized);
|
|
142
2312
|
assert.deepEqual(deserialized, data);
|
|
143
2313
|
});
|
|
144
2314
|
test('pack/unpack sequential data', function(){
|
|
145
2315
|
var data = { foo:1, bar: 2 };
|
|
146
|
-
let packr = new Packr({ sequential: true });
|
|
147
|
-
let unpackr = new Unpackr({ sequential: true });
|
|
2316
|
+
let packr = new Packr$1({ sequential: true });
|
|
2317
|
+
let unpackr = new Unpackr$1({ sequential: true });
|
|
148
2318
|
var serialized = packr.pack(data);
|
|
149
2319
|
var deserialized = unpackr.unpack(serialized);
|
|
150
2320
|
assert.deepEqual(deserialized, data);
|
|
@@ -205,7 +2375,7 @@
|
|
|
205
2375
|
prop2: 'more string',
|
|
206
2376
|
num: 3,
|
|
207
2377
|
};
|
|
208
|
-
let packr = new Packr();
|
|
2378
|
+
let packr = new Packr$1();
|
|
209
2379
|
addExtension({
|
|
210
2380
|
Class: Extended,
|
|
211
2381
|
type: 11,
|
|
@@ -259,7 +2429,7 @@
|
|
|
259
2429
|
prop2: 'more string',
|
|
260
2430
|
num: 3,
|
|
261
2431
|
};
|
|
262
|
-
let packr = new Packr();
|
|
2432
|
+
let packr = new Packr$1();
|
|
263
2433
|
addExtension({
|
|
264
2434
|
Class: Extended,
|
|
265
2435
|
type: 12,
|
|
@@ -304,7 +2474,7 @@
|
|
|
304
2474
|
object.children[1] = object;
|
|
305
2475
|
object.children[2] = object.children[0];
|
|
306
2476
|
object.childrenAgain = object.children;
|
|
307
|
-
let packr = new Packr({
|
|
2477
|
+
let packr = new Packr$1({
|
|
308
2478
|
moreTypes: true,
|
|
309
2479
|
structuredClone: true,
|
|
310
2480
|
});
|
|
@@ -329,7 +2499,7 @@
|
|
|
329
2499
|
float32Array: fa,
|
|
330
2500
|
uint16Array: new Uint16Array([3,4])
|
|
331
2501
|
};
|
|
332
|
-
let packr = new Packr({
|
|
2502
|
+
let packr = new Packr$1({
|
|
333
2503
|
moreTypes: true,
|
|
334
2504
|
structuredClone: true,
|
|
335
2505
|
});
|
|
@@ -354,6 +2524,18 @@
|
|
|
354
2524
|
assert.deepEqual(deserialized, data);
|
|
355
2525
|
});
|
|
356
2526
|
|
|
2527
|
+
test('separate instances', function() {
|
|
2528
|
+
const packr = new Packr$1({
|
|
2529
|
+
structures: [['m', 'e'], ['action', 'share']]
|
|
2530
|
+
});
|
|
2531
|
+
const packr2 = new Packr$1({
|
|
2532
|
+
structures: [['m', 'e'], ['action', 'share']]
|
|
2533
|
+
});
|
|
2534
|
+
let packed = packr.pack([{m: 1, e: 2}, {action: 3, share: 4}]);
|
|
2535
|
+
// also tried directly decoding this without the first Packr instance packed = new Uint8Array([0x92, 0x40, 0x01, 0x02, 0x41, 0x03, 0x04]);
|
|
2536
|
+
console.log(packr2.unpack(packed));
|
|
2537
|
+
});
|
|
2538
|
+
|
|
357
2539
|
test('many shared structures', function() {
|
|
358
2540
|
let data = [];
|
|
359
2541
|
for (let i = 0; i < 200; i++) {
|
|
@@ -361,7 +2543,7 @@
|
|
|
361
2543
|
}
|
|
362
2544
|
let structures = [];
|
|
363
2545
|
let savedStructures;
|
|
364
|
-
let packr = new Packr({
|
|
2546
|
+
let packr = new Packr$1({
|
|
365
2547
|
structures,
|
|
366
2548
|
saveStructures(structures) {
|
|
367
2549
|
savedStructures = structures;
|
|
@@ -372,7 +2554,7 @@
|
|
|
372
2554
|
var deserialized = packr.unpack(serializedWith32);
|
|
373
2555
|
assert.deepEqual(deserialized, data);
|
|
374
2556
|
structures = structures.slice(0, 32);
|
|
375
|
-
packr = new Packr({
|
|
2557
|
+
packr = new Packr$1({
|
|
376
2558
|
structures,
|
|
377
2559
|
maxSharedStructures: 100,
|
|
378
2560
|
saveStructures(structures) {
|
|
@@ -382,7 +2564,7 @@
|
|
|
382
2564
|
deserialized = packr.unpack(serializedWith32);
|
|
383
2565
|
assert.deepEqual(deserialized, data);
|
|
384
2566
|
structures = structures.slice(0, 32);
|
|
385
|
-
packr = new Packr({
|
|
2567
|
+
packr = new Packr$1({
|
|
386
2568
|
structures,
|
|
387
2569
|
maxSharedStructures: 100,
|
|
388
2570
|
saveStructures(structures) {
|
|
@@ -408,7 +2590,7 @@
|
|
|
408
2590
|
structures.push(['a' + i]);
|
|
409
2591
|
}
|
|
410
2592
|
const structures2 = [...structures];
|
|
411
|
-
const packr = new Packr({
|
|
2593
|
+
const packr = new Packr$1({
|
|
412
2594
|
getStructures() {
|
|
413
2595
|
return structures
|
|
414
2596
|
},
|
|
@@ -416,7 +2598,7 @@
|
|
|
416
2598
|
},
|
|
417
2599
|
maxSharedStructures: 100
|
|
418
2600
|
});
|
|
419
|
-
const packr2 = new Packr({
|
|
2601
|
+
const packr2 = new Packr$1({
|
|
420
2602
|
getStructures() {
|
|
421
2603
|
return structures2
|
|
422
2604
|
},
|
|
@@ -466,7 +2648,7 @@
|
|
|
466
2648
|
ancient: new Date(-3532219539133),
|
|
467
2649
|
invalidDate: new Date('invalid')
|
|
468
2650
|
};
|
|
469
|
-
let packr = new Packr();
|
|
2651
|
+
let packr = new Packr$1();
|
|
470
2652
|
var serialized = packr.pack(data);
|
|
471
2653
|
var deserialized = packr.unpack(serialized);
|
|
472
2654
|
assert.equal(deserialized.map.get(4), 'four');
|
|
@@ -488,7 +2670,7 @@
|
|
|
488
2670
|
date: new Date(1532219539011),
|
|
489
2671
|
invalidDate: new Date('invalid')
|
|
490
2672
|
};
|
|
491
|
-
let packr = new Packr({
|
|
2673
|
+
let packr = new Packr$1({
|
|
492
2674
|
mapsAsObjects: true,
|
|
493
2675
|
useTimestamp32: true,
|
|
494
2676
|
onInvalidDate: () => 'Custom invalid date'
|
|
@@ -544,7 +2726,7 @@
|
|
|
544
2726
|
c: 0.00000000000352501,
|
|
545
2727
|
d: 3252.77,
|
|
546
2728
|
};
|
|
547
|
-
let packr = new Packr({
|
|
2729
|
+
let packr = new Packr$1({
|
|
548
2730
|
useFloat32: DECIMAL_FIT
|
|
549
2731
|
});
|
|
550
2732
|
var serialized = packr.pack(data);
|
|
@@ -556,7 +2738,7 @@
|
|
|
556
2738
|
var data = {
|
|
557
2739
|
a: 325283295382932843n
|
|
558
2740
|
};
|
|
559
|
-
let packr = new Packr({
|
|
2741
|
+
let packr = new Packr$1({
|
|
560
2742
|
int64AsNumber: true
|
|
561
2743
|
});
|
|
562
2744
|
var serialized = packr.pack(data);
|
|
@@ -595,7 +2777,7 @@
|
|
|
595
2777
|
tooBig: 2n**66n
|
|
596
2778
|
};
|
|
597
2779
|
assert.throws(function(){ serialized = pack(tooBigInt); });
|
|
598
|
-
let packr = new Packr({
|
|
2780
|
+
let packr = new Packr$1({
|
|
599
2781
|
largeBigIntToFloat: true
|
|
600
2782
|
});
|
|
601
2783
|
serialized = packr.pack(tooBigInt);
|
|
@@ -678,7 +2860,7 @@
|
|
|
678
2860
|
let structures = [];
|
|
679
2861
|
var serialized = pack(data);
|
|
680
2862
|
console.log('MessagePack size', serialized.length);
|
|
681
|
-
let packr = new Packr({ structures, bundleStrings: false });
|
|
2863
|
+
let packr = new Packr$1({ structures, bundleStrings: false });
|
|
682
2864
|
var serialized = packr.pack(data);
|
|
683
2865
|
console.log('msgpackr w/ record ext size', serialized.length);
|
|
684
2866
|
for (var i = 0; i < ITERATIONS; i++) {
|
|
@@ -689,7 +2871,7 @@
|
|
|
689
2871
|
var data = sampleData;
|
|
690
2872
|
this.timeout(10000);
|
|
691
2873
|
let structures = [];
|
|
692
|
-
let packr = new Packr({ structures, bundleStrings: false });
|
|
2874
|
+
let packr = new Packr$1({ structures, bundleStrings: false });
|
|
693
2875
|
let buffer = typeof Buffer != 'undefined' ? Buffer.alloc(0x10000) : new Uint8Array(0x10000);
|
|
694
2876
|
|
|
695
2877
|
for (var i = 0; i < ITERATIONS; i++) {
|