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/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 = [];
@@ -89,7 +87,7 @@ class Decoder {
89
87
  }
90
88
  let res = {};
91
89
  //map.forEach((v,k) => res[Object.keys(this._keyMap)[Object.values(this._keyMap).indexOf(k)] || k] = v)
92
- map.forEach((v,k) => res[this._mapKey.has(k) ? this._mapKey.get(k) : k] = v);
90
+ map.forEach((v,k) => res[safeKey(this._mapKey.has(k) ? this._mapKey.get(k) : k)] = v);
93
91
  return res
94
92
  }
95
93
 
@@ -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
@@ -291,8 +290,8 @@ function read() {
291
290
  let key;
292
291
  if (currentDecoder.mapsAsObjects) {
293
292
  let object = {};
294
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[currentDecoder.decodeKey(key)] = read();
295
- else while ((key = read()) != STOP_CODE) object[key] = read();
293
+ if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[safeKey(currentDecoder.decodeKey(key))] = read();
294
+ else while ((key = read()) != STOP_CODE) object[safeKey(key)] = read();
296
295
  return object
297
296
  } else {
298
297
  if (restoreMapsAsObject) {
@@ -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
@@ -340,8 +339,8 @@ function read() {
340
339
  case 5: // map
341
340
  if (currentDecoder.mapsAsObjects) {
342
341
  let object = {};
343
- if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[currentDecoder.decodeKey(read())] = read();
344
- else for (let i = 0; i < token; i++) object[read()] = read();
342
+ if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
343
+ else for (let i = 0; i < token; i++) object[safeKey(read())] = read();
345
344
  return object
346
345
  } else {
347
346
  if (restoreMapsAsObject) {
@@ -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
@@ -457,8 +456,8 @@ function createStructureReader(structure) {
457
456
  if (this.slowReads++ >= 3) { // create a fast compiled reader
458
457
  let array = this.length == length ? this : this.slice(0, length);
459
458
  compiledReader = currentDecoder.keyMap
460
- ? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? k + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
461
- : new Function('r', 'return {' + array.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}');
459
+ ? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? safeKey(k) + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
460
+ : new Function('r', 'return {' + array.map(key => validName.test(key) ? safeKey(key) + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}');
462
461
  if (this.compiledReader)
463
462
  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
464
463
  compiledReader.propertyCount = length;
@@ -466,14 +465,20 @@ function createStructureReader(structure) {
466
465
  return compiledReader(read)
467
466
  }
468
467
  let object = {};
469
- if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[currentDecoder.decodeKey(this[i])] = read();
470
- else for (let i = 0; i < length; i++) object[this[i]] = read();
468
+ if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[safeKey(currentDecoder.decodeKey(this[i]))] = read();
469
+ else for (let i = 0; i < length; i++) {
470
+ object[safeKey(this[i])] = read();
471
+ }
471
472
  return object
472
473
  }
473
474
  structure.slowReads = 0;
474
475
  return readObject
475
476
  }
476
477
 
478
+ function safeKey(key) {
479
+ return key === '__proto__' ? '__proto_' : key
480
+ }
481
+
477
482
  let readFixedString = readStringJS;
478
483
 
479
484
  exports.isNativeAccelerationEnabled = false;
@@ -484,9 +489,9 @@ function setExtractor(extractStrings) {
484
489
  return function readString(length) {
485
490
  let string = strings[stringPosition++];
486
491
  if (string == null) {
487
- if (bundledStrings)
492
+ if (bundledStrings$1)
488
493
  return readStringJS(length)
489
- let extraction = extractStrings(position, srcEnd, length, src);
494
+ let extraction = extractStrings(position$1, srcEnd, length, src);
490
495
  if (typeof extraction == 'string') {
491
496
  string = extraction;
492
497
  strings = EMPTY_ARRAY;
@@ -501,13 +506,13 @@ function setExtractor(extractStrings) {
501
506
  }
502
507
  let srcStringLength = string.length;
503
508
  if (srcStringLength <= length) {
504
- position += length;
509
+ position$1 += length;
505
510
  return string
506
511
  }
507
512
  srcString = string;
508
- srcStringStart = position;
509
- srcStringEnd = position + srcStringLength;
510
- position += length;
513
+ srcStringStart = position$1;
514
+ srcStringEnd = position$1 + srcStringLength;
515
+ position$1 += length;
511
516
  return string.slice(0, length) // we know we just want the beginning
512
517
  }
513
518
  }
@@ -519,29 +524,29 @@ function readStringJS(length) {
519
524
  return result
520
525
  }
521
526
  if (length > 64 && decoder)
522
- return decoder.decode(src.subarray(position, position += length))
523
- const end = position + length;
527
+ return decoder.decode(src.subarray(position$1, position$1 += length))
528
+ const end = position$1 + length;
524
529
  const units = [];
525
530
  result = '';
526
- while (position < end) {
527
- const byte1 = src[position++];
531
+ while (position$1 < end) {
532
+ const byte1 = src[position$1++];
528
533
  if ((byte1 & 0x80) === 0) {
529
534
  // 1 byte
530
535
  units.push(byte1);
531
536
  } else if ((byte1 & 0xe0) === 0xc0) {
532
537
  // 2 bytes
533
- const byte2 = src[position++] & 0x3f;
538
+ const byte2 = src[position$1++] & 0x3f;
534
539
  units.push(((byte1 & 0x1f) << 6) | byte2);
535
540
  } else if ((byte1 & 0xf0) === 0xe0) {
536
541
  // 3 bytes
537
- const byte2 = src[position++] & 0x3f;
538
- const byte3 = src[position++] & 0x3f;
542
+ const byte2 = src[position$1++] & 0x3f;
543
+ const byte3 = src[position$1++] & 0x3f;
539
544
  units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
540
545
  } else if ((byte1 & 0xf8) === 0xf0) {
541
546
  // 4 bytes
542
- const byte2 = src[position++] & 0x3f;
543
- const byte3 = src[position++] & 0x3f;
544
- 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;
545
550
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
546
551
  if (unit > 0xffff) {
547
552
  unit -= 0x10000;
@@ -567,12 +572,12 @@ function readStringJS(length) {
567
572
  }
568
573
  let fromCharCode = String.fromCharCode;
569
574
  function longStringInJS(length) {
570
- let start = position;
575
+ let start = position$1;
571
576
  let bytes = new Array(length);
572
577
  for (let i = 0; i < length; i++) {
573
- const byte = src[position++];
578
+ const byte = src[position$1++];
574
579
  if ((byte & 0x80) > 0) {
575
- position = start;
580
+ position$1 = start;
576
581
  return
577
582
  }
578
583
  bytes[i] = byte;
@@ -585,131 +590,131 @@ function shortStringInJS(length) {
585
590
  if (length === 0)
586
591
  return ''
587
592
  else {
588
- let a = src[position++];
593
+ let a = src[position$1++];
589
594
  if ((a & 0x80) > 1) {
590
- position -= 1;
595
+ position$1 -= 1;
591
596
  return
592
597
  }
593
598
  return fromCharCode(a)
594
599
  }
595
600
  } else {
596
- let a = src[position++];
597
- let b = src[position++];
601
+ let a = src[position$1++];
602
+ let b = src[position$1++];
598
603
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
599
- position -= 2;
604
+ position$1 -= 2;
600
605
  return
601
606
  }
602
607
  if (length < 3)
603
608
  return fromCharCode(a, b)
604
- let c = src[position++];
609
+ let c = src[position$1++];
605
610
  if ((c & 0x80) > 0) {
606
- position -= 3;
611
+ position$1 -= 3;
607
612
  return
608
613
  }
609
614
  return fromCharCode(a, b, c)
610
615
  }
611
616
  } else {
612
- let a = src[position++];
613
- let b = src[position++];
614
- let c = src[position++];
615
- 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++];
616
621
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
617
- position -= 4;
622
+ position$1 -= 4;
618
623
  return
619
624
  }
620
625
  if (length < 6) {
621
626
  if (length === 4)
622
627
  return fromCharCode(a, b, c, d)
623
628
  else {
624
- let e = src[position++];
629
+ let e = src[position$1++];
625
630
  if ((e & 0x80) > 0) {
626
- position -= 5;
631
+ position$1 -= 5;
627
632
  return
628
633
  }
629
634
  return fromCharCode(a, b, c, d, e)
630
635
  }
631
636
  } else if (length < 8) {
632
- let e = src[position++];
633
- let f = src[position++];
637
+ let e = src[position$1++];
638
+ let f = src[position$1++];
634
639
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
635
- position -= 6;
640
+ position$1 -= 6;
636
641
  return
637
642
  }
638
643
  if (length < 7)
639
644
  return fromCharCode(a, b, c, d, e, f)
640
- let g = src[position++];
645
+ let g = src[position$1++];
641
646
  if ((g & 0x80) > 0) {
642
- position -= 7;
647
+ position$1 -= 7;
643
648
  return
644
649
  }
645
650
  return fromCharCode(a, b, c, d, e, f, g)
646
651
  } else {
647
- let e = src[position++];
648
- let f = src[position++];
649
- let g = src[position++];
650
- 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++];
651
656
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
652
- position -= 8;
657
+ position$1 -= 8;
653
658
  return
654
659
  }
655
660
  if (length < 10) {
656
661
  if (length === 8)
657
662
  return fromCharCode(a, b, c, d, e, f, g, h)
658
663
  else {
659
- let i = src[position++];
664
+ let i = src[position$1++];
660
665
  if ((i & 0x80) > 0) {
661
- position -= 9;
666
+ position$1 -= 9;
662
667
  return
663
668
  }
664
669
  return fromCharCode(a, b, c, d, e, f, g, h, i)
665
670
  }
666
671
  } else if (length < 12) {
667
- let i = src[position++];
668
- let j = src[position++];
672
+ let i = src[position$1++];
673
+ let j = src[position$1++];
669
674
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
670
- position -= 10;
675
+ position$1 -= 10;
671
676
  return
672
677
  }
673
678
  if (length < 11)
674
679
  return fromCharCode(a, b, c, d, e, f, g, h, i, j)
675
- let k = src[position++];
680
+ let k = src[position$1++];
676
681
  if ((k & 0x80) > 0) {
677
- position -= 11;
682
+ position$1 -= 11;
678
683
  return
679
684
  }
680
685
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
681
686
  } else {
682
- let i = src[position++];
683
- let j = src[position++];
684
- let k = src[position++];
685
- 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++];
686
691
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
687
- position -= 12;
692
+ position$1 -= 12;
688
693
  return
689
694
  }
690
695
  if (length < 14) {
691
696
  if (length === 12)
692
697
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
693
698
  else {
694
- let m = src[position++];
699
+ let m = src[position$1++];
695
700
  if ((m & 0x80) > 0) {
696
- position -= 13;
701
+ position$1 -= 13;
697
702
  return
698
703
  }
699
704
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
700
705
  }
701
706
  } else {
702
- let m = src[position++];
703
- let n = src[position++];
707
+ let m = src[position$1++];
708
+ let n = src[position$1++];
704
709
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
705
- position -= 14;
710
+ position$1 -= 14;
706
711
  return
707
712
  }
708
713
  if (length < 15)
709
714
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
710
- let o = src[position++];
715
+ let o = src[position$1++];
711
716
  if ((o & 0x80) > 0) {
712
- position -= 15;
717
+ position$1 -= 15;
713
718
  return
714
719
  }
715
720
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
@@ -722,24 +727,36 @@ function shortStringInJS(length) {
722
727
  function readBin(length) {
723
728
  return currentDecoder.copyBuffers ?
724
729
  // specifically use the copying slice (not the node one)
725
- Uint8Array.prototype.slice.call(src, position, position += length) :
726
- src.subarray(position, position += length)
730
+ Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
731
+ src.subarray(position$1, position$1 += length)
727
732
  }
728
-
733
+ let f32Array = new Float32Array(1);
734
+ let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
729
735
  function getFloat16() {
730
- let byte0 = src[position++];
731
- let byte1 = src[position++];
732
- let half = (byte0 << 8) + byte1;
733
- let exp = (half >> 10) & 0x1f;
734
- let mant = half & 0x3ff;
735
- let val;
736
- if (exp == 0) val = Math.exp(mant, -24);
737
- else if (exp != 31) val = Math.exp(mant + 1024, exp - 25);
738
- else val = mant == 0 ? Infinity : NaN;
739
- return half & 0x8000 ? -val : val
736
+ let byte0 = src[position$1++];
737
+ let byte1 = src[position$1++];
738
+ let exponent = (byte0 & 0x7f) >> 2;
739
+ if (exponent === 0x1f) { // specials
740
+ if (byte1 || (byte0 & 3))
741
+ return NaN;
742
+ return (byte0 & 0x80) ? -Infinity : Infinity;
743
+ }
744
+ if (exponent === 0) { // sub-normals
745
+ // significand with 10 fractional bits and divided by 2^14
746
+ let abs = (((byte0 & 3) << 8) | byte1) / (1 << 24);
747
+ return (byte0 & 0x80) ? -abs : abs
748
+ }
749
+
750
+ u8Array[3] = (byte0 & 0x80) | // sign bit
751
+ ((exponent >> 1) + 56); // 4 of 5 of the exponent bits, re-offset-ed
752
+ u8Array[2] = ((byte0 & 7) << 5) | // last exponent bit and first two mantissa bits
753
+ (byte1 >> 3); // next 5 bits of mantissa
754
+ u8Array[1] = byte1 << 5; // last three bits of mantissa
755
+ u8Array[0] = 0;
756
+ return f32Array[0];
740
757
  }
741
758
 
742
- let keyCache = new Array(4096);
759
+ new Array(4096);
743
760
 
744
761
  class Tag {
745
762
  constructor(value, tag) {
@@ -795,23 +812,23 @@ const recordDefinition = (definition) => {
795
812
  let object = {};
796
813
  if (currentDecoder.keyMap) for (let i = 2,l = definition.length; i < l; i++) {
797
814
  let key = currentDecoder.decodeKey(structure[i - 2]);
798
- object[key] = definition[i];
815
+ object[safeKey(key)] = definition[i];
799
816
  }
800
817
  else for (let i = 2,l = definition.length; i < l; i++) {
801
818
  let key = structure[i - 2];
802
- object[key] = definition[i];
819
+ object[safeKey(key)] = definition[i];
803
820
  }
804
821
  return object
805
822
  };
806
823
  currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition;
807
824
  currentExtensions[14] = (value) => {
808
- if (bundledStrings)
809
- 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)
810
827
  return new Tag(value, 14)
811
828
  };
812
829
  currentExtensions[15] = (value) => {
813
- if (bundledStrings)
814
- 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)
815
832
  return new Tag(value, 15)
816
833
  };
817
834
  let glbl = { Error, RegExp };
@@ -819,7 +836,7 @@ currentExtensions[27] = (data) => { // http://cbor.schmorp.de/generic-object
819
836
  return (glbl[data[0]] || Error)(data[1], data[2])
820
837
  };
821
838
  const packedTable = (read) => {
822
- if (src[position++] != 0x84)
839
+ if (src[position$1++] != 0x84)
823
840
  throw new Error('Packed values structure must be followed by a 4 element array')
824
841
  let newPackedValues = read(); // packed values
825
842
  packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues;
@@ -841,18 +858,23 @@ currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { // packed reference
841
858
  return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
842
859
  throw new Error('No support for non-integer packed references yet')
843
860
  };
844
- currentExtensions[25] = (id) => {
845
- return stringRefs[id]
846
- };
847
- currentExtensions[256] = (read) => {
848
- stringRefs = [];
849
- try {
850
- return read()
851
- } finally {
852
- stringRefs = null;
853
- }
854
- };
855
- currentExtensions[256].handlesRead = true;
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
856
878
 
857
879
  currentExtensions[28] = (read) => {
858
880
  // shareable http://cbor.schmorp.de/value-sharing (for structured clones)
@@ -861,7 +883,7 @@ currentExtensions[28] = (read) => {
861
883
  referenceMap.id = 0;
862
884
  }
863
885
  let id = referenceMap.id++;
864
- let token = src[position];
886
+ let token = src[position$1];
865
887
  let target;
866
888
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
867
889
  // ahead past references to record structure definitions
@@ -938,7 +960,7 @@ currentExtensionRanges.push((tag, input) => {
938
960
  return input
939
961
  });
940
962
 
941
- const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
963
+ const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
942
964
  const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
943
965
  typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
944
966
  typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
@@ -955,7 +977,7 @@ function registerTypedArray(TypedArray, tag) {
955
977
  if (!littleEndian && bytesPerElement == 1)
956
978
  continue
957
979
  let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : 3;
958
- currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine) ? (buffer) => {
980
+ currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
959
981
  if (!TypedArray)
960
982
  throw new Error('Could not find typed array for code ' + tag)
961
983
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
@@ -977,36 +999,36 @@ function registerTypedArray(TypedArray, tag) {
977
999
 
978
1000
  function readBundleExt() {
979
1001
  let length = readJustLength();
980
- let bundlePosition = position + read();
1002
+ let bundlePosition = position$1 + read();
981
1003
  for (let i = 2; i < length; i++) {
982
1004
  // skip past bundles that were already read
983
1005
  let bundleLength = readJustLength(); // this will increment position, so must add to position afterwards
984
- position += bundleLength;
1006
+ position$1 += bundleLength;
985
1007
  }
986
- let dataPosition = position;
987
- position = bundlePosition;
988
- bundledStrings = [readStringJS(readJustLength()), readStringJS(readJustLength())];
989
- bundledStrings.position0 = 0;
990
- bundledStrings.position1 = 0;
991
- bundledStrings.postBundlePosition = position;
992
- 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;
993
1015
  return read()
994
1016
  }
995
1017
 
996
1018
  function readJustLength() {
997
- let token = src[position++] & 0x1f;
1019
+ let token = src[position$1++] & 0x1f;
998
1020
  if (token > 0x17) {
999
1021
  switch (token) {
1000
1022
  case 0x18:
1001
- token = src[position++];
1023
+ token = src[position$1++];
1002
1024
  break
1003
1025
  case 0x19:
1004
- token = dataView.getUint16(position);
1005
- position += 2;
1026
+ token = dataView.getUint16(position$1);
1027
+ position$1 += 2;
1006
1028
  break
1007
1029
  case 0x1a:
1008
- token = dataView.getUint32(position);
1009
- position += 4;
1030
+ token = dataView.getUint32(position$1);
1031
+ position$1 += 4;
1010
1032
  break
1011
1033
  }
1012
1034
  }
@@ -1032,14 +1054,14 @@ function loadShared() {
1032
1054
 
1033
1055
  function saveState(callback) {
1034
1056
  let savedSrcEnd = srcEnd;
1035
- let savedPosition = position;
1057
+ let savedPosition = position$1;
1036
1058
  let savedStringPosition = stringPosition;
1037
1059
  let savedSrcStringStart = srcStringStart;
1038
1060
  let savedSrcStringEnd = srcStringEnd;
1039
1061
  let savedSrcString = srcString;
1040
1062
  let savedStrings = strings;
1041
1063
  let savedReferenceMap = referenceMap;
1042
- let savedBundledStrings = bundledStrings;
1064
+ let savedBundledStrings = bundledStrings$1;
1043
1065
 
1044
1066
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1045
1067
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1048,14 +1070,14 @@ function saveState(callback) {
1048
1070
  let savedSequentialMode = sequentialMode;
1049
1071
  let value = callback();
1050
1072
  srcEnd = savedSrcEnd;
1051
- position = savedPosition;
1073
+ position$1 = savedPosition;
1052
1074
  stringPosition = savedStringPosition;
1053
1075
  srcStringStart = savedSrcStringStart;
1054
1076
  srcStringEnd = savedSrcStringEnd;
1055
1077
  srcString = savedSrcString;
1056
1078
  strings = savedStrings;
1057
1079
  referenceMap = savedReferenceMap;
1058
- bundledStrings = savedBundledStrings;
1080
+ bundledStrings$1 = savedBundledStrings;
1059
1081
  src = savedSrc;
1060
1082
  sequentialMode = savedSequentialMode;
1061
1083
  currentStructures = savedStructures;
@@ -1069,7 +1091,7 @@ function clearSource() {
1069
1091
  currentStructures = null;
1070
1092
  }
1071
1093
 
1072
- function addExtension(extension) {
1094
+ function addExtension$1(extension) {
1073
1095
  currentExtensions[extension.tag] = extension.decode;
1074
1096
  }
1075
1097
 
@@ -1086,8 +1108,6 @@ const FLOAT32_OPTIONS = {
1086
1108
  DECIMAL_ROUND: 3,
1087
1109
  DECIMAL_FIT: 4
1088
1110
  };
1089
- let f32Array = new Float32Array(1);
1090
- let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1091
1111
  function roundFloat32(float32Number) {
1092
1112
  f32Array[0] = float32Number;
1093
1113
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
@@ -1102,13 +1122,15 @@ let extensions, extensionClasses;
1102
1122
  const hasNodeBuffer = typeof Buffer !== 'undefined';
1103
1123
  const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
1104
1124
  const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1125
+ const BlobConstructor = typeof Blob === 'undefined' ? {} : Blob;
1105
1126
  const MAX_STRUCTURES = 0x100;
1106
1127
  const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1128
+ let throwOnIterable;
1107
1129
  let target;
1108
1130
  let targetView;
1109
- let position$1 = 0;
1131
+ let position = 0;
1110
1132
  let safeEnd;
1111
- let bundledStrings$1 = null;
1133
+ let bundledStrings = null;
1112
1134
  const MAX_BUNDLE_SIZE = 0xf000;
1113
1135
  const hasNonLatin = /[\u0080-\uFFFF]/;
1114
1136
  const RECORD_SYMBOL = Symbol('record-id');
@@ -1177,28 +1199,28 @@ class Encoder extends Decoder {
1177
1199
  if (!target) {
1178
1200
  target = new ByteArrayAllocate(8192);
1179
1201
  targetView = new DataView(target.buffer, 0, 8192);
1180
- position$1 = 0;
1202
+ position = 0;
1181
1203
  }
1182
1204
  safeEnd = target.length - 10;
1183
- if (safeEnd - position$1 < 0x800) {
1205
+ if (safeEnd - position < 0x800) {
1184
1206
  // don't start too close to the end,
1185
1207
  target = new ByteArrayAllocate(target.length);
1186
1208
  targetView = new DataView(target.buffer, 0, target.length);
1187
1209
  safeEnd = target.length - 10;
1188
- position$1 = 0;
1210
+ position = 0;
1189
1211
  } else if (encodeOptions === REUSE_BUFFER_MODE)
1190
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1191
- start = position$1;
1212
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1213
+ start = position;
1192
1214
  if (encoder.useSelfDescribedHeader) {
1193
- targetView.setUint32(position$1, 0xd9d9f700); // tag two byte, then self-descriptive tag
1194
- position$1 += 3;
1215
+ targetView.setUint32(position, 0xd9d9f700); // tag two byte, then self-descriptive tag
1216
+ position += 3;
1195
1217
  }
1196
1218
  referenceMap = encoder.structuredClone ? new Map() : null;
1197
1219
  if (encoder.bundleStrings && typeof value !== 'string') {
1198
- bundledStrings$1 = [];
1199
- bundledStrings$1.size = Infinity; // force a new bundle start on first string
1220
+ bundledStrings = [];
1221
+ bundledStrings.size = Infinity; // force a new bundle start on first string
1200
1222
  } else
1201
- bundledStrings$1 = null;
1223
+ bundledStrings = null;
1202
1224
 
1203
1225
  sharedStructures = encoder.structures;
1204
1226
  if (sharedStructures) {
@@ -1254,8 +1276,8 @@ class Encoder extends Decoder {
1254
1276
  packedValues.samplingPackedValues = samplingPackedValues;
1255
1277
  findRepetitiveStrings(value, packedValues);
1256
1278
  if (packedValues.values.length > 0) {
1257
- target[position$1++] = 0xd8; // one-byte tag
1258
- target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
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
1259
1281
  writeArrayHeader(4);
1260
1282
  let valuesArray = packedValues.values;
1261
1283
  encode(valuesArray);
@@ -1267,27 +1289,30 @@ class Encoder extends Decoder {
1267
1289
  }
1268
1290
  }
1269
1291
  }
1292
+ throwOnIterable = encodeOptions & THROW_ON_ITERABLE;
1270
1293
  try {
1294
+ if (throwOnIterable)
1295
+ return;
1271
1296
  encode(value);
1272
- if (bundledStrings$1) {
1297
+ if (bundledStrings) {
1273
1298
  writeBundles(start, encode);
1274
1299
  }
1275
- encoder.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1300
+ encoder.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1276
1301
  if (referenceMap && referenceMap.idsToInsert) {
1277
- position$1 += referenceMap.idsToInsert.length * 2;
1278
- if (position$1 > safeEnd)
1279
- makeRoom(position$1);
1280
- encoder.offset = position$1;
1281
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
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);
1282
1307
  referenceMap = null;
1283
1308
  return serialized
1284
1309
  }
1285
1310
  if (encodeOptions & REUSE_BUFFER_MODE) {
1286
1311
  target.start = start;
1287
- target.end = position$1;
1312
+ target.end = position;
1288
1313
  return target
1289
1314
  }
1290
- return target.subarray(start, position$1) // position can change if we call encode again in saveShared, so we get the buffer now
1315
+ return target.subarray(start, position) // position can change if we call encode again in saveShared, so we get the buffer now
1291
1316
  } finally {
1292
1317
  if (sharedStructures) {
1293
1318
  if (serializationsSinceTransitionRebuild < 10)
@@ -1314,13 +1339,13 @@ class Encoder extends Decoder {
1314
1339
  encoder.structures = encoder.structures.slice(0, maxSharedStructures);
1315
1340
  }
1316
1341
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1317
- let returnBuffer = target.subarray(start, position$1);
1342
+ let returnBuffer = target.subarray(start, position);
1318
1343
  if (encoder.updateSharedData() === false)
1319
1344
  return encoder.encode(value) // re-encode if it fails
1320
1345
  return returnBuffer
1321
1346
  }
1322
1347
  if (encodeOptions & RESET_BUFFER_MODE)
1323
- position$1 = start;
1348
+ position = start;
1324
1349
  }
1325
1350
  };
1326
1351
  this.findCommonStringsToPack = () => {
@@ -1344,8 +1369,8 @@ class Encoder extends Decoder {
1344
1369
  }
1345
1370
  };
1346
1371
  const encode = (value) => {
1347
- if (position$1 > safeEnd)
1348
- target = makeRoom(position$1);
1372
+ if (position > safeEnd)
1373
+ target = makeRoom(position);
1349
1374
 
1350
1375
  var type = typeof value;
1351
1376
  var length;
@@ -1354,9 +1379,9 @@ class Encoder extends Decoder {
1354
1379
  let packedPosition = packedObjectMap[value];
1355
1380
  if (packedPosition >= 0) {
1356
1381
  if (packedPosition < 16)
1357
- target[position$1++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1382
+ target[position++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1358
1383
  else {
1359
- target[position$1++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1384
+ target[position++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1360
1385
  if (packedPosition & 1)
1361
1386
  encode((15 - packedPosition) >> 1);
1362
1387
  else
@@ -1388,30 +1413,30 @@ class Encoder extends Decoder {
1388
1413
  }
1389
1414
  }
1390
1415
  let strLength = value.length;
1391
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x400) {
1392
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1416
+ if (bundledStrings && strLength >= 4 && strLength < 0x400) {
1417
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1393
1418
  let extStart;
1394
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1395
- if (position$1 + maxBytes > safeEnd)
1396
- target = makeRoom(position$1 + maxBytes);
1397
- target[position$1++] = 0xd9; // tag 16-bit
1398
- target[position$1++] = 0xdf; // tag 0xdff9
1399
- target[position$1++] = 0xf9;
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;
1400
1425
  // TODO: If we only have one bundle with any string data, only write one string bundle
1401
- target[position$1++] = bundledStrings$1.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
1402
- target[position$1++] = 0x1a; // 32-bit unsigned int
1403
- extStart = position$1 - start;
1404
- position$1 += 4; // reserve for writing bundle reference
1405
- if (bundledStrings$1.position) {
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) {
1406
1431
  writeBundles(start, encode); // write the last bundles
1407
1432
  }
1408
- bundledStrings$1 = ['', '']; // create new ones
1409
- bundledStrings$1.size = 0;
1410
- bundledStrings$1.position = extStart;
1433
+ bundledStrings = ['', '']; // create new ones
1434
+ bundledStrings.size = 0;
1435
+ bundledStrings.position = extStart;
1411
1436
  }
1412
1437
  let twoByte = hasNonLatin.test(value);
1413
- bundledStrings$1[twoByte ? 0 : 1] += value;
1414
- target[position$1++] = twoByte ? 0xce : 0xcf;
1438
+ bundledStrings[twoByte ? 0 : 1] += value;
1439
+ target[position++] = twoByte ? 0xce : 0xcf;
1415
1440
  encode(strLength);
1416
1441
  return
1417
1442
  }
@@ -1427,11 +1452,11 @@ class Encoder extends Decoder {
1427
1452
  headerSize = 5;
1428
1453
  }
1429
1454
  let maxBytes = strLength * 3;
1430
- if (position$1 + maxBytes > safeEnd)
1431
- target = makeRoom(position$1 + maxBytes);
1455
+ if (position + maxBytes > safeEnd)
1456
+ target = makeRoom(position + maxBytes);
1432
1457
 
1433
1458
  if (strLength < 0x40 || !encodeUtf8) {
1434
- let i, c1, c2, strPosition = position$1 + headerSize;
1459
+ let i, c1, c2, strPosition = position + headerSize;
1435
1460
  for (i = 0; i < strLength; i++) {
1436
1461
  c1 = value.charCodeAt(i);
1437
1462
  if (c1 < 0x80) {
@@ -1455,105 +1480,105 @@ class Encoder extends Decoder {
1455
1480
  target[strPosition++] = c1 & 0x3f | 0x80;
1456
1481
  }
1457
1482
  }
1458
- length = strPosition - position$1 - headerSize;
1483
+ length = strPosition - position - headerSize;
1459
1484
  } else {
1460
- length = encodeUtf8(value, position$1 + headerSize, maxBytes);
1485
+ length = encodeUtf8(value, position + headerSize, maxBytes);
1461
1486
  }
1462
1487
 
1463
1488
  if (length < 0x18) {
1464
- target[position$1++] = 0x60 | length;
1489
+ target[position++] = 0x60 | length;
1465
1490
  } else if (length < 0x100) {
1466
1491
  if (headerSize < 2) {
1467
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1492
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1468
1493
  }
1469
- target[position$1++] = 0x78;
1470
- target[position$1++] = length;
1494
+ target[position++] = 0x78;
1495
+ target[position++] = length;
1471
1496
  } else if (length < 0x10000) {
1472
1497
  if (headerSize < 3) {
1473
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1498
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1474
1499
  }
1475
- target[position$1++] = 0x79;
1476
- target[position$1++] = length >> 8;
1477
- target[position$1++] = length & 0xff;
1500
+ target[position++] = 0x79;
1501
+ target[position++] = length >> 8;
1502
+ target[position++] = length & 0xff;
1478
1503
  } else {
1479
1504
  if (headerSize < 5) {
1480
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1505
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1481
1506
  }
1482
- target[position$1++] = 0x7a;
1483
- targetView.setUint32(position$1, length);
1484
- position$1 += 4;
1507
+ target[position++] = 0x7a;
1508
+ targetView.setUint32(position, length);
1509
+ position += 4;
1485
1510
  }
1486
- position$1 += length;
1511
+ position += length;
1487
1512
  } else if (type === 'number') {
1488
1513
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1489
1514
  // positive uint
1490
1515
  if (value < 0x18) {
1491
- target[position$1++] = value;
1516
+ target[position++] = value;
1492
1517
  } else if (value < 0x100) {
1493
- target[position$1++] = 0x18;
1494
- target[position$1++] = value;
1518
+ target[position++] = 0x18;
1519
+ target[position++] = value;
1495
1520
  } else if (value < 0x10000) {
1496
- target[position$1++] = 0x19;
1497
- target[position$1++] = value >> 8;
1498
- target[position$1++] = value & 0xff;
1521
+ target[position++] = 0x19;
1522
+ target[position++] = value >> 8;
1523
+ target[position++] = value & 0xff;
1499
1524
  } else {
1500
- target[position$1++] = 0x1a;
1501
- targetView.setUint32(position$1, value);
1502
- position$1 += 4;
1525
+ target[position++] = 0x1a;
1526
+ targetView.setUint32(position, value);
1527
+ position += 4;
1503
1528
  }
1504
1529
  } else if (value >> 0 === value) { // negative integer
1505
1530
  if (value >= -0x18) {
1506
- target[position$1++] = 0x1f - value;
1531
+ target[position++] = 0x1f - value;
1507
1532
  } else if (value >= -0x100) {
1508
- target[position$1++] = 0x38;
1509
- target[position$1++] = ~value;
1533
+ target[position++] = 0x38;
1534
+ target[position++] = ~value;
1510
1535
  } else if (value >= -0x10000) {
1511
- target[position$1++] = 0x39;
1512
- targetView.setUint16(position$1, ~value);
1513
- position$1 += 2;
1536
+ target[position++] = 0x39;
1537
+ targetView.setUint16(position, ~value);
1538
+ position += 2;
1514
1539
  } else {
1515
- target[position$1++] = 0x3a;
1516
- targetView.setUint32(position$1, ~value);
1517
- position$1 += 4;
1540
+ target[position++] = 0x3a;
1541
+ targetView.setUint32(position, ~value);
1542
+ position += 4;
1518
1543
  }
1519
1544
  } else {
1520
1545
  let useFloat32;
1521
1546
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1522
- target[position$1++] = 0xfa;
1523
- targetView.setFloat32(position$1, value);
1547
+ target[position++] = 0xfa;
1548
+ targetView.setFloat32(position, value);
1524
1549
  let xShifted;
1525
1550
  if (useFloat32 < 4 ||
1526
1551
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1527
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1528
- position$1 += 4;
1552
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1553
+ position += 4;
1529
1554
  return
1530
1555
  } else
1531
- position$1--; // move back into position for writing a double
1556
+ position--; // move back into position for writing a double
1532
1557
  }
1533
- target[position$1++] = 0xfb;
1534
- targetView.setFloat64(position$1, value);
1535
- position$1 += 8;
1558
+ target[position++] = 0xfb;
1559
+ targetView.setFloat64(position, value);
1560
+ position += 8;
1536
1561
  }
1537
1562
  } else if (type === 'object') {
1538
1563
  if (!value)
1539
- target[position$1++] = 0xf6;
1564
+ target[position++] = 0xf6;
1540
1565
  else {
1541
1566
  if (referenceMap) {
1542
1567
  let referee = referenceMap.get(value);
1543
1568
  if (referee) {
1544
- target[position$1++] = 0xd8;
1545
- target[position$1++] = 29; // http://cbor.schmorp.de/value-sharing
1546
- target[position$1++] = 0x19; // 16-bit uint
1569
+ target[position++] = 0xd8;
1570
+ target[position++] = 29; // http://cbor.schmorp.de/value-sharing
1571
+ target[position++] = 0x19; // 16-bit uint
1547
1572
  if (!referee.references) {
1548
1573
  let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1549
1574
  referee.references = [];
1550
1575
  idsToInsert.push(referee);
1551
1576
  }
1552
- referee.references.push(position$1 - start);
1553
- position$1 += 2; // TODO: also support 32-bit
1577
+ referee.references.push(position - start);
1578
+ position += 2; // TODO: also support 32-bit
1554
1579
  return
1555
1580
  } else
1556
- referenceMap.set(value, { offset: position$1 - start });
1581
+ referenceMap.set(value, { offset: position - start });
1557
1582
  }
1558
1583
  let constructor = value.constructor;
1559
1584
  if (constructor === Object) {
@@ -1561,7 +1586,7 @@ class Encoder extends Decoder {
1561
1586
  } else if (constructor === Array) {
1562
1587
  length = value.length;
1563
1588
  if (length < 0x18) {
1564
- target[position$1++] = 0x80 | length;
1589
+ target[position++] = 0x80 | length;
1565
1590
  } else {
1566
1591
  writeArrayHeader(length);
1567
1592
  }
@@ -1571,24 +1596,24 @@ class Encoder extends Decoder {
1571
1596
  } else if (constructor === Map) {
1572
1597
  if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
1573
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
1574
- target[position$1++] = 0xd9;
1575
- target[position$1++] = 1;
1576
- target[position$1++] = 3;
1599
+ target[position++] = 0xd9;
1600
+ target[position++] = 1;
1601
+ target[position++] = 3;
1577
1602
  }
1578
1603
  length = value.size;
1579
1604
  if (length < 0x18) {
1580
- target[position$1++] = 0xa0 | length;
1605
+ target[position++] = 0xa0 | length;
1581
1606
  } else if (length < 0x100) {
1582
- target[position$1++] = 0xb8;
1583
- target[position$1++] = length;
1607
+ target[position++] = 0xb8;
1608
+ target[position++] = length;
1584
1609
  } else if (length < 0x10000) {
1585
- target[position$1++] = 0xb9;
1586
- target[position$1++] = length >> 8;
1587
- target[position$1++] = length & 0xff;
1610
+ target[position++] = 0xb9;
1611
+ target[position++] = length >> 8;
1612
+ target[position++] = length & 0xff;
1588
1613
  } else {
1589
- target[position$1++] = 0xba;
1590
- targetView.setUint32(position$1, length);
1591
- position$1 += 4;
1614
+ target[position++] = 0xba;
1615
+ targetView.setUint32(position, length);
1616
+ position += 4;
1592
1617
  }
1593
1618
  if (encoder.keyMap) {
1594
1619
  for (let [ key, entryValue ] of value) {
@@ -1610,58 +1635,68 @@ class Encoder extends Decoder {
1610
1635
  if (tag == undefined)
1611
1636
  tag = extension.getTag && extension.getTag.call(this, value);
1612
1637
  if (tag < 0x18) {
1613
- target[position$1++] = 0xc0 | tag;
1638
+ target[position++] = 0xc0 | tag;
1614
1639
  } else if (tag < 0x100) {
1615
- target[position$1++] = 0xd8;
1616
- target[position$1++] = tag;
1640
+ target[position++] = 0xd8;
1641
+ target[position++] = tag;
1617
1642
  } else if (tag < 0x10000) {
1618
- target[position$1++] = 0xd9;
1619
- target[position$1++] = tag >> 8;
1620
- target[position$1++] = tag & 0xff;
1643
+ target[position++] = 0xd9;
1644
+ target[position++] = tag >> 8;
1645
+ target[position++] = tag & 0xff;
1621
1646
  } else if (tag > -1) {
1622
- target[position$1++] = 0xda;
1623
- targetView.setUint32(position$1, tag);
1624
- position$1 += 4;
1647
+ target[position++] = 0xda;
1648
+ targetView.setUint32(position, tag);
1649
+ position += 4;
1625
1650
  } // else undefined, don't write tag
1626
1651
  extension.encode.call(this, value, encode, makeRoom);
1627
1652
  return
1628
1653
  }
1629
1654
  }
1630
1655
  if (value[Symbol.iterator]) {
1631
- target[position$1++] = 0x9f; // indefinite length array
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
1632
1662
  for (let entry of value) {
1633
1663
  encode(entry);
1634
1664
  }
1635
- target[position$1++] = 0xff; // stop-code
1665
+ target[position++] = 0xff; // stop-code
1636
1666
  return
1637
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
+ }
1638
1673
  // no extension found, write as object
1639
1674
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1640
1675
  }
1641
1676
  }
1642
1677
  } else if (type === 'boolean') {
1643
- target[position$1++] = value ? 0xf5 : 0xf4;
1678
+ target[position++] = value ? 0xf5 : 0xf4;
1644
1679
  } else if (type === 'bigint') {
1645
1680
  if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
1646
1681
  // use an unsigned int as long as it fits
1647
- target[position$1++] = 0x1b;
1648
- targetView.setBigUint64(position$1, value);
1682
+ target[position++] = 0x1b;
1683
+ targetView.setBigUint64(position, value);
1649
1684
  } else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
1650
1685
  // if we can fit an unsigned int, use that
1651
- target[position$1++] = 0x3b;
1652
- targetView.setBigUint64(position$1, -value - BigInt(1));
1686
+ target[position++] = 0x3b;
1687
+ targetView.setBigUint64(position, -value - BigInt(1));
1653
1688
  } else {
1654
1689
  // overflow
1655
1690
  if (this.largeBigIntToFloat) {
1656
- target[position$1++] = 0xfb;
1657
- targetView.setFloat64(position$1, Number(value));
1691
+ target[position++] = 0xfb;
1692
+ targetView.setFloat64(position, Number(value));
1658
1693
  } else {
1659
1694
  throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1660
1695
  }
1661
1696
  }
1662
- position$1 += 8;
1697
+ position += 8;
1663
1698
  } else if (type === 'undefined') {
1664
- target[position$1++] = 0xf7;
1699
+ target[position++] = 0xf7;
1665
1700
  } else {
1666
1701
  throw new Error('Unknown type: ' + type)
1667
1702
  }
@@ -1673,18 +1708,18 @@ class Encoder extends Decoder {
1673
1708
  let vals = Object.values(object);
1674
1709
  let length = keys.length;
1675
1710
  if (length < 0x18) {
1676
- target[position$1++] = 0xa0 | length;
1711
+ target[position++] = 0xa0 | length;
1677
1712
  } else if (length < 0x100) {
1678
- target[position$1++] = 0xb8;
1679
- target[position$1++] = length;
1713
+ target[position++] = 0xb8;
1714
+ target[position++] = length;
1680
1715
  } else if (length < 0x10000) {
1681
- target[position$1++] = 0xb9;
1682
- target[position$1++] = length >> 8;
1683
- target[position$1++] = length & 0xff;
1716
+ target[position++] = 0xb9;
1717
+ target[position++] = length >> 8;
1718
+ target[position++] = length & 0xff;
1684
1719
  } else {
1685
- target[position$1++] = 0xba;
1686
- targetView.setUint32(position$1, length);
1687
- position$1 += 4;
1720
+ target[position++] = 0xba;
1721
+ targetView.setUint32(position, length);
1722
+ position += 4;
1688
1723
  }
1689
1724
  if (encoder.keyMap) {
1690
1725
  for (let i = 0; i < length; i++) {
@@ -1699,9 +1734,9 @@ class Encoder extends Decoder {
1699
1734
  }
1700
1735
  } :
1701
1736
  (object, safePrototype) => {
1702
- target[position$1++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
1703
- let objectOffset = position$1 - start;
1704
- position$1 += 2;
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;
1705
1740
  let size = 0;
1706
1741
  if (encoder.keyMap) {
1707
1742
  for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
@@ -1754,9 +1789,9 @@ class Encoder extends Decoder {
1754
1789
  let recordId = transition[RECORD_SYMBOL];
1755
1790
  if (recordId !== undefined) {
1756
1791
  recordId &= 0xffff;
1757
- target[position$1++] = 0xd9;
1758
- target[position$1++] = (recordId >> 8) | 0xe0;
1759
- target[position$1++] = recordId & 0xff;
1792
+ target[position++] = 0xd9;
1793
+ target[position++] = (recordId >> 8) | 0xe0;
1794
+ target[position++] = recordId & 0xff;
1760
1795
  } else {
1761
1796
  if (!keys)
1762
1797
  keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
@@ -1774,9 +1809,9 @@ class Encoder extends Decoder {
1774
1809
  }
1775
1810
  structures[recordId] = keys;
1776
1811
  if (recordId < maxSharedStructures) {
1777
- target[position$1++] = 0xd9;
1778
- target[position$1++] = (recordId >> 8) | 0xe0;
1779
- target[position$1++] = recordId & 0xff;
1812
+ target[position++] = 0xd9;
1813
+ target[position++] = (recordId >> 8) | 0xe0;
1814
+ target[position++] = recordId & 0xff;
1780
1815
  transition = structures.transitions;
1781
1816
  for (let i = 0; i < length; i++) {
1782
1817
  if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
@@ -1787,8 +1822,8 @@ class Encoder extends Decoder {
1787
1822
  hasSharedUpdate = true;
1788
1823
  } else {
1789
1824
  transition[RECORD_SYMBOL] = recordId;
1790
- targetView.setUint32(position$1, 0xd9dfff00); // tag two byte, then record definition id
1791
- position$1 += 3;
1825
+ targetView.setUint32(position, 0xd9dfff00); // tag two byte, then record definition id
1826
+ position += 3;
1792
1827
  if (newTransitions)
1793
1828
  transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
1794
1829
  // record the removal of the id, we can maintain our shared structure
@@ -1798,15 +1833,19 @@ class Encoder extends Decoder {
1798
1833
  writeArrayHeader(length + 2);
1799
1834
  encode(0xe000 + recordId);
1800
1835
  encode(keys);
1801
- for (let v of Object.values(object)) encode(v);
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]);
1802
1840
  return
1803
1841
  }
1804
1842
  }
1805
1843
  if (length < 0x18) { // write the array header
1806
- target[position$1++] = 0x80 | length;
1844
+ target[position++] = 0x80 | length;
1807
1845
  } else {
1808
1846
  writeArrayHeader(length);
1809
1847
  }
1848
+ if (safePrototype === null) return; // special exit for iterator
1810
1849
  for (let key in object)
1811
1850
  if (safePrototype || object.hasOwnProperty(key))
1812
1851
  encode(object[key]);
@@ -1827,17 +1866,141 @@ class Encoder extends Decoder {
1827
1866
  target.copy(newBuffer, 0, start, end);
1828
1867
  else
1829
1868
  newBuffer.set(target.slice(start, end));
1830
- position$1 -= start;
1869
+ position -= start;
1831
1870
  start = 0;
1832
1871
  safeEnd = newBuffer.length - 10;
1833
1872
  return target = newBuffer
1834
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
+ }
1835
1998
  }
1836
1999
  useBuffer(buffer) {
1837
2000
  // this means we are finished using our own buffer and we can write over it safely
1838
2001
  target = buffer;
1839
2002
  targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1840
- position$1 = 0;
2003
+ position = 0;
1841
2004
  }
1842
2005
  clearSharedData() {
1843
2006
  if (this.structures)
@@ -1867,6 +2030,23 @@ class Encoder extends Decoder {
1867
2030
  return saveResults
1868
2031
  }
1869
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
+ }
1870
2050
  class SharedData {
1871
2051
  constructor(structures, values, version) {
1872
2052
  this.structures = structures;
@@ -1877,18 +2057,18 @@ class SharedData {
1877
2057
 
1878
2058
  function writeArrayHeader(length) {
1879
2059
  if (length < 0x18)
1880
- target[position$1++] = 0x80 | length;
2060
+ target[position++] = 0x80 | length;
1881
2061
  else if (length < 0x100) {
1882
- target[position$1++] = 0x98;
1883
- target[position$1++] = length;
2062
+ target[position++] = 0x98;
2063
+ target[position++] = length;
1884
2064
  } else if (length < 0x10000) {
1885
- target[position$1++] = 0x99;
1886
- target[position$1++] = length >> 8;
1887
- target[position$1++] = length & 0xff;
2065
+ target[position++] = 0x99;
2066
+ target[position++] = length >> 8;
2067
+ target[position++] = length & 0xff;
1888
2068
  } else {
1889
- target[position$1++] = 0x9a;
1890
- targetView.setUint32(position$1, length);
1891
- position$1 += 4;
2069
+ target[position++] = 0x9a;
2070
+ targetView.setUint32(position, length);
2071
+ position += 4;
1892
2072
  }
1893
2073
  }
1894
2074
 
@@ -1941,7 +2121,7 @@ function findRepetitiveStrings(value, packedValues) {
1941
2121
  case 'function': console.log(value);
1942
2122
  }
1943
2123
  }
1944
- const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
2124
+ const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
1945
2125
  extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
1946
2126
  Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
1947
2127
  typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
@@ -1955,14 +2135,14 @@ extensions = [{ // Date
1955
2135
  let seconds = date.getTime() / 1000;
1956
2136
  if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
1957
2137
  // Timestamp 32
1958
- target[position$1++] = 0x1a;
1959
- targetView.setUint32(position$1, seconds);
1960
- position$1 += 4;
2138
+ target[position++] = 0x1a;
2139
+ targetView.setUint32(position, seconds);
2140
+ position += 4;
1961
2141
  } else {
1962
2142
  // Timestamp float64
1963
- target[position$1++] = 0xfb;
1964
- targetView.setFloat64(position$1, seconds);
1965
- position$1 += 8;
2143
+ target[position++] = 0xfb;
2144
+ targetView.setFloat64(position, seconds);
2145
+ position += 8;
1966
2146
  }
1967
2147
  }
1968
2148
  }, { // Set
@@ -2018,8 +2198,8 @@ extensions = [{ // Date
2018
2198
  let packedValues = sharedData.packedValues || [];
2019
2199
  let sharedStructures = sharedData.structures || [];
2020
2200
  if (packedValues.values.length > 0) {
2021
- target[position$1++] = 0xd8; // one-byte tag
2022
- target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
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
2023
2203
  writeArrayHeader(4);
2024
2204
  let valuesArray = packedValues.values;
2025
2205
  encode(valuesArray);
@@ -2031,8 +2211,8 @@ extensions = [{ // Date
2031
2211
  }
2032
2212
  }
2033
2213
  if (sharedStructures) {
2034
- targetView.setUint32(position$1, 0xd9dffe00);
2035
- position$1 += 3;
2214
+ targetView.setUint32(position, 0xd9dffe00);
2215
+ position += 3;
2036
2216
  let definitions = sharedStructures.slice(0);
2037
2217
  definitions.unshift(0xe000);
2038
2218
  definitions.push(new Tag(sharedData.version, 0x53687264));
@@ -2042,7 +2222,7 @@ extensions = [{ // Date
2042
2222
  }
2043
2223
  }];
2044
2224
  function typedArrayEncoder(tag, size) {
2045
- if (!isLittleEndianMachine$1 && size > 1)
2225
+ if (!isLittleEndianMachine && size > 1)
2046
2226
  tag -= 4; // the big endian equivalents are 4 less
2047
2227
  return {
2048
2228
  tag: tag,
@@ -2058,26 +2238,26 @@ function typedArrayEncoder(tag, size) {
2058
2238
  function writeBuffer(buffer, makeRoom) {
2059
2239
  let length = buffer.byteLength;
2060
2240
  if (length < 0x18) {
2061
- target[position$1++] = 0x40 + length;
2241
+ target[position++] = 0x40 + length;
2062
2242
  } else if (length < 0x100) {
2063
- target[position$1++] = 0x58;
2064
- target[position$1++] = length;
2243
+ target[position++] = 0x58;
2244
+ target[position++] = length;
2065
2245
  } else if (length < 0x10000) {
2066
- target[position$1++] = 0x59;
2067
- target[position$1++] = length >> 8;
2068
- target[position$1++] = length & 0xff;
2246
+ target[position++] = 0x59;
2247
+ target[position++] = length >> 8;
2248
+ target[position++] = length & 0xff;
2069
2249
  } else {
2070
- target[position$1++] = 0x5a;
2071
- targetView.setUint32(position$1, length);
2072
- position$1 += 4;
2250
+ target[position++] = 0x5a;
2251
+ targetView.setUint32(position, length);
2252
+ position += 4;
2073
2253
  }
2074
- if (position$1 + length >= target.length) {
2075
- makeRoom(position$1 + length);
2254
+ if (position + length >= target.length) {
2255
+ makeRoom(position + length);
2076
2256
  }
2077
2257
  // if it is already a typed array (has an ArrayBuffer), use that, but if it is an ArrayBuffer itself,
2078
2258
  // must wrap it to set it.
2079
- target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position$1);
2080
- position$1 += length;
2259
+ target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position);
2260
+ position += length;
2081
2261
  }
2082
2262
 
2083
2263
  function insertIds(serialized, idsToInsert) {
@@ -2106,27 +2286,30 @@ function insertIds(serialized, idsToInsert) {
2106
2286
  return serialized
2107
2287
  }
2108
2288
  function writeBundles(start, encode) {
2109
- targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start + 1); // the offset to bundle
2110
- let writeStrings = bundledStrings$1;
2111
- bundledStrings$1 = null;
2289
+ targetView.setUint32(bundledStrings.position + start, position - bundledStrings.position - start + 1); // the offset to bundle
2290
+ let writeStrings = bundledStrings;
2291
+ bundledStrings = null;
2112
2292
  encode(writeStrings[0]);
2113
2293
  encode(writeStrings[1]);
2114
2294
  }
2115
2295
 
2116
- function addExtension$1(extension) {
2296
+ function addExtension(extension) {
2117
2297
  if (extension.Class) {
2118
2298
  if (!extension.encode)
2119
2299
  throw new Error('Extension has no encode function')
2120
2300
  extensionClasses.unshift(extension.Class);
2121
2301
  extensions.unshift(extension);
2122
2302
  }
2123
- addExtension(extension);
2303
+ addExtension$1(extension);
2124
2304
  }
2125
2305
  let defaultEncoder = new Encoder({ useRecords: false });
2126
2306
  const encode = defaultEncoder.encode;
2307
+ const encodeAsIterable = defaultEncoder.encodeAsIterable;
2308
+ const encodeAsAsyncIterable = defaultEncoder.encodeAsAsyncIterable;
2127
2309
  const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
2128
2310
  const REUSE_BUFFER_MODE = 512;
2129
2311
  const RESET_BUFFER_MODE = 1024;
2312
+ const THROW_ON_ITERABLE = 2048;
2130
2313
 
2131
2314
  class EncoderStream extends stream.Transform {
2132
2315
  constructor(options) {
@@ -2137,8 +2320,10 @@ class EncoderStream extends stream.Transform {
2137
2320
  options.sequential = true;
2138
2321
  this.encoder = options.encoder || new Encoder(options);
2139
2322
  }
2140
- _transform(value, encoding, callback) {
2141
- this.push(this.encoder.encode(value));
2323
+ async _transform(value, encoding, callback) {
2324
+ for await (let chunk of this.encoder.encodeAsAsyncIterable(value)) {
2325
+ this.push(chunk);
2326
+ }
2142
2327
  callback();
2143
2328
  }
2144
2329
  }
@@ -2294,13 +2479,16 @@ exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
2294
2479
  exports.NEVER = NEVER;
2295
2480
  exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
2296
2481
  exports.Tag = Tag;
2297
- exports.addExtension = addExtension$1;
2482
+ exports.addExtension = addExtension;
2298
2483
  exports.clearSource = clearSource;
2299
2484
  exports.decode = decode;
2300
2485
  exports.decodeIter = decodeIter;
2301
2486
  exports.decodeMultiple = decodeMultiple;
2302
2487
  exports.encode = encode;
2488
+ exports.encodeAsAsyncIterable = encodeAsAsyncIterable;
2489
+ exports.encodeAsIterable = encodeAsIterable;
2303
2490
  exports.encodeIter = encodeIter;
2304
2491
  exports.mapsAsObjects = mapsAsObjects;
2305
2492
  exports.roundFloat32 = roundFloat32;
2306
2493
  exports.useRecords = useRecords;
2494
+ //# sourceMappingURL=node.cjs.map