cbor-x 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/decode.js +18 -12
- package/dist/decode-no-eval.cjs +1137 -0
- package/dist/decode-no-eval.cjs.map +1 -0
- package/dist/index-no-eval.cjs +2376 -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 +511 -341
- 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 +518 -346
- package/dist/node.cjs.map +1 -0
- package/dist/test.js +172 -31
- package/dist/test.js.map +1 -0
- package/encode.d.ts +1 -1
- package/encode.js +165 -2
- package/index.d.ts +9 -5
- package/index.js +1 -1
- package/node-index.js +1 -1
- package/package.json +7 -6
- package/rollup.config.js +51 -8
- package/stream.js +4 -2
package/dist/node.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var stream = require('stream');
|
|
6
4
|
var module$1 = require('module');
|
|
7
5
|
|
|
@@ -11,7 +9,7 @@ try {
|
|
|
11
9
|
} catch(error) {}
|
|
12
10
|
let src;
|
|
13
11
|
let srcEnd;
|
|
14
|
-
let position = 0;
|
|
12
|
+
let position$1 = 0;
|
|
15
13
|
const EMPTY_ARRAY = [];
|
|
16
14
|
const LEGACY_RECORD_INLINE_ID = 105;
|
|
17
15
|
const RECORD_DEFINITIONS_ID = 0xdffe;
|
|
@@ -26,7 +24,7 @@ let currentStructures;
|
|
|
26
24
|
let srcString;
|
|
27
25
|
let srcStringStart = 0;
|
|
28
26
|
let srcStringEnd = 0;
|
|
29
|
-
let bundledStrings;
|
|
27
|
+
let bundledStrings$1;
|
|
30
28
|
let referenceMap;
|
|
31
29
|
let currentExtensions = [];
|
|
32
30
|
let currentExtensionRanges = [];
|
|
@@ -115,12 +113,12 @@ class Decoder {
|
|
|
115
113
|
})
|
|
116
114
|
}
|
|
117
115
|
srcEnd = end > -1 ? end : source.length;
|
|
118
|
-
position = 0;
|
|
116
|
+
position$1 = 0;
|
|
119
117
|
stringPosition = 0;
|
|
120
118
|
srcStringEnd = 0;
|
|
121
119
|
srcString = null;
|
|
122
120
|
strings = EMPTY_ARRAY;
|
|
123
|
-
bundledStrings = null;
|
|
121
|
+
bundledStrings$1 = null;
|
|
124
122
|
src = source;
|
|
125
123
|
// this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
|
|
126
124
|
// technique for getting data from a database where it can be copied into an existing buffer instead of creating
|
|
@@ -163,8 +161,8 @@ class Decoder {
|
|
|
163
161
|
if (forEach(value) === false) {
|
|
164
162
|
return
|
|
165
163
|
}
|
|
166
|
-
while(position < size) {
|
|
167
|
-
lastPosition = position;
|
|
164
|
+
while(position$1 < size) {
|
|
165
|
+
lastPosition = position$1;
|
|
168
166
|
if (forEach(checkedRead()) === false) {
|
|
169
167
|
return
|
|
170
168
|
}
|
|
@@ -172,8 +170,8 @@ class Decoder {
|
|
|
172
170
|
}
|
|
173
171
|
else {
|
|
174
172
|
values = [ value ];
|
|
175
|
-
while(position < size) {
|
|
176
|
-
lastPosition = position;
|
|
173
|
+
while(position$1 < size) {
|
|
174
|
+
lastPosition = position$1;
|
|
177
175
|
values.push(checkedRead());
|
|
178
176
|
}
|
|
179
177
|
return values
|
|
@@ -191,23 +189,24 @@ class Decoder {
|
|
|
191
189
|
function checkedRead() {
|
|
192
190
|
try {
|
|
193
191
|
let result = read();
|
|
194
|
-
if (bundledStrings) {
|
|
195
|
-
if (position >= bundledStrings.postBundlePosition) {
|
|
192
|
+
if (bundledStrings$1) {
|
|
193
|
+
if (position$1 >= bundledStrings$1.postBundlePosition) {
|
|
196
194
|
let error = new Error('Unexpected bundle position');
|
|
197
195
|
error.incomplete = true;
|
|
198
196
|
throw error
|
|
199
197
|
}
|
|
200
198
|
// bundled strings to skip past
|
|
201
|
-
position = bundledStrings.postBundlePosition;
|
|
199
|
+
position$1 = bundledStrings$1.postBundlePosition;
|
|
200
|
+
bundledStrings$1 = null;
|
|
202
201
|
}
|
|
203
202
|
|
|
204
|
-
if (position == srcEnd) {
|
|
203
|
+
if (position$1 == srcEnd) {
|
|
205
204
|
// finished reading this source, cleanup references
|
|
206
205
|
currentStructures = null;
|
|
207
206
|
src = null;
|
|
208
207
|
if (referenceMap)
|
|
209
208
|
referenceMap = null;
|
|
210
|
-
} else if (position > srcEnd) {
|
|
209
|
+
} else if (position$1 > srcEnd) {
|
|
211
210
|
// over read
|
|
212
211
|
let error = new Error('Unexpected end of CBOR data');
|
|
213
212
|
error.incomplete = true;
|
|
@@ -227,52 +226,52 @@ function checkedRead() {
|
|
|
227
226
|
}
|
|
228
227
|
|
|
229
228
|
function read() {
|
|
230
|
-
let token = src[position++];
|
|
229
|
+
let token = src[position$1++];
|
|
231
230
|
let majorType = token >> 5;
|
|
232
231
|
token = token & 0x1f;
|
|
233
232
|
if (token > 0x17) {
|
|
234
233
|
switch (token) {
|
|
235
234
|
case 0x18:
|
|
236
|
-
token = src[position++];
|
|
235
|
+
token = src[position$1++];
|
|
237
236
|
break
|
|
238
237
|
case 0x19:
|
|
239
238
|
if (majorType == 7) {
|
|
240
239
|
return getFloat16()
|
|
241
240
|
}
|
|
242
|
-
token = dataView.getUint16(position);
|
|
243
|
-
position += 2;
|
|
241
|
+
token = dataView.getUint16(position$1);
|
|
242
|
+
position$1 += 2;
|
|
244
243
|
break
|
|
245
244
|
case 0x1a:
|
|
246
245
|
if (majorType == 7) {
|
|
247
|
-
let value = dataView.getFloat32(position);
|
|
246
|
+
let value = dataView.getFloat32(position$1);
|
|
248
247
|
if (currentDecoder.useFloat32 > 2) {
|
|
249
248
|
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
250
|
-
let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
|
|
251
|
-
position += 4;
|
|
249
|
+
let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
|
|
250
|
+
position$1 += 4;
|
|
252
251
|
return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
253
252
|
}
|
|
254
|
-
position += 4;
|
|
253
|
+
position$1 += 4;
|
|
255
254
|
return value
|
|
256
255
|
}
|
|
257
|
-
token = dataView.getUint32(position);
|
|
258
|
-
position += 4;
|
|
256
|
+
token = dataView.getUint32(position$1);
|
|
257
|
+
position$1 += 4;
|
|
259
258
|
break
|
|
260
259
|
case 0x1b:
|
|
261
260
|
if (majorType == 7) {
|
|
262
|
-
let value = dataView.getFloat64(position);
|
|
263
|
-
position += 8;
|
|
261
|
+
let value = dataView.getFloat64(position$1);
|
|
262
|
+
position$1 += 8;
|
|
264
263
|
return value
|
|
265
264
|
}
|
|
266
265
|
if (majorType > 1) {
|
|
267
|
-
if (dataView.getUint32(position) > 0)
|
|
266
|
+
if (dataView.getUint32(position$1) > 0)
|
|
268
267
|
throw new Error('JavaScript does not support arrays, maps, or strings with length over 4294967295')
|
|
269
|
-
token = dataView.getUint32(position + 4);
|
|
268
|
+
token = dataView.getUint32(position$1 + 4);
|
|
270
269
|
} else if (currentDecoder.int64AsNumber) {
|
|
271
|
-
token = dataView.getUint32(position) * 0x100000000;
|
|
272
|
-
token += dataView.getUint32(position + 4);
|
|
270
|
+
token = dataView.getUint32(position$1) * 0x100000000;
|
|
271
|
+
token += dataView.getUint32(position$1 + 4);
|
|
273
272
|
} else
|
|
274
|
-
token = dataView.getBigUint64(position);
|
|
275
|
-
position += 8;
|
|
273
|
+
token = dataView.getBigUint64(position$1);
|
|
274
|
+
position$1 += 8;
|
|
276
275
|
break
|
|
277
276
|
case 0x1f:
|
|
278
277
|
// indefinite length
|
|
@@ -321,8 +320,8 @@ function read() {
|
|
|
321
320
|
case 2: // buffer
|
|
322
321
|
return readBin(token)
|
|
323
322
|
case 3: // string
|
|
324
|
-
if (srcStringEnd >= position) {
|
|
325
|
-
return srcString.slice(position - srcStringStart, (position += token) - srcStringStart)
|
|
323
|
+
if (srcStringEnd >= position$1) {
|
|
324
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += token) - srcStringStart)
|
|
326
325
|
}
|
|
327
326
|
if (srcStringEnd == 0 && srcEnd < 140 && token < 32) {
|
|
328
327
|
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
@@ -426,24 +425,24 @@ const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
|
|
|
426
425
|
function createStructureReader(structure) {
|
|
427
426
|
function readObject() {
|
|
428
427
|
// get the array size from the header
|
|
429
|
-
let length = src[position++];
|
|
428
|
+
let length = src[position$1++];
|
|
430
429
|
//let majorType = token >> 5
|
|
431
430
|
length = length & 0x1f;
|
|
432
431
|
if (length > 0x17) {
|
|
433
432
|
switch (length) {
|
|
434
433
|
case 0x18:
|
|
435
|
-
length = src[position++];
|
|
434
|
+
length = src[position$1++];
|
|
436
435
|
break
|
|
437
436
|
case 0x19:
|
|
438
|
-
length = dataView.getUint16(position);
|
|
439
|
-
position += 2;
|
|
437
|
+
length = dataView.getUint16(position$1);
|
|
438
|
+
position$1 += 2;
|
|
440
439
|
break
|
|
441
440
|
case 0x1a:
|
|
442
|
-
length = dataView.getUint32(position);
|
|
443
|
-
position += 4;
|
|
441
|
+
length = dataView.getUint32(position$1);
|
|
442
|
+
position$1 += 4;
|
|
444
443
|
break
|
|
445
444
|
default:
|
|
446
|
-
throw new Error('Expected array header, but got ' + src[position - 1])
|
|
445
|
+
throw new Error('Expected array header, but got ' + src[position$1 - 1])
|
|
447
446
|
}
|
|
448
447
|
}
|
|
449
448
|
// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
|
|
@@ -490,9 +489,9 @@ function setExtractor(extractStrings) {
|
|
|
490
489
|
return function readString(length) {
|
|
491
490
|
let string = strings[stringPosition++];
|
|
492
491
|
if (string == null) {
|
|
493
|
-
if (bundledStrings)
|
|
492
|
+
if (bundledStrings$1)
|
|
494
493
|
return readStringJS(length)
|
|
495
|
-
let extraction = extractStrings(position, srcEnd, length, src);
|
|
494
|
+
let extraction = extractStrings(position$1, srcEnd, length, src);
|
|
496
495
|
if (typeof extraction == 'string') {
|
|
497
496
|
string = extraction;
|
|
498
497
|
strings = EMPTY_ARRAY;
|
|
@@ -507,13 +506,13 @@ function setExtractor(extractStrings) {
|
|
|
507
506
|
}
|
|
508
507
|
let srcStringLength = string.length;
|
|
509
508
|
if (srcStringLength <= length) {
|
|
510
|
-
position += length;
|
|
509
|
+
position$1 += length;
|
|
511
510
|
return string
|
|
512
511
|
}
|
|
513
512
|
srcString = string;
|
|
514
|
-
srcStringStart = position;
|
|
515
|
-
srcStringEnd = position + srcStringLength;
|
|
516
|
-
position += length;
|
|
513
|
+
srcStringStart = position$1;
|
|
514
|
+
srcStringEnd = position$1 + srcStringLength;
|
|
515
|
+
position$1 += length;
|
|
517
516
|
return string.slice(0, length) // we know we just want the beginning
|
|
518
517
|
}
|
|
519
518
|
}
|
|
@@ -525,29 +524,29 @@ function readStringJS(length) {
|
|
|
525
524
|
return result
|
|
526
525
|
}
|
|
527
526
|
if (length > 64 && decoder)
|
|
528
|
-
return decoder.decode(src.subarray(position, position += length))
|
|
529
|
-
const end = position + length;
|
|
527
|
+
return decoder.decode(src.subarray(position$1, position$1 += length))
|
|
528
|
+
const end = position$1 + length;
|
|
530
529
|
const units = [];
|
|
531
530
|
result = '';
|
|
532
|
-
while (position < end) {
|
|
533
|
-
const byte1 = src[position++];
|
|
531
|
+
while (position$1 < end) {
|
|
532
|
+
const byte1 = src[position$1++];
|
|
534
533
|
if ((byte1 & 0x80) === 0) {
|
|
535
534
|
// 1 byte
|
|
536
535
|
units.push(byte1);
|
|
537
536
|
} else if ((byte1 & 0xe0) === 0xc0) {
|
|
538
537
|
// 2 bytes
|
|
539
|
-
const byte2 = src[position++] & 0x3f;
|
|
538
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
540
539
|
units.push(((byte1 & 0x1f) << 6) | byte2);
|
|
541
540
|
} else if ((byte1 & 0xf0) === 0xe0) {
|
|
542
541
|
// 3 bytes
|
|
543
|
-
const byte2 = src[position++] & 0x3f;
|
|
544
|
-
const byte3 = src[position++] & 0x3f;
|
|
542
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
543
|
+
const byte3 = src[position$1++] & 0x3f;
|
|
545
544
|
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
|
|
546
545
|
} else if ((byte1 & 0xf8) === 0xf0) {
|
|
547
546
|
// 4 bytes
|
|
548
|
-
const byte2 = src[position++] & 0x3f;
|
|
549
|
-
const byte3 = src[position++] & 0x3f;
|
|
550
|
-
const byte4 = src[position++] & 0x3f;
|
|
547
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
548
|
+
const byte3 = src[position$1++] & 0x3f;
|
|
549
|
+
const byte4 = src[position$1++] & 0x3f;
|
|
551
550
|
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
|
552
551
|
if (unit > 0xffff) {
|
|
553
552
|
unit -= 0x10000;
|
|
@@ -573,12 +572,12 @@ function readStringJS(length) {
|
|
|
573
572
|
}
|
|
574
573
|
let fromCharCode = String.fromCharCode;
|
|
575
574
|
function longStringInJS(length) {
|
|
576
|
-
let start = position;
|
|
575
|
+
let start = position$1;
|
|
577
576
|
let bytes = new Array(length);
|
|
578
577
|
for (let i = 0; i < length; i++) {
|
|
579
|
-
const byte = src[position++];
|
|
578
|
+
const byte = src[position$1++];
|
|
580
579
|
if ((byte & 0x80) > 0) {
|
|
581
|
-
position = start;
|
|
580
|
+
position$1 = start;
|
|
582
581
|
return
|
|
583
582
|
}
|
|
584
583
|
bytes[i] = byte;
|
|
@@ -591,131 +590,131 @@ function shortStringInJS(length) {
|
|
|
591
590
|
if (length === 0)
|
|
592
591
|
return ''
|
|
593
592
|
else {
|
|
594
|
-
let a = src[position++];
|
|
593
|
+
let a = src[position$1++];
|
|
595
594
|
if ((a & 0x80) > 1) {
|
|
596
|
-
position -= 1;
|
|
595
|
+
position$1 -= 1;
|
|
597
596
|
return
|
|
598
597
|
}
|
|
599
598
|
return fromCharCode(a)
|
|
600
599
|
}
|
|
601
600
|
} else {
|
|
602
|
-
let a = src[position++];
|
|
603
|
-
let b = src[position++];
|
|
601
|
+
let a = src[position$1++];
|
|
602
|
+
let b = src[position$1++];
|
|
604
603
|
if ((a & 0x80) > 0 || (b & 0x80) > 0) {
|
|
605
|
-
position -= 2;
|
|
604
|
+
position$1 -= 2;
|
|
606
605
|
return
|
|
607
606
|
}
|
|
608
607
|
if (length < 3)
|
|
609
608
|
return fromCharCode(a, b)
|
|
610
|
-
let c = src[position++];
|
|
609
|
+
let c = src[position$1++];
|
|
611
610
|
if ((c & 0x80) > 0) {
|
|
612
|
-
position -= 3;
|
|
611
|
+
position$1 -= 3;
|
|
613
612
|
return
|
|
614
613
|
}
|
|
615
614
|
return fromCharCode(a, b, c)
|
|
616
615
|
}
|
|
617
616
|
} else {
|
|
618
|
-
let a = src[position++];
|
|
619
|
-
let b = src[position++];
|
|
620
|
-
let c = src[position++];
|
|
621
|
-
let d = src[position++];
|
|
617
|
+
let a = src[position$1++];
|
|
618
|
+
let b = src[position$1++];
|
|
619
|
+
let c = src[position$1++];
|
|
620
|
+
let d = src[position$1++];
|
|
622
621
|
if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
|
|
623
|
-
position -= 4;
|
|
622
|
+
position$1 -= 4;
|
|
624
623
|
return
|
|
625
624
|
}
|
|
626
625
|
if (length < 6) {
|
|
627
626
|
if (length === 4)
|
|
628
627
|
return fromCharCode(a, b, c, d)
|
|
629
628
|
else {
|
|
630
|
-
let e = src[position++];
|
|
629
|
+
let e = src[position$1++];
|
|
631
630
|
if ((e & 0x80) > 0) {
|
|
632
|
-
position -= 5;
|
|
631
|
+
position$1 -= 5;
|
|
633
632
|
return
|
|
634
633
|
}
|
|
635
634
|
return fromCharCode(a, b, c, d, e)
|
|
636
635
|
}
|
|
637
636
|
} else if (length < 8) {
|
|
638
|
-
let e = src[position++];
|
|
639
|
-
let f = src[position++];
|
|
637
|
+
let e = src[position$1++];
|
|
638
|
+
let f = src[position$1++];
|
|
640
639
|
if ((e & 0x80) > 0 || (f & 0x80) > 0) {
|
|
641
|
-
position -= 6;
|
|
640
|
+
position$1 -= 6;
|
|
642
641
|
return
|
|
643
642
|
}
|
|
644
643
|
if (length < 7)
|
|
645
644
|
return fromCharCode(a, b, c, d, e, f)
|
|
646
|
-
let g = src[position++];
|
|
645
|
+
let g = src[position$1++];
|
|
647
646
|
if ((g & 0x80) > 0) {
|
|
648
|
-
position -= 7;
|
|
647
|
+
position$1 -= 7;
|
|
649
648
|
return
|
|
650
649
|
}
|
|
651
650
|
return fromCharCode(a, b, c, d, e, f, g)
|
|
652
651
|
} else {
|
|
653
|
-
let e = src[position++];
|
|
654
|
-
let f = src[position++];
|
|
655
|
-
let g = src[position++];
|
|
656
|
-
let h = src[position++];
|
|
652
|
+
let e = src[position$1++];
|
|
653
|
+
let f = src[position$1++];
|
|
654
|
+
let g = src[position$1++];
|
|
655
|
+
let h = src[position$1++];
|
|
657
656
|
if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
|
|
658
|
-
position -= 8;
|
|
657
|
+
position$1 -= 8;
|
|
659
658
|
return
|
|
660
659
|
}
|
|
661
660
|
if (length < 10) {
|
|
662
661
|
if (length === 8)
|
|
663
662
|
return fromCharCode(a, b, c, d, e, f, g, h)
|
|
664
663
|
else {
|
|
665
|
-
let i = src[position++];
|
|
664
|
+
let i = src[position$1++];
|
|
666
665
|
if ((i & 0x80) > 0) {
|
|
667
|
-
position -= 9;
|
|
666
|
+
position$1 -= 9;
|
|
668
667
|
return
|
|
669
668
|
}
|
|
670
669
|
return fromCharCode(a, b, c, d, e, f, g, h, i)
|
|
671
670
|
}
|
|
672
671
|
} else if (length < 12) {
|
|
673
|
-
let i = src[position++];
|
|
674
|
-
let j = src[position++];
|
|
672
|
+
let i = src[position$1++];
|
|
673
|
+
let j = src[position$1++];
|
|
675
674
|
if ((i & 0x80) > 0 || (j & 0x80) > 0) {
|
|
676
|
-
position -= 10;
|
|
675
|
+
position$1 -= 10;
|
|
677
676
|
return
|
|
678
677
|
}
|
|
679
678
|
if (length < 11)
|
|
680
679
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j)
|
|
681
|
-
let k = src[position++];
|
|
680
|
+
let k = src[position$1++];
|
|
682
681
|
if ((k & 0x80) > 0) {
|
|
683
|
-
position -= 11;
|
|
682
|
+
position$1 -= 11;
|
|
684
683
|
return
|
|
685
684
|
}
|
|
686
685
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
|
|
687
686
|
} else {
|
|
688
|
-
let i = src[position++];
|
|
689
|
-
let j = src[position++];
|
|
690
|
-
let k = src[position++];
|
|
691
|
-
let l = src[position++];
|
|
687
|
+
let i = src[position$1++];
|
|
688
|
+
let j = src[position$1++];
|
|
689
|
+
let k = src[position$1++];
|
|
690
|
+
let l = src[position$1++];
|
|
692
691
|
if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
|
|
693
|
-
position -= 12;
|
|
692
|
+
position$1 -= 12;
|
|
694
693
|
return
|
|
695
694
|
}
|
|
696
695
|
if (length < 14) {
|
|
697
696
|
if (length === 12)
|
|
698
697
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
|
|
699
698
|
else {
|
|
700
|
-
let m = src[position++];
|
|
699
|
+
let m = src[position$1++];
|
|
701
700
|
if ((m & 0x80) > 0) {
|
|
702
|
-
position -= 13;
|
|
701
|
+
position$1 -= 13;
|
|
703
702
|
return
|
|
704
703
|
}
|
|
705
704
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
|
|
706
705
|
}
|
|
707
706
|
} else {
|
|
708
|
-
let m = src[position++];
|
|
709
|
-
let n = src[position++];
|
|
707
|
+
let m = src[position$1++];
|
|
708
|
+
let n = src[position$1++];
|
|
710
709
|
if ((m & 0x80) > 0 || (n & 0x80) > 0) {
|
|
711
|
-
position -= 14;
|
|
710
|
+
position$1 -= 14;
|
|
712
711
|
return
|
|
713
712
|
}
|
|
714
713
|
if (length < 15)
|
|
715
714
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
|
|
716
|
-
let o = src[position++];
|
|
715
|
+
let o = src[position$1++];
|
|
717
716
|
if ((o & 0x80) > 0) {
|
|
718
|
-
position -= 15;
|
|
717
|
+
position$1 -= 15;
|
|
719
718
|
return
|
|
720
719
|
}
|
|
721
720
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
|
|
@@ -728,14 +727,14 @@ function shortStringInJS(length) {
|
|
|
728
727
|
function readBin(length) {
|
|
729
728
|
return currentDecoder.copyBuffers ?
|
|
730
729
|
// specifically use the copying slice (not the node one)
|
|
731
|
-
Uint8Array.prototype.slice.call(src, position, position += length) :
|
|
732
|
-
src.subarray(position, position += length)
|
|
730
|
+
Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
|
|
731
|
+
src.subarray(position$1, position$1 += length)
|
|
733
732
|
}
|
|
734
733
|
let f32Array = new Float32Array(1);
|
|
735
734
|
let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
736
735
|
function getFloat16() {
|
|
737
|
-
let byte0 = src[position++];
|
|
738
|
-
let byte1 = src[position++];
|
|
736
|
+
let byte0 = src[position$1++];
|
|
737
|
+
let byte1 = src[position$1++];
|
|
739
738
|
let exponent = (byte0 & 0x7f) >> 2;
|
|
740
739
|
if (exponent === 0x1f) { // specials
|
|
741
740
|
if (byte1 || (byte0 & 3))
|
|
@@ -757,7 +756,7 @@ function getFloat16() {
|
|
|
757
756
|
return f32Array[0];
|
|
758
757
|
}
|
|
759
758
|
|
|
760
|
-
|
|
759
|
+
new Array(4096);
|
|
761
760
|
|
|
762
761
|
class Tag {
|
|
763
762
|
constructor(value, tag) {
|
|
@@ -823,13 +822,13 @@ const recordDefinition = (definition) => {
|
|
|
823
822
|
};
|
|
824
823
|
currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition;
|
|
825
824
|
currentExtensions[14] = (value) => {
|
|
826
|
-
if (bundledStrings)
|
|
827
|
-
return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 += value)
|
|
825
|
+
if (bundledStrings$1)
|
|
826
|
+
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
|
|
828
827
|
return new Tag(value, 14)
|
|
829
828
|
};
|
|
830
829
|
currentExtensions[15] = (value) => {
|
|
831
|
-
if (bundledStrings)
|
|
832
|
-
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
|
|
830
|
+
if (bundledStrings$1)
|
|
831
|
+
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
|
|
833
832
|
return new Tag(value, 15)
|
|
834
833
|
};
|
|
835
834
|
let glbl = { Error, RegExp };
|
|
@@ -837,7 +836,7 @@ currentExtensions[27] = (data) => { // http://cbor.schmorp.de/generic-object
|
|
|
837
836
|
return (glbl[data[0]] || Error)(data[1], data[2])
|
|
838
837
|
};
|
|
839
838
|
const packedTable = (read) => {
|
|
840
|
-
if (src[position++] != 0x84)
|
|
839
|
+
if (src[position$1++] != 0x84)
|
|
841
840
|
throw new Error('Packed values structure must be followed by a 4 element array')
|
|
842
841
|
let newPackedValues = read(); // packed values
|
|
843
842
|
packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues;
|
|
@@ -859,18 +858,23 @@ currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { // packed reference
|
|
|
859
858
|
return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
|
|
860
859
|
throw new Error('No support for non-integer packed references yet')
|
|
861
860
|
};
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
861
|
+
|
|
862
|
+
// The following code is an incomplete implementation of http://cbor.schmorp.de/stringref
|
|
863
|
+
// the real thing would need to implemennt more logic to populate the stringRefs table and
|
|
864
|
+
// maintain a stack of stringRef "namespaces".
|
|
865
|
+
//
|
|
866
|
+
// currentExtensions[25] = (id) => {
|
|
867
|
+
// return stringRefs[id]
|
|
868
|
+
// }
|
|
869
|
+
// currentExtensions[256] = (read) => {
|
|
870
|
+
// stringRefs = []
|
|
871
|
+
// try {
|
|
872
|
+
// return read()
|
|
873
|
+
// } finally {
|
|
874
|
+
// stringRefs = null
|
|
875
|
+
// }
|
|
876
|
+
// }
|
|
877
|
+
// currentExtensions[256].handlesRead = true
|
|
874
878
|
|
|
875
879
|
currentExtensions[28] = (read) => {
|
|
876
880
|
// shareable http://cbor.schmorp.de/value-sharing (for structured clones)
|
|
@@ -879,7 +883,7 @@ currentExtensions[28] = (read) => {
|
|
|
879
883
|
referenceMap.id = 0;
|
|
880
884
|
}
|
|
881
885
|
let id = referenceMap.id++;
|
|
882
|
-
let token = src[position];
|
|
886
|
+
let token = src[position$1];
|
|
883
887
|
let target;
|
|
884
888
|
// TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
|
|
885
889
|
// ahead past references to record structure definitions
|
|
@@ -956,7 +960,7 @@ currentExtensionRanges.push((tag, input) => {
|
|
|
956
960
|
return input
|
|
957
961
|
});
|
|
958
962
|
|
|
959
|
-
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
963
|
+
const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
960
964
|
const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
961
965
|
typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
962
966
|
typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
|
|
@@ -973,7 +977,7 @@ function registerTypedArray(TypedArray, tag) {
|
|
|
973
977
|
if (!littleEndian && bytesPerElement == 1)
|
|
974
978
|
continue
|
|
975
979
|
let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : 3;
|
|
976
|
-
currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine) ? (buffer) => {
|
|
980
|
+
currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
|
|
977
981
|
if (!TypedArray)
|
|
978
982
|
throw new Error('Could not find typed array for code ' + tag)
|
|
979
983
|
// we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
|
|
@@ -995,36 +999,36 @@ function registerTypedArray(TypedArray, tag) {
|
|
|
995
999
|
|
|
996
1000
|
function readBundleExt() {
|
|
997
1001
|
let length = readJustLength();
|
|
998
|
-
let bundlePosition = position + read();
|
|
1002
|
+
let bundlePosition = position$1 + read();
|
|
999
1003
|
for (let i = 2; i < length; i++) {
|
|
1000
1004
|
// skip past bundles that were already read
|
|
1001
1005
|
let bundleLength = readJustLength(); // this will increment position, so must add to position afterwards
|
|
1002
|
-
position += bundleLength;
|
|
1006
|
+
position$1 += bundleLength;
|
|
1003
1007
|
}
|
|
1004
|
-
let dataPosition = position;
|
|
1005
|
-
position = bundlePosition;
|
|
1006
|
-
bundledStrings = [readStringJS(readJustLength()), readStringJS(readJustLength())];
|
|
1007
|
-
bundledStrings.position0 = 0;
|
|
1008
|
-
bundledStrings.position1 = 0;
|
|
1009
|
-
bundledStrings.postBundlePosition = position;
|
|
1010
|
-
position = dataPosition;
|
|
1008
|
+
let dataPosition = position$1;
|
|
1009
|
+
position$1 = bundlePosition;
|
|
1010
|
+
bundledStrings$1 = [readStringJS(readJustLength()), readStringJS(readJustLength())];
|
|
1011
|
+
bundledStrings$1.position0 = 0;
|
|
1012
|
+
bundledStrings$1.position1 = 0;
|
|
1013
|
+
bundledStrings$1.postBundlePosition = position$1;
|
|
1014
|
+
position$1 = dataPosition;
|
|
1011
1015
|
return read()
|
|
1012
1016
|
}
|
|
1013
1017
|
|
|
1014
1018
|
function readJustLength() {
|
|
1015
|
-
let token = src[position++] & 0x1f;
|
|
1019
|
+
let token = src[position$1++] & 0x1f;
|
|
1016
1020
|
if (token > 0x17) {
|
|
1017
1021
|
switch (token) {
|
|
1018
1022
|
case 0x18:
|
|
1019
|
-
token = src[position++];
|
|
1023
|
+
token = src[position$1++];
|
|
1020
1024
|
break
|
|
1021
1025
|
case 0x19:
|
|
1022
|
-
token = dataView.getUint16(position);
|
|
1023
|
-
position += 2;
|
|
1026
|
+
token = dataView.getUint16(position$1);
|
|
1027
|
+
position$1 += 2;
|
|
1024
1028
|
break
|
|
1025
1029
|
case 0x1a:
|
|
1026
|
-
token = dataView.getUint32(position);
|
|
1027
|
-
position += 4;
|
|
1030
|
+
token = dataView.getUint32(position$1);
|
|
1031
|
+
position$1 += 4;
|
|
1028
1032
|
break
|
|
1029
1033
|
}
|
|
1030
1034
|
}
|
|
@@ -1050,14 +1054,14 @@ function loadShared() {
|
|
|
1050
1054
|
|
|
1051
1055
|
function saveState(callback) {
|
|
1052
1056
|
let savedSrcEnd = srcEnd;
|
|
1053
|
-
let savedPosition = position;
|
|
1057
|
+
let savedPosition = position$1;
|
|
1054
1058
|
let savedStringPosition = stringPosition;
|
|
1055
1059
|
let savedSrcStringStart = srcStringStart;
|
|
1056
1060
|
let savedSrcStringEnd = srcStringEnd;
|
|
1057
1061
|
let savedSrcString = srcString;
|
|
1058
1062
|
let savedStrings = strings;
|
|
1059
1063
|
let savedReferenceMap = referenceMap;
|
|
1060
|
-
let savedBundledStrings = bundledStrings;
|
|
1064
|
+
let savedBundledStrings = bundledStrings$1;
|
|
1061
1065
|
|
|
1062
1066
|
// TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
|
|
1063
1067
|
let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
|
|
@@ -1066,14 +1070,14 @@ function saveState(callback) {
|
|
|
1066
1070
|
let savedSequentialMode = sequentialMode;
|
|
1067
1071
|
let value = callback();
|
|
1068
1072
|
srcEnd = savedSrcEnd;
|
|
1069
|
-
position = savedPosition;
|
|
1073
|
+
position$1 = savedPosition;
|
|
1070
1074
|
stringPosition = savedStringPosition;
|
|
1071
1075
|
srcStringStart = savedSrcStringStart;
|
|
1072
1076
|
srcStringEnd = savedSrcStringEnd;
|
|
1073
1077
|
srcString = savedSrcString;
|
|
1074
1078
|
strings = savedStrings;
|
|
1075
1079
|
referenceMap = savedReferenceMap;
|
|
1076
|
-
bundledStrings = savedBundledStrings;
|
|
1080
|
+
bundledStrings$1 = savedBundledStrings;
|
|
1077
1081
|
src = savedSrc;
|
|
1078
1082
|
sequentialMode = savedSequentialMode;
|
|
1079
1083
|
currentStructures = savedStructures;
|
|
@@ -1087,7 +1091,7 @@ function clearSource() {
|
|
|
1087
1091
|
currentStructures = null;
|
|
1088
1092
|
}
|
|
1089
1093
|
|
|
1090
|
-
function addExtension(extension) {
|
|
1094
|
+
function addExtension$1(extension) {
|
|
1091
1095
|
currentExtensions[extension.tag] = extension.decode;
|
|
1092
1096
|
}
|
|
1093
1097
|
|
|
@@ -1118,13 +1122,15 @@ let extensions, extensionClasses;
|
|
|
1118
1122
|
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
1119
1123
|
const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
|
|
1120
1124
|
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
1125
|
+
const BlobConstructor = typeof Blob === 'undefined' ? {} : Blob;
|
|
1121
1126
|
const MAX_STRUCTURES = 0x100;
|
|
1122
1127
|
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1128
|
+
let throwOnIterable;
|
|
1123
1129
|
let target;
|
|
1124
1130
|
let targetView;
|
|
1125
|
-
let position
|
|
1131
|
+
let position = 0;
|
|
1126
1132
|
let safeEnd;
|
|
1127
|
-
let bundledStrings
|
|
1133
|
+
let bundledStrings = null;
|
|
1128
1134
|
const MAX_BUNDLE_SIZE = 0xf000;
|
|
1129
1135
|
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1130
1136
|
const RECORD_SYMBOL = Symbol('record-id');
|
|
@@ -1193,28 +1199,28 @@ class Encoder extends Decoder {
|
|
|
1193
1199
|
if (!target) {
|
|
1194
1200
|
target = new ByteArrayAllocate(8192);
|
|
1195
1201
|
targetView = new DataView(target.buffer, 0, 8192);
|
|
1196
|
-
position
|
|
1202
|
+
position = 0;
|
|
1197
1203
|
}
|
|
1198
1204
|
safeEnd = target.length - 10;
|
|
1199
|
-
if (safeEnd - position
|
|
1205
|
+
if (safeEnd - position < 0x800) {
|
|
1200
1206
|
// don't start too close to the end,
|
|
1201
1207
|
target = new ByteArrayAllocate(target.length);
|
|
1202
1208
|
targetView = new DataView(target.buffer, 0, target.length);
|
|
1203
1209
|
safeEnd = target.length - 10;
|
|
1204
|
-
position
|
|
1210
|
+
position = 0;
|
|
1205
1211
|
} else if (encodeOptions === REUSE_BUFFER_MODE)
|
|
1206
|
-
position
|
|
1207
|
-
start = position
|
|
1212
|
+
position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
|
|
1213
|
+
start = position;
|
|
1208
1214
|
if (encoder.useSelfDescribedHeader) {
|
|
1209
|
-
targetView.setUint32(position
|
|
1210
|
-
position
|
|
1215
|
+
targetView.setUint32(position, 0xd9d9f700); // tag two byte, then self-descriptive tag
|
|
1216
|
+
position += 3;
|
|
1211
1217
|
}
|
|
1212
1218
|
referenceMap = encoder.structuredClone ? new Map() : null;
|
|
1213
1219
|
if (encoder.bundleStrings && typeof value !== 'string') {
|
|
1214
|
-
bundledStrings
|
|
1215
|
-
bundledStrings
|
|
1220
|
+
bundledStrings = [];
|
|
1221
|
+
bundledStrings.size = Infinity; // force a new bundle start on first string
|
|
1216
1222
|
} else
|
|
1217
|
-
bundledStrings
|
|
1223
|
+
bundledStrings = null;
|
|
1218
1224
|
|
|
1219
1225
|
sharedStructures = encoder.structures;
|
|
1220
1226
|
if (sharedStructures) {
|
|
@@ -1270,8 +1276,8 @@ class Encoder extends Decoder {
|
|
|
1270
1276
|
packedValues.samplingPackedValues = samplingPackedValues;
|
|
1271
1277
|
findRepetitiveStrings(value, packedValues);
|
|
1272
1278
|
if (packedValues.values.length > 0) {
|
|
1273
|
-
target[position
|
|
1274
|
-
target[position
|
|
1279
|
+
target[position++] = 0xd8; // one-byte tag
|
|
1280
|
+
target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1275
1281
|
writeArrayHeader(4);
|
|
1276
1282
|
let valuesArray = packedValues.values;
|
|
1277
1283
|
encode(valuesArray);
|
|
@@ -1283,27 +1289,30 @@ class Encoder extends Decoder {
|
|
|
1283
1289
|
}
|
|
1284
1290
|
}
|
|
1285
1291
|
}
|
|
1292
|
+
throwOnIterable = encodeOptions & THROW_ON_ITERABLE;
|
|
1286
1293
|
try {
|
|
1294
|
+
if (throwOnIterable)
|
|
1295
|
+
return;
|
|
1287
1296
|
encode(value);
|
|
1288
|
-
if (bundledStrings
|
|
1297
|
+
if (bundledStrings) {
|
|
1289
1298
|
writeBundles(start, encode);
|
|
1290
1299
|
}
|
|
1291
|
-
encoder.offset = position
|
|
1300
|
+
encoder.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1292
1301
|
if (referenceMap && referenceMap.idsToInsert) {
|
|
1293
|
-
position
|
|
1294
|
-
if (position
|
|
1295
|
-
makeRoom(position
|
|
1296
|
-
encoder.offset = position
|
|
1297
|
-
let serialized = insertIds(target.subarray(start, position
|
|
1302
|
+
position += referenceMap.idsToInsert.length * 2;
|
|
1303
|
+
if (position > safeEnd)
|
|
1304
|
+
makeRoom(position);
|
|
1305
|
+
encoder.offset = position;
|
|
1306
|
+
let serialized = insertIds(target.subarray(start, position), referenceMap.idsToInsert);
|
|
1298
1307
|
referenceMap = null;
|
|
1299
1308
|
return serialized
|
|
1300
1309
|
}
|
|
1301
1310
|
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1302
1311
|
target.start = start;
|
|
1303
|
-
target.end = position
|
|
1312
|
+
target.end = position;
|
|
1304
1313
|
return target
|
|
1305
1314
|
}
|
|
1306
|
-
return target.subarray(start, position
|
|
1315
|
+
return target.subarray(start, position) // position can change if we call encode again in saveShared, so we get the buffer now
|
|
1307
1316
|
} finally {
|
|
1308
1317
|
if (sharedStructures) {
|
|
1309
1318
|
if (serializationsSinceTransitionRebuild < 10)
|
|
@@ -1330,13 +1339,13 @@ class Encoder extends Decoder {
|
|
|
1330
1339
|
encoder.structures = encoder.structures.slice(0, maxSharedStructures);
|
|
1331
1340
|
}
|
|
1332
1341
|
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1333
|
-
let returnBuffer = target.subarray(start, position
|
|
1342
|
+
let returnBuffer = target.subarray(start, position);
|
|
1334
1343
|
if (encoder.updateSharedData() === false)
|
|
1335
1344
|
return encoder.encode(value) // re-encode if it fails
|
|
1336
1345
|
return returnBuffer
|
|
1337
1346
|
}
|
|
1338
1347
|
if (encodeOptions & RESET_BUFFER_MODE)
|
|
1339
|
-
position
|
|
1348
|
+
position = start;
|
|
1340
1349
|
}
|
|
1341
1350
|
};
|
|
1342
1351
|
this.findCommonStringsToPack = () => {
|
|
@@ -1360,8 +1369,8 @@ class Encoder extends Decoder {
|
|
|
1360
1369
|
}
|
|
1361
1370
|
};
|
|
1362
1371
|
const encode = (value) => {
|
|
1363
|
-
if (position
|
|
1364
|
-
target = makeRoom(position
|
|
1372
|
+
if (position > safeEnd)
|
|
1373
|
+
target = makeRoom(position);
|
|
1365
1374
|
|
|
1366
1375
|
var type = typeof value;
|
|
1367
1376
|
var length;
|
|
@@ -1370,9 +1379,9 @@ class Encoder extends Decoder {
|
|
|
1370
1379
|
let packedPosition = packedObjectMap[value];
|
|
1371
1380
|
if (packedPosition >= 0) {
|
|
1372
1381
|
if (packedPosition < 16)
|
|
1373
|
-
target[position
|
|
1382
|
+
target[position++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1374
1383
|
else {
|
|
1375
|
-
target[position
|
|
1384
|
+
target[position++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1376
1385
|
if (packedPosition & 1)
|
|
1377
1386
|
encode((15 - packedPosition) >> 1);
|
|
1378
1387
|
else
|
|
@@ -1404,30 +1413,30 @@ class Encoder extends Decoder {
|
|
|
1404
1413
|
}
|
|
1405
1414
|
}
|
|
1406
1415
|
let strLength = value.length;
|
|
1407
|
-
if (bundledStrings
|
|
1408
|
-
if ((bundledStrings
|
|
1416
|
+
if (bundledStrings && strLength >= 4 && strLength < 0x400) {
|
|
1417
|
+
if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
1409
1418
|
let extStart;
|
|
1410
|
-
let maxBytes = (bundledStrings
|
|
1411
|
-
if (position
|
|
1412
|
-
target = makeRoom(position
|
|
1413
|
-
target[position
|
|
1414
|
-
target[position
|
|
1415
|
-
target[position
|
|
1419
|
+
let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
|
|
1420
|
+
if (position + maxBytes > safeEnd)
|
|
1421
|
+
target = makeRoom(position + maxBytes);
|
|
1422
|
+
target[position++] = 0xd9; // tag 16-bit
|
|
1423
|
+
target[position++] = 0xdf; // tag 0xdff9
|
|
1424
|
+
target[position++] = 0xf9;
|
|
1416
1425
|
// TODO: If we only have one bundle with any string data, only write one string bundle
|
|
1417
|
-
target[position
|
|
1418
|
-
target[position
|
|
1419
|
-
extStart = position
|
|
1420
|
-
position
|
|
1421
|
-
if (bundledStrings
|
|
1426
|
+
target[position++] = bundledStrings.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
|
|
1427
|
+
target[position++] = 0x1a; // 32-bit unsigned int
|
|
1428
|
+
extStart = position - start;
|
|
1429
|
+
position += 4; // reserve for writing bundle reference
|
|
1430
|
+
if (bundledStrings.position) {
|
|
1422
1431
|
writeBundles(start, encode); // write the last bundles
|
|
1423
1432
|
}
|
|
1424
|
-
bundledStrings
|
|
1425
|
-
bundledStrings
|
|
1426
|
-
bundledStrings
|
|
1433
|
+
bundledStrings = ['', '']; // create new ones
|
|
1434
|
+
bundledStrings.size = 0;
|
|
1435
|
+
bundledStrings.position = extStart;
|
|
1427
1436
|
}
|
|
1428
1437
|
let twoByte = hasNonLatin.test(value);
|
|
1429
|
-
bundledStrings
|
|
1430
|
-
target[position
|
|
1438
|
+
bundledStrings[twoByte ? 0 : 1] += value;
|
|
1439
|
+
target[position++] = twoByte ? 0xce : 0xcf;
|
|
1431
1440
|
encode(strLength);
|
|
1432
1441
|
return
|
|
1433
1442
|
}
|
|
@@ -1443,11 +1452,11 @@ class Encoder extends Decoder {
|
|
|
1443
1452
|
headerSize = 5;
|
|
1444
1453
|
}
|
|
1445
1454
|
let maxBytes = strLength * 3;
|
|
1446
|
-
if (position
|
|
1447
|
-
target = makeRoom(position
|
|
1455
|
+
if (position + maxBytes > safeEnd)
|
|
1456
|
+
target = makeRoom(position + maxBytes);
|
|
1448
1457
|
|
|
1449
1458
|
if (strLength < 0x40 || !encodeUtf8) {
|
|
1450
|
-
let i, c1, c2, strPosition = position
|
|
1459
|
+
let i, c1, c2, strPosition = position + headerSize;
|
|
1451
1460
|
for (i = 0; i < strLength; i++) {
|
|
1452
1461
|
c1 = value.charCodeAt(i);
|
|
1453
1462
|
if (c1 < 0x80) {
|
|
@@ -1471,105 +1480,105 @@ class Encoder extends Decoder {
|
|
|
1471
1480
|
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1472
1481
|
}
|
|
1473
1482
|
}
|
|
1474
|
-
length = strPosition - position
|
|
1483
|
+
length = strPosition - position - headerSize;
|
|
1475
1484
|
} else {
|
|
1476
|
-
length = encodeUtf8(value, position
|
|
1485
|
+
length = encodeUtf8(value, position + headerSize, maxBytes);
|
|
1477
1486
|
}
|
|
1478
1487
|
|
|
1479
1488
|
if (length < 0x18) {
|
|
1480
|
-
target[position
|
|
1489
|
+
target[position++] = 0x60 | length;
|
|
1481
1490
|
} else if (length < 0x100) {
|
|
1482
1491
|
if (headerSize < 2) {
|
|
1483
|
-
target.copyWithin(position
|
|
1492
|
+
target.copyWithin(position + 2, position + 1, position + 1 + length);
|
|
1484
1493
|
}
|
|
1485
|
-
target[position
|
|
1486
|
-
target[position
|
|
1494
|
+
target[position++] = 0x78;
|
|
1495
|
+
target[position++] = length;
|
|
1487
1496
|
} else if (length < 0x10000) {
|
|
1488
1497
|
if (headerSize < 3) {
|
|
1489
|
-
target.copyWithin(position
|
|
1498
|
+
target.copyWithin(position + 3, position + 2, position + 2 + length);
|
|
1490
1499
|
}
|
|
1491
|
-
target[position
|
|
1492
|
-
target[position
|
|
1493
|
-
target[position
|
|
1500
|
+
target[position++] = 0x79;
|
|
1501
|
+
target[position++] = length >> 8;
|
|
1502
|
+
target[position++] = length & 0xff;
|
|
1494
1503
|
} else {
|
|
1495
1504
|
if (headerSize < 5) {
|
|
1496
|
-
target.copyWithin(position
|
|
1505
|
+
target.copyWithin(position + 5, position + 3, position + 3 + length);
|
|
1497
1506
|
}
|
|
1498
|
-
target[position
|
|
1499
|
-
targetView.setUint32(position
|
|
1500
|
-
position
|
|
1507
|
+
target[position++] = 0x7a;
|
|
1508
|
+
targetView.setUint32(position, length);
|
|
1509
|
+
position += 4;
|
|
1501
1510
|
}
|
|
1502
|
-
position
|
|
1511
|
+
position += length;
|
|
1503
1512
|
} else if (type === 'number') {
|
|
1504
1513
|
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1505
1514
|
// positive uint
|
|
1506
1515
|
if (value < 0x18) {
|
|
1507
|
-
target[position
|
|
1516
|
+
target[position++] = value;
|
|
1508
1517
|
} else if (value < 0x100) {
|
|
1509
|
-
target[position
|
|
1510
|
-
target[position
|
|
1518
|
+
target[position++] = 0x18;
|
|
1519
|
+
target[position++] = value;
|
|
1511
1520
|
} else if (value < 0x10000) {
|
|
1512
|
-
target[position
|
|
1513
|
-
target[position
|
|
1514
|
-
target[position
|
|
1521
|
+
target[position++] = 0x19;
|
|
1522
|
+
target[position++] = value >> 8;
|
|
1523
|
+
target[position++] = value & 0xff;
|
|
1515
1524
|
} else {
|
|
1516
|
-
target[position
|
|
1517
|
-
targetView.setUint32(position
|
|
1518
|
-
position
|
|
1525
|
+
target[position++] = 0x1a;
|
|
1526
|
+
targetView.setUint32(position, value);
|
|
1527
|
+
position += 4;
|
|
1519
1528
|
}
|
|
1520
1529
|
} else if (value >> 0 === value) { // negative integer
|
|
1521
1530
|
if (value >= -0x18) {
|
|
1522
|
-
target[position
|
|
1531
|
+
target[position++] = 0x1f - value;
|
|
1523
1532
|
} else if (value >= -0x100) {
|
|
1524
|
-
target[position
|
|
1525
|
-
target[position
|
|
1533
|
+
target[position++] = 0x38;
|
|
1534
|
+
target[position++] = ~value;
|
|
1526
1535
|
} else if (value >= -0x10000) {
|
|
1527
|
-
target[position
|
|
1528
|
-
targetView.setUint16(position
|
|
1529
|
-
position
|
|
1536
|
+
target[position++] = 0x39;
|
|
1537
|
+
targetView.setUint16(position, ~value);
|
|
1538
|
+
position += 2;
|
|
1530
1539
|
} else {
|
|
1531
|
-
target[position
|
|
1532
|
-
targetView.setUint32(position
|
|
1533
|
-
position
|
|
1540
|
+
target[position++] = 0x3a;
|
|
1541
|
+
targetView.setUint32(position, ~value);
|
|
1542
|
+
position += 4;
|
|
1534
1543
|
}
|
|
1535
1544
|
} else {
|
|
1536
1545
|
let useFloat32;
|
|
1537
1546
|
if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
|
|
1538
|
-
target[position
|
|
1539
|
-
targetView.setFloat32(position
|
|
1547
|
+
target[position++] = 0xfa;
|
|
1548
|
+
targetView.setFloat32(position, value);
|
|
1540
1549
|
let xShifted;
|
|
1541
1550
|
if (useFloat32 < 4 ||
|
|
1542
1551
|
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1543
|
-
((xShifted = value * mult10[((target[position
|
|
1544
|
-
position
|
|
1552
|
+
((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
|
|
1553
|
+
position += 4;
|
|
1545
1554
|
return
|
|
1546
1555
|
} else
|
|
1547
|
-
position
|
|
1556
|
+
position--; // move back into position for writing a double
|
|
1548
1557
|
}
|
|
1549
|
-
target[position
|
|
1550
|
-
targetView.setFloat64(position
|
|
1551
|
-
position
|
|
1558
|
+
target[position++] = 0xfb;
|
|
1559
|
+
targetView.setFloat64(position, value);
|
|
1560
|
+
position += 8;
|
|
1552
1561
|
}
|
|
1553
1562
|
} else if (type === 'object') {
|
|
1554
1563
|
if (!value)
|
|
1555
|
-
target[position
|
|
1564
|
+
target[position++] = 0xf6;
|
|
1556
1565
|
else {
|
|
1557
1566
|
if (referenceMap) {
|
|
1558
1567
|
let referee = referenceMap.get(value);
|
|
1559
1568
|
if (referee) {
|
|
1560
|
-
target[position
|
|
1561
|
-
target[position
|
|
1562
|
-
target[position
|
|
1569
|
+
target[position++] = 0xd8;
|
|
1570
|
+
target[position++] = 29; // http://cbor.schmorp.de/value-sharing
|
|
1571
|
+
target[position++] = 0x19; // 16-bit uint
|
|
1563
1572
|
if (!referee.references) {
|
|
1564
1573
|
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
|
|
1565
1574
|
referee.references = [];
|
|
1566
1575
|
idsToInsert.push(referee);
|
|
1567
1576
|
}
|
|
1568
|
-
referee.references.push(position
|
|
1569
|
-
position
|
|
1577
|
+
referee.references.push(position - start);
|
|
1578
|
+
position += 2; // TODO: also support 32-bit
|
|
1570
1579
|
return
|
|
1571
1580
|
} else
|
|
1572
|
-
referenceMap.set(value, { offset: position
|
|
1581
|
+
referenceMap.set(value, { offset: position - start });
|
|
1573
1582
|
}
|
|
1574
1583
|
let constructor = value.constructor;
|
|
1575
1584
|
if (constructor === Object) {
|
|
@@ -1577,7 +1586,7 @@ class Encoder extends Decoder {
|
|
|
1577
1586
|
} else if (constructor === Array) {
|
|
1578
1587
|
length = value.length;
|
|
1579
1588
|
if (length < 0x18) {
|
|
1580
|
-
target[position
|
|
1589
|
+
target[position++] = 0x80 | length;
|
|
1581
1590
|
} else {
|
|
1582
1591
|
writeArrayHeader(length);
|
|
1583
1592
|
}
|
|
@@ -1587,24 +1596,24 @@ class Encoder extends Decoder {
|
|
|
1587
1596
|
} else if (constructor === Map) {
|
|
1588
1597
|
if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
|
|
1589
1598
|
// 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
|
|
1590
|
-
target[position
|
|
1591
|
-
target[position
|
|
1592
|
-
target[position
|
|
1599
|
+
target[position++] = 0xd9;
|
|
1600
|
+
target[position++] = 1;
|
|
1601
|
+
target[position++] = 3;
|
|
1593
1602
|
}
|
|
1594
1603
|
length = value.size;
|
|
1595
1604
|
if (length < 0x18) {
|
|
1596
|
-
target[position
|
|
1605
|
+
target[position++] = 0xa0 | length;
|
|
1597
1606
|
} else if (length < 0x100) {
|
|
1598
|
-
target[position
|
|
1599
|
-
target[position
|
|
1607
|
+
target[position++] = 0xb8;
|
|
1608
|
+
target[position++] = length;
|
|
1600
1609
|
} else if (length < 0x10000) {
|
|
1601
|
-
target[position
|
|
1602
|
-
target[position
|
|
1603
|
-
target[position
|
|
1610
|
+
target[position++] = 0xb9;
|
|
1611
|
+
target[position++] = length >> 8;
|
|
1612
|
+
target[position++] = length & 0xff;
|
|
1604
1613
|
} else {
|
|
1605
|
-
target[position
|
|
1606
|
-
targetView.setUint32(position
|
|
1607
|
-
position
|
|
1614
|
+
target[position++] = 0xba;
|
|
1615
|
+
targetView.setUint32(position, length);
|
|
1616
|
+
position += 4;
|
|
1608
1617
|
}
|
|
1609
1618
|
if (encoder.keyMap) {
|
|
1610
1619
|
for (let [ key, entryValue ] of value) {
|
|
@@ -1626,58 +1635,68 @@ class Encoder extends Decoder {
|
|
|
1626
1635
|
if (tag == undefined)
|
|
1627
1636
|
tag = extension.getTag && extension.getTag.call(this, value);
|
|
1628
1637
|
if (tag < 0x18) {
|
|
1629
|
-
target[position
|
|
1638
|
+
target[position++] = 0xc0 | tag;
|
|
1630
1639
|
} else if (tag < 0x100) {
|
|
1631
|
-
target[position
|
|
1632
|
-
target[position
|
|
1640
|
+
target[position++] = 0xd8;
|
|
1641
|
+
target[position++] = tag;
|
|
1633
1642
|
} else if (tag < 0x10000) {
|
|
1634
|
-
target[position
|
|
1635
|
-
target[position
|
|
1636
|
-
target[position
|
|
1643
|
+
target[position++] = 0xd9;
|
|
1644
|
+
target[position++] = tag >> 8;
|
|
1645
|
+
target[position++] = tag & 0xff;
|
|
1637
1646
|
} else if (tag > -1) {
|
|
1638
|
-
target[position
|
|
1639
|
-
targetView.setUint32(position
|
|
1640
|
-
position
|
|
1647
|
+
target[position++] = 0xda;
|
|
1648
|
+
targetView.setUint32(position, tag);
|
|
1649
|
+
position += 4;
|
|
1641
1650
|
} // else undefined, don't write tag
|
|
1642
1651
|
extension.encode.call(this, value, encode, makeRoom);
|
|
1643
1652
|
return
|
|
1644
1653
|
}
|
|
1645
1654
|
}
|
|
1646
1655
|
if (value[Symbol.iterator]) {
|
|
1647
|
-
|
|
1656
|
+
if (throwOnIterable) {
|
|
1657
|
+
let error = new Error('Iterable should be serialized as iterator');
|
|
1658
|
+
error.iteratorNotHandled = true;
|
|
1659
|
+
throw error;
|
|
1660
|
+
}
|
|
1661
|
+
target[position++] = 0x9f; // indefinite length array
|
|
1648
1662
|
for (let entry of value) {
|
|
1649
1663
|
encode(entry);
|
|
1650
1664
|
}
|
|
1651
|
-
target[position
|
|
1665
|
+
target[position++] = 0xff; // stop-code
|
|
1652
1666
|
return
|
|
1653
1667
|
}
|
|
1668
|
+
if (value[Symbol.asyncIterator] || constructor === BlobConstructor) {
|
|
1669
|
+
let error = new Error('Iterable/blob should be serialized as iterator');
|
|
1670
|
+
error.iteratorNotHandled = true;
|
|
1671
|
+
throw error;
|
|
1672
|
+
}
|
|
1654
1673
|
// no extension found, write as object
|
|
1655
1674
|
writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
|
|
1656
1675
|
}
|
|
1657
1676
|
}
|
|
1658
1677
|
} else if (type === 'boolean') {
|
|
1659
|
-
target[position
|
|
1678
|
+
target[position++] = value ? 0xf5 : 0xf4;
|
|
1660
1679
|
} else if (type === 'bigint') {
|
|
1661
1680
|
if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
|
|
1662
1681
|
// use an unsigned int as long as it fits
|
|
1663
|
-
target[position
|
|
1664
|
-
targetView.setBigUint64(position
|
|
1682
|
+
target[position++] = 0x1b;
|
|
1683
|
+
targetView.setBigUint64(position, value);
|
|
1665
1684
|
} else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
|
|
1666
1685
|
// if we can fit an unsigned int, use that
|
|
1667
|
-
target[position
|
|
1668
|
-
targetView.setBigUint64(position
|
|
1686
|
+
target[position++] = 0x3b;
|
|
1687
|
+
targetView.setBigUint64(position, -value - BigInt(1));
|
|
1669
1688
|
} else {
|
|
1670
1689
|
// overflow
|
|
1671
1690
|
if (this.largeBigIntToFloat) {
|
|
1672
|
-
target[position
|
|
1673
|
-
targetView.setFloat64(position
|
|
1691
|
+
target[position++] = 0xfb;
|
|
1692
|
+
targetView.setFloat64(position, Number(value));
|
|
1674
1693
|
} else {
|
|
1675
1694
|
throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
|
|
1676
1695
|
}
|
|
1677
1696
|
}
|
|
1678
|
-
position
|
|
1697
|
+
position += 8;
|
|
1679
1698
|
} else if (type === 'undefined') {
|
|
1680
|
-
target[position
|
|
1699
|
+
target[position++] = 0xf7;
|
|
1681
1700
|
} else {
|
|
1682
1701
|
throw new Error('Unknown type: ' + type)
|
|
1683
1702
|
}
|
|
@@ -1689,18 +1708,18 @@ class Encoder extends Decoder {
|
|
|
1689
1708
|
let vals = Object.values(object);
|
|
1690
1709
|
let length = keys.length;
|
|
1691
1710
|
if (length < 0x18) {
|
|
1692
|
-
target[position
|
|
1711
|
+
target[position++] = 0xa0 | length;
|
|
1693
1712
|
} else if (length < 0x100) {
|
|
1694
|
-
target[position
|
|
1695
|
-
target[position
|
|
1713
|
+
target[position++] = 0xb8;
|
|
1714
|
+
target[position++] = length;
|
|
1696
1715
|
} else if (length < 0x10000) {
|
|
1697
|
-
target[position
|
|
1698
|
-
target[position
|
|
1699
|
-
target[position
|
|
1716
|
+
target[position++] = 0xb9;
|
|
1717
|
+
target[position++] = length >> 8;
|
|
1718
|
+
target[position++] = length & 0xff;
|
|
1700
1719
|
} else {
|
|
1701
|
-
target[position
|
|
1702
|
-
targetView.setUint32(position
|
|
1703
|
-
position
|
|
1720
|
+
target[position++] = 0xba;
|
|
1721
|
+
targetView.setUint32(position, length);
|
|
1722
|
+
position += 4;
|
|
1704
1723
|
}
|
|
1705
1724
|
if (encoder.keyMap) {
|
|
1706
1725
|
for (let i = 0; i < length; i++) {
|
|
@@ -1715,9 +1734,9 @@ class Encoder extends Decoder {
|
|
|
1715
1734
|
}
|
|
1716
1735
|
} :
|
|
1717
1736
|
(object, safePrototype) => {
|
|
1718
|
-
target[position
|
|
1719
|
-
let objectOffset = position
|
|
1720
|
-
position
|
|
1737
|
+
target[position++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
|
|
1738
|
+
let objectOffset = position - start;
|
|
1739
|
+
position += 2;
|
|
1721
1740
|
let size = 0;
|
|
1722
1741
|
if (encoder.keyMap) {
|
|
1723
1742
|
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
@@ -1770,9 +1789,9 @@ class Encoder extends Decoder {
|
|
|
1770
1789
|
let recordId = transition[RECORD_SYMBOL];
|
|
1771
1790
|
if (recordId !== undefined) {
|
|
1772
1791
|
recordId &= 0xffff;
|
|
1773
|
-
target[position
|
|
1774
|
-
target[position
|
|
1775
|
-
target[position
|
|
1792
|
+
target[position++] = 0xd9;
|
|
1793
|
+
target[position++] = (recordId >> 8) | 0xe0;
|
|
1794
|
+
target[position++] = recordId & 0xff;
|
|
1776
1795
|
} else {
|
|
1777
1796
|
if (!keys)
|
|
1778
1797
|
keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
|
|
@@ -1790,9 +1809,9 @@ class Encoder extends Decoder {
|
|
|
1790
1809
|
}
|
|
1791
1810
|
structures[recordId] = keys;
|
|
1792
1811
|
if (recordId < maxSharedStructures) {
|
|
1793
|
-
target[position
|
|
1794
|
-
target[position
|
|
1795
|
-
target[position
|
|
1812
|
+
target[position++] = 0xd9;
|
|
1813
|
+
target[position++] = (recordId >> 8) | 0xe0;
|
|
1814
|
+
target[position++] = recordId & 0xff;
|
|
1796
1815
|
transition = structures.transitions;
|
|
1797
1816
|
for (let i = 0; i < length; i++) {
|
|
1798
1817
|
if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
|
|
@@ -1803,8 +1822,8 @@ class Encoder extends Decoder {
|
|
|
1803
1822
|
hasSharedUpdate = true;
|
|
1804
1823
|
} else {
|
|
1805
1824
|
transition[RECORD_SYMBOL] = recordId;
|
|
1806
|
-
targetView.setUint32(position
|
|
1807
|
-
position
|
|
1825
|
+
targetView.setUint32(position, 0xd9dfff00); // tag two byte, then record definition id
|
|
1826
|
+
position += 3;
|
|
1808
1827
|
if (newTransitions)
|
|
1809
1828
|
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
1810
1829
|
// record the removal of the id, we can maintain our shared structure
|
|
@@ -1814,15 +1833,19 @@ class Encoder extends Decoder {
|
|
|
1814
1833
|
writeArrayHeader(length + 2);
|
|
1815
1834
|
encode(0xe000 + recordId);
|
|
1816
1835
|
encode(keys);
|
|
1817
|
-
|
|
1836
|
+
if (safePrototype === null) return; // special exit for iterator
|
|
1837
|
+
for (let key in object)
|
|
1838
|
+
if (safePrototype || object.hasOwnProperty(key))
|
|
1839
|
+
encode(object[key]);
|
|
1818
1840
|
return
|
|
1819
1841
|
}
|
|
1820
1842
|
}
|
|
1821
1843
|
if (length < 0x18) { // write the array header
|
|
1822
|
-
target[position
|
|
1844
|
+
target[position++] = 0x80 | length;
|
|
1823
1845
|
} else {
|
|
1824
1846
|
writeArrayHeader(length);
|
|
1825
1847
|
}
|
|
1848
|
+
if (safePrototype === null) return; // special exit for iterator
|
|
1826
1849
|
for (let key in object)
|
|
1827
1850
|
if (safePrototype || object.hasOwnProperty(key))
|
|
1828
1851
|
encode(object[key]);
|
|
@@ -1843,17 +1866,141 @@ class Encoder extends Decoder {
|
|
|
1843
1866
|
target.copy(newBuffer, 0, start, end);
|
|
1844
1867
|
else
|
|
1845
1868
|
newBuffer.set(target.slice(start, end));
|
|
1846
|
-
position
|
|
1869
|
+
position -= start;
|
|
1847
1870
|
start = 0;
|
|
1848
1871
|
safeEnd = newBuffer.length - 10;
|
|
1849
1872
|
return target = newBuffer
|
|
1850
1873
|
};
|
|
1874
|
+
let chunkThreshold = 100;
|
|
1875
|
+
let continuedChunkThreshold = 1000;
|
|
1876
|
+
this.encodeAsIterable = function(value, options) {
|
|
1877
|
+
return startEncoding(value, options, encodeObjectAsIterable);
|
|
1878
|
+
};
|
|
1879
|
+
this.encodeAsAsyncIterable = function(value, options) {
|
|
1880
|
+
return startEncoding(value, options, encodeObjectAsAsyncIterable);
|
|
1881
|
+
};
|
|
1882
|
+
|
|
1883
|
+
function* encodeObjectAsIterable(object, iterateProperties, finalIterable) {
|
|
1884
|
+
let constructor = object.constructor;
|
|
1885
|
+
if (constructor === Object) {
|
|
1886
|
+
let useRecords = encoder.useRecords !== false;
|
|
1887
|
+
if (useRecords)
|
|
1888
|
+
writeObject(object, null); // write the record identifier
|
|
1889
|
+
else
|
|
1890
|
+
writeEntityLength(Object.keys(object).length, 0xa0);
|
|
1891
|
+
for (let key in object) {
|
|
1892
|
+
let value = object[key];
|
|
1893
|
+
if (!useRecords) encode(key);
|
|
1894
|
+
if (value && typeof value === 'object') {
|
|
1895
|
+
if (iterateProperties[key])
|
|
1896
|
+
yield* encodeObjectAsIterable(value, iterateProperties[key]);
|
|
1897
|
+
else
|
|
1898
|
+
yield* tryEncode(value, iterateProperties, key);
|
|
1899
|
+
} else encode(value);
|
|
1900
|
+
}
|
|
1901
|
+
} else if (constructor === Array) {
|
|
1902
|
+
writeArrayHeader(object.length);
|
|
1903
|
+
for (let i = 0; i < length; i++) {
|
|
1904
|
+
let value = object[i];
|
|
1905
|
+
if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
|
|
1906
|
+
if (iterateProperties.element)
|
|
1907
|
+
yield* encodeObjectAsIterable(value, iterateProperties.element);
|
|
1908
|
+
else
|
|
1909
|
+
yield* tryEncode(value, iterateProperties, 'element');
|
|
1910
|
+
} else encode(value);
|
|
1911
|
+
}
|
|
1912
|
+
} else if (object[Symbol.iterator]) {
|
|
1913
|
+
target[position++] = 0x9f; // start indefinite array
|
|
1914
|
+
for (let value of object) {
|
|
1915
|
+
if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
|
|
1916
|
+
if (iterateProperties.element)
|
|
1917
|
+
yield* encodeObjectAsIterable(value, iterateProperties.element);
|
|
1918
|
+
else
|
|
1919
|
+
yield* tryEncode(value, iterateProperties, 'element');
|
|
1920
|
+
} else encode(value);
|
|
1921
|
+
}
|
|
1922
|
+
target[position++] = 0xff; // stop byte
|
|
1923
|
+
} else if (constructor === BlobConstructor){
|
|
1924
|
+
writeEntityLength(object.size, 0x40); // encode as binary data
|
|
1925
|
+
yield target.subarray(start, position);
|
|
1926
|
+
yield object; // directly return blobs, they have to be encoded asynchronously
|
|
1927
|
+
restartEncoding();
|
|
1928
|
+
} else if (object[Symbol.asyncIterator]) {
|
|
1929
|
+
target[position++] = 0x9f; // start indefinite array
|
|
1930
|
+
yield target.subarray(start, position);
|
|
1931
|
+
yield object; // directly return async iterators, they have to be encoded asynchronously
|
|
1932
|
+
restartEncoding();
|
|
1933
|
+
target[position++] = 0xff; // stop byte
|
|
1934
|
+
} else {
|
|
1935
|
+
encode(object);
|
|
1936
|
+
}
|
|
1937
|
+
if (finalIterable && position > start) yield target.subarray(start, position);
|
|
1938
|
+
else if (position - start > chunkThreshold) {
|
|
1939
|
+
yield target.subarray(start, position);
|
|
1940
|
+
restartEncoding();
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
function* tryEncode(value, iterateProperties, key) {
|
|
1944
|
+
let restart = position - start;
|
|
1945
|
+
try {
|
|
1946
|
+
encode(value);
|
|
1947
|
+
if (position - start > chunkThreshold) {
|
|
1948
|
+
yield target.subarray(start, position);
|
|
1949
|
+
restartEncoding();
|
|
1950
|
+
}
|
|
1951
|
+
} catch (error) {
|
|
1952
|
+
if (error.iteratorNotHandled) {
|
|
1953
|
+
iterateProperties[key] = {};
|
|
1954
|
+
position = start + restart; // restart our position so we don't have partial data from last encode
|
|
1955
|
+
yield* encodeObjectAsIterable.call(this, value, iterateProperties[key]);
|
|
1956
|
+
} else throw error;
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
function restartEncoding() {
|
|
1960
|
+
chunkThreshold = continuedChunkThreshold;
|
|
1961
|
+
encoder.encode(null, THROW_ON_ITERABLE); // restart encoding
|
|
1962
|
+
}
|
|
1963
|
+
function startEncoding(value, options, encodeIterable) {
|
|
1964
|
+
if (options && options.chunkThreshold) // explicitly specified chunk sizes
|
|
1965
|
+
chunkThreshold = continuedChunkThreshold = options.chunkThreshold;
|
|
1966
|
+
else // we start with a smaller threshold to get initial bytes sent quickly
|
|
1967
|
+
chunkThreshold = 100;
|
|
1968
|
+
if (value && typeof value === 'object') {
|
|
1969
|
+
encoder.encode(null, THROW_ON_ITERABLE); // start encoding
|
|
1970
|
+
return encodeIterable(value, encoder.iterateProperties || (encoder.iterateProperties = {}), true);
|
|
1971
|
+
}
|
|
1972
|
+
return [encoder.encode(value)];
|
|
1973
|
+
}
|
|
1974
|
+
|
|
1975
|
+
async function* encodeObjectAsAsyncIterable(value, iterateProperties) {
|
|
1976
|
+
for (let encodedValue of encodeObjectAsIterable(value, iterateProperties, true)) {
|
|
1977
|
+
let constructor = encodedValue.constructor;
|
|
1978
|
+
if (constructor === ByteArray || constructor === Uint8Array)
|
|
1979
|
+
yield encodedValue;
|
|
1980
|
+
else if (constructor === BlobConstructor) {
|
|
1981
|
+
let reader = encodedValue.stream().getReader();
|
|
1982
|
+
let next;
|
|
1983
|
+
while (!(next = await reader.read()).done) {
|
|
1984
|
+
yield next.value;
|
|
1985
|
+
}
|
|
1986
|
+
} else if (encodedValue[Symbol.asyncIterator]) {
|
|
1987
|
+
for await (let asyncValue of encodedValue) {
|
|
1988
|
+
restartEncoding();
|
|
1989
|
+
if (asyncValue)
|
|
1990
|
+
yield* encodeObjectAsAsyncIterable(asyncValue, iterateProperties.async || (iterateProperties.async = {}));
|
|
1991
|
+
else yield encoder.encode(asyncValue);
|
|
1992
|
+
}
|
|
1993
|
+
} else {
|
|
1994
|
+
yield encodedValue;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1851
1998
|
}
|
|
1852
1999
|
useBuffer(buffer) {
|
|
1853
2000
|
// this means we are finished using our own buffer and we can write over it safely
|
|
1854
2001
|
target = buffer;
|
|
1855
2002
|
targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
|
|
1856
|
-
position
|
|
2003
|
+
position = 0;
|
|
1857
2004
|
}
|
|
1858
2005
|
clearSharedData() {
|
|
1859
2006
|
if (this.structures)
|
|
@@ -1883,6 +2030,23 @@ class Encoder extends Decoder {
|
|
|
1883
2030
|
return saveResults
|
|
1884
2031
|
}
|
|
1885
2032
|
}
|
|
2033
|
+
function writeEntityLength(length, majorValue) {
|
|
2034
|
+
if (length < 0x18)
|
|
2035
|
+
target[position++] = majorValue | length;
|
|
2036
|
+
else if (length < 0x100) {
|
|
2037
|
+
target[position++] = majorValue | 0x18;
|
|
2038
|
+
target[position++] = length;
|
|
2039
|
+
} else if (length < 0x10000) {
|
|
2040
|
+
target[position++] = majorValue | 0x19;
|
|
2041
|
+
target[position++] = length >> 8;
|
|
2042
|
+
target[position++] = length & 0xff;
|
|
2043
|
+
} else {
|
|
2044
|
+
target[position++] = majorValue | 0x1a;
|
|
2045
|
+
targetView.setUint32(position, length);
|
|
2046
|
+
position += 4;
|
|
2047
|
+
}
|
|
2048
|
+
|
|
2049
|
+
}
|
|
1886
2050
|
class SharedData {
|
|
1887
2051
|
constructor(structures, values, version) {
|
|
1888
2052
|
this.structures = structures;
|
|
@@ -1893,18 +2057,18 @@ class SharedData {
|
|
|
1893
2057
|
|
|
1894
2058
|
function writeArrayHeader(length) {
|
|
1895
2059
|
if (length < 0x18)
|
|
1896
|
-
target[position
|
|
2060
|
+
target[position++] = 0x80 | length;
|
|
1897
2061
|
else if (length < 0x100) {
|
|
1898
|
-
target[position
|
|
1899
|
-
target[position
|
|
2062
|
+
target[position++] = 0x98;
|
|
2063
|
+
target[position++] = length;
|
|
1900
2064
|
} else if (length < 0x10000) {
|
|
1901
|
-
target[position
|
|
1902
|
-
target[position
|
|
1903
|
-
target[position
|
|
2065
|
+
target[position++] = 0x99;
|
|
2066
|
+
target[position++] = length >> 8;
|
|
2067
|
+
target[position++] = length & 0xff;
|
|
1904
2068
|
} else {
|
|
1905
|
-
target[position
|
|
1906
|
-
targetView.setUint32(position
|
|
1907
|
-
position
|
|
2069
|
+
target[position++] = 0x9a;
|
|
2070
|
+
targetView.setUint32(position, length);
|
|
2071
|
+
position += 4;
|
|
1908
2072
|
}
|
|
1909
2073
|
}
|
|
1910
2074
|
|
|
@@ -1957,7 +2121,7 @@ function findRepetitiveStrings(value, packedValues) {
|
|
|
1957
2121
|
case 'function': console.log(value);
|
|
1958
2122
|
}
|
|
1959
2123
|
}
|
|
1960
|
-
const isLittleEndianMachine
|
|
2124
|
+
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
1961
2125
|
extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
|
|
1962
2126
|
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
1963
2127
|
typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
@@ -1971,14 +2135,14 @@ extensions = [{ // Date
|
|
|
1971
2135
|
let seconds = date.getTime() / 1000;
|
|
1972
2136
|
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
|
|
1973
2137
|
// Timestamp 32
|
|
1974
|
-
target[position
|
|
1975
|
-
targetView.setUint32(position
|
|
1976
|
-
position
|
|
2138
|
+
target[position++] = 0x1a;
|
|
2139
|
+
targetView.setUint32(position, seconds);
|
|
2140
|
+
position += 4;
|
|
1977
2141
|
} else {
|
|
1978
2142
|
// Timestamp float64
|
|
1979
|
-
target[position
|
|
1980
|
-
targetView.setFloat64(position
|
|
1981
|
-
position
|
|
2143
|
+
target[position++] = 0xfb;
|
|
2144
|
+
targetView.setFloat64(position, seconds);
|
|
2145
|
+
position += 8;
|
|
1982
2146
|
}
|
|
1983
2147
|
}
|
|
1984
2148
|
}, { // Set
|
|
@@ -2034,8 +2198,8 @@ extensions = [{ // Date
|
|
|
2034
2198
|
let packedValues = sharedData.packedValues || [];
|
|
2035
2199
|
let sharedStructures = sharedData.structures || [];
|
|
2036
2200
|
if (packedValues.values.length > 0) {
|
|
2037
|
-
target[position
|
|
2038
|
-
target[position
|
|
2201
|
+
target[position++] = 0xd8; // one-byte tag
|
|
2202
|
+
target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
2039
2203
|
writeArrayHeader(4);
|
|
2040
2204
|
let valuesArray = packedValues.values;
|
|
2041
2205
|
encode(valuesArray);
|
|
@@ -2047,8 +2211,8 @@ extensions = [{ // Date
|
|
|
2047
2211
|
}
|
|
2048
2212
|
}
|
|
2049
2213
|
if (sharedStructures) {
|
|
2050
|
-
targetView.setUint32(position
|
|
2051
|
-
position
|
|
2214
|
+
targetView.setUint32(position, 0xd9dffe00);
|
|
2215
|
+
position += 3;
|
|
2052
2216
|
let definitions = sharedStructures.slice(0);
|
|
2053
2217
|
definitions.unshift(0xe000);
|
|
2054
2218
|
definitions.push(new Tag(sharedData.version, 0x53687264));
|
|
@@ -2058,7 +2222,7 @@ extensions = [{ // Date
|
|
|
2058
2222
|
}
|
|
2059
2223
|
}];
|
|
2060
2224
|
function typedArrayEncoder(tag, size) {
|
|
2061
|
-
if (!isLittleEndianMachine
|
|
2225
|
+
if (!isLittleEndianMachine && size > 1)
|
|
2062
2226
|
tag -= 4; // the big endian equivalents are 4 less
|
|
2063
2227
|
return {
|
|
2064
2228
|
tag: tag,
|
|
@@ -2074,26 +2238,26 @@ function typedArrayEncoder(tag, size) {
|
|
|
2074
2238
|
function writeBuffer(buffer, makeRoom) {
|
|
2075
2239
|
let length = buffer.byteLength;
|
|
2076
2240
|
if (length < 0x18) {
|
|
2077
|
-
target[position
|
|
2241
|
+
target[position++] = 0x40 + length;
|
|
2078
2242
|
} else if (length < 0x100) {
|
|
2079
|
-
target[position
|
|
2080
|
-
target[position
|
|
2243
|
+
target[position++] = 0x58;
|
|
2244
|
+
target[position++] = length;
|
|
2081
2245
|
} else if (length < 0x10000) {
|
|
2082
|
-
target[position
|
|
2083
|
-
target[position
|
|
2084
|
-
target[position
|
|
2246
|
+
target[position++] = 0x59;
|
|
2247
|
+
target[position++] = length >> 8;
|
|
2248
|
+
target[position++] = length & 0xff;
|
|
2085
2249
|
} else {
|
|
2086
|
-
target[position
|
|
2087
|
-
targetView.setUint32(position
|
|
2088
|
-
position
|
|
2250
|
+
target[position++] = 0x5a;
|
|
2251
|
+
targetView.setUint32(position, length);
|
|
2252
|
+
position += 4;
|
|
2089
2253
|
}
|
|
2090
|
-
if (position
|
|
2091
|
-
makeRoom(position
|
|
2254
|
+
if (position + length >= target.length) {
|
|
2255
|
+
makeRoom(position + length);
|
|
2092
2256
|
}
|
|
2093
2257
|
// if it is already a typed array (has an ArrayBuffer), use that, but if it is an ArrayBuffer itself,
|
|
2094
2258
|
// must wrap it to set it.
|
|
2095
|
-
target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position
|
|
2096
|
-
position
|
|
2259
|
+
target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position);
|
|
2260
|
+
position += length;
|
|
2097
2261
|
}
|
|
2098
2262
|
|
|
2099
2263
|
function insertIds(serialized, idsToInsert) {
|
|
@@ -2122,27 +2286,30 @@ function insertIds(serialized, idsToInsert) {
|
|
|
2122
2286
|
return serialized
|
|
2123
2287
|
}
|
|
2124
2288
|
function writeBundles(start, encode) {
|
|
2125
|
-
targetView.setUint32(bundledStrings
|
|
2126
|
-
let writeStrings = bundledStrings
|
|
2127
|
-
bundledStrings
|
|
2289
|
+
targetView.setUint32(bundledStrings.position + start, position - bundledStrings.position - start + 1); // the offset to bundle
|
|
2290
|
+
let writeStrings = bundledStrings;
|
|
2291
|
+
bundledStrings = null;
|
|
2128
2292
|
encode(writeStrings[0]);
|
|
2129
2293
|
encode(writeStrings[1]);
|
|
2130
2294
|
}
|
|
2131
2295
|
|
|
2132
|
-
function addExtension
|
|
2296
|
+
function addExtension(extension) {
|
|
2133
2297
|
if (extension.Class) {
|
|
2134
2298
|
if (!extension.encode)
|
|
2135
2299
|
throw new Error('Extension has no encode function')
|
|
2136
2300
|
extensionClasses.unshift(extension.Class);
|
|
2137
2301
|
extensions.unshift(extension);
|
|
2138
2302
|
}
|
|
2139
|
-
addExtension(extension);
|
|
2303
|
+
addExtension$1(extension);
|
|
2140
2304
|
}
|
|
2141
2305
|
let defaultEncoder = new Encoder({ useRecords: false });
|
|
2142
2306
|
const encode = defaultEncoder.encode;
|
|
2307
|
+
const encodeAsIterable = defaultEncoder.encodeAsIterable;
|
|
2308
|
+
const encodeAsAsyncIterable = defaultEncoder.encodeAsAsyncIterable;
|
|
2143
2309
|
const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
|
|
2144
2310
|
const REUSE_BUFFER_MODE = 512;
|
|
2145
2311
|
const RESET_BUFFER_MODE = 1024;
|
|
2312
|
+
const THROW_ON_ITERABLE = 2048;
|
|
2146
2313
|
|
|
2147
2314
|
class EncoderStream extends stream.Transform {
|
|
2148
2315
|
constructor(options) {
|
|
@@ -2153,8 +2320,10 @@ class EncoderStream extends stream.Transform {
|
|
|
2153
2320
|
options.sequential = true;
|
|
2154
2321
|
this.encoder = options.encoder || new Encoder(options);
|
|
2155
2322
|
}
|
|
2156
|
-
_transform(value, encoding, callback) {
|
|
2157
|
-
|
|
2323
|
+
async _transform(value, encoding, callback) {
|
|
2324
|
+
for await (let chunk of this.encoder.encodeAsAsyncIterable(value)) {
|
|
2325
|
+
this.push(chunk);
|
|
2326
|
+
}
|
|
2158
2327
|
callback();
|
|
2159
2328
|
}
|
|
2160
2329
|
}
|
|
@@ -2310,13 +2479,16 @@ exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
|
|
|
2310
2479
|
exports.NEVER = NEVER;
|
|
2311
2480
|
exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
|
|
2312
2481
|
exports.Tag = Tag;
|
|
2313
|
-
exports.addExtension = addExtension
|
|
2482
|
+
exports.addExtension = addExtension;
|
|
2314
2483
|
exports.clearSource = clearSource;
|
|
2315
2484
|
exports.decode = decode;
|
|
2316
2485
|
exports.decodeIter = decodeIter;
|
|
2317
2486
|
exports.decodeMultiple = decodeMultiple;
|
|
2318
2487
|
exports.encode = encode;
|
|
2488
|
+
exports.encodeAsAsyncIterable = encodeAsAsyncIterable;
|
|
2489
|
+
exports.encodeAsIterable = encodeAsIterable;
|
|
2319
2490
|
exports.encodeIter = encodeIter;
|
|
2320
2491
|
exports.mapsAsObjects = mapsAsObjects;
|
|
2321
2492
|
exports.roundFloat32 = roundFloat32;
|
|
2322
2493
|
exports.useRecords = useRecords;
|
|
2494
|
+
//# sourceMappingURL=node.cjs.map
|