msgpackr 1.11.12 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/dist/index-no-eval.cjs +242 -230
- package/dist/index-no-eval.cjs.map +1 -1
- package/dist/index-no-eval.min.js +1 -1
- package/dist/index-no-eval.min.js.map +1 -1
- package/dist/index.js +242 -230
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +264 -1048
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +278 -1121
- package/dist/test.js.map +1 -1
- package/dist/unpack-no-eval.cjs +176 -179
- package/dist/unpack-no-eval.cjs.map +1 -1
- package/index.js +5 -5
- package/iterators.js +28 -28
- package/node-index.js +12 -13
- package/pack.js +607 -606
- package/package.json +1 -1
- package/stream.js +31 -31
- package/unpack.js +626 -628
- package/struct.js +0 -815
package/dist/test.js
CHANGED
|
@@ -29,7 +29,6 @@
|
|
|
29
29
|
C1.name = 'MessagePack 0xC1';
|
|
30
30
|
var sequentialMode = false;
|
|
31
31
|
var inlineObjectReadThreshold = 2;
|
|
32
|
-
var readStruct$1, onLoadedStructures$1, onSaveState;
|
|
33
32
|
|
|
34
33
|
let Unpackr$1 = class Unpackr {
|
|
35
34
|
constructor(options) {
|
|
@@ -59,10 +58,10 @@
|
|
|
59
58
|
unpack(source, options) {
|
|
60
59
|
if (src) {
|
|
61
60
|
// re-entrant execution, save the state and restore it after we do this unpack
|
|
62
|
-
return saveState
|
|
61
|
+
return saveState(() => {
|
|
63
62
|
clearSource();
|
|
64
|
-
return this ? this.unpack(source, options) : Unpackr$1.prototype.unpack.call(defaultOptions, source, options)
|
|
65
|
-
})
|
|
63
|
+
return this ? this.unpack(source, options) : Unpackr$1.prototype.unpack.call(defaultOptions, source, options);
|
|
64
|
+
});
|
|
66
65
|
}
|
|
67
66
|
if (!source.buffer && source.constructor === ArrayBuffer)
|
|
68
67
|
source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
|
|
@@ -88,14 +87,14 @@
|
|
|
88
87
|
// if it doesn't have a buffer, maybe it is the wrong type of object
|
|
89
88
|
src = null;
|
|
90
89
|
if (source instanceof Uint8Array)
|
|
91
|
-
throw error
|
|
92
|
-
throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
|
|
90
|
+
throw error;
|
|
91
|
+
throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source));
|
|
93
92
|
}
|
|
94
93
|
if (this instanceof Unpackr$1) {
|
|
95
94
|
currentUnpackr = this;
|
|
96
95
|
if (this.structures) {
|
|
97
96
|
currentStructures = this.structures;
|
|
98
|
-
return checkedRead(options)
|
|
97
|
+
return checkedRead(options);
|
|
99
98
|
} else if (!currentStructures || currentStructures.length > 0) {
|
|
100
99
|
currentStructures = [];
|
|
101
100
|
}
|
|
@@ -104,7 +103,7 @@
|
|
|
104
103
|
if (!currentStructures || currentStructures.length > 0)
|
|
105
104
|
currentStructures = [];
|
|
106
105
|
}
|
|
107
|
-
return checkedRead(options)
|
|
106
|
+
return checkedRead(options);
|
|
108
107
|
}
|
|
109
108
|
unpackMultiple(source, forEach) {
|
|
110
109
|
let values, lastPosition = 0;
|
|
@@ -117,7 +116,7 @@
|
|
|
117
116
|
while(position$1 < size) {
|
|
118
117
|
lastPosition = position$1;
|
|
119
118
|
if (forEach(checkedRead(), lastPosition, position$1) === false) {
|
|
120
|
-
return
|
|
119
|
+
return;
|
|
121
120
|
}
|
|
122
121
|
}
|
|
123
122
|
}
|
|
@@ -127,20 +126,20 @@
|
|
|
127
126
|
lastPosition = position$1;
|
|
128
127
|
values.push(checkedRead());
|
|
129
128
|
}
|
|
130
|
-
return values
|
|
129
|
+
return values;
|
|
131
130
|
}
|
|
132
131
|
} catch(error) {
|
|
133
132
|
error.lastPosition = lastPosition;
|
|
134
133
|
error.values = values;
|
|
135
|
-
throw error
|
|
134
|
+
throw error;
|
|
136
135
|
} finally {
|
|
137
136
|
sequentialMode = false;
|
|
138
137
|
clearSource();
|
|
139
138
|
}
|
|
140
139
|
}
|
|
141
140
|
_mergeStructures(loadedStructures, existingStructures) {
|
|
142
|
-
if (
|
|
143
|
-
loadedStructures =
|
|
141
|
+
if (this._onLoadedStructures)
|
|
142
|
+
loadedStructures = this._onLoadedStructures(loadedStructures);
|
|
144
143
|
loadedStructures = loadedStructures || [];
|
|
145
144
|
if (Object.isFrozen(loadedStructures))
|
|
146
145
|
loadedStructures = loadedStructures.map(structure => structure.slice(0));
|
|
@@ -164,10 +163,10 @@
|
|
|
164
163
|
}
|
|
165
164
|
}
|
|
166
165
|
}
|
|
167
|
-
return this.structures = loadedStructures
|
|
166
|
+
return this.structures = loadedStructures;
|
|
168
167
|
}
|
|
169
168
|
decode(source, options) {
|
|
170
|
-
return this.unpack(source, options)
|
|
169
|
+
return this.unpack(source, options);
|
|
171
170
|
}
|
|
172
171
|
};
|
|
173
172
|
function checkedRead(options) {
|
|
@@ -178,8 +177,8 @@
|
|
|
178
177
|
currentStructures.length = sharedLength;
|
|
179
178
|
}
|
|
180
179
|
let result;
|
|
181
|
-
if (currentUnpackr.
|
|
182
|
-
result =
|
|
180
|
+
if (currentUnpackr._readStruct && src[position$1] < 0x40 && src[position$1] >= 0x20) {
|
|
181
|
+
result = currentUnpackr._readStruct(src, position$1, srcEnd);
|
|
183
182
|
src = null; // dispose of this so that recursive unpack calls don't save state
|
|
184
183
|
if (!(options && options.lazy) && result)
|
|
185
184
|
result = result.toJSON();
|
|
@@ -205,7 +204,7 @@
|
|
|
205
204
|
referenceMap = null;
|
|
206
205
|
} else if (position$1 > srcEnd) {
|
|
207
206
|
// over read
|
|
208
|
-
throw new Error('Unexpected end of MessagePack data')
|
|
207
|
+
throw new Error('Unexpected end of MessagePack data');
|
|
209
208
|
} else if (!sequentialMode) {
|
|
210
209
|
let jsonView;
|
|
211
210
|
try {
|
|
@@ -213,10 +212,10 @@
|
|
|
213
212
|
} catch(error) {
|
|
214
213
|
jsonView = '(JSON view not available ' + error + ')';
|
|
215
214
|
}
|
|
216
|
-
throw new Error('Data read, but end of buffer not reached ' + jsonView)
|
|
215
|
+
throw new Error('Data read, but end of buffer not reached ' + jsonView);
|
|
217
216
|
}
|
|
218
217
|
// else more to read, but we are reading sequentially, so don't clear source yet
|
|
219
|
-
return result
|
|
218
|
+
return result;
|
|
220
219
|
} catch(error) {
|
|
221
220
|
if (currentStructures && currentStructures.restoreStructures)
|
|
222
221
|
restoreStructures();
|
|
@@ -224,7 +223,7 @@
|
|
|
224
223
|
if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
|
|
225
224
|
error.incomplete = true;
|
|
226
225
|
}
|
|
227
|
-
throw error
|
|
226
|
+
throw error;
|
|
228
227
|
}
|
|
229
228
|
}
|
|
230
229
|
|
|
@@ -240,7 +239,7 @@
|
|
|
240
239
|
if (token < 0xa0) {
|
|
241
240
|
if (token < 0x80) {
|
|
242
241
|
if (token < 0x40)
|
|
243
|
-
return token
|
|
242
|
+
return token;
|
|
244
243
|
else {
|
|
245
244
|
let structure = currentStructures[token & 0x3f] ||
|
|
246
245
|
currentUnpackr.getStructures && loadStructures()[token & 0x3f];
|
|
@@ -248,9 +247,9 @@
|
|
|
248
247
|
if (!structure.read) {
|
|
249
248
|
structure.read = createStructureReader(structure, token & 0x3f);
|
|
250
249
|
}
|
|
251
|
-
return structure.read()
|
|
250
|
+
return structure.read();
|
|
252
251
|
} else
|
|
253
|
-
return token
|
|
252
|
+
return token;
|
|
254
253
|
}
|
|
255
254
|
} else if (token < 0x90) {
|
|
256
255
|
// map
|
|
@@ -263,13 +262,13 @@
|
|
|
263
262
|
key = '__proto_';
|
|
264
263
|
object[key] = read();
|
|
265
264
|
}
|
|
266
|
-
return object
|
|
265
|
+
return object;
|
|
267
266
|
} else {
|
|
268
267
|
let map = new Map();
|
|
269
268
|
for (let i = 0; i < token; i++) {
|
|
270
269
|
map.set(read(), read());
|
|
271
270
|
}
|
|
272
|
-
return map
|
|
271
|
+
return map;
|
|
273
272
|
}
|
|
274
273
|
} else {
|
|
275
274
|
token -= 0x90;
|
|
@@ -278,91 +277,91 @@
|
|
|
278
277
|
array[i] = read();
|
|
279
278
|
}
|
|
280
279
|
if (currentUnpackr.freezeData)
|
|
281
|
-
return Object.freeze(array)
|
|
282
|
-
return array
|
|
280
|
+
return Object.freeze(array);
|
|
281
|
+
return array;
|
|
283
282
|
}
|
|
284
283
|
} else if (token < 0xc0) {
|
|
285
284
|
// fixstr
|
|
286
285
|
let length = token - 0xa0;
|
|
287
286
|
if (srcStringEnd >= position$1) {
|
|
288
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
|
|
287
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart);
|
|
289
288
|
}
|
|
290
289
|
if (srcStringEnd == 0 && srcEnd < 140) {
|
|
291
290
|
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
292
291
|
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
|
|
293
292
|
if (string != null)
|
|
294
|
-
return string
|
|
293
|
+
return string;
|
|
295
294
|
}
|
|
296
|
-
return readFixedString(length)
|
|
295
|
+
return readFixedString(length);
|
|
297
296
|
} else {
|
|
298
297
|
let value;
|
|
299
298
|
switch (token) {
|
|
300
|
-
case 0xc0: return null
|
|
299
|
+
case 0xc0: return null;
|
|
301
300
|
case 0xc1:
|
|
302
301
|
if (bundledStrings$1) {
|
|
303
302
|
value = read(); // followed by the length of the string in characters (not bytes!)
|
|
304
303
|
if (value > 0)
|
|
305
|
-
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
|
|
304
|
+
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value);
|
|
306
305
|
else
|
|
307
|
-
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
|
|
306
|
+
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value);
|
|
308
307
|
}
|
|
309
308
|
return C1; // "never-used", return special object to denote that
|
|
310
|
-
case 0xc2: return false
|
|
311
|
-
case 0xc3: return true
|
|
309
|
+
case 0xc2: return false;
|
|
310
|
+
case 0xc3: return true;
|
|
312
311
|
case 0xc4:
|
|
313
312
|
// bin 8
|
|
314
313
|
value = src[position$1++];
|
|
315
314
|
if (value === undefined)
|
|
316
|
-
throw new Error('Unexpected end of buffer')
|
|
317
|
-
return readBin(value)
|
|
315
|
+
throw new Error('Unexpected end of buffer');
|
|
316
|
+
return readBin(value);
|
|
318
317
|
case 0xc5:
|
|
319
318
|
// bin 16
|
|
320
319
|
value = dataView.getUint16(position$1);
|
|
321
320
|
position$1 += 2;
|
|
322
|
-
return readBin(value)
|
|
321
|
+
return readBin(value);
|
|
323
322
|
case 0xc6:
|
|
324
323
|
// bin 32
|
|
325
324
|
value = dataView.getUint32(position$1);
|
|
326
325
|
position$1 += 4;
|
|
327
|
-
return readBin(value)
|
|
326
|
+
return readBin(value);
|
|
328
327
|
case 0xc7:
|
|
329
328
|
// ext 8
|
|
330
|
-
return readExt(src[position$1++])
|
|
329
|
+
return readExt(src[position$1++]);
|
|
331
330
|
case 0xc8:
|
|
332
331
|
// ext 16
|
|
333
332
|
value = dataView.getUint16(position$1);
|
|
334
333
|
position$1 += 2;
|
|
335
|
-
return readExt(value)
|
|
334
|
+
return readExt(value);
|
|
336
335
|
case 0xc9:
|
|
337
336
|
// ext 32
|
|
338
337
|
value = dataView.getUint32(position$1);
|
|
339
338
|
position$1 += 4;
|
|
340
|
-
return readExt(value)
|
|
339
|
+
return readExt(value);
|
|
341
340
|
case 0xca:
|
|
342
341
|
value = dataView.getFloat32(position$1);
|
|
343
342
|
if (currentUnpackr.useFloat32 > 2) {
|
|
344
343
|
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
345
344
|
let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
|
|
346
345
|
position$1 += 4;
|
|
347
|
-
return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
346
|
+
return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
|
|
348
347
|
}
|
|
349
348
|
position$1 += 4;
|
|
350
|
-
return value
|
|
349
|
+
return value;
|
|
351
350
|
case 0xcb:
|
|
352
351
|
value = dataView.getFloat64(position$1);
|
|
353
352
|
position$1 += 8;
|
|
354
|
-
return value
|
|
353
|
+
return value;
|
|
355
354
|
// uint handlers
|
|
356
355
|
case 0xcc:
|
|
357
|
-
return src[position$1++]
|
|
356
|
+
return src[position$1++];
|
|
358
357
|
case 0xcd:
|
|
359
358
|
value = dataView.getUint16(position$1);
|
|
360
359
|
position$1 += 2;
|
|
361
|
-
return value
|
|
360
|
+
return value;
|
|
362
361
|
case 0xce:
|
|
363
362
|
value = dataView.getUint32(position$1);
|
|
364
363
|
position$1 += 4;
|
|
365
|
-
return value
|
|
364
|
+
return value;
|
|
366
365
|
case 0xcf:
|
|
367
366
|
if (currentUnpackr.int64AsType === 'number') {
|
|
368
367
|
value = dataView.getUint32(position$1) * 0x100000000;
|
|
@@ -375,19 +374,19 @@
|
|
|
375
374
|
} else
|
|
376
375
|
value = dataView.getBigUint64(position$1);
|
|
377
376
|
position$1 += 8;
|
|
378
|
-
return value
|
|
377
|
+
return value;
|
|
379
378
|
|
|
380
379
|
// int handlers
|
|
381
380
|
case 0xd0:
|
|
382
|
-
return dataView.getInt8(position$1++)
|
|
381
|
+
return dataView.getInt8(position$1++);
|
|
383
382
|
case 0xd1:
|
|
384
383
|
value = dataView.getInt16(position$1);
|
|
385
384
|
position$1 += 2;
|
|
386
|
-
return value
|
|
385
|
+
return value;
|
|
387
386
|
case 0xd2:
|
|
388
387
|
value = dataView.getInt32(position$1);
|
|
389
388
|
position$1 += 4;
|
|
390
|
-
return value
|
|
389
|
+
return value;
|
|
391
390
|
case 0xd3:
|
|
392
391
|
if (currentUnpackr.int64AsType === 'number') {
|
|
393
392
|
value = dataView.getInt32(position$1) * 0x100000000;
|
|
@@ -400,96 +399,96 @@
|
|
|
400
399
|
} else
|
|
401
400
|
value = dataView.getBigInt64(position$1);
|
|
402
401
|
position$1 += 8;
|
|
403
|
-
return value
|
|
402
|
+
return value;
|
|
404
403
|
|
|
405
404
|
case 0xd4:
|
|
406
405
|
// fixext 1
|
|
407
406
|
value = src[position$1++];
|
|
408
407
|
if (value == 0x72) {
|
|
409
|
-
return recordDefinition(src[position$1++] & 0x3f)
|
|
408
|
+
return recordDefinition(src[position$1++] & 0x3f);
|
|
410
409
|
} else {
|
|
411
410
|
let extension = currentExtensions[value];
|
|
412
411
|
if (extension) {
|
|
413
412
|
if (extension.read) {
|
|
414
413
|
position$1++; // skip filler byte
|
|
415
|
-
return extension.read(read())
|
|
414
|
+
return extension.read(read());
|
|
416
415
|
} else if (extension.noBuffer) {
|
|
417
416
|
position$1++; // skip filler byte
|
|
418
|
-
return extension()
|
|
417
|
+
return extension();
|
|
419
418
|
} else
|
|
420
|
-
return extension(src.subarray(position$1, ++position$1))
|
|
419
|
+
return extension(src.subarray(position$1, ++position$1));
|
|
421
420
|
} else
|
|
422
|
-
throw new Error('Unknown extension ' + value)
|
|
421
|
+
throw new Error('Unknown extension ' + value);
|
|
423
422
|
}
|
|
424
423
|
case 0xd5:
|
|
425
424
|
// fixext 2
|
|
426
425
|
value = src[position$1];
|
|
427
426
|
if (value == 0x72) {
|
|
428
427
|
position$1++;
|
|
429
|
-
return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
|
|
428
|
+
return recordDefinition(src[position$1++] & 0x3f, src[position$1++]);
|
|
430
429
|
} else
|
|
431
|
-
return readExt(2)
|
|
430
|
+
return readExt(2);
|
|
432
431
|
case 0xd6:
|
|
433
432
|
// fixext 4
|
|
434
|
-
return readExt(4)
|
|
433
|
+
return readExt(4);
|
|
435
434
|
case 0xd7:
|
|
436
435
|
// fixext 8
|
|
437
|
-
return readExt(8)
|
|
436
|
+
return readExt(8);
|
|
438
437
|
case 0xd8:
|
|
439
438
|
// fixext 16
|
|
440
|
-
return readExt(16)
|
|
439
|
+
return readExt(16);
|
|
441
440
|
case 0xd9:
|
|
442
441
|
// str 8
|
|
443
442
|
value = src[position$1++];
|
|
444
443
|
if (srcStringEnd >= position$1) {
|
|
445
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
|
|
444
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
|
|
446
445
|
}
|
|
447
|
-
return readString8(value)
|
|
446
|
+
return readString8(value);
|
|
448
447
|
case 0xda:
|
|
449
448
|
// str 16
|
|
450
449
|
value = dataView.getUint16(position$1);
|
|
451
450
|
position$1 += 2;
|
|
452
451
|
if (srcStringEnd >= position$1) {
|
|
453
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
|
|
452
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
|
|
454
453
|
}
|
|
455
|
-
return readString16(value)
|
|
454
|
+
return readString16(value);
|
|
456
455
|
case 0xdb:
|
|
457
456
|
// str 32
|
|
458
457
|
value = dataView.getUint32(position$1);
|
|
459
458
|
position$1 += 4;
|
|
460
459
|
if (srcStringEnd >= position$1) {
|
|
461
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
|
|
460
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
|
|
462
461
|
}
|
|
463
|
-
return readString32(value)
|
|
462
|
+
return readString32(value);
|
|
464
463
|
case 0xdc:
|
|
465
464
|
// array 16
|
|
466
465
|
value = dataView.getUint16(position$1);
|
|
467
466
|
position$1 += 2;
|
|
468
|
-
return readArray(value)
|
|
467
|
+
return readArray(value);
|
|
469
468
|
case 0xdd:
|
|
470
469
|
// array 32
|
|
471
470
|
value = dataView.getUint32(position$1);
|
|
472
471
|
position$1 += 4;
|
|
473
|
-
return readArray(value)
|
|
472
|
+
return readArray(value);
|
|
474
473
|
case 0xde:
|
|
475
474
|
// map 16
|
|
476
475
|
value = dataView.getUint16(position$1);
|
|
477
476
|
position$1 += 2;
|
|
478
|
-
return readMap(value)
|
|
477
|
+
return readMap(value);
|
|
479
478
|
case 0xdf:
|
|
480
479
|
// map 32
|
|
481
480
|
value = dataView.getUint32(position$1);
|
|
482
481
|
position$1 += 4;
|
|
483
|
-
return readMap(value)
|
|
482
|
+
return readMap(value);
|
|
484
483
|
default: // negative int
|
|
485
484
|
if (token >= 0xe0)
|
|
486
|
-
return token - 0x100
|
|
485
|
+
return token - 0x100;
|
|
487
486
|
if (token === undefined) {
|
|
488
487
|
let error = new Error('Unexpected end of MessagePack data');
|
|
489
488
|
error.incomplete = true;
|
|
490
|
-
throw error
|
|
489
|
+
throw error;
|
|
491
490
|
}
|
|
492
|
-
throw new Error('Unknown MessagePack token ' + token)
|
|
491
|
+
throw new Error('Unknown MessagePack token ' + token);
|
|
493
492
|
|
|
494
493
|
}
|
|
495
494
|
}
|
|
@@ -510,7 +509,7 @@
|
|
|
510
509
|
}
|
|
511
510
|
if (structure.highByte === 0)
|
|
512
511
|
structure.read = createSecondByteReader(firstId, structure.read);
|
|
513
|
-
return optimizedReadObject() // second byte is already read, if there is one so immediately read object
|
|
512
|
+
return optimizedReadObject(); // second byte is already read, if there is one so immediately read object
|
|
514
513
|
}
|
|
515
514
|
let object = {};
|
|
516
515
|
for (let i = 0, l = structure.length; i < l; i++) {
|
|
@@ -521,38 +520,38 @@
|
|
|
521
520
|
}
|
|
522
521
|
if (currentUnpackr.freezeData)
|
|
523
522
|
return Object.freeze(object);
|
|
524
|
-
return object
|
|
523
|
+
return object;
|
|
525
524
|
}
|
|
526
525
|
readObject.count = 0;
|
|
527
526
|
if (structure.highByte === 0) {
|
|
528
|
-
return createSecondByteReader(firstId, readObject)
|
|
527
|
+
return createSecondByteReader(firstId, readObject);
|
|
529
528
|
}
|
|
530
|
-
return readObject
|
|
529
|
+
return readObject;
|
|
531
530
|
}
|
|
532
531
|
|
|
533
532
|
const createSecondByteReader = (firstId, read0) => {
|
|
534
533
|
return function() {
|
|
535
534
|
let highByte = src[position$1++];
|
|
536
535
|
if (highByte === 0)
|
|
537
|
-
return read0()
|
|
536
|
+
return read0();
|
|
538
537
|
let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
|
|
539
538
|
let structure = currentStructures[id] || loadStructures()[id];
|
|
540
539
|
if (!structure) {
|
|
541
|
-
throw new Error('Record id is not defined for ' + id)
|
|
540
|
+
throw new Error('Record id is not defined for ' + id);
|
|
542
541
|
}
|
|
543
542
|
if (!structure.read)
|
|
544
543
|
structure.read = createStructureReader(structure, firstId);
|
|
545
|
-
return structure.read()
|
|
546
|
-
}
|
|
544
|
+
return structure.read();
|
|
545
|
+
};
|
|
547
546
|
};
|
|
548
547
|
|
|
549
548
|
function loadStructures() {
|
|
550
|
-
let loadedStructures = saveState
|
|
549
|
+
let loadedStructures = saveState(() => {
|
|
551
550
|
// save the state in case getStructures modifies our buffer
|
|
552
551
|
src = null;
|
|
553
|
-
return currentUnpackr.getStructures()
|
|
552
|
+
return currentUnpackr.getStructures();
|
|
554
553
|
});
|
|
555
|
-
return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
|
|
554
|
+
return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures);
|
|
556
555
|
}
|
|
557
556
|
|
|
558
557
|
var readFixedString = readStringJS;
|
|
@@ -570,7 +569,7 @@
|
|
|
570
569
|
let string = strings[stringPosition++];
|
|
571
570
|
if (string == null) {
|
|
572
571
|
if (bundledStrings$1)
|
|
573
|
-
return readStringJS(length)
|
|
572
|
+
return readStringJS(length);
|
|
574
573
|
let byteOffset = src.byteOffset;
|
|
575
574
|
let extraction = extractStrings(position$1 - headerLength + byteOffset, srcEnd + byteOffset, src.buffer);
|
|
576
575
|
if (typeof extraction == 'string') {
|
|
@@ -582,30 +581,30 @@
|
|
|
582
581
|
srcStringEnd = 1; // even if a utf-8 string was decoded, must indicate we are in the midst of extracted strings and can't skip strings
|
|
583
582
|
string = strings[0];
|
|
584
583
|
if (string === undefined)
|
|
585
|
-
throw new Error('Unexpected end of buffer')
|
|
584
|
+
throw new Error('Unexpected end of buffer');
|
|
586
585
|
}
|
|
587
586
|
}
|
|
588
587
|
let srcStringLength = string.length;
|
|
589
588
|
if (srcStringLength <= length) {
|
|
590
589
|
position$1 += length;
|
|
591
|
-
return string
|
|
590
|
+
return string;
|
|
592
591
|
}
|
|
593
592
|
srcString = string;
|
|
594
593
|
srcStringStart = position$1;
|
|
595
594
|
srcStringEnd = position$1 + srcStringLength;
|
|
596
595
|
position$1 += length;
|
|
597
|
-
return string.slice(0, length) // we know we just want the beginning
|
|
598
|
-
}
|
|
596
|
+
return string.slice(0, length); // we know we just want the beginning
|
|
597
|
+
};
|
|
599
598
|
}
|
|
600
599
|
}
|
|
601
600
|
function readStringJS(length) {
|
|
602
601
|
let result;
|
|
603
602
|
if (length < 16) {
|
|
604
603
|
if (result = shortStringInJS(length))
|
|
605
|
-
return result
|
|
604
|
+
return result;
|
|
606
605
|
}
|
|
607
606
|
if (length > 64 && decoder)
|
|
608
|
-
return decoder.decode(src.subarray(position$1, position$1 += length))
|
|
607
|
+
return decoder.decode(src.subarray(position$1, position$1 += length));
|
|
609
608
|
const end = position$1 + length;
|
|
610
609
|
const units = [];
|
|
611
610
|
result = '';
|
|
@@ -668,17 +667,7 @@
|
|
|
668
667
|
result += fromCharCode.apply(String, units);
|
|
669
668
|
}
|
|
670
669
|
|
|
671
|
-
return result
|
|
672
|
-
}
|
|
673
|
-
function readString(source, start, length) {
|
|
674
|
-
let existingSrc = src;
|
|
675
|
-
src = source;
|
|
676
|
-
position$1 = start;
|
|
677
|
-
try {
|
|
678
|
-
return readStringJS(length);
|
|
679
|
-
} finally {
|
|
680
|
-
src = existingSrc;
|
|
681
|
-
}
|
|
670
|
+
return result;
|
|
682
671
|
}
|
|
683
672
|
|
|
684
673
|
function readArray(length) {
|
|
@@ -687,8 +676,8 @@
|
|
|
687
676
|
array[i] = read();
|
|
688
677
|
}
|
|
689
678
|
if (currentUnpackr.freezeData)
|
|
690
|
-
return Object.freeze(array)
|
|
691
|
-
return array
|
|
679
|
+
return Object.freeze(array);
|
|
680
|
+
return array;
|
|
692
681
|
}
|
|
693
682
|
|
|
694
683
|
function readMap(length) {
|
|
@@ -700,13 +689,13 @@
|
|
|
700
689
|
key = '__proto_';
|
|
701
690
|
object[key] = read();
|
|
702
691
|
}
|
|
703
|
-
return object
|
|
692
|
+
return object;
|
|
704
693
|
} else {
|
|
705
694
|
let map = new Map();
|
|
706
695
|
for (let i = 0; i < length; i++) {
|
|
707
696
|
map.set(read(), read());
|
|
708
697
|
}
|
|
709
|
-
return map
|
|
698
|
+
return map;
|
|
710
699
|
}
|
|
711
700
|
}
|
|
712
701
|
|
|
@@ -718,40 +707,40 @@
|
|
|
718
707
|
const byte = src[position$1++];
|
|
719
708
|
if ((byte & 0x80) > 0) {
|
|
720
709
|
position$1 = start;
|
|
721
|
-
return
|
|
710
|
+
return;
|
|
722
711
|
}
|
|
723
712
|
bytes[i] = byte;
|
|
724
713
|
}
|
|
725
|
-
return fromCharCode.apply(String, bytes)
|
|
714
|
+
return fromCharCode.apply(String, bytes);
|
|
726
715
|
}
|
|
727
716
|
function shortStringInJS(length) {
|
|
728
717
|
if (length < 4) {
|
|
729
718
|
if (length < 2) {
|
|
730
719
|
if (length === 0)
|
|
731
|
-
return ''
|
|
720
|
+
return '';
|
|
732
721
|
else {
|
|
733
722
|
let a = src[position$1++];
|
|
734
723
|
if ((a & 0x80) > 1) {
|
|
735
724
|
position$1 -= 1;
|
|
736
|
-
return
|
|
725
|
+
return;
|
|
737
726
|
}
|
|
738
|
-
return fromCharCode(a)
|
|
727
|
+
return fromCharCode(a);
|
|
739
728
|
}
|
|
740
729
|
} else {
|
|
741
730
|
let a = src[position$1++];
|
|
742
731
|
let b = src[position$1++];
|
|
743
732
|
if ((a & 0x80) > 0 || (b & 0x80) > 0) {
|
|
744
733
|
position$1 -= 2;
|
|
745
|
-
return
|
|
734
|
+
return;
|
|
746
735
|
}
|
|
747
736
|
if (length < 3)
|
|
748
|
-
return fromCharCode(a, b)
|
|
737
|
+
return fromCharCode(a, b);
|
|
749
738
|
let c = src[position$1++];
|
|
750
739
|
if ((c & 0x80) > 0) {
|
|
751
740
|
position$1 -= 3;
|
|
752
|
-
return
|
|
741
|
+
return;
|
|
753
742
|
}
|
|
754
|
-
return fromCharCode(a, b, c)
|
|
743
|
+
return fromCharCode(a, b, c);
|
|
755
744
|
}
|
|
756
745
|
} else {
|
|
757
746
|
let a = src[position$1++];
|
|
@@ -760,34 +749,34 @@
|
|
|
760
749
|
let d = src[position$1++];
|
|
761
750
|
if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
|
|
762
751
|
position$1 -= 4;
|
|
763
|
-
return
|
|
752
|
+
return;
|
|
764
753
|
}
|
|
765
754
|
if (length < 6) {
|
|
766
755
|
if (length === 4)
|
|
767
|
-
return fromCharCode(a, b, c, d)
|
|
756
|
+
return fromCharCode(a, b, c, d);
|
|
768
757
|
else {
|
|
769
758
|
let e = src[position$1++];
|
|
770
759
|
if ((e & 0x80) > 0) {
|
|
771
760
|
position$1 -= 5;
|
|
772
|
-
return
|
|
761
|
+
return;
|
|
773
762
|
}
|
|
774
|
-
return fromCharCode(a, b, c, d, e)
|
|
763
|
+
return fromCharCode(a, b, c, d, e);
|
|
775
764
|
}
|
|
776
765
|
} else if (length < 8) {
|
|
777
766
|
let e = src[position$1++];
|
|
778
767
|
let f = src[position$1++];
|
|
779
768
|
if ((e & 0x80) > 0 || (f & 0x80) > 0) {
|
|
780
769
|
position$1 -= 6;
|
|
781
|
-
return
|
|
770
|
+
return;
|
|
782
771
|
}
|
|
783
772
|
if (length < 7)
|
|
784
|
-
return fromCharCode(a, b, c, d, e, f)
|
|
773
|
+
return fromCharCode(a, b, c, d, e, f);
|
|
785
774
|
let g = src[position$1++];
|
|
786
775
|
if ((g & 0x80) > 0) {
|
|
787
776
|
position$1 -= 7;
|
|
788
|
-
return
|
|
777
|
+
return;
|
|
789
778
|
}
|
|
790
|
-
return fromCharCode(a, b, c, d, e, f, g)
|
|
779
|
+
return fromCharCode(a, b, c, d, e, f, g);
|
|
791
780
|
} else {
|
|
792
781
|
let e = src[position$1++];
|
|
793
782
|
let f = src[position$1++];
|
|
@@ -795,34 +784,34 @@
|
|
|
795
784
|
let h = src[position$1++];
|
|
796
785
|
if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
|
|
797
786
|
position$1 -= 8;
|
|
798
|
-
return
|
|
787
|
+
return;
|
|
799
788
|
}
|
|
800
789
|
if (length < 10) {
|
|
801
790
|
if (length === 8)
|
|
802
|
-
return fromCharCode(a, b, c, d, e, f, g, h)
|
|
791
|
+
return fromCharCode(a, b, c, d, e, f, g, h);
|
|
803
792
|
else {
|
|
804
793
|
let i = src[position$1++];
|
|
805
794
|
if ((i & 0x80) > 0) {
|
|
806
795
|
position$1 -= 9;
|
|
807
|
-
return
|
|
796
|
+
return;
|
|
808
797
|
}
|
|
809
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i)
|
|
798
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
810
799
|
}
|
|
811
800
|
} else if (length < 12) {
|
|
812
801
|
let i = src[position$1++];
|
|
813
802
|
let j = src[position$1++];
|
|
814
803
|
if ((i & 0x80) > 0 || (j & 0x80) > 0) {
|
|
815
804
|
position$1 -= 10;
|
|
816
|
-
return
|
|
805
|
+
return;
|
|
817
806
|
}
|
|
818
807
|
if (length < 11)
|
|
819
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j)
|
|
808
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
820
809
|
let k = src[position$1++];
|
|
821
810
|
if ((k & 0x80) > 0) {
|
|
822
811
|
position$1 -= 11;
|
|
823
|
-
return
|
|
812
|
+
return;
|
|
824
813
|
}
|
|
825
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
|
|
814
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
826
815
|
} else {
|
|
827
816
|
let i = src[position$1++];
|
|
828
817
|
let j = src[position$1++];
|
|
@@ -830,34 +819,34 @@
|
|
|
830
819
|
let l = src[position$1++];
|
|
831
820
|
if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
|
|
832
821
|
position$1 -= 12;
|
|
833
|
-
return
|
|
822
|
+
return;
|
|
834
823
|
}
|
|
835
824
|
if (length < 14) {
|
|
836
825
|
if (length === 12)
|
|
837
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
|
|
826
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
838
827
|
else {
|
|
839
828
|
let m = src[position$1++];
|
|
840
829
|
if ((m & 0x80) > 0) {
|
|
841
830
|
position$1 -= 13;
|
|
842
|
-
return
|
|
831
|
+
return;
|
|
843
832
|
}
|
|
844
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
|
|
833
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
845
834
|
}
|
|
846
835
|
} else {
|
|
847
836
|
let m = src[position$1++];
|
|
848
837
|
let n = src[position$1++];
|
|
849
838
|
if ((m & 0x80) > 0 || (n & 0x80) > 0) {
|
|
850
839
|
position$1 -= 14;
|
|
851
|
-
return
|
|
840
|
+
return;
|
|
852
841
|
}
|
|
853
842
|
if (length < 15)
|
|
854
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
|
|
843
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
855
844
|
let o = src[position$1++];
|
|
856
845
|
if ((o & 0x80) > 0) {
|
|
857
846
|
position$1 -= 15;
|
|
858
|
-
return
|
|
847
|
+
return;
|
|
859
848
|
}
|
|
860
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
|
|
849
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
861
850
|
}
|
|
862
851
|
}
|
|
863
852
|
}
|
|
@@ -875,22 +864,22 @@
|
|
|
875
864
|
case 0xd9:
|
|
876
865
|
// str 8
|
|
877
866
|
length = src[position$1++];
|
|
878
|
-
break
|
|
867
|
+
break;
|
|
879
868
|
case 0xda:
|
|
880
869
|
// str 16
|
|
881
870
|
length = dataView.getUint16(position$1);
|
|
882
871
|
position$1 += 2;
|
|
883
|
-
break
|
|
872
|
+
break;
|
|
884
873
|
case 0xdb:
|
|
885
874
|
// str 32
|
|
886
875
|
length = dataView.getUint32(position$1);
|
|
887
876
|
position$1 += 4;
|
|
888
|
-
break
|
|
877
|
+
break;
|
|
889
878
|
default:
|
|
890
|
-
throw new Error('Expected string')
|
|
879
|
+
throw new Error('Expected string');
|
|
891
880
|
}
|
|
892
881
|
}
|
|
893
|
-
return readStringJS(length)
|
|
882
|
+
return readStringJS(length);
|
|
894
883
|
}
|
|
895
884
|
|
|
896
885
|
|
|
@@ -898,7 +887,7 @@
|
|
|
898
887
|
return currentUnpackr.copyBuffers ?
|
|
899
888
|
// specifically use the copying slice (not the node one)
|
|
900
889
|
Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
|
|
901
|
-
src.subarray(position$1, position$1 += length)
|
|
890
|
+
src.subarray(position$1, position$1 += length);
|
|
902
891
|
}
|
|
903
892
|
function readExt(length) {
|
|
904
893
|
let type = src[position$1++];
|
|
@@ -911,10 +900,10 @@
|
|
|
911
900
|
} finally {
|
|
912
901
|
position$1 = end;
|
|
913
902
|
}
|
|
914
|
-
})
|
|
903
|
+
});
|
|
915
904
|
}
|
|
916
905
|
else
|
|
917
|
-
throw new Error('Unknown extension type ' + type)
|
|
906
|
+
throw new Error('Unknown extension type ' + type);
|
|
918
907
|
}
|
|
919
908
|
|
|
920
909
|
var keyCache = new Array(4096);
|
|
@@ -924,12 +913,12 @@
|
|
|
924
913
|
// fixstr, potentially use key cache
|
|
925
914
|
length = length - 0xa0;
|
|
926
915
|
if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
|
|
927
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
|
|
916
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart);
|
|
928
917
|
else if (!(srcStringEnd == 0 && srcEnd < 180))
|
|
929
|
-
return readFixedString(length)
|
|
918
|
+
return readFixedString(length);
|
|
930
919
|
} else { // not cacheable, go back and do a standard read
|
|
931
920
|
position$1--;
|
|
932
|
-
return asSafeString(read())
|
|
921
|
+
return asSafeString(read());
|
|
933
922
|
}
|
|
934
923
|
let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
|
|
935
924
|
let entry = keyCache[key];
|
|
@@ -942,7 +931,7 @@
|
|
|
942
931
|
chunk = dataView.getUint32(checkPosition);
|
|
943
932
|
if (chunk != entry[i++]) {
|
|
944
933
|
checkPosition = 0x70000000;
|
|
945
|
-
break
|
|
934
|
+
break;
|
|
946
935
|
}
|
|
947
936
|
checkPosition += 4;
|
|
948
937
|
}
|
|
@@ -951,12 +940,12 @@
|
|
|
951
940
|
chunk = src[checkPosition++];
|
|
952
941
|
if (chunk != entry[i++]) {
|
|
953
942
|
checkPosition = 0x70000000;
|
|
954
|
-
break
|
|
943
|
+
break;
|
|
955
944
|
}
|
|
956
945
|
}
|
|
957
946
|
if (checkPosition === end) {
|
|
958
947
|
position$1 = checkPosition;
|
|
959
|
-
return entry.string
|
|
948
|
+
return entry.string;
|
|
960
949
|
}
|
|
961
950
|
end -= 3;
|
|
962
951
|
checkPosition = position$1;
|
|
@@ -977,8 +966,8 @@
|
|
|
977
966
|
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
978
967
|
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
|
|
979
968
|
if (string != null)
|
|
980
|
-
return entry.string = string
|
|
981
|
-
return entry.string = readFixedString(length)
|
|
969
|
+
return entry.string = string;
|
|
970
|
+
return entry.string = readFixedString(length);
|
|
982
971
|
}
|
|
983
972
|
|
|
984
973
|
function asSafeString(property) {
|
|
@@ -1009,7 +998,7 @@
|
|
|
1009
998
|
}
|
|
1010
999
|
currentStructures[id] = structure;
|
|
1011
1000
|
structure.read = createStructureReader(structure, firstByte);
|
|
1012
|
-
return structure.read()
|
|
1001
|
+
return structure.read();
|
|
1013
1002
|
};
|
|
1014
1003
|
currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
|
|
1015
1004
|
currentExtensions[0].noBuffer = true;
|
|
@@ -1031,17 +1020,17 @@
|
|
|
1031
1020
|
out <<= BigInt(64);
|
|
1032
1021
|
out |= view.getBigUint64(i);
|
|
1033
1022
|
}
|
|
1034
|
-
return out
|
|
1023
|
+
return out;
|
|
1035
1024
|
}
|
|
1036
1025
|
// if (length === 8) return view.getBigUint64(start)
|
|
1037
1026
|
let middle = start + (length >> 4 << 3);
|
|
1038
1027
|
let left = decode(start, middle);
|
|
1039
1028
|
let right = decode(middle, end);
|
|
1040
|
-
return (left << BigInt((end - middle) * 8)) | right
|
|
1029
|
+
return (left << BigInt((end - middle) * 8)) | right;
|
|
1041
1030
|
};
|
|
1042
1031
|
head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
|
|
1043
1032
|
}
|
|
1044
|
-
return head
|
|
1033
|
+
return head;
|
|
1045
1034
|
};
|
|
1046
1035
|
|
|
1047
1036
|
let errors = {
|
|
@@ -1052,14 +1041,14 @@
|
|
|
1052
1041
|
if (!errors[data[0]]) {
|
|
1053
1042
|
let error = Error(data[1], { cause: data[2] });
|
|
1054
1043
|
error.name = data[0];
|
|
1055
|
-
return error
|
|
1044
|
+
return error;
|
|
1056
1045
|
}
|
|
1057
|
-
return errors[data[0]](data[1], { cause: data[2] })
|
|
1046
|
+
return errors[data[0]](data[1], { cause: data[2] });
|
|
1058
1047
|
};
|
|
1059
1048
|
|
|
1060
1049
|
currentExtensions[0x69] = (data) => {
|
|
1061
1050
|
// id extension (for structured clones)
|
|
1062
|
-
if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
|
|
1051
|
+
if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled');
|
|
1063
1052
|
let id = dataView.getUint32(position$1 - 4);
|
|
1064
1053
|
if (!referenceMap)
|
|
1065
1054
|
referenceMap = new Map();
|
|
@@ -1080,7 +1069,7 @@
|
|
|
1080
1069
|
let targetProperties = read(); // read the next value as the target object to id
|
|
1081
1070
|
if (!refEntry.used) {
|
|
1082
1071
|
// no cycle, can just use the returned read object
|
|
1083
|
-
return refEntry.target = targetProperties // replace the placeholder with the real one
|
|
1072
|
+
return refEntry.target = targetProperties; // replace the placeholder with the real one
|
|
1084
1073
|
} else {
|
|
1085
1074
|
// there is a cycle, so we have to assign properties to original target
|
|
1086
1075
|
Object.assign(target, targetProperties);
|
|
@@ -1091,16 +1080,16 @@
|
|
|
1091
1080
|
for (let [k, v] of targetProperties.entries()) target.set(k, v);
|
|
1092
1081
|
if (target instanceof Set)
|
|
1093
1082
|
for (let i of Array.from(targetProperties)) target.add(i);
|
|
1094
|
-
return target
|
|
1083
|
+
return target;
|
|
1095
1084
|
};
|
|
1096
1085
|
|
|
1097
1086
|
currentExtensions[0x70] = (data) => {
|
|
1098
1087
|
// pointer extension (for structured clones)
|
|
1099
|
-
if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
|
|
1088
|
+
if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled');
|
|
1100
1089
|
let id = dataView.getUint32(position$1 - 4);
|
|
1101
1090
|
let refEntry = referenceMap.get(id);
|
|
1102
1091
|
refEntry.used = true;
|
|
1103
|
-
return refEntry.target
|
|
1092
|
+
return refEntry.target;
|
|
1104
1093
|
};
|
|
1105
1094
|
|
|
1106
1095
|
currentExtensions[0x73] = () => new Set(read());
|
|
@@ -1115,15 +1104,15 @@
|
|
|
1115
1104
|
|
|
1116
1105
|
let typedArrayName = typedArrays[typeCode];
|
|
1117
1106
|
if (!typedArrayName) {
|
|
1118
|
-
if (typeCode === 16) return buffer
|
|
1119
|
-
if (typeCode === 17) return new DataView(buffer)
|
|
1120
|
-
throw new Error('Could not find typed array for code ' + typeCode)
|
|
1107
|
+
if (typeCode === 16) return buffer;
|
|
1108
|
+
if (typeCode === 17) return new DataView(buffer);
|
|
1109
|
+
throw new Error('Could not find typed array for code ' + typeCode);
|
|
1121
1110
|
}
|
|
1122
|
-
return new glbl[typedArrayName](buffer)
|
|
1111
|
+
return new glbl[typedArrayName](buffer);
|
|
1123
1112
|
};
|
|
1124
1113
|
currentExtensions[0x78] = () => {
|
|
1125
1114
|
let data = read();
|
|
1126
|
-
return new RegExp(data[0], data[1])
|
|
1115
|
+
return new RegExp(data[0], data[1]);
|
|
1127
1116
|
};
|
|
1128
1117
|
const TEMP_BUNDLE = [];
|
|
1129
1118
|
currentExtensions[0x62] = (data) => {
|
|
@@ -1136,30 +1125,30 @@
|
|
|
1136
1125
|
bundledStrings$1.position1 = 0;
|
|
1137
1126
|
bundledStrings$1.postBundlePosition = position$1;
|
|
1138
1127
|
position$1 = dataPosition;
|
|
1139
|
-
return read()
|
|
1128
|
+
return read();
|
|
1140
1129
|
};
|
|
1141
1130
|
|
|
1142
1131
|
currentExtensions[0xff] = (data) => {
|
|
1143
1132
|
// 32-bit date extension
|
|
1144
1133
|
if (data.length == 4)
|
|
1145
|
-
return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
|
|
1134
|
+
return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000);
|
|
1146
1135
|
else if (data.length == 8)
|
|
1147
1136
|
return new Date(
|
|
1148
1137
|
((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
|
|
1149
|
-
((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
|
|
1138
|
+
((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000);
|
|
1150
1139
|
else if (data.length == 12)
|
|
1151
1140
|
return new Date(
|
|
1152
1141
|
((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
|
|
1153
|
-
(((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
|
|
1142
|
+
(((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000);
|
|
1154
1143
|
else
|
|
1155
|
-
return new Date('invalid')
|
|
1144
|
+
return new Date('invalid');
|
|
1156
1145
|
};
|
|
1157
1146
|
// registration of bulk record definition?
|
|
1158
1147
|
// currentExtensions[0x52] = () =>
|
|
1159
1148
|
|
|
1160
|
-
function saveState
|
|
1161
|
-
if (
|
|
1162
|
-
|
|
1149
|
+
function saveState(callback) {
|
|
1150
|
+
if (currentUnpackr && currentUnpackr._onSaveState)
|
|
1151
|
+
currentUnpackr._onSaveState();
|
|
1163
1152
|
let savedSrcEnd = srcEnd;
|
|
1164
1153
|
let savedPosition = position$1;
|
|
1165
1154
|
let savedStringPosition = stringPosition;
|
|
@@ -1192,7 +1181,7 @@
|
|
|
1192
1181
|
currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
|
|
1193
1182
|
currentUnpackr = savedPackr;
|
|
1194
1183
|
dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
|
|
1195
|
-
return value
|
|
1184
|
+
return value;
|
|
1196
1185
|
}
|
|
1197
1186
|
function clearSource() {
|
|
1198
1187
|
src = null;
|
|
@@ -1226,30 +1215,28 @@
|
|
|
1226
1215
|
function roundFloat32$1(float32Number) {
|
|
1227
1216
|
f32Array[0] = float32Number;
|
|
1228
1217
|
let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
|
|
1229
|
-
return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
1230
|
-
}
|
|
1231
|
-
function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
|
|
1232
|
-
readStruct$1 = updatedReadStruct;
|
|
1233
|
-
onLoadedStructures$1 = loadedStructs;
|
|
1234
|
-
onSaveState = saveState;
|
|
1218
|
+
return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
|
|
1235
1219
|
}
|
|
1220
|
+
// Marker for downstream libraries (e.g. structon) to detect per-instance
|
|
1221
|
+
// struct-decoding hooks (this._readStruct, this._onLoadedStructures,
|
|
1222
|
+
// this._onSaveState). See `checkedRead` for the dispatch.
|
|
1223
|
+
Unpackr$1.SUPPORTS_STRUCT_HOOKS = true;
|
|
1236
1224
|
|
|
1237
|
-
let textEncoder
|
|
1225
|
+
let textEncoder;
|
|
1238
1226
|
try {
|
|
1239
|
-
textEncoder
|
|
1227
|
+
textEncoder = new TextEncoder();
|
|
1240
1228
|
} catch (error) {}
|
|
1241
1229
|
let extensions, extensionClasses;
|
|
1242
|
-
const hasNodeBuffer
|
|
1243
|
-
const ByteArrayAllocate = hasNodeBuffer
|
|
1244
|
-
function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
|
|
1245
|
-
const ByteArray = hasNodeBuffer
|
|
1246
|
-
const MAX_BUFFER_SIZE = hasNodeBuffer
|
|
1230
|
+
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
1231
|
+
const ByteArrayAllocate = hasNodeBuffer ?
|
|
1232
|
+
function(length) { return Buffer.allocUnsafeSlow(length); } : Uint8Array;
|
|
1233
|
+
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
1234
|
+
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1247
1235
|
let target, keysTarget;
|
|
1248
1236
|
let targetView;
|
|
1249
1237
|
let position = 0;
|
|
1250
1238
|
let safeEnd;
|
|
1251
1239
|
let bundledStrings = null;
|
|
1252
|
-
let writeStructSlots;
|
|
1253
1240
|
const MAX_BUNDLE_SIZE = 0x5500; // maximum characters such that the encoded bytes fits in 16 bits.
|
|
1254
1241
|
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1255
1242
|
const RECORD_SYMBOL = Symbol('record-id');
|
|
@@ -1262,10 +1249,10 @@
|
|
|
1262
1249
|
let structures;
|
|
1263
1250
|
let referenceMap;
|
|
1264
1251
|
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
|
|
1265
|
-
return target.utf8Write(string, position, target.byteLength - position)
|
|
1266
|
-
} : (textEncoder
|
|
1252
|
+
return target.utf8Write(string, position, target.byteLength - position);
|
|
1253
|
+
} : (textEncoder && textEncoder.encodeInto) ?
|
|
1267
1254
|
function(string, position) {
|
|
1268
|
-
return textEncoder
|
|
1255
|
+
return textEncoder.encodeInto(string, target.subarray(position)).written;
|
|
1269
1256
|
} : false;
|
|
1270
1257
|
|
|
1271
1258
|
let packr = this;
|
|
@@ -1277,7 +1264,7 @@
|
|
|
1277
1264
|
if (maxSharedStructures == null)
|
|
1278
1265
|
maxSharedStructures = hasSharedStructures ? 32 : 0;
|
|
1279
1266
|
if (maxSharedStructures > 8160)
|
|
1280
|
-
throw new Error('Maximum maxSharedStructure is 8160')
|
|
1267
|
+
throw new Error('Maximum maxSharedStructure is 8160');
|
|
1281
1268
|
if (options.structuredClone && options.moreTypes == undefined) {
|
|
1282
1269
|
this.moreTypes = true;
|
|
1283
1270
|
}
|
|
@@ -1291,7 +1278,7 @@
|
|
|
1291
1278
|
let sharedLimitId = maxSharedStructures + 0x40;
|
|
1292
1279
|
let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
|
|
1293
1280
|
if (maxStructureId > 8256) {
|
|
1294
|
-
throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
|
|
1281
|
+
throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192');
|
|
1295
1282
|
}
|
|
1296
1283
|
let recordIdsToRemove = [];
|
|
1297
1284
|
let transitionsCount = 0;
|
|
@@ -1327,7 +1314,7 @@
|
|
|
1327
1314
|
let sharedLength = structures.sharedLength || 0;
|
|
1328
1315
|
if (sharedLength > maxSharedStructures) {
|
|
1329
1316
|
//if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
|
|
1330
|
-
throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
|
|
1317
|
+
throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength);
|
|
1331
1318
|
}
|
|
1332
1319
|
if (!structures.transitions) {
|
|
1333
1320
|
// rebuild our structure transitions
|
|
@@ -1335,7 +1322,7 @@
|
|
|
1335
1322
|
for (let i = 0; i < sharedLength; i++) {
|
|
1336
1323
|
let keys = structures[i];
|
|
1337
1324
|
if (!keys)
|
|
1338
|
-
continue
|
|
1325
|
+
continue;
|
|
1339
1326
|
let nextTransition, transition = structures.transitions;
|
|
1340
1327
|
for (let j = 0, l = keys.length; j < l; j++) {
|
|
1341
1328
|
let key = keys[j];
|
|
@@ -1357,7 +1344,7 @@
|
|
|
1357
1344
|
hasSharedUpdate = false;
|
|
1358
1345
|
let encodingError;
|
|
1359
1346
|
try {
|
|
1360
|
-
if (packr.
|
|
1347
|
+
if (packr._writeStruct && value && typeof value === 'object') {
|
|
1361
1348
|
if (value.constructor === Object) writeStruct(value); // simple object
|
|
1362
1349
|
else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
|
|
1363
1350
|
// allow user classes, if they don't need special handling (but do use toJSON if available)
|
|
@@ -1401,15 +1388,15 @@
|
|
|
1401
1388
|
packr.offset = position;
|
|
1402
1389
|
let serialized = insertIds(target.subarray(start, position), idsToInsert);
|
|
1403
1390
|
referenceMap = null;
|
|
1404
|
-
return serialized
|
|
1391
|
+
return serialized;
|
|
1405
1392
|
}
|
|
1406
1393
|
packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1407
1394
|
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1408
1395
|
target.start = start;
|
|
1409
1396
|
target.end = position;
|
|
1410
|
-
return target
|
|
1397
|
+
return target;
|
|
1411
1398
|
}
|
|
1412
|
-
return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
|
|
1399
|
+
return target.subarray(start, position); // position can change if we call pack again in saveStructures, so we get the buffer now
|
|
1413
1400
|
} catch(error) {
|
|
1414
1401
|
encodingError = error;
|
|
1415
1402
|
throw error;
|
|
@@ -1420,16 +1407,16 @@
|
|
|
1420
1407
|
let sharedLength = structures.sharedLength || 0;
|
|
1421
1408
|
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1422
1409
|
let returnBuffer = target.subarray(start, position);
|
|
1423
|
-
let newSharedData = prepareStructures
|
|
1410
|
+
let newSharedData = (packr._prepareStructures || prepareStructures)(structures, packr);
|
|
1424
1411
|
if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
|
|
1425
1412
|
if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
|
|
1426
1413
|
// get updated structures and try again if the update failed
|
|
1427
|
-
return packr.pack(value, encodeOptions)
|
|
1414
|
+
return packr.pack(value, encodeOptions);
|
|
1428
1415
|
}
|
|
1429
1416
|
packr.lastNamedStructuresLength = sharedLength;
|
|
1430
1417
|
// don't keep large buffers around
|
|
1431
1418
|
if (target.length > 0x40000000) target = null;
|
|
1432
|
-
return returnBuffer
|
|
1419
|
+
return returnBuffer;
|
|
1433
1420
|
}
|
|
1434
1421
|
}
|
|
1435
1422
|
}
|
|
@@ -1515,7 +1502,7 @@
|
|
|
1515
1502
|
bundledStrings[twoByte ? 0 : 1] += value;
|
|
1516
1503
|
target[position++] = 0xc1;
|
|
1517
1504
|
pack(twoByte ? -strLength : strLength);
|
|
1518
|
-
return
|
|
1505
|
+
return;
|
|
1519
1506
|
}
|
|
1520
1507
|
let headerSize;
|
|
1521
1508
|
// first we estimate the header size, so we can write to the correct location
|
|
@@ -1589,7 +1576,7 @@
|
|
|
1589
1576
|
} else if (type === 'number') {
|
|
1590
1577
|
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1591
1578
|
// positive uint
|
|
1592
|
-
if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.
|
|
1579
|
+
if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this._writeStruct)) {
|
|
1593
1580
|
target[position++] = value;
|
|
1594
1581
|
} else if (value < 0x100) {
|
|
1595
1582
|
target[position++] = 0xcc;
|
|
@@ -1628,7 +1615,7 @@
|
|
|
1628
1615
|
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1629
1616
|
((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
|
|
1630
1617
|
position += 4;
|
|
1631
|
-
return
|
|
1618
|
+
return;
|
|
1632
1619
|
} else
|
|
1633
1620
|
position--; // move back into position for writing a double
|
|
1634
1621
|
}
|
|
@@ -1651,7 +1638,7 @@
|
|
|
1651
1638
|
target[position++] = 0x70; // "p" for pointer
|
|
1652
1639
|
targetView.setUint32(position, referee.id);
|
|
1653
1640
|
position += 4;
|
|
1654
|
-
return
|
|
1641
|
+
return;
|
|
1655
1642
|
} else
|
|
1656
1643
|
referenceMap.set(value, { offset: position - start });
|
|
1657
1644
|
}
|
|
@@ -1701,7 +1688,7 @@
|
|
|
1701
1688
|
} else {
|
|
1702
1689
|
pack(writeResult);
|
|
1703
1690
|
}
|
|
1704
|
-
return
|
|
1691
|
+
return;
|
|
1705
1692
|
}
|
|
1706
1693
|
let currentTarget = target;
|
|
1707
1694
|
let currentTargetView = targetView;
|
|
@@ -1718,7 +1705,7 @@
|
|
|
1718
1705
|
makeRoom(position);
|
|
1719
1706
|
return {
|
|
1720
1707
|
target, targetView, position: position - size
|
|
1721
|
-
}
|
|
1708
|
+
};
|
|
1722
1709
|
}, pack);
|
|
1723
1710
|
} finally {
|
|
1724
1711
|
// restore current target information (unless already restored)
|
|
@@ -1734,7 +1721,7 @@
|
|
|
1734
1721
|
makeRoom(result.length + position);
|
|
1735
1722
|
position = writeExtensionData(result, target, position, extension.type);
|
|
1736
1723
|
}
|
|
1737
|
-
return
|
|
1724
|
+
return;
|
|
1738
1725
|
}
|
|
1739
1726
|
}
|
|
1740
1727
|
// check isArray after extensions, because extensions can extend Array
|
|
@@ -1746,7 +1733,7 @@
|
|
|
1746
1733
|
const json = value.toJSON();
|
|
1747
1734
|
// if for some reason value.toJSON returns itself it'll loop forever
|
|
1748
1735
|
if (json !== value)
|
|
1749
|
-
return pack(json)
|
|
1736
|
+
return pack(json);
|
|
1750
1737
|
}
|
|
1751
1738
|
|
|
1752
1739
|
// if there is a writeFunction, use it, otherwise just encode as undefined
|
|
@@ -1785,7 +1772,7 @@
|
|
|
1785
1772
|
let chunks = [];
|
|
1786
1773
|
while (true) {
|
|
1787
1774
|
chunks.push(value & mask);
|
|
1788
|
-
if ((value >> BigInt(63)) === empty) break
|
|
1775
|
+
if ((value >> BigInt(63)) === empty) break;
|
|
1789
1776
|
value >>= BigInt(64);
|
|
1790
1777
|
}
|
|
1791
1778
|
|
|
@@ -1800,7 +1787,7 @@
|
|
|
1800
1787
|
string = '00' + string;
|
|
1801
1788
|
}
|
|
1802
1789
|
|
|
1803
|
-
if (hasNodeBuffer
|
|
1790
|
+
if (hasNodeBuffer) {
|
|
1804
1791
|
array = Buffer.from(string, 'hex');
|
|
1805
1792
|
} else {
|
|
1806
1793
|
array = new Uint8Array(string.length / 2);
|
|
@@ -1817,11 +1804,11 @@
|
|
|
1817
1804
|
if (array.length + position > safeEnd)
|
|
1818
1805
|
makeRoom(array.length + position);
|
|
1819
1806
|
position = writeExtensionData(array, target, position, 0x42);
|
|
1820
|
-
return
|
|
1807
|
+
return;
|
|
1821
1808
|
} else {
|
|
1822
1809
|
throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
|
|
1823
1810
|
' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
|
|
1824
|
-
' largeBigIntToString to convert to string')
|
|
1811
|
+
' largeBigIntToString to convert to string');
|
|
1825
1812
|
}
|
|
1826
1813
|
}
|
|
1827
1814
|
position += 8;
|
|
@@ -1834,7 +1821,7 @@
|
|
|
1834
1821
|
target[position++] = 0;
|
|
1835
1822
|
}
|
|
1836
1823
|
} else {
|
|
1837
|
-
throw new Error('Unknown type: ' + type)
|
|
1824
|
+
throw new Error('Unknown type: ' + type);
|
|
1838
1825
|
}
|
|
1839
1826
|
};
|
|
1840
1827
|
|
|
@@ -1980,12 +1967,30 @@
|
|
|
1980
1967
|
checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
|
|
1981
1968
|
} : writeRecord;
|
|
1982
1969
|
|
|
1970
|
+
const writeStruct = (object) => {
|
|
1971
|
+
let newPosition = packr._writeStruct(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
|
|
1972
|
+
if (notifySharedUpdate)
|
|
1973
|
+
return hasSharedUpdate = true;
|
|
1974
|
+
position = newPosition;
|
|
1975
|
+
let startTarget = target;
|
|
1976
|
+
pack(value);
|
|
1977
|
+
resetStructures();
|
|
1978
|
+
if (startTarget !== target) {
|
|
1979
|
+
return { position, targetView, target }; // indicate the buffer was re-allocated
|
|
1980
|
+
}
|
|
1981
|
+
return position;
|
|
1982
|
+
});
|
|
1983
|
+
if (newPosition === 0) // bail and go to a msgpack object
|
|
1984
|
+
return writeObject(object);
|
|
1985
|
+
position = newPosition;
|
|
1986
|
+
};
|
|
1987
|
+
|
|
1983
1988
|
const makeRoom = (end) => {
|
|
1984
1989
|
let newSize;
|
|
1985
1990
|
if (end > 0x1000000) {
|
|
1986
1991
|
// special handling for really large buffers
|
|
1987
1992
|
if ((end - start) > MAX_BUFFER_SIZE)
|
|
1988
|
-
throw new Error('Packed buffer would be larger than maximum buffer size')
|
|
1993
|
+
throw new Error('Packed buffer would be larger than maximum buffer size');
|
|
1989
1994
|
newSize = Math.min(MAX_BUFFER_SIZE,
|
|
1990
1995
|
Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
|
|
1991
1996
|
} else // faster handling for smaller buffers
|
|
@@ -2000,7 +2005,7 @@
|
|
|
2000
2005
|
position -= start;
|
|
2001
2006
|
start = 0;
|
|
2002
2007
|
safeEnd = newBuffer.length - 10;
|
|
2003
|
-
return target = newBuffer
|
|
2008
|
+
return target = newBuffer;
|
|
2004
2009
|
};
|
|
2005
2010
|
const newRecord = (transition, keys, newTransitions) => {
|
|
2006
2011
|
let recordId = structures.nextId;
|
|
@@ -2082,23 +2087,6 @@
|
|
|
2082
2087
|
target[insertionOffset + start] = keysTarget[0];
|
|
2083
2088
|
}
|
|
2084
2089
|
};
|
|
2085
|
-
const writeStruct = (object) => {
|
|
2086
|
-
let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
|
|
2087
|
-
if (notifySharedUpdate)
|
|
2088
|
-
return hasSharedUpdate = true;
|
|
2089
|
-
position = newPosition;
|
|
2090
|
-
let startTarget = target;
|
|
2091
|
-
pack(value);
|
|
2092
|
-
resetStructures();
|
|
2093
|
-
if (startTarget !== target) {
|
|
2094
|
-
return { position, targetView, target }; // indicate the buffer was re-allocated
|
|
2095
|
-
}
|
|
2096
|
-
return position;
|
|
2097
|
-
}, this);
|
|
2098
|
-
if (newPosition === 0) // bail and go to a msgpack object
|
|
2099
|
-
return writeObject(object);
|
|
2100
|
-
position = newPosition;
|
|
2101
|
-
};
|
|
2102
2090
|
}
|
|
2103
2091
|
useBuffer(buffer) {
|
|
2104
2092
|
// this means we are finished using our own buffer and we can write over it safely
|
|
@@ -2141,7 +2129,7 @@
|
|
|
2141
2129
|
} else if (isNaN(seconds)) {
|
|
2142
2130
|
if (this.onInvalidDate) {
|
|
2143
2131
|
allocateForWrite(0);
|
|
2144
|
-
return pack(this.onInvalidDate())
|
|
2132
|
+
return pack(this.onInvalidDate());
|
|
2145
2133
|
}
|
|
2146
2134
|
// Intentionally invalid timestamp
|
|
2147
2135
|
let { target, targetView, position} = allocateForWrite(3);
|
|
@@ -2162,7 +2150,7 @@
|
|
|
2162
2150
|
pack(set, allocateForWrite, pack) {
|
|
2163
2151
|
if (this.setAsEmptyObject) {
|
|
2164
2152
|
allocateForWrite(0);
|
|
2165
|
-
return pack({})
|
|
2153
|
+
return pack({});
|
|
2166
2154
|
}
|
|
2167
2155
|
let array = Array.from(set);
|
|
2168
2156
|
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
@@ -2198,7 +2186,7 @@
|
|
|
2198
2186
|
if (this.moreTypes)
|
|
2199
2187
|
writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
|
|
2200
2188
|
else
|
|
2201
|
-
writeBuffer(hasNodeBuffer
|
|
2189
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
2202
2190
|
}
|
|
2203
2191
|
}, {
|
|
2204
2192
|
pack(typedArray, allocateForWrite) {
|
|
@@ -2213,7 +2201,7 @@
|
|
|
2213
2201
|
if (this.moreTypes)
|
|
2214
2202
|
writeExtBuffer(arrayBuffer, 0x11, allocateForWrite);
|
|
2215
2203
|
else
|
|
2216
|
-
writeBuffer(hasNodeBuffer
|
|
2204
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
2217
2205
|
}
|
|
2218
2206
|
}, {
|
|
2219
2207
|
pack(c1, allocateForWrite) { // specific 0xC1 object
|
|
@@ -2270,19 +2258,19 @@
|
|
|
2270
2258
|
switch (length) {
|
|
2271
2259
|
case 1:
|
|
2272
2260
|
target[position++] = 0xd4;
|
|
2273
|
-
break
|
|
2261
|
+
break;
|
|
2274
2262
|
case 2:
|
|
2275
2263
|
target[position++] = 0xd5;
|
|
2276
|
-
break
|
|
2264
|
+
break;
|
|
2277
2265
|
case 4:
|
|
2278
2266
|
target[position++] = 0xd6;
|
|
2279
|
-
break
|
|
2267
|
+
break;
|
|
2280
2268
|
case 8:
|
|
2281
2269
|
target[position++] = 0xd7;
|
|
2282
|
-
break
|
|
2270
|
+
break;
|
|
2283
2271
|
case 16:
|
|
2284
2272
|
target[position++] = 0xd8;
|
|
2285
|
-
break
|
|
2273
|
+
break;
|
|
2286
2274
|
default:
|
|
2287
2275
|
if (length < 0x100) {
|
|
2288
2276
|
target[position++] = 0xc7;
|
|
@@ -2302,7 +2290,7 @@
|
|
|
2302
2290
|
target[position++] = type;
|
|
2303
2291
|
target.set(result, position);
|
|
2304
2292
|
position += length;
|
|
2305
|
-
return position
|
|
2293
|
+
return position;
|
|
2306
2294
|
}
|
|
2307
2295
|
|
|
2308
2296
|
function insertIds(serialized, idsToInsert) {
|
|
@@ -2324,7 +2312,7 @@
|
|
|
2324
2312
|
serialized[position++] = id & 0xff;
|
|
2325
2313
|
lastEnd = offset;
|
|
2326
2314
|
}
|
|
2327
|
-
return serialized
|
|
2315
|
+
return serialized;
|
|
2328
2316
|
}
|
|
2329
2317
|
|
|
2330
2318
|
function writeBundles(start, pack, incrementPosition) {
|
|
@@ -2341,28 +2329,29 @@
|
|
|
2341
2329
|
function addExtension$1(extension) {
|
|
2342
2330
|
if (extension.Class) {
|
|
2343
2331
|
if (!extension.pack && !extension.write)
|
|
2344
|
-
throw new Error('Extension has no pack or write function')
|
|
2332
|
+
throw new Error('Extension has no pack or write function');
|
|
2345
2333
|
if (extension.pack && !extension.type)
|
|
2346
|
-
throw new Error('Extension has no type (numeric code to identify the extension)')
|
|
2334
|
+
throw new Error('Extension has no type (numeric code to identify the extension)');
|
|
2347
2335
|
extensionClasses.unshift(extension.Class);
|
|
2348
2336
|
extensions.unshift(extension);
|
|
2349
2337
|
}
|
|
2350
2338
|
addExtension$2(extension);
|
|
2351
2339
|
}
|
|
2352
|
-
function prepareStructures
|
|
2340
|
+
function prepareStructures(structures, packr) {
|
|
2353
2341
|
structures.isCompatible = (existingStructures) => {
|
|
2354
2342
|
let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
|
|
2355
2343
|
if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
|
|
2356
2344
|
packr._mergeStructures(existingStructures);
|
|
2357
2345
|
return compatible;
|
|
2358
2346
|
};
|
|
2359
|
-
return structures
|
|
2360
|
-
}
|
|
2361
|
-
function setWriteStructSlots(writeSlots, makeStructures) {
|
|
2362
|
-
writeStructSlots = writeSlots;
|
|
2363
|
-
prepareStructures$1 = makeStructures;
|
|
2347
|
+
return structures;
|
|
2364
2348
|
}
|
|
2365
2349
|
|
|
2350
|
+
// Marker for downstream libraries (e.g. structon) to detect that this Packr
|
|
2351
|
+
// supports per-instance struct-encoding hooks (this._writeStruct,
|
|
2352
|
+
// this._prepareStructures). See `pack` for the dispatch.
|
|
2353
|
+
Packr$1.SUPPORTS_STRUCT_HOOKS = true;
|
|
2354
|
+
|
|
2366
2355
|
let defaultPackr = new Packr$1({ useRecords: false });
|
|
2367
2356
|
const pack$1 = defaultPackr.pack;
|
|
2368
2357
|
defaultPackr.pack;
|
|
@@ -2370,779 +2359,6 @@
|
|
|
2370
2359
|
const RESET_BUFFER_MODE = 1024;
|
|
2371
2360
|
const RESERVE_START_SPACE = 2048;
|
|
2372
2361
|
|
|
2373
|
-
const ASCII = 3; // the MIBenum from https://www.iana.org/assignments/character-sets/character-sets.xhtml (and other character encodings could be referenced by MIBenum)
|
|
2374
|
-
const NUMBER = 0;
|
|
2375
|
-
const UTF8 = 2;
|
|
2376
|
-
const OBJECT_DATA = 1;
|
|
2377
|
-
const DATE = 16;
|
|
2378
|
-
const TYPE_NAMES = ['num', 'object', 'string', 'ascii'];
|
|
2379
|
-
TYPE_NAMES[DATE] = 'date';
|
|
2380
|
-
const float32Headers = [false, true, true, false, false, true, true, false];
|
|
2381
|
-
let evalSupported;
|
|
2382
|
-
try {
|
|
2383
|
-
new Function('');
|
|
2384
|
-
evalSupported = true;
|
|
2385
|
-
} catch(error) {
|
|
2386
|
-
// if eval variants are not supported, do not create inline object readers ever
|
|
2387
|
-
}
|
|
2388
|
-
|
|
2389
|
-
let updatedPosition;
|
|
2390
|
-
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
2391
|
-
let textEncoder, currentSource;
|
|
2392
|
-
try {
|
|
2393
|
-
textEncoder = new TextEncoder();
|
|
2394
|
-
} catch (error) {}
|
|
2395
|
-
const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
|
|
2396
|
-
return target.utf8Write(string, position, target.byteLength - position)
|
|
2397
|
-
} : (textEncoder && textEncoder.encodeInto) ?
|
|
2398
|
-
function(target, string, position) {
|
|
2399
|
-
return textEncoder.encodeInto(string, target.subarray(position)).written
|
|
2400
|
-
} : false;
|
|
2401
|
-
setWriteStructSlots(writeStruct, prepareStructures);
|
|
2402
|
-
function writeStruct(object, target, encodingStart, position, structures, makeRoom, pack, packr) {
|
|
2403
|
-
let typedStructs = packr.typedStructs || (packr.typedStructs = []);
|
|
2404
|
-
// note that we rely on pack.js to load stored structures before we get to this point
|
|
2405
|
-
let targetView = target.dataView;
|
|
2406
|
-
let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
|
|
2407
|
-
let safeEnd = target.length - 10;
|
|
2408
|
-
let start = position;
|
|
2409
|
-
if (position > safeEnd) {
|
|
2410
|
-
target = makeRoom(position);
|
|
2411
|
-
targetView = target.dataView;
|
|
2412
|
-
position -= encodingStart;
|
|
2413
|
-
start -= encodingStart;
|
|
2414
|
-
refsStartPosition -= encodingStart;
|
|
2415
|
-
encodingStart = 0;
|
|
2416
|
-
safeEnd = target.length - 10;
|
|
2417
|
-
}
|
|
2418
|
-
|
|
2419
|
-
let refOffset, refPosition = refsStartPosition;
|
|
2420
|
-
|
|
2421
|
-
let transition = typedStructs.transitions || (typedStructs.transitions = Object.create(null));
|
|
2422
|
-
let nextId = typedStructs.nextId || typedStructs.length;
|
|
2423
|
-
let headerSize =
|
|
2424
|
-
nextId < 0xf ? 1 :
|
|
2425
|
-
nextId < 0xf0 ? 2 :
|
|
2426
|
-
nextId < 0xf000 ? 3 :
|
|
2427
|
-
nextId < 0xf00000 ? 4 : 0;
|
|
2428
|
-
if (headerSize === 0)
|
|
2429
|
-
return 0;
|
|
2430
|
-
position += headerSize;
|
|
2431
|
-
let queuedReferences = [];
|
|
2432
|
-
let usedAscii0;
|
|
2433
|
-
let keyIndex = 0;
|
|
2434
|
-
for (let key in object) {
|
|
2435
|
-
let value = object[key];
|
|
2436
|
-
let nextTransition = transition[key];
|
|
2437
|
-
if (!nextTransition) {
|
|
2438
|
-
transition[key] = nextTransition = {
|
|
2439
|
-
key,
|
|
2440
|
-
parent: transition,
|
|
2441
|
-
enumerationOffset: 0,
|
|
2442
|
-
ascii0: null,
|
|
2443
|
-
ascii8: null,
|
|
2444
|
-
num8: null,
|
|
2445
|
-
string16: null,
|
|
2446
|
-
object16: null,
|
|
2447
|
-
num32: null,
|
|
2448
|
-
float64: null,
|
|
2449
|
-
date64: null
|
|
2450
|
-
};
|
|
2451
|
-
}
|
|
2452
|
-
if (position > safeEnd) {
|
|
2453
|
-
target = makeRoom(position);
|
|
2454
|
-
targetView = target.dataView;
|
|
2455
|
-
position -= encodingStart;
|
|
2456
|
-
start -= encodingStart;
|
|
2457
|
-
refsStartPosition -= encodingStart;
|
|
2458
|
-
refPosition -= encodingStart;
|
|
2459
|
-
encodingStart = 0;
|
|
2460
|
-
safeEnd = target.length - 10;
|
|
2461
|
-
}
|
|
2462
|
-
switch (typeof value) {
|
|
2463
|
-
case 'number':
|
|
2464
|
-
let number = value;
|
|
2465
|
-
// first check to see if we are using a lot of ids and should default to wide/common format
|
|
2466
|
-
if (nextId < 200 || !nextTransition.num64) {
|
|
2467
|
-
if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
|
|
2468
|
-
if (number < 0xf6 && number >= 0 && (nextTransition.num8 && !(nextId > 200 && nextTransition.num32) || number < 0x20 && !nextTransition.num32)) {
|
|
2469
|
-
transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
|
|
2470
|
-
target[position++] = number;
|
|
2471
|
-
} else {
|
|
2472
|
-
transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
|
|
2473
|
-
targetView.setUint32(position, number, true);
|
|
2474
|
-
position += 4;
|
|
2475
|
-
}
|
|
2476
|
-
break;
|
|
2477
|
-
} else if (number < 0x100000000 && number >= -0x80000000) {
|
|
2478
|
-
targetView.setFloat32(position, number, true);
|
|
2479
|
-
if (float32Headers[target[position + 3] >>> 5]) {
|
|
2480
|
-
let xShifted;
|
|
2481
|
-
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
2482
|
-
if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
|
|
2483
|
-
transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
|
|
2484
|
-
position += 4;
|
|
2485
|
-
break;
|
|
2486
|
-
}
|
|
2487
|
-
}
|
|
2488
|
-
}
|
|
2489
|
-
}
|
|
2490
|
-
transition = nextTransition.num64 || createTypeTransition(nextTransition, NUMBER, 8);
|
|
2491
|
-
targetView.setFloat64(position, number, true);
|
|
2492
|
-
position += 8;
|
|
2493
|
-
break;
|
|
2494
|
-
case 'string':
|
|
2495
|
-
let strLength = value.length;
|
|
2496
|
-
refOffset = refPosition - refsStartPosition;
|
|
2497
|
-
if ((strLength << 2) + refPosition > safeEnd) {
|
|
2498
|
-
target = makeRoom((strLength << 2) + refPosition);
|
|
2499
|
-
targetView = target.dataView;
|
|
2500
|
-
position -= encodingStart;
|
|
2501
|
-
start -= encodingStart;
|
|
2502
|
-
refsStartPosition -= encodingStart;
|
|
2503
|
-
refPosition -= encodingStart;
|
|
2504
|
-
encodingStart = 0;
|
|
2505
|
-
safeEnd = target.length - 10;
|
|
2506
|
-
}
|
|
2507
|
-
if (strLength > ((0xff00 + refOffset) >> 2)) {
|
|
2508
|
-
queuedReferences.push(key, value, position - start);
|
|
2509
|
-
break;
|
|
2510
|
-
}
|
|
2511
|
-
let isNotAscii;
|
|
2512
|
-
let strStart = refPosition;
|
|
2513
|
-
if (strLength < 0x40) {
|
|
2514
|
-
let i, c1, c2;
|
|
2515
|
-
for (i = 0; i < strLength; i++) {
|
|
2516
|
-
c1 = value.charCodeAt(i);
|
|
2517
|
-
if (c1 < 0x80) {
|
|
2518
|
-
target[refPosition++] = c1;
|
|
2519
|
-
} else if (c1 < 0x800) {
|
|
2520
|
-
isNotAscii = true;
|
|
2521
|
-
target[refPosition++] = c1 >> 6 | 0xc0;
|
|
2522
|
-
target[refPosition++] = c1 & 0x3f | 0x80;
|
|
2523
|
-
} else if (
|
|
2524
|
-
(c1 & 0xfc00) === 0xd800 &&
|
|
2525
|
-
((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
|
2526
|
-
) {
|
|
2527
|
-
isNotAscii = true;
|
|
2528
|
-
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
2529
|
-
i++;
|
|
2530
|
-
target[refPosition++] = c1 >> 18 | 0xf0;
|
|
2531
|
-
target[refPosition++] = c1 >> 12 & 0x3f | 0x80;
|
|
2532
|
-
target[refPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
2533
|
-
target[refPosition++] = c1 & 0x3f | 0x80;
|
|
2534
|
-
} else {
|
|
2535
|
-
isNotAscii = true;
|
|
2536
|
-
target[refPosition++] = c1 >> 12 | 0xe0;
|
|
2537
|
-
target[refPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
2538
|
-
target[refPosition++] = c1 & 0x3f | 0x80;
|
|
2539
|
-
}
|
|
2540
|
-
}
|
|
2541
|
-
} else {
|
|
2542
|
-
refPosition += encodeUtf8(target, value, refPosition);
|
|
2543
|
-
isNotAscii = refPosition - strStart > strLength;
|
|
2544
|
-
}
|
|
2545
|
-
if (refOffset < 0xa0 || (refOffset < 0xf6 && (nextTransition.ascii8 || nextTransition.string8))) {
|
|
2546
|
-
// short strings
|
|
2547
|
-
if (isNotAscii) {
|
|
2548
|
-
if (!(transition = nextTransition.string8)) {
|
|
2549
|
-
if (typedStructs.length > 10 && (transition = nextTransition.ascii8)) {
|
|
2550
|
-
// we can safely change ascii to utf8 in place since they are compatible
|
|
2551
|
-
transition.__type = UTF8;
|
|
2552
|
-
nextTransition.ascii8 = null;
|
|
2553
|
-
nextTransition.string8 = transition;
|
|
2554
|
-
pack(null, 0, true); // special call to notify that structures have been updated
|
|
2555
|
-
} else {
|
|
2556
|
-
transition = createTypeTransition(nextTransition, UTF8, 1);
|
|
2557
|
-
}
|
|
2558
|
-
}
|
|
2559
|
-
} else if (refOffset === 0 && !usedAscii0) {
|
|
2560
|
-
usedAscii0 = true;
|
|
2561
|
-
transition = nextTransition.ascii0 || createTypeTransition(nextTransition, ASCII, 0);
|
|
2562
|
-
break; // don't increment position
|
|
2563
|
-
}// else ascii:
|
|
2564
|
-
else if (!(transition = nextTransition.ascii8) && !(typedStructs.length > 10 && (transition = nextTransition.string8)))
|
|
2565
|
-
transition = createTypeTransition(nextTransition, ASCII, 1);
|
|
2566
|
-
target[position++] = refOffset;
|
|
2567
|
-
} else {
|
|
2568
|
-
// TODO: Enable ascii16 at some point, but get the logic right
|
|
2569
|
-
//if (isNotAscii)
|
|
2570
|
-
transition = nextTransition.string16 || createTypeTransition(nextTransition, UTF8, 2);
|
|
2571
|
-
//else
|
|
2572
|
-
//transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
|
|
2573
|
-
targetView.setUint16(position, refOffset, true);
|
|
2574
|
-
position += 2;
|
|
2575
|
-
}
|
|
2576
|
-
break;
|
|
2577
|
-
case 'object':
|
|
2578
|
-
if (value) {
|
|
2579
|
-
if (value.constructor === Date) {
|
|
2580
|
-
transition = nextTransition.date64 || createTypeTransition(nextTransition, DATE, 8);
|
|
2581
|
-
targetView.setFloat64(position, value.getTime(), true);
|
|
2582
|
-
position += 8;
|
|
2583
|
-
} else {
|
|
2584
|
-
queuedReferences.push(key, value, keyIndex);
|
|
2585
|
-
}
|
|
2586
|
-
break;
|
|
2587
|
-
} else { // null
|
|
2588
|
-
nextTransition = anyType(nextTransition, position, targetView, -10); // match CBOR with this
|
|
2589
|
-
if (nextTransition) {
|
|
2590
|
-
transition = nextTransition;
|
|
2591
|
-
position = updatedPosition;
|
|
2592
|
-
} else queuedReferences.push(key, value, keyIndex);
|
|
2593
|
-
}
|
|
2594
|
-
break;
|
|
2595
|
-
case 'boolean':
|
|
2596
|
-
transition = nextTransition.num8 || nextTransition.ascii8 || createTypeTransition(nextTransition, NUMBER, 1);
|
|
2597
|
-
target[position++] = value ? 0xf9 : 0xf8; // match CBOR with these
|
|
2598
|
-
break;
|
|
2599
|
-
case 'undefined':
|
|
2600
|
-
nextTransition = anyType(nextTransition, position, targetView, -9); // match CBOR with this
|
|
2601
|
-
if (nextTransition) {
|
|
2602
|
-
transition = nextTransition;
|
|
2603
|
-
position = updatedPosition;
|
|
2604
|
-
} else queuedReferences.push(key, value, keyIndex);
|
|
2605
|
-
break;
|
|
2606
|
-
default:
|
|
2607
|
-
queuedReferences.push(key, value, keyIndex);
|
|
2608
|
-
}
|
|
2609
|
-
keyIndex++;
|
|
2610
|
-
}
|
|
2611
|
-
|
|
2612
|
-
for (let i = 0, l = queuedReferences.length; i < l;) {
|
|
2613
|
-
let key = queuedReferences[i++];
|
|
2614
|
-
let value = queuedReferences[i++];
|
|
2615
|
-
let propertyIndex = queuedReferences[i++];
|
|
2616
|
-
let nextTransition = transition[key];
|
|
2617
|
-
if (!nextTransition) {
|
|
2618
|
-
transition[key] = nextTransition = {
|
|
2619
|
-
key,
|
|
2620
|
-
parent: transition,
|
|
2621
|
-
enumerationOffset: propertyIndex - keyIndex,
|
|
2622
|
-
ascii0: null,
|
|
2623
|
-
ascii8: null,
|
|
2624
|
-
num8: null,
|
|
2625
|
-
string16: null,
|
|
2626
|
-
object16: null,
|
|
2627
|
-
num32: null,
|
|
2628
|
-
float64: null
|
|
2629
|
-
};
|
|
2630
|
-
}
|
|
2631
|
-
let newPosition;
|
|
2632
|
-
if (value) {
|
|
2633
|
-
/*if (typeof value === 'string') { // TODO: we could re-enable long strings
|
|
2634
|
-
if (position + value.length * 3 > safeEnd) {
|
|
2635
|
-
target = makeRoom(position + value.length * 3);
|
|
2636
|
-
position -= start;
|
|
2637
|
-
targetView = target.dataView;
|
|
2638
|
-
start = 0;
|
|
2639
|
-
}
|
|
2640
|
-
newPosition = position + target.utf8Write(value, position, 0xffffffff);
|
|
2641
|
-
} else { */
|
|
2642
|
-
let size;
|
|
2643
|
-
refOffset = refPosition - refsStartPosition;
|
|
2644
|
-
if (refOffset < 0xff00) {
|
|
2645
|
-
transition = nextTransition.object16;
|
|
2646
|
-
if (transition)
|
|
2647
|
-
size = 2;
|
|
2648
|
-
else if ((transition = nextTransition.object32))
|
|
2649
|
-
size = 4;
|
|
2650
|
-
else {
|
|
2651
|
-
transition = createTypeTransition(nextTransition, OBJECT_DATA, 2);
|
|
2652
|
-
size = 2;
|
|
2653
|
-
}
|
|
2654
|
-
} else {
|
|
2655
|
-
transition = nextTransition.object32 || createTypeTransition(nextTransition, OBJECT_DATA, 4);
|
|
2656
|
-
size = 4;
|
|
2657
|
-
}
|
|
2658
|
-
newPosition = pack(value, refPosition);
|
|
2659
|
-
//}
|
|
2660
|
-
if (typeof newPosition === 'object') {
|
|
2661
|
-
// re-allocated
|
|
2662
|
-
refPosition = newPosition.position;
|
|
2663
|
-
targetView = newPosition.targetView;
|
|
2664
|
-
target = newPosition.target;
|
|
2665
|
-
refsStartPosition -= encodingStart;
|
|
2666
|
-
position -= encodingStart;
|
|
2667
|
-
start -= encodingStart;
|
|
2668
|
-
encodingStart = 0;
|
|
2669
|
-
} else
|
|
2670
|
-
refPosition = newPosition;
|
|
2671
|
-
if (size === 2) {
|
|
2672
|
-
targetView.setUint16(position, refOffset, true);
|
|
2673
|
-
position += 2;
|
|
2674
|
-
} else {
|
|
2675
|
-
targetView.setUint32(position, refOffset, true);
|
|
2676
|
-
position += 4;
|
|
2677
|
-
}
|
|
2678
|
-
} else { // null or undefined
|
|
2679
|
-
transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
|
|
2680
|
-
targetView.setInt16(position, value === null ? -10 : -9, true);
|
|
2681
|
-
position += 2;
|
|
2682
|
-
}
|
|
2683
|
-
keyIndex++;
|
|
2684
|
-
}
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
let recordId = transition[RECORD_SYMBOL];
|
|
2688
|
-
if (recordId == null) {
|
|
2689
|
-
recordId = packr.typedStructs.length;
|
|
2690
|
-
let structure = [];
|
|
2691
|
-
let nextTransition = transition;
|
|
2692
|
-
let key, type;
|
|
2693
|
-
while ((type = nextTransition.__type) !== undefined) {
|
|
2694
|
-
let size = nextTransition.__size;
|
|
2695
|
-
nextTransition = nextTransition.__parent;
|
|
2696
|
-
key = nextTransition.key;
|
|
2697
|
-
let property = [type, size, key];
|
|
2698
|
-
if (nextTransition.enumerationOffset)
|
|
2699
|
-
property.push(nextTransition.enumerationOffset);
|
|
2700
|
-
structure.push(property);
|
|
2701
|
-
nextTransition = nextTransition.parent;
|
|
2702
|
-
}
|
|
2703
|
-
structure.reverse();
|
|
2704
|
-
transition[RECORD_SYMBOL] = recordId;
|
|
2705
|
-
packr.typedStructs[recordId] = structure;
|
|
2706
|
-
pack(null, 0, true); // special call to notify that structures have been updated
|
|
2707
|
-
}
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
switch (headerSize) {
|
|
2711
|
-
case 1:
|
|
2712
|
-
if (recordId >= 0x10) return 0;
|
|
2713
|
-
target[start] = recordId + 0x20;
|
|
2714
|
-
break;
|
|
2715
|
-
case 2:
|
|
2716
|
-
if (recordId >= 0x100) return 0;
|
|
2717
|
-
target[start] = 0x38;
|
|
2718
|
-
target[start + 1] = recordId;
|
|
2719
|
-
break;
|
|
2720
|
-
case 3:
|
|
2721
|
-
if (recordId >= 0x10000) return 0;
|
|
2722
|
-
target[start] = 0x39;
|
|
2723
|
-
targetView.setUint16(start + 1, recordId, true);
|
|
2724
|
-
break;
|
|
2725
|
-
case 4:
|
|
2726
|
-
if (recordId >= 0x1000000) return 0;
|
|
2727
|
-
targetView.setUint32(start, (recordId << 8) + 0x3a, true);
|
|
2728
|
-
break;
|
|
2729
|
-
}
|
|
2730
|
-
|
|
2731
|
-
if (position < refsStartPosition) {
|
|
2732
|
-
if (refsStartPosition === refPosition)
|
|
2733
|
-
return position; // no refs
|
|
2734
|
-
// adjust positioning
|
|
2735
|
-
target.copyWithin(position, refsStartPosition, refPosition);
|
|
2736
|
-
refPosition += position - refsStartPosition;
|
|
2737
|
-
typedStructs.lastStringStart = position - start;
|
|
2738
|
-
} else if (position > refsStartPosition) {
|
|
2739
|
-
if (refsStartPosition === refPosition)
|
|
2740
|
-
return position; // no refs
|
|
2741
|
-
typedStructs.lastStringStart = position - start;
|
|
2742
|
-
return writeStruct(object, target, encodingStart, start, structures, makeRoom, pack, packr);
|
|
2743
|
-
}
|
|
2744
|
-
return refPosition;
|
|
2745
|
-
}
|
|
2746
|
-
function anyType(transition, position, targetView, value) {
|
|
2747
|
-
let nextTransition;
|
|
2748
|
-
if ((nextTransition = transition.ascii8 || transition.num8)) {
|
|
2749
|
-
targetView.setInt8(position, value, true);
|
|
2750
|
-
updatedPosition = position + 1;
|
|
2751
|
-
return nextTransition;
|
|
2752
|
-
}
|
|
2753
|
-
if ((nextTransition = transition.string16 || transition.object16)) {
|
|
2754
|
-
targetView.setInt16(position, value, true);
|
|
2755
|
-
updatedPosition = position + 2;
|
|
2756
|
-
return nextTransition;
|
|
2757
|
-
}
|
|
2758
|
-
if (nextTransition = transition.num32) {
|
|
2759
|
-
targetView.setUint32(position, 0xe0000100 + value, true);
|
|
2760
|
-
updatedPosition = position + 4;
|
|
2761
|
-
return nextTransition;
|
|
2762
|
-
}
|
|
2763
|
-
// transition.float64
|
|
2764
|
-
if (nextTransition = transition.num64) {
|
|
2765
|
-
targetView.setFloat64(position, NaN, true);
|
|
2766
|
-
targetView.setInt8(position, value);
|
|
2767
|
-
updatedPosition = position + 8;
|
|
2768
|
-
return nextTransition;
|
|
2769
|
-
}
|
|
2770
|
-
updatedPosition = position;
|
|
2771
|
-
// TODO: can we do an "any" type where we defer the decision?
|
|
2772
|
-
return;
|
|
2773
|
-
}
|
|
2774
|
-
function createTypeTransition(transition, type, size) {
|
|
2775
|
-
let typeName = TYPE_NAMES[type] + (size << 3);
|
|
2776
|
-
let newTransition = transition[typeName] || (transition[typeName] = Object.create(null));
|
|
2777
|
-
newTransition.__type = type;
|
|
2778
|
-
newTransition.__size = size;
|
|
2779
|
-
newTransition.__parent = transition;
|
|
2780
|
-
return newTransition;
|
|
2781
|
-
}
|
|
2782
|
-
function onLoadedStructures(sharedData) {
|
|
2783
|
-
if (!(sharedData instanceof Map))
|
|
2784
|
-
return sharedData;
|
|
2785
|
-
let typed = sharedData.get('typed') || [];
|
|
2786
|
-
if (Object.isFrozen(typed))
|
|
2787
|
-
typed = typed.map(structure => structure.slice(0));
|
|
2788
|
-
let named = sharedData.get('named');
|
|
2789
|
-
let transitions = Object.create(null);
|
|
2790
|
-
for (let i = 0, l = typed.length; i < l; i++) {
|
|
2791
|
-
let structure = typed[i];
|
|
2792
|
-
let transition = transitions;
|
|
2793
|
-
for (let [type, size, key] of structure) {
|
|
2794
|
-
let nextTransition = transition[key];
|
|
2795
|
-
if (!nextTransition) {
|
|
2796
|
-
transition[key] = nextTransition = {
|
|
2797
|
-
key,
|
|
2798
|
-
parent: transition,
|
|
2799
|
-
enumerationOffset: 0,
|
|
2800
|
-
ascii0: null,
|
|
2801
|
-
ascii8: null,
|
|
2802
|
-
num8: null,
|
|
2803
|
-
string16: null,
|
|
2804
|
-
object16: null,
|
|
2805
|
-
num32: null,
|
|
2806
|
-
float64: null,
|
|
2807
|
-
date64: null,
|
|
2808
|
-
};
|
|
2809
|
-
}
|
|
2810
|
-
transition = createTypeTransition(nextTransition, type, size);
|
|
2811
|
-
}
|
|
2812
|
-
transition[RECORD_SYMBOL] = i;
|
|
2813
|
-
}
|
|
2814
|
-
typed.transitions = transitions;
|
|
2815
|
-
this.typedStructs = typed;
|
|
2816
|
-
this.lastTypedStructuresLength = typed.length;
|
|
2817
|
-
return named;
|
|
2818
|
-
}
|
|
2819
|
-
var sourceSymbol = Symbol.for('source');
|
|
2820
|
-
function readStruct(src, position, srcEnd, unpackr) {
|
|
2821
|
-
let recordId = src[position++] - 0x20;
|
|
2822
|
-
if (recordId >= 24) {
|
|
2823
|
-
switch(recordId) {
|
|
2824
|
-
case 24: recordId = src[position++]; break;
|
|
2825
|
-
// little endian:
|
|
2826
|
-
case 25: recordId = src[position++] + (src[position++] << 8); break;
|
|
2827
|
-
case 26: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16); break;
|
|
2828
|
-
case 27: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16) + (src[position++] << 24); break;
|
|
2829
|
-
}
|
|
2830
|
-
}
|
|
2831
|
-
let structure = unpackr.typedStructs && unpackr.typedStructs[recordId];
|
|
2832
|
-
if (!structure) {
|
|
2833
|
-
// copy src buffer because getStructures will override it
|
|
2834
|
-
src = Uint8Array.prototype.slice.call(src, position, srcEnd);
|
|
2835
|
-
srcEnd -= position;
|
|
2836
|
-
position = 0;
|
|
2837
|
-
if (!unpackr.getStructures)
|
|
2838
|
-
throw new Error(`Reference to shared structure ${recordId} without getStructures method`);
|
|
2839
|
-
unpackr._mergeStructures(unpackr.getStructures());
|
|
2840
|
-
if (!unpackr.typedStructs)
|
|
2841
|
-
throw new Error('Could not find any shared typed structures');
|
|
2842
|
-
unpackr.lastTypedStructuresLength = unpackr.typedStructs.length;
|
|
2843
|
-
structure = unpackr.typedStructs[recordId];
|
|
2844
|
-
if (!structure)
|
|
2845
|
-
throw new Error('Could not find typed structure ' + recordId);
|
|
2846
|
-
}
|
|
2847
|
-
var construct = structure.construct;
|
|
2848
|
-
var fullConstruct = structure.fullConstruct;
|
|
2849
|
-
if (!construct) {
|
|
2850
|
-
construct = structure.construct = function LazyObject() {
|
|
2851
|
-
};
|
|
2852
|
-
fullConstruct = structure.fullConstruct = function LoadedObject() {
|
|
2853
|
-
};
|
|
2854
|
-
fullConstruct.prototype = unpackr.structPrototype || {};
|
|
2855
|
-
var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
|
|
2856
|
-
let properties = [];
|
|
2857
|
-
let currentOffset = 0;
|
|
2858
|
-
let lastRefProperty;
|
|
2859
|
-
for (let i = 0, l = structure.length; i < l; i++) {
|
|
2860
|
-
let definition = structure[i];
|
|
2861
|
-
let [ type, size, key, enumerationOffset ] = definition;
|
|
2862
|
-
if (key === '__proto__')
|
|
2863
|
-
key = '__proto_';
|
|
2864
|
-
let property = {
|
|
2865
|
-
key,
|
|
2866
|
-
offset: currentOffset,
|
|
2867
|
-
};
|
|
2868
|
-
if (enumerationOffset)
|
|
2869
|
-
properties.splice(i + enumerationOffset, 0, property);
|
|
2870
|
-
else
|
|
2871
|
-
properties.push(property);
|
|
2872
|
-
let getRef;
|
|
2873
|
-
switch(size) { // TODO: Move into a separate function
|
|
2874
|
-
case 0: getRef = () => 0; break;
|
|
2875
|
-
case 1:
|
|
2876
|
-
getRef = (source, position) => {
|
|
2877
|
-
let ref = source.bytes[position + property.offset];
|
|
2878
|
-
return ref >= 0xf6 ? toConstant(ref) : ref;
|
|
2879
|
-
};
|
|
2880
|
-
break;
|
|
2881
|
-
case 2:
|
|
2882
|
-
getRef = (source, position) => {
|
|
2883
|
-
let src = source.bytes;
|
|
2884
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2885
|
-
let ref = dataView.getUint16(position + property.offset, true);
|
|
2886
|
-
return ref >= 0xff00 ? toConstant(ref & 0xff) : ref;
|
|
2887
|
-
};
|
|
2888
|
-
break;
|
|
2889
|
-
case 4:
|
|
2890
|
-
getRef = (source, position) => {
|
|
2891
|
-
let src = source.bytes;
|
|
2892
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2893
|
-
let ref = dataView.getUint32(position + property.offset, true);
|
|
2894
|
-
return ref >= 0xffffff00 ? toConstant(ref & 0xff) : ref;
|
|
2895
|
-
};
|
|
2896
|
-
break;
|
|
2897
|
-
}
|
|
2898
|
-
property.getRef = getRef;
|
|
2899
|
-
currentOffset += size;
|
|
2900
|
-
let get;
|
|
2901
|
-
switch(type) {
|
|
2902
|
-
case ASCII:
|
|
2903
|
-
if (lastRefProperty && !lastRefProperty.next)
|
|
2904
|
-
lastRefProperty.next = property;
|
|
2905
|
-
lastRefProperty = property;
|
|
2906
|
-
property.multiGetCount = 0;
|
|
2907
|
-
get = function(source) {
|
|
2908
|
-
let src = source.bytes;
|
|
2909
|
-
let position = source.position;
|
|
2910
|
-
let refStart = currentOffset + position;
|
|
2911
|
-
let ref = getRef(source, position);
|
|
2912
|
-
if (typeof ref !== 'number') return ref;
|
|
2913
|
-
|
|
2914
|
-
let end, next = property.next;
|
|
2915
|
-
while(next) {
|
|
2916
|
-
end = next.getRef(source, position);
|
|
2917
|
-
if (typeof end === 'number')
|
|
2918
|
-
break;
|
|
2919
|
-
else
|
|
2920
|
-
end = null;
|
|
2921
|
-
next = next.next;
|
|
2922
|
-
}
|
|
2923
|
-
if (end == null)
|
|
2924
|
-
end = source.bytesEnd - refStart;
|
|
2925
|
-
if (source.srcString) {
|
|
2926
|
-
return source.srcString.slice(ref, end);
|
|
2927
|
-
}
|
|
2928
|
-
/*if (property.multiGetCount > 0) {
|
|
2929
|
-
let asciiEnd;
|
|
2930
|
-
next = firstRefProperty;
|
|
2931
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2932
|
-
do {
|
|
2933
|
-
asciiEnd = dataView.getUint16(source.position + next.offset, true);
|
|
2934
|
-
if (asciiEnd < 0xff00)
|
|
2935
|
-
break;
|
|
2936
|
-
else
|
|
2937
|
-
asciiEnd = null;
|
|
2938
|
-
} while((next = next.next));
|
|
2939
|
-
if (asciiEnd == null)
|
|
2940
|
-
asciiEnd = source.bytesEnd - refStart
|
|
2941
|
-
source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
|
|
2942
|
-
return source.srcString.slice(ref, end);
|
|
2943
|
-
}
|
|
2944
|
-
if (source.prevStringGet) {
|
|
2945
|
-
source.prevStringGet.multiGetCount += 2;
|
|
2946
|
-
} else {
|
|
2947
|
-
source.prevStringGet = property;
|
|
2948
|
-
property.multiGetCount--;
|
|
2949
|
-
}*/
|
|
2950
|
-
return readString(src, ref + refStart, end - ref);
|
|
2951
|
-
//return src.toString('latin1', ref + refStart, end + refStart);
|
|
2952
|
-
};
|
|
2953
|
-
break;
|
|
2954
|
-
case UTF8: case OBJECT_DATA:
|
|
2955
|
-
if (lastRefProperty && !lastRefProperty.next)
|
|
2956
|
-
lastRefProperty.next = property;
|
|
2957
|
-
lastRefProperty = property;
|
|
2958
|
-
get = function(source) {
|
|
2959
|
-
let position = source.position;
|
|
2960
|
-
let refStart = currentOffset + position;
|
|
2961
|
-
let ref = getRef(source, position);
|
|
2962
|
-
if (typeof ref !== 'number') return ref;
|
|
2963
|
-
let src = source.bytes;
|
|
2964
|
-
let end, next = property.next;
|
|
2965
|
-
while(next) {
|
|
2966
|
-
end = next.getRef(source, position);
|
|
2967
|
-
if (typeof end === 'number')
|
|
2968
|
-
break;
|
|
2969
|
-
else
|
|
2970
|
-
end = null;
|
|
2971
|
-
next = next.next;
|
|
2972
|
-
}
|
|
2973
|
-
if (end == null)
|
|
2974
|
-
end = source.bytesEnd - refStart;
|
|
2975
|
-
if (type === UTF8) {
|
|
2976
|
-
return src.toString('utf8', ref + refStart, end + refStart);
|
|
2977
|
-
} else {
|
|
2978
|
-
currentSource = source;
|
|
2979
|
-
try {
|
|
2980
|
-
return unpackr.unpack(src, { start: ref + refStart, end: end + refStart });
|
|
2981
|
-
} finally {
|
|
2982
|
-
currentSource = null;
|
|
2983
|
-
}
|
|
2984
|
-
}
|
|
2985
|
-
};
|
|
2986
|
-
break;
|
|
2987
|
-
case NUMBER:
|
|
2988
|
-
switch(size) {
|
|
2989
|
-
case 4:
|
|
2990
|
-
get = function (source) {
|
|
2991
|
-
let src = source.bytes;
|
|
2992
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2993
|
-
let position = source.position + property.offset;
|
|
2994
|
-
let value = dataView.getInt32(position, true);
|
|
2995
|
-
if (value < 0x20000000) {
|
|
2996
|
-
if (value > -0x1f000000)
|
|
2997
|
-
return value;
|
|
2998
|
-
if (value > -0x20000000)
|
|
2999
|
-
return toConstant(value & 0xff);
|
|
3000
|
-
}
|
|
3001
|
-
let fValue = dataView.getFloat32(position, true);
|
|
3002
|
-
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
3003
|
-
let multiplier = mult10[((src[position + 3] & 0x7f) << 1) | (src[position + 2] >> 7)];
|
|
3004
|
-
return ((multiplier * fValue + (fValue > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
|
|
3005
|
-
};
|
|
3006
|
-
break;
|
|
3007
|
-
case 8:
|
|
3008
|
-
get = function (source) {
|
|
3009
|
-
let src = source.bytes;
|
|
3010
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
3011
|
-
let value = dataView.getFloat64(source.position + property.offset, true);
|
|
3012
|
-
if (isNaN(value)) {
|
|
3013
|
-
let byte = src[source.position + property.offset];
|
|
3014
|
-
if (byte >= 0xf6)
|
|
3015
|
-
return toConstant(byte);
|
|
3016
|
-
}
|
|
3017
|
-
return value;
|
|
3018
|
-
};
|
|
3019
|
-
break;
|
|
3020
|
-
case 1:
|
|
3021
|
-
get = function (source) {
|
|
3022
|
-
let src = source.bytes;
|
|
3023
|
-
let value = src[source.position + property.offset];
|
|
3024
|
-
return value < 0xf6 ? value : toConstant(value);
|
|
3025
|
-
};
|
|
3026
|
-
break;
|
|
3027
|
-
}
|
|
3028
|
-
break;
|
|
3029
|
-
case DATE:
|
|
3030
|
-
get = function (source) {
|
|
3031
|
-
let src = source.bytes;
|
|
3032
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
3033
|
-
return new Date(dataView.getFloat64(source.position + property.offset, true));
|
|
3034
|
-
};
|
|
3035
|
-
break;
|
|
3036
|
-
|
|
3037
|
-
}
|
|
3038
|
-
property.get = get;
|
|
3039
|
-
}
|
|
3040
|
-
// TODO: load the srcString for faster string decoding on toJSON
|
|
3041
|
-
if (evalSupported) {
|
|
3042
|
-
let objectLiteralProperties = [];
|
|
3043
|
-
let args = [];
|
|
3044
|
-
let i = 0;
|
|
3045
|
-
let hasInheritedProperties;
|
|
3046
|
-
for (let property of properties) { // assign in enumeration order
|
|
3047
|
-
if (unpackr.alwaysLazyProperty && unpackr.alwaysLazyProperty(property.key)) {
|
|
3048
|
-
// these properties are not eagerly evaluated and this can be used for creating properties
|
|
3049
|
-
// that are not serialized as JSON
|
|
3050
|
-
hasInheritedProperties = true;
|
|
3051
|
-
continue;
|
|
3052
|
-
}
|
|
3053
|
-
Object.defineProperty(prototype, property.key, { get: withSource(property.get), enumerable: true });
|
|
3054
|
-
let valueFunction = 'v' + i++;
|
|
3055
|
-
args.push(valueFunction);
|
|
3056
|
-
objectLiteralProperties.push('o[' + JSON.stringify(property.key) + ']=' + valueFunction + '(s)');
|
|
3057
|
-
}
|
|
3058
|
-
if (hasInheritedProperties) {
|
|
3059
|
-
objectLiteralProperties.push('__proto__:this');
|
|
3060
|
-
}
|
|
3061
|
-
let toObject = (new Function(...args, 'var c=this;return function(s){var o=new c();' + objectLiteralProperties.join(';') + ';return o;}')).apply(fullConstruct, properties.map(prop => prop.get));
|
|
3062
|
-
Object.defineProperty(prototype, 'toJSON', {
|
|
3063
|
-
value(omitUnderscoredProperties) {
|
|
3064
|
-
return toObject.call(this, this[sourceSymbol]);
|
|
3065
|
-
}
|
|
3066
|
-
});
|
|
3067
|
-
} else {
|
|
3068
|
-
Object.defineProperty(prototype, 'toJSON', {
|
|
3069
|
-
value(omitUnderscoredProperties) {
|
|
3070
|
-
// return an enumerable object with own properties to JSON stringify
|
|
3071
|
-
let resolved = {};
|
|
3072
|
-
for (let i = 0, l = properties.length; i < l; i++) {
|
|
3073
|
-
// TODO: check alwaysLazyProperty
|
|
3074
|
-
let key = properties[i].key;
|
|
3075
|
-
|
|
3076
|
-
resolved[key] = this[key];
|
|
3077
|
-
}
|
|
3078
|
-
return resolved;
|
|
3079
|
-
},
|
|
3080
|
-
// not enumerable or anything
|
|
3081
|
-
});
|
|
3082
|
-
}
|
|
3083
|
-
}
|
|
3084
|
-
var instance = new construct();
|
|
3085
|
-
instance[sourceSymbol] = {
|
|
3086
|
-
bytes: src,
|
|
3087
|
-
position,
|
|
3088
|
-
srcString: '',
|
|
3089
|
-
bytesEnd: srcEnd
|
|
3090
|
-
};
|
|
3091
|
-
return instance;
|
|
3092
|
-
}
|
|
3093
|
-
function toConstant(code) {
|
|
3094
|
-
switch(code) {
|
|
3095
|
-
case 0xf6: return null;
|
|
3096
|
-
case 0xf7: return undefined;
|
|
3097
|
-
case 0xf8: return false;
|
|
3098
|
-
case 0xf9: return true;
|
|
3099
|
-
}
|
|
3100
|
-
throw new Error('Unknown constant');
|
|
3101
|
-
}
|
|
3102
|
-
function withSource(get) {
|
|
3103
|
-
return function() {
|
|
3104
|
-
return get(this[sourceSymbol]);
|
|
3105
|
-
}
|
|
3106
|
-
}
|
|
3107
|
-
|
|
3108
|
-
function saveState() {
|
|
3109
|
-
if (currentSource) {
|
|
3110
|
-
currentSource.bytes = Uint8Array.prototype.slice.call(currentSource.bytes, currentSource.position, currentSource.bytesEnd);
|
|
3111
|
-
currentSource.position = 0;
|
|
3112
|
-
currentSource.bytesEnd = currentSource.bytes.length;
|
|
3113
|
-
}
|
|
3114
|
-
}
|
|
3115
|
-
function prepareStructures(structures, packr) {
|
|
3116
|
-
if (packr.typedStructs) {
|
|
3117
|
-
let structMap = new Map();
|
|
3118
|
-
structMap.set('named', structures);
|
|
3119
|
-
structMap.set('typed', packr.typedStructs);
|
|
3120
|
-
structures = structMap;
|
|
3121
|
-
}
|
|
3122
|
-
let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
|
|
3123
|
-
structures.isCompatible = existing => {
|
|
3124
|
-
let compatible = true;
|
|
3125
|
-
if (existing instanceof Map) {
|
|
3126
|
-
let named = existing.get('named') || [];
|
|
3127
|
-
if (named.length !== (packr.lastNamedStructuresLength || 0))
|
|
3128
|
-
compatible = false;
|
|
3129
|
-
let typed = existing.get('typed') || [];
|
|
3130
|
-
if (typed.length !== lastTypedStructuresLength)
|
|
3131
|
-
compatible = false;
|
|
3132
|
-
} else if (existing instanceof Array || Array.isArray(existing)) {
|
|
3133
|
-
if (existing.length !== (packr.lastNamedStructuresLength || 0))
|
|
3134
|
-
compatible = false;
|
|
3135
|
-
}
|
|
3136
|
-
if (!compatible)
|
|
3137
|
-
packr._mergeStructures(existing);
|
|
3138
|
-
return compatible;
|
|
3139
|
-
};
|
|
3140
|
-
packr.lastTypedStructuresLength = packr.typedStructs && packr.typedStructs.length;
|
|
3141
|
-
return structures;
|
|
3142
|
-
}
|
|
3143
|
-
|
|
3144
|
-
setReadStruct(readStruct, onLoadedStructures, saveState);
|
|
3145
|
-
|
|
3146
2362
|
const nativeAccelerationDisabled = process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
|
|
3147
2363
|
|
|
3148
2364
|
if (!nativeAccelerationDisabled) {
|
|
@@ -3171,18 +2387,11 @@
|
|
|
3171
2387
|
const sampleData = allSampleData[3];
|
|
3172
2388
|
function tryRequire(module) {
|
|
3173
2389
|
try {
|
|
3174
|
-
return require(module)
|
|
2390
|
+
return require(module);
|
|
3175
2391
|
} catch(error) {
|
|
3176
|
-
return {}
|
|
2392
|
+
return {};
|
|
3177
2393
|
}
|
|
3178
2394
|
}
|
|
3179
|
-
|
|
3180
|
-
let seed = 0;
|
|
3181
|
-
function random() {
|
|
3182
|
-
seed++;
|
|
3183
|
-
let a = seed * 15485863;
|
|
3184
|
-
return (a * a * a % 2038074743) / 2038074743;
|
|
3185
|
-
}
|
|
3186
2395
|
//if (typeof chai === 'undefined') { chai = require('chai') }
|
|
3187
2396
|
var assert = chai.assert;
|
|
3188
2397
|
//if (typeof msgpackr === 'undefined') { msgpackr = require('..') }
|
|
@@ -3312,39 +2521,6 @@
|
|
|
3312
2521
|
var deserialized = unpack(ab);
|
|
3313
2522
|
assert.deepEqual(deserialized, data);
|
|
3314
2523
|
});
|
|
3315
|
-
test('pack/unpack varying data with random access structures', function () {
|
|
3316
|
-
let structures = [];
|
|
3317
|
-
let packr = new Packr({
|
|
3318
|
-
structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
|
|
3319
|
-
}, getStructures() {
|
|
3320
|
-
console.log('getStructures');
|
|
3321
|
-
}
|
|
3322
|
-
});
|
|
3323
|
-
for (let i = 0; i < 2000; i++) {
|
|
3324
|
-
let data = {};
|
|
3325
|
-
let props = ['foo', 'bar', 'a', 'b', 'c', 'name', 'age', 'd'];
|
|
3326
|
-
|
|
3327
|
-
function makeString() {
|
|
3328
|
-
let str = '';
|
|
3329
|
-
while (random() < 0.9) {
|
|
3330
|
-
str += random() < 0.8 ? 'hello world' : String.fromCharCode(300);
|
|
3331
|
-
}
|
|
3332
|
-
return str;
|
|
3333
|
-
}
|
|
3334
|
-
|
|
3335
|
-
for (let i = 0; i < random() * 20; i++) {
|
|
3336
|
-
data[props[Math.floor(random() * 8)]] =
|
|
3337
|
-
random() < 0.3 ? Math.floor(random() * 400) / 2 :
|
|
3338
|
-
random() < 0.3 ? makeString() : random() < 0.3 ? true : random() < 0.3 ? sampleData : null;
|
|
3339
|
-
}
|
|
3340
|
-
var serialized = packr.pack(data);
|
|
3341
|
-
var deserialized = packr.unpack(serialized);
|
|
3342
|
-
for (let key in deserialized) {
|
|
3343
|
-
deserialized[key];
|
|
3344
|
-
}
|
|
3345
|
-
assert.deepEqual(deserialized, data);
|
|
3346
|
-
}
|
|
3347
|
-
});
|
|
3348
2524
|
|
|
3349
2525
|
for (let sampleData of allSampleData) {
|
|
3350
2526
|
let snippet = JSON.stringify(sampleData).slice(0, 20) + '...';
|
|
@@ -3367,25 +2543,6 @@
|
|
|
3367
2543
|
var deserialized = unpack(serialized);
|
|
3368
2544
|
assert.deepEqual(deserialized, data);
|
|
3369
2545
|
});
|
|
3370
|
-
test('pack/unpack sample data with random access structures ' + snippet, function () {
|
|
3371
|
-
var data = sampleData;
|
|
3372
|
-
let structures = [];
|
|
3373
|
-
let packr = new Packr({
|
|
3374
|
-
structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
|
|
3375
|
-
}, getStructures() {
|
|
3376
|
-
console.log('getStructures');
|
|
3377
|
-
}
|
|
3378
|
-
});
|
|
3379
|
-
for (let i = 0; i < 20; i++) {
|
|
3380
|
-
var serialized = packr.pack(data);
|
|
3381
|
-
var deserialized = packr.unpack(serialized, {lazy: true});
|
|
3382
|
-
var copied = {};
|
|
3383
|
-
for (let key in deserialized) {
|
|
3384
|
-
copied[key] = deserialized[key];
|
|
3385
|
-
}
|
|
3386
|
-
assert.deepEqual(copied, data);
|
|
3387
|
-
}
|
|
3388
|
-
});
|
|
3389
2546
|
test('pack/unpack sample data with bundled strings ' + snippet, function () {
|
|
3390
2547
|
var data = sampleData;
|
|
3391
2548
|
let packr = new Packr({ /*structures,*/ useRecords: false, bundleStrings: true});
|
|
@@ -3576,7 +2733,7 @@
|
|
|
3576
2733
|
|
|
3577
2734
|
}
|
|
3578
2735
|
Extended.prototype.getDouble = function() {
|
|
3579
|
-
return this.value * 2
|
|
2736
|
+
return this.value * 2;
|
|
3580
2737
|
};
|
|
3581
2738
|
var instance = new Extended();
|
|
3582
2739
|
instance.value = 4;
|
|
@@ -3596,10 +2753,10 @@
|
|
|
3596
2753
|
let data = packr.unpack(buffer);
|
|
3597
2754
|
e.value = data[0];
|
|
3598
2755
|
e.string = data[1];
|
|
3599
|
-
return e
|
|
2756
|
+
return e;
|
|
3600
2757
|
},
|
|
3601
2758
|
pack: function(instance) {
|
|
3602
|
-
return packr.pack([instance.value, instance.string])
|
|
2759
|
+
return packr.pack([instance.value, instance.string]);
|
|
3603
2760
|
}
|
|
3604
2761
|
});
|
|
3605
2762
|
var serialized = pack(data);
|
|
@@ -3625,10 +2782,10 @@
|
|
|
3625
2782
|
type: 12,
|
|
3626
2783
|
read: function(data) {
|
|
3627
2784
|
Object.setPrototypeOf(data, ExtendArray.prototype);
|
|
3628
|
-
return data
|
|
2785
|
+
return data;
|
|
3629
2786
|
},
|
|
3630
2787
|
write: function(instance) {
|
|
3631
|
-
return [...instance]
|
|
2788
|
+
return [...instance];
|
|
3632
2789
|
}
|
|
3633
2790
|
});
|
|
3634
2791
|
var serialized = pack(data);
|
|
@@ -3681,10 +2838,10 @@
|
|
|
3681
2838
|
Class: TestClass,
|
|
3682
2839
|
type: 0x01,
|
|
3683
2840
|
pack() {
|
|
3684
|
-
return typeof Buffer != 'undefined' ? Buffer.alloc(256) : new Uint8Array(256)
|
|
2841
|
+
return typeof Buffer != 'undefined' ? Buffer.alloc(256) : new Uint8Array(256);
|
|
3685
2842
|
},
|
|
3686
2843
|
unpack(data) {
|
|
3687
|
-
return data.length
|
|
2844
|
+
return data.length;
|
|
3688
2845
|
}
|
|
3689
2846
|
});
|
|
3690
2847
|
let result = unpack(pack(new TestClass()));
|
|
@@ -3696,7 +2853,7 @@
|
|
|
3696
2853
|
|
|
3697
2854
|
}
|
|
3698
2855
|
Extended.prototype.getDouble = function() {
|
|
3699
|
-
return this.value * 2
|
|
2856
|
+
return this.value * 2;
|
|
3700
2857
|
};
|
|
3701
2858
|
var instance = new Extended();
|
|
3702
2859
|
instance.value = 4;
|
|
@@ -3715,10 +2872,10 @@
|
|
|
3715
2872
|
let e = new Extended();
|
|
3716
2873
|
e.value = data[0];
|
|
3717
2874
|
e.string = data[1];
|
|
3718
|
-
return e
|
|
2875
|
+
return e;
|
|
3719
2876
|
},
|
|
3720
2877
|
write: function(instance) {
|
|
3721
|
-
return [instance.value, instance.string]
|
|
2878
|
+
return [instance.value, instance.string];
|
|
3722
2879
|
}
|
|
3723
2880
|
});
|
|
3724
2881
|
var serialized = pack(data);
|
|
@@ -3731,7 +2888,7 @@
|
|
|
3731
2888
|
|
|
3732
2889
|
}
|
|
3733
2890
|
Extended.prototype.getDouble = function() {
|
|
3734
|
-
return this.value * 2
|
|
2891
|
+
return this.value * 2;
|
|
3735
2892
|
};
|
|
3736
2893
|
var instance = new Extended();
|
|
3737
2894
|
instance.value = 4;
|
|
@@ -3748,10 +2905,10 @@
|
|
|
3748
2905
|
type: 13,
|
|
3749
2906
|
read: function(data) {
|
|
3750
2907
|
Object.setPrototypeOf(data, Extended.prototype);
|
|
3751
|
-
return data
|
|
2908
|
+
return data;
|
|
3752
2909
|
},
|
|
3753
2910
|
write: function(data) {
|
|
3754
|
-
return data
|
|
2911
|
+
return data;
|
|
3755
2912
|
}
|
|
3756
2913
|
});
|
|
3757
2914
|
var serialized = pack(data);
|
|
@@ -3776,10 +2933,10 @@
|
|
|
3776
2933
|
type: 14,
|
|
3777
2934
|
read: function(data) {
|
|
3778
2935
|
Object.setPrototypeOf(data, ExtendArray3.prototype);
|
|
3779
|
-
return data
|
|
2936
|
+
return data;
|
|
3780
2937
|
},
|
|
3781
2938
|
write: function(data) {
|
|
3782
|
-
return data
|
|
2939
|
+
return data;
|
|
3783
2940
|
}
|
|
3784
2941
|
});
|
|
3785
2942
|
var serialized = pack(data);
|
|
@@ -3794,10 +2951,10 @@
|
|
|
3794
2951
|
|
|
3795
2952
|
}
|
|
3796
2953
|
Extended.prototype.__call__ = function(){
|
|
3797
|
-
return this.value * 4
|
|
2954
|
+
return this.value * 4;
|
|
3798
2955
|
};
|
|
3799
2956
|
Extended.prototype.getDouble = function() {
|
|
3800
|
-
return this.value * 2
|
|
2957
|
+
return this.value * 2;
|
|
3801
2958
|
};
|
|
3802
2959
|
|
|
3803
2960
|
var instance = function() { instance.__call__();/* callable stuff */ };
|
|
@@ -3814,10 +2971,10 @@
|
|
|
3814
2971
|
var e = function() { e.__call__(); };
|
|
3815
2972
|
Object.setPrototypeOf(e,Extended.prototype);
|
|
3816
2973
|
e.value = packr.unpack(buffer);
|
|
3817
|
-
return e
|
|
2974
|
+
return e;
|
|
3818
2975
|
},
|
|
3819
2976
|
pack: function(instance) {
|
|
3820
|
-
return packr.pack(instance.value)
|
|
2977
|
+
return packr.pack(instance.value);
|
|
3821
2978
|
}
|
|
3822
2979
|
});
|
|
3823
2980
|
var serialized = pack(data);
|
|
@@ -3833,7 +2990,7 @@
|
|
|
3833
2990
|
addExtension({
|
|
3834
2991
|
Class: Date,
|
|
3835
2992
|
write(date) {
|
|
3836
|
-
return date.toString()
|
|
2993
|
+
return date.toString();
|
|
3837
2994
|
}
|
|
3838
2995
|
});
|
|
3839
2996
|
var serialized = pack(data);
|
|
@@ -4130,7 +3287,7 @@
|
|
|
4130
3287
|
const structures2 = [...structures];
|
|
4131
3288
|
const packr = new Packr({
|
|
4132
3289
|
getStructures() {
|
|
4133
|
-
return structures
|
|
3290
|
+
return structures;
|
|
4134
3291
|
},
|
|
4135
3292
|
saveStructures(structures) {
|
|
4136
3293
|
},
|
|
@@ -4138,7 +3295,7 @@
|
|
|
4138
3295
|
});
|
|
4139
3296
|
const packr2 = new Packr({
|
|
4140
3297
|
getStructures() {
|
|
4141
|
-
return structures2
|
|
3298
|
+
return structures2;
|
|
4142
3299
|
},
|
|
4143
3300
|
saveStructures(structures) {
|
|
4144
3301
|
},
|
|
@@ -4459,9 +3616,9 @@
|
|
|
4459
3616
|
|
|
4460
3617
|
test('pack toJSON returning this', () => {
|
|
4461
3618
|
class Serializable {
|
|
4462
|
-
someData = [1, 2, 3, 4]
|
|
3619
|
+
someData = [1, 2, 3, 4];
|
|
4463
3620
|
toJSON() {
|
|
4464
|
-
return this
|
|
3621
|
+
return this;
|
|
4465
3622
|
}
|
|
4466
3623
|
}
|
|
4467
3624
|
const serialized = pack(new Serializable);
|