cbor-x 1.4.1 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/node.cjs CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var stream = require('stream');
6
4
  var module$1 = require('module');
7
5
 
@@ -11,7 +9,7 @@ try {
11
9
  } catch(error) {}
12
10
  let src;
13
11
  let srcEnd;
14
- let position = 0;
12
+ let position$1 = 0;
15
13
  const EMPTY_ARRAY = [];
16
14
  const LEGACY_RECORD_INLINE_ID = 105;
17
15
  const RECORD_DEFINITIONS_ID = 0xdffe;
@@ -26,7 +24,7 @@ let currentStructures;
26
24
  let srcString;
27
25
  let srcStringStart = 0;
28
26
  let srcStringEnd = 0;
29
- let bundledStrings;
27
+ let bundledStrings$1;
30
28
  let referenceMap;
31
29
  let currentExtensions = [];
32
30
  let currentExtensionRanges = [];
@@ -115,12 +113,12 @@ class Decoder {
115
113
  })
116
114
  }
117
115
  srcEnd = end > -1 ? end : source.length;
118
- position = 0;
116
+ position$1 = 0;
119
117
  stringPosition = 0;
120
118
  srcStringEnd = 0;
121
119
  srcString = null;
122
120
  strings = EMPTY_ARRAY;
123
- bundledStrings = null;
121
+ bundledStrings$1 = null;
124
122
  src = source;
125
123
  // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
126
124
  // technique for getting data from a database where it can be copied into an existing buffer instead of creating
@@ -163,8 +161,8 @@ class Decoder {
163
161
  if (forEach(value) === false) {
164
162
  return
165
163
  }
166
- while(position < size) {
167
- lastPosition = position;
164
+ while(position$1 < size) {
165
+ lastPosition = position$1;
168
166
  if (forEach(checkedRead()) === false) {
169
167
  return
170
168
  }
@@ -172,8 +170,8 @@ class Decoder {
172
170
  }
173
171
  else {
174
172
  values = [ value ];
175
- while(position < size) {
176
- lastPosition = position;
173
+ while(position$1 < size) {
174
+ lastPosition = position$1;
177
175
  values.push(checkedRead());
178
176
  }
179
177
  return values
@@ -191,23 +189,24 @@ class Decoder {
191
189
  function checkedRead() {
192
190
  try {
193
191
  let result = read();
194
- if (bundledStrings) {
195
- if (position >= bundledStrings.postBundlePosition) {
192
+ if (bundledStrings$1) {
193
+ if (position$1 >= bundledStrings$1.postBundlePosition) {
196
194
  let error = new Error('Unexpected bundle position');
197
195
  error.incomplete = true;
198
196
  throw error
199
197
  }
200
198
  // bundled strings to skip past
201
- position = bundledStrings.postBundlePosition;
199
+ position$1 = bundledStrings$1.postBundlePosition;
200
+ bundledStrings$1 = null;
202
201
  }
203
202
 
204
- if (position == srcEnd) {
203
+ if (position$1 == srcEnd) {
205
204
  // finished reading this source, cleanup references
206
205
  currentStructures = null;
207
206
  src = null;
208
207
  if (referenceMap)
209
208
  referenceMap = null;
210
- } else if (position > srcEnd) {
209
+ } else if (position$1 > srcEnd) {
211
210
  // over read
212
211
  let error = new Error('Unexpected end of CBOR data');
213
212
  error.incomplete = true;
@@ -227,52 +226,52 @@ function checkedRead() {
227
226
  }
228
227
 
229
228
  function read() {
230
- let token = src[position++];
229
+ let token = src[position$1++];
231
230
  let majorType = token >> 5;
232
231
  token = token & 0x1f;
233
232
  if (token > 0x17) {
234
233
  switch (token) {
235
234
  case 0x18:
236
- token = src[position++];
235
+ token = src[position$1++];
237
236
  break
238
237
  case 0x19:
239
238
  if (majorType == 7) {
240
239
  return getFloat16()
241
240
  }
242
- token = dataView.getUint16(position);
243
- position += 2;
241
+ token = dataView.getUint16(position$1);
242
+ position$1 += 2;
244
243
  break
245
244
  case 0x1a:
246
245
  if (majorType == 7) {
247
- let value = dataView.getFloat32(position);
246
+ let value = dataView.getFloat32(position$1);
248
247
  if (currentDecoder.useFloat32 > 2) {
249
248
  // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
250
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
251
- position += 4;
249
+ let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
250
+ position$1 += 4;
252
251
  return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
253
252
  }
254
- position += 4;
253
+ position$1 += 4;
255
254
  return value
256
255
  }
257
- token = dataView.getUint32(position);
258
- position += 4;
256
+ token = dataView.getUint32(position$1);
257
+ position$1 += 4;
259
258
  break
260
259
  case 0x1b:
261
260
  if (majorType == 7) {
262
- let value = dataView.getFloat64(position);
263
- position += 8;
261
+ let value = dataView.getFloat64(position$1);
262
+ position$1 += 8;
264
263
  return value
265
264
  }
266
265
  if (majorType > 1) {
267
- if (dataView.getUint32(position) > 0)
266
+ if (dataView.getUint32(position$1) > 0)
268
267
  throw new Error('JavaScript does not support arrays, maps, or strings with length over 4294967295')
269
- token = dataView.getUint32(position + 4);
268
+ token = dataView.getUint32(position$1 + 4);
270
269
  } else if (currentDecoder.int64AsNumber) {
271
- token = dataView.getUint32(position) * 0x100000000;
272
- token += dataView.getUint32(position + 4);
270
+ token = dataView.getUint32(position$1) * 0x100000000;
271
+ token += dataView.getUint32(position$1 + 4);
273
272
  } else
274
- token = dataView.getBigUint64(position);
275
- position += 8;
273
+ token = dataView.getBigUint64(position$1);
274
+ position$1 += 8;
276
275
  break
277
276
  case 0x1f:
278
277
  // indefinite length
@@ -321,8 +320,8 @@ function read() {
321
320
  case 2: // buffer
322
321
  return readBin(token)
323
322
  case 3: // string
324
- if (srcStringEnd >= position) {
325
- return srcString.slice(position - srcStringStart, (position += token) - srcStringStart)
323
+ if (srcStringEnd >= position$1) {
324
+ return srcString.slice(position$1 - srcStringStart, (position$1 += token) - srcStringStart)
326
325
  }
327
326
  if (srcStringEnd == 0 && srcEnd < 140 && token < 32) {
328
327
  // for small blocks, avoiding the overhead of the extract call is helpful
@@ -362,13 +361,28 @@ function read() {
362
361
  return structure.read()
363
362
  }
364
363
  if (token < 0x10000) {
365
- if (token == RECORD_INLINE_ID) // we do a special check for this so that we can keep the currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
366
- return recordDefinition(read())
364
+ if (token == RECORD_INLINE_ID) { // we do a special check for this so that we can keep the
365
+ // currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
366
+ let length = readJustLength();
367
+ let id = read();
368
+ let structure = read();
369
+ recordDefinition(id, structure);
370
+ let object = {};
371
+ if (currentDecoder.keyMap) for (let i = 2; i < length; i++) {
372
+ let key = currentDecoder.decodeKey(structure[i - 2]);
373
+ object[safeKey(key)] = read();
374
+ }
375
+ else for (let i = 2; i < length; i++) {
376
+ let key = structure[i - 2];
377
+ object[safeKey(key)] = read();
378
+ }
379
+ return object
380
+ }
367
381
  else if (token == RECORD_DEFINITIONS_ID) {
368
382
  let length = readJustLength();
369
383
  let id = read();
370
384
  for (let i = 2; i < length; i++) {
371
- recordDefinition([id++, read()]);
385
+ recordDefinition(id++, read());
372
386
  }
373
387
  return read()
374
388
  } else if (token == BUNDLED_STRINGS_ID) {
@@ -426,24 +440,24 @@ const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
426
440
  function createStructureReader(structure) {
427
441
  function readObject() {
428
442
  // get the array size from the header
429
- let length = src[position++];
443
+ let length = src[position$1++];
430
444
  //let majorType = token >> 5
431
445
  length = length & 0x1f;
432
446
  if (length > 0x17) {
433
447
  switch (length) {
434
448
  case 0x18:
435
- length = src[position++];
449
+ length = src[position$1++];
436
450
  break
437
451
  case 0x19:
438
- length = dataView.getUint16(position);
439
- position += 2;
452
+ length = dataView.getUint16(position$1);
453
+ position$1 += 2;
440
454
  break
441
455
  case 0x1a:
442
- length = dataView.getUint32(position);
443
- position += 4;
456
+ length = dataView.getUint32(position$1);
457
+ position$1 += 4;
444
458
  break
445
459
  default:
446
- throw new Error('Expected array header, but got ' + src[position - 1])
460
+ throw new Error('Expected array header, but got ' + src[position$1 - 1])
447
461
  }
448
462
  }
449
463
  // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
@@ -490,9 +504,9 @@ function setExtractor(extractStrings) {
490
504
  return function readString(length) {
491
505
  let string = strings[stringPosition++];
492
506
  if (string == null) {
493
- if (bundledStrings)
507
+ if (bundledStrings$1)
494
508
  return readStringJS(length)
495
- let extraction = extractStrings(position, srcEnd, length, src);
509
+ let extraction = extractStrings(position$1, srcEnd, length, src);
496
510
  if (typeof extraction == 'string') {
497
511
  string = extraction;
498
512
  strings = EMPTY_ARRAY;
@@ -507,13 +521,13 @@ function setExtractor(extractStrings) {
507
521
  }
508
522
  let srcStringLength = string.length;
509
523
  if (srcStringLength <= length) {
510
- position += length;
524
+ position$1 += length;
511
525
  return string
512
526
  }
513
527
  srcString = string;
514
- srcStringStart = position;
515
- srcStringEnd = position + srcStringLength;
516
- position += length;
528
+ srcStringStart = position$1;
529
+ srcStringEnd = position$1 + srcStringLength;
530
+ position$1 += length;
517
531
  return string.slice(0, length) // we know we just want the beginning
518
532
  }
519
533
  }
@@ -525,29 +539,29 @@ function readStringJS(length) {
525
539
  return result
526
540
  }
527
541
  if (length > 64 && decoder)
528
- return decoder.decode(src.subarray(position, position += length))
529
- const end = position + length;
542
+ return decoder.decode(src.subarray(position$1, position$1 += length))
543
+ const end = position$1 + length;
530
544
  const units = [];
531
545
  result = '';
532
- while (position < end) {
533
- const byte1 = src[position++];
546
+ while (position$1 < end) {
547
+ const byte1 = src[position$1++];
534
548
  if ((byte1 & 0x80) === 0) {
535
549
  // 1 byte
536
550
  units.push(byte1);
537
551
  } else if ((byte1 & 0xe0) === 0xc0) {
538
552
  // 2 bytes
539
- const byte2 = src[position++] & 0x3f;
553
+ const byte2 = src[position$1++] & 0x3f;
540
554
  units.push(((byte1 & 0x1f) << 6) | byte2);
541
555
  } else if ((byte1 & 0xf0) === 0xe0) {
542
556
  // 3 bytes
543
- const byte2 = src[position++] & 0x3f;
544
- const byte3 = src[position++] & 0x3f;
557
+ const byte2 = src[position$1++] & 0x3f;
558
+ const byte3 = src[position$1++] & 0x3f;
545
559
  units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
546
560
  } else if ((byte1 & 0xf8) === 0xf0) {
547
561
  // 4 bytes
548
- const byte2 = src[position++] & 0x3f;
549
- const byte3 = src[position++] & 0x3f;
550
- const byte4 = src[position++] & 0x3f;
562
+ const byte2 = src[position$1++] & 0x3f;
563
+ const byte3 = src[position$1++] & 0x3f;
564
+ const byte4 = src[position$1++] & 0x3f;
551
565
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
552
566
  if (unit > 0xffff) {
553
567
  unit -= 0x10000;
@@ -573,12 +587,12 @@ function readStringJS(length) {
573
587
  }
574
588
  let fromCharCode = String.fromCharCode;
575
589
  function longStringInJS(length) {
576
- let start = position;
590
+ let start = position$1;
577
591
  let bytes = new Array(length);
578
592
  for (let i = 0; i < length; i++) {
579
- const byte = src[position++];
593
+ const byte = src[position$1++];
580
594
  if ((byte & 0x80) > 0) {
581
- position = start;
595
+ position$1 = start;
582
596
  return
583
597
  }
584
598
  bytes[i] = byte;
@@ -591,131 +605,131 @@ function shortStringInJS(length) {
591
605
  if (length === 0)
592
606
  return ''
593
607
  else {
594
- let a = src[position++];
608
+ let a = src[position$1++];
595
609
  if ((a & 0x80) > 1) {
596
- position -= 1;
610
+ position$1 -= 1;
597
611
  return
598
612
  }
599
613
  return fromCharCode(a)
600
614
  }
601
615
  } else {
602
- let a = src[position++];
603
- let b = src[position++];
616
+ let a = src[position$1++];
617
+ let b = src[position$1++];
604
618
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
605
- position -= 2;
619
+ position$1 -= 2;
606
620
  return
607
621
  }
608
622
  if (length < 3)
609
623
  return fromCharCode(a, b)
610
- let c = src[position++];
624
+ let c = src[position$1++];
611
625
  if ((c & 0x80) > 0) {
612
- position -= 3;
626
+ position$1 -= 3;
613
627
  return
614
628
  }
615
629
  return fromCharCode(a, b, c)
616
630
  }
617
631
  } else {
618
- let a = src[position++];
619
- let b = src[position++];
620
- let c = src[position++];
621
- let d = src[position++];
632
+ let a = src[position$1++];
633
+ let b = src[position$1++];
634
+ let c = src[position$1++];
635
+ let d = src[position$1++];
622
636
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
623
- position -= 4;
637
+ position$1 -= 4;
624
638
  return
625
639
  }
626
640
  if (length < 6) {
627
641
  if (length === 4)
628
642
  return fromCharCode(a, b, c, d)
629
643
  else {
630
- let e = src[position++];
644
+ let e = src[position$1++];
631
645
  if ((e & 0x80) > 0) {
632
- position -= 5;
646
+ position$1 -= 5;
633
647
  return
634
648
  }
635
649
  return fromCharCode(a, b, c, d, e)
636
650
  }
637
651
  } else if (length < 8) {
638
- let e = src[position++];
639
- let f = src[position++];
652
+ let e = src[position$1++];
653
+ let f = src[position$1++];
640
654
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
641
- position -= 6;
655
+ position$1 -= 6;
642
656
  return
643
657
  }
644
658
  if (length < 7)
645
659
  return fromCharCode(a, b, c, d, e, f)
646
- let g = src[position++];
660
+ let g = src[position$1++];
647
661
  if ((g & 0x80) > 0) {
648
- position -= 7;
662
+ position$1 -= 7;
649
663
  return
650
664
  }
651
665
  return fromCharCode(a, b, c, d, e, f, g)
652
666
  } else {
653
- let e = src[position++];
654
- let f = src[position++];
655
- let g = src[position++];
656
- let h = src[position++];
667
+ let e = src[position$1++];
668
+ let f = src[position$1++];
669
+ let g = src[position$1++];
670
+ let h = src[position$1++];
657
671
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
658
- position -= 8;
672
+ position$1 -= 8;
659
673
  return
660
674
  }
661
675
  if (length < 10) {
662
676
  if (length === 8)
663
677
  return fromCharCode(a, b, c, d, e, f, g, h)
664
678
  else {
665
- let i = src[position++];
679
+ let i = src[position$1++];
666
680
  if ((i & 0x80) > 0) {
667
- position -= 9;
681
+ position$1 -= 9;
668
682
  return
669
683
  }
670
684
  return fromCharCode(a, b, c, d, e, f, g, h, i)
671
685
  }
672
686
  } else if (length < 12) {
673
- let i = src[position++];
674
- let j = src[position++];
687
+ let i = src[position$1++];
688
+ let j = src[position$1++];
675
689
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
676
- position -= 10;
690
+ position$1 -= 10;
677
691
  return
678
692
  }
679
693
  if (length < 11)
680
694
  return fromCharCode(a, b, c, d, e, f, g, h, i, j)
681
- let k = src[position++];
695
+ let k = src[position$1++];
682
696
  if ((k & 0x80) > 0) {
683
- position -= 11;
697
+ position$1 -= 11;
684
698
  return
685
699
  }
686
700
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
687
701
  } else {
688
- let i = src[position++];
689
- let j = src[position++];
690
- let k = src[position++];
691
- let l = src[position++];
702
+ let i = src[position$1++];
703
+ let j = src[position$1++];
704
+ let k = src[position$1++];
705
+ let l = src[position$1++];
692
706
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
693
- position -= 12;
707
+ position$1 -= 12;
694
708
  return
695
709
  }
696
710
  if (length < 14) {
697
711
  if (length === 12)
698
712
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
699
713
  else {
700
- let m = src[position++];
714
+ let m = src[position$1++];
701
715
  if ((m & 0x80) > 0) {
702
- position -= 13;
716
+ position$1 -= 13;
703
717
  return
704
718
  }
705
719
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
706
720
  }
707
721
  } else {
708
- let m = src[position++];
709
- let n = src[position++];
722
+ let m = src[position$1++];
723
+ let n = src[position$1++];
710
724
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
711
- position -= 14;
725
+ position$1 -= 14;
712
726
  return
713
727
  }
714
728
  if (length < 15)
715
729
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
716
- let o = src[position++];
730
+ let o = src[position$1++];
717
731
  if ((o & 0x80) > 0) {
718
- position -= 15;
732
+ position$1 -= 15;
719
733
  return
720
734
  }
721
735
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
@@ -728,14 +742,14 @@ function shortStringInJS(length) {
728
742
  function readBin(length) {
729
743
  return currentDecoder.copyBuffers ?
730
744
  // specifically use the copying slice (not the node one)
731
- Uint8Array.prototype.slice.call(src, position, position += length) :
732
- src.subarray(position, position += length)
745
+ Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
746
+ src.subarray(position$1, position$1 += length)
733
747
  }
734
748
  let f32Array = new Float32Array(1);
735
749
  let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
736
750
  function getFloat16() {
737
- let byte0 = src[position++];
738
- let byte1 = src[position++];
751
+ let byte0 = src[position$1++];
752
+ let byte1 = src[position$1++];
739
753
  let exponent = (byte0 & 0x7f) >> 2;
740
754
  if (exponent === 0x1f) { // specials
741
755
  if (byte1 || (byte0 & 3))
@@ -757,7 +771,7 @@ function getFloat16() {
757
771
  return f32Array[0];
758
772
  }
759
773
 
760
- let keyCache = new Array(4096);
774
+ new Array(4096);
761
775
 
762
776
  class Tag {
763
777
  constructor(value, tag) {
@@ -800,9 +814,8 @@ currentExtensions[5] = (fraction) => {
800
814
  };
801
815
 
802
816
  // the registration of the record definition extension
803
- const recordDefinition = (definition) => {
804
- let id = definition[0] - 0xe000;
805
- let structure = definition[1];
817
+ const recordDefinition = (id, structure) => {
818
+ id = id - 0xe000;
806
819
  let existingStructure = currentStructures[id];
807
820
  if (existingStructure && existingStructure.isShared) {
808
821
  (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
@@ -810,26 +823,26 @@ const recordDefinition = (definition) => {
810
823
  currentStructures[id] = structure;
811
824
 
812
825
  structure.read = createStructureReader(structure);
826
+ };
827
+ currentExtensions[LEGACY_RECORD_INLINE_ID] = (data) => {
828
+ let length = data.length;
829
+ let structure = data[1];
830
+ recordDefinition(data[0], structure);
813
831
  let object = {};
814
- if (currentDecoder.keyMap) for (let i = 2,l = definition.length; i < l; i++) {
815
- let key = currentDecoder.decodeKey(structure[i - 2]);
816
- object[safeKey(key)] = definition[i];
817
- }
818
- else for (let i = 2,l = definition.length; i < l; i++) {
819
- let key = structure[i - 2];
820
- object[safeKey(key)] = definition[i];
821
- }
832
+ for (let i = 2; i < length; i++) {
833
+ let key = structure[i - 2];
834
+ object[safeKey(key)] = data[i];
835
+ }
822
836
  return object
823
837
  };
824
- currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition;
825
838
  currentExtensions[14] = (value) => {
826
- if (bundledStrings)
827
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 += value)
839
+ if (bundledStrings$1)
840
+ return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
828
841
  return new Tag(value, 14)
829
842
  };
830
843
  currentExtensions[15] = (value) => {
831
- if (bundledStrings)
832
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
844
+ if (bundledStrings$1)
845
+ return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
833
846
  return new Tag(value, 15)
834
847
  };
835
848
  let glbl = { Error, RegExp };
@@ -837,7 +850,7 @@ currentExtensions[27] = (data) => { // http://cbor.schmorp.de/generic-object
837
850
  return (glbl[data[0]] || Error)(data[1], data[2])
838
851
  };
839
852
  const packedTable = (read) => {
840
- if (src[position++] != 0x84)
853
+ if (src[position$1++] != 0x84)
841
854
  throw new Error('Packed values structure must be followed by a 4 element array')
842
855
  let newPackedValues = read(); // packed values
843
856
  packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues;
@@ -859,18 +872,23 @@ currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { // packed reference
859
872
  return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
860
873
  throw new Error('No support for non-integer packed references yet')
861
874
  };
862
- currentExtensions[25] = (id) => {
863
- return stringRefs[id]
864
- };
865
- currentExtensions[256] = (read) => {
866
- stringRefs = [];
867
- try {
868
- return read()
869
- } finally {
870
- stringRefs = null;
871
- }
872
- };
873
- currentExtensions[256].handlesRead = true;
875
+
876
+ // The following code is an incomplete implementation of http://cbor.schmorp.de/stringref
877
+ // the real thing would need to implemennt more logic to populate the stringRefs table and
878
+ // maintain a stack of stringRef "namespaces".
879
+ //
880
+ // currentExtensions[25] = (id) => {
881
+ // return stringRefs[id]
882
+ // }
883
+ // currentExtensions[256] = (read) => {
884
+ // stringRefs = []
885
+ // try {
886
+ // return read()
887
+ // } finally {
888
+ // stringRefs = null
889
+ // }
890
+ // }
891
+ // currentExtensions[256].handlesRead = true
874
892
 
875
893
  currentExtensions[28] = (read) => {
876
894
  // shareable http://cbor.schmorp.de/value-sharing (for structured clones)
@@ -879,7 +897,7 @@ currentExtensions[28] = (read) => {
879
897
  referenceMap.id = 0;
880
898
  }
881
899
  let id = referenceMap.id++;
882
- let token = src[position];
900
+ let token = src[position$1];
883
901
  let target;
884
902
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
885
903
  // ahead past references to record structure definitions
@@ -956,7 +974,7 @@ currentExtensionRanges.push((tag, input) => {
956
974
  return input
957
975
  });
958
976
 
959
- const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
977
+ const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
960
978
  const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
961
979
  typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
962
980
  typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
@@ -973,7 +991,7 @@ function registerTypedArray(TypedArray, tag) {
973
991
  if (!littleEndian && bytesPerElement == 1)
974
992
  continue
975
993
  let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : 3;
976
- currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine) ? (buffer) => {
994
+ currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
977
995
  if (!TypedArray)
978
996
  throw new Error('Could not find typed array for code ' + tag)
979
997
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
@@ -995,36 +1013,36 @@ function registerTypedArray(TypedArray, tag) {
995
1013
 
996
1014
  function readBundleExt() {
997
1015
  let length = readJustLength();
998
- let bundlePosition = position + read();
1016
+ let bundlePosition = position$1 + read();
999
1017
  for (let i = 2; i < length; i++) {
1000
1018
  // skip past bundles that were already read
1001
1019
  let bundleLength = readJustLength(); // this will increment position, so must add to position afterwards
1002
- position += bundleLength;
1020
+ position$1 += bundleLength;
1003
1021
  }
1004
- let dataPosition = position;
1005
- position = bundlePosition;
1006
- bundledStrings = [readStringJS(readJustLength()), readStringJS(readJustLength())];
1007
- bundledStrings.position0 = 0;
1008
- bundledStrings.position1 = 0;
1009
- bundledStrings.postBundlePosition = position;
1010
- position = dataPosition;
1022
+ let dataPosition = position$1;
1023
+ position$1 = bundlePosition;
1024
+ bundledStrings$1 = [readStringJS(readJustLength()), readStringJS(readJustLength())];
1025
+ bundledStrings$1.position0 = 0;
1026
+ bundledStrings$1.position1 = 0;
1027
+ bundledStrings$1.postBundlePosition = position$1;
1028
+ position$1 = dataPosition;
1011
1029
  return read()
1012
1030
  }
1013
1031
 
1014
1032
  function readJustLength() {
1015
- let token = src[position++] & 0x1f;
1033
+ let token = src[position$1++] & 0x1f;
1016
1034
  if (token > 0x17) {
1017
1035
  switch (token) {
1018
1036
  case 0x18:
1019
- token = src[position++];
1037
+ token = src[position$1++];
1020
1038
  break
1021
1039
  case 0x19:
1022
- token = dataView.getUint16(position);
1023
- position += 2;
1040
+ token = dataView.getUint16(position$1);
1041
+ position$1 += 2;
1024
1042
  break
1025
1043
  case 0x1a:
1026
- token = dataView.getUint32(position);
1027
- position += 4;
1044
+ token = dataView.getUint32(position$1);
1045
+ position$1 += 4;
1028
1046
  break
1029
1047
  }
1030
1048
  }
@@ -1050,14 +1068,14 @@ function loadShared() {
1050
1068
 
1051
1069
  function saveState(callback) {
1052
1070
  let savedSrcEnd = srcEnd;
1053
- let savedPosition = position;
1071
+ let savedPosition = position$1;
1054
1072
  let savedStringPosition = stringPosition;
1055
1073
  let savedSrcStringStart = srcStringStart;
1056
1074
  let savedSrcStringEnd = srcStringEnd;
1057
1075
  let savedSrcString = srcString;
1058
1076
  let savedStrings = strings;
1059
1077
  let savedReferenceMap = referenceMap;
1060
- let savedBundledStrings = bundledStrings;
1078
+ let savedBundledStrings = bundledStrings$1;
1061
1079
 
1062
1080
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1063
1081
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1066,14 +1084,14 @@ function saveState(callback) {
1066
1084
  let savedSequentialMode = sequentialMode;
1067
1085
  let value = callback();
1068
1086
  srcEnd = savedSrcEnd;
1069
- position = savedPosition;
1087
+ position$1 = savedPosition;
1070
1088
  stringPosition = savedStringPosition;
1071
1089
  srcStringStart = savedSrcStringStart;
1072
1090
  srcStringEnd = savedSrcStringEnd;
1073
1091
  srcString = savedSrcString;
1074
1092
  strings = savedStrings;
1075
1093
  referenceMap = savedReferenceMap;
1076
- bundledStrings = savedBundledStrings;
1094
+ bundledStrings$1 = savedBundledStrings;
1077
1095
  src = savedSrc;
1078
1096
  sequentialMode = savedSequentialMode;
1079
1097
  currentStructures = savedStructures;
@@ -1087,7 +1105,7 @@ function clearSource() {
1087
1105
  currentStructures = null;
1088
1106
  }
1089
1107
 
1090
- function addExtension(extension) {
1108
+ function addExtension$1(extension) {
1091
1109
  currentExtensions[extension.tag] = extension.decode;
1092
1110
  }
1093
1111
 
@@ -1115,16 +1133,18 @@ try {
1115
1133
  textEncoder = new TextEncoder();
1116
1134
  } catch (error) {}
1117
1135
  let extensions, extensionClasses;
1118
- const hasNodeBuffer = typeof Buffer !== 'undefined';
1119
- const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
1120
- const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1136
+ const Buffer$1 = globalThis.Buffer;
1137
+ const hasNodeBuffer = typeof Buffer$1 !== 'undefined';
1138
+ const ByteArrayAllocate = hasNodeBuffer ? Buffer$1.allocUnsafeSlow : Uint8Array;
1139
+ const ByteArray = hasNodeBuffer ? Buffer$1 : Uint8Array;
1121
1140
  const MAX_STRUCTURES = 0x100;
1122
1141
  const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1142
+ let throwOnIterable;
1123
1143
  let target;
1124
1144
  let targetView;
1125
- let position$1 = 0;
1145
+ let position = 0;
1126
1146
  let safeEnd;
1127
- let bundledStrings$1 = null;
1147
+ let bundledStrings = null;
1128
1148
  const MAX_BUNDLE_SIZE = 0xf000;
1129
1149
  const hasNonLatin = /[\u0080-\uFFFF]/;
1130
1150
  const RECORD_SYMBOL = Symbol('record-id');
@@ -1193,28 +1213,28 @@ class Encoder extends Decoder {
1193
1213
  if (!target) {
1194
1214
  target = new ByteArrayAllocate(8192);
1195
1215
  targetView = new DataView(target.buffer, 0, 8192);
1196
- position$1 = 0;
1216
+ position = 0;
1197
1217
  }
1198
1218
  safeEnd = target.length - 10;
1199
- if (safeEnd - position$1 < 0x800) {
1219
+ if (safeEnd - position < 0x800) {
1200
1220
  // don't start too close to the end,
1201
1221
  target = new ByteArrayAllocate(target.length);
1202
1222
  targetView = new DataView(target.buffer, 0, target.length);
1203
1223
  safeEnd = target.length - 10;
1204
- position$1 = 0;
1224
+ position = 0;
1205
1225
  } else if (encodeOptions === REUSE_BUFFER_MODE)
1206
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1207
- start = position$1;
1226
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1227
+ start = position;
1208
1228
  if (encoder.useSelfDescribedHeader) {
1209
- targetView.setUint32(position$1, 0xd9d9f700); // tag two byte, then self-descriptive tag
1210
- position$1 += 3;
1229
+ targetView.setUint32(position, 0xd9d9f700); // tag two byte, then self-descriptive tag
1230
+ position += 3;
1211
1231
  }
1212
1232
  referenceMap = encoder.structuredClone ? new Map() : null;
1213
1233
  if (encoder.bundleStrings && typeof value !== 'string') {
1214
- bundledStrings$1 = [];
1215
- bundledStrings$1.size = Infinity; // force a new bundle start on first string
1234
+ bundledStrings = [];
1235
+ bundledStrings.size = Infinity; // force a new bundle start on first string
1216
1236
  } else
1217
- bundledStrings$1 = null;
1237
+ bundledStrings = null;
1218
1238
 
1219
1239
  sharedStructures = encoder.structures;
1220
1240
  if (sharedStructures) {
@@ -1270,8 +1290,8 @@ class Encoder extends Decoder {
1270
1290
  packedValues.samplingPackedValues = samplingPackedValues;
1271
1291
  findRepetitiveStrings(value, packedValues);
1272
1292
  if (packedValues.values.length > 0) {
1273
- target[position$1++] = 0xd8; // one-byte tag
1274
- target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1293
+ target[position++] = 0xd8; // one-byte tag
1294
+ target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1275
1295
  writeArrayHeader(4);
1276
1296
  let valuesArray = packedValues.values;
1277
1297
  encode(valuesArray);
@@ -1283,27 +1303,30 @@ class Encoder extends Decoder {
1283
1303
  }
1284
1304
  }
1285
1305
  }
1306
+ throwOnIterable = encodeOptions & THROW_ON_ITERABLE;
1286
1307
  try {
1308
+ if (throwOnIterable)
1309
+ return;
1287
1310
  encode(value);
1288
- if (bundledStrings$1) {
1311
+ if (bundledStrings) {
1289
1312
  writeBundles(start, encode);
1290
1313
  }
1291
- encoder.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1314
+ encoder.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1292
1315
  if (referenceMap && referenceMap.idsToInsert) {
1293
- position$1 += referenceMap.idsToInsert.length * 2;
1294
- if (position$1 > safeEnd)
1295
- makeRoom(position$1);
1296
- encoder.offset = position$1;
1297
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1316
+ position += referenceMap.idsToInsert.length * 2;
1317
+ if (position > safeEnd)
1318
+ makeRoom(position);
1319
+ encoder.offset = position;
1320
+ let serialized = insertIds(target.subarray(start, position), referenceMap.idsToInsert);
1298
1321
  referenceMap = null;
1299
1322
  return serialized
1300
1323
  }
1301
1324
  if (encodeOptions & REUSE_BUFFER_MODE) {
1302
1325
  target.start = start;
1303
- target.end = position$1;
1326
+ target.end = position;
1304
1327
  return target
1305
1328
  }
1306
- return target.subarray(start, position$1) // position can change if we call encode again in saveShared, so we get the buffer now
1329
+ return target.subarray(start, position) // position can change if we call encode again in saveShared, so we get the buffer now
1307
1330
  } finally {
1308
1331
  if (sharedStructures) {
1309
1332
  if (serializationsSinceTransitionRebuild < 10)
@@ -1330,13 +1353,13 @@ class Encoder extends Decoder {
1330
1353
  encoder.structures = encoder.structures.slice(0, maxSharedStructures);
1331
1354
  }
1332
1355
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1333
- let returnBuffer = target.subarray(start, position$1);
1356
+ let returnBuffer = target.subarray(start, position);
1334
1357
  if (encoder.updateSharedData() === false)
1335
1358
  return encoder.encode(value) // re-encode if it fails
1336
1359
  return returnBuffer
1337
1360
  }
1338
1361
  if (encodeOptions & RESET_BUFFER_MODE)
1339
- position$1 = start;
1362
+ position = start;
1340
1363
  }
1341
1364
  };
1342
1365
  this.findCommonStringsToPack = () => {
@@ -1360,8 +1383,8 @@ class Encoder extends Decoder {
1360
1383
  }
1361
1384
  };
1362
1385
  const encode = (value) => {
1363
- if (position$1 > safeEnd)
1364
- target = makeRoom(position$1);
1386
+ if (position > safeEnd)
1387
+ target = makeRoom(position);
1365
1388
 
1366
1389
  var type = typeof value;
1367
1390
  var length;
@@ -1370,9 +1393,9 @@ class Encoder extends Decoder {
1370
1393
  let packedPosition = packedObjectMap[value];
1371
1394
  if (packedPosition >= 0) {
1372
1395
  if (packedPosition < 16)
1373
- target[position$1++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1396
+ target[position++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1374
1397
  else {
1375
- target[position$1++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1398
+ target[position++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1376
1399
  if (packedPosition & 1)
1377
1400
  encode((15 - packedPosition) >> 1);
1378
1401
  else
@@ -1404,30 +1427,30 @@ class Encoder extends Decoder {
1404
1427
  }
1405
1428
  }
1406
1429
  let strLength = value.length;
1407
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x400) {
1408
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1430
+ if (bundledStrings && strLength >= 4 && strLength < 0x400) {
1431
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1409
1432
  let extStart;
1410
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1411
- if (position$1 + maxBytes > safeEnd)
1412
- target = makeRoom(position$1 + maxBytes);
1413
- target[position$1++] = 0xd9; // tag 16-bit
1414
- target[position$1++] = 0xdf; // tag 0xdff9
1415
- target[position$1++] = 0xf9;
1433
+ let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1434
+ if (position + maxBytes > safeEnd)
1435
+ target = makeRoom(position + maxBytes);
1436
+ target[position++] = 0xd9; // tag 16-bit
1437
+ target[position++] = 0xdf; // tag 0xdff9
1438
+ target[position++] = 0xf9;
1416
1439
  // TODO: If we only have one bundle with any string data, only write one string bundle
1417
- target[position$1++] = bundledStrings$1.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
1418
- target[position$1++] = 0x1a; // 32-bit unsigned int
1419
- extStart = position$1 - start;
1420
- position$1 += 4; // reserve for writing bundle reference
1421
- if (bundledStrings$1.position) {
1440
+ target[position++] = bundledStrings.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
1441
+ target[position++] = 0x1a; // 32-bit unsigned int
1442
+ extStart = position - start;
1443
+ position += 4; // reserve for writing bundle reference
1444
+ if (bundledStrings.position) {
1422
1445
  writeBundles(start, encode); // write the last bundles
1423
1446
  }
1424
- bundledStrings$1 = ['', '']; // create new ones
1425
- bundledStrings$1.size = 0;
1426
- bundledStrings$1.position = extStart;
1447
+ bundledStrings = ['', '']; // create new ones
1448
+ bundledStrings.size = 0;
1449
+ bundledStrings.position = extStart;
1427
1450
  }
1428
1451
  let twoByte = hasNonLatin.test(value);
1429
- bundledStrings$1[twoByte ? 0 : 1] += value;
1430
- target[position$1++] = twoByte ? 0xce : 0xcf;
1452
+ bundledStrings[twoByte ? 0 : 1] += value;
1453
+ target[position++] = twoByte ? 0xce : 0xcf;
1431
1454
  encode(strLength);
1432
1455
  return
1433
1456
  }
@@ -1443,11 +1466,11 @@ class Encoder extends Decoder {
1443
1466
  headerSize = 5;
1444
1467
  }
1445
1468
  let maxBytes = strLength * 3;
1446
- if (position$1 + maxBytes > safeEnd)
1447
- target = makeRoom(position$1 + maxBytes);
1469
+ if (position + maxBytes > safeEnd)
1470
+ target = makeRoom(position + maxBytes);
1448
1471
 
1449
1472
  if (strLength < 0x40 || !encodeUtf8) {
1450
- let i, c1, c2, strPosition = position$1 + headerSize;
1473
+ let i, c1, c2, strPosition = position + headerSize;
1451
1474
  for (i = 0; i < strLength; i++) {
1452
1475
  c1 = value.charCodeAt(i);
1453
1476
  if (c1 < 0x80) {
@@ -1471,105 +1494,105 @@ class Encoder extends Decoder {
1471
1494
  target[strPosition++] = c1 & 0x3f | 0x80;
1472
1495
  }
1473
1496
  }
1474
- length = strPosition - position$1 - headerSize;
1497
+ length = strPosition - position - headerSize;
1475
1498
  } else {
1476
- length = encodeUtf8(value, position$1 + headerSize, maxBytes);
1499
+ length = encodeUtf8(value, position + headerSize, maxBytes);
1477
1500
  }
1478
1501
 
1479
1502
  if (length < 0x18) {
1480
- target[position$1++] = 0x60 | length;
1503
+ target[position++] = 0x60 | length;
1481
1504
  } else if (length < 0x100) {
1482
1505
  if (headerSize < 2) {
1483
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1506
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1484
1507
  }
1485
- target[position$1++] = 0x78;
1486
- target[position$1++] = length;
1508
+ target[position++] = 0x78;
1509
+ target[position++] = length;
1487
1510
  } else if (length < 0x10000) {
1488
1511
  if (headerSize < 3) {
1489
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1512
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1490
1513
  }
1491
- target[position$1++] = 0x79;
1492
- target[position$1++] = length >> 8;
1493
- target[position$1++] = length & 0xff;
1514
+ target[position++] = 0x79;
1515
+ target[position++] = length >> 8;
1516
+ target[position++] = length & 0xff;
1494
1517
  } else {
1495
1518
  if (headerSize < 5) {
1496
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1519
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1497
1520
  }
1498
- target[position$1++] = 0x7a;
1499
- targetView.setUint32(position$1, length);
1500
- position$1 += 4;
1521
+ target[position++] = 0x7a;
1522
+ targetView.setUint32(position, length);
1523
+ position += 4;
1501
1524
  }
1502
- position$1 += length;
1525
+ position += length;
1503
1526
  } else if (type === 'number') {
1504
1527
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1505
1528
  // positive uint
1506
1529
  if (value < 0x18) {
1507
- target[position$1++] = value;
1530
+ target[position++] = value;
1508
1531
  } else if (value < 0x100) {
1509
- target[position$1++] = 0x18;
1510
- target[position$1++] = value;
1532
+ target[position++] = 0x18;
1533
+ target[position++] = value;
1511
1534
  } else if (value < 0x10000) {
1512
- target[position$1++] = 0x19;
1513
- target[position$1++] = value >> 8;
1514
- target[position$1++] = value & 0xff;
1535
+ target[position++] = 0x19;
1536
+ target[position++] = value >> 8;
1537
+ target[position++] = value & 0xff;
1515
1538
  } else {
1516
- target[position$1++] = 0x1a;
1517
- targetView.setUint32(position$1, value);
1518
- position$1 += 4;
1539
+ target[position++] = 0x1a;
1540
+ targetView.setUint32(position, value);
1541
+ position += 4;
1519
1542
  }
1520
1543
  } else if (value >> 0 === value) { // negative integer
1521
1544
  if (value >= -0x18) {
1522
- target[position$1++] = 0x1f - value;
1545
+ target[position++] = 0x1f - value;
1523
1546
  } else if (value >= -0x100) {
1524
- target[position$1++] = 0x38;
1525
- target[position$1++] = ~value;
1547
+ target[position++] = 0x38;
1548
+ target[position++] = ~value;
1526
1549
  } else if (value >= -0x10000) {
1527
- target[position$1++] = 0x39;
1528
- targetView.setUint16(position$1, ~value);
1529
- position$1 += 2;
1550
+ target[position++] = 0x39;
1551
+ targetView.setUint16(position, ~value);
1552
+ position += 2;
1530
1553
  } else {
1531
- target[position$1++] = 0x3a;
1532
- targetView.setUint32(position$1, ~value);
1533
- position$1 += 4;
1554
+ target[position++] = 0x3a;
1555
+ targetView.setUint32(position, ~value);
1556
+ position += 4;
1534
1557
  }
1535
1558
  } else {
1536
1559
  let useFloat32;
1537
1560
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1538
- target[position$1++] = 0xfa;
1539
- targetView.setFloat32(position$1, value);
1561
+ target[position++] = 0xfa;
1562
+ targetView.setFloat32(position, value);
1540
1563
  let xShifted;
1541
1564
  if (useFloat32 < 4 ||
1542
1565
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1543
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1544
- position$1 += 4;
1566
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1567
+ position += 4;
1545
1568
  return
1546
1569
  } else
1547
- position$1--; // move back into position for writing a double
1570
+ position--; // move back into position for writing a double
1548
1571
  }
1549
- target[position$1++] = 0xfb;
1550
- targetView.setFloat64(position$1, value);
1551
- position$1 += 8;
1572
+ target[position++] = 0xfb;
1573
+ targetView.setFloat64(position, value);
1574
+ position += 8;
1552
1575
  }
1553
1576
  } else if (type === 'object') {
1554
1577
  if (!value)
1555
- target[position$1++] = 0xf6;
1578
+ target[position++] = 0xf6;
1556
1579
  else {
1557
1580
  if (referenceMap) {
1558
1581
  let referee = referenceMap.get(value);
1559
1582
  if (referee) {
1560
- target[position$1++] = 0xd8;
1561
- target[position$1++] = 29; // http://cbor.schmorp.de/value-sharing
1562
- target[position$1++] = 0x19; // 16-bit uint
1583
+ target[position++] = 0xd8;
1584
+ target[position++] = 29; // http://cbor.schmorp.de/value-sharing
1585
+ target[position++] = 0x19; // 16-bit uint
1563
1586
  if (!referee.references) {
1564
1587
  let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1565
1588
  referee.references = [];
1566
1589
  idsToInsert.push(referee);
1567
1590
  }
1568
- referee.references.push(position$1 - start);
1569
- position$1 += 2; // TODO: also support 32-bit
1591
+ referee.references.push(position - start);
1592
+ position += 2; // TODO: also support 32-bit
1570
1593
  return
1571
1594
  } else
1572
- referenceMap.set(value, { offset: position$1 - start });
1595
+ referenceMap.set(value, { offset: position - start });
1573
1596
  }
1574
1597
  let constructor = value.constructor;
1575
1598
  if (constructor === Object) {
@@ -1577,7 +1600,7 @@ class Encoder extends Decoder {
1577
1600
  } else if (constructor === Array) {
1578
1601
  length = value.length;
1579
1602
  if (length < 0x18) {
1580
- target[position$1++] = 0x80 | length;
1603
+ target[position++] = 0x80 | length;
1581
1604
  } else {
1582
1605
  writeArrayHeader(length);
1583
1606
  }
@@ -1587,24 +1610,24 @@ class Encoder extends Decoder {
1587
1610
  } else if (constructor === Map) {
1588
1611
  if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
1589
1612
  // use Tag 259 (https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec--explicit-maps.md) for maps if the user wants it that way
1590
- target[position$1++] = 0xd9;
1591
- target[position$1++] = 1;
1592
- target[position$1++] = 3;
1613
+ target[position++] = 0xd9;
1614
+ target[position++] = 1;
1615
+ target[position++] = 3;
1593
1616
  }
1594
1617
  length = value.size;
1595
1618
  if (length < 0x18) {
1596
- target[position$1++] = 0xa0 | length;
1619
+ target[position++] = 0xa0 | length;
1597
1620
  } else if (length < 0x100) {
1598
- target[position$1++] = 0xb8;
1599
- target[position$1++] = length;
1621
+ target[position++] = 0xb8;
1622
+ target[position++] = length;
1600
1623
  } else if (length < 0x10000) {
1601
- target[position$1++] = 0xb9;
1602
- target[position$1++] = length >> 8;
1603
- target[position$1++] = length & 0xff;
1624
+ target[position++] = 0xb9;
1625
+ target[position++] = length >> 8;
1626
+ target[position++] = length & 0xff;
1604
1627
  } else {
1605
- target[position$1++] = 0xba;
1606
- targetView.setUint32(position$1, length);
1607
- position$1 += 4;
1628
+ target[position++] = 0xba;
1629
+ targetView.setUint32(position, length);
1630
+ position += 4;
1608
1631
  }
1609
1632
  if (encoder.keyMap) {
1610
1633
  for (let [ key, entryValue ] of value) {
@@ -1626,58 +1649,68 @@ class Encoder extends Decoder {
1626
1649
  if (tag == undefined)
1627
1650
  tag = extension.getTag && extension.getTag.call(this, value);
1628
1651
  if (tag < 0x18) {
1629
- target[position$1++] = 0xc0 | tag;
1652
+ target[position++] = 0xc0 | tag;
1630
1653
  } else if (tag < 0x100) {
1631
- target[position$1++] = 0xd8;
1632
- target[position$1++] = tag;
1654
+ target[position++] = 0xd8;
1655
+ target[position++] = tag;
1633
1656
  } else if (tag < 0x10000) {
1634
- target[position$1++] = 0xd9;
1635
- target[position$1++] = tag >> 8;
1636
- target[position$1++] = tag & 0xff;
1657
+ target[position++] = 0xd9;
1658
+ target[position++] = tag >> 8;
1659
+ target[position++] = tag & 0xff;
1637
1660
  } else if (tag > -1) {
1638
- target[position$1++] = 0xda;
1639
- targetView.setUint32(position$1, tag);
1640
- position$1 += 4;
1661
+ target[position++] = 0xda;
1662
+ targetView.setUint32(position, tag);
1663
+ position += 4;
1641
1664
  } // else undefined, don't write tag
1642
1665
  extension.encode.call(this, value, encode, makeRoom);
1643
1666
  return
1644
1667
  }
1645
1668
  }
1646
1669
  if (value[Symbol.iterator]) {
1647
- target[position$1++] = 0x9f; // indefinite length array
1670
+ if (throwOnIterable) {
1671
+ let error = new Error('Iterable should be serialized as iterator');
1672
+ error.iteratorNotHandled = true;
1673
+ throw error;
1674
+ }
1675
+ target[position++] = 0x9f; // indefinite length array
1648
1676
  for (let entry of value) {
1649
1677
  encode(entry);
1650
1678
  }
1651
- target[position$1++] = 0xff; // stop-code
1679
+ target[position++] = 0xff; // stop-code
1652
1680
  return
1653
1681
  }
1682
+ if (value[Symbol.asyncIterator] || isBlob(value)) {
1683
+ let error = new Error('Iterable/blob should be serialized as iterator');
1684
+ error.iteratorNotHandled = true;
1685
+ throw error;
1686
+ }
1654
1687
  // no extension found, write as object
1655
1688
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1656
1689
  }
1657
1690
  }
1658
1691
  } else if (type === 'boolean') {
1659
- target[position$1++] = value ? 0xf5 : 0xf4;
1692
+ target[position++] = value ? 0xf5 : 0xf4;
1660
1693
  } else if (type === 'bigint') {
1661
1694
  if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
1662
1695
  // use an unsigned int as long as it fits
1663
- target[position$1++] = 0x1b;
1664
- targetView.setBigUint64(position$1, value);
1696
+ target[position++] = 0x1b;
1697
+ targetView.setBigUint64(position, value);
1665
1698
  } else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
1666
1699
  // if we can fit an unsigned int, use that
1667
- target[position$1++] = 0x3b;
1668
- targetView.setBigUint64(position$1, -value - BigInt(1));
1700
+ target[position++] = 0x3b;
1701
+ targetView.setBigUint64(position, -value - BigInt(1));
1669
1702
  } else {
1670
1703
  // overflow
1671
1704
  if (this.largeBigIntToFloat) {
1672
- target[position$1++] = 0xfb;
1673
- targetView.setFloat64(position$1, Number(value));
1705
+ target[position++] = 0xfb;
1706
+ targetView.setFloat64(position, Number(value));
1674
1707
  } else {
1675
1708
  throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1676
1709
  }
1677
1710
  }
1678
- position$1 += 8;
1711
+ position += 8;
1679
1712
  } else if (type === 'undefined') {
1680
- target[position$1++] = 0xf7;
1713
+ target[position++] = 0xf7;
1681
1714
  } else {
1682
1715
  throw new Error('Unknown type: ' + type)
1683
1716
  }
@@ -1689,18 +1722,18 @@ class Encoder extends Decoder {
1689
1722
  let vals = Object.values(object);
1690
1723
  let length = keys.length;
1691
1724
  if (length < 0x18) {
1692
- target[position$1++] = 0xa0 | length;
1725
+ target[position++] = 0xa0 | length;
1693
1726
  } else if (length < 0x100) {
1694
- target[position$1++] = 0xb8;
1695
- target[position$1++] = length;
1727
+ target[position++] = 0xb8;
1728
+ target[position++] = length;
1696
1729
  } else if (length < 0x10000) {
1697
- target[position$1++] = 0xb9;
1698
- target[position$1++] = length >> 8;
1699
- target[position$1++] = length & 0xff;
1730
+ target[position++] = 0xb9;
1731
+ target[position++] = length >> 8;
1732
+ target[position++] = length & 0xff;
1700
1733
  } else {
1701
- target[position$1++] = 0xba;
1702
- targetView.setUint32(position$1, length);
1703
- position$1 += 4;
1734
+ target[position++] = 0xba;
1735
+ targetView.setUint32(position, length);
1736
+ position += 4;
1704
1737
  }
1705
1738
  if (encoder.keyMap) {
1706
1739
  for (let i = 0; i < length; i++) {
@@ -1715,9 +1748,9 @@ class Encoder extends Decoder {
1715
1748
  }
1716
1749
  } :
1717
1750
  (object, safePrototype) => {
1718
- target[position$1++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
1719
- let objectOffset = position$1 - start;
1720
- position$1 += 2;
1751
+ target[position++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
1752
+ let objectOffset = position - start;
1753
+ position += 2;
1721
1754
  let size = 0;
1722
1755
  if (encoder.keyMap) {
1723
1756
  for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
@@ -1770,9 +1803,9 @@ class Encoder extends Decoder {
1770
1803
  let recordId = transition[RECORD_SYMBOL];
1771
1804
  if (recordId !== undefined) {
1772
1805
  recordId &= 0xffff;
1773
- target[position$1++] = 0xd9;
1774
- target[position$1++] = (recordId >> 8) | 0xe0;
1775
- target[position$1++] = recordId & 0xff;
1806
+ target[position++] = 0xd9;
1807
+ target[position++] = (recordId >> 8) | 0xe0;
1808
+ target[position++] = recordId & 0xff;
1776
1809
  } else {
1777
1810
  if (!keys)
1778
1811
  keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
@@ -1790,9 +1823,9 @@ class Encoder extends Decoder {
1790
1823
  }
1791
1824
  structures[recordId] = keys;
1792
1825
  if (recordId < maxSharedStructures) {
1793
- target[position$1++] = 0xd9;
1794
- target[position$1++] = (recordId >> 8) | 0xe0;
1795
- target[position$1++] = recordId & 0xff;
1826
+ target[position++] = 0xd9;
1827
+ target[position++] = (recordId >> 8) | 0xe0;
1828
+ target[position++] = recordId & 0xff;
1796
1829
  transition = structures.transitions;
1797
1830
  for (let i = 0; i < length; i++) {
1798
1831
  if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
@@ -1803,8 +1836,8 @@ class Encoder extends Decoder {
1803
1836
  hasSharedUpdate = true;
1804
1837
  } else {
1805
1838
  transition[RECORD_SYMBOL] = recordId;
1806
- targetView.setUint32(position$1, 0xd9dfff00); // tag two byte, then record definition id
1807
- position$1 += 3;
1839
+ targetView.setUint32(position, 0xd9dfff00); // tag two byte, then record definition id
1840
+ position += 3;
1808
1841
  if (newTransitions)
1809
1842
  transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
1810
1843
  // record the removal of the id, we can maintain our shared structure
@@ -1814,15 +1847,19 @@ class Encoder extends Decoder {
1814
1847
  writeArrayHeader(length + 2);
1815
1848
  encode(0xe000 + recordId);
1816
1849
  encode(keys);
1817
- for (let v of Object.values(object)) encode(v);
1850
+ if (safePrototype === null) return; // special exit for iterator
1851
+ for (let key in object)
1852
+ if (safePrototype || object.hasOwnProperty(key))
1853
+ encode(object[key]);
1818
1854
  return
1819
1855
  }
1820
1856
  }
1821
1857
  if (length < 0x18) { // write the array header
1822
- target[position$1++] = 0x80 | length;
1858
+ target[position++] = 0x80 | length;
1823
1859
  } else {
1824
1860
  writeArrayHeader(length);
1825
1861
  }
1862
+ if (safePrototype === null) return; // special exit for iterator
1826
1863
  for (let key in object)
1827
1864
  if (safePrototype || object.hasOwnProperty(key))
1828
1865
  encode(object[key]);
@@ -1843,17 +1880,142 @@ class Encoder extends Decoder {
1843
1880
  target.copy(newBuffer, 0, start, end);
1844
1881
  else
1845
1882
  newBuffer.set(target.slice(start, end));
1846
- position$1 -= start;
1883
+ position -= start;
1847
1884
  start = 0;
1848
1885
  safeEnd = newBuffer.length - 10;
1849
1886
  return target = newBuffer
1850
1887
  };
1888
+ let chunkThreshold = 100;
1889
+ let continuedChunkThreshold = 1000;
1890
+ this.encodeAsIterable = function(value, options) {
1891
+ return startEncoding(value, options, encodeObjectAsIterable);
1892
+ };
1893
+ this.encodeAsAsyncIterable = function(value, options) {
1894
+ return startEncoding(value, options, encodeObjectAsAsyncIterable);
1895
+ };
1896
+
1897
+ function* encodeObjectAsIterable(object, iterateProperties, finalIterable) {
1898
+ let constructor = object.constructor;
1899
+ if (constructor === Object) {
1900
+ let useRecords = encoder.useRecords !== false;
1901
+ if (useRecords)
1902
+ writeObject(object, null); // write the record identifier
1903
+ else
1904
+ writeEntityLength(Object.keys(object).length, 0xa0);
1905
+ for (let key in object) {
1906
+ let value = object[key];
1907
+ if (!useRecords) encode(key);
1908
+ if (value && typeof value === 'object') {
1909
+ if (iterateProperties[key])
1910
+ yield* encodeObjectAsIterable(value, iterateProperties[key]);
1911
+ else
1912
+ yield* tryEncode(value, iterateProperties, key);
1913
+ } else encode(value);
1914
+ }
1915
+ } else if (constructor === Array) {
1916
+ let length = object.length;
1917
+ writeArrayHeader(length);
1918
+ for (let i = 0; i < length; i++) {
1919
+ let value = object[i];
1920
+ if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
1921
+ if (iterateProperties.element)
1922
+ yield* encodeObjectAsIterable(value, iterateProperties.element);
1923
+ else
1924
+ yield* tryEncode(value, iterateProperties, 'element');
1925
+ } else encode(value);
1926
+ }
1927
+ } else if (object[Symbol.iterator]) {
1928
+ target[position++] = 0x9f; // start indefinite array
1929
+ for (let value of object) {
1930
+ if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
1931
+ if (iterateProperties.element)
1932
+ yield* encodeObjectAsIterable(value, iterateProperties.element);
1933
+ else
1934
+ yield* tryEncode(value, iterateProperties, 'element');
1935
+ } else encode(value);
1936
+ }
1937
+ target[position++] = 0xff; // stop byte
1938
+ } else if (isBlob(object)){
1939
+ writeEntityLength(object.size, 0x40); // encode as binary data
1940
+ yield target.subarray(start, position);
1941
+ yield object; // directly return blobs, they have to be encoded asynchronously
1942
+ restartEncoding();
1943
+ } else if (object[Symbol.asyncIterator]) {
1944
+ target[position++] = 0x9f; // start indefinite array
1945
+ yield target.subarray(start, position);
1946
+ yield object; // directly return async iterators, they have to be encoded asynchronously
1947
+ restartEncoding();
1948
+ target[position++] = 0xff; // stop byte
1949
+ } else {
1950
+ encode(object);
1951
+ }
1952
+ if (finalIterable && position > start) yield target.subarray(start, position);
1953
+ else if (position - start > chunkThreshold) {
1954
+ yield target.subarray(start, position);
1955
+ restartEncoding();
1956
+ }
1957
+ }
1958
+ function* tryEncode(value, iterateProperties, key) {
1959
+ let restart = position - start;
1960
+ try {
1961
+ encode(value);
1962
+ if (position - start > chunkThreshold) {
1963
+ yield target.subarray(start, position);
1964
+ restartEncoding();
1965
+ }
1966
+ } catch (error) {
1967
+ if (error.iteratorNotHandled) {
1968
+ iterateProperties[key] = {};
1969
+ position = start + restart; // restart our position so we don't have partial data from last encode
1970
+ yield* encodeObjectAsIterable.call(this, value, iterateProperties[key]);
1971
+ } else throw error;
1972
+ }
1973
+ }
1974
+ function restartEncoding() {
1975
+ chunkThreshold = continuedChunkThreshold;
1976
+ encoder.encode(null, THROW_ON_ITERABLE); // restart encoding
1977
+ }
1978
+ function startEncoding(value, options, encodeIterable) {
1979
+ if (options && options.chunkThreshold) // explicitly specified chunk sizes
1980
+ chunkThreshold = continuedChunkThreshold = options.chunkThreshold;
1981
+ else // we start with a smaller threshold to get initial bytes sent quickly
1982
+ chunkThreshold = 100;
1983
+ if (value && typeof value === 'object') {
1984
+ encoder.encode(null, THROW_ON_ITERABLE); // start encoding
1985
+ return encodeIterable(value, encoder.iterateProperties || (encoder.iterateProperties = {}), true);
1986
+ }
1987
+ return [encoder.encode(value)];
1988
+ }
1989
+
1990
+ async function* encodeObjectAsAsyncIterable(value, iterateProperties) {
1991
+ for (let encodedValue of encodeObjectAsIterable(value, iterateProperties, true)) {
1992
+ let constructor = encodedValue.constructor;
1993
+ if (constructor === ByteArray || constructor === Uint8Array)
1994
+ yield encodedValue;
1995
+ else if (isBlob(encodedValue)) {
1996
+ let reader = encodedValue.stream().getReader();
1997
+ let next;
1998
+ while (!(next = await reader.read()).done) {
1999
+ yield next.value;
2000
+ }
2001
+ } else if (encodedValue[Symbol.asyncIterator]) {
2002
+ for await (let asyncValue of encodedValue) {
2003
+ restartEncoding();
2004
+ if (asyncValue)
2005
+ yield* encodeObjectAsAsyncIterable(asyncValue, iterateProperties.async || (iterateProperties.async = {}));
2006
+ else yield encoder.encode(asyncValue);
2007
+ }
2008
+ } else {
2009
+ yield encodedValue;
2010
+ }
2011
+ }
2012
+ }
1851
2013
  }
1852
2014
  useBuffer(buffer) {
1853
2015
  // this means we are finished using our own buffer and we can write over it safely
1854
2016
  target = buffer;
1855
2017
  targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1856
- position$1 = 0;
2018
+ position = 0;
1857
2019
  }
1858
2020
  clearSharedData() {
1859
2021
  if (this.structures)
@@ -1883,6 +2045,23 @@ class Encoder extends Decoder {
1883
2045
  return saveResults
1884
2046
  }
1885
2047
  }
2048
+ function writeEntityLength(length, majorValue) {
2049
+ if (length < 0x18)
2050
+ target[position++] = majorValue | length;
2051
+ else if (length < 0x100) {
2052
+ target[position++] = majorValue | 0x18;
2053
+ target[position++] = length;
2054
+ } else if (length < 0x10000) {
2055
+ target[position++] = majorValue | 0x19;
2056
+ target[position++] = length >> 8;
2057
+ target[position++] = length & 0xff;
2058
+ } else {
2059
+ target[position++] = majorValue | 0x1a;
2060
+ targetView.setUint32(position, length);
2061
+ position += 4;
2062
+ }
2063
+
2064
+ }
1886
2065
  class SharedData {
1887
2066
  constructor(structures, values, version) {
1888
2067
  this.structures = structures;
@@ -1893,21 +2072,28 @@ class SharedData {
1893
2072
 
1894
2073
  function writeArrayHeader(length) {
1895
2074
  if (length < 0x18)
1896
- target[position$1++] = 0x80 | length;
2075
+ target[position++] = 0x80 | length;
1897
2076
  else if (length < 0x100) {
1898
- target[position$1++] = 0x98;
1899
- target[position$1++] = length;
2077
+ target[position++] = 0x98;
2078
+ target[position++] = length;
1900
2079
  } else if (length < 0x10000) {
1901
- target[position$1++] = 0x99;
1902
- target[position$1++] = length >> 8;
1903
- target[position$1++] = length & 0xff;
2080
+ target[position++] = 0x99;
2081
+ target[position++] = length >> 8;
2082
+ target[position++] = length & 0xff;
1904
2083
  } else {
1905
- target[position$1++] = 0x9a;
1906
- targetView.setUint32(position$1, length);
1907
- position$1 += 4;
2084
+ target[position++] = 0x9a;
2085
+ targetView.setUint32(position, length);
2086
+ position += 4;
1908
2087
  }
1909
2088
  }
1910
2089
 
2090
+ const BlobConstructor = typeof Blob === 'undefined' ? function(){} : Blob;
2091
+ function isBlob(object) {
2092
+ if (object instanceof BlobConstructor)
2093
+ return true;
2094
+ let tag = object[Symbol.toStringTag];
2095
+ return tag === 'Blob' || tag === 'File';
2096
+ }
1911
2097
  function findRepetitiveStrings(value, packedValues) {
1912
2098
  switch(typeof value) {
1913
2099
  case 'string':
@@ -1957,7 +2143,7 @@ function findRepetitiveStrings(value, packedValues) {
1957
2143
  case 'function': console.log(value);
1958
2144
  }
1959
2145
  }
1960
- const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
2146
+ const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
1961
2147
  extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
1962
2148
  Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
1963
2149
  typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
@@ -1971,14 +2157,14 @@ extensions = [{ // Date
1971
2157
  let seconds = date.getTime() / 1000;
1972
2158
  if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
1973
2159
  // Timestamp 32
1974
- target[position$1++] = 0x1a;
1975
- targetView.setUint32(position$1, seconds);
1976
- position$1 += 4;
2160
+ target[position++] = 0x1a;
2161
+ targetView.setUint32(position, seconds);
2162
+ position += 4;
1977
2163
  } else {
1978
2164
  // Timestamp float64
1979
- target[position$1++] = 0xfb;
1980
- targetView.setFloat64(position$1, seconds);
1981
- position$1 += 8;
2165
+ target[position++] = 0xfb;
2166
+ targetView.setFloat64(position, seconds);
2167
+ position += 8;
1982
2168
  }
1983
2169
  }
1984
2170
  }, { // Set
@@ -2034,8 +2220,8 @@ extensions = [{ // Date
2034
2220
  let packedValues = sharedData.packedValues || [];
2035
2221
  let sharedStructures = sharedData.structures || [];
2036
2222
  if (packedValues.values.length > 0) {
2037
- target[position$1++] = 0xd8; // one-byte tag
2038
- target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
2223
+ target[position++] = 0xd8; // one-byte tag
2224
+ target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
2039
2225
  writeArrayHeader(4);
2040
2226
  let valuesArray = packedValues.values;
2041
2227
  encode(valuesArray);
@@ -2047,8 +2233,8 @@ extensions = [{ // Date
2047
2233
  }
2048
2234
  }
2049
2235
  if (sharedStructures) {
2050
- targetView.setUint32(position$1, 0xd9dffe00);
2051
- position$1 += 3;
2236
+ targetView.setUint32(position, 0xd9dffe00);
2237
+ position += 3;
2052
2238
  let definitions = sharedStructures.slice(0);
2053
2239
  definitions.unshift(0xe000);
2054
2240
  definitions.push(new Tag(sharedData.version, 0x53687264));
@@ -2058,7 +2244,7 @@ extensions = [{ // Date
2058
2244
  }
2059
2245
  }];
2060
2246
  function typedArrayEncoder(tag, size) {
2061
- if (!isLittleEndianMachine$1 && size > 1)
2247
+ if (!isLittleEndianMachine && size > 1)
2062
2248
  tag -= 4; // the big endian equivalents are 4 less
2063
2249
  return {
2064
2250
  tag: tag,
@@ -2066,7 +2252,7 @@ function typedArrayEncoder(tag, size) {
2066
2252
  let length = typedArray.byteLength;
2067
2253
  let offset = typedArray.byteOffset || 0;
2068
2254
  let buffer = typedArray.buffer || typedArray;
2069
- encode(hasNodeBuffer ? Buffer.from(buffer, offset, length) :
2255
+ encode(hasNodeBuffer ? Buffer$1.from(buffer, offset, length) :
2070
2256
  new Uint8Array(buffer, offset, length));
2071
2257
  }
2072
2258
  }
@@ -2074,26 +2260,26 @@ function typedArrayEncoder(tag, size) {
2074
2260
  function writeBuffer(buffer, makeRoom) {
2075
2261
  let length = buffer.byteLength;
2076
2262
  if (length < 0x18) {
2077
- target[position$1++] = 0x40 + length;
2263
+ target[position++] = 0x40 + length;
2078
2264
  } else if (length < 0x100) {
2079
- target[position$1++] = 0x58;
2080
- target[position$1++] = length;
2265
+ target[position++] = 0x58;
2266
+ target[position++] = length;
2081
2267
  } else if (length < 0x10000) {
2082
- target[position$1++] = 0x59;
2083
- target[position$1++] = length >> 8;
2084
- target[position$1++] = length & 0xff;
2268
+ target[position++] = 0x59;
2269
+ target[position++] = length >> 8;
2270
+ target[position++] = length & 0xff;
2085
2271
  } else {
2086
- target[position$1++] = 0x5a;
2087
- targetView.setUint32(position$1, length);
2088
- position$1 += 4;
2272
+ target[position++] = 0x5a;
2273
+ targetView.setUint32(position, length);
2274
+ position += 4;
2089
2275
  }
2090
- if (position$1 + length >= target.length) {
2091
- makeRoom(position$1 + length);
2276
+ if (position + length >= target.length) {
2277
+ makeRoom(position + length);
2092
2278
  }
2093
2279
  // if it is already a typed array (has an ArrayBuffer), use that, but if it is an ArrayBuffer itself,
2094
2280
  // must wrap it to set it.
2095
- target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position$1);
2096
- position$1 += length;
2281
+ target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position);
2282
+ position += length;
2097
2283
  }
2098
2284
 
2099
2285
  function insertIds(serialized, idsToInsert) {
@@ -2122,27 +2308,30 @@ function insertIds(serialized, idsToInsert) {
2122
2308
  return serialized
2123
2309
  }
2124
2310
  function writeBundles(start, encode) {
2125
- targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start + 1); // the offset to bundle
2126
- let writeStrings = bundledStrings$1;
2127
- bundledStrings$1 = null;
2311
+ targetView.setUint32(bundledStrings.position + start, position - bundledStrings.position - start + 1); // the offset to bundle
2312
+ let writeStrings = bundledStrings;
2313
+ bundledStrings = null;
2128
2314
  encode(writeStrings[0]);
2129
2315
  encode(writeStrings[1]);
2130
2316
  }
2131
2317
 
2132
- function addExtension$1(extension) {
2318
+ function addExtension(extension) {
2133
2319
  if (extension.Class) {
2134
2320
  if (!extension.encode)
2135
2321
  throw new Error('Extension has no encode function')
2136
2322
  extensionClasses.unshift(extension.Class);
2137
2323
  extensions.unshift(extension);
2138
2324
  }
2139
- addExtension(extension);
2325
+ addExtension$1(extension);
2140
2326
  }
2141
2327
  let defaultEncoder = new Encoder({ useRecords: false });
2142
2328
  const encode = defaultEncoder.encode;
2329
+ const encodeAsIterable = defaultEncoder.encodeAsIterable;
2330
+ const encodeAsAsyncIterable = defaultEncoder.encodeAsAsyncIterable;
2143
2331
  const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
2144
2332
  const REUSE_BUFFER_MODE = 512;
2145
2333
  const RESET_BUFFER_MODE = 1024;
2334
+ const THROW_ON_ITERABLE = 2048;
2146
2335
 
2147
2336
  class EncoderStream extends stream.Transform {
2148
2337
  constructor(options) {
@@ -2153,8 +2342,10 @@ class EncoderStream extends stream.Transform {
2153
2342
  options.sequential = true;
2154
2343
  this.encoder = options.encoder || new Encoder(options);
2155
2344
  }
2156
- _transform(value, encoding, callback) {
2157
- this.push(this.encoder.encode(value));
2345
+ async _transform(value, encoding, callback) {
2346
+ for await (let chunk of this.encoder.encodeAsAsyncIterable(value)) {
2347
+ this.push(chunk);
2348
+ }
2158
2349
  callback();
2159
2350
  }
2160
2351
  }
@@ -2310,13 +2501,16 @@ exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
2310
2501
  exports.NEVER = NEVER;
2311
2502
  exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
2312
2503
  exports.Tag = Tag;
2313
- exports.addExtension = addExtension$1;
2504
+ exports.addExtension = addExtension;
2314
2505
  exports.clearSource = clearSource;
2315
2506
  exports.decode = decode;
2316
2507
  exports.decodeIter = decodeIter;
2317
2508
  exports.decodeMultiple = decodeMultiple;
2318
2509
  exports.encode = encode;
2510
+ exports.encodeAsAsyncIterable = encodeAsAsyncIterable;
2511
+ exports.encodeAsIterable = encodeAsIterable;
2319
2512
  exports.encodeIter = encodeIter;
2320
2513
  exports.mapsAsObjects = mapsAsObjects;
2321
2514
  exports.roundFloat32 = roundFloat32;
2322
2515
  exports.useRecords = useRecords;
2516
+ //# sourceMappingURL=node.cjs.map