cbor-x 1.4.1 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/decode.js +47 -27
- package/dist/decode-no-eval.cjs +1151 -0
- package/dist/decode-no-eval.cjs.map +1 -0
- package/dist/index-no-eval.cjs +2398 -0
- package/dist/index-no-eval.cjs.map +1 -0
- package/dist/index-no-eval.min.js +2 -0
- package/dist/index-no-eval.min.js.map +1 -0
- package/dist/index.js +552 -360
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +2 -140
- package/dist/index.min.js.map +1 -0
- package/dist/node.cjs +559 -365
- package/dist/node.cjs.map +1 -0
- package/dist/test.js +181 -32
- package/dist/test.js.map +1 -0
- package/encode.d.ts +1 -1
- package/encode.js +173 -2
- package/index.d.ts +9 -5
- package/index.js +1 -1
- package/node-index.js +1 -1
- package/package.json +10 -6
- package/rollup.config.js +51 -8
- package/stream.js +4 -2
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
3
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = global || self, factory(global.CBOR = {}));
|
|
5
|
-
}(this, (function (exports) { 'use strict';
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CBOR = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
7
|
let decoder;
|
|
8
8
|
try {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
} catch(error) {}
|
|
11
11
|
let src;
|
|
12
12
|
let srcEnd;
|
|
13
|
-
let position = 0;
|
|
13
|
+
let position$1 = 0;
|
|
14
14
|
const LEGACY_RECORD_INLINE_ID = 105;
|
|
15
15
|
const RECORD_DEFINITIONS_ID = 0xdffe;
|
|
16
16
|
const RECORD_INLINE_ID = 0xdfff; // temporary first-come first-serve tag // proposed tag: 0x7265 // 're'
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
let srcString;
|
|
23
23
|
let srcStringStart = 0;
|
|
24
24
|
let srcStringEnd = 0;
|
|
25
|
-
let bundledStrings;
|
|
25
|
+
let bundledStrings$1;
|
|
26
26
|
let referenceMap;
|
|
27
27
|
let currentExtensions = [];
|
|
28
28
|
let currentExtensionRanges = [];
|
|
@@ -111,10 +111,10 @@
|
|
|
111
111
|
})
|
|
112
112
|
}
|
|
113
113
|
srcEnd = end > -1 ? end : source.length;
|
|
114
|
-
position = 0;
|
|
114
|
+
position$1 = 0;
|
|
115
115
|
srcStringEnd = 0;
|
|
116
116
|
srcString = null;
|
|
117
|
-
bundledStrings = null;
|
|
117
|
+
bundledStrings$1 = null;
|
|
118
118
|
src = source;
|
|
119
119
|
// this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
|
|
120
120
|
// technique for getting data from a database where it can be copied into an existing buffer instead of creating
|
|
@@ -157,8 +157,8 @@
|
|
|
157
157
|
if (forEach(value) === false) {
|
|
158
158
|
return
|
|
159
159
|
}
|
|
160
|
-
while(position < size) {
|
|
161
|
-
lastPosition = position;
|
|
160
|
+
while(position$1 < size) {
|
|
161
|
+
lastPosition = position$1;
|
|
162
162
|
if (forEach(checkedRead()) === false) {
|
|
163
163
|
return
|
|
164
164
|
}
|
|
@@ -166,8 +166,8 @@
|
|
|
166
166
|
}
|
|
167
167
|
else {
|
|
168
168
|
values = [ value ];
|
|
169
|
-
while(position < size) {
|
|
170
|
-
lastPosition = position;
|
|
169
|
+
while(position$1 < size) {
|
|
170
|
+
lastPosition = position$1;
|
|
171
171
|
values.push(checkedRead());
|
|
172
172
|
}
|
|
173
173
|
return values
|
|
@@ -185,23 +185,24 @@
|
|
|
185
185
|
function checkedRead() {
|
|
186
186
|
try {
|
|
187
187
|
let result = read();
|
|
188
|
-
if (bundledStrings) {
|
|
189
|
-
if (position >= bundledStrings.postBundlePosition) {
|
|
188
|
+
if (bundledStrings$1) {
|
|
189
|
+
if (position$1 >= bundledStrings$1.postBundlePosition) {
|
|
190
190
|
let error = new Error('Unexpected bundle position');
|
|
191
191
|
error.incomplete = true;
|
|
192
192
|
throw error
|
|
193
193
|
}
|
|
194
194
|
// bundled strings to skip past
|
|
195
|
-
position = bundledStrings.postBundlePosition;
|
|
195
|
+
position$1 = bundledStrings$1.postBundlePosition;
|
|
196
|
+
bundledStrings$1 = null;
|
|
196
197
|
}
|
|
197
198
|
|
|
198
|
-
if (position == srcEnd) {
|
|
199
|
+
if (position$1 == srcEnd) {
|
|
199
200
|
// finished reading this source, cleanup references
|
|
200
201
|
currentStructures = null;
|
|
201
202
|
src = null;
|
|
202
203
|
if (referenceMap)
|
|
203
204
|
referenceMap = null;
|
|
204
|
-
} else if (position > srcEnd) {
|
|
205
|
+
} else if (position$1 > srcEnd) {
|
|
205
206
|
// over read
|
|
206
207
|
let error = new Error('Unexpected end of CBOR data');
|
|
207
208
|
error.incomplete = true;
|
|
@@ -221,52 +222,52 @@
|
|
|
221
222
|
}
|
|
222
223
|
|
|
223
224
|
function read() {
|
|
224
|
-
let token = src[position++];
|
|
225
|
+
let token = src[position$1++];
|
|
225
226
|
let majorType = token >> 5;
|
|
226
227
|
token = token & 0x1f;
|
|
227
228
|
if (token > 0x17) {
|
|
228
229
|
switch (token) {
|
|
229
230
|
case 0x18:
|
|
230
|
-
token = src[position++];
|
|
231
|
+
token = src[position$1++];
|
|
231
232
|
break
|
|
232
233
|
case 0x19:
|
|
233
234
|
if (majorType == 7) {
|
|
234
235
|
return getFloat16()
|
|
235
236
|
}
|
|
236
|
-
token = dataView.getUint16(position);
|
|
237
|
-
position += 2;
|
|
237
|
+
token = dataView.getUint16(position$1);
|
|
238
|
+
position$1 += 2;
|
|
238
239
|
break
|
|
239
240
|
case 0x1a:
|
|
240
241
|
if (majorType == 7) {
|
|
241
|
-
let value = dataView.getFloat32(position);
|
|
242
|
+
let value = dataView.getFloat32(position$1);
|
|
242
243
|
if (currentDecoder.useFloat32 > 2) {
|
|
243
244
|
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
244
|
-
let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
|
|
245
|
-
position += 4;
|
|
245
|
+
let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
|
|
246
|
+
position$1 += 4;
|
|
246
247
|
return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
247
248
|
}
|
|
248
|
-
position += 4;
|
|
249
|
+
position$1 += 4;
|
|
249
250
|
return value
|
|
250
251
|
}
|
|
251
|
-
token = dataView.getUint32(position);
|
|
252
|
-
position += 4;
|
|
252
|
+
token = dataView.getUint32(position$1);
|
|
253
|
+
position$1 += 4;
|
|
253
254
|
break
|
|
254
255
|
case 0x1b:
|
|
255
256
|
if (majorType == 7) {
|
|
256
|
-
let value = dataView.getFloat64(position);
|
|
257
|
-
position += 8;
|
|
257
|
+
let value = dataView.getFloat64(position$1);
|
|
258
|
+
position$1 += 8;
|
|
258
259
|
return value
|
|
259
260
|
}
|
|
260
261
|
if (majorType > 1) {
|
|
261
|
-
if (dataView.getUint32(position) > 0)
|
|
262
|
+
if (dataView.getUint32(position$1) > 0)
|
|
262
263
|
throw new Error('JavaScript does not support arrays, maps, or strings with length over 4294967295')
|
|
263
|
-
token = dataView.getUint32(position + 4);
|
|
264
|
+
token = dataView.getUint32(position$1 + 4);
|
|
264
265
|
} else if (currentDecoder.int64AsNumber) {
|
|
265
|
-
token = dataView.getUint32(position) * 0x100000000;
|
|
266
|
-
token += dataView.getUint32(position + 4);
|
|
266
|
+
token = dataView.getUint32(position$1) * 0x100000000;
|
|
267
|
+
token += dataView.getUint32(position$1 + 4);
|
|
267
268
|
} else
|
|
268
|
-
token = dataView.getBigUint64(position);
|
|
269
|
-
position += 8;
|
|
269
|
+
token = dataView.getBigUint64(position$1);
|
|
270
|
+
position$1 += 8;
|
|
270
271
|
break
|
|
271
272
|
case 0x1f:
|
|
272
273
|
// indefinite length
|
|
@@ -315,8 +316,8 @@
|
|
|
315
316
|
case 2: // buffer
|
|
316
317
|
return readBin(token)
|
|
317
318
|
case 3: // string
|
|
318
|
-
if (srcStringEnd >= position) {
|
|
319
|
-
return srcString.slice(position - srcStringStart, (position += token) - srcStringStart)
|
|
319
|
+
if (srcStringEnd >= position$1) {
|
|
320
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += token) - srcStringStart)
|
|
320
321
|
}
|
|
321
322
|
if (srcStringEnd == 0 && srcEnd < 140 && token < 32) {
|
|
322
323
|
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
@@ -356,13 +357,28 @@
|
|
|
356
357
|
return structure.read()
|
|
357
358
|
}
|
|
358
359
|
if (token < 0x10000) {
|
|
359
|
-
if (token == RECORD_INLINE_ID) // we do a special check for this so that we can keep the
|
|
360
|
-
|
|
360
|
+
if (token == RECORD_INLINE_ID) { // we do a special check for this so that we can keep the
|
|
361
|
+
// currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
|
|
362
|
+
let length = readJustLength();
|
|
363
|
+
let id = read();
|
|
364
|
+
let structure = read();
|
|
365
|
+
recordDefinition(id, structure);
|
|
366
|
+
let object = {};
|
|
367
|
+
if (currentDecoder.keyMap) for (let i = 2; i < length; i++) {
|
|
368
|
+
let key = currentDecoder.decodeKey(structure[i - 2]);
|
|
369
|
+
object[safeKey(key)] = read();
|
|
370
|
+
}
|
|
371
|
+
else for (let i = 2; i < length; i++) {
|
|
372
|
+
let key = structure[i - 2];
|
|
373
|
+
object[safeKey(key)] = read();
|
|
374
|
+
}
|
|
375
|
+
return object
|
|
376
|
+
}
|
|
361
377
|
else if (token == RECORD_DEFINITIONS_ID) {
|
|
362
378
|
let length = readJustLength();
|
|
363
379
|
let id = read();
|
|
364
380
|
for (let i = 2; i < length; i++) {
|
|
365
|
-
recordDefinition(
|
|
381
|
+
recordDefinition(id++, read());
|
|
366
382
|
}
|
|
367
383
|
return read()
|
|
368
384
|
} else if (token == BUNDLED_STRINGS_ID) {
|
|
@@ -420,24 +436,24 @@
|
|
|
420
436
|
function createStructureReader(structure) {
|
|
421
437
|
function readObject() {
|
|
422
438
|
// get the array size from the header
|
|
423
|
-
let length = src[position++];
|
|
439
|
+
let length = src[position$1++];
|
|
424
440
|
//let majorType = token >> 5
|
|
425
441
|
length = length & 0x1f;
|
|
426
442
|
if (length > 0x17) {
|
|
427
443
|
switch (length) {
|
|
428
444
|
case 0x18:
|
|
429
|
-
length = src[position++];
|
|
445
|
+
length = src[position$1++];
|
|
430
446
|
break
|
|
431
447
|
case 0x19:
|
|
432
|
-
length = dataView.getUint16(position);
|
|
433
|
-
position += 2;
|
|
448
|
+
length = dataView.getUint16(position$1);
|
|
449
|
+
position$1 += 2;
|
|
434
450
|
break
|
|
435
451
|
case 0x1a:
|
|
436
|
-
length = dataView.getUint32(position);
|
|
437
|
-
position += 4;
|
|
452
|
+
length = dataView.getUint32(position$1);
|
|
453
|
+
position$1 += 4;
|
|
438
454
|
break
|
|
439
455
|
default:
|
|
440
|
-
throw new Error('Expected array header, but got ' + src[position - 1])
|
|
456
|
+
throw new Error('Expected array header, but got ' + src[position$1 - 1])
|
|
441
457
|
}
|
|
442
458
|
}
|
|
443
459
|
// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
|
|
@@ -484,29 +500,29 @@
|
|
|
484
500
|
return result
|
|
485
501
|
}
|
|
486
502
|
if (length > 64 && decoder)
|
|
487
|
-
return decoder.decode(src.subarray(position, position += length))
|
|
488
|
-
const end = position + length;
|
|
503
|
+
return decoder.decode(src.subarray(position$1, position$1 += length))
|
|
504
|
+
const end = position$1 + length;
|
|
489
505
|
const units = [];
|
|
490
506
|
result = '';
|
|
491
|
-
while (position < end) {
|
|
492
|
-
const byte1 = src[position++];
|
|
507
|
+
while (position$1 < end) {
|
|
508
|
+
const byte1 = src[position$1++];
|
|
493
509
|
if ((byte1 & 0x80) === 0) {
|
|
494
510
|
// 1 byte
|
|
495
511
|
units.push(byte1);
|
|
496
512
|
} else if ((byte1 & 0xe0) === 0xc0) {
|
|
497
513
|
// 2 bytes
|
|
498
|
-
const byte2 = src[position++] & 0x3f;
|
|
514
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
499
515
|
units.push(((byte1 & 0x1f) << 6) | byte2);
|
|
500
516
|
} else if ((byte1 & 0xf0) === 0xe0) {
|
|
501
517
|
// 3 bytes
|
|
502
|
-
const byte2 = src[position++] & 0x3f;
|
|
503
|
-
const byte3 = src[position++] & 0x3f;
|
|
518
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
519
|
+
const byte3 = src[position$1++] & 0x3f;
|
|
504
520
|
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
|
|
505
521
|
} else if ((byte1 & 0xf8) === 0xf0) {
|
|
506
522
|
// 4 bytes
|
|
507
|
-
const byte2 = src[position++] & 0x3f;
|
|
508
|
-
const byte3 = src[position++] & 0x3f;
|
|
509
|
-
const byte4 = src[position++] & 0x3f;
|
|
523
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
524
|
+
const byte3 = src[position$1++] & 0x3f;
|
|
525
|
+
const byte4 = src[position$1++] & 0x3f;
|
|
510
526
|
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
|
511
527
|
if (unit > 0xffff) {
|
|
512
528
|
unit -= 0x10000;
|
|
@@ -532,12 +548,12 @@
|
|
|
532
548
|
}
|
|
533
549
|
let fromCharCode = String.fromCharCode;
|
|
534
550
|
function longStringInJS(length) {
|
|
535
|
-
let start = position;
|
|
551
|
+
let start = position$1;
|
|
536
552
|
let bytes = new Array(length);
|
|
537
553
|
for (let i = 0; i < length; i++) {
|
|
538
|
-
const byte = src[position++];
|
|
554
|
+
const byte = src[position$1++];
|
|
539
555
|
if ((byte & 0x80) > 0) {
|
|
540
|
-
position = start;
|
|
556
|
+
position$1 = start;
|
|
541
557
|
return
|
|
542
558
|
}
|
|
543
559
|
bytes[i] = byte;
|
|
@@ -550,131 +566,131 @@
|
|
|
550
566
|
if (length === 0)
|
|
551
567
|
return ''
|
|
552
568
|
else {
|
|
553
|
-
let a = src[position++];
|
|
569
|
+
let a = src[position$1++];
|
|
554
570
|
if ((a & 0x80) > 1) {
|
|
555
|
-
position -= 1;
|
|
571
|
+
position$1 -= 1;
|
|
556
572
|
return
|
|
557
573
|
}
|
|
558
574
|
return fromCharCode(a)
|
|
559
575
|
}
|
|
560
576
|
} else {
|
|
561
|
-
let a = src[position++];
|
|
562
|
-
let b = src[position++];
|
|
577
|
+
let a = src[position$1++];
|
|
578
|
+
let b = src[position$1++];
|
|
563
579
|
if ((a & 0x80) > 0 || (b & 0x80) > 0) {
|
|
564
|
-
position -= 2;
|
|
580
|
+
position$1 -= 2;
|
|
565
581
|
return
|
|
566
582
|
}
|
|
567
583
|
if (length < 3)
|
|
568
584
|
return fromCharCode(a, b)
|
|
569
|
-
let c = src[position++];
|
|
585
|
+
let c = src[position$1++];
|
|
570
586
|
if ((c & 0x80) > 0) {
|
|
571
|
-
position -= 3;
|
|
587
|
+
position$1 -= 3;
|
|
572
588
|
return
|
|
573
589
|
}
|
|
574
590
|
return fromCharCode(a, b, c)
|
|
575
591
|
}
|
|
576
592
|
} else {
|
|
577
|
-
let a = src[position++];
|
|
578
|
-
let b = src[position++];
|
|
579
|
-
let c = src[position++];
|
|
580
|
-
let d = src[position++];
|
|
593
|
+
let a = src[position$1++];
|
|
594
|
+
let b = src[position$1++];
|
|
595
|
+
let c = src[position$1++];
|
|
596
|
+
let d = src[position$1++];
|
|
581
597
|
if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
|
|
582
|
-
position -= 4;
|
|
598
|
+
position$1 -= 4;
|
|
583
599
|
return
|
|
584
600
|
}
|
|
585
601
|
if (length < 6) {
|
|
586
602
|
if (length === 4)
|
|
587
603
|
return fromCharCode(a, b, c, d)
|
|
588
604
|
else {
|
|
589
|
-
let e = src[position++];
|
|
605
|
+
let e = src[position$1++];
|
|
590
606
|
if ((e & 0x80) > 0) {
|
|
591
|
-
position -= 5;
|
|
607
|
+
position$1 -= 5;
|
|
592
608
|
return
|
|
593
609
|
}
|
|
594
610
|
return fromCharCode(a, b, c, d, e)
|
|
595
611
|
}
|
|
596
612
|
} else if (length < 8) {
|
|
597
|
-
let e = src[position++];
|
|
598
|
-
let f = src[position++];
|
|
613
|
+
let e = src[position$1++];
|
|
614
|
+
let f = src[position$1++];
|
|
599
615
|
if ((e & 0x80) > 0 || (f & 0x80) > 0) {
|
|
600
|
-
position -= 6;
|
|
616
|
+
position$1 -= 6;
|
|
601
617
|
return
|
|
602
618
|
}
|
|
603
619
|
if (length < 7)
|
|
604
620
|
return fromCharCode(a, b, c, d, e, f)
|
|
605
|
-
let g = src[position++];
|
|
621
|
+
let g = src[position$1++];
|
|
606
622
|
if ((g & 0x80) > 0) {
|
|
607
|
-
position -= 7;
|
|
623
|
+
position$1 -= 7;
|
|
608
624
|
return
|
|
609
625
|
}
|
|
610
626
|
return fromCharCode(a, b, c, d, e, f, g)
|
|
611
627
|
} else {
|
|
612
|
-
let e = src[position++];
|
|
613
|
-
let f = src[position++];
|
|
614
|
-
let g = src[position++];
|
|
615
|
-
let h = src[position++];
|
|
628
|
+
let e = src[position$1++];
|
|
629
|
+
let f = src[position$1++];
|
|
630
|
+
let g = src[position$1++];
|
|
631
|
+
let h = src[position$1++];
|
|
616
632
|
if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
|
|
617
|
-
position -= 8;
|
|
633
|
+
position$1 -= 8;
|
|
618
634
|
return
|
|
619
635
|
}
|
|
620
636
|
if (length < 10) {
|
|
621
637
|
if (length === 8)
|
|
622
638
|
return fromCharCode(a, b, c, d, e, f, g, h)
|
|
623
639
|
else {
|
|
624
|
-
let i = src[position++];
|
|
640
|
+
let i = src[position$1++];
|
|
625
641
|
if ((i & 0x80) > 0) {
|
|
626
|
-
position -= 9;
|
|
642
|
+
position$1 -= 9;
|
|
627
643
|
return
|
|
628
644
|
}
|
|
629
645
|
return fromCharCode(a, b, c, d, e, f, g, h, i)
|
|
630
646
|
}
|
|
631
647
|
} else if (length < 12) {
|
|
632
|
-
let i = src[position++];
|
|
633
|
-
let j = src[position++];
|
|
648
|
+
let i = src[position$1++];
|
|
649
|
+
let j = src[position$1++];
|
|
634
650
|
if ((i & 0x80) > 0 || (j & 0x80) > 0) {
|
|
635
|
-
position -= 10;
|
|
651
|
+
position$1 -= 10;
|
|
636
652
|
return
|
|
637
653
|
}
|
|
638
654
|
if (length < 11)
|
|
639
655
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j)
|
|
640
|
-
let k = src[position++];
|
|
656
|
+
let k = src[position$1++];
|
|
641
657
|
if ((k & 0x80) > 0) {
|
|
642
|
-
position -= 11;
|
|
658
|
+
position$1 -= 11;
|
|
643
659
|
return
|
|
644
660
|
}
|
|
645
661
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
|
|
646
662
|
} else {
|
|
647
|
-
let i = src[position++];
|
|
648
|
-
let j = src[position++];
|
|
649
|
-
let k = src[position++];
|
|
650
|
-
let l = src[position++];
|
|
663
|
+
let i = src[position$1++];
|
|
664
|
+
let j = src[position$1++];
|
|
665
|
+
let k = src[position$1++];
|
|
666
|
+
let l = src[position$1++];
|
|
651
667
|
if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
|
|
652
|
-
position -= 12;
|
|
668
|
+
position$1 -= 12;
|
|
653
669
|
return
|
|
654
670
|
}
|
|
655
671
|
if (length < 14) {
|
|
656
672
|
if (length === 12)
|
|
657
673
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
|
|
658
674
|
else {
|
|
659
|
-
let m = src[position++];
|
|
675
|
+
let m = src[position$1++];
|
|
660
676
|
if ((m & 0x80) > 0) {
|
|
661
|
-
position -= 13;
|
|
677
|
+
position$1 -= 13;
|
|
662
678
|
return
|
|
663
679
|
}
|
|
664
680
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
|
|
665
681
|
}
|
|
666
682
|
} else {
|
|
667
|
-
let m = src[position++];
|
|
668
|
-
let n = src[position++];
|
|
683
|
+
let m = src[position$1++];
|
|
684
|
+
let n = src[position$1++];
|
|
669
685
|
if ((m & 0x80) > 0 || (n & 0x80) > 0) {
|
|
670
|
-
position -= 14;
|
|
686
|
+
position$1 -= 14;
|
|
671
687
|
return
|
|
672
688
|
}
|
|
673
689
|
if (length < 15)
|
|
674
690
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
|
|
675
|
-
let o = src[position++];
|
|
691
|
+
let o = src[position$1++];
|
|
676
692
|
if ((o & 0x80) > 0) {
|
|
677
|
-
position -= 15;
|
|
693
|
+
position$1 -= 15;
|
|
678
694
|
return
|
|
679
695
|
}
|
|
680
696
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
|
|
@@ -687,14 +703,14 @@
|
|
|
687
703
|
function readBin(length) {
|
|
688
704
|
return currentDecoder.copyBuffers ?
|
|
689
705
|
// specifically use the copying slice (not the node one)
|
|
690
|
-
Uint8Array.prototype.slice.call(src, position, position += length) :
|
|
691
|
-
src.subarray(position, position += length)
|
|
706
|
+
Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
|
|
707
|
+
src.subarray(position$1, position$1 += length)
|
|
692
708
|
}
|
|
693
709
|
let f32Array = new Float32Array(1);
|
|
694
710
|
let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
695
711
|
function getFloat16() {
|
|
696
|
-
let byte0 = src[position++];
|
|
697
|
-
let byte1 = src[position++];
|
|
712
|
+
let byte0 = src[position$1++];
|
|
713
|
+
let byte1 = src[position$1++];
|
|
698
714
|
let exponent = (byte0 & 0x7f) >> 2;
|
|
699
715
|
if (exponent === 0x1f) { // specials
|
|
700
716
|
if (byte1 || (byte0 & 3))
|
|
@@ -716,7 +732,7 @@
|
|
|
716
732
|
return f32Array[0];
|
|
717
733
|
}
|
|
718
734
|
|
|
719
|
-
|
|
735
|
+
new Array(4096);
|
|
720
736
|
|
|
721
737
|
class Tag {
|
|
722
738
|
constructor(value, tag) {
|
|
@@ -759,9 +775,8 @@
|
|
|
759
775
|
};
|
|
760
776
|
|
|
761
777
|
// the registration of the record definition extension
|
|
762
|
-
const recordDefinition = (
|
|
763
|
-
|
|
764
|
-
let structure = definition[1];
|
|
778
|
+
const recordDefinition = (id, structure) => {
|
|
779
|
+
id = id - 0xe000;
|
|
765
780
|
let existingStructure = currentStructures[id];
|
|
766
781
|
if (existingStructure && existingStructure.isShared) {
|
|
767
782
|
(currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
|
|
@@ -769,26 +784,26 @@
|
|
|
769
784
|
currentStructures[id] = structure;
|
|
770
785
|
|
|
771
786
|
structure.read = createStructureReader(structure);
|
|
787
|
+
};
|
|
788
|
+
currentExtensions[LEGACY_RECORD_INLINE_ID] = (data) => {
|
|
789
|
+
let length = data.length;
|
|
790
|
+
let structure = data[1];
|
|
791
|
+
recordDefinition(data[0], structure);
|
|
772
792
|
let object = {};
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
else for (let i = 2,l = definition.length; i < l; i++) {
|
|
778
|
-
let key = structure[i - 2];
|
|
779
|
-
object[safeKey(key)] = definition[i];
|
|
780
|
-
}
|
|
793
|
+
for (let i = 2; i < length; i++) {
|
|
794
|
+
let key = structure[i - 2];
|
|
795
|
+
object[safeKey(key)] = data[i];
|
|
796
|
+
}
|
|
781
797
|
return object
|
|
782
798
|
};
|
|
783
|
-
currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition;
|
|
784
799
|
currentExtensions[14] = (value) => {
|
|
785
|
-
if (bundledStrings)
|
|
786
|
-
return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 += value)
|
|
800
|
+
if (bundledStrings$1)
|
|
801
|
+
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
|
|
787
802
|
return new Tag(value, 14)
|
|
788
803
|
};
|
|
789
804
|
currentExtensions[15] = (value) => {
|
|
790
|
-
if (bundledStrings)
|
|
791
|
-
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
|
|
805
|
+
if (bundledStrings$1)
|
|
806
|
+
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
|
|
792
807
|
return new Tag(value, 15)
|
|
793
808
|
};
|
|
794
809
|
let glbl = { Error, RegExp };
|
|
@@ -796,7 +811,7 @@
|
|
|
796
811
|
return (glbl[data[0]] || Error)(data[1], data[2])
|
|
797
812
|
};
|
|
798
813
|
const packedTable = (read) => {
|
|
799
|
-
if (src[position++] != 0x84)
|
|
814
|
+
if (src[position$1++] != 0x84)
|
|
800
815
|
throw new Error('Packed values structure must be followed by a 4 element array')
|
|
801
816
|
let newPackedValues = read(); // packed values
|
|
802
817
|
packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues;
|
|
@@ -818,18 +833,23 @@
|
|
|
818
833
|
return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
|
|
819
834
|
throw new Error('No support for non-integer packed references yet')
|
|
820
835
|
};
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
836
|
+
|
|
837
|
+
// The following code is an incomplete implementation of http://cbor.schmorp.de/stringref
|
|
838
|
+
// the real thing would need to implemennt more logic to populate the stringRefs table and
|
|
839
|
+
// maintain a stack of stringRef "namespaces".
|
|
840
|
+
//
|
|
841
|
+
// currentExtensions[25] = (id) => {
|
|
842
|
+
// return stringRefs[id]
|
|
843
|
+
// }
|
|
844
|
+
// currentExtensions[256] = (read) => {
|
|
845
|
+
// stringRefs = []
|
|
846
|
+
// try {
|
|
847
|
+
// return read()
|
|
848
|
+
// } finally {
|
|
849
|
+
// stringRefs = null
|
|
850
|
+
// }
|
|
851
|
+
// }
|
|
852
|
+
// currentExtensions[256].handlesRead = true
|
|
833
853
|
|
|
834
854
|
currentExtensions[28] = (read) => {
|
|
835
855
|
// shareable http://cbor.schmorp.de/value-sharing (for structured clones)
|
|
@@ -838,7 +858,7 @@
|
|
|
838
858
|
referenceMap.id = 0;
|
|
839
859
|
}
|
|
840
860
|
let id = referenceMap.id++;
|
|
841
|
-
let token = src[position];
|
|
861
|
+
let token = src[position$1];
|
|
842
862
|
let target;
|
|
843
863
|
// TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
|
|
844
864
|
// ahead past references to record structure definitions
|
|
@@ -915,7 +935,7 @@
|
|
|
915
935
|
return input
|
|
916
936
|
});
|
|
917
937
|
|
|
918
|
-
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
938
|
+
const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
919
939
|
const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
920
940
|
typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
921
941
|
typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
|
|
@@ -932,7 +952,7 @@
|
|
|
932
952
|
if (!littleEndian && bytesPerElement == 1)
|
|
933
953
|
continue
|
|
934
954
|
let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : 3;
|
|
935
|
-
currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine) ? (buffer) => {
|
|
955
|
+
currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
|
|
936
956
|
if (!TypedArray)
|
|
937
957
|
throw new Error('Could not find typed array for code ' + tag)
|
|
938
958
|
// we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
|
|
@@ -954,36 +974,36 @@
|
|
|
954
974
|
|
|
955
975
|
function readBundleExt() {
|
|
956
976
|
let length = readJustLength();
|
|
957
|
-
let bundlePosition = position + read();
|
|
977
|
+
let bundlePosition = position$1 + read();
|
|
958
978
|
for (let i = 2; i < length; i++) {
|
|
959
979
|
// skip past bundles that were already read
|
|
960
980
|
let bundleLength = readJustLength(); // this will increment position, so must add to position afterwards
|
|
961
|
-
position += bundleLength;
|
|
981
|
+
position$1 += bundleLength;
|
|
962
982
|
}
|
|
963
|
-
let dataPosition = position;
|
|
964
|
-
position = bundlePosition;
|
|
965
|
-
bundledStrings = [readStringJS(readJustLength()), readStringJS(readJustLength())];
|
|
966
|
-
bundledStrings.position0 = 0;
|
|
967
|
-
bundledStrings.position1 = 0;
|
|
968
|
-
bundledStrings.postBundlePosition = position;
|
|
969
|
-
position = dataPosition;
|
|
983
|
+
let dataPosition = position$1;
|
|
984
|
+
position$1 = bundlePosition;
|
|
985
|
+
bundledStrings$1 = [readStringJS(readJustLength()), readStringJS(readJustLength())];
|
|
986
|
+
bundledStrings$1.position0 = 0;
|
|
987
|
+
bundledStrings$1.position1 = 0;
|
|
988
|
+
bundledStrings$1.postBundlePosition = position$1;
|
|
989
|
+
position$1 = dataPosition;
|
|
970
990
|
return read()
|
|
971
991
|
}
|
|
972
992
|
|
|
973
993
|
function readJustLength() {
|
|
974
|
-
let token = src[position++] & 0x1f;
|
|
994
|
+
let token = src[position$1++] & 0x1f;
|
|
975
995
|
if (token > 0x17) {
|
|
976
996
|
switch (token) {
|
|
977
997
|
case 0x18:
|
|
978
|
-
token = src[position++];
|
|
998
|
+
token = src[position$1++];
|
|
979
999
|
break
|
|
980
1000
|
case 0x19:
|
|
981
|
-
token = dataView.getUint16(position);
|
|
982
|
-
position += 2;
|
|
1001
|
+
token = dataView.getUint16(position$1);
|
|
1002
|
+
position$1 += 2;
|
|
983
1003
|
break
|
|
984
1004
|
case 0x1a:
|
|
985
|
-
token = dataView.getUint32(position);
|
|
986
|
-
position += 4;
|
|
1005
|
+
token = dataView.getUint32(position$1);
|
|
1006
|
+
position$1 += 4;
|
|
987
1007
|
break
|
|
988
1008
|
}
|
|
989
1009
|
}
|
|
@@ -1009,12 +1029,12 @@
|
|
|
1009
1029
|
|
|
1010
1030
|
function saveState(callback) {
|
|
1011
1031
|
let savedSrcEnd = srcEnd;
|
|
1012
|
-
let savedPosition = position;
|
|
1032
|
+
let savedPosition = position$1;
|
|
1013
1033
|
let savedSrcStringStart = srcStringStart;
|
|
1014
1034
|
let savedSrcStringEnd = srcStringEnd;
|
|
1015
1035
|
let savedSrcString = srcString;
|
|
1016
1036
|
let savedReferenceMap = referenceMap;
|
|
1017
|
-
let savedBundledStrings = bundledStrings;
|
|
1037
|
+
let savedBundledStrings = bundledStrings$1;
|
|
1018
1038
|
|
|
1019
1039
|
// TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
|
|
1020
1040
|
let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
|
|
@@ -1023,12 +1043,12 @@
|
|
|
1023
1043
|
let savedSequentialMode = sequentialMode;
|
|
1024
1044
|
let value = callback();
|
|
1025
1045
|
srcEnd = savedSrcEnd;
|
|
1026
|
-
position = savedPosition;
|
|
1046
|
+
position$1 = savedPosition;
|
|
1027
1047
|
srcStringStart = savedSrcStringStart;
|
|
1028
1048
|
srcStringEnd = savedSrcStringEnd;
|
|
1029
1049
|
srcString = savedSrcString;
|
|
1030
1050
|
referenceMap = savedReferenceMap;
|
|
1031
|
-
bundledStrings = savedBundledStrings;
|
|
1051
|
+
bundledStrings$1 = savedBundledStrings;
|
|
1032
1052
|
src = savedSrc;
|
|
1033
1053
|
sequentialMode = savedSequentialMode;
|
|
1034
1054
|
currentStructures = savedStructures;
|
|
@@ -1042,7 +1062,7 @@
|
|
|
1042
1062
|
currentStructures = null;
|
|
1043
1063
|
}
|
|
1044
1064
|
|
|
1045
|
-
function addExtension(extension) {
|
|
1065
|
+
function addExtension$1(extension) {
|
|
1046
1066
|
currentExtensions[extension.tag] = extension.decode;
|
|
1047
1067
|
}
|
|
1048
1068
|
|
|
@@ -1070,16 +1090,18 @@
|
|
|
1070
1090
|
textEncoder = new TextEncoder();
|
|
1071
1091
|
} catch (error) {}
|
|
1072
1092
|
let extensions, extensionClasses;
|
|
1073
|
-
const
|
|
1074
|
-
const
|
|
1075
|
-
const
|
|
1093
|
+
const Buffer$1 = globalThis.Buffer;
|
|
1094
|
+
const hasNodeBuffer = typeof Buffer$1 !== 'undefined';
|
|
1095
|
+
const ByteArrayAllocate = hasNodeBuffer ? Buffer$1.allocUnsafeSlow : Uint8Array;
|
|
1096
|
+
const ByteArray = hasNodeBuffer ? Buffer$1 : Uint8Array;
|
|
1076
1097
|
const MAX_STRUCTURES = 0x100;
|
|
1077
1098
|
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1099
|
+
let throwOnIterable;
|
|
1078
1100
|
let target;
|
|
1079
1101
|
let targetView;
|
|
1080
|
-
let position
|
|
1102
|
+
let position = 0;
|
|
1081
1103
|
let safeEnd;
|
|
1082
|
-
let bundledStrings
|
|
1104
|
+
let bundledStrings = null;
|
|
1083
1105
|
const MAX_BUNDLE_SIZE = 0xf000;
|
|
1084
1106
|
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1085
1107
|
const RECORD_SYMBOL = Symbol('record-id');
|
|
@@ -1148,28 +1170,28 @@
|
|
|
1148
1170
|
if (!target) {
|
|
1149
1171
|
target = new ByteArrayAllocate(8192);
|
|
1150
1172
|
targetView = new DataView(target.buffer, 0, 8192);
|
|
1151
|
-
position
|
|
1173
|
+
position = 0;
|
|
1152
1174
|
}
|
|
1153
1175
|
safeEnd = target.length - 10;
|
|
1154
|
-
if (safeEnd - position
|
|
1176
|
+
if (safeEnd - position < 0x800) {
|
|
1155
1177
|
// don't start too close to the end,
|
|
1156
1178
|
target = new ByteArrayAllocate(target.length);
|
|
1157
1179
|
targetView = new DataView(target.buffer, 0, target.length);
|
|
1158
1180
|
safeEnd = target.length - 10;
|
|
1159
|
-
position
|
|
1181
|
+
position = 0;
|
|
1160
1182
|
} else if (encodeOptions === REUSE_BUFFER_MODE)
|
|
1161
|
-
position
|
|
1162
|
-
start = position
|
|
1183
|
+
position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
|
|
1184
|
+
start = position;
|
|
1163
1185
|
if (encoder.useSelfDescribedHeader) {
|
|
1164
|
-
targetView.setUint32(position
|
|
1165
|
-
position
|
|
1186
|
+
targetView.setUint32(position, 0xd9d9f700); // tag two byte, then self-descriptive tag
|
|
1187
|
+
position += 3;
|
|
1166
1188
|
}
|
|
1167
1189
|
referenceMap = encoder.structuredClone ? new Map() : null;
|
|
1168
1190
|
if (encoder.bundleStrings && typeof value !== 'string') {
|
|
1169
|
-
bundledStrings
|
|
1170
|
-
bundledStrings
|
|
1191
|
+
bundledStrings = [];
|
|
1192
|
+
bundledStrings.size = Infinity; // force a new bundle start on first string
|
|
1171
1193
|
} else
|
|
1172
|
-
bundledStrings
|
|
1194
|
+
bundledStrings = null;
|
|
1173
1195
|
|
|
1174
1196
|
sharedStructures = encoder.structures;
|
|
1175
1197
|
if (sharedStructures) {
|
|
@@ -1225,8 +1247,8 @@
|
|
|
1225
1247
|
packedValues.samplingPackedValues = samplingPackedValues;
|
|
1226
1248
|
findRepetitiveStrings(value, packedValues);
|
|
1227
1249
|
if (packedValues.values.length > 0) {
|
|
1228
|
-
target[position
|
|
1229
|
-
target[position
|
|
1250
|
+
target[position++] = 0xd8; // one-byte tag
|
|
1251
|
+
target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1230
1252
|
writeArrayHeader(4);
|
|
1231
1253
|
let valuesArray = packedValues.values;
|
|
1232
1254
|
encode(valuesArray);
|
|
@@ -1238,27 +1260,30 @@
|
|
|
1238
1260
|
}
|
|
1239
1261
|
}
|
|
1240
1262
|
}
|
|
1263
|
+
throwOnIterable = encodeOptions & THROW_ON_ITERABLE;
|
|
1241
1264
|
try {
|
|
1265
|
+
if (throwOnIterable)
|
|
1266
|
+
return;
|
|
1242
1267
|
encode(value);
|
|
1243
|
-
if (bundledStrings
|
|
1268
|
+
if (bundledStrings) {
|
|
1244
1269
|
writeBundles(start, encode);
|
|
1245
1270
|
}
|
|
1246
|
-
encoder.offset = position
|
|
1271
|
+
encoder.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1247
1272
|
if (referenceMap && referenceMap.idsToInsert) {
|
|
1248
|
-
position
|
|
1249
|
-
if (position
|
|
1250
|
-
makeRoom(position
|
|
1251
|
-
encoder.offset = position
|
|
1252
|
-
let serialized = insertIds(target.subarray(start, position
|
|
1273
|
+
position += referenceMap.idsToInsert.length * 2;
|
|
1274
|
+
if (position > safeEnd)
|
|
1275
|
+
makeRoom(position);
|
|
1276
|
+
encoder.offset = position;
|
|
1277
|
+
let serialized = insertIds(target.subarray(start, position), referenceMap.idsToInsert);
|
|
1253
1278
|
referenceMap = null;
|
|
1254
1279
|
return serialized
|
|
1255
1280
|
}
|
|
1256
1281
|
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1257
1282
|
target.start = start;
|
|
1258
|
-
target.end = position
|
|
1283
|
+
target.end = position;
|
|
1259
1284
|
return target
|
|
1260
1285
|
}
|
|
1261
|
-
return target.subarray(start, position
|
|
1286
|
+
return target.subarray(start, position) // position can change if we call encode again in saveShared, so we get the buffer now
|
|
1262
1287
|
} finally {
|
|
1263
1288
|
if (sharedStructures) {
|
|
1264
1289
|
if (serializationsSinceTransitionRebuild < 10)
|
|
@@ -1285,13 +1310,13 @@
|
|
|
1285
1310
|
encoder.structures = encoder.structures.slice(0, maxSharedStructures);
|
|
1286
1311
|
}
|
|
1287
1312
|
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1288
|
-
let returnBuffer = target.subarray(start, position
|
|
1313
|
+
let returnBuffer = target.subarray(start, position);
|
|
1289
1314
|
if (encoder.updateSharedData() === false)
|
|
1290
1315
|
return encoder.encode(value) // re-encode if it fails
|
|
1291
1316
|
return returnBuffer
|
|
1292
1317
|
}
|
|
1293
1318
|
if (encodeOptions & RESET_BUFFER_MODE)
|
|
1294
|
-
position
|
|
1319
|
+
position = start;
|
|
1295
1320
|
}
|
|
1296
1321
|
};
|
|
1297
1322
|
this.findCommonStringsToPack = () => {
|
|
@@ -1315,8 +1340,8 @@
|
|
|
1315
1340
|
}
|
|
1316
1341
|
};
|
|
1317
1342
|
const encode = (value) => {
|
|
1318
|
-
if (position
|
|
1319
|
-
target = makeRoom(position
|
|
1343
|
+
if (position > safeEnd)
|
|
1344
|
+
target = makeRoom(position);
|
|
1320
1345
|
|
|
1321
1346
|
var type = typeof value;
|
|
1322
1347
|
var length;
|
|
@@ -1325,9 +1350,9 @@
|
|
|
1325
1350
|
let packedPosition = packedObjectMap[value];
|
|
1326
1351
|
if (packedPosition >= 0) {
|
|
1327
1352
|
if (packedPosition < 16)
|
|
1328
|
-
target[position
|
|
1353
|
+
target[position++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1329
1354
|
else {
|
|
1330
|
-
target[position
|
|
1355
|
+
target[position++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1331
1356
|
if (packedPosition & 1)
|
|
1332
1357
|
encode((15 - packedPosition) >> 1);
|
|
1333
1358
|
else
|
|
@@ -1359,30 +1384,30 @@
|
|
|
1359
1384
|
}
|
|
1360
1385
|
}
|
|
1361
1386
|
let strLength = value.length;
|
|
1362
|
-
if (bundledStrings
|
|
1363
|
-
if ((bundledStrings
|
|
1387
|
+
if (bundledStrings && strLength >= 4 && strLength < 0x400) {
|
|
1388
|
+
if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
1364
1389
|
let extStart;
|
|
1365
|
-
let maxBytes = (bundledStrings
|
|
1366
|
-
if (position
|
|
1367
|
-
target = makeRoom(position
|
|
1368
|
-
target[position
|
|
1369
|
-
target[position
|
|
1370
|
-
target[position
|
|
1390
|
+
let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
|
|
1391
|
+
if (position + maxBytes > safeEnd)
|
|
1392
|
+
target = makeRoom(position + maxBytes);
|
|
1393
|
+
target[position++] = 0xd9; // tag 16-bit
|
|
1394
|
+
target[position++] = 0xdf; // tag 0xdff9
|
|
1395
|
+
target[position++] = 0xf9;
|
|
1371
1396
|
// TODO: If we only have one bundle with any string data, only write one string bundle
|
|
1372
|
-
target[position
|
|
1373
|
-
target[position
|
|
1374
|
-
extStart = position
|
|
1375
|
-
position
|
|
1376
|
-
if (bundledStrings
|
|
1397
|
+
target[position++] = bundledStrings.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
|
|
1398
|
+
target[position++] = 0x1a; // 32-bit unsigned int
|
|
1399
|
+
extStart = position - start;
|
|
1400
|
+
position += 4; // reserve for writing bundle reference
|
|
1401
|
+
if (bundledStrings.position) {
|
|
1377
1402
|
writeBundles(start, encode); // write the last bundles
|
|
1378
1403
|
}
|
|
1379
|
-
bundledStrings
|
|
1380
|
-
bundledStrings
|
|
1381
|
-
bundledStrings
|
|
1404
|
+
bundledStrings = ['', '']; // create new ones
|
|
1405
|
+
bundledStrings.size = 0;
|
|
1406
|
+
bundledStrings.position = extStart;
|
|
1382
1407
|
}
|
|
1383
1408
|
let twoByte = hasNonLatin.test(value);
|
|
1384
|
-
bundledStrings
|
|
1385
|
-
target[position
|
|
1409
|
+
bundledStrings[twoByte ? 0 : 1] += value;
|
|
1410
|
+
target[position++] = twoByte ? 0xce : 0xcf;
|
|
1386
1411
|
encode(strLength);
|
|
1387
1412
|
return
|
|
1388
1413
|
}
|
|
@@ -1398,11 +1423,11 @@
|
|
|
1398
1423
|
headerSize = 5;
|
|
1399
1424
|
}
|
|
1400
1425
|
let maxBytes = strLength * 3;
|
|
1401
|
-
if (position
|
|
1402
|
-
target = makeRoom(position
|
|
1426
|
+
if (position + maxBytes > safeEnd)
|
|
1427
|
+
target = makeRoom(position + maxBytes);
|
|
1403
1428
|
|
|
1404
1429
|
if (strLength < 0x40 || !encodeUtf8) {
|
|
1405
|
-
let i, c1, c2, strPosition = position
|
|
1430
|
+
let i, c1, c2, strPosition = position + headerSize;
|
|
1406
1431
|
for (i = 0; i < strLength; i++) {
|
|
1407
1432
|
c1 = value.charCodeAt(i);
|
|
1408
1433
|
if (c1 < 0x80) {
|
|
@@ -1426,105 +1451,105 @@
|
|
|
1426
1451
|
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1427
1452
|
}
|
|
1428
1453
|
}
|
|
1429
|
-
length = strPosition - position
|
|
1454
|
+
length = strPosition - position - headerSize;
|
|
1430
1455
|
} else {
|
|
1431
|
-
length = encodeUtf8(value, position
|
|
1456
|
+
length = encodeUtf8(value, position + headerSize, maxBytes);
|
|
1432
1457
|
}
|
|
1433
1458
|
|
|
1434
1459
|
if (length < 0x18) {
|
|
1435
|
-
target[position
|
|
1460
|
+
target[position++] = 0x60 | length;
|
|
1436
1461
|
} else if (length < 0x100) {
|
|
1437
1462
|
if (headerSize < 2) {
|
|
1438
|
-
target.copyWithin(position
|
|
1463
|
+
target.copyWithin(position + 2, position + 1, position + 1 + length);
|
|
1439
1464
|
}
|
|
1440
|
-
target[position
|
|
1441
|
-
target[position
|
|
1465
|
+
target[position++] = 0x78;
|
|
1466
|
+
target[position++] = length;
|
|
1442
1467
|
} else if (length < 0x10000) {
|
|
1443
1468
|
if (headerSize < 3) {
|
|
1444
|
-
target.copyWithin(position
|
|
1469
|
+
target.copyWithin(position + 3, position + 2, position + 2 + length);
|
|
1445
1470
|
}
|
|
1446
|
-
target[position
|
|
1447
|
-
target[position
|
|
1448
|
-
target[position
|
|
1471
|
+
target[position++] = 0x79;
|
|
1472
|
+
target[position++] = length >> 8;
|
|
1473
|
+
target[position++] = length & 0xff;
|
|
1449
1474
|
} else {
|
|
1450
1475
|
if (headerSize < 5) {
|
|
1451
|
-
target.copyWithin(position
|
|
1476
|
+
target.copyWithin(position + 5, position + 3, position + 3 + length);
|
|
1452
1477
|
}
|
|
1453
|
-
target[position
|
|
1454
|
-
targetView.setUint32(position
|
|
1455
|
-
position
|
|
1478
|
+
target[position++] = 0x7a;
|
|
1479
|
+
targetView.setUint32(position, length);
|
|
1480
|
+
position += 4;
|
|
1456
1481
|
}
|
|
1457
|
-
position
|
|
1482
|
+
position += length;
|
|
1458
1483
|
} else if (type === 'number') {
|
|
1459
1484
|
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1460
1485
|
// positive uint
|
|
1461
1486
|
if (value < 0x18) {
|
|
1462
|
-
target[position
|
|
1487
|
+
target[position++] = value;
|
|
1463
1488
|
} else if (value < 0x100) {
|
|
1464
|
-
target[position
|
|
1465
|
-
target[position
|
|
1489
|
+
target[position++] = 0x18;
|
|
1490
|
+
target[position++] = value;
|
|
1466
1491
|
} else if (value < 0x10000) {
|
|
1467
|
-
target[position
|
|
1468
|
-
target[position
|
|
1469
|
-
target[position
|
|
1492
|
+
target[position++] = 0x19;
|
|
1493
|
+
target[position++] = value >> 8;
|
|
1494
|
+
target[position++] = value & 0xff;
|
|
1470
1495
|
} else {
|
|
1471
|
-
target[position
|
|
1472
|
-
targetView.setUint32(position
|
|
1473
|
-
position
|
|
1496
|
+
target[position++] = 0x1a;
|
|
1497
|
+
targetView.setUint32(position, value);
|
|
1498
|
+
position += 4;
|
|
1474
1499
|
}
|
|
1475
1500
|
} else if (value >> 0 === value) { // negative integer
|
|
1476
1501
|
if (value >= -0x18) {
|
|
1477
|
-
target[position
|
|
1502
|
+
target[position++] = 0x1f - value;
|
|
1478
1503
|
} else if (value >= -0x100) {
|
|
1479
|
-
target[position
|
|
1480
|
-
target[position
|
|
1504
|
+
target[position++] = 0x38;
|
|
1505
|
+
target[position++] = ~value;
|
|
1481
1506
|
} else if (value >= -0x10000) {
|
|
1482
|
-
target[position
|
|
1483
|
-
targetView.setUint16(position
|
|
1484
|
-
position
|
|
1507
|
+
target[position++] = 0x39;
|
|
1508
|
+
targetView.setUint16(position, ~value);
|
|
1509
|
+
position += 2;
|
|
1485
1510
|
} else {
|
|
1486
|
-
target[position
|
|
1487
|
-
targetView.setUint32(position
|
|
1488
|
-
position
|
|
1511
|
+
target[position++] = 0x3a;
|
|
1512
|
+
targetView.setUint32(position, ~value);
|
|
1513
|
+
position += 4;
|
|
1489
1514
|
}
|
|
1490
1515
|
} else {
|
|
1491
1516
|
let useFloat32;
|
|
1492
1517
|
if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
|
|
1493
|
-
target[position
|
|
1494
|
-
targetView.setFloat32(position
|
|
1518
|
+
target[position++] = 0xfa;
|
|
1519
|
+
targetView.setFloat32(position, value);
|
|
1495
1520
|
let xShifted;
|
|
1496
1521
|
if (useFloat32 < 4 ||
|
|
1497
1522
|
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1498
|
-
((xShifted = value * mult10[((target[position
|
|
1499
|
-
position
|
|
1523
|
+
((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
|
|
1524
|
+
position += 4;
|
|
1500
1525
|
return
|
|
1501
1526
|
} else
|
|
1502
|
-
position
|
|
1527
|
+
position--; // move back into position for writing a double
|
|
1503
1528
|
}
|
|
1504
|
-
target[position
|
|
1505
|
-
targetView.setFloat64(position
|
|
1506
|
-
position
|
|
1529
|
+
target[position++] = 0xfb;
|
|
1530
|
+
targetView.setFloat64(position, value);
|
|
1531
|
+
position += 8;
|
|
1507
1532
|
}
|
|
1508
1533
|
} else if (type === 'object') {
|
|
1509
1534
|
if (!value)
|
|
1510
|
-
target[position
|
|
1535
|
+
target[position++] = 0xf6;
|
|
1511
1536
|
else {
|
|
1512
1537
|
if (referenceMap) {
|
|
1513
1538
|
let referee = referenceMap.get(value);
|
|
1514
1539
|
if (referee) {
|
|
1515
|
-
target[position
|
|
1516
|
-
target[position
|
|
1517
|
-
target[position
|
|
1540
|
+
target[position++] = 0xd8;
|
|
1541
|
+
target[position++] = 29; // http://cbor.schmorp.de/value-sharing
|
|
1542
|
+
target[position++] = 0x19; // 16-bit uint
|
|
1518
1543
|
if (!referee.references) {
|
|
1519
1544
|
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
|
|
1520
1545
|
referee.references = [];
|
|
1521
1546
|
idsToInsert.push(referee);
|
|
1522
1547
|
}
|
|
1523
|
-
referee.references.push(position
|
|
1524
|
-
position
|
|
1548
|
+
referee.references.push(position - start);
|
|
1549
|
+
position += 2; // TODO: also support 32-bit
|
|
1525
1550
|
return
|
|
1526
1551
|
} else
|
|
1527
|
-
referenceMap.set(value, { offset: position
|
|
1552
|
+
referenceMap.set(value, { offset: position - start });
|
|
1528
1553
|
}
|
|
1529
1554
|
let constructor = value.constructor;
|
|
1530
1555
|
if (constructor === Object) {
|
|
@@ -1532,7 +1557,7 @@
|
|
|
1532
1557
|
} else if (constructor === Array) {
|
|
1533
1558
|
length = value.length;
|
|
1534
1559
|
if (length < 0x18) {
|
|
1535
|
-
target[position
|
|
1560
|
+
target[position++] = 0x80 | length;
|
|
1536
1561
|
} else {
|
|
1537
1562
|
writeArrayHeader(length);
|
|
1538
1563
|
}
|
|
@@ -1542,24 +1567,24 @@
|
|
|
1542
1567
|
} else if (constructor === Map) {
|
|
1543
1568
|
if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
|
|
1544
1569
|
// use Tag 259 (https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec--explicit-maps.md) for maps if the user wants it that way
|
|
1545
|
-
target[position
|
|
1546
|
-
target[position
|
|
1547
|
-
target[position
|
|
1570
|
+
target[position++] = 0xd9;
|
|
1571
|
+
target[position++] = 1;
|
|
1572
|
+
target[position++] = 3;
|
|
1548
1573
|
}
|
|
1549
1574
|
length = value.size;
|
|
1550
1575
|
if (length < 0x18) {
|
|
1551
|
-
target[position
|
|
1576
|
+
target[position++] = 0xa0 | length;
|
|
1552
1577
|
} else if (length < 0x100) {
|
|
1553
|
-
target[position
|
|
1554
|
-
target[position
|
|
1578
|
+
target[position++] = 0xb8;
|
|
1579
|
+
target[position++] = length;
|
|
1555
1580
|
} else if (length < 0x10000) {
|
|
1556
|
-
target[position
|
|
1557
|
-
target[position
|
|
1558
|
-
target[position
|
|
1581
|
+
target[position++] = 0xb9;
|
|
1582
|
+
target[position++] = length >> 8;
|
|
1583
|
+
target[position++] = length & 0xff;
|
|
1559
1584
|
} else {
|
|
1560
|
-
target[position
|
|
1561
|
-
targetView.setUint32(position
|
|
1562
|
-
position
|
|
1585
|
+
target[position++] = 0xba;
|
|
1586
|
+
targetView.setUint32(position, length);
|
|
1587
|
+
position += 4;
|
|
1563
1588
|
}
|
|
1564
1589
|
if (encoder.keyMap) {
|
|
1565
1590
|
for (let [ key, entryValue ] of value) {
|
|
@@ -1581,58 +1606,68 @@
|
|
|
1581
1606
|
if (tag == undefined)
|
|
1582
1607
|
tag = extension.getTag && extension.getTag.call(this, value);
|
|
1583
1608
|
if (tag < 0x18) {
|
|
1584
|
-
target[position
|
|
1609
|
+
target[position++] = 0xc0 | tag;
|
|
1585
1610
|
} else if (tag < 0x100) {
|
|
1586
|
-
target[position
|
|
1587
|
-
target[position
|
|
1611
|
+
target[position++] = 0xd8;
|
|
1612
|
+
target[position++] = tag;
|
|
1588
1613
|
} else if (tag < 0x10000) {
|
|
1589
|
-
target[position
|
|
1590
|
-
target[position
|
|
1591
|
-
target[position
|
|
1614
|
+
target[position++] = 0xd9;
|
|
1615
|
+
target[position++] = tag >> 8;
|
|
1616
|
+
target[position++] = tag & 0xff;
|
|
1592
1617
|
} else if (tag > -1) {
|
|
1593
|
-
target[position
|
|
1594
|
-
targetView.setUint32(position
|
|
1595
|
-
position
|
|
1618
|
+
target[position++] = 0xda;
|
|
1619
|
+
targetView.setUint32(position, tag);
|
|
1620
|
+
position += 4;
|
|
1596
1621
|
} // else undefined, don't write tag
|
|
1597
1622
|
extension.encode.call(this, value, encode, makeRoom);
|
|
1598
1623
|
return
|
|
1599
1624
|
}
|
|
1600
1625
|
}
|
|
1601
1626
|
if (value[Symbol.iterator]) {
|
|
1602
|
-
|
|
1627
|
+
if (throwOnIterable) {
|
|
1628
|
+
let error = new Error('Iterable should be serialized as iterator');
|
|
1629
|
+
error.iteratorNotHandled = true;
|
|
1630
|
+
throw error;
|
|
1631
|
+
}
|
|
1632
|
+
target[position++] = 0x9f; // indefinite length array
|
|
1603
1633
|
for (let entry of value) {
|
|
1604
1634
|
encode(entry);
|
|
1605
1635
|
}
|
|
1606
|
-
target[position
|
|
1636
|
+
target[position++] = 0xff; // stop-code
|
|
1607
1637
|
return
|
|
1608
1638
|
}
|
|
1639
|
+
if (value[Symbol.asyncIterator] || isBlob(value)) {
|
|
1640
|
+
let error = new Error('Iterable/blob should be serialized as iterator');
|
|
1641
|
+
error.iteratorNotHandled = true;
|
|
1642
|
+
throw error;
|
|
1643
|
+
}
|
|
1609
1644
|
// no extension found, write as object
|
|
1610
1645
|
writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
|
|
1611
1646
|
}
|
|
1612
1647
|
}
|
|
1613
1648
|
} else if (type === 'boolean') {
|
|
1614
|
-
target[position
|
|
1649
|
+
target[position++] = value ? 0xf5 : 0xf4;
|
|
1615
1650
|
} else if (type === 'bigint') {
|
|
1616
1651
|
if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
|
|
1617
1652
|
// use an unsigned int as long as it fits
|
|
1618
|
-
target[position
|
|
1619
|
-
targetView.setBigUint64(position
|
|
1653
|
+
target[position++] = 0x1b;
|
|
1654
|
+
targetView.setBigUint64(position, value);
|
|
1620
1655
|
} else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
|
|
1621
1656
|
// if we can fit an unsigned int, use that
|
|
1622
|
-
target[position
|
|
1623
|
-
targetView.setBigUint64(position
|
|
1657
|
+
target[position++] = 0x3b;
|
|
1658
|
+
targetView.setBigUint64(position, -value - BigInt(1));
|
|
1624
1659
|
} else {
|
|
1625
1660
|
// overflow
|
|
1626
1661
|
if (this.largeBigIntToFloat) {
|
|
1627
|
-
target[position
|
|
1628
|
-
targetView.setFloat64(position
|
|
1662
|
+
target[position++] = 0xfb;
|
|
1663
|
+
targetView.setFloat64(position, Number(value));
|
|
1629
1664
|
} else {
|
|
1630
1665
|
throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
|
|
1631
1666
|
}
|
|
1632
1667
|
}
|
|
1633
|
-
position
|
|
1668
|
+
position += 8;
|
|
1634
1669
|
} else if (type === 'undefined') {
|
|
1635
|
-
target[position
|
|
1670
|
+
target[position++] = 0xf7;
|
|
1636
1671
|
} else {
|
|
1637
1672
|
throw new Error('Unknown type: ' + type)
|
|
1638
1673
|
}
|
|
@@ -1644,18 +1679,18 @@
|
|
|
1644
1679
|
let vals = Object.values(object);
|
|
1645
1680
|
let length = keys.length;
|
|
1646
1681
|
if (length < 0x18) {
|
|
1647
|
-
target[position
|
|
1682
|
+
target[position++] = 0xa0 | length;
|
|
1648
1683
|
} else if (length < 0x100) {
|
|
1649
|
-
target[position
|
|
1650
|
-
target[position
|
|
1684
|
+
target[position++] = 0xb8;
|
|
1685
|
+
target[position++] = length;
|
|
1651
1686
|
} else if (length < 0x10000) {
|
|
1652
|
-
target[position
|
|
1653
|
-
target[position
|
|
1654
|
-
target[position
|
|
1687
|
+
target[position++] = 0xb9;
|
|
1688
|
+
target[position++] = length >> 8;
|
|
1689
|
+
target[position++] = length & 0xff;
|
|
1655
1690
|
} else {
|
|
1656
|
-
target[position
|
|
1657
|
-
targetView.setUint32(position
|
|
1658
|
-
position
|
|
1691
|
+
target[position++] = 0xba;
|
|
1692
|
+
targetView.setUint32(position, length);
|
|
1693
|
+
position += 4;
|
|
1659
1694
|
}
|
|
1660
1695
|
if (encoder.keyMap) {
|
|
1661
1696
|
for (let i = 0; i < length; i++) {
|
|
@@ -1670,9 +1705,9 @@
|
|
|
1670
1705
|
}
|
|
1671
1706
|
} :
|
|
1672
1707
|
(object, safePrototype) => {
|
|
1673
|
-
target[position
|
|
1674
|
-
let objectOffset = position
|
|
1675
|
-
position
|
|
1708
|
+
target[position++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
|
|
1709
|
+
let objectOffset = position - start;
|
|
1710
|
+
position += 2;
|
|
1676
1711
|
let size = 0;
|
|
1677
1712
|
if (encoder.keyMap) {
|
|
1678
1713
|
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
@@ -1725,9 +1760,9 @@
|
|
|
1725
1760
|
let recordId = transition[RECORD_SYMBOL];
|
|
1726
1761
|
if (recordId !== undefined) {
|
|
1727
1762
|
recordId &= 0xffff;
|
|
1728
|
-
target[position
|
|
1729
|
-
target[position
|
|
1730
|
-
target[position
|
|
1763
|
+
target[position++] = 0xd9;
|
|
1764
|
+
target[position++] = (recordId >> 8) | 0xe0;
|
|
1765
|
+
target[position++] = recordId & 0xff;
|
|
1731
1766
|
} else {
|
|
1732
1767
|
if (!keys)
|
|
1733
1768
|
keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
|
|
@@ -1745,9 +1780,9 @@
|
|
|
1745
1780
|
}
|
|
1746
1781
|
structures[recordId] = keys;
|
|
1747
1782
|
if (recordId < maxSharedStructures) {
|
|
1748
|
-
target[position
|
|
1749
|
-
target[position
|
|
1750
|
-
target[position
|
|
1783
|
+
target[position++] = 0xd9;
|
|
1784
|
+
target[position++] = (recordId >> 8) | 0xe0;
|
|
1785
|
+
target[position++] = recordId & 0xff;
|
|
1751
1786
|
transition = structures.transitions;
|
|
1752
1787
|
for (let i = 0; i < length; i++) {
|
|
1753
1788
|
if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
|
|
@@ -1758,8 +1793,8 @@
|
|
|
1758
1793
|
hasSharedUpdate = true;
|
|
1759
1794
|
} else {
|
|
1760
1795
|
transition[RECORD_SYMBOL] = recordId;
|
|
1761
|
-
targetView.setUint32(position
|
|
1762
|
-
position
|
|
1796
|
+
targetView.setUint32(position, 0xd9dfff00); // tag two byte, then record definition id
|
|
1797
|
+
position += 3;
|
|
1763
1798
|
if (newTransitions)
|
|
1764
1799
|
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
1765
1800
|
// record the removal of the id, we can maintain our shared structure
|
|
@@ -1769,15 +1804,19 @@
|
|
|
1769
1804
|
writeArrayHeader(length + 2);
|
|
1770
1805
|
encode(0xe000 + recordId);
|
|
1771
1806
|
encode(keys);
|
|
1772
|
-
|
|
1807
|
+
if (safePrototype === null) return; // special exit for iterator
|
|
1808
|
+
for (let key in object)
|
|
1809
|
+
if (safePrototype || object.hasOwnProperty(key))
|
|
1810
|
+
encode(object[key]);
|
|
1773
1811
|
return
|
|
1774
1812
|
}
|
|
1775
1813
|
}
|
|
1776
1814
|
if (length < 0x18) { // write the array header
|
|
1777
|
-
target[position
|
|
1815
|
+
target[position++] = 0x80 | length;
|
|
1778
1816
|
} else {
|
|
1779
1817
|
writeArrayHeader(length);
|
|
1780
1818
|
}
|
|
1819
|
+
if (safePrototype === null) return; // special exit for iterator
|
|
1781
1820
|
for (let key in object)
|
|
1782
1821
|
if (safePrototype || object.hasOwnProperty(key))
|
|
1783
1822
|
encode(object[key]);
|
|
@@ -1798,17 +1837,142 @@
|
|
|
1798
1837
|
target.copy(newBuffer, 0, start, end);
|
|
1799
1838
|
else
|
|
1800
1839
|
newBuffer.set(target.slice(start, end));
|
|
1801
|
-
position
|
|
1840
|
+
position -= start;
|
|
1802
1841
|
start = 0;
|
|
1803
1842
|
safeEnd = newBuffer.length - 10;
|
|
1804
1843
|
return target = newBuffer
|
|
1805
1844
|
};
|
|
1845
|
+
let chunkThreshold = 100;
|
|
1846
|
+
let continuedChunkThreshold = 1000;
|
|
1847
|
+
this.encodeAsIterable = function(value, options) {
|
|
1848
|
+
return startEncoding(value, options, encodeObjectAsIterable);
|
|
1849
|
+
};
|
|
1850
|
+
this.encodeAsAsyncIterable = function(value, options) {
|
|
1851
|
+
return startEncoding(value, options, encodeObjectAsAsyncIterable);
|
|
1852
|
+
};
|
|
1853
|
+
|
|
1854
|
+
function* encodeObjectAsIterable(object, iterateProperties, finalIterable) {
|
|
1855
|
+
let constructor = object.constructor;
|
|
1856
|
+
if (constructor === Object) {
|
|
1857
|
+
let useRecords = encoder.useRecords !== false;
|
|
1858
|
+
if (useRecords)
|
|
1859
|
+
writeObject(object, null); // write the record identifier
|
|
1860
|
+
else
|
|
1861
|
+
writeEntityLength(Object.keys(object).length, 0xa0);
|
|
1862
|
+
for (let key in object) {
|
|
1863
|
+
let value = object[key];
|
|
1864
|
+
if (!useRecords) encode(key);
|
|
1865
|
+
if (value && typeof value === 'object') {
|
|
1866
|
+
if (iterateProperties[key])
|
|
1867
|
+
yield* encodeObjectAsIterable(value, iterateProperties[key]);
|
|
1868
|
+
else
|
|
1869
|
+
yield* tryEncode(value, iterateProperties, key);
|
|
1870
|
+
} else encode(value);
|
|
1871
|
+
}
|
|
1872
|
+
} else if (constructor === Array) {
|
|
1873
|
+
let length = object.length;
|
|
1874
|
+
writeArrayHeader(length);
|
|
1875
|
+
for (let i = 0; i < length; i++) {
|
|
1876
|
+
let value = object[i];
|
|
1877
|
+
if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
|
|
1878
|
+
if (iterateProperties.element)
|
|
1879
|
+
yield* encodeObjectAsIterable(value, iterateProperties.element);
|
|
1880
|
+
else
|
|
1881
|
+
yield* tryEncode(value, iterateProperties, 'element');
|
|
1882
|
+
} else encode(value);
|
|
1883
|
+
}
|
|
1884
|
+
} else if (object[Symbol.iterator]) {
|
|
1885
|
+
target[position++] = 0x9f; // start indefinite array
|
|
1886
|
+
for (let value of object) {
|
|
1887
|
+
if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
|
|
1888
|
+
if (iterateProperties.element)
|
|
1889
|
+
yield* encodeObjectAsIterable(value, iterateProperties.element);
|
|
1890
|
+
else
|
|
1891
|
+
yield* tryEncode(value, iterateProperties, 'element');
|
|
1892
|
+
} else encode(value);
|
|
1893
|
+
}
|
|
1894
|
+
target[position++] = 0xff; // stop byte
|
|
1895
|
+
} else if (isBlob(object)){
|
|
1896
|
+
writeEntityLength(object.size, 0x40); // encode as binary data
|
|
1897
|
+
yield target.subarray(start, position);
|
|
1898
|
+
yield object; // directly return blobs, they have to be encoded asynchronously
|
|
1899
|
+
restartEncoding();
|
|
1900
|
+
} else if (object[Symbol.asyncIterator]) {
|
|
1901
|
+
target[position++] = 0x9f; // start indefinite array
|
|
1902
|
+
yield target.subarray(start, position);
|
|
1903
|
+
yield object; // directly return async iterators, they have to be encoded asynchronously
|
|
1904
|
+
restartEncoding();
|
|
1905
|
+
target[position++] = 0xff; // stop byte
|
|
1906
|
+
} else {
|
|
1907
|
+
encode(object);
|
|
1908
|
+
}
|
|
1909
|
+
if (finalIterable && position > start) yield target.subarray(start, position);
|
|
1910
|
+
else if (position - start > chunkThreshold) {
|
|
1911
|
+
yield target.subarray(start, position);
|
|
1912
|
+
restartEncoding();
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
function* tryEncode(value, iterateProperties, key) {
|
|
1916
|
+
let restart = position - start;
|
|
1917
|
+
try {
|
|
1918
|
+
encode(value);
|
|
1919
|
+
if (position - start > chunkThreshold) {
|
|
1920
|
+
yield target.subarray(start, position);
|
|
1921
|
+
restartEncoding();
|
|
1922
|
+
}
|
|
1923
|
+
} catch (error) {
|
|
1924
|
+
if (error.iteratorNotHandled) {
|
|
1925
|
+
iterateProperties[key] = {};
|
|
1926
|
+
position = start + restart; // restart our position so we don't have partial data from last encode
|
|
1927
|
+
yield* encodeObjectAsIterable.call(this, value, iterateProperties[key]);
|
|
1928
|
+
} else throw error;
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
function restartEncoding() {
|
|
1932
|
+
chunkThreshold = continuedChunkThreshold;
|
|
1933
|
+
encoder.encode(null, THROW_ON_ITERABLE); // restart encoding
|
|
1934
|
+
}
|
|
1935
|
+
function startEncoding(value, options, encodeIterable) {
|
|
1936
|
+
if (options && options.chunkThreshold) // explicitly specified chunk sizes
|
|
1937
|
+
chunkThreshold = continuedChunkThreshold = options.chunkThreshold;
|
|
1938
|
+
else // we start with a smaller threshold to get initial bytes sent quickly
|
|
1939
|
+
chunkThreshold = 100;
|
|
1940
|
+
if (value && typeof value === 'object') {
|
|
1941
|
+
encoder.encode(null, THROW_ON_ITERABLE); // start encoding
|
|
1942
|
+
return encodeIterable(value, encoder.iterateProperties || (encoder.iterateProperties = {}), true);
|
|
1943
|
+
}
|
|
1944
|
+
return [encoder.encode(value)];
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
async function* encodeObjectAsAsyncIterable(value, iterateProperties) {
|
|
1948
|
+
for (let encodedValue of encodeObjectAsIterable(value, iterateProperties, true)) {
|
|
1949
|
+
let constructor = encodedValue.constructor;
|
|
1950
|
+
if (constructor === ByteArray || constructor === Uint8Array)
|
|
1951
|
+
yield encodedValue;
|
|
1952
|
+
else if (isBlob(encodedValue)) {
|
|
1953
|
+
let reader = encodedValue.stream().getReader();
|
|
1954
|
+
let next;
|
|
1955
|
+
while (!(next = await reader.read()).done) {
|
|
1956
|
+
yield next.value;
|
|
1957
|
+
}
|
|
1958
|
+
} else if (encodedValue[Symbol.asyncIterator]) {
|
|
1959
|
+
for await (let asyncValue of encodedValue) {
|
|
1960
|
+
restartEncoding();
|
|
1961
|
+
if (asyncValue)
|
|
1962
|
+
yield* encodeObjectAsAsyncIterable(asyncValue, iterateProperties.async || (iterateProperties.async = {}));
|
|
1963
|
+
else yield encoder.encode(asyncValue);
|
|
1964
|
+
}
|
|
1965
|
+
} else {
|
|
1966
|
+
yield encodedValue;
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1806
1970
|
}
|
|
1807
1971
|
useBuffer(buffer) {
|
|
1808
1972
|
// this means we are finished using our own buffer and we can write over it safely
|
|
1809
1973
|
target = buffer;
|
|
1810
1974
|
targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
|
|
1811
|
-
position
|
|
1975
|
+
position = 0;
|
|
1812
1976
|
}
|
|
1813
1977
|
clearSharedData() {
|
|
1814
1978
|
if (this.structures)
|
|
@@ -1838,6 +2002,23 @@
|
|
|
1838
2002
|
return saveResults
|
|
1839
2003
|
}
|
|
1840
2004
|
}
|
|
2005
|
+
function writeEntityLength(length, majorValue) {
|
|
2006
|
+
if (length < 0x18)
|
|
2007
|
+
target[position++] = majorValue | length;
|
|
2008
|
+
else if (length < 0x100) {
|
|
2009
|
+
target[position++] = majorValue | 0x18;
|
|
2010
|
+
target[position++] = length;
|
|
2011
|
+
} else if (length < 0x10000) {
|
|
2012
|
+
target[position++] = majorValue | 0x19;
|
|
2013
|
+
target[position++] = length >> 8;
|
|
2014
|
+
target[position++] = length & 0xff;
|
|
2015
|
+
} else {
|
|
2016
|
+
target[position++] = majorValue | 0x1a;
|
|
2017
|
+
targetView.setUint32(position, length);
|
|
2018
|
+
position += 4;
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
}
|
|
1841
2022
|
class SharedData {
|
|
1842
2023
|
constructor(structures, values, version) {
|
|
1843
2024
|
this.structures = structures;
|
|
@@ -1848,21 +2029,28 @@
|
|
|
1848
2029
|
|
|
1849
2030
|
function writeArrayHeader(length) {
|
|
1850
2031
|
if (length < 0x18)
|
|
1851
|
-
target[position
|
|
2032
|
+
target[position++] = 0x80 | length;
|
|
1852
2033
|
else if (length < 0x100) {
|
|
1853
|
-
target[position
|
|
1854
|
-
target[position
|
|
2034
|
+
target[position++] = 0x98;
|
|
2035
|
+
target[position++] = length;
|
|
1855
2036
|
} else if (length < 0x10000) {
|
|
1856
|
-
target[position
|
|
1857
|
-
target[position
|
|
1858
|
-
target[position
|
|
2037
|
+
target[position++] = 0x99;
|
|
2038
|
+
target[position++] = length >> 8;
|
|
2039
|
+
target[position++] = length & 0xff;
|
|
1859
2040
|
} else {
|
|
1860
|
-
target[position
|
|
1861
|
-
targetView.setUint32(position
|
|
1862
|
-
position
|
|
2041
|
+
target[position++] = 0x9a;
|
|
2042
|
+
targetView.setUint32(position, length);
|
|
2043
|
+
position += 4;
|
|
1863
2044
|
}
|
|
1864
2045
|
}
|
|
1865
2046
|
|
|
2047
|
+
const BlobConstructor = typeof Blob === 'undefined' ? function(){} : Blob;
|
|
2048
|
+
function isBlob(object) {
|
|
2049
|
+
if (object instanceof BlobConstructor)
|
|
2050
|
+
return true;
|
|
2051
|
+
let tag = object[Symbol.toStringTag];
|
|
2052
|
+
return tag === 'Blob' || tag === 'File';
|
|
2053
|
+
}
|
|
1866
2054
|
function findRepetitiveStrings(value, packedValues) {
|
|
1867
2055
|
switch(typeof value) {
|
|
1868
2056
|
case 'string':
|
|
@@ -1912,7 +2100,7 @@
|
|
|
1912
2100
|
case 'function': console.log(value);
|
|
1913
2101
|
}
|
|
1914
2102
|
}
|
|
1915
|
-
const isLittleEndianMachine
|
|
2103
|
+
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
1916
2104
|
extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
|
|
1917
2105
|
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
1918
2106
|
typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
@@ -1926,14 +2114,14 @@
|
|
|
1926
2114
|
let seconds = date.getTime() / 1000;
|
|
1927
2115
|
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
|
|
1928
2116
|
// Timestamp 32
|
|
1929
|
-
target[position
|
|
1930
|
-
targetView.setUint32(position
|
|
1931
|
-
position
|
|
2117
|
+
target[position++] = 0x1a;
|
|
2118
|
+
targetView.setUint32(position, seconds);
|
|
2119
|
+
position += 4;
|
|
1932
2120
|
} else {
|
|
1933
2121
|
// Timestamp float64
|
|
1934
|
-
target[position
|
|
1935
|
-
targetView.setFloat64(position
|
|
1936
|
-
position
|
|
2122
|
+
target[position++] = 0xfb;
|
|
2123
|
+
targetView.setFloat64(position, seconds);
|
|
2124
|
+
position += 8;
|
|
1937
2125
|
}
|
|
1938
2126
|
}
|
|
1939
2127
|
}, { // Set
|
|
@@ -1989,8 +2177,8 @@
|
|
|
1989
2177
|
let packedValues = sharedData.packedValues || [];
|
|
1990
2178
|
let sharedStructures = sharedData.structures || [];
|
|
1991
2179
|
if (packedValues.values.length > 0) {
|
|
1992
|
-
target[position
|
|
1993
|
-
target[position
|
|
2180
|
+
target[position++] = 0xd8; // one-byte tag
|
|
2181
|
+
target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1994
2182
|
writeArrayHeader(4);
|
|
1995
2183
|
let valuesArray = packedValues.values;
|
|
1996
2184
|
encode(valuesArray);
|
|
@@ -2002,8 +2190,8 @@
|
|
|
2002
2190
|
}
|
|
2003
2191
|
}
|
|
2004
2192
|
if (sharedStructures) {
|
|
2005
|
-
targetView.setUint32(position
|
|
2006
|
-
position
|
|
2193
|
+
targetView.setUint32(position, 0xd9dffe00);
|
|
2194
|
+
position += 3;
|
|
2007
2195
|
let definitions = sharedStructures.slice(0);
|
|
2008
2196
|
definitions.unshift(0xe000);
|
|
2009
2197
|
definitions.push(new Tag(sharedData.version, 0x53687264));
|
|
@@ -2013,7 +2201,7 @@
|
|
|
2013
2201
|
}
|
|
2014
2202
|
}];
|
|
2015
2203
|
function typedArrayEncoder(tag, size) {
|
|
2016
|
-
if (!isLittleEndianMachine
|
|
2204
|
+
if (!isLittleEndianMachine && size > 1)
|
|
2017
2205
|
tag -= 4; // the big endian equivalents are 4 less
|
|
2018
2206
|
return {
|
|
2019
2207
|
tag: tag,
|
|
@@ -2021,7 +2209,7 @@
|
|
|
2021
2209
|
let length = typedArray.byteLength;
|
|
2022
2210
|
let offset = typedArray.byteOffset || 0;
|
|
2023
2211
|
let buffer = typedArray.buffer || typedArray;
|
|
2024
|
-
encode(hasNodeBuffer ? Buffer.from(buffer, offset, length) :
|
|
2212
|
+
encode(hasNodeBuffer ? Buffer$1.from(buffer, offset, length) :
|
|
2025
2213
|
new Uint8Array(buffer, offset, length));
|
|
2026
2214
|
}
|
|
2027
2215
|
}
|
|
@@ -2029,26 +2217,26 @@
|
|
|
2029
2217
|
function writeBuffer(buffer, makeRoom) {
|
|
2030
2218
|
let length = buffer.byteLength;
|
|
2031
2219
|
if (length < 0x18) {
|
|
2032
|
-
target[position
|
|
2220
|
+
target[position++] = 0x40 + length;
|
|
2033
2221
|
} else if (length < 0x100) {
|
|
2034
|
-
target[position
|
|
2035
|
-
target[position
|
|
2222
|
+
target[position++] = 0x58;
|
|
2223
|
+
target[position++] = length;
|
|
2036
2224
|
} else if (length < 0x10000) {
|
|
2037
|
-
target[position
|
|
2038
|
-
target[position
|
|
2039
|
-
target[position
|
|
2225
|
+
target[position++] = 0x59;
|
|
2226
|
+
target[position++] = length >> 8;
|
|
2227
|
+
target[position++] = length & 0xff;
|
|
2040
2228
|
} else {
|
|
2041
|
-
target[position
|
|
2042
|
-
targetView.setUint32(position
|
|
2043
|
-
position
|
|
2229
|
+
target[position++] = 0x5a;
|
|
2230
|
+
targetView.setUint32(position, length);
|
|
2231
|
+
position += 4;
|
|
2044
2232
|
}
|
|
2045
|
-
if (position
|
|
2046
|
-
makeRoom(position
|
|
2233
|
+
if (position + length >= target.length) {
|
|
2234
|
+
makeRoom(position + length);
|
|
2047
2235
|
}
|
|
2048
2236
|
// if it is already a typed array (has an ArrayBuffer), use that, but if it is an ArrayBuffer itself,
|
|
2049
2237
|
// must wrap it to set it.
|
|
2050
|
-
target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position
|
|
2051
|
-
position
|
|
2238
|
+
target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position);
|
|
2239
|
+
position += length;
|
|
2052
2240
|
}
|
|
2053
2241
|
|
|
2054
2242
|
function insertIds(serialized, idsToInsert) {
|
|
@@ -2077,27 +2265,30 @@
|
|
|
2077
2265
|
return serialized
|
|
2078
2266
|
}
|
|
2079
2267
|
function writeBundles(start, encode) {
|
|
2080
|
-
targetView.setUint32(bundledStrings
|
|
2081
|
-
let writeStrings = bundledStrings
|
|
2082
|
-
bundledStrings
|
|
2268
|
+
targetView.setUint32(bundledStrings.position + start, position - bundledStrings.position - start + 1); // the offset to bundle
|
|
2269
|
+
let writeStrings = bundledStrings;
|
|
2270
|
+
bundledStrings = null;
|
|
2083
2271
|
encode(writeStrings[0]);
|
|
2084
2272
|
encode(writeStrings[1]);
|
|
2085
2273
|
}
|
|
2086
2274
|
|
|
2087
|
-
function addExtension
|
|
2275
|
+
function addExtension(extension) {
|
|
2088
2276
|
if (extension.Class) {
|
|
2089
2277
|
if (!extension.encode)
|
|
2090
2278
|
throw new Error('Extension has no encode function')
|
|
2091
2279
|
extensionClasses.unshift(extension.Class);
|
|
2092
2280
|
extensions.unshift(extension);
|
|
2093
2281
|
}
|
|
2094
|
-
addExtension(extension);
|
|
2282
|
+
addExtension$1(extension);
|
|
2095
2283
|
}
|
|
2096
2284
|
let defaultEncoder = new Encoder({ useRecords: false });
|
|
2097
2285
|
const encode = defaultEncoder.encode;
|
|
2286
|
+
const encodeAsIterable = defaultEncoder.encodeAsIterable;
|
|
2287
|
+
const encodeAsAsyncIterable = defaultEncoder.encodeAsAsyncIterable;
|
|
2098
2288
|
const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
|
|
2099
2289
|
const REUSE_BUFFER_MODE = 512;
|
|
2100
2290
|
const RESET_BUFFER_MODE = 1024;
|
|
2291
|
+
const THROW_ON_ITERABLE = 2048;
|
|
2101
2292
|
|
|
2102
2293
|
/**
|
|
2103
2294
|
* Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer
|
|
@@ -2191,16 +2382,17 @@
|
|
|
2191
2382
|
exports.NEVER = NEVER;
|
|
2192
2383
|
exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
|
|
2193
2384
|
exports.Tag = Tag;
|
|
2194
|
-
exports.addExtension = addExtension
|
|
2385
|
+
exports.addExtension = addExtension;
|
|
2195
2386
|
exports.clearSource = clearSource;
|
|
2196
2387
|
exports.decode = decode;
|
|
2197
2388
|
exports.decodeIter = decodeIter;
|
|
2198
2389
|
exports.decodeMultiple = decodeMultiple;
|
|
2199
2390
|
exports.encode = encode;
|
|
2391
|
+
exports.encodeAsAsyncIterable = encodeAsAsyncIterable;
|
|
2392
|
+
exports.encodeAsIterable = encodeAsIterable;
|
|
2200
2393
|
exports.encodeIter = encodeIter;
|
|
2201
2394
|
exports.isNativeAccelerationEnabled = isNativeAccelerationEnabled;
|
|
2202
2395
|
exports.roundFloat32 = roundFloat32;
|
|
2203
2396
|
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
})));
|
|
2397
|
+
}));
|
|
2398
|
+
//# sourceMappingURL=index.js.map
|