msgpackr 1.6.1 → 1.7.0-alpha2

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