cbor-x 1.4.0 → 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 +21 -1
- package/decode.js +56 -34
- 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 +549 -363
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +2 -135
- package/dist/index.min.js.map +1 -0
- package/dist/node.cjs +556 -368
- package/dist/node.cjs.map +1 -0
- package/dist/test.js +181 -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/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 = [];
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
}
|
|
86
86
|
let res = {};
|
|
87
87
|
//map.forEach((v,k) => res[Object.keys(this._keyMap)[Object.values(this._keyMap).indexOf(k)] || k] = v)
|
|
88
|
-
map.forEach((v,k) => res[this._mapKey.has(k) ? this._mapKey.get(k) : k] = v);
|
|
88
|
+
map.forEach((v,k) => res[safeKey(this._mapKey.has(k) ? this._mapKey.get(k) : k)] = v);
|
|
89
89
|
return res
|
|
90
90
|
}
|
|
91
91
|
|
|
@@ -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
|
|
@@ -285,8 +286,8 @@
|
|
|
285
286
|
let key;
|
|
286
287
|
if (currentDecoder.mapsAsObjects) {
|
|
287
288
|
let object = {};
|
|
288
|
-
if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[currentDecoder.decodeKey(key)] = read();
|
|
289
|
-
else while ((key = read()) != STOP_CODE) object[key] = read();
|
|
289
|
+
if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[safeKey(currentDecoder.decodeKey(key))] = read();
|
|
290
|
+
else while ((key = read()) != STOP_CODE) object[safeKey(key)] = read();
|
|
290
291
|
return object
|
|
291
292
|
} else {
|
|
292
293
|
if (restoreMapsAsObject) {
|
|
@@ -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
|
|
@@ -334,8 +335,8 @@
|
|
|
334
335
|
case 5: // map
|
|
335
336
|
if (currentDecoder.mapsAsObjects) {
|
|
336
337
|
let object = {};
|
|
337
|
-
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[currentDecoder.decodeKey(read())] = read();
|
|
338
|
-
else for (let i = 0; i < token; i++) object[read()] = read();
|
|
338
|
+
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
|
|
339
|
+
else for (let i = 0; i < token; i++) object[safeKey(read())] = read();
|
|
339
340
|
return object
|
|
340
341
|
} else {
|
|
341
342
|
if (restoreMapsAsObject) {
|
|
@@ -420,24 +421,24 @@
|
|
|
420
421
|
function createStructureReader(structure) {
|
|
421
422
|
function readObject() {
|
|
422
423
|
// get the array size from the header
|
|
423
|
-
let length = src[position++];
|
|
424
|
+
let length = src[position$1++];
|
|
424
425
|
//let majorType = token >> 5
|
|
425
426
|
length = length & 0x1f;
|
|
426
427
|
if (length > 0x17) {
|
|
427
428
|
switch (length) {
|
|
428
429
|
case 0x18:
|
|
429
|
-
length = src[position++];
|
|
430
|
+
length = src[position$1++];
|
|
430
431
|
break
|
|
431
432
|
case 0x19:
|
|
432
|
-
length = dataView.getUint16(position);
|
|
433
|
-
position += 2;
|
|
433
|
+
length = dataView.getUint16(position$1);
|
|
434
|
+
position$1 += 2;
|
|
434
435
|
break
|
|
435
436
|
case 0x1a:
|
|
436
|
-
length = dataView.getUint32(position);
|
|
437
|
-
position += 4;
|
|
437
|
+
length = dataView.getUint32(position$1);
|
|
438
|
+
position$1 += 4;
|
|
438
439
|
break
|
|
439
440
|
default:
|
|
440
|
-
throw new Error('Expected array header, but got ' + src[position - 1])
|
|
441
|
+
throw new Error('Expected array header, but got ' + src[position$1 - 1])
|
|
441
442
|
}
|
|
442
443
|
}
|
|
443
444
|
// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
|
|
@@ -451,8 +452,8 @@
|
|
|
451
452
|
if (this.slowReads++ >= 3) { // create a fast compiled reader
|
|
452
453
|
let array = this.length == length ? this : this.slice(0, length);
|
|
453
454
|
compiledReader = currentDecoder.keyMap
|
|
454
|
-
? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? k + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
|
|
455
|
-
: new Function('r', 'return {' + array.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}');
|
|
455
|
+
? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? safeKey(k) + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
|
|
456
|
+
: new Function('r', 'return {' + array.map(key => validName.test(key) ? safeKey(key) + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}');
|
|
456
457
|
if (this.compiledReader)
|
|
457
458
|
compiledReader.next = this.compiledReader; // if there is an existing one, we store multiple readers as a linked list because it is usually pretty rare to have multiple readers (of different length) for the same structure
|
|
458
459
|
compiledReader.propertyCount = length;
|
|
@@ -460,14 +461,20 @@
|
|
|
460
461
|
return compiledReader(read)
|
|
461
462
|
}
|
|
462
463
|
let object = {};
|
|
463
|
-
if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[currentDecoder.decodeKey(this[i])] = read();
|
|
464
|
-
else for (let i = 0; i < length; i++)
|
|
464
|
+
if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[safeKey(currentDecoder.decodeKey(this[i]))] = read();
|
|
465
|
+
else for (let i = 0; i < length; i++) {
|
|
466
|
+
object[safeKey(this[i])] = read();
|
|
467
|
+
}
|
|
465
468
|
return object
|
|
466
469
|
}
|
|
467
470
|
structure.slowReads = 0;
|
|
468
471
|
return readObject
|
|
469
472
|
}
|
|
470
473
|
|
|
474
|
+
function safeKey(key) {
|
|
475
|
+
return key === '__proto__' ? '__proto_' : key
|
|
476
|
+
}
|
|
477
|
+
|
|
471
478
|
let readFixedString = readStringJS;
|
|
472
479
|
|
|
473
480
|
let isNativeAccelerationEnabled = false;
|
|
@@ -478,29 +485,29 @@
|
|
|
478
485
|
return result
|
|
479
486
|
}
|
|
480
487
|
if (length > 64 && decoder)
|
|
481
|
-
return decoder.decode(src.subarray(position, position += length))
|
|
482
|
-
const end = position + length;
|
|
488
|
+
return decoder.decode(src.subarray(position$1, position$1 += length))
|
|
489
|
+
const end = position$1 + length;
|
|
483
490
|
const units = [];
|
|
484
491
|
result = '';
|
|
485
|
-
while (position < end) {
|
|
486
|
-
const byte1 = src[position++];
|
|
492
|
+
while (position$1 < end) {
|
|
493
|
+
const byte1 = src[position$1++];
|
|
487
494
|
if ((byte1 & 0x80) === 0) {
|
|
488
495
|
// 1 byte
|
|
489
496
|
units.push(byte1);
|
|
490
497
|
} else if ((byte1 & 0xe0) === 0xc0) {
|
|
491
498
|
// 2 bytes
|
|
492
|
-
const byte2 = src[position++] & 0x3f;
|
|
499
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
493
500
|
units.push(((byte1 & 0x1f) << 6) | byte2);
|
|
494
501
|
} else if ((byte1 & 0xf0) === 0xe0) {
|
|
495
502
|
// 3 bytes
|
|
496
|
-
const byte2 = src[position++] & 0x3f;
|
|
497
|
-
const byte3 = src[position++] & 0x3f;
|
|
503
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
504
|
+
const byte3 = src[position$1++] & 0x3f;
|
|
498
505
|
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
|
|
499
506
|
} else if ((byte1 & 0xf8) === 0xf0) {
|
|
500
507
|
// 4 bytes
|
|
501
|
-
const byte2 = src[position++] & 0x3f;
|
|
502
|
-
const byte3 = src[position++] & 0x3f;
|
|
503
|
-
const byte4 = src[position++] & 0x3f;
|
|
508
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
509
|
+
const byte3 = src[position$1++] & 0x3f;
|
|
510
|
+
const byte4 = src[position$1++] & 0x3f;
|
|
504
511
|
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
|
505
512
|
if (unit > 0xffff) {
|
|
506
513
|
unit -= 0x10000;
|
|
@@ -526,12 +533,12 @@
|
|
|
526
533
|
}
|
|
527
534
|
let fromCharCode = String.fromCharCode;
|
|
528
535
|
function longStringInJS(length) {
|
|
529
|
-
let start = position;
|
|
536
|
+
let start = position$1;
|
|
530
537
|
let bytes = new Array(length);
|
|
531
538
|
for (let i = 0; i < length; i++) {
|
|
532
|
-
const byte = src[position++];
|
|
539
|
+
const byte = src[position$1++];
|
|
533
540
|
if ((byte & 0x80) > 0) {
|
|
534
|
-
position = start;
|
|
541
|
+
position$1 = start;
|
|
535
542
|
return
|
|
536
543
|
}
|
|
537
544
|
bytes[i] = byte;
|
|
@@ -544,131 +551,131 @@
|
|
|
544
551
|
if (length === 0)
|
|
545
552
|
return ''
|
|
546
553
|
else {
|
|
547
|
-
let a = src[position++];
|
|
554
|
+
let a = src[position$1++];
|
|
548
555
|
if ((a & 0x80) > 1) {
|
|
549
|
-
position -= 1;
|
|
556
|
+
position$1 -= 1;
|
|
550
557
|
return
|
|
551
558
|
}
|
|
552
559
|
return fromCharCode(a)
|
|
553
560
|
}
|
|
554
561
|
} else {
|
|
555
|
-
let a = src[position++];
|
|
556
|
-
let b = src[position++];
|
|
562
|
+
let a = src[position$1++];
|
|
563
|
+
let b = src[position$1++];
|
|
557
564
|
if ((a & 0x80) > 0 || (b & 0x80) > 0) {
|
|
558
|
-
position -= 2;
|
|
565
|
+
position$1 -= 2;
|
|
559
566
|
return
|
|
560
567
|
}
|
|
561
568
|
if (length < 3)
|
|
562
569
|
return fromCharCode(a, b)
|
|
563
|
-
let c = src[position++];
|
|
570
|
+
let c = src[position$1++];
|
|
564
571
|
if ((c & 0x80) > 0) {
|
|
565
|
-
position -= 3;
|
|
572
|
+
position$1 -= 3;
|
|
566
573
|
return
|
|
567
574
|
}
|
|
568
575
|
return fromCharCode(a, b, c)
|
|
569
576
|
}
|
|
570
577
|
} else {
|
|
571
|
-
let a = src[position++];
|
|
572
|
-
let b = src[position++];
|
|
573
|
-
let c = src[position++];
|
|
574
|
-
let d = src[position++];
|
|
578
|
+
let a = src[position$1++];
|
|
579
|
+
let b = src[position$1++];
|
|
580
|
+
let c = src[position$1++];
|
|
581
|
+
let d = src[position$1++];
|
|
575
582
|
if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
|
|
576
|
-
position -= 4;
|
|
583
|
+
position$1 -= 4;
|
|
577
584
|
return
|
|
578
585
|
}
|
|
579
586
|
if (length < 6) {
|
|
580
587
|
if (length === 4)
|
|
581
588
|
return fromCharCode(a, b, c, d)
|
|
582
589
|
else {
|
|
583
|
-
let e = src[position++];
|
|
590
|
+
let e = src[position$1++];
|
|
584
591
|
if ((e & 0x80) > 0) {
|
|
585
|
-
position -= 5;
|
|
592
|
+
position$1 -= 5;
|
|
586
593
|
return
|
|
587
594
|
}
|
|
588
595
|
return fromCharCode(a, b, c, d, e)
|
|
589
596
|
}
|
|
590
597
|
} else if (length < 8) {
|
|
591
|
-
let e = src[position++];
|
|
592
|
-
let f = src[position++];
|
|
598
|
+
let e = src[position$1++];
|
|
599
|
+
let f = src[position$1++];
|
|
593
600
|
if ((e & 0x80) > 0 || (f & 0x80) > 0) {
|
|
594
|
-
position -= 6;
|
|
601
|
+
position$1 -= 6;
|
|
595
602
|
return
|
|
596
603
|
}
|
|
597
604
|
if (length < 7)
|
|
598
605
|
return fromCharCode(a, b, c, d, e, f)
|
|
599
|
-
let g = src[position++];
|
|
606
|
+
let g = src[position$1++];
|
|
600
607
|
if ((g & 0x80) > 0) {
|
|
601
|
-
position -= 7;
|
|
608
|
+
position$1 -= 7;
|
|
602
609
|
return
|
|
603
610
|
}
|
|
604
611
|
return fromCharCode(a, b, c, d, e, f, g)
|
|
605
612
|
} else {
|
|
606
|
-
let e = src[position++];
|
|
607
|
-
let f = src[position++];
|
|
608
|
-
let g = src[position++];
|
|
609
|
-
let h = src[position++];
|
|
613
|
+
let e = src[position$1++];
|
|
614
|
+
let f = src[position$1++];
|
|
615
|
+
let g = src[position$1++];
|
|
616
|
+
let h = src[position$1++];
|
|
610
617
|
if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
|
|
611
|
-
position -= 8;
|
|
618
|
+
position$1 -= 8;
|
|
612
619
|
return
|
|
613
620
|
}
|
|
614
621
|
if (length < 10) {
|
|
615
622
|
if (length === 8)
|
|
616
623
|
return fromCharCode(a, b, c, d, e, f, g, h)
|
|
617
624
|
else {
|
|
618
|
-
let i = src[position++];
|
|
625
|
+
let i = src[position$1++];
|
|
619
626
|
if ((i & 0x80) > 0) {
|
|
620
|
-
position -= 9;
|
|
627
|
+
position$1 -= 9;
|
|
621
628
|
return
|
|
622
629
|
}
|
|
623
630
|
return fromCharCode(a, b, c, d, e, f, g, h, i)
|
|
624
631
|
}
|
|
625
632
|
} else if (length < 12) {
|
|
626
|
-
let i = src[position++];
|
|
627
|
-
let j = src[position++];
|
|
633
|
+
let i = src[position$1++];
|
|
634
|
+
let j = src[position$1++];
|
|
628
635
|
if ((i & 0x80) > 0 || (j & 0x80) > 0) {
|
|
629
|
-
position -= 10;
|
|
636
|
+
position$1 -= 10;
|
|
630
637
|
return
|
|
631
638
|
}
|
|
632
639
|
if (length < 11)
|
|
633
640
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j)
|
|
634
|
-
let k = src[position++];
|
|
641
|
+
let k = src[position$1++];
|
|
635
642
|
if ((k & 0x80) > 0) {
|
|
636
|
-
position -= 11;
|
|
643
|
+
position$1 -= 11;
|
|
637
644
|
return
|
|
638
645
|
}
|
|
639
646
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
|
|
640
647
|
} else {
|
|
641
|
-
let i = src[position++];
|
|
642
|
-
let j = src[position++];
|
|
643
|
-
let k = src[position++];
|
|
644
|
-
let l = src[position++];
|
|
648
|
+
let i = src[position$1++];
|
|
649
|
+
let j = src[position$1++];
|
|
650
|
+
let k = src[position$1++];
|
|
651
|
+
let l = src[position$1++];
|
|
645
652
|
if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
|
|
646
|
-
position -= 12;
|
|
653
|
+
position$1 -= 12;
|
|
647
654
|
return
|
|
648
655
|
}
|
|
649
656
|
if (length < 14) {
|
|
650
657
|
if (length === 12)
|
|
651
658
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
|
|
652
659
|
else {
|
|
653
|
-
let m = src[position++];
|
|
660
|
+
let m = src[position$1++];
|
|
654
661
|
if ((m & 0x80) > 0) {
|
|
655
|
-
position -= 13;
|
|
662
|
+
position$1 -= 13;
|
|
656
663
|
return
|
|
657
664
|
}
|
|
658
665
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
|
|
659
666
|
}
|
|
660
667
|
} else {
|
|
661
|
-
let m = src[position++];
|
|
662
|
-
let n = src[position++];
|
|
668
|
+
let m = src[position$1++];
|
|
669
|
+
let n = src[position$1++];
|
|
663
670
|
if ((m & 0x80) > 0 || (n & 0x80) > 0) {
|
|
664
|
-
position -= 14;
|
|
671
|
+
position$1 -= 14;
|
|
665
672
|
return
|
|
666
673
|
}
|
|
667
674
|
if (length < 15)
|
|
668
675
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
|
|
669
|
-
let o = src[position++];
|
|
676
|
+
let o = src[position$1++];
|
|
670
677
|
if ((o & 0x80) > 0) {
|
|
671
|
-
position -= 15;
|
|
678
|
+
position$1 -= 15;
|
|
672
679
|
return
|
|
673
680
|
}
|
|
674
681
|
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
|
|
@@ -681,24 +688,36 @@
|
|
|
681
688
|
function readBin(length) {
|
|
682
689
|
return currentDecoder.copyBuffers ?
|
|
683
690
|
// specifically use the copying slice (not the node one)
|
|
684
|
-
Uint8Array.prototype.slice.call(src, position, position += length) :
|
|
685
|
-
src.subarray(position, position += length)
|
|
691
|
+
Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
|
|
692
|
+
src.subarray(position$1, position$1 += length)
|
|
686
693
|
}
|
|
687
|
-
|
|
694
|
+
let f32Array = new Float32Array(1);
|
|
695
|
+
let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
688
696
|
function getFloat16() {
|
|
689
|
-
let byte0 = src[position++];
|
|
690
|
-
let byte1 = src[position++];
|
|
691
|
-
let
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
697
|
+
let byte0 = src[position$1++];
|
|
698
|
+
let byte1 = src[position$1++];
|
|
699
|
+
let exponent = (byte0 & 0x7f) >> 2;
|
|
700
|
+
if (exponent === 0x1f) { // specials
|
|
701
|
+
if (byte1 || (byte0 & 3))
|
|
702
|
+
return NaN;
|
|
703
|
+
return (byte0 & 0x80) ? -Infinity : Infinity;
|
|
704
|
+
}
|
|
705
|
+
if (exponent === 0) { // sub-normals
|
|
706
|
+
// significand with 10 fractional bits and divided by 2^14
|
|
707
|
+
let abs = (((byte0 & 3) << 8) | byte1) / (1 << 24);
|
|
708
|
+
return (byte0 & 0x80) ? -abs : abs
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
u8Array[3] = (byte0 & 0x80) | // sign bit
|
|
712
|
+
((exponent >> 1) + 56); // 4 of 5 of the exponent bits, re-offset-ed
|
|
713
|
+
u8Array[2] = ((byte0 & 7) << 5) | // last exponent bit and first two mantissa bits
|
|
714
|
+
(byte1 >> 3); // next 5 bits of mantissa
|
|
715
|
+
u8Array[1] = byte1 << 5; // last three bits of mantissa
|
|
716
|
+
u8Array[0] = 0;
|
|
717
|
+
return f32Array[0];
|
|
699
718
|
}
|
|
700
719
|
|
|
701
|
-
|
|
720
|
+
new Array(4096);
|
|
702
721
|
|
|
703
722
|
class Tag {
|
|
704
723
|
constructor(value, tag) {
|
|
@@ -754,23 +773,23 @@
|
|
|
754
773
|
let object = {};
|
|
755
774
|
if (currentDecoder.keyMap) for (let i = 2,l = definition.length; i < l; i++) {
|
|
756
775
|
let key = currentDecoder.decodeKey(structure[i - 2]);
|
|
757
|
-
object[key] = definition[i];
|
|
776
|
+
object[safeKey(key)] = definition[i];
|
|
758
777
|
}
|
|
759
778
|
else for (let i = 2,l = definition.length; i < l; i++) {
|
|
760
779
|
let key = structure[i - 2];
|
|
761
|
-
object[key] = definition[i];
|
|
780
|
+
object[safeKey(key)] = definition[i];
|
|
762
781
|
}
|
|
763
782
|
return object
|
|
764
783
|
};
|
|
765
784
|
currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition;
|
|
766
785
|
currentExtensions[14] = (value) => {
|
|
767
|
-
if (bundledStrings)
|
|
768
|
-
return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 += value)
|
|
786
|
+
if (bundledStrings$1)
|
|
787
|
+
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
|
|
769
788
|
return new Tag(value, 14)
|
|
770
789
|
};
|
|
771
790
|
currentExtensions[15] = (value) => {
|
|
772
|
-
if (bundledStrings)
|
|
773
|
-
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
|
|
791
|
+
if (bundledStrings$1)
|
|
792
|
+
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
|
|
774
793
|
return new Tag(value, 15)
|
|
775
794
|
};
|
|
776
795
|
let glbl = { Error, RegExp };
|
|
@@ -778,7 +797,7 @@
|
|
|
778
797
|
return (glbl[data[0]] || Error)(data[1], data[2])
|
|
779
798
|
};
|
|
780
799
|
const packedTable = (read) => {
|
|
781
|
-
if (src[position++] != 0x84)
|
|
800
|
+
if (src[position$1++] != 0x84)
|
|
782
801
|
throw new Error('Packed values structure must be followed by a 4 element array')
|
|
783
802
|
let newPackedValues = read(); // packed values
|
|
784
803
|
packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues;
|
|
@@ -800,18 +819,23 @@
|
|
|
800
819
|
return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
|
|
801
820
|
throw new Error('No support for non-integer packed references yet')
|
|
802
821
|
};
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
822
|
+
|
|
823
|
+
// The following code is an incomplete implementation of http://cbor.schmorp.de/stringref
|
|
824
|
+
// the real thing would need to implemennt more logic to populate the stringRefs table and
|
|
825
|
+
// maintain a stack of stringRef "namespaces".
|
|
826
|
+
//
|
|
827
|
+
// currentExtensions[25] = (id) => {
|
|
828
|
+
// return stringRefs[id]
|
|
829
|
+
// }
|
|
830
|
+
// currentExtensions[256] = (read) => {
|
|
831
|
+
// stringRefs = []
|
|
832
|
+
// try {
|
|
833
|
+
// return read()
|
|
834
|
+
// } finally {
|
|
835
|
+
// stringRefs = null
|
|
836
|
+
// }
|
|
837
|
+
// }
|
|
838
|
+
// currentExtensions[256].handlesRead = true
|
|
815
839
|
|
|
816
840
|
currentExtensions[28] = (read) => {
|
|
817
841
|
// shareable http://cbor.schmorp.de/value-sharing (for structured clones)
|
|
@@ -820,7 +844,7 @@
|
|
|
820
844
|
referenceMap.id = 0;
|
|
821
845
|
}
|
|
822
846
|
let id = referenceMap.id++;
|
|
823
|
-
let token = src[position];
|
|
847
|
+
let token = src[position$1];
|
|
824
848
|
let target;
|
|
825
849
|
// TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
|
|
826
850
|
// ahead past references to record structure definitions
|
|
@@ -897,7 +921,7 @@
|
|
|
897
921
|
return input
|
|
898
922
|
});
|
|
899
923
|
|
|
900
|
-
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
924
|
+
const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
901
925
|
const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
902
926
|
typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
903
927
|
typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
|
|
@@ -914,7 +938,7 @@
|
|
|
914
938
|
if (!littleEndian && bytesPerElement == 1)
|
|
915
939
|
continue
|
|
916
940
|
let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : 3;
|
|
917
|
-
currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine) ? (buffer) => {
|
|
941
|
+
currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
|
|
918
942
|
if (!TypedArray)
|
|
919
943
|
throw new Error('Could not find typed array for code ' + tag)
|
|
920
944
|
// we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
|
|
@@ -936,36 +960,36 @@
|
|
|
936
960
|
|
|
937
961
|
function readBundleExt() {
|
|
938
962
|
let length = readJustLength();
|
|
939
|
-
let bundlePosition = position + read();
|
|
963
|
+
let bundlePosition = position$1 + read();
|
|
940
964
|
for (let i = 2; i < length; i++) {
|
|
941
965
|
// skip past bundles that were already read
|
|
942
966
|
let bundleLength = readJustLength(); // this will increment position, so must add to position afterwards
|
|
943
|
-
position += bundleLength;
|
|
967
|
+
position$1 += bundleLength;
|
|
944
968
|
}
|
|
945
|
-
let dataPosition = position;
|
|
946
|
-
position = bundlePosition;
|
|
947
|
-
bundledStrings = [readStringJS(readJustLength()), readStringJS(readJustLength())];
|
|
948
|
-
bundledStrings.position0 = 0;
|
|
949
|
-
bundledStrings.position1 = 0;
|
|
950
|
-
bundledStrings.postBundlePosition = position;
|
|
951
|
-
position = dataPosition;
|
|
969
|
+
let dataPosition = position$1;
|
|
970
|
+
position$1 = bundlePosition;
|
|
971
|
+
bundledStrings$1 = [readStringJS(readJustLength()), readStringJS(readJustLength())];
|
|
972
|
+
bundledStrings$1.position0 = 0;
|
|
973
|
+
bundledStrings$1.position1 = 0;
|
|
974
|
+
bundledStrings$1.postBundlePosition = position$1;
|
|
975
|
+
position$1 = dataPosition;
|
|
952
976
|
return read()
|
|
953
977
|
}
|
|
954
978
|
|
|
955
979
|
function readJustLength() {
|
|
956
|
-
let token = src[position++] & 0x1f;
|
|
980
|
+
let token = src[position$1++] & 0x1f;
|
|
957
981
|
if (token > 0x17) {
|
|
958
982
|
switch (token) {
|
|
959
983
|
case 0x18:
|
|
960
|
-
token = src[position++];
|
|
984
|
+
token = src[position$1++];
|
|
961
985
|
break
|
|
962
986
|
case 0x19:
|
|
963
|
-
token = dataView.getUint16(position);
|
|
964
|
-
position += 2;
|
|
987
|
+
token = dataView.getUint16(position$1);
|
|
988
|
+
position$1 += 2;
|
|
965
989
|
break
|
|
966
990
|
case 0x1a:
|
|
967
|
-
token = dataView.getUint32(position);
|
|
968
|
-
position += 4;
|
|
991
|
+
token = dataView.getUint32(position$1);
|
|
992
|
+
position$1 += 4;
|
|
969
993
|
break
|
|
970
994
|
}
|
|
971
995
|
}
|
|
@@ -991,12 +1015,12 @@
|
|
|
991
1015
|
|
|
992
1016
|
function saveState(callback) {
|
|
993
1017
|
let savedSrcEnd = srcEnd;
|
|
994
|
-
let savedPosition = position;
|
|
1018
|
+
let savedPosition = position$1;
|
|
995
1019
|
let savedSrcStringStart = srcStringStart;
|
|
996
1020
|
let savedSrcStringEnd = srcStringEnd;
|
|
997
1021
|
let savedSrcString = srcString;
|
|
998
1022
|
let savedReferenceMap = referenceMap;
|
|
999
|
-
let savedBundledStrings = bundledStrings;
|
|
1023
|
+
let savedBundledStrings = bundledStrings$1;
|
|
1000
1024
|
|
|
1001
1025
|
// TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
|
|
1002
1026
|
let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
|
|
@@ -1005,12 +1029,12 @@
|
|
|
1005
1029
|
let savedSequentialMode = sequentialMode;
|
|
1006
1030
|
let value = callback();
|
|
1007
1031
|
srcEnd = savedSrcEnd;
|
|
1008
|
-
position = savedPosition;
|
|
1032
|
+
position$1 = savedPosition;
|
|
1009
1033
|
srcStringStart = savedSrcStringStart;
|
|
1010
1034
|
srcStringEnd = savedSrcStringEnd;
|
|
1011
1035
|
srcString = savedSrcString;
|
|
1012
1036
|
referenceMap = savedReferenceMap;
|
|
1013
|
-
bundledStrings = savedBundledStrings;
|
|
1037
|
+
bundledStrings$1 = savedBundledStrings;
|
|
1014
1038
|
src = savedSrc;
|
|
1015
1039
|
sequentialMode = savedSequentialMode;
|
|
1016
1040
|
currentStructures = savedStructures;
|
|
@@ -1024,7 +1048,7 @@
|
|
|
1024
1048
|
currentStructures = null;
|
|
1025
1049
|
}
|
|
1026
1050
|
|
|
1027
|
-
function addExtension(extension) {
|
|
1051
|
+
function addExtension$1(extension) {
|
|
1028
1052
|
currentExtensions[extension.tag] = extension.decode;
|
|
1029
1053
|
}
|
|
1030
1054
|
|
|
@@ -1041,8 +1065,6 @@
|
|
|
1041
1065
|
DECIMAL_ROUND: 3,
|
|
1042
1066
|
DECIMAL_FIT: 4
|
|
1043
1067
|
};
|
|
1044
|
-
let f32Array = new Float32Array(1);
|
|
1045
|
-
let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
1046
1068
|
function roundFloat32(float32Number) {
|
|
1047
1069
|
f32Array[0] = float32Number;
|
|
1048
1070
|
let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
|
|
@@ -1057,13 +1079,15 @@
|
|
|
1057
1079
|
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
1058
1080
|
const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
|
|
1059
1081
|
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
1082
|
+
const BlobConstructor = typeof Blob === 'undefined' ? {} : Blob;
|
|
1060
1083
|
const MAX_STRUCTURES = 0x100;
|
|
1061
1084
|
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1085
|
+
let throwOnIterable;
|
|
1062
1086
|
let target;
|
|
1063
1087
|
let targetView;
|
|
1064
|
-
let position
|
|
1088
|
+
let position = 0;
|
|
1065
1089
|
let safeEnd;
|
|
1066
|
-
let bundledStrings
|
|
1090
|
+
let bundledStrings = null;
|
|
1067
1091
|
const MAX_BUNDLE_SIZE = 0xf000;
|
|
1068
1092
|
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1069
1093
|
const RECORD_SYMBOL = Symbol('record-id');
|
|
@@ -1132,28 +1156,28 @@
|
|
|
1132
1156
|
if (!target) {
|
|
1133
1157
|
target = new ByteArrayAllocate(8192);
|
|
1134
1158
|
targetView = new DataView(target.buffer, 0, 8192);
|
|
1135
|
-
position
|
|
1159
|
+
position = 0;
|
|
1136
1160
|
}
|
|
1137
1161
|
safeEnd = target.length - 10;
|
|
1138
|
-
if (safeEnd - position
|
|
1162
|
+
if (safeEnd - position < 0x800) {
|
|
1139
1163
|
// don't start too close to the end,
|
|
1140
1164
|
target = new ByteArrayAllocate(target.length);
|
|
1141
1165
|
targetView = new DataView(target.buffer, 0, target.length);
|
|
1142
1166
|
safeEnd = target.length - 10;
|
|
1143
|
-
position
|
|
1167
|
+
position = 0;
|
|
1144
1168
|
} else if (encodeOptions === REUSE_BUFFER_MODE)
|
|
1145
|
-
position
|
|
1146
|
-
start = position
|
|
1169
|
+
position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
|
|
1170
|
+
start = position;
|
|
1147
1171
|
if (encoder.useSelfDescribedHeader) {
|
|
1148
|
-
targetView.setUint32(position
|
|
1149
|
-
position
|
|
1172
|
+
targetView.setUint32(position, 0xd9d9f700); // tag two byte, then self-descriptive tag
|
|
1173
|
+
position += 3;
|
|
1150
1174
|
}
|
|
1151
1175
|
referenceMap = encoder.structuredClone ? new Map() : null;
|
|
1152
1176
|
if (encoder.bundleStrings && typeof value !== 'string') {
|
|
1153
|
-
bundledStrings
|
|
1154
|
-
bundledStrings
|
|
1177
|
+
bundledStrings = [];
|
|
1178
|
+
bundledStrings.size = Infinity; // force a new bundle start on first string
|
|
1155
1179
|
} else
|
|
1156
|
-
bundledStrings
|
|
1180
|
+
bundledStrings = null;
|
|
1157
1181
|
|
|
1158
1182
|
sharedStructures = encoder.structures;
|
|
1159
1183
|
if (sharedStructures) {
|
|
@@ -1209,8 +1233,8 @@
|
|
|
1209
1233
|
packedValues.samplingPackedValues = samplingPackedValues;
|
|
1210
1234
|
findRepetitiveStrings(value, packedValues);
|
|
1211
1235
|
if (packedValues.values.length > 0) {
|
|
1212
|
-
target[position
|
|
1213
|
-
target[position
|
|
1236
|
+
target[position++] = 0xd8; // one-byte tag
|
|
1237
|
+
target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1214
1238
|
writeArrayHeader(4);
|
|
1215
1239
|
let valuesArray = packedValues.values;
|
|
1216
1240
|
encode(valuesArray);
|
|
@@ -1222,27 +1246,30 @@
|
|
|
1222
1246
|
}
|
|
1223
1247
|
}
|
|
1224
1248
|
}
|
|
1249
|
+
throwOnIterable = encodeOptions & THROW_ON_ITERABLE;
|
|
1225
1250
|
try {
|
|
1251
|
+
if (throwOnIterable)
|
|
1252
|
+
return;
|
|
1226
1253
|
encode(value);
|
|
1227
|
-
if (bundledStrings
|
|
1254
|
+
if (bundledStrings) {
|
|
1228
1255
|
writeBundles(start, encode);
|
|
1229
1256
|
}
|
|
1230
|
-
encoder.offset = position
|
|
1257
|
+
encoder.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1231
1258
|
if (referenceMap && referenceMap.idsToInsert) {
|
|
1232
|
-
position
|
|
1233
|
-
if (position
|
|
1234
|
-
makeRoom(position
|
|
1235
|
-
encoder.offset = position
|
|
1236
|
-
let serialized = insertIds(target.subarray(start, position
|
|
1259
|
+
position += referenceMap.idsToInsert.length * 2;
|
|
1260
|
+
if (position > safeEnd)
|
|
1261
|
+
makeRoom(position);
|
|
1262
|
+
encoder.offset = position;
|
|
1263
|
+
let serialized = insertIds(target.subarray(start, position), referenceMap.idsToInsert);
|
|
1237
1264
|
referenceMap = null;
|
|
1238
1265
|
return serialized
|
|
1239
1266
|
}
|
|
1240
1267
|
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1241
1268
|
target.start = start;
|
|
1242
|
-
target.end = position
|
|
1269
|
+
target.end = position;
|
|
1243
1270
|
return target
|
|
1244
1271
|
}
|
|
1245
|
-
return target.subarray(start, position
|
|
1272
|
+
return target.subarray(start, position) // position can change if we call encode again in saveShared, so we get the buffer now
|
|
1246
1273
|
} finally {
|
|
1247
1274
|
if (sharedStructures) {
|
|
1248
1275
|
if (serializationsSinceTransitionRebuild < 10)
|
|
@@ -1269,13 +1296,13 @@
|
|
|
1269
1296
|
encoder.structures = encoder.structures.slice(0, maxSharedStructures);
|
|
1270
1297
|
}
|
|
1271
1298
|
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1272
|
-
let returnBuffer = target.subarray(start, position
|
|
1299
|
+
let returnBuffer = target.subarray(start, position);
|
|
1273
1300
|
if (encoder.updateSharedData() === false)
|
|
1274
1301
|
return encoder.encode(value) // re-encode if it fails
|
|
1275
1302
|
return returnBuffer
|
|
1276
1303
|
}
|
|
1277
1304
|
if (encodeOptions & RESET_BUFFER_MODE)
|
|
1278
|
-
position
|
|
1305
|
+
position = start;
|
|
1279
1306
|
}
|
|
1280
1307
|
};
|
|
1281
1308
|
this.findCommonStringsToPack = () => {
|
|
@@ -1299,8 +1326,8 @@
|
|
|
1299
1326
|
}
|
|
1300
1327
|
};
|
|
1301
1328
|
const encode = (value) => {
|
|
1302
|
-
if (position
|
|
1303
|
-
target = makeRoom(position
|
|
1329
|
+
if (position > safeEnd)
|
|
1330
|
+
target = makeRoom(position);
|
|
1304
1331
|
|
|
1305
1332
|
var type = typeof value;
|
|
1306
1333
|
var length;
|
|
@@ -1309,9 +1336,9 @@
|
|
|
1309
1336
|
let packedPosition = packedObjectMap[value];
|
|
1310
1337
|
if (packedPosition >= 0) {
|
|
1311
1338
|
if (packedPosition < 16)
|
|
1312
|
-
target[position
|
|
1339
|
+
target[position++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1313
1340
|
else {
|
|
1314
|
-
target[position
|
|
1341
|
+
target[position++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1315
1342
|
if (packedPosition & 1)
|
|
1316
1343
|
encode((15 - packedPosition) >> 1);
|
|
1317
1344
|
else
|
|
@@ -1343,30 +1370,30 @@
|
|
|
1343
1370
|
}
|
|
1344
1371
|
}
|
|
1345
1372
|
let strLength = value.length;
|
|
1346
|
-
if (bundledStrings
|
|
1347
|
-
if ((bundledStrings
|
|
1373
|
+
if (bundledStrings && strLength >= 4 && strLength < 0x400) {
|
|
1374
|
+
if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
1348
1375
|
let extStart;
|
|
1349
|
-
let maxBytes = (bundledStrings
|
|
1350
|
-
if (position
|
|
1351
|
-
target = makeRoom(position
|
|
1352
|
-
target[position
|
|
1353
|
-
target[position
|
|
1354
|
-
target[position
|
|
1376
|
+
let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
|
|
1377
|
+
if (position + maxBytes > safeEnd)
|
|
1378
|
+
target = makeRoom(position + maxBytes);
|
|
1379
|
+
target[position++] = 0xd9; // tag 16-bit
|
|
1380
|
+
target[position++] = 0xdf; // tag 0xdff9
|
|
1381
|
+
target[position++] = 0xf9;
|
|
1355
1382
|
// TODO: If we only have one bundle with any string data, only write one string bundle
|
|
1356
|
-
target[position
|
|
1357
|
-
target[position
|
|
1358
|
-
extStart = position
|
|
1359
|
-
position
|
|
1360
|
-
if (bundledStrings
|
|
1383
|
+
target[position++] = bundledStrings.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
|
|
1384
|
+
target[position++] = 0x1a; // 32-bit unsigned int
|
|
1385
|
+
extStart = position - start;
|
|
1386
|
+
position += 4; // reserve for writing bundle reference
|
|
1387
|
+
if (bundledStrings.position) {
|
|
1361
1388
|
writeBundles(start, encode); // write the last bundles
|
|
1362
1389
|
}
|
|
1363
|
-
bundledStrings
|
|
1364
|
-
bundledStrings
|
|
1365
|
-
bundledStrings
|
|
1390
|
+
bundledStrings = ['', '']; // create new ones
|
|
1391
|
+
bundledStrings.size = 0;
|
|
1392
|
+
bundledStrings.position = extStart;
|
|
1366
1393
|
}
|
|
1367
1394
|
let twoByte = hasNonLatin.test(value);
|
|
1368
|
-
bundledStrings
|
|
1369
|
-
target[position
|
|
1395
|
+
bundledStrings[twoByte ? 0 : 1] += value;
|
|
1396
|
+
target[position++] = twoByte ? 0xce : 0xcf;
|
|
1370
1397
|
encode(strLength);
|
|
1371
1398
|
return
|
|
1372
1399
|
}
|
|
@@ -1382,11 +1409,11 @@
|
|
|
1382
1409
|
headerSize = 5;
|
|
1383
1410
|
}
|
|
1384
1411
|
let maxBytes = strLength * 3;
|
|
1385
|
-
if (position
|
|
1386
|
-
target = makeRoom(position
|
|
1412
|
+
if (position + maxBytes > safeEnd)
|
|
1413
|
+
target = makeRoom(position + maxBytes);
|
|
1387
1414
|
|
|
1388
1415
|
if (strLength < 0x40 || !encodeUtf8) {
|
|
1389
|
-
let i, c1, c2, strPosition = position
|
|
1416
|
+
let i, c1, c2, strPosition = position + headerSize;
|
|
1390
1417
|
for (i = 0; i < strLength; i++) {
|
|
1391
1418
|
c1 = value.charCodeAt(i);
|
|
1392
1419
|
if (c1 < 0x80) {
|
|
@@ -1410,105 +1437,105 @@
|
|
|
1410
1437
|
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1411
1438
|
}
|
|
1412
1439
|
}
|
|
1413
|
-
length = strPosition - position
|
|
1440
|
+
length = strPosition - position - headerSize;
|
|
1414
1441
|
} else {
|
|
1415
|
-
length = encodeUtf8(value, position
|
|
1442
|
+
length = encodeUtf8(value, position + headerSize, maxBytes);
|
|
1416
1443
|
}
|
|
1417
1444
|
|
|
1418
1445
|
if (length < 0x18) {
|
|
1419
|
-
target[position
|
|
1446
|
+
target[position++] = 0x60 | length;
|
|
1420
1447
|
} else if (length < 0x100) {
|
|
1421
1448
|
if (headerSize < 2) {
|
|
1422
|
-
target.copyWithin(position
|
|
1449
|
+
target.copyWithin(position + 2, position + 1, position + 1 + length);
|
|
1423
1450
|
}
|
|
1424
|
-
target[position
|
|
1425
|
-
target[position
|
|
1451
|
+
target[position++] = 0x78;
|
|
1452
|
+
target[position++] = length;
|
|
1426
1453
|
} else if (length < 0x10000) {
|
|
1427
1454
|
if (headerSize < 3) {
|
|
1428
|
-
target.copyWithin(position
|
|
1455
|
+
target.copyWithin(position + 3, position + 2, position + 2 + length);
|
|
1429
1456
|
}
|
|
1430
|
-
target[position
|
|
1431
|
-
target[position
|
|
1432
|
-
target[position
|
|
1457
|
+
target[position++] = 0x79;
|
|
1458
|
+
target[position++] = length >> 8;
|
|
1459
|
+
target[position++] = length & 0xff;
|
|
1433
1460
|
} else {
|
|
1434
1461
|
if (headerSize < 5) {
|
|
1435
|
-
target.copyWithin(position
|
|
1462
|
+
target.copyWithin(position + 5, position + 3, position + 3 + length);
|
|
1436
1463
|
}
|
|
1437
|
-
target[position
|
|
1438
|
-
targetView.setUint32(position
|
|
1439
|
-
position
|
|
1464
|
+
target[position++] = 0x7a;
|
|
1465
|
+
targetView.setUint32(position, length);
|
|
1466
|
+
position += 4;
|
|
1440
1467
|
}
|
|
1441
|
-
position
|
|
1468
|
+
position += length;
|
|
1442
1469
|
} else if (type === 'number') {
|
|
1443
1470
|
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1444
1471
|
// positive uint
|
|
1445
1472
|
if (value < 0x18) {
|
|
1446
|
-
target[position
|
|
1473
|
+
target[position++] = value;
|
|
1447
1474
|
} else if (value < 0x100) {
|
|
1448
|
-
target[position
|
|
1449
|
-
target[position
|
|
1475
|
+
target[position++] = 0x18;
|
|
1476
|
+
target[position++] = value;
|
|
1450
1477
|
} else if (value < 0x10000) {
|
|
1451
|
-
target[position
|
|
1452
|
-
target[position
|
|
1453
|
-
target[position
|
|
1478
|
+
target[position++] = 0x19;
|
|
1479
|
+
target[position++] = value >> 8;
|
|
1480
|
+
target[position++] = value & 0xff;
|
|
1454
1481
|
} else {
|
|
1455
|
-
target[position
|
|
1456
|
-
targetView.setUint32(position
|
|
1457
|
-
position
|
|
1482
|
+
target[position++] = 0x1a;
|
|
1483
|
+
targetView.setUint32(position, value);
|
|
1484
|
+
position += 4;
|
|
1458
1485
|
}
|
|
1459
1486
|
} else if (value >> 0 === value) { // negative integer
|
|
1460
1487
|
if (value >= -0x18) {
|
|
1461
|
-
target[position
|
|
1488
|
+
target[position++] = 0x1f - value;
|
|
1462
1489
|
} else if (value >= -0x100) {
|
|
1463
|
-
target[position
|
|
1464
|
-
target[position
|
|
1490
|
+
target[position++] = 0x38;
|
|
1491
|
+
target[position++] = ~value;
|
|
1465
1492
|
} else if (value >= -0x10000) {
|
|
1466
|
-
target[position
|
|
1467
|
-
targetView.setUint16(position
|
|
1468
|
-
position
|
|
1493
|
+
target[position++] = 0x39;
|
|
1494
|
+
targetView.setUint16(position, ~value);
|
|
1495
|
+
position += 2;
|
|
1469
1496
|
} else {
|
|
1470
|
-
target[position
|
|
1471
|
-
targetView.setUint32(position
|
|
1472
|
-
position
|
|
1497
|
+
target[position++] = 0x3a;
|
|
1498
|
+
targetView.setUint32(position, ~value);
|
|
1499
|
+
position += 4;
|
|
1473
1500
|
}
|
|
1474
1501
|
} else {
|
|
1475
1502
|
let useFloat32;
|
|
1476
1503
|
if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
|
|
1477
|
-
target[position
|
|
1478
|
-
targetView.setFloat32(position
|
|
1504
|
+
target[position++] = 0xfa;
|
|
1505
|
+
targetView.setFloat32(position, value);
|
|
1479
1506
|
let xShifted;
|
|
1480
1507
|
if (useFloat32 < 4 ||
|
|
1481
1508
|
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1482
|
-
((xShifted = value * mult10[((target[position
|
|
1483
|
-
position
|
|
1509
|
+
((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
|
|
1510
|
+
position += 4;
|
|
1484
1511
|
return
|
|
1485
1512
|
} else
|
|
1486
|
-
position
|
|
1513
|
+
position--; // move back into position for writing a double
|
|
1487
1514
|
}
|
|
1488
|
-
target[position
|
|
1489
|
-
targetView.setFloat64(position
|
|
1490
|
-
position
|
|
1515
|
+
target[position++] = 0xfb;
|
|
1516
|
+
targetView.setFloat64(position, value);
|
|
1517
|
+
position += 8;
|
|
1491
1518
|
}
|
|
1492
1519
|
} else if (type === 'object') {
|
|
1493
1520
|
if (!value)
|
|
1494
|
-
target[position
|
|
1521
|
+
target[position++] = 0xf6;
|
|
1495
1522
|
else {
|
|
1496
1523
|
if (referenceMap) {
|
|
1497
1524
|
let referee = referenceMap.get(value);
|
|
1498
1525
|
if (referee) {
|
|
1499
|
-
target[position
|
|
1500
|
-
target[position
|
|
1501
|
-
target[position
|
|
1526
|
+
target[position++] = 0xd8;
|
|
1527
|
+
target[position++] = 29; // http://cbor.schmorp.de/value-sharing
|
|
1528
|
+
target[position++] = 0x19; // 16-bit uint
|
|
1502
1529
|
if (!referee.references) {
|
|
1503
1530
|
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
|
|
1504
1531
|
referee.references = [];
|
|
1505
1532
|
idsToInsert.push(referee);
|
|
1506
1533
|
}
|
|
1507
|
-
referee.references.push(position
|
|
1508
|
-
position
|
|
1534
|
+
referee.references.push(position - start);
|
|
1535
|
+
position += 2; // TODO: also support 32-bit
|
|
1509
1536
|
return
|
|
1510
1537
|
} else
|
|
1511
|
-
referenceMap.set(value, { offset: position
|
|
1538
|
+
referenceMap.set(value, { offset: position - start });
|
|
1512
1539
|
}
|
|
1513
1540
|
let constructor = value.constructor;
|
|
1514
1541
|
if (constructor === Object) {
|
|
@@ -1516,7 +1543,7 @@
|
|
|
1516
1543
|
} else if (constructor === Array) {
|
|
1517
1544
|
length = value.length;
|
|
1518
1545
|
if (length < 0x18) {
|
|
1519
|
-
target[position
|
|
1546
|
+
target[position++] = 0x80 | length;
|
|
1520
1547
|
} else {
|
|
1521
1548
|
writeArrayHeader(length);
|
|
1522
1549
|
}
|
|
@@ -1526,24 +1553,24 @@
|
|
|
1526
1553
|
} else if (constructor === Map) {
|
|
1527
1554
|
if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
|
|
1528
1555
|
// 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
|
|
1529
|
-
target[position
|
|
1530
|
-
target[position
|
|
1531
|
-
target[position
|
|
1556
|
+
target[position++] = 0xd9;
|
|
1557
|
+
target[position++] = 1;
|
|
1558
|
+
target[position++] = 3;
|
|
1532
1559
|
}
|
|
1533
1560
|
length = value.size;
|
|
1534
1561
|
if (length < 0x18) {
|
|
1535
|
-
target[position
|
|
1562
|
+
target[position++] = 0xa0 | length;
|
|
1536
1563
|
} else if (length < 0x100) {
|
|
1537
|
-
target[position
|
|
1538
|
-
target[position
|
|
1564
|
+
target[position++] = 0xb8;
|
|
1565
|
+
target[position++] = length;
|
|
1539
1566
|
} else if (length < 0x10000) {
|
|
1540
|
-
target[position
|
|
1541
|
-
target[position
|
|
1542
|
-
target[position
|
|
1567
|
+
target[position++] = 0xb9;
|
|
1568
|
+
target[position++] = length >> 8;
|
|
1569
|
+
target[position++] = length & 0xff;
|
|
1543
1570
|
} else {
|
|
1544
|
-
target[position
|
|
1545
|
-
targetView.setUint32(position
|
|
1546
|
-
position
|
|
1571
|
+
target[position++] = 0xba;
|
|
1572
|
+
targetView.setUint32(position, length);
|
|
1573
|
+
position += 4;
|
|
1547
1574
|
}
|
|
1548
1575
|
if (encoder.keyMap) {
|
|
1549
1576
|
for (let [ key, entryValue ] of value) {
|
|
@@ -1565,58 +1592,68 @@
|
|
|
1565
1592
|
if (tag == undefined)
|
|
1566
1593
|
tag = extension.getTag && extension.getTag.call(this, value);
|
|
1567
1594
|
if (tag < 0x18) {
|
|
1568
|
-
target[position
|
|
1595
|
+
target[position++] = 0xc0 | tag;
|
|
1569
1596
|
} else if (tag < 0x100) {
|
|
1570
|
-
target[position
|
|
1571
|
-
target[position
|
|
1597
|
+
target[position++] = 0xd8;
|
|
1598
|
+
target[position++] = tag;
|
|
1572
1599
|
} else if (tag < 0x10000) {
|
|
1573
|
-
target[position
|
|
1574
|
-
target[position
|
|
1575
|
-
target[position
|
|
1600
|
+
target[position++] = 0xd9;
|
|
1601
|
+
target[position++] = tag >> 8;
|
|
1602
|
+
target[position++] = tag & 0xff;
|
|
1576
1603
|
} else if (tag > -1) {
|
|
1577
|
-
target[position
|
|
1578
|
-
targetView.setUint32(position
|
|
1579
|
-
position
|
|
1604
|
+
target[position++] = 0xda;
|
|
1605
|
+
targetView.setUint32(position, tag);
|
|
1606
|
+
position += 4;
|
|
1580
1607
|
} // else undefined, don't write tag
|
|
1581
1608
|
extension.encode.call(this, value, encode, makeRoom);
|
|
1582
1609
|
return
|
|
1583
1610
|
}
|
|
1584
1611
|
}
|
|
1585
1612
|
if (value[Symbol.iterator]) {
|
|
1586
|
-
|
|
1613
|
+
if (throwOnIterable) {
|
|
1614
|
+
let error = new Error('Iterable should be serialized as iterator');
|
|
1615
|
+
error.iteratorNotHandled = true;
|
|
1616
|
+
throw error;
|
|
1617
|
+
}
|
|
1618
|
+
target[position++] = 0x9f; // indefinite length array
|
|
1587
1619
|
for (let entry of value) {
|
|
1588
1620
|
encode(entry);
|
|
1589
1621
|
}
|
|
1590
|
-
target[position
|
|
1622
|
+
target[position++] = 0xff; // stop-code
|
|
1591
1623
|
return
|
|
1592
1624
|
}
|
|
1625
|
+
if (value[Symbol.asyncIterator] || constructor === BlobConstructor) {
|
|
1626
|
+
let error = new Error('Iterable/blob should be serialized as iterator');
|
|
1627
|
+
error.iteratorNotHandled = true;
|
|
1628
|
+
throw error;
|
|
1629
|
+
}
|
|
1593
1630
|
// no extension found, write as object
|
|
1594
1631
|
writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
|
|
1595
1632
|
}
|
|
1596
1633
|
}
|
|
1597
1634
|
} else if (type === 'boolean') {
|
|
1598
|
-
target[position
|
|
1635
|
+
target[position++] = value ? 0xf5 : 0xf4;
|
|
1599
1636
|
} else if (type === 'bigint') {
|
|
1600
1637
|
if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
|
|
1601
1638
|
// use an unsigned int as long as it fits
|
|
1602
|
-
target[position
|
|
1603
|
-
targetView.setBigUint64(position
|
|
1639
|
+
target[position++] = 0x1b;
|
|
1640
|
+
targetView.setBigUint64(position, value);
|
|
1604
1641
|
} else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
|
|
1605
1642
|
// if we can fit an unsigned int, use that
|
|
1606
|
-
target[position
|
|
1607
|
-
targetView.setBigUint64(position
|
|
1643
|
+
target[position++] = 0x3b;
|
|
1644
|
+
targetView.setBigUint64(position, -value - BigInt(1));
|
|
1608
1645
|
} else {
|
|
1609
1646
|
// overflow
|
|
1610
1647
|
if (this.largeBigIntToFloat) {
|
|
1611
|
-
target[position
|
|
1612
|
-
targetView.setFloat64(position
|
|
1648
|
+
target[position++] = 0xfb;
|
|
1649
|
+
targetView.setFloat64(position, Number(value));
|
|
1613
1650
|
} else {
|
|
1614
1651
|
throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
|
|
1615
1652
|
}
|
|
1616
1653
|
}
|
|
1617
|
-
position
|
|
1654
|
+
position += 8;
|
|
1618
1655
|
} else if (type === 'undefined') {
|
|
1619
|
-
target[position
|
|
1656
|
+
target[position++] = 0xf7;
|
|
1620
1657
|
} else {
|
|
1621
1658
|
throw new Error('Unknown type: ' + type)
|
|
1622
1659
|
}
|
|
@@ -1628,18 +1665,18 @@
|
|
|
1628
1665
|
let vals = Object.values(object);
|
|
1629
1666
|
let length = keys.length;
|
|
1630
1667
|
if (length < 0x18) {
|
|
1631
|
-
target[position
|
|
1668
|
+
target[position++] = 0xa0 | length;
|
|
1632
1669
|
} else if (length < 0x100) {
|
|
1633
|
-
target[position
|
|
1634
|
-
target[position
|
|
1670
|
+
target[position++] = 0xb8;
|
|
1671
|
+
target[position++] = length;
|
|
1635
1672
|
} else if (length < 0x10000) {
|
|
1636
|
-
target[position
|
|
1637
|
-
target[position
|
|
1638
|
-
target[position
|
|
1673
|
+
target[position++] = 0xb9;
|
|
1674
|
+
target[position++] = length >> 8;
|
|
1675
|
+
target[position++] = length & 0xff;
|
|
1639
1676
|
} else {
|
|
1640
|
-
target[position
|
|
1641
|
-
targetView.setUint32(position
|
|
1642
|
-
position
|
|
1677
|
+
target[position++] = 0xba;
|
|
1678
|
+
targetView.setUint32(position, length);
|
|
1679
|
+
position += 4;
|
|
1643
1680
|
}
|
|
1644
1681
|
if (encoder.keyMap) {
|
|
1645
1682
|
for (let i = 0; i < length; i++) {
|
|
@@ -1654,9 +1691,9 @@
|
|
|
1654
1691
|
}
|
|
1655
1692
|
} :
|
|
1656
1693
|
(object, safePrototype) => {
|
|
1657
|
-
target[position
|
|
1658
|
-
let objectOffset = position
|
|
1659
|
-
position
|
|
1694
|
+
target[position++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
|
|
1695
|
+
let objectOffset = position - start;
|
|
1696
|
+
position += 2;
|
|
1660
1697
|
let size = 0;
|
|
1661
1698
|
if (encoder.keyMap) {
|
|
1662
1699
|
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
@@ -1709,9 +1746,9 @@
|
|
|
1709
1746
|
let recordId = transition[RECORD_SYMBOL];
|
|
1710
1747
|
if (recordId !== undefined) {
|
|
1711
1748
|
recordId &= 0xffff;
|
|
1712
|
-
target[position
|
|
1713
|
-
target[position
|
|
1714
|
-
target[position
|
|
1749
|
+
target[position++] = 0xd9;
|
|
1750
|
+
target[position++] = (recordId >> 8) | 0xe0;
|
|
1751
|
+
target[position++] = recordId & 0xff;
|
|
1715
1752
|
} else {
|
|
1716
1753
|
if (!keys)
|
|
1717
1754
|
keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
|
|
@@ -1729,9 +1766,9 @@
|
|
|
1729
1766
|
}
|
|
1730
1767
|
structures[recordId] = keys;
|
|
1731
1768
|
if (recordId < maxSharedStructures) {
|
|
1732
|
-
target[position
|
|
1733
|
-
target[position
|
|
1734
|
-
target[position
|
|
1769
|
+
target[position++] = 0xd9;
|
|
1770
|
+
target[position++] = (recordId >> 8) | 0xe0;
|
|
1771
|
+
target[position++] = recordId & 0xff;
|
|
1735
1772
|
transition = structures.transitions;
|
|
1736
1773
|
for (let i = 0; i < length; i++) {
|
|
1737
1774
|
if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
|
|
@@ -1742,8 +1779,8 @@
|
|
|
1742
1779
|
hasSharedUpdate = true;
|
|
1743
1780
|
} else {
|
|
1744
1781
|
transition[RECORD_SYMBOL] = recordId;
|
|
1745
|
-
targetView.setUint32(position
|
|
1746
|
-
position
|
|
1782
|
+
targetView.setUint32(position, 0xd9dfff00); // tag two byte, then record definition id
|
|
1783
|
+
position += 3;
|
|
1747
1784
|
if (newTransitions)
|
|
1748
1785
|
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
1749
1786
|
// record the removal of the id, we can maintain our shared structure
|
|
@@ -1753,15 +1790,19 @@
|
|
|
1753
1790
|
writeArrayHeader(length + 2);
|
|
1754
1791
|
encode(0xe000 + recordId);
|
|
1755
1792
|
encode(keys);
|
|
1756
|
-
|
|
1793
|
+
if (safePrototype === null) return; // special exit for iterator
|
|
1794
|
+
for (let key in object)
|
|
1795
|
+
if (safePrototype || object.hasOwnProperty(key))
|
|
1796
|
+
encode(object[key]);
|
|
1757
1797
|
return
|
|
1758
1798
|
}
|
|
1759
1799
|
}
|
|
1760
1800
|
if (length < 0x18) { // write the array header
|
|
1761
|
-
target[position
|
|
1801
|
+
target[position++] = 0x80 | length;
|
|
1762
1802
|
} else {
|
|
1763
1803
|
writeArrayHeader(length);
|
|
1764
1804
|
}
|
|
1805
|
+
if (safePrototype === null) return; // special exit for iterator
|
|
1765
1806
|
for (let key in object)
|
|
1766
1807
|
if (safePrototype || object.hasOwnProperty(key))
|
|
1767
1808
|
encode(object[key]);
|
|
@@ -1782,17 +1823,141 @@
|
|
|
1782
1823
|
target.copy(newBuffer, 0, start, end);
|
|
1783
1824
|
else
|
|
1784
1825
|
newBuffer.set(target.slice(start, end));
|
|
1785
|
-
position
|
|
1826
|
+
position -= start;
|
|
1786
1827
|
start = 0;
|
|
1787
1828
|
safeEnd = newBuffer.length - 10;
|
|
1788
1829
|
return target = newBuffer
|
|
1789
1830
|
};
|
|
1831
|
+
let chunkThreshold = 100;
|
|
1832
|
+
let continuedChunkThreshold = 1000;
|
|
1833
|
+
this.encodeAsIterable = function(value, options) {
|
|
1834
|
+
return startEncoding(value, options, encodeObjectAsIterable);
|
|
1835
|
+
};
|
|
1836
|
+
this.encodeAsAsyncIterable = function(value, options) {
|
|
1837
|
+
return startEncoding(value, options, encodeObjectAsAsyncIterable);
|
|
1838
|
+
};
|
|
1839
|
+
|
|
1840
|
+
function* encodeObjectAsIterable(object, iterateProperties, finalIterable) {
|
|
1841
|
+
let constructor = object.constructor;
|
|
1842
|
+
if (constructor === Object) {
|
|
1843
|
+
let useRecords = encoder.useRecords !== false;
|
|
1844
|
+
if (useRecords)
|
|
1845
|
+
writeObject(object, null); // write the record identifier
|
|
1846
|
+
else
|
|
1847
|
+
writeEntityLength(Object.keys(object).length, 0xa0);
|
|
1848
|
+
for (let key in object) {
|
|
1849
|
+
let value = object[key];
|
|
1850
|
+
if (!useRecords) encode(key);
|
|
1851
|
+
if (value && typeof value === 'object') {
|
|
1852
|
+
if (iterateProperties[key])
|
|
1853
|
+
yield* encodeObjectAsIterable(value, iterateProperties[key]);
|
|
1854
|
+
else
|
|
1855
|
+
yield* tryEncode(value, iterateProperties, key);
|
|
1856
|
+
} else encode(value);
|
|
1857
|
+
}
|
|
1858
|
+
} else if (constructor === Array) {
|
|
1859
|
+
writeArrayHeader(object.length);
|
|
1860
|
+
for (let i = 0; i < length; i++) {
|
|
1861
|
+
let value = object[i];
|
|
1862
|
+
if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
|
|
1863
|
+
if (iterateProperties.element)
|
|
1864
|
+
yield* encodeObjectAsIterable(value, iterateProperties.element);
|
|
1865
|
+
else
|
|
1866
|
+
yield* tryEncode(value, iterateProperties, 'element');
|
|
1867
|
+
} else encode(value);
|
|
1868
|
+
}
|
|
1869
|
+
} else if (object[Symbol.iterator]) {
|
|
1870
|
+
target[position++] = 0x9f; // start indefinite array
|
|
1871
|
+
for (let value of object) {
|
|
1872
|
+
if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
|
|
1873
|
+
if (iterateProperties.element)
|
|
1874
|
+
yield* encodeObjectAsIterable(value, iterateProperties.element);
|
|
1875
|
+
else
|
|
1876
|
+
yield* tryEncode(value, iterateProperties, 'element');
|
|
1877
|
+
} else encode(value);
|
|
1878
|
+
}
|
|
1879
|
+
target[position++] = 0xff; // stop byte
|
|
1880
|
+
} else if (constructor === BlobConstructor){
|
|
1881
|
+
writeEntityLength(object.size, 0x40); // encode as binary data
|
|
1882
|
+
yield target.subarray(start, position);
|
|
1883
|
+
yield object; // directly return blobs, they have to be encoded asynchronously
|
|
1884
|
+
restartEncoding();
|
|
1885
|
+
} else if (object[Symbol.asyncIterator]) {
|
|
1886
|
+
target[position++] = 0x9f; // start indefinite array
|
|
1887
|
+
yield target.subarray(start, position);
|
|
1888
|
+
yield object; // directly return async iterators, they have to be encoded asynchronously
|
|
1889
|
+
restartEncoding();
|
|
1890
|
+
target[position++] = 0xff; // stop byte
|
|
1891
|
+
} else {
|
|
1892
|
+
encode(object);
|
|
1893
|
+
}
|
|
1894
|
+
if (finalIterable && position > start) yield target.subarray(start, position);
|
|
1895
|
+
else if (position - start > chunkThreshold) {
|
|
1896
|
+
yield target.subarray(start, position);
|
|
1897
|
+
restartEncoding();
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
function* tryEncode(value, iterateProperties, key) {
|
|
1901
|
+
let restart = position - start;
|
|
1902
|
+
try {
|
|
1903
|
+
encode(value);
|
|
1904
|
+
if (position - start > chunkThreshold) {
|
|
1905
|
+
yield target.subarray(start, position);
|
|
1906
|
+
restartEncoding();
|
|
1907
|
+
}
|
|
1908
|
+
} catch (error) {
|
|
1909
|
+
if (error.iteratorNotHandled) {
|
|
1910
|
+
iterateProperties[key] = {};
|
|
1911
|
+
position = start + restart; // restart our position so we don't have partial data from last encode
|
|
1912
|
+
yield* encodeObjectAsIterable.call(this, value, iterateProperties[key]);
|
|
1913
|
+
} else throw error;
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
function restartEncoding() {
|
|
1917
|
+
chunkThreshold = continuedChunkThreshold;
|
|
1918
|
+
encoder.encode(null, THROW_ON_ITERABLE); // restart encoding
|
|
1919
|
+
}
|
|
1920
|
+
function startEncoding(value, options, encodeIterable) {
|
|
1921
|
+
if (options && options.chunkThreshold) // explicitly specified chunk sizes
|
|
1922
|
+
chunkThreshold = continuedChunkThreshold = options.chunkThreshold;
|
|
1923
|
+
else // we start with a smaller threshold to get initial bytes sent quickly
|
|
1924
|
+
chunkThreshold = 100;
|
|
1925
|
+
if (value && typeof value === 'object') {
|
|
1926
|
+
encoder.encode(null, THROW_ON_ITERABLE); // start encoding
|
|
1927
|
+
return encodeIterable(value, encoder.iterateProperties || (encoder.iterateProperties = {}), true);
|
|
1928
|
+
}
|
|
1929
|
+
return [encoder.encode(value)];
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
async function* encodeObjectAsAsyncIterable(value, iterateProperties) {
|
|
1933
|
+
for (let encodedValue of encodeObjectAsIterable(value, iterateProperties, true)) {
|
|
1934
|
+
let constructor = encodedValue.constructor;
|
|
1935
|
+
if (constructor === ByteArray || constructor === Uint8Array)
|
|
1936
|
+
yield encodedValue;
|
|
1937
|
+
else if (constructor === BlobConstructor) {
|
|
1938
|
+
let reader = encodedValue.stream().getReader();
|
|
1939
|
+
let next;
|
|
1940
|
+
while (!(next = await reader.read()).done) {
|
|
1941
|
+
yield next.value;
|
|
1942
|
+
}
|
|
1943
|
+
} else if (encodedValue[Symbol.asyncIterator]) {
|
|
1944
|
+
for await (let asyncValue of encodedValue) {
|
|
1945
|
+
restartEncoding();
|
|
1946
|
+
if (asyncValue)
|
|
1947
|
+
yield* encodeObjectAsAsyncIterable(asyncValue, iterateProperties.async || (iterateProperties.async = {}));
|
|
1948
|
+
else yield encoder.encode(asyncValue);
|
|
1949
|
+
}
|
|
1950
|
+
} else {
|
|
1951
|
+
yield encodedValue;
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1790
1955
|
}
|
|
1791
1956
|
useBuffer(buffer) {
|
|
1792
1957
|
// this means we are finished using our own buffer and we can write over it safely
|
|
1793
1958
|
target = buffer;
|
|
1794
1959
|
targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
|
|
1795
|
-
position
|
|
1960
|
+
position = 0;
|
|
1796
1961
|
}
|
|
1797
1962
|
clearSharedData() {
|
|
1798
1963
|
if (this.structures)
|
|
@@ -1822,6 +1987,23 @@
|
|
|
1822
1987
|
return saveResults
|
|
1823
1988
|
}
|
|
1824
1989
|
}
|
|
1990
|
+
function writeEntityLength(length, majorValue) {
|
|
1991
|
+
if (length < 0x18)
|
|
1992
|
+
target[position++] = majorValue | length;
|
|
1993
|
+
else if (length < 0x100) {
|
|
1994
|
+
target[position++] = majorValue | 0x18;
|
|
1995
|
+
target[position++] = length;
|
|
1996
|
+
} else if (length < 0x10000) {
|
|
1997
|
+
target[position++] = majorValue | 0x19;
|
|
1998
|
+
target[position++] = length >> 8;
|
|
1999
|
+
target[position++] = length & 0xff;
|
|
2000
|
+
} else {
|
|
2001
|
+
target[position++] = majorValue | 0x1a;
|
|
2002
|
+
targetView.setUint32(position, length);
|
|
2003
|
+
position += 4;
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
}
|
|
1825
2007
|
class SharedData {
|
|
1826
2008
|
constructor(structures, values, version) {
|
|
1827
2009
|
this.structures = structures;
|
|
@@ -1832,18 +2014,18 @@
|
|
|
1832
2014
|
|
|
1833
2015
|
function writeArrayHeader(length) {
|
|
1834
2016
|
if (length < 0x18)
|
|
1835
|
-
target[position
|
|
2017
|
+
target[position++] = 0x80 | length;
|
|
1836
2018
|
else if (length < 0x100) {
|
|
1837
|
-
target[position
|
|
1838
|
-
target[position
|
|
2019
|
+
target[position++] = 0x98;
|
|
2020
|
+
target[position++] = length;
|
|
1839
2021
|
} else if (length < 0x10000) {
|
|
1840
|
-
target[position
|
|
1841
|
-
target[position
|
|
1842
|
-
target[position
|
|
2022
|
+
target[position++] = 0x99;
|
|
2023
|
+
target[position++] = length >> 8;
|
|
2024
|
+
target[position++] = length & 0xff;
|
|
1843
2025
|
} else {
|
|
1844
|
-
target[position
|
|
1845
|
-
targetView.setUint32(position
|
|
1846
|
-
position
|
|
2026
|
+
target[position++] = 0x9a;
|
|
2027
|
+
targetView.setUint32(position, length);
|
|
2028
|
+
position += 4;
|
|
1847
2029
|
}
|
|
1848
2030
|
}
|
|
1849
2031
|
|
|
@@ -1896,7 +2078,7 @@
|
|
|
1896
2078
|
case 'function': console.log(value);
|
|
1897
2079
|
}
|
|
1898
2080
|
}
|
|
1899
|
-
const isLittleEndianMachine
|
|
2081
|
+
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
1900
2082
|
extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
|
|
1901
2083
|
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
1902
2084
|
typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
@@ -1910,14 +2092,14 @@
|
|
|
1910
2092
|
let seconds = date.getTime() / 1000;
|
|
1911
2093
|
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
|
|
1912
2094
|
// Timestamp 32
|
|
1913
|
-
target[position
|
|
1914
|
-
targetView.setUint32(position
|
|
1915
|
-
position
|
|
2095
|
+
target[position++] = 0x1a;
|
|
2096
|
+
targetView.setUint32(position, seconds);
|
|
2097
|
+
position += 4;
|
|
1916
2098
|
} else {
|
|
1917
2099
|
// Timestamp float64
|
|
1918
|
-
target[position
|
|
1919
|
-
targetView.setFloat64(position
|
|
1920
|
-
position
|
|
2100
|
+
target[position++] = 0xfb;
|
|
2101
|
+
targetView.setFloat64(position, seconds);
|
|
2102
|
+
position += 8;
|
|
1921
2103
|
}
|
|
1922
2104
|
}
|
|
1923
2105
|
}, { // Set
|
|
@@ -1973,8 +2155,8 @@
|
|
|
1973
2155
|
let packedValues = sharedData.packedValues || [];
|
|
1974
2156
|
let sharedStructures = sharedData.structures || [];
|
|
1975
2157
|
if (packedValues.values.length > 0) {
|
|
1976
|
-
target[position
|
|
1977
|
-
target[position
|
|
2158
|
+
target[position++] = 0xd8; // one-byte tag
|
|
2159
|
+
target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1978
2160
|
writeArrayHeader(4);
|
|
1979
2161
|
let valuesArray = packedValues.values;
|
|
1980
2162
|
encode(valuesArray);
|
|
@@ -1986,8 +2168,8 @@
|
|
|
1986
2168
|
}
|
|
1987
2169
|
}
|
|
1988
2170
|
if (sharedStructures) {
|
|
1989
|
-
targetView.setUint32(position
|
|
1990
|
-
position
|
|
2171
|
+
targetView.setUint32(position, 0xd9dffe00);
|
|
2172
|
+
position += 3;
|
|
1991
2173
|
let definitions = sharedStructures.slice(0);
|
|
1992
2174
|
definitions.unshift(0xe000);
|
|
1993
2175
|
definitions.push(new Tag(sharedData.version, 0x53687264));
|
|
@@ -1997,7 +2179,7 @@
|
|
|
1997
2179
|
}
|
|
1998
2180
|
}];
|
|
1999
2181
|
function typedArrayEncoder(tag, size) {
|
|
2000
|
-
if (!isLittleEndianMachine
|
|
2182
|
+
if (!isLittleEndianMachine && size > 1)
|
|
2001
2183
|
tag -= 4; // the big endian equivalents are 4 less
|
|
2002
2184
|
return {
|
|
2003
2185
|
tag: tag,
|
|
@@ -2013,26 +2195,26 @@
|
|
|
2013
2195
|
function writeBuffer(buffer, makeRoom) {
|
|
2014
2196
|
let length = buffer.byteLength;
|
|
2015
2197
|
if (length < 0x18) {
|
|
2016
|
-
target[position
|
|
2198
|
+
target[position++] = 0x40 + length;
|
|
2017
2199
|
} else if (length < 0x100) {
|
|
2018
|
-
target[position
|
|
2019
|
-
target[position
|
|
2200
|
+
target[position++] = 0x58;
|
|
2201
|
+
target[position++] = length;
|
|
2020
2202
|
} else if (length < 0x10000) {
|
|
2021
|
-
target[position
|
|
2022
|
-
target[position
|
|
2023
|
-
target[position
|
|
2203
|
+
target[position++] = 0x59;
|
|
2204
|
+
target[position++] = length >> 8;
|
|
2205
|
+
target[position++] = length & 0xff;
|
|
2024
2206
|
} else {
|
|
2025
|
-
target[position
|
|
2026
|
-
targetView.setUint32(position
|
|
2027
|
-
position
|
|
2207
|
+
target[position++] = 0x5a;
|
|
2208
|
+
targetView.setUint32(position, length);
|
|
2209
|
+
position += 4;
|
|
2028
2210
|
}
|
|
2029
|
-
if (position
|
|
2030
|
-
makeRoom(position
|
|
2211
|
+
if (position + length >= target.length) {
|
|
2212
|
+
makeRoom(position + length);
|
|
2031
2213
|
}
|
|
2032
2214
|
// if it is already a typed array (has an ArrayBuffer), use that, but if it is an ArrayBuffer itself,
|
|
2033
2215
|
// must wrap it to set it.
|
|
2034
|
-
target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position
|
|
2035
|
-
position
|
|
2216
|
+
target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position);
|
|
2217
|
+
position += length;
|
|
2036
2218
|
}
|
|
2037
2219
|
|
|
2038
2220
|
function insertIds(serialized, idsToInsert) {
|
|
@@ -2061,27 +2243,30 @@
|
|
|
2061
2243
|
return serialized
|
|
2062
2244
|
}
|
|
2063
2245
|
function writeBundles(start, encode) {
|
|
2064
|
-
targetView.setUint32(bundledStrings
|
|
2065
|
-
let writeStrings = bundledStrings
|
|
2066
|
-
bundledStrings
|
|
2246
|
+
targetView.setUint32(bundledStrings.position + start, position - bundledStrings.position - start + 1); // the offset to bundle
|
|
2247
|
+
let writeStrings = bundledStrings;
|
|
2248
|
+
bundledStrings = null;
|
|
2067
2249
|
encode(writeStrings[0]);
|
|
2068
2250
|
encode(writeStrings[1]);
|
|
2069
2251
|
}
|
|
2070
2252
|
|
|
2071
|
-
function addExtension
|
|
2253
|
+
function addExtension(extension) {
|
|
2072
2254
|
if (extension.Class) {
|
|
2073
2255
|
if (!extension.encode)
|
|
2074
2256
|
throw new Error('Extension has no encode function')
|
|
2075
2257
|
extensionClasses.unshift(extension.Class);
|
|
2076
2258
|
extensions.unshift(extension);
|
|
2077
2259
|
}
|
|
2078
|
-
addExtension(extension);
|
|
2260
|
+
addExtension$1(extension);
|
|
2079
2261
|
}
|
|
2080
2262
|
let defaultEncoder = new Encoder({ useRecords: false });
|
|
2081
2263
|
const encode = defaultEncoder.encode;
|
|
2264
|
+
const encodeAsIterable = defaultEncoder.encodeAsIterable;
|
|
2265
|
+
const encodeAsAsyncIterable = defaultEncoder.encodeAsAsyncIterable;
|
|
2082
2266
|
const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
|
|
2083
2267
|
const REUSE_BUFFER_MODE = 512;
|
|
2084
2268
|
const RESET_BUFFER_MODE = 1024;
|
|
2269
|
+
const THROW_ON_ITERABLE = 2048;
|
|
2085
2270
|
|
|
2086
2271
|
/**
|
|
2087
2272
|
* Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer
|
|
@@ -2175,16 +2360,17 @@
|
|
|
2175
2360
|
exports.NEVER = NEVER;
|
|
2176
2361
|
exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
|
|
2177
2362
|
exports.Tag = Tag;
|
|
2178
|
-
exports.addExtension = addExtension
|
|
2363
|
+
exports.addExtension = addExtension;
|
|
2179
2364
|
exports.clearSource = clearSource;
|
|
2180
2365
|
exports.decode = decode;
|
|
2181
2366
|
exports.decodeIter = decodeIter;
|
|
2182
2367
|
exports.decodeMultiple = decodeMultiple;
|
|
2183
2368
|
exports.encode = encode;
|
|
2369
|
+
exports.encodeAsAsyncIterable = encodeAsAsyncIterable;
|
|
2370
|
+
exports.encodeAsIterable = encodeAsIterable;
|
|
2184
2371
|
exports.encodeIter = encodeIter;
|
|
2185
2372
|
exports.isNativeAccelerationEnabled = isNativeAccelerationEnabled;
|
|
2186
2373
|
exports.roundFloat32 = roundFloat32;
|
|
2187
2374
|
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
})));
|
|
2375
|
+
}));
|
|
2376
|
+
//# sourceMappingURL=index.js.map
|