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