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