msgpackr 1.11.11 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/dist/index-no-eval.cjs +209 -245
- package/dist/index-no-eval.cjs.map +1 -1
- package/dist/index-no-eval.min.js +1 -1
- package/dist/index-no-eval.min.js.map +1 -1
- package/dist/index.js +209 -245
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +245 -1072
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +246 -1137
- package/dist/test.js.map +1 -1
- package/dist/unpack-no-eval.cjs +167 -186
- package/dist/unpack-no-eval.cjs.map +1 -1
- package/index.js +5 -5
- package/iterators.js +28 -28
- package/node-index.js +12 -13
- package/pack.js +580 -611
- package/package.json +1 -1
- package/stream.js +37 -32
- package/unpack.js +612 -630
- package/struct.js +0 -815
package/dist/node.cjs
CHANGED
|
@@ -31,7 +31,6 @@ const C1 = new C1Type();
|
|
|
31
31
|
C1.name = 'MessagePack 0xC1';
|
|
32
32
|
var sequentialMode = false;
|
|
33
33
|
var inlineObjectReadThreshold = 2;
|
|
34
|
-
var readStruct$1, onLoadedStructures$1, onSaveState;
|
|
35
34
|
|
|
36
35
|
class Unpackr {
|
|
37
36
|
constructor(options) {
|
|
@@ -61,10 +60,10 @@ class Unpackr {
|
|
|
61
60
|
unpack(source, options) {
|
|
62
61
|
if (src) {
|
|
63
62
|
// re-entrant execution, save the state and restore it after we do this unpack
|
|
64
|
-
return saveState
|
|
63
|
+
return saveState(() => {
|
|
65
64
|
clearSource();
|
|
66
|
-
return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
|
|
67
|
-
})
|
|
65
|
+
return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options);
|
|
66
|
+
});
|
|
68
67
|
}
|
|
69
68
|
if (!source.buffer && source.constructor === ArrayBuffer)
|
|
70
69
|
source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
|
|
@@ -90,14 +89,14 @@ class Unpackr {
|
|
|
90
89
|
// if it doesn't have a buffer, maybe it is the wrong type of object
|
|
91
90
|
src = null;
|
|
92
91
|
if (source instanceof Uint8Array)
|
|
93
|
-
throw error
|
|
94
|
-
throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
|
|
92
|
+
throw error;
|
|
93
|
+
throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source));
|
|
95
94
|
}
|
|
96
95
|
if (this instanceof Unpackr) {
|
|
97
96
|
currentUnpackr = this;
|
|
98
97
|
if (this.structures) {
|
|
99
98
|
currentStructures = this.structures;
|
|
100
|
-
return checkedRead(
|
|
99
|
+
return checkedRead();
|
|
101
100
|
} else if (!currentStructures || currentStructures.length > 0) {
|
|
102
101
|
currentStructures = [];
|
|
103
102
|
}
|
|
@@ -106,7 +105,7 @@ class Unpackr {
|
|
|
106
105
|
if (!currentStructures || currentStructures.length > 0)
|
|
107
106
|
currentStructures = [];
|
|
108
107
|
}
|
|
109
|
-
return checkedRead(
|
|
108
|
+
return checkedRead();
|
|
110
109
|
}
|
|
111
110
|
unpackMultiple(source, forEach) {
|
|
112
111
|
let values, lastPosition = 0;
|
|
@@ -119,7 +118,7 @@ class Unpackr {
|
|
|
119
118
|
while(position$1 < size) {
|
|
120
119
|
lastPosition = position$1;
|
|
121
120
|
if (forEach(checkedRead(), lastPosition, position$1) === false) {
|
|
122
|
-
return
|
|
121
|
+
return;
|
|
123
122
|
}
|
|
124
123
|
}
|
|
125
124
|
}
|
|
@@ -129,20 +128,18 @@ class Unpackr {
|
|
|
129
128
|
lastPosition = position$1;
|
|
130
129
|
values.push(checkedRead());
|
|
131
130
|
}
|
|
132
|
-
return values
|
|
131
|
+
return values;
|
|
133
132
|
}
|
|
134
133
|
} catch(error) {
|
|
135
134
|
error.lastPosition = lastPosition;
|
|
136
135
|
error.values = values;
|
|
137
|
-
throw error
|
|
136
|
+
throw error;
|
|
138
137
|
} finally {
|
|
139
138
|
sequentialMode = false;
|
|
140
139
|
clearSource();
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
142
|
_mergeStructures(loadedStructures, existingStructures) {
|
|
144
|
-
if (onLoadedStructures$1)
|
|
145
|
-
loadedStructures = onLoadedStructures$1.call(this, loadedStructures);
|
|
146
143
|
loadedStructures = loadedStructures || [];
|
|
147
144
|
if (Object.isFrozen(loadedStructures))
|
|
148
145
|
loadedStructures = loadedStructures.map(structure => structure.slice(0));
|
|
@@ -166,10 +163,10 @@ class Unpackr {
|
|
|
166
163
|
}
|
|
167
164
|
}
|
|
168
165
|
}
|
|
169
|
-
return this.structures = loadedStructures
|
|
166
|
+
return this.structures = loadedStructures;
|
|
170
167
|
}
|
|
171
168
|
decode(source, options) {
|
|
172
|
-
return this.unpack(source, options)
|
|
169
|
+
return this.unpack(source, options);
|
|
173
170
|
}
|
|
174
171
|
}
|
|
175
172
|
function checkedRead(options) {
|
|
@@ -179,15 +176,7 @@ function checkedRead(options) {
|
|
|
179
176
|
if (sharedLength < currentStructures.length)
|
|
180
177
|
currentStructures.length = sharedLength;
|
|
181
178
|
}
|
|
182
|
-
let result;
|
|
183
|
-
if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct$1) {
|
|
184
|
-
result = readStruct$1(src, position$1, srcEnd, currentUnpackr);
|
|
185
|
-
src = null; // dispose of this so that recursive unpack calls don't save state
|
|
186
|
-
if (!(options && options.lazy) && result)
|
|
187
|
-
result = result.toJSON();
|
|
188
|
-
position$1 = srcEnd;
|
|
189
|
-
} else
|
|
190
|
-
result = read();
|
|
179
|
+
let result = read();
|
|
191
180
|
if (bundledStrings$1) { // bundled strings to skip past
|
|
192
181
|
position$1 = bundledStrings$1.postBundlePosition;
|
|
193
182
|
bundledStrings$1 = null;
|
|
@@ -207,7 +196,7 @@ function checkedRead(options) {
|
|
|
207
196
|
referenceMap = null;
|
|
208
197
|
} else if (position$1 > srcEnd) {
|
|
209
198
|
// over read
|
|
210
|
-
throw new Error('Unexpected end of MessagePack data')
|
|
199
|
+
throw new Error('Unexpected end of MessagePack data');
|
|
211
200
|
} else if (!sequentialMode) {
|
|
212
201
|
let jsonView;
|
|
213
202
|
try {
|
|
@@ -215,10 +204,10 @@ function checkedRead(options) {
|
|
|
215
204
|
} catch(error) {
|
|
216
205
|
jsonView = '(JSON view not available ' + error + ')';
|
|
217
206
|
}
|
|
218
|
-
throw new Error('Data read, but end of buffer not reached ' + jsonView)
|
|
207
|
+
throw new Error('Data read, but end of buffer not reached ' + jsonView);
|
|
219
208
|
}
|
|
220
209
|
// else more to read, but we are reading sequentially, so don't clear source yet
|
|
221
|
-
return result
|
|
210
|
+
return result;
|
|
222
211
|
} catch(error) {
|
|
223
212
|
if (currentStructures && currentStructures.restoreStructures)
|
|
224
213
|
restoreStructures();
|
|
@@ -226,7 +215,7 @@ function checkedRead(options) {
|
|
|
226
215
|
if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
|
|
227
216
|
error.incomplete = true;
|
|
228
217
|
}
|
|
229
|
-
throw error
|
|
218
|
+
throw error;
|
|
230
219
|
}
|
|
231
220
|
}
|
|
232
221
|
|
|
@@ -242,7 +231,7 @@ function read() {
|
|
|
242
231
|
if (token < 0xa0) {
|
|
243
232
|
if (token < 0x80) {
|
|
244
233
|
if (token < 0x40)
|
|
245
|
-
return token
|
|
234
|
+
return token;
|
|
246
235
|
else {
|
|
247
236
|
let structure = currentStructures[token & 0x3f] ||
|
|
248
237
|
currentUnpackr.getStructures && loadStructures()[token & 0x3f];
|
|
@@ -250,9 +239,9 @@ function read() {
|
|
|
250
239
|
if (!structure.read) {
|
|
251
240
|
structure.read = createStructureReader(structure, token & 0x3f);
|
|
252
241
|
}
|
|
253
|
-
return structure.read()
|
|
242
|
+
return structure.read();
|
|
254
243
|
} else
|
|
255
|
-
return token
|
|
244
|
+
return token;
|
|
256
245
|
}
|
|
257
246
|
} else if (token < 0x90) {
|
|
258
247
|
// map
|
|
@@ -265,13 +254,13 @@ function read() {
|
|
|
265
254
|
key = '__proto_';
|
|
266
255
|
object[key] = read();
|
|
267
256
|
}
|
|
268
|
-
return object
|
|
257
|
+
return object;
|
|
269
258
|
} else {
|
|
270
259
|
let map = new Map();
|
|
271
260
|
for (let i = 0; i < token; i++) {
|
|
272
261
|
map.set(read(), read());
|
|
273
262
|
}
|
|
274
|
-
return map
|
|
263
|
+
return map;
|
|
275
264
|
}
|
|
276
265
|
} else {
|
|
277
266
|
token -= 0x90;
|
|
@@ -280,91 +269,91 @@ function read() {
|
|
|
280
269
|
array[i] = read();
|
|
281
270
|
}
|
|
282
271
|
if (currentUnpackr.freezeData)
|
|
283
|
-
return Object.freeze(array)
|
|
284
|
-
return array
|
|
272
|
+
return Object.freeze(array);
|
|
273
|
+
return array;
|
|
285
274
|
}
|
|
286
275
|
} else if (token < 0xc0) {
|
|
287
276
|
// fixstr
|
|
288
277
|
let length = token - 0xa0;
|
|
289
278
|
if (srcStringEnd >= position$1) {
|
|
290
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
|
|
279
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart);
|
|
291
280
|
}
|
|
292
281
|
if (srcStringEnd == 0 && srcEnd < 140) {
|
|
293
282
|
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
294
283
|
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
|
|
295
284
|
if (string != null)
|
|
296
|
-
return string
|
|
285
|
+
return string;
|
|
297
286
|
}
|
|
298
|
-
return readFixedString(length)
|
|
287
|
+
return readFixedString(length);
|
|
299
288
|
} else {
|
|
300
289
|
let value;
|
|
301
290
|
switch (token) {
|
|
302
|
-
case 0xc0: return null
|
|
291
|
+
case 0xc0: return null;
|
|
303
292
|
case 0xc1:
|
|
304
293
|
if (bundledStrings$1) {
|
|
305
294
|
value = read(); // followed by the length of the string in characters (not bytes!)
|
|
306
295
|
if (value > 0)
|
|
307
|
-
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
|
|
296
|
+
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value);
|
|
308
297
|
else
|
|
309
|
-
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
|
|
298
|
+
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value);
|
|
310
299
|
}
|
|
311
300
|
return C1; // "never-used", return special object to denote that
|
|
312
|
-
case 0xc2: return false
|
|
313
|
-
case 0xc3: return true
|
|
301
|
+
case 0xc2: return false;
|
|
302
|
+
case 0xc3: return true;
|
|
314
303
|
case 0xc4:
|
|
315
304
|
// bin 8
|
|
316
305
|
value = src[position$1++];
|
|
317
306
|
if (value === undefined)
|
|
318
|
-
throw new Error('Unexpected end of buffer')
|
|
319
|
-
return readBin(value)
|
|
307
|
+
throw new Error('Unexpected end of buffer');
|
|
308
|
+
return readBin(value);
|
|
320
309
|
case 0xc5:
|
|
321
310
|
// bin 16
|
|
322
311
|
value = dataView.getUint16(position$1);
|
|
323
312
|
position$1 += 2;
|
|
324
|
-
return readBin(value)
|
|
313
|
+
return readBin(value);
|
|
325
314
|
case 0xc6:
|
|
326
315
|
// bin 32
|
|
327
316
|
value = dataView.getUint32(position$1);
|
|
328
317
|
position$1 += 4;
|
|
329
|
-
return readBin(value)
|
|
318
|
+
return readBin(value);
|
|
330
319
|
case 0xc7:
|
|
331
320
|
// ext 8
|
|
332
|
-
return readExt(src[position$1++])
|
|
321
|
+
return readExt(src[position$1++]);
|
|
333
322
|
case 0xc8:
|
|
334
323
|
// ext 16
|
|
335
324
|
value = dataView.getUint16(position$1);
|
|
336
325
|
position$1 += 2;
|
|
337
|
-
return readExt(value)
|
|
326
|
+
return readExt(value);
|
|
338
327
|
case 0xc9:
|
|
339
328
|
// ext 32
|
|
340
329
|
value = dataView.getUint32(position$1);
|
|
341
330
|
position$1 += 4;
|
|
342
|
-
return readExt(value)
|
|
331
|
+
return readExt(value);
|
|
343
332
|
case 0xca:
|
|
344
333
|
value = dataView.getFloat32(position$1);
|
|
345
334
|
if (currentUnpackr.useFloat32 > 2) {
|
|
346
335
|
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
347
336
|
let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
|
|
348
337
|
position$1 += 4;
|
|
349
|
-
return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
338
|
+
return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
|
|
350
339
|
}
|
|
351
340
|
position$1 += 4;
|
|
352
|
-
return value
|
|
341
|
+
return value;
|
|
353
342
|
case 0xcb:
|
|
354
343
|
value = dataView.getFloat64(position$1);
|
|
355
344
|
position$1 += 8;
|
|
356
|
-
return value
|
|
345
|
+
return value;
|
|
357
346
|
// uint handlers
|
|
358
347
|
case 0xcc:
|
|
359
|
-
return src[position$1++]
|
|
348
|
+
return src[position$1++];
|
|
360
349
|
case 0xcd:
|
|
361
350
|
value = dataView.getUint16(position$1);
|
|
362
351
|
position$1 += 2;
|
|
363
|
-
return value
|
|
352
|
+
return value;
|
|
364
353
|
case 0xce:
|
|
365
354
|
value = dataView.getUint32(position$1);
|
|
366
355
|
position$1 += 4;
|
|
367
|
-
return value
|
|
356
|
+
return value;
|
|
368
357
|
case 0xcf:
|
|
369
358
|
if (currentUnpackr.int64AsType === 'number') {
|
|
370
359
|
value = dataView.getUint32(position$1) * 0x100000000;
|
|
@@ -377,19 +366,19 @@ function read() {
|
|
|
377
366
|
} else
|
|
378
367
|
value = dataView.getBigUint64(position$1);
|
|
379
368
|
position$1 += 8;
|
|
380
|
-
return value
|
|
369
|
+
return value;
|
|
381
370
|
|
|
382
371
|
// int handlers
|
|
383
372
|
case 0xd0:
|
|
384
|
-
return dataView.getInt8(position$1++)
|
|
373
|
+
return dataView.getInt8(position$1++);
|
|
385
374
|
case 0xd1:
|
|
386
375
|
value = dataView.getInt16(position$1);
|
|
387
376
|
position$1 += 2;
|
|
388
|
-
return value
|
|
377
|
+
return value;
|
|
389
378
|
case 0xd2:
|
|
390
379
|
value = dataView.getInt32(position$1);
|
|
391
380
|
position$1 += 4;
|
|
392
|
-
return value
|
|
381
|
+
return value;
|
|
393
382
|
case 0xd3:
|
|
394
383
|
if (currentUnpackr.int64AsType === 'number') {
|
|
395
384
|
value = dataView.getInt32(position$1) * 0x100000000;
|
|
@@ -402,96 +391,96 @@ function read() {
|
|
|
402
391
|
} else
|
|
403
392
|
value = dataView.getBigInt64(position$1);
|
|
404
393
|
position$1 += 8;
|
|
405
|
-
return value
|
|
394
|
+
return value;
|
|
406
395
|
|
|
407
396
|
case 0xd4:
|
|
408
397
|
// fixext 1
|
|
409
398
|
value = src[position$1++];
|
|
410
399
|
if (value == 0x72) {
|
|
411
|
-
return recordDefinition(src[position$1++] & 0x3f)
|
|
400
|
+
return recordDefinition(src[position$1++] & 0x3f);
|
|
412
401
|
} else {
|
|
413
402
|
let extension = currentExtensions[value];
|
|
414
403
|
if (extension) {
|
|
415
404
|
if (extension.read) {
|
|
416
405
|
position$1++; // skip filler byte
|
|
417
|
-
return extension.read(read())
|
|
406
|
+
return extension.read(read());
|
|
418
407
|
} else if (extension.noBuffer) {
|
|
419
408
|
position$1++; // skip filler byte
|
|
420
|
-
return extension()
|
|
409
|
+
return extension();
|
|
421
410
|
} else
|
|
422
|
-
return extension(src.subarray(position$1, ++position$1))
|
|
411
|
+
return extension(src.subarray(position$1, ++position$1));
|
|
423
412
|
} else
|
|
424
|
-
throw new Error('Unknown extension ' + value)
|
|
413
|
+
throw new Error('Unknown extension ' + value);
|
|
425
414
|
}
|
|
426
415
|
case 0xd5:
|
|
427
416
|
// fixext 2
|
|
428
417
|
value = src[position$1];
|
|
429
418
|
if (value == 0x72) {
|
|
430
419
|
position$1++;
|
|
431
|
-
return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
|
|
420
|
+
return recordDefinition(src[position$1++] & 0x3f, src[position$1++]);
|
|
432
421
|
} else
|
|
433
|
-
return readExt(2)
|
|
422
|
+
return readExt(2);
|
|
434
423
|
case 0xd6:
|
|
435
424
|
// fixext 4
|
|
436
|
-
return readExt(4)
|
|
425
|
+
return readExt(4);
|
|
437
426
|
case 0xd7:
|
|
438
427
|
// fixext 8
|
|
439
|
-
return readExt(8)
|
|
428
|
+
return readExt(8);
|
|
440
429
|
case 0xd8:
|
|
441
430
|
// fixext 16
|
|
442
|
-
return readExt(16)
|
|
431
|
+
return readExt(16);
|
|
443
432
|
case 0xd9:
|
|
444
433
|
// str 8
|
|
445
434
|
value = src[position$1++];
|
|
446
435
|
if (srcStringEnd >= position$1) {
|
|
447
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
|
|
436
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
|
|
448
437
|
}
|
|
449
|
-
return readString8(value)
|
|
438
|
+
return readString8(value);
|
|
450
439
|
case 0xda:
|
|
451
440
|
// str 16
|
|
452
441
|
value = dataView.getUint16(position$1);
|
|
453
442
|
position$1 += 2;
|
|
454
443
|
if (srcStringEnd >= position$1) {
|
|
455
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
|
|
444
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
|
|
456
445
|
}
|
|
457
|
-
return readString16(value)
|
|
446
|
+
return readString16(value);
|
|
458
447
|
case 0xdb:
|
|
459
448
|
// str 32
|
|
460
449
|
value = dataView.getUint32(position$1);
|
|
461
450
|
position$1 += 4;
|
|
462
451
|
if (srcStringEnd >= position$1) {
|
|
463
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
|
|
452
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
|
|
464
453
|
}
|
|
465
|
-
return readString32(value)
|
|
454
|
+
return readString32(value);
|
|
466
455
|
case 0xdc:
|
|
467
456
|
// array 16
|
|
468
457
|
value = dataView.getUint16(position$1);
|
|
469
458
|
position$1 += 2;
|
|
470
|
-
return readArray(value)
|
|
459
|
+
return readArray(value);
|
|
471
460
|
case 0xdd:
|
|
472
461
|
// array 32
|
|
473
462
|
value = dataView.getUint32(position$1);
|
|
474
463
|
position$1 += 4;
|
|
475
|
-
return readArray(value)
|
|
464
|
+
return readArray(value);
|
|
476
465
|
case 0xde:
|
|
477
466
|
// map 16
|
|
478
467
|
value = dataView.getUint16(position$1);
|
|
479
468
|
position$1 += 2;
|
|
480
|
-
return readMap(value)
|
|
469
|
+
return readMap(value);
|
|
481
470
|
case 0xdf:
|
|
482
471
|
// map 32
|
|
483
472
|
value = dataView.getUint32(position$1);
|
|
484
473
|
position$1 += 4;
|
|
485
|
-
return readMap(value)
|
|
474
|
+
return readMap(value);
|
|
486
475
|
default: // negative int
|
|
487
476
|
if (token >= 0xe0)
|
|
488
|
-
return token - 0x100
|
|
477
|
+
return token - 0x100;
|
|
489
478
|
if (token === undefined) {
|
|
490
479
|
let error = new Error('Unexpected end of MessagePack data');
|
|
491
480
|
error.incomplete = true;
|
|
492
|
-
throw error
|
|
481
|
+
throw error;
|
|
493
482
|
}
|
|
494
|
-
throw new Error('Unknown MessagePack token ' + token)
|
|
483
|
+
throw new Error('Unknown MessagePack token ' + token);
|
|
495
484
|
|
|
496
485
|
}
|
|
497
486
|
}
|
|
@@ -512,7 +501,7 @@ function createStructureReader(structure, firstId) {
|
|
|
512
501
|
}
|
|
513
502
|
if (structure.highByte === 0)
|
|
514
503
|
structure.read = createSecondByteReader(firstId, structure.read);
|
|
515
|
-
return optimizedReadObject() // second byte is already read, if there is one so immediately read object
|
|
504
|
+
return optimizedReadObject(); // second byte is already read, if there is one so immediately read object
|
|
516
505
|
}
|
|
517
506
|
let object = {};
|
|
518
507
|
for (let i = 0, l = structure.length; i < l; i++) {
|
|
@@ -523,38 +512,38 @@ function createStructureReader(structure, firstId) {
|
|
|
523
512
|
}
|
|
524
513
|
if (currentUnpackr.freezeData)
|
|
525
514
|
return Object.freeze(object);
|
|
526
|
-
return object
|
|
515
|
+
return object;
|
|
527
516
|
}
|
|
528
517
|
readObject.count = 0;
|
|
529
518
|
if (structure.highByte === 0) {
|
|
530
|
-
return createSecondByteReader(firstId, readObject)
|
|
519
|
+
return createSecondByteReader(firstId, readObject);
|
|
531
520
|
}
|
|
532
|
-
return readObject
|
|
521
|
+
return readObject;
|
|
533
522
|
}
|
|
534
523
|
|
|
535
524
|
const createSecondByteReader = (firstId, read0) => {
|
|
536
525
|
return function() {
|
|
537
526
|
let highByte = src[position$1++];
|
|
538
527
|
if (highByte === 0)
|
|
539
|
-
return read0()
|
|
528
|
+
return read0();
|
|
540
529
|
let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
|
|
541
530
|
let structure = currentStructures[id] || loadStructures()[id];
|
|
542
531
|
if (!structure) {
|
|
543
|
-
throw new Error('Record id is not defined for ' + id)
|
|
532
|
+
throw new Error('Record id is not defined for ' + id);
|
|
544
533
|
}
|
|
545
534
|
if (!structure.read)
|
|
546
535
|
structure.read = createStructureReader(structure, firstId);
|
|
547
|
-
return structure.read()
|
|
548
|
-
}
|
|
536
|
+
return structure.read();
|
|
537
|
+
};
|
|
549
538
|
};
|
|
550
539
|
|
|
551
540
|
function loadStructures() {
|
|
552
|
-
let loadedStructures = saveState
|
|
541
|
+
let loadedStructures = saveState(() => {
|
|
553
542
|
// save the state in case getStructures modifies our buffer
|
|
554
543
|
src = null;
|
|
555
|
-
return currentUnpackr.getStructures()
|
|
544
|
+
return currentUnpackr.getStructures();
|
|
556
545
|
});
|
|
557
|
-
return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
|
|
546
|
+
return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures);
|
|
558
547
|
}
|
|
559
548
|
|
|
560
549
|
var readFixedString = readStringJS;
|
|
@@ -574,7 +563,7 @@ function setExtractor(extractStrings) {
|
|
|
574
563
|
let string = strings[stringPosition++];
|
|
575
564
|
if (string == null) {
|
|
576
565
|
if (bundledStrings$1)
|
|
577
|
-
return readStringJS(length)
|
|
566
|
+
return readStringJS(length);
|
|
578
567
|
let byteOffset = src.byteOffset;
|
|
579
568
|
let extraction = extractStrings(position$1 - headerLength + byteOffset, srcEnd + byteOffset, src.buffer);
|
|
580
569
|
if (typeof extraction == 'string') {
|
|
@@ -586,30 +575,30 @@ function setExtractor(extractStrings) {
|
|
|
586
575
|
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
|
|
587
576
|
string = strings[0];
|
|
588
577
|
if (string === undefined)
|
|
589
|
-
throw new Error('Unexpected end of buffer')
|
|
578
|
+
throw new Error('Unexpected end of buffer');
|
|
590
579
|
}
|
|
591
580
|
}
|
|
592
581
|
let srcStringLength = string.length;
|
|
593
582
|
if (srcStringLength <= length) {
|
|
594
583
|
position$1 += length;
|
|
595
|
-
return string
|
|
584
|
+
return string;
|
|
596
585
|
}
|
|
597
586
|
srcString = string;
|
|
598
587
|
srcStringStart = position$1;
|
|
599
588
|
srcStringEnd = position$1 + srcStringLength;
|
|
600
589
|
position$1 += length;
|
|
601
|
-
return string.slice(0, length) // we know we just want the beginning
|
|
602
|
-
}
|
|
590
|
+
return string.slice(0, length); // we know we just want the beginning
|
|
591
|
+
};
|
|
603
592
|
}
|
|
604
593
|
}
|
|
605
594
|
function readStringJS(length) {
|
|
606
595
|
let result;
|
|
607
596
|
if (length < 16) {
|
|
608
597
|
if (result = shortStringInJS(length))
|
|
609
|
-
return result
|
|
598
|
+
return result;
|
|
610
599
|
}
|
|
611
600
|
if (length > 64 && decoder)
|
|
612
|
-
return decoder.decode(src.subarray(position$1, position$1 += length))
|
|
601
|
+
return decoder.decode(src.subarray(position$1, position$1 += length));
|
|
613
602
|
const end = position$1 + length;
|
|
614
603
|
const units = [];
|
|
615
604
|
result = '';
|
|
@@ -672,17 +661,7 @@ function readStringJS(length) {
|
|
|
672
661
|
result += fromCharCode.apply(String, units);
|
|
673
662
|
}
|
|
674
663
|
|
|
675
|
-
return result
|
|
676
|
-
}
|
|
677
|
-
function readString(source, start, length) {
|
|
678
|
-
let existingSrc = src;
|
|
679
|
-
src = source;
|
|
680
|
-
position$1 = start;
|
|
681
|
-
try {
|
|
682
|
-
return readStringJS(length);
|
|
683
|
-
} finally {
|
|
684
|
-
src = existingSrc;
|
|
685
|
-
}
|
|
664
|
+
return result;
|
|
686
665
|
}
|
|
687
666
|
|
|
688
667
|
function readArray(length) {
|
|
@@ -691,8 +670,8 @@ function readArray(length) {
|
|
|
691
670
|
array[i] = read();
|
|
692
671
|
}
|
|
693
672
|
if (currentUnpackr.freezeData)
|
|
694
|
-
return Object.freeze(array)
|
|
695
|
-
return array
|
|
673
|
+
return Object.freeze(array);
|
|
674
|
+
return array;
|
|
696
675
|
}
|
|
697
676
|
|
|
698
677
|
function readMap(length) {
|
|
@@ -704,13 +683,13 @@ function readMap(length) {
|
|
|
704
683
|
key = '__proto_';
|
|
705
684
|
object[key] = read();
|
|
706
685
|
}
|
|
707
|
-
return object
|
|
686
|
+
return object;
|
|
708
687
|
} else {
|
|
709
688
|
let map = new Map();
|
|
710
689
|
for (let i = 0; i < length; i++) {
|
|
711
690
|
map.set(read(), read());
|
|
712
691
|
}
|
|
713
|
-
return map
|
|
692
|
+
return map;
|
|
714
693
|
}
|
|
715
694
|
}
|
|
716
695
|
|
|
@@ -722,40 +701,40 @@ function longStringInJS(length) {
|
|
|
722
701
|
const byte = src[position$1++];
|
|
723
702
|
if ((byte & 0x80) > 0) {
|
|
724
703
|
position$1 = start;
|
|
725
|
-
return
|
|
704
|
+
return;
|
|
726
705
|
}
|
|
727
706
|
bytes[i] = byte;
|
|
728
707
|
}
|
|
729
|
-
return fromCharCode.apply(String, bytes)
|
|
708
|
+
return fromCharCode.apply(String, bytes);
|
|
730
709
|
}
|
|
731
710
|
function shortStringInJS(length) {
|
|
732
711
|
if (length < 4) {
|
|
733
712
|
if (length < 2) {
|
|
734
713
|
if (length === 0)
|
|
735
|
-
return ''
|
|
714
|
+
return '';
|
|
736
715
|
else {
|
|
737
716
|
let a = src[position$1++];
|
|
738
717
|
if ((a & 0x80) > 1) {
|
|
739
718
|
position$1 -= 1;
|
|
740
|
-
return
|
|
719
|
+
return;
|
|
741
720
|
}
|
|
742
|
-
return fromCharCode(a)
|
|
721
|
+
return fromCharCode(a);
|
|
743
722
|
}
|
|
744
723
|
} else {
|
|
745
724
|
let a = src[position$1++];
|
|
746
725
|
let b = src[position$1++];
|
|
747
726
|
if ((a & 0x80) > 0 || (b & 0x80) > 0) {
|
|
748
727
|
position$1 -= 2;
|
|
749
|
-
return
|
|
728
|
+
return;
|
|
750
729
|
}
|
|
751
730
|
if (length < 3)
|
|
752
|
-
return fromCharCode(a, b)
|
|
731
|
+
return fromCharCode(a, b);
|
|
753
732
|
let c = src[position$1++];
|
|
754
733
|
if ((c & 0x80) > 0) {
|
|
755
734
|
position$1 -= 3;
|
|
756
|
-
return
|
|
735
|
+
return;
|
|
757
736
|
}
|
|
758
|
-
return fromCharCode(a, b, c)
|
|
737
|
+
return fromCharCode(a, b, c);
|
|
759
738
|
}
|
|
760
739
|
} else {
|
|
761
740
|
let a = src[position$1++];
|
|
@@ -764,34 +743,34 @@ function shortStringInJS(length) {
|
|
|
764
743
|
let d = src[position$1++];
|
|
765
744
|
if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
|
|
766
745
|
position$1 -= 4;
|
|
767
|
-
return
|
|
746
|
+
return;
|
|
768
747
|
}
|
|
769
748
|
if (length < 6) {
|
|
770
749
|
if (length === 4)
|
|
771
|
-
return fromCharCode(a, b, c, d)
|
|
750
|
+
return fromCharCode(a, b, c, d);
|
|
772
751
|
else {
|
|
773
752
|
let e = src[position$1++];
|
|
774
753
|
if ((e & 0x80) > 0) {
|
|
775
754
|
position$1 -= 5;
|
|
776
|
-
return
|
|
755
|
+
return;
|
|
777
756
|
}
|
|
778
|
-
return fromCharCode(a, b, c, d, e)
|
|
757
|
+
return fromCharCode(a, b, c, d, e);
|
|
779
758
|
}
|
|
780
759
|
} else if (length < 8) {
|
|
781
760
|
let e = src[position$1++];
|
|
782
761
|
let f = src[position$1++];
|
|
783
762
|
if ((e & 0x80) > 0 || (f & 0x80) > 0) {
|
|
784
763
|
position$1 -= 6;
|
|
785
|
-
return
|
|
764
|
+
return;
|
|
786
765
|
}
|
|
787
766
|
if (length < 7)
|
|
788
|
-
return fromCharCode(a, b, c, d, e, f)
|
|
767
|
+
return fromCharCode(a, b, c, d, e, f);
|
|
789
768
|
let g = src[position$1++];
|
|
790
769
|
if ((g & 0x80) > 0) {
|
|
791
770
|
position$1 -= 7;
|
|
792
|
-
return
|
|
771
|
+
return;
|
|
793
772
|
}
|
|
794
|
-
return fromCharCode(a, b, c, d, e, f, g)
|
|
773
|
+
return fromCharCode(a, b, c, d, e, f, g);
|
|
795
774
|
} else {
|
|
796
775
|
let e = src[position$1++];
|
|
797
776
|
let f = src[position$1++];
|
|
@@ -799,34 +778,34 @@ function shortStringInJS(length) {
|
|
|
799
778
|
let h = src[position$1++];
|
|
800
779
|
if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
|
|
801
780
|
position$1 -= 8;
|
|
802
|
-
return
|
|
781
|
+
return;
|
|
803
782
|
}
|
|
804
783
|
if (length < 10) {
|
|
805
784
|
if (length === 8)
|
|
806
|
-
return fromCharCode(a, b, c, d, e, f, g, h)
|
|
785
|
+
return fromCharCode(a, b, c, d, e, f, g, h);
|
|
807
786
|
else {
|
|
808
787
|
let i = src[position$1++];
|
|
809
788
|
if ((i & 0x80) > 0) {
|
|
810
789
|
position$1 -= 9;
|
|
811
|
-
return
|
|
790
|
+
return;
|
|
812
791
|
}
|
|
813
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i)
|
|
792
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
814
793
|
}
|
|
815
794
|
} else if (length < 12) {
|
|
816
795
|
let i = src[position$1++];
|
|
817
796
|
let j = src[position$1++];
|
|
818
797
|
if ((i & 0x80) > 0 || (j & 0x80) > 0) {
|
|
819
798
|
position$1 -= 10;
|
|
820
|
-
return
|
|
799
|
+
return;
|
|
821
800
|
}
|
|
822
801
|
if (length < 11)
|
|
823
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j)
|
|
802
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
824
803
|
let k = src[position$1++];
|
|
825
804
|
if ((k & 0x80) > 0) {
|
|
826
805
|
position$1 -= 11;
|
|
827
|
-
return
|
|
806
|
+
return;
|
|
828
807
|
}
|
|
829
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
|
|
808
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
830
809
|
} else {
|
|
831
810
|
let i = src[position$1++];
|
|
832
811
|
let j = src[position$1++];
|
|
@@ -834,34 +813,34 @@ function shortStringInJS(length) {
|
|
|
834
813
|
let l = src[position$1++];
|
|
835
814
|
if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
|
|
836
815
|
position$1 -= 12;
|
|
837
|
-
return
|
|
816
|
+
return;
|
|
838
817
|
}
|
|
839
818
|
if (length < 14) {
|
|
840
819
|
if (length === 12)
|
|
841
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
|
|
820
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
842
821
|
else {
|
|
843
822
|
let m = src[position$1++];
|
|
844
823
|
if ((m & 0x80) > 0) {
|
|
845
824
|
position$1 -= 13;
|
|
846
|
-
return
|
|
825
|
+
return;
|
|
847
826
|
}
|
|
848
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
|
|
827
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
849
828
|
}
|
|
850
829
|
} else {
|
|
851
830
|
let m = src[position$1++];
|
|
852
831
|
let n = src[position$1++];
|
|
853
832
|
if ((m & 0x80) > 0 || (n & 0x80) > 0) {
|
|
854
833
|
position$1 -= 14;
|
|
855
|
-
return
|
|
834
|
+
return;
|
|
856
835
|
}
|
|
857
836
|
if (length < 15)
|
|
858
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
|
|
837
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
859
838
|
let o = src[position$1++];
|
|
860
839
|
if ((o & 0x80) > 0) {
|
|
861
840
|
position$1 -= 15;
|
|
862
|
-
return
|
|
841
|
+
return;
|
|
863
842
|
}
|
|
864
|
-
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
|
|
843
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
865
844
|
}
|
|
866
845
|
}
|
|
867
846
|
}
|
|
@@ -879,22 +858,22 @@ function readOnlyJSString() {
|
|
|
879
858
|
case 0xd9:
|
|
880
859
|
// str 8
|
|
881
860
|
length = src[position$1++];
|
|
882
|
-
break
|
|
861
|
+
break;
|
|
883
862
|
case 0xda:
|
|
884
863
|
// str 16
|
|
885
864
|
length = dataView.getUint16(position$1);
|
|
886
865
|
position$1 += 2;
|
|
887
|
-
break
|
|
866
|
+
break;
|
|
888
867
|
case 0xdb:
|
|
889
868
|
// str 32
|
|
890
869
|
length = dataView.getUint32(position$1);
|
|
891
870
|
position$1 += 4;
|
|
892
|
-
break
|
|
871
|
+
break;
|
|
893
872
|
default:
|
|
894
|
-
throw new Error('Expected string')
|
|
873
|
+
throw new Error('Expected string');
|
|
895
874
|
}
|
|
896
875
|
}
|
|
897
|
-
return readStringJS(length)
|
|
876
|
+
return readStringJS(length);
|
|
898
877
|
}
|
|
899
878
|
|
|
900
879
|
|
|
@@ -902,7 +881,7 @@ function readBin(length) {
|
|
|
902
881
|
return currentUnpackr.copyBuffers ?
|
|
903
882
|
// specifically use the copying slice (not the node one)
|
|
904
883
|
Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
|
|
905
|
-
src.subarray(position$1, position$1 += length)
|
|
884
|
+
src.subarray(position$1, position$1 += length);
|
|
906
885
|
}
|
|
907
886
|
function readExt(length) {
|
|
908
887
|
let type = src[position$1++];
|
|
@@ -915,10 +894,10 @@ function readExt(length) {
|
|
|
915
894
|
} finally {
|
|
916
895
|
position$1 = end;
|
|
917
896
|
}
|
|
918
|
-
})
|
|
897
|
+
});
|
|
919
898
|
}
|
|
920
899
|
else
|
|
921
|
-
throw new Error('Unknown extension type ' + type)
|
|
900
|
+
throw new Error('Unknown extension type ' + type);
|
|
922
901
|
}
|
|
923
902
|
|
|
924
903
|
var keyCache = new Array(4096);
|
|
@@ -928,12 +907,12 @@ function readKey() {
|
|
|
928
907
|
// fixstr, potentially use key cache
|
|
929
908
|
length = length - 0xa0;
|
|
930
909
|
if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
|
|
931
|
-
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
|
|
910
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart);
|
|
932
911
|
else if (!(srcStringEnd == 0 && srcEnd < 180))
|
|
933
|
-
return readFixedString(length)
|
|
912
|
+
return readFixedString(length);
|
|
934
913
|
} else { // not cacheable, go back and do a standard read
|
|
935
914
|
position$1--;
|
|
936
|
-
return asSafeString(read())
|
|
915
|
+
return asSafeString(read());
|
|
937
916
|
}
|
|
938
917
|
let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
|
|
939
918
|
let entry = keyCache[key];
|
|
@@ -946,7 +925,7 @@ function readKey() {
|
|
|
946
925
|
chunk = dataView.getUint32(checkPosition);
|
|
947
926
|
if (chunk != entry[i++]) {
|
|
948
927
|
checkPosition = 0x70000000;
|
|
949
|
-
break
|
|
928
|
+
break;
|
|
950
929
|
}
|
|
951
930
|
checkPosition += 4;
|
|
952
931
|
}
|
|
@@ -955,12 +934,12 @@ function readKey() {
|
|
|
955
934
|
chunk = src[checkPosition++];
|
|
956
935
|
if (chunk != entry[i++]) {
|
|
957
936
|
checkPosition = 0x70000000;
|
|
958
|
-
break
|
|
937
|
+
break;
|
|
959
938
|
}
|
|
960
939
|
}
|
|
961
940
|
if (checkPosition === end) {
|
|
962
941
|
position$1 = checkPosition;
|
|
963
|
-
return entry.string
|
|
942
|
+
return entry.string;
|
|
964
943
|
}
|
|
965
944
|
end -= 3;
|
|
966
945
|
checkPosition = position$1;
|
|
@@ -981,8 +960,8 @@ function readKey() {
|
|
|
981
960
|
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
982
961
|
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
|
|
983
962
|
if (string != null)
|
|
984
|
-
return entry.string = string
|
|
985
|
-
return entry.string = readFixedString(length)
|
|
963
|
+
return entry.string = string;
|
|
964
|
+
return entry.string = readFixedString(length);
|
|
986
965
|
}
|
|
987
966
|
|
|
988
967
|
function asSafeString(property) {
|
|
@@ -1013,7 +992,7 @@ const recordDefinition = (id, highByte) => {
|
|
|
1013
992
|
}
|
|
1014
993
|
currentStructures[id] = structure;
|
|
1015
994
|
structure.read = createStructureReader(structure, firstByte);
|
|
1016
|
-
return structure.read()
|
|
995
|
+
return structure.read();
|
|
1017
996
|
};
|
|
1018
997
|
currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
|
|
1019
998
|
currentExtensions[0].noBuffer = true;
|
|
@@ -1035,17 +1014,17 @@ currentExtensions[0x42] = data => {
|
|
|
1035
1014
|
out <<= BigInt(64);
|
|
1036
1015
|
out |= view.getBigUint64(i);
|
|
1037
1016
|
}
|
|
1038
|
-
return out
|
|
1017
|
+
return out;
|
|
1039
1018
|
}
|
|
1040
1019
|
// if (length === 8) return view.getBigUint64(start)
|
|
1041
1020
|
let middle = start + (length >> 4 << 3);
|
|
1042
1021
|
let left = decode(start, middle);
|
|
1043
1022
|
let right = decode(middle, end);
|
|
1044
|
-
return (left << BigInt((end - middle) * 8)) | right
|
|
1023
|
+
return (left << BigInt((end - middle) * 8)) | right;
|
|
1045
1024
|
};
|
|
1046
1025
|
head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
|
|
1047
1026
|
}
|
|
1048
|
-
return head
|
|
1027
|
+
return head;
|
|
1049
1028
|
};
|
|
1050
1029
|
|
|
1051
1030
|
let errors = {
|
|
@@ -1056,14 +1035,14 @@ currentExtensions[0x65] = () => {
|
|
|
1056
1035
|
if (!errors[data[0]]) {
|
|
1057
1036
|
let error = Error(data[1], { cause: data[2] });
|
|
1058
1037
|
error.name = data[0];
|
|
1059
|
-
return error
|
|
1038
|
+
return error;
|
|
1060
1039
|
}
|
|
1061
|
-
return errors[data[0]](data[1], { cause: data[2] })
|
|
1040
|
+
return errors[data[0]](data[1], { cause: data[2] });
|
|
1062
1041
|
};
|
|
1063
1042
|
|
|
1064
1043
|
currentExtensions[0x69] = (data) => {
|
|
1065
1044
|
// id extension (for structured clones)
|
|
1066
|
-
if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
|
|
1045
|
+
if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled');
|
|
1067
1046
|
let id = dataView.getUint32(position$1 - 4);
|
|
1068
1047
|
if (!referenceMap)
|
|
1069
1048
|
referenceMap = new Map();
|
|
@@ -1084,7 +1063,7 @@ currentExtensions[0x69] = (data) => {
|
|
|
1084
1063
|
let targetProperties = read(); // read the next value as the target object to id
|
|
1085
1064
|
if (!refEntry.used) {
|
|
1086
1065
|
// no cycle, can just use the returned read object
|
|
1087
|
-
return refEntry.target = targetProperties // replace the placeholder with the real one
|
|
1066
|
+
return refEntry.target = targetProperties; // replace the placeholder with the real one
|
|
1088
1067
|
} else {
|
|
1089
1068
|
// there is a cycle, so we have to assign properties to original target
|
|
1090
1069
|
Object.assign(target, targetProperties);
|
|
@@ -1095,16 +1074,16 @@ currentExtensions[0x69] = (data) => {
|
|
|
1095
1074
|
for (let [k, v] of targetProperties.entries()) target.set(k, v);
|
|
1096
1075
|
if (target instanceof Set)
|
|
1097
1076
|
for (let i of Array.from(targetProperties)) target.add(i);
|
|
1098
|
-
return target
|
|
1077
|
+
return target;
|
|
1099
1078
|
};
|
|
1100
1079
|
|
|
1101
1080
|
currentExtensions[0x70] = (data) => {
|
|
1102
1081
|
// pointer extension (for structured clones)
|
|
1103
|
-
if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
|
|
1082
|
+
if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled');
|
|
1104
1083
|
let id = dataView.getUint32(position$1 - 4);
|
|
1105
1084
|
let refEntry = referenceMap.get(id);
|
|
1106
1085
|
refEntry.used = true;
|
|
1107
|
-
return refEntry.target
|
|
1086
|
+
return refEntry.target;
|
|
1108
1087
|
};
|
|
1109
1088
|
|
|
1110
1089
|
currentExtensions[0x73] = () => new Set(read());
|
|
@@ -1119,15 +1098,15 @@ currentExtensions[0x74] = (data) => {
|
|
|
1119
1098
|
|
|
1120
1099
|
let typedArrayName = typedArrays[typeCode];
|
|
1121
1100
|
if (!typedArrayName) {
|
|
1122
|
-
if (typeCode === 16) return buffer
|
|
1123
|
-
if (typeCode === 17) return new DataView(buffer)
|
|
1124
|
-
throw new Error('Could not find typed array for code ' + typeCode)
|
|
1101
|
+
if (typeCode === 16) return buffer;
|
|
1102
|
+
if (typeCode === 17) return new DataView(buffer);
|
|
1103
|
+
throw new Error('Could not find typed array for code ' + typeCode);
|
|
1125
1104
|
}
|
|
1126
|
-
return new glbl[typedArrayName](buffer)
|
|
1105
|
+
return new glbl[typedArrayName](buffer);
|
|
1127
1106
|
};
|
|
1128
1107
|
currentExtensions[0x78] = () => {
|
|
1129
1108
|
let data = read();
|
|
1130
|
-
return new RegExp(data[0], data[1])
|
|
1109
|
+
return new RegExp(data[0], data[1]);
|
|
1131
1110
|
};
|
|
1132
1111
|
const TEMP_BUNDLE = [];
|
|
1133
1112
|
currentExtensions[0x62] = (data) => {
|
|
@@ -1140,30 +1119,28 @@ currentExtensions[0x62] = (data) => {
|
|
|
1140
1119
|
bundledStrings$1.position1 = 0;
|
|
1141
1120
|
bundledStrings$1.postBundlePosition = position$1;
|
|
1142
1121
|
position$1 = dataPosition;
|
|
1143
|
-
return read()
|
|
1122
|
+
return read();
|
|
1144
1123
|
};
|
|
1145
1124
|
|
|
1146
1125
|
currentExtensions[0xff] = (data) => {
|
|
1147
1126
|
// 32-bit date extension
|
|
1148
1127
|
if (data.length == 4)
|
|
1149
|
-
return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
|
|
1128
|
+
return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000);
|
|
1150
1129
|
else if (data.length == 8)
|
|
1151
1130
|
return new Date(
|
|
1152
1131
|
((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
|
|
1153
|
-
((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
|
|
1132
|
+
((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000);
|
|
1154
1133
|
else if (data.length == 12)
|
|
1155
1134
|
return new Date(
|
|
1156
1135
|
((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
|
|
1157
|
-
(((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
|
|
1136
|
+
(((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000);
|
|
1158
1137
|
else
|
|
1159
|
-
return new Date('invalid')
|
|
1138
|
+
return new Date('invalid');
|
|
1160
1139
|
};
|
|
1161
1140
|
// registration of bulk record definition?
|
|
1162
1141
|
// currentExtensions[0x52] = () =>
|
|
1163
1142
|
|
|
1164
|
-
function saveState
|
|
1165
|
-
if (onSaveState)
|
|
1166
|
-
onSaveState();
|
|
1143
|
+
function saveState(callback) {
|
|
1167
1144
|
let savedSrcEnd = srcEnd;
|
|
1168
1145
|
let savedPosition = position$1;
|
|
1169
1146
|
let savedStringPosition = stringPosition;
|
|
@@ -1196,7 +1173,7 @@ function saveState$1(callback) {
|
|
|
1196
1173
|
currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
|
|
1197
1174
|
currentUnpackr = savedPackr;
|
|
1198
1175
|
dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
|
|
1199
|
-
return value
|
|
1176
|
+
return value;
|
|
1200
1177
|
}
|
|
1201
1178
|
function clearSource() {
|
|
1202
1179
|
src = null;
|
|
@@ -1231,30 +1208,24 @@ let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
|
1231
1208
|
function roundFloat32(float32Number) {
|
|
1232
1209
|
f32Array[0] = float32Number;
|
|
1233
1210
|
let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
|
|
1234
|
-
return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
1235
|
-
}
|
|
1236
|
-
function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
|
|
1237
|
-
readStruct$1 = updatedReadStruct;
|
|
1238
|
-
onLoadedStructures$1 = loadedStructs;
|
|
1239
|
-
onSaveState = saveState;
|
|
1211
|
+
return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
|
|
1240
1212
|
}
|
|
1241
1213
|
|
|
1242
|
-
let textEncoder
|
|
1214
|
+
let textEncoder;
|
|
1243
1215
|
try {
|
|
1244
|
-
textEncoder
|
|
1216
|
+
textEncoder = new TextEncoder();
|
|
1245
1217
|
} catch (error) {}
|
|
1246
1218
|
let extensions, extensionClasses;
|
|
1247
|
-
const hasNodeBuffer
|
|
1248
|
-
const ByteArrayAllocate = hasNodeBuffer
|
|
1249
|
-
function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
|
|
1250
|
-
const ByteArray = hasNodeBuffer
|
|
1251
|
-
const MAX_BUFFER_SIZE = hasNodeBuffer
|
|
1219
|
+
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
1220
|
+
const ByteArrayAllocate = hasNodeBuffer ?
|
|
1221
|
+
function(length) { return Buffer.allocUnsafeSlow(length); } : Uint8Array;
|
|
1222
|
+
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
1223
|
+
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1252
1224
|
let target, keysTarget;
|
|
1253
1225
|
let targetView;
|
|
1254
1226
|
let position = 0;
|
|
1255
1227
|
let safeEnd;
|
|
1256
1228
|
let bundledStrings = null;
|
|
1257
|
-
let writeStructSlots;
|
|
1258
1229
|
const MAX_BUNDLE_SIZE = 0x5500; // maximum characters such that the encoded bytes fits in 16 bits.
|
|
1259
1230
|
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1260
1231
|
const RECORD_SYMBOL = Symbol('record-id');
|
|
@@ -1267,10 +1238,10 @@ class Packr extends Unpackr {
|
|
|
1267
1238
|
let structures;
|
|
1268
1239
|
let referenceMap;
|
|
1269
1240
|
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
|
|
1270
|
-
return target.utf8Write(string, position, target.byteLength - position)
|
|
1271
|
-
} : (textEncoder
|
|
1241
|
+
return target.utf8Write(string, position, target.byteLength - position);
|
|
1242
|
+
} : (textEncoder && textEncoder.encodeInto) ?
|
|
1272
1243
|
function(string, position) {
|
|
1273
|
-
return textEncoder
|
|
1244
|
+
return textEncoder.encodeInto(string, target.subarray(position)).written;
|
|
1274
1245
|
} : false;
|
|
1275
1246
|
|
|
1276
1247
|
let packr = this;
|
|
@@ -1282,7 +1253,7 @@ class Packr extends Unpackr {
|
|
|
1282
1253
|
if (maxSharedStructures == null)
|
|
1283
1254
|
maxSharedStructures = hasSharedStructures ? 32 : 0;
|
|
1284
1255
|
if (maxSharedStructures > 8160)
|
|
1285
|
-
throw new Error('Maximum maxSharedStructure is 8160')
|
|
1256
|
+
throw new Error('Maximum maxSharedStructure is 8160');
|
|
1286
1257
|
if (options.structuredClone && options.moreTypes == undefined) {
|
|
1287
1258
|
this.moreTypes = true;
|
|
1288
1259
|
}
|
|
@@ -1296,7 +1267,7 @@ class Packr extends Unpackr {
|
|
|
1296
1267
|
let sharedLimitId = maxSharedStructures + 0x40;
|
|
1297
1268
|
let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
|
|
1298
1269
|
if (maxStructureId > 8256) {
|
|
1299
|
-
throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
|
|
1270
|
+
throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192');
|
|
1300
1271
|
}
|
|
1301
1272
|
let recordIdsToRemove = [];
|
|
1302
1273
|
let transitionsCount = 0;
|
|
@@ -1332,7 +1303,7 @@ class Packr extends Unpackr {
|
|
|
1332
1303
|
let sharedLength = structures.sharedLength || 0;
|
|
1333
1304
|
if (sharedLength > maxSharedStructures) {
|
|
1334
1305
|
//if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
|
|
1335
|
-
throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
|
|
1306
|
+
throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength);
|
|
1336
1307
|
}
|
|
1337
1308
|
if (!structures.transitions) {
|
|
1338
1309
|
// rebuild our structure transitions
|
|
@@ -1340,7 +1311,7 @@ class Packr extends Unpackr {
|
|
|
1340
1311
|
for (let i = 0; i < sharedLength; i++) {
|
|
1341
1312
|
let keys = structures[i];
|
|
1342
1313
|
if (!keys)
|
|
1343
|
-
continue
|
|
1314
|
+
continue;
|
|
1344
1315
|
let nextTransition, transition = structures.transitions;
|
|
1345
1316
|
for (let j = 0, l = keys.length; j < l; j++) {
|
|
1346
1317
|
let key = keys[j];
|
|
@@ -1362,14 +1333,7 @@ class Packr extends Unpackr {
|
|
|
1362
1333
|
hasSharedUpdate = false;
|
|
1363
1334
|
let encodingError;
|
|
1364
1335
|
try {
|
|
1365
|
-
|
|
1366
|
-
if (value.constructor === Object) writeStruct(value); // simple object
|
|
1367
|
-
else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
|
|
1368
|
-
// allow user classes, if they don't need special handling (but do use toJSON if available)
|
|
1369
|
-
writeStruct(value.toJSON ? value.toJSON() : value);
|
|
1370
|
-
} else pack(value);
|
|
1371
|
-
} else
|
|
1372
|
-
pack(value);
|
|
1336
|
+
pack(value);
|
|
1373
1337
|
let lastBundle = bundledStrings;
|
|
1374
1338
|
if (bundledStrings)
|
|
1375
1339
|
writeBundles(start, pack, 0);
|
|
@@ -1406,15 +1370,15 @@ class Packr extends Unpackr {
|
|
|
1406
1370
|
packr.offset = position;
|
|
1407
1371
|
let serialized = insertIds(target.subarray(start, position), idsToInsert);
|
|
1408
1372
|
referenceMap = null;
|
|
1409
|
-
return serialized
|
|
1373
|
+
return serialized;
|
|
1410
1374
|
}
|
|
1411
1375
|
packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1412
1376
|
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1413
1377
|
target.start = start;
|
|
1414
1378
|
target.end = position;
|
|
1415
|
-
return target
|
|
1379
|
+
return target;
|
|
1416
1380
|
}
|
|
1417
|
-
return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
|
|
1381
|
+
return target.subarray(start, position); // position can change if we call pack again in saveStructures, so we get the buffer now
|
|
1418
1382
|
} catch(error) {
|
|
1419
1383
|
encodingError = error;
|
|
1420
1384
|
throw error;
|
|
@@ -1425,16 +1389,16 @@ class Packr extends Unpackr {
|
|
|
1425
1389
|
let sharedLength = structures.sharedLength || 0;
|
|
1426
1390
|
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1427
1391
|
let returnBuffer = target.subarray(start, position);
|
|
1428
|
-
let newSharedData = prepareStructures
|
|
1392
|
+
let newSharedData = prepareStructures(structures, packr);
|
|
1429
1393
|
if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
|
|
1430
1394
|
if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
|
|
1431
1395
|
// get updated structures and try again if the update failed
|
|
1432
|
-
return packr.pack(value, encodeOptions)
|
|
1396
|
+
return packr.pack(value, encodeOptions);
|
|
1433
1397
|
}
|
|
1434
1398
|
packr.lastNamedStructuresLength = sharedLength;
|
|
1435
1399
|
// don't keep large buffers around
|
|
1436
1400
|
if (target.length > 0x40000000) target = null;
|
|
1437
|
-
return returnBuffer
|
|
1401
|
+
return returnBuffer;
|
|
1438
1402
|
}
|
|
1439
1403
|
}
|
|
1440
1404
|
}
|
|
@@ -1520,7 +1484,7 @@ class Packr extends Unpackr {
|
|
|
1520
1484
|
bundledStrings[twoByte ? 0 : 1] += value;
|
|
1521
1485
|
target[position++] = 0xc1;
|
|
1522
1486
|
pack(twoByte ? -strLength : strLength);
|
|
1523
|
-
return
|
|
1487
|
+
return;
|
|
1524
1488
|
}
|
|
1525
1489
|
let headerSize;
|
|
1526
1490
|
// first we estimate the header size, so we can write to the correct location
|
|
@@ -1594,7 +1558,7 @@ class Packr extends Unpackr {
|
|
|
1594
1558
|
} else if (type === 'number') {
|
|
1595
1559
|
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1596
1560
|
// positive uint
|
|
1597
|
-
if (value <
|
|
1561
|
+
if (value < 0x40 || (value < 0x80 && this.useRecords === false)) {
|
|
1598
1562
|
target[position++] = value;
|
|
1599
1563
|
} else if (value < 0x100) {
|
|
1600
1564
|
target[position++] = 0xcc;
|
|
@@ -1633,7 +1597,7 @@ class Packr extends Unpackr {
|
|
|
1633
1597
|
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1634
1598
|
((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
|
|
1635
1599
|
position += 4;
|
|
1636
|
-
return
|
|
1600
|
+
return;
|
|
1637
1601
|
} else
|
|
1638
1602
|
position--; // move back into position for writing a double
|
|
1639
1603
|
}
|
|
@@ -1656,7 +1620,7 @@ class Packr extends Unpackr {
|
|
|
1656
1620
|
target[position++] = 0x70; // "p" for pointer
|
|
1657
1621
|
targetView.setUint32(position, referee.id);
|
|
1658
1622
|
position += 4;
|
|
1659
|
-
return
|
|
1623
|
+
return;
|
|
1660
1624
|
} else
|
|
1661
1625
|
referenceMap.set(value, { offset: position - start });
|
|
1662
1626
|
}
|
|
@@ -1706,7 +1670,7 @@ class Packr extends Unpackr {
|
|
|
1706
1670
|
} else {
|
|
1707
1671
|
pack(writeResult);
|
|
1708
1672
|
}
|
|
1709
|
-
return
|
|
1673
|
+
return;
|
|
1710
1674
|
}
|
|
1711
1675
|
let currentTarget = target;
|
|
1712
1676
|
let currentTargetView = targetView;
|
|
@@ -1723,7 +1687,7 @@ class Packr extends Unpackr {
|
|
|
1723
1687
|
makeRoom(position);
|
|
1724
1688
|
return {
|
|
1725
1689
|
target, targetView, position: position - size
|
|
1726
|
-
}
|
|
1690
|
+
};
|
|
1727
1691
|
}, pack);
|
|
1728
1692
|
} finally {
|
|
1729
1693
|
// restore current target information (unless already restored)
|
|
@@ -1739,7 +1703,7 @@ class Packr extends Unpackr {
|
|
|
1739
1703
|
makeRoom(result.length + position);
|
|
1740
1704
|
position = writeExtensionData(result, target, position, extension.type);
|
|
1741
1705
|
}
|
|
1742
|
-
return
|
|
1706
|
+
return;
|
|
1743
1707
|
}
|
|
1744
1708
|
}
|
|
1745
1709
|
// check isArray after extensions, because extensions can extend Array
|
|
@@ -1751,7 +1715,7 @@ class Packr extends Unpackr {
|
|
|
1751
1715
|
const json = value.toJSON();
|
|
1752
1716
|
// if for some reason value.toJSON returns itself it'll loop forever
|
|
1753
1717
|
if (json !== value)
|
|
1754
|
-
return pack(json)
|
|
1718
|
+
return pack(json);
|
|
1755
1719
|
}
|
|
1756
1720
|
|
|
1757
1721
|
// if there is a writeFunction, use it, otherwise just encode as undefined
|
|
@@ -1790,7 +1754,7 @@ class Packr extends Unpackr {
|
|
|
1790
1754
|
let chunks = [];
|
|
1791
1755
|
while (true) {
|
|
1792
1756
|
chunks.push(value & mask);
|
|
1793
|
-
if ((value >> BigInt(63)) === empty) break
|
|
1757
|
+
if ((value >> BigInt(63)) === empty) break;
|
|
1794
1758
|
value >>= BigInt(64);
|
|
1795
1759
|
}
|
|
1796
1760
|
|
|
@@ -1805,7 +1769,7 @@ class Packr extends Unpackr {
|
|
|
1805
1769
|
string = '00' + string;
|
|
1806
1770
|
}
|
|
1807
1771
|
|
|
1808
|
-
if (hasNodeBuffer
|
|
1772
|
+
if (hasNodeBuffer) {
|
|
1809
1773
|
array = Buffer.from(string, 'hex');
|
|
1810
1774
|
} else {
|
|
1811
1775
|
array = new Uint8Array(string.length / 2);
|
|
@@ -1822,11 +1786,11 @@ class Packr extends Unpackr {
|
|
|
1822
1786
|
if (array.length + position > safeEnd)
|
|
1823
1787
|
makeRoom(array.length + position);
|
|
1824
1788
|
position = writeExtensionData(array, target, position, 0x42);
|
|
1825
|
-
return
|
|
1789
|
+
return;
|
|
1826
1790
|
} else {
|
|
1827
1791
|
throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
|
|
1828
1792
|
' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
|
|
1829
|
-
' largeBigIntToString to convert to string')
|
|
1793
|
+
' largeBigIntToString to convert to string');
|
|
1830
1794
|
}
|
|
1831
1795
|
}
|
|
1832
1796
|
position += 8;
|
|
@@ -1839,7 +1803,7 @@ class Packr extends Unpackr {
|
|
|
1839
1803
|
target[position++] = 0;
|
|
1840
1804
|
}
|
|
1841
1805
|
} else {
|
|
1842
|
-
throw new Error('Unknown type: ' + type)
|
|
1806
|
+
throw new Error('Unknown type: ' + type);
|
|
1843
1807
|
}
|
|
1844
1808
|
};
|
|
1845
1809
|
|
|
@@ -1990,7 +1954,7 @@ class Packr extends Unpackr {
|
|
|
1990
1954
|
if (end > 0x1000000) {
|
|
1991
1955
|
// special handling for really large buffers
|
|
1992
1956
|
if ((end - start) > MAX_BUFFER_SIZE)
|
|
1993
|
-
throw new Error('Packed buffer would be larger than maximum buffer size')
|
|
1957
|
+
throw new Error('Packed buffer would be larger than maximum buffer size');
|
|
1994
1958
|
newSize = Math.min(MAX_BUFFER_SIZE,
|
|
1995
1959
|
Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
|
|
1996
1960
|
} else // faster handling for smaller buffers
|
|
@@ -2005,7 +1969,7 @@ class Packr extends Unpackr {
|
|
|
2005
1969
|
position -= start;
|
|
2006
1970
|
start = 0;
|
|
2007
1971
|
safeEnd = newBuffer.length - 10;
|
|
2008
|
-
return target = newBuffer
|
|
1972
|
+
return target = newBuffer;
|
|
2009
1973
|
};
|
|
2010
1974
|
const newRecord = (transition, keys, newTransitions) => {
|
|
2011
1975
|
let recordId = structures.nextId;
|
|
@@ -2087,23 +2051,6 @@ class Packr extends Unpackr {
|
|
|
2087
2051
|
target[insertionOffset + start] = keysTarget[0];
|
|
2088
2052
|
}
|
|
2089
2053
|
};
|
|
2090
|
-
const writeStruct = (object) => {
|
|
2091
|
-
let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
|
|
2092
|
-
if (notifySharedUpdate)
|
|
2093
|
-
return hasSharedUpdate = true;
|
|
2094
|
-
position = newPosition;
|
|
2095
|
-
let startTarget = target;
|
|
2096
|
-
pack(value);
|
|
2097
|
-
resetStructures();
|
|
2098
|
-
if (startTarget !== target) {
|
|
2099
|
-
return { position, targetView, target }; // indicate the buffer was re-allocated
|
|
2100
|
-
}
|
|
2101
|
-
return position;
|
|
2102
|
-
}, this);
|
|
2103
|
-
if (newPosition === 0) // bail and go to a msgpack object
|
|
2104
|
-
return writeObject(object);
|
|
2105
|
-
position = newPosition;
|
|
2106
|
-
};
|
|
2107
2054
|
}
|
|
2108
2055
|
useBuffer(buffer) {
|
|
2109
2056
|
// this means we are finished using our own buffer and we can write over it safely
|
|
@@ -2121,8 +2068,6 @@ class Packr extends Unpackr {
|
|
|
2121
2068
|
clearSharedData() {
|
|
2122
2069
|
if (this.structures)
|
|
2123
2070
|
this.structures = [];
|
|
2124
|
-
if (this.typedStructs)
|
|
2125
|
-
this.typedStructs = [];
|
|
2126
2071
|
}
|
|
2127
2072
|
}
|
|
2128
2073
|
|
|
@@ -2146,7 +2091,7 @@ extensions = [{
|
|
|
2146
2091
|
} else if (isNaN(seconds)) {
|
|
2147
2092
|
if (this.onInvalidDate) {
|
|
2148
2093
|
allocateForWrite(0);
|
|
2149
|
-
return pack(this.onInvalidDate())
|
|
2094
|
+
return pack(this.onInvalidDate());
|
|
2150
2095
|
}
|
|
2151
2096
|
// Intentionally invalid timestamp
|
|
2152
2097
|
let { target, targetView, position} = allocateForWrite(3);
|
|
@@ -2167,7 +2112,7 @@ extensions = [{
|
|
|
2167
2112
|
pack(set, allocateForWrite, pack) {
|
|
2168
2113
|
if (this.setAsEmptyObject) {
|
|
2169
2114
|
allocateForWrite(0);
|
|
2170
|
-
return pack({})
|
|
2115
|
+
return pack({});
|
|
2171
2116
|
}
|
|
2172
2117
|
let array = Array.from(set);
|
|
2173
2118
|
let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
@@ -2203,7 +2148,7 @@ extensions = [{
|
|
|
2203
2148
|
if (this.moreTypes)
|
|
2204
2149
|
writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
|
|
2205
2150
|
else
|
|
2206
|
-
writeBuffer(hasNodeBuffer
|
|
2151
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
2207
2152
|
}
|
|
2208
2153
|
}, {
|
|
2209
2154
|
pack(typedArray, allocateForWrite) {
|
|
@@ -2218,7 +2163,7 @@ extensions = [{
|
|
|
2218
2163
|
if (this.moreTypes)
|
|
2219
2164
|
writeExtBuffer(arrayBuffer, 0x11, allocateForWrite);
|
|
2220
2165
|
else
|
|
2221
|
-
writeBuffer(hasNodeBuffer
|
|
2166
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
2222
2167
|
}
|
|
2223
2168
|
}, {
|
|
2224
2169
|
pack(c1, allocateForWrite) { // specific 0xC1 object
|
|
@@ -2275,19 +2220,19 @@ function writeExtensionData(result, target, position, type) {
|
|
|
2275
2220
|
switch (length) {
|
|
2276
2221
|
case 1:
|
|
2277
2222
|
target[position++] = 0xd4;
|
|
2278
|
-
break
|
|
2223
|
+
break;
|
|
2279
2224
|
case 2:
|
|
2280
2225
|
target[position++] = 0xd5;
|
|
2281
|
-
break
|
|
2226
|
+
break;
|
|
2282
2227
|
case 4:
|
|
2283
2228
|
target[position++] = 0xd6;
|
|
2284
|
-
break
|
|
2229
|
+
break;
|
|
2285
2230
|
case 8:
|
|
2286
2231
|
target[position++] = 0xd7;
|
|
2287
|
-
break
|
|
2232
|
+
break;
|
|
2288
2233
|
case 16:
|
|
2289
2234
|
target[position++] = 0xd8;
|
|
2290
|
-
break
|
|
2235
|
+
break;
|
|
2291
2236
|
default:
|
|
2292
2237
|
if (length < 0x100) {
|
|
2293
2238
|
target[position++] = 0xc7;
|
|
@@ -2307,7 +2252,7 @@ function writeExtensionData(result, target, position, type) {
|
|
|
2307
2252
|
target[position++] = type;
|
|
2308
2253
|
target.set(result, position);
|
|
2309
2254
|
position += length;
|
|
2310
|
-
return position
|
|
2255
|
+
return position;
|
|
2311
2256
|
}
|
|
2312
2257
|
|
|
2313
2258
|
function insertIds(serialized, idsToInsert) {
|
|
@@ -2329,7 +2274,7 @@ function insertIds(serialized, idsToInsert) {
|
|
|
2329
2274
|
serialized[position++] = id & 0xff;
|
|
2330
2275
|
lastEnd = offset;
|
|
2331
2276
|
}
|
|
2332
|
-
return serialized
|
|
2277
|
+
return serialized;
|
|
2333
2278
|
}
|
|
2334
2279
|
|
|
2335
2280
|
function writeBundles(start, pack, incrementPosition) {
|
|
@@ -2346,26 +2291,22 @@ function writeBundles(start, pack, incrementPosition) {
|
|
|
2346
2291
|
function addExtension(extension) {
|
|
2347
2292
|
if (extension.Class) {
|
|
2348
2293
|
if (!extension.pack && !extension.write)
|
|
2349
|
-
throw new Error('Extension has no pack or write function')
|
|
2294
|
+
throw new Error('Extension has no pack or write function');
|
|
2350
2295
|
if (extension.pack && !extension.type)
|
|
2351
|
-
throw new Error('Extension has no type (numeric code to identify the extension)')
|
|
2296
|
+
throw new Error('Extension has no type (numeric code to identify the extension)');
|
|
2352
2297
|
extensionClasses.unshift(extension.Class);
|
|
2353
2298
|
extensions.unshift(extension);
|
|
2354
2299
|
}
|
|
2355
2300
|
addExtension$1(extension);
|
|
2356
2301
|
}
|
|
2357
|
-
function prepareStructures
|
|
2302
|
+
function prepareStructures(structures, packr) {
|
|
2358
2303
|
structures.isCompatible = (existingStructures) => {
|
|
2359
2304
|
let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
|
|
2360
2305
|
if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
|
|
2361
2306
|
packr._mergeStructures(existingStructures);
|
|
2362
2307
|
return compatible;
|
|
2363
2308
|
};
|
|
2364
|
-
return structures
|
|
2365
|
-
}
|
|
2366
|
-
function setWriteStructSlots(writeSlots, makeStructures) {
|
|
2367
|
-
writeStructSlots = writeSlots;
|
|
2368
|
-
prepareStructures$1 = makeStructures;
|
|
2309
|
+
return structures;
|
|
2369
2310
|
}
|
|
2370
2311
|
|
|
2371
2312
|
let defaultPackr = new Packr({ useRecords: false });
|
|
@@ -2377,779 +2318,6 @@ const REUSE_BUFFER_MODE = 512;
|
|
|
2377
2318
|
const RESET_BUFFER_MODE = 1024;
|
|
2378
2319
|
const RESERVE_START_SPACE = 2048;
|
|
2379
2320
|
|
|
2380
|
-
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)
|
|
2381
|
-
const NUMBER = 0;
|
|
2382
|
-
const UTF8 = 2;
|
|
2383
|
-
const OBJECT_DATA = 1;
|
|
2384
|
-
const DATE = 16;
|
|
2385
|
-
const TYPE_NAMES = ['num', 'object', 'string', 'ascii'];
|
|
2386
|
-
TYPE_NAMES[DATE] = 'date';
|
|
2387
|
-
const float32Headers = [false, true, true, false, false, true, true, false];
|
|
2388
|
-
let evalSupported;
|
|
2389
|
-
try {
|
|
2390
|
-
new Function('');
|
|
2391
|
-
evalSupported = true;
|
|
2392
|
-
} catch(error) {
|
|
2393
|
-
// if eval variants are not supported, do not create inline object readers ever
|
|
2394
|
-
}
|
|
2395
|
-
|
|
2396
|
-
let updatedPosition;
|
|
2397
|
-
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
2398
|
-
let textEncoder, currentSource;
|
|
2399
|
-
try {
|
|
2400
|
-
textEncoder = new TextEncoder();
|
|
2401
|
-
} catch (error) {}
|
|
2402
|
-
const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
|
|
2403
|
-
return target.utf8Write(string, position, target.byteLength - position)
|
|
2404
|
-
} : (textEncoder && textEncoder.encodeInto) ?
|
|
2405
|
-
function(target, string, position) {
|
|
2406
|
-
return textEncoder.encodeInto(string, target.subarray(position)).written
|
|
2407
|
-
} : false;
|
|
2408
|
-
setWriteStructSlots(writeStruct, prepareStructures);
|
|
2409
|
-
function writeStruct(object, target, encodingStart, position, structures, makeRoom, pack, packr) {
|
|
2410
|
-
let typedStructs = packr.typedStructs || (packr.typedStructs = []);
|
|
2411
|
-
// note that we rely on pack.js to load stored structures before we get to this point
|
|
2412
|
-
let targetView = target.dataView;
|
|
2413
|
-
let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
|
|
2414
|
-
let safeEnd = target.length - 10;
|
|
2415
|
-
let start = position;
|
|
2416
|
-
if (position > safeEnd) {
|
|
2417
|
-
target = makeRoom(position);
|
|
2418
|
-
targetView = target.dataView;
|
|
2419
|
-
position -= encodingStart;
|
|
2420
|
-
start -= encodingStart;
|
|
2421
|
-
refsStartPosition -= encodingStart;
|
|
2422
|
-
encodingStart = 0;
|
|
2423
|
-
safeEnd = target.length - 10;
|
|
2424
|
-
}
|
|
2425
|
-
|
|
2426
|
-
let refOffset, refPosition = refsStartPosition;
|
|
2427
|
-
|
|
2428
|
-
let transition = typedStructs.transitions || (typedStructs.transitions = Object.create(null));
|
|
2429
|
-
let nextId = typedStructs.nextId || typedStructs.length;
|
|
2430
|
-
let headerSize =
|
|
2431
|
-
nextId < 0xf ? 1 :
|
|
2432
|
-
nextId < 0xf0 ? 2 :
|
|
2433
|
-
nextId < 0xf000 ? 3 :
|
|
2434
|
-
nextId < 0xf00000 ? 4 : 0;
|
|
2435
|
-
if (headerSize === 0)
|
|
2436
|
-
return 0;
|
|
2437
|
-
position += headerSize;
|
|
2438
|
-
let queuedReferences = [];
|
|
2439
|
-
let usedAscii0;
|
|
2440
|
-
let keyIndex = 0;
|
|
2441
|
-
for (let key in object) {
|
|
2442
|
-
let value = object[key];
|
|
2443
|
-
let nextTransition = transition[key];
|
|
2444
|
-
if (!nextTransition) {
|
|
2445
|
-
transition[key] = nextTransition = {
|
|
2446
|
-
key,
|
|
2447
|
-
parent: transition,
|
|
2448
|
-
enumerationOffset: 0,
|
|
2449
|
-
ascii0: null,
|
|
2450
|
-
ascii8: null,
|
|
2451
|
-
num8: null,
|
|
2452
|
-
string16: null,
|
|
2453
|
-
object16: null,
|
|
2454
|
-
num32: null,
|
|
2455
|
-
float64: null,
|
|
2456
|
-
date64: null
|
|
2457
|
-
};
|
|
2458
|
-
}
|
|
2459
|
-
if (position > safeEnd) {
|
|
2460
|
-
target = makeRoom(position);
|
|
2461
|
-
targetView = target.dataView;
|
|
2462
|
-
position -= encodingStart;
|
|
2463
|
-
start -= encodingStart;
|
|
2464
|
-
refsStartPosition -= encodingStart;
|
|
2465
|
-
refPosition -= encodingStart;
|
|
2466
|
-
encodingStart = 0;
|
|
2467
|
-
safeEnd = target.length - 10;
|
|
2468
|
-
}
|
|
2469
|
-
switch (typeof value) {
|
|
2470
|
-
case 'number':
|
|
2471
|
-
let number = value;
|
|
2472
|
-
// first check to see if we are using a lot of ids and should default to wide/common format
|
|
2473
|
-
if (nextId < 200 || !nextTransition.num64) {
|
|
2474
|
-
if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
|
|
2475
|
-
if (number < 0xf6 && number >= 0 && (nextTransition.num8 && !(nextId > 200 && nextTransition.num32) || number < 0x20 && !nextTransition.num32)) {
|
|
2476
|
-
transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
|
|
2477
|
-
target[position++] = number;
|
|
2478
|
-
} else {
|
|
2479
|
-
transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
|
|
2480
|
-
targetView.setUint32(position, number, true);
|
|
2481
|
-
position += 4;
|
|
2482
|
-
}
|
|
2483
|
-
break;
|
|
2484
|
-
} else if (number < 0x100000000 && number >= -0x80000000) {
|
|
2485
|
-
targetView.setFloat32(position, number, true);
|
|
2486
|
-
if (float32Headers[target[position + 3] >>> 5]) {
|
|
2487
|
-
let xShifted;
|
|
2488
|
-
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
2489
|
-
if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
|
|
2490
|
-
transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
|
|
2491
|
-
position += 4;
|
|
2492
|
-
break;
|
|
2493
|
-
}
|
|
2494
|
-
}
|
|
2495
|
-
}
|
|
2496
|
-
}
|
|
2497
|
-
transition = nextTransition.num64 || createTypeTransition(nextTransition, NUMBER, 8);
|
|
2498
|
-
targetView.setFloat64(position, number, true);
|
|
2499
|
-
position += 8;
|
|
2500
|
-
break;
|
|
2501
|
-
case 'string':
|
|
2502
|
-
let strLength = value.length;
|
|
2503
|
-
refOffset = refPosition - refsStartPosition;
|
|
2504
|
-
if ((strLength << 2) + refPosition > safeEnd) {
|
|
2505
|
-
target = makeRoom((strLength << 2) + refPosition);
|
|
2506
|
-
targetView = target.dataView;
|
|
2507
|
-
position -= encodingStart;
|
|
2508
|
-
start -= encodingStart;
|
|
2509
|
-
refsStartPosition -= encodingStart;
|
|
2510
|
-
refPosition -= encodingStart;
|
|
2511
|
-
encodingStart = 0;
|
|
2512
|
-
safeEnd = target.length - 10;
|
|
2513
|
-
}
|
|
2514
|
-
if (strLength > ((0xff00 + refOffset) >> 2)) {
|
|
2515
|
-
queuedReferences.push(key, value, position - start);
|
|
2516
|
-
break;
|
|
2517
|
-
}
|
|
2518
|
-
let isNotAscii;
|
|
2519
|
-
let strStart = refPosition;
|
|
2520
|
-
if (strLength < 0x40) {
|
|
2521
|
-
let i, c1, c2;
|
|
2522
|
-
for (i = 0; i < strLength; i++) {
|
|
2523
|
-
c1 = value.charCodeAt(i);
|
|
2524
|
-
if (c1 < 0x80) {
|
|
2525
|
-
target[refPosition++] = c1;
|
|
2526
|
-
} else if (c1 < 0x800) {
|
|
2527
|
-
isNotAscii = true;
|
|
2528
|
-
target[refPosition++] = c1 >> 6 | 0xc0;
|
|
2529
|
-
target[refPosition++] = c1 & 0x3f | 0x80;
|
|
2530
|
-
} else if (
|
|
2531
|
-
(c1 & 0xfc00) === 0xd800 &&
|
|
2532
|
-
((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
|
2533
|
-
) {
|
|
2534
|
-
isNotAscii = true;
|
|
2535
|
-
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
2536
|
-
i++;
|
|
2537
|
-
target[refPosition++] = c1 >> 18 | 0xf0;
|
|
2538
|
-
target[refPosition++] = c1 >> 12 & 0x3f | 0x80;
|
|
2539
|
-
target[refPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
2540
|
-
target[refPosition++] = c1 & 0x3f | 0x80;
|
|
2541
|
-
} else {
|
|
2542
|
-
isNotAscii = true;
|
|
2543
|
-
target[refPosition++] = c1 >> 12 | 0xe0;
|
|
2544
|
-
target[refPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
2545
|
-
target[refPosition++] = c1 & 0x3f | 0x80;
|
|
2546
|
-
}
|
|
2547
|
-
}
|
|
2548
|
-
} else {
|
|
2549
|
-
refPosition += encodeUtf8(target, value, refPosition);
|
|
2550
|
-
isNotAscii = refPosition - strStart > strLength;
|
|
2551
|
-
}
|
|
2552
|
-
if (refOffset < 0xa0 || (refOffset < 0xf6 && (nextTransition.ascii8 || nextTransition.string8))) {
|
|
2553
|
-
// short strings
|
|
2554
|
-
if (isNotAscii) {
|
|
2555
|
-
if (!(transition = nextTransition.string8)) {
|
|
2556
|
-
if (typedStructs.length > 10 && (transition = nextTransition.ascii8)) {
|
|
2557
|
-
// we can safely change ascii to utf8 in place since they are compatible
|
|
2558
|
-
transition.__type = UTF8;
|
|
2559
|
-
nextTransition.ascii8 = null;
|
|
2560
|
-
nextTransition.string8 = transition;
|
|
2561
|
-
pack(null, 0, true); // special call to notify that structures have been updated
|
|
2562
|
-
} else {
|
|
2563
|
-
transition = createTypeTransition(nextTransition, UTF8, 1);
|
|
2564
|
-
}
|
|
2565
|
-
}
|
|
2566
|
-
} else if (refOffset === 0 && !usedAscii0) {
|
|
2567
|
-
usedAscii0 = true;
|
|
2568
|
-
transition = nextTransition.ascii0 || createTypeTransition(nextTransition, ASCII, 0);
|
|
2569
|
-
break; // don't increment position
|
|
2570
|
-
}// else ascii:
|
|
2571
|
-
else if (!(transition = nextTransition.ascii8) && !(typedStructs.length > 10 && (transition = nextTransition.string8)))
|
|
2572
|
-
transition = createTypeTransition(nextTransition, ASCII, 1);
|
|
2573
|
-
target[position++] = refOffset;
|
|
2574
|
-
} else {
|
|
2575
|
-
// TODO: Enable ascii16 at some point, but get the logic right
|
|
2576
|
-
//if (isNotAscii)
|
|
2577
|
-
transition = nextTransition.string16 || createTypeTransition(nextTransition, UTF8, 2);
|
|
2578
|
-
//else
|
|
2579
|
-
//transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
|
|
2580
|
-
targetView.setUint16(position, refOffset, true);
|
|
2581
|
-
position += 2;
|
|
2582
|
-
}
|
|
2583
|
-
break;
|
|
2584
|
-
case 'object':
|
|
2585
|
-
if (value) {
|
|
2586
|
-
if (value.constructor === Date) {
|
|
2587
|
-
transition = nextTransition.date64 || createTypeTransition(nextTransition, DATE, 8);
|
|
2588
|
-
targetView.setFloat64(position, value.getTime(), true);
|
|
2589
|
-
position += 8;
|
|
2590
|
-
} else {
|
|
2591
|
-
queuedReferences.push(key, value, keyIndex);
|
|
2592
|
-
}
|
|
2593
|
-
break;
|
|
2594
|
-
} else { // null
|
|
2595
|
-
nextTransition = anyType(nextTransition, position, targetView, -10); // match CBOR with this
|
|
2596
|
-
if (nextTransition) {
|
|
2597
|
-
transition = nextTransition;
|
|
2598
|
-
position = updatedPosition;
|
|
2599
|
-
} else queuedReferences.push(key, value, keyIndex);
|
|
2600
|
-
}
|
|
2601
|
-
break;
|
|
2602
|
-
case 'boolean':
|
|
2603
|
-
transition = nextTransition.num8 || nextTransition.ascii8 || createTypeTransition(nextTransition, NUMBER, 1);
|
|
2604
|
-
target[position++] = value ? 0xf9 : 0xf8; // match CBOR with these
|
|
2605
|
-
break;
|
|
2606
|
-
case 'undefined':
|
|
2607
|
-
nextTransition = anyType(nextTransition, position, targetView, -9); // match CBOR with this
|
|
2608
|
-
if (nextTransition) {
|
|
2609
|
-
transition = nextTransition;
|
|
2610
|
-
position = updatedPosition;
|
|
2611
|
-
} else queuedReferences.push(key, value, keyIndex);
|
|
2612
|
-
break;
|
|
2613
|
-
default:
|
|
2614
|
-
queuedReferences.push(key, value, keyIndex);
|
|
2615
|
-
}
|
|
2616
|
-
keyIndex++;
|
|
2617
|
-
}
|
|
2618
|
-
|
|
2619
|
-
for (let i = 0, l = queuedReferences.length; i < l;) {
|
|
2620
|
-
let key = queuedReferences[i++];
|
|
2621
|
-
let value = queuedReferences[i++];
|
|
2622
|
-
let propertyIndex = queuedReferences[i++];
|
|
2623
|
-
let nextTransition = transition[key];
|
|
2624
|
-
if (!nextTransition) {
|
|
2625
|
-
transition[key] = nextTransition = {
|
|
2626
|
-
key,
|
|
2627
|
-
parent: transition,
|
|
2628
|
-
enumerationOffset: propertyIndex - keyIndex,
|
|
2629
|
-
ascii0: null,
|
|
2630
|
-
ascii8: null,
|
|
2631
|
-
num8: null,
|
|
2632
|
-
string16: null,
|
|
2633
|
-
object16: null,
|
|
2634
|
-
num32: null,
|
|
2635
|
-
float64: null
|
|
2636
|
-
};
|
|
2637
|
-
}
|
|
2638
|
-
let newPosition;
|
|
2639
|
-
if (value) {
|
|
2640
|
-
/*if (typeof value === 'string') { // TODO: we could re-enable long strings
|
|
2641
|
-
if (position + value.length * 3 > safeEnd) {
|
|
2642
|
-
target = makeRoom(position + value.length * 3);
|
|
2643
|
-
position -= start;
|
|
2644
|
-
targetView = target.dataView;
|
|
2645
|
-
start = 0;
|
|
2646
|
-
}
|
|
2647
|
-
newPosition = position + target.utf8Write(value, position, 0xffffffff);
|
|
2648
|
-
} else { */
|
|
2649
|
-
let size;
|
|
2650
|
-
refOffset = refPosition - refsStartPosition;
|
|
2651
|
-
if (refOffset < 0xff00) {
|
|
2652
|
-
transition = nextTransition.object16;
|
|
2653
|
-
if (transition)
|
|
2654
|
-
size = 2;
|
|
2655
|
-
else if ((transition = nextTransition.object32))
|
|
2656
|
-
size = 4;
|
|
2657
|
-
else {
|
|
2658
|
-
transition = createTypeTransition(nextTransition, OBJECT_DATA, 2);
|
|
2659
|
-
size = 2;
|
|
2660
|
-
}
|
|
2661
|
-
} else {
|
|
2662
|
-
transition = nextTransition.object32 || createTypeTransition(nextTransition, OBJECT_DATA, 4);
|
|
2663
|
-
size = 4;
|
|
2664
|
-
}
|
|
2665
|
-
newPosition = pack(value, refPosition);
|
|
2666
|
-
//}
|
|
2667
|
-
if (typeof newPosition === 'object') {
|
|
2668
|
-
// re-allocated
|
|
2669
|
-
refPosition = newPosition.position;
|
|
2670
|
-
targetView = newPosition.targetView;
|
|
2671
|
-
target = newPosition.target;
|
|
2672
|
-
refsStartPosition -= encodingStart;
|
|
2673
|
-
position -= encodingStart;
|
|
2674
|
-
start -= encodingStart;
|
|
2675
|
-
encodingStart = 0;
|
|
2676
|
-
} else
|
|
2677
|
-
refPosition = newPosition;
|
|
2678
|
-
if (size === 2) {
|
|
2679
|
-
targetView.setUint16(position, refOffset, true);
|
|
2680
|
-
position += 2;
|
|
2681
|
-
} else {
|
|
2682
|
-
targetView.setUint32(position, refOffset, true);
|
|
2683
|
-
position += 4;
|
|
2684
|
-
}
|
|
2685
|
-
} else { // null or undefined
|
|
2686
|
-
transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
|
|
2687
|
-
targetView.setInt16(position, value === null ? -10 : -9, true);
|
|
2688
|
-
position += 2;
|
|
2689
|
-
}
|
|
2690
|
-
keyIndex++;
|
|
2691
|
-
}
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
let recordId = transition[RECORD_SYMBOL];
|
|
2695
|
-
if (recordId == null) {
|
|
2696
|
-
recordId = packr.typedStructs.length;
|
|
2697
|
-
let structure = [];
|
|
2698
|
-
let nextTransition = transition;
|
|
2699
|
-
let key, type;
|
|
2700
|
-
while ((type = nextTransition.__type) !== undefined) {
|
|
2701
|
-
let size = nextTransition.__size;
|
|
2702
|
-
nextTransition = nextTransition.__parent;
|
|
2703
|
-
key = nextTransition.key;
|
|
2704
|
-
let property = [type, size, key];
|
|
2705
|
-
if (nextTransition.enumerationOffset)
|
|
2706
|
-
property.push(nextTransition.enumerationOffset);
|
|
2707
|
-
structure.push(property);
|
|
2708
|
-
nextTransition = nextTransition.parent;
|
|
2709
|
-
}
|
|
2710
|
-
structure.reverse();
|
|
2711
|
-
transition[RECORD_SYMBOL] = recordId;
|
|
2712
|
-
packr.typedStructs[recordId] = structure;
|
|
2713
|
-
pack(null, 0, true); // special call to notify that structures have been updated
|
|
2714
|
-
}
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
switch (headerSize) {
|
|
2718
|
-
case 1:
|
|
2719
|
-
if (recordId >= 0x10) return 0;
|
|
2720
|
-
target[start] = recordId + 0x20;
|
|
2721
|
-
break;
|
|
2722
|
-
case 2:
|
|
2723
|
-
if (recordId >= 0x100) return 0;
|
|
2724
|
-
target[start] = 0x38;
|
|
2725
|
-
target[start + 1] = recordId;
|
|
2726
|
-
break;
|
|
2727
|
-
case 3:
|
|
2728
|
-
if (recordId >= 0x10000) return 0;
|
|
2729
|
-
target[start] = 0x39;
|
|
2730
|
-
targetView.setUint16(start + 1, recordId, true);
|
|
2731
|
-
break;
|
|
2732
|
-
case 4:
|
|
2733
|
-
if (recordId >= 0x1000000) return 0;
|
|
2734
|
-
targetView.setUint32(start, (recordId << 8) + 0x3a, true);
|
|
2735
|
-
break;
|
|
2736
|
-
}
|
|
2737
|
-
|
|
2738
|
-
if (position < refsStartPosition) {
|
|
2739
|
-
if (refsStartPosition === refPosition)
|
|
2740
|
-
return position; // no refs
|
|
2741
|
-
// adjust positioning
|
|
2742
|
-
target.copyWithin(position, refsStartPosition, refPosition);
|
|
2743
|
-
refPosition += position - refsStartPosition;
|
|
2744
|
-
typedStructs.lastStringStart = position - start;
|
|
2745
|
-
} else if (position > refsStartPosition) {
|
|
2746
|
-
if (refsStartPosition === refPosition)
|
|
2747
|
-
return position; // no refs
|
|
2748
|
-
typedStructs.lastStringStart = position - start;
|
|
2749
|
-
return writeStruct(object, target, encodingStart, start, structures, makeRoom, pack, packr);
|
|
2750
|
-
}
|
|
2751
|
-
return refPosition;
|
|
2752
|
-
}
|
|
2753
|
-
function anyType(transition, position, targetView, value) {
|
|
2754
|
-
let nextTransition;
|
|
2755
|
-
if ((nextTransition = transition.ascii8 || transition.num8)) {
|
|
2756
|
-
targetView.setInt8(position, value, true);
|
|
2757
|
-
updatedPosition = position + 1;
|
|
2758
|
-
return nextTransition;
|
|
2759
|
-
}
|
|
2760
|
-
if ((nextTransition = transition.string16 || transition.object16)) {
|
|
2761
|
-
targetView.setInt16(position, value, true);
|
|
2762
|
-
updatedPosition = position + 2;
|
|
2763
|
-
return nextTransition;
|
|
2764
|
-
}
|
|
2765
|
-
if (nextTransition = transition.num32) {
|
|
2766
|
-
targetView.setUint32(position, 0xe0000100 + value, true);
|
|
2767
|
-
updatedPosition = position + 4;
|
|
2768
|
-
return nextTransition;
|
|
2769
|
-
}
|
|
2770
|
-
// transition.float64
|
|
2771
|
-
if (nextTransition = transition.num64) {
|
|
2772
|
-
targetView.setFloat64(position, NaN, true);
|
|
2773
|
-
targetView.setInt8(position, value);
|
|
2774
|
-
updatedPosition = position + 8;
|
|
2775
|
-
return nextTransition;
|
|
2776
|
-
}
|
|
2777
|
-
updatedPosition = position;
|
|
2778
|
-
// TODO: can we do an "any" type where we defer the decision?
|
|
2779
|
-
return;
|
|
2780
|
-
}
|
|
2781
|
-
function createTypeTransition(transition, type, size) {
|
|
2782
|
-
let typeName = TYPE_NAMES[type] + (size << 3);
|
|
2783
|
-
let newTransition = transition[typeName] || (transition[typeName] = Object.create(null));
|
|
2784
|
-
newTransition.__type = type;
|
|
2785
|
-
newTransition.__size = size;
|
|
2786
|
-
newTransition.__parent = transition;
|
|
2787
|
-
return newTransition;
|
|
2788
|
-
}
|
|
2789
|
-
function onLoadedStructures(sharedData) {
|
|
2790
|
-
if (!(sharedData instanceof Map))
|
|
2791
|
-
return sharedData;
|
|
2792
|
-
let typed = sharedData.get('typed') || [];
|
|
2793
|
-
if (Object.isFrozen(typed))
|
|
2794
|
-
typed = typed.map(structure => structure.slice(0));
|
|
2795
|
-
let named = sharedData.get('named');
|
|
2796
|
-
let transitions = Object.create(null);
|
|
2797
|
-
for (let i = 0, l = typed.length; i < l; i++) {
|
|
2798
|
-
let structure = typed[i];
|
|
2799
|
-
let transition = transitions;
|
|
2800
|
-
for (let [type, size, key] of structure) {
|
|
2801
|
-
let nextTransition = transition[key];
|
|
2802
|
-
if (!nextTransition) {
|
|
2803
|
-
transition[key] = nextTransition = {
|
|
2804
|
-
key,
|
|
2805
|
-
parent: transition,
|
|
2806
|
-
enumerationOffset: 0,
|
|
2807
|
-
ascii0: null,
|
|
2808
|
-
ascii8: null,
|
|
2809
|
-
num8: null,
|
|
2810
|
-
string16: null,
|
|
2811
|
-
object16: null,
|
|
2812
|
-
num32: null,
|
|
2813
|
-
float64: null,
|
|
2814
|
-
date64: null,
|
|
2815
|
-
};
|
|
2816
|
-
}
|
|
2817
|
-
transition = createTypeTransition(nextTransition, type, size);
|
|
2818
|
-
}
|
|
2819
|
-
transition[RECORD_SYMBOL] = i;
|
|
2820
|
-
}
|
|
2821
|
-
typed.transitions = transitions;
|
|
2822
|
-
this.typedStructs = typed;
|
|
2823
|
-
this.lastTypedStructuresLength = typed.length;
|
|
2824
|
-
return named;
|
|
2825
|
-
}
|
|
2826
|
-
var sourceSymbol = Symbol.for('source');
|
|
2827
|
-
function readStruct(src, position, srcEnd, unpackr) {
|
|
2828
|
-
let recordId = src[position++] - 0x20;
|
|
2829
|
-
if (recordId >= 24) {
|
|
2830
|
-
switch(recordId) {
|
|
2831
|
-
case 24: recordId = src[position++]; break;
|
|
2832
|
-
// little endian:
|
|
2833
|
-
case 25: recordId = src[position++] + (src[position++] << 8); break;
|
|
2834
|
-
case 26: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16); break;
|
|
2835
|
-
case 27: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16) + (src[position++] << 24); break;
|
|
2836
|
-
}
|
|
2837
|
-
}
|
|
2838
|
-
let structure = unpackr.typedStructs && unpackr.typedStructs[recordId];
|
|
2839
|
-
if (!structure) {
|
|
2840
|
-
// copy src buffer because getStructures will override it
|
|
2841
|
-
src = Uint8Array.prototype.slice.call(src, position, srcEnd);
|
|
2842
|
-
srcEnd -= position;
|
|
2843
|
-
position = 0;
|
|
2844
|
-
if (!unpackr.getStructures)
|
|
2845
|
-
throw new Error(`Reference to shared structure ${recordId} without getStructures method`);
|
|
2846
|
-
unpackr._mergeStructures(unpackr.getStructures());
|
|
2847
|
-
if (!unpackr.typedStructs)
|
|
2848
|
-
throw new Error('Could not find any shared typed structures');
|
|
2849
|
-
unpackr.lastTypedStructuresLength = unpackr.typedStructs.length;
|
|
2850
|
-
structure = unpackr.typedStructs[recordId];
|
|
2851
|
-
if (!structure)
|
|
2852
|
-
throw new Error('Could not find typed structure ' + recordId);
|
|
2853
|
-
}
|
|
2854
|
-
var construct = structure.construct;
|
|
2855
|
-
var fullConstruct = structure.fullConstruct;
|
|
2856
|
-
if (!construct) {
|
|
2857
|
-
construct = structure.construct = function LazyObject() {
|
|
2858
|
-
};
|
|
2859
|
-
fullConstruct = structure.fullConstruct = function LoadedObject() {
|
|
2860
|
-
};
|
|
2861
|
-
fullConstruct.prototype = unpackr.structPrototype || {};
|
|
2862
|
-
var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
|
|
2863
|
-
let properties = [];
|
|
2864
|
-
let currentOffset = 0;
|
|
2865
|
-
let lastRefProperty;
|
|
2866
|
-
for (let i = 0, l = structure.length; i < l; i++) {
|
|
2867
|
-
let definition = structure[i];
|
|
2868
|
-
let [ type, size, key, enumerationOffset ] = definition;
|
|
2869
|
-
if (key === '__proto__')
|
|
2870
|
-
key = '__proto_';
|
|
2871
|
-
let property = {
|
|
2872
|
-
key,
|
|
2873
|
-
offset: currentOffset,
|
|
2874
|
-
};
|
|
2875
|
-
if (enumerationOffset)
|
|
2876
|
-
properties.splice(i + enumerationOffset, 0, property);
|
|
2877
|
-
else
|
|
2878
|
-
properties.push(property);
|
|
2879
|
-
let getRef;
|
|
2880
|
-
switch(size) { // TODO: Move into a separate function
|
|
2881
|
-
case 0: getRef = () => 0; break;
|
|
2882
|
-
case 1:
|
|
2883
|
-
getRef = (source, position) => {
|
|
2884
|
-
let ref = source.bytes[position + property.offset];
|
|
2885
|
-
return ref >= 0xf6 ? toConstant(ref) : ref;
|
|
2886
|
-
};
|
|
2887
|
-
break;
|
|
2888
|
-
case 2:
|
|
2889
|
-
getRef = (source, position) => {
|
|
2890
|
-
let src = source.bytes;
|
|
2891
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2892
|
-
let ref = dataView.getUint16(position + property.offset, true);
|
|
2893
|
-
return ref >= 0xff00 ? toConstant(ref & 0xff) : ref;
|
|
2894
|
-
};
|
|
2895
|
-
break;
|
|
2896
|
-
case 4:
|
|
2897
|
-
getRef = (source, position) => {
|
|
2898
|
-
let src = source.bytes;
|
|
2899
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2900
|
-
let ref = dataView.getUint32(position + property.offset, true);
|
|
2901
|
-
return ref >= 0xffffff00 ? toConstant(ref & 0xff) : ref;
|
|
2902
|
-
};
|
|
2903
|
-
break;
|
|
2904
|
-
}
|
|
2905
|
-
property.getRef = getRef;
|
|
2906
|
-
currentOffset += size;
|
|
2907
|
-
let get;
|
|
2908
|
-
switch(type) {
|
|
2909
|
-
case ASCII:
|
|
2910
|
-
if (lastRefProperty && !lastRefProperty.next)
|
|
2911
|
-
lastRefProperty.next = property;
|
|
2912
|
-
lastRefProperty = property;
|
|
2913
|
-
property.multiGetCount = 0;
|
|
2914
|
-
get = function(source) {
|
|
2915
|
-
let src = source.bytes;
|
|
2916
|
-
let position = source.position;
|
|
2917
|
-
let refStart = currentOffset + position;
|
|
2918
|
-
let ref = getRef(source, position);
|
|
2919
|
-
if (typeof ref !== 'number') return ref;
|
|
2920
|
-
|
|
2921
|
-
let end, next = property.next;
|
|
2922
|
-
while(next) {
|
|
2923
|
-
end = next.getRef(source, position);
|
|
2924
|
-
if (typeof end === 'number')
|
|
2925
|
-
break;
|
|
2926
|
-
else
|
|
2927
|
-
end = null;
|
|
2928
|
-
next = next.next;
|
|
2929
|
-
}
|
|
2930
|
-
if (end == null)
|
|
2931
|
-
end = source.bytesEnd - refStart;
|
|
2932
|
-
if (source.srcString) {
|
|
2933
|
-
return source.srcString.slice(ref, end);
|
|
2934
|
-
}
|
|
2935
|
-
/*if (property.multiGetCount > 0) {
|
|
2936
|
-
let asciiEnd;
|
|
2937
|
-
next = firstRefProperty;
|
|
2938
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2939
|
-
do {
|
|
2940
|
-
asciiEnd = dataView.getUint16(source.position + next.offset, true);
|
|
2941
|
-
if (asciiEnd < 0xff00)
|
|
2942
|
-
break;
|
|
2943
|
-
else
|
|
2944
|
-
asciiEnd = null;
|
|
2945
|
-
} while((next = next.next));
|
|
2946
|
-
if (asciiEnd == null)
|
|
2947
|
-
asciiEnd = source.bytesEnd - refStart
|
|
2948
|
-
source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
|
|
2949
|
-
return source.srcString.slice(ref, end);
|
|
2950
|
-
}
|
|
2951
|
-
if (source.prevStringGet) {
|
|
2952
|
-
source.prevStringGet.multiGetCount += 2;
|
|
2953
|
-
} else {
|
|
2954
|
-
source.prevStringGet = property;
|
|
2955
|
-
property.multiGetCount--;
|
|
2956
|
-
}*/
|
|
2957
|
-
return readString(src, ref + refStart, end - ref);
|
|
2958
|
-
//return src.toString('latin1', ref + refStart, end + refStart);
|
|
2959
|
-
};
|
|
2960
|
-
break;
|
|
2961
|
-
case UTF8: case OBJECT_DATA:
|
|
2962
|
-
if (lastRefProperty && !lastRefProperty.next)
|
|
2963
|
-
lastRefProperty.next = property;
|
|
2964
|
-
lastRefProperty = property;
|
|
2965
|
-
get = function(source) {
|
|
2966
|
-
let position = source.position;
|
|
2967
|
-
let refStart = currentOffset + position;
|
|
2968
|
-
let ref = getRef(source, position);
|
|
2969
|
-
if (typeof ref !== 'number') return ref;
|
|
2970
|
-
let src = source.bytes;
|
|
2971
|
-
let end, next = property.next;
|
|
2972
|
-
while(next) {
|
|
2973
|
-
end = next.getRef(source, position);
|
|
2974
|
-
if (typeof end === 'number')
|
|
2975
|
-
break;
|
|
2976
|
-
else
|
|
2977
|
-
end = null;
|
|
2978
|
-
next = next.next;
|
|
2979
|
-
}
|
|
2980
|
-
if (end == null)
|
|
2981
|
-
end = source.bytesEnd - refStart;
|
|
2982
|
-
if (type === UTF8) {
|
|
2983
|
-
return src.toString('utf8', ref + refStart, end + refStart);
|
|
2984
|
-
} else {
|
|
2985
|
-
currentSource = source;
|
|
2986
|
-
try {
|
|
2987
|
-
return unpackr.unpack(src, { start: ref + refStart, end: end + refStart });
|
|
2988
|
-
} finally {
|
|
2989
|
-
currentSource = null;
|
|
2990
|
-
}
|
|
2991
|
-
}
|
|
2992
|
-
};
|
|
2993
|
-
break;
|
|
2994
|
-
case NUMBER:
|
|
2995
|
-
switch(size) {
|
|
2996
|
-
case 4:
|
|
2997
|
-
get = function (source) {
|
|
2998
|
-
let src = source.bytes;
|
|
2999
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
3000
|
-
let position = source.position + property.offset;
|
|
3001
|
-
let value = dataView.getInt32(position, true);
|
|
3002
|
-
if (value < 0x20000000) {
|
|
3003
|
-
if (value > -0x1f000000)
|
|
3004
|
-
return value;
|
|
3005
|
-
if (value > -0x20000000)
|
|
3006
|
-
return toConstant(value & 0xff);
|
|
3007
|
-
}
|
|
3008
|
-
let fValue = dataView.getFloat32(position, true);
|
|
3009
|
-
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
3010
|
-
let multiplier = mult10[((src[position + 3] & 0x7f) << 1) | (src[position + 2] >> 7)];
|
|
3011
|
-
return ((multiplier * fValue + (fValue > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
|
|
3012
|
-
};
|
|
3013
|
-
break;
|
|
3014
|
-
case 8:
|
|
3015
|
-
get = function (source) {
|
|
3016
|
-
let src = source.bytes;
|
|
3017
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
3018
|
-
let value = dataView.getFloat64(source.position + property.offset, true);
|
|
3019
|
-
if (isNaN(value)) {
|
|
3020
|
-
let byte = src[source.position + property.offset];
|
|
3021
|
-
if (byte >= 0xf6)
|
|
3022
|
-
return toConstant(byte);
|
|
3023
|
-
}
|
|
3024
|
-
return value;
|
|
3025
|
-
};
|
|
3026
|
-
break;
|
|
3027
|
-
case 1:
|
|
3028
|
-
get = function (source) {
|
|
3029
|
-
let src = source.bytes;
|
|
3030
|
-
let value = src[source.position + property.offset];
|
|
3031
|
-
return value < 0xf6 ? value : toConstant(value);
|
|
3032
|
-
};
|
|
3033
|
-
break;
|
|
3034
|
-
}
|
|
3035
|
-
break;
|
|
3036
|
-
case DATE:
|
|
3037
|
-
get = function (source) {
|
|
3038
|
-
let src = source.bytes;
|
|
3039
|
-
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
3040
|
-
return new Date(dataView.getFloat64(source.position + property.offset, true));
|
|
3041
|
-
};
|
|
3042
|
-
break;
|
|
3043
|
-
|
|
3044
|
-
}
|
|
3045
|
-
property.get = get;
|
|
3046
|
-
}
|
|
3047
|
-
// TODO: load the srcString for faster string decoding on toJSON
|
|
3048
|
-
if (evalSupported) {
|
|
3049
|
-
let objectLiteralProperties = [];
|
|
3050
|
-
let args = [];
|
|
3051
|
-
let i = 0;
|
|
3052
|
-
let hasInheritedProperties;
|
|
3053
|
-
for (let property of properties) { // assign in enumeration order
|
|
3054
|
-
if (unpackr.alwaysLazyProperty && unpackr.alwaysLazyProperty(property.key)) {
|
|
3055
|
-
// these properties are not eagerly evaluated and this can be used for creating properties
|
|
3056
|
-
// that are not serialized as JSON
|
|
3057
|
-
hasInheritedProperties = true;
|
|
3058
|
-
continue;
|
|
3059
|
-
}
|
|
3060
|
-
Object.defineProperty(prototype, property.key, { get: withSource(property.get), enumerable: true });
|
|
3061
|
-
let valueFunction = 'v' + i++;
|
|
3062
|
-
args.push(valueFunction);
|
|
3063
|
-
objectLiteralProperties.push('o[' + JSON.stringify(property.key) + ']=' + valueFunction + '(s)');
|
|
3064
|
-
}
|
|
3065
|
-
if (hasInheritedProperties) {
|
|
3066
|
-
objectLiteralProperties.push('__proto__:this');
|
|
3067
|
-
}
|
|
3068
|
-
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));
|
|
3069
|
-
Object.defineProperty(prototype, 'toJSON', {
|
|
3070
|
-
value(omitUnderscoredProperties) {
|
|
3071
|
-
return toObject.call(this, this[sourceSymbol]);
|
|
3072
|
-
}
|
|
3073
|
-
});
|
|
3074
|
-
} else {
|
|
3075
|
-
Object.defineProperty(prototype, 'toJSON', {
|
|
3076
|
-
value(omitUnderscoredProperties) {
|
|
3077
|
-
// return an enumerable object with own properties to JSON stringify
|
|
3078
|
-
let resolved = {};
|
|
3079
|
-
for (let i = 0, l = properties.length; i < l; i++) {
|
|
3080
|
-
// TODO: check alwaysLazyProperty
|
|
3081
|
-
let key = properties[i].key;
|
|
3082
|
-
|
|
3083
|
-
resolved[key] = this[key];
|
|
3084
|
-
}
|
|
3085
|
-
return resolved;
|
|
3086
|
-
},
|
|
3087
|
-
// not enumerable or anything
|
|
3088
|
-
});
|
|
3089
|
-
}
|
|
3090
|
-
}
|
|
3091
|
-
var instance = new construct();
|
|
3092
|
-
instance[sourceSymbol] = {
|
|
3093
|
-
bytes: src,
|
|
3094
|
-
position,
|
|
3095
|
-
srcString: '',
|
|
3096
|
-
bytesEnd: srcEnd
|
|
3097
|
-
};
|
|
3098
|
-
return instance;
|
|
3099
|
-
}
|
|
3100
|
-
function toConstant(code) {
|
|
3101
|
-
switch(code) {
|
|
3102
|
-
case 0xf6: return null;
|
|
3103
|
-
case 0xf7: return undefined;
|
|
3104
|
-
case 0xf8: return false;
|
|
3105
|
-
case 0xf9: return true;
|
|
3106
|
-
}
|
|
3107
|
-
throw new Error('Unknown constant');
|
|
3108
|
-
}
|
|
3109
|
-
function withSource(get) {
|
|
3110
|
-
return function() {
|
|
3111
|
-
return get(this[sourceSymbol]);
|
|
3112
|
-
}
|
|
3113
|
-
}
|
|
3114
|
-
|
|
3115
|
-
function saveState() {
|
|
3116
|
-
if (currentSource) {
|
|
3117
|
-
currentSource.bytes = Uint8Array.prototype.slice.call(currentSource.bytes, currentSource.position, currentSource.bytesEnd);
|
|
3118
|
-
currentSource.position = 0;
|
|
3119
|
-
currentSource.bytesEnd = currentSource.bytes.length;
|
|
3120
|
-
}
|
|
3121
|
-
}
|
|
3122
|
-
function prepareStructures(structures, packr) {
|
|
3123
|
-
if (packr.typedStructs) {
|
|
3124
|
-
let structMap = new Map();
|
|
3125
|
-
structMap.set('named', structures);
|
|
3126
|
-
structMap.set('typed', packr.typedStructs);
|
|
3127
|
-
structures = structMap;
|
|
3128
|
-
}
|
|
3129
|
-
let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
|
|
3130
|
-
structures.isCompatible = existing => {
|
|
3131
|
-
let compatible = true;
|
|
3132
|
-
if (existing instanceof Map) {
|
|
3133
|
-
let named = existing.get('named') || [];
|
|
3134
|
-
if (named.length !== (packr.lastNamedStructuresLength || 0))
|
|
3135
|
-
compatible = false;
|
|
3136
|
-
let typed = existing.get('typed') || [];
|
|
3137
|
-
if (typed.length !== lastTypedStructuresLength)
|
|
3138
|
-
compatible = false;
|
|
3139
|
-
} else if (existing instanceof Array || Array.isArray(existing)) {
|
|
3140
|
-
if (existing.length !== (packr.lastNamedStructuresLength || 0))
|
|
3141
|
-
compatible = false;
|
|
3142
|
-
}
|
|
3143
|
-
if (!compatible)
|
|
3144
|
-
packr._mergeStructures(existing);
|
|
3145
|
-
return compatible;
|
|
3146
|
-
};
|
|
3147
|
-
packr.lastTypedStructuresLength = packr.typedStructs && packr.typedStructs.length;
|
|
3148
|
-
return structures;
|
|
3149
|
-
}
|
|
3150
|
-
|
|
3151
|
-
setReadStruct(readStruct, onLoadedStructures, saveState);
|
|
3152
|
-
|
|
3153
2321
|
class PackrStream extends stream.Transform {
|
|
3154
2322
|
constructor(options) {
|
|
3155
2323
|
if (!options)
|
|
@@ -3172,6 +2340,7 @@ class UnpackrStream extends stream.Transform {
|
|
|
3172
2340
|
options.objectMode = true;
|
|
3173
2341
|
super(options);
|
|
3174
2342
|
options.structures = [];
|
|
2343
|
+
this.maxIncompleteBufferSize = options.maxIncompleteBufferSize !== undefined ? options.maxIncompleteBufferSize : 0x4000000;
|
|
3175
2344
|
this.unpackr = options.unpackr || new Unpackr(options);
|
|
3176
2345
|
}
|
|
3177
2346
|
_transform(chunk, encoding, callback) {
|
|
@@ -3184,22 +2353,26 @@ class UnpackrStream extends stream.Transform {
|
|
|
3184
2353
|
values = this.unpackr.unpackMultiple(chunk);
|
|
3185
2354
|
} catch(error) {
|
|
3186
2355
|
if (error.incomplete) {
|
|
3187
|
-
|
|
2356
|
+
let incompleteBuffer = chunk.slice(error.lastPosition);
|
|
2357
|
+
if (incompleteBuffer.length > this.maxIncompleteBufferSize) {
|
|
2358
|
+
this.incompleteBuffer = null;
|
|
2359
|
+
return callback(new Error('Maximum incomplete buffer size exceeded'));
|
|
2360
|
+
}
|
|
2361
|
+
this.incompleteBuffer = incompleteBuffer;
|
|
3188
2362
|
values = error.values;
|
|
3189
|
-
}
|
|
3190
|
-
|
|
3191
|
-
throw error
|
|
3192
|
-
} finally {
|
|
3193
|
-
for (let value of values || []) {
|
|
3194
|
-
if (value === null)
|
|
3195
|
-
value = this.getNullValue();
|
|
3196
|
-
this.push(value);
|
|
2363
|
+
} else {
|
|
2364
|
+
return callback(error);
|
|
3197
2365
|
}
|
|
3198
2366
|
}
|
|
3199
|
-
|
|
2367
|
+
for (let value of values || []) {
|
|
2368
|
+
if (value === null)
|
|
2369
|
+
value = this.getNullValue();
|
|
2370
|
+
this.push(value);
|
|
2371
|
+
}
|
|
2372
|
+
callback();
|
|
3200
2373
|
}
|
|
3201
2374
|
getNullValue() {
|
|
3202
|
-
return Symbol.for(null)
|
|
2375
|
+
return Symbol.for(null);
|
|
3203
2376
|
}
|
|
3204
2377
|
}
|
|
3205
2378
|
|
|
@@ -3212,13 +2385,13 @@ class UnpackrStream extends stream.Transform {
|
|
|
3212
2385
|
*/
|
|
3213
2386
|
function packIter (objectIterator, options = {}) {
|
|
3214
2387
|
if (!objectIterator || typeof objectIterator !== 'object') {
|
|
3215
|
-
throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
|
|
2388
|
+
throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable');
|
|
3216
2389
|
} else if (typeof objectIterator[Symbol.iterator] === 'function') {
|
|
3217
|
-
return packIterSync(objectIterator, options)
|
|
2390
|
+
return packIterSync(objectIterator, options);
|
|
3218
2391
|
} else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
|
|
3219
|
-
return packIterAsync(objectIterator, options)
|
|
2392
|
+
return packIterAsync(objectIterator, options);
|
|
3220
2393
|
} else {
|
|
3221
|
-
throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
|
|
2394
|
+
throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise');
|
|
3222
2395
|
}
|
|
3223
2396
|
}
|
|
3224
2397
|
|
|
@@ -3245,7 +2418,7 @@ async function * packIterAsync (objectIterator, options) {
|
|
|
3245
2418
|
*/
|
|
3246
2419
|
function unpackIter (bufferIterator, options = {}) {
|
|
3247
2420
|
if (!bufferIterator || typeof bufferIterator !== 'object') {
|
|
3248
|
-
throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
|
|
2421
|
+
throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise');
|
|
3249
2422
|
}
|
|
3250
2423
|
|
|
3251
2424
|
const unpackr = new Unpackr(options);
|
|
@@ -3265,10 +2438,10 @@ function unpackIter (bufferIterator, options = {}) {
|
|
|
3265
2438
|
incomplete = chunk.slice(err.lastPosition);
|
|
3266
2439
|
yields = err.values;
|
|
3267
2440
|
} else {
|
|
3268
|
-
throw err
|
|
2441
|
+
throw err;
|
|
3269
2442
|
}
|
|
3270
2443
|
}
|
|
3271
|
-
return yields
|
|
2444
|
+
return yields;
|
|
3272
2445
|
};
|
|
3273
2446
|
|
|
3274
2447
|
if (typeof bufferIterator[Symbol.iterator] === 'function') {
|
|
@@ -3276,13 +2449,13 @@ function unpackIter (bufferIterator, options = {}) {
|
|
|
3276
2449
|
for (const value of bufferIterator) {
|
|
3277
2450
|
yield * parser(value);
|
|
3278
2451
|
}
|
|
3279
|
-
})()
|
|
2452
|
+
})();
|
|
3280
2453
|
} else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
|
|
3281
2454
|
return (async function * iter () {
|
|
3282
2455
|
for await (const value of bufferIterator) {
|
|
3283
2456
|
yield * parser(value);
|
|
3284
2457
|
}
|
|
3285
|
-
})()
|
|
2458
|
+
})();
|
|
3286
2459
|
}
|
|
3287
2460
|
}
|
|
3288
2461
|
const decodeIter = unpackIter;
|