cbor-x 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
3
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = global || self, factory(global.CBOR = {}));
5
- }(this, (function (exports) { 'use strict';
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CBOR = {}));
5
+ })(this, (function (exports) { 'use strict';
6
6
 
7
7
  let decoder;
8
8
  try {
@@ -10,7 +10,7 @@
10
10
  } catch(error) {}
11
11
  let src;
12
12
  let srcEnd;
13
- let position = 0;
13
+ let position$1 = 0;
14
14
  const LEGACY_RECORD_INLINE_ID = 105;
15
15
  const RECORD_DEFINITIONS_ID = 0xdffe;
16
16
  const RECORD_INLINE_ID = 0xdfff; // temporary first-come first-serve tag // proposed tag: 0x7265 // 're'
@@ -22,7 +22,7 @@
22
22
  let srcString;
23
23
  let srcStringStart = 0;
24
24
  let srcStringEnd = 0;
25
- let bundledStrings;
25
+ let bundledStrings$1;
26
26
  let referenceMap;
27
27
  let currentExtensions = [];
28
28
  let currentExtensionRanges = [];
@@ -111,10 +111,10 @@
111
111
  })
112
112
  }
113
113
  srcEnd = end > -1 ? end : source.length;
114
- position = 0;
114
+ position$1 = 0;
115
115
  srcStringEnd = 0;
116
116
  srcString = null;
117
- bundledStrings = null;
117
+ bundledStrings$1 = null;
118
118
  src = source;
119
119
  // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
120
120
  // technique for getting data from a database where it can be copied into an existing buffer instead of creating
@@ -157,8 +157,8 @@
157
157
  if (forEach(value) === false) {
158
158
  return
159
159
  }
160
- while(position < size) {
161
- lastPosition = position;
160
+ while(position$1 < size) {
161
+ lastPosition = position$1;
162
162
  if (forEach(checkedRead()) === false) {
163
163
  return
164
164
  }
@@ -166,8 +166,8 @@
166
166
  }
167
167
  else {
168
168
  values = [ value ];
169
- while(position < size) {
170
- lastPosition = position;
169
+ while(position$1 < size) {
170
+ lastPosition = position$1;
171
171
  values.push(checkedRead());
172
172
  }
173
173
  return values
@@ -185,23 +185,24 @@
185
185
  function checkedRead() {
186
186
  try {
187
187
  let result = read();
188
- if (bundledStrings) {
189
- if (position >= bundledStrings.postBundlePosition) {
188
+ if (bundledStrings$1) {
189
+ if (position$1 >= bundledStrings$1.postBundlePosition) {
190
190
  let error = new Error('Unexpected bundle position');
191
191
  error.incomplete = true;
192
192
  throw error
193
193
  }
194
194
  // bundled strings to skip past
195
- position = bundledStrings.postBundlePosition;
195
+ position$1 = bundledStrings$1.postBundlePosition;
196
+ bundledStrings$1 = null;
196
197
  }
197
198
 
198
- if (position == srcEnd) {
199
+ if (position$1 == srcEnd) {
199
200
  // finished reading this source, cleanup references
200
201
  currentStructures = null;
201
202
  src = null;
202
203
  if (referenceMap)
203
204
  referenceMap = null;
204
- } else if (position > srcEnd) {
205
+ } else if (position$1 > srcEnd) {
205
206
  // over read
206
207
  let error = new Error('Unexpected end of CBOR data');
207
208
  error.incomplete = true;
@@ -221,52 +222,52 @@
221
222
  }
222
223
 
223
224
  function read() {
224
- let token = src[position++];
225
+ let token = src[position$1++];
225
226
  let majorType = token >> 5;
226
227
  token = token & 0x1f;
227
228
  if (token > 0x17) {
228
229
  switch (token) {
229
230
  case 0x18:
230
- token = src[position++];
231
+ token = src[position$1++];
231
232
  break
232
233
  case 0x19:
233
234
  if (majorType == 7) {
234
235
  return getFloat16()
235
236
  }
236
- token = dataView.getUint16(position);
237
- position += 2;
237
+ token = dataView.getUint16(position$1);
238
+ position$1 += 2;
238
239
  break
239
240
  case 0x1a:
240
241
  if (majorType == 7) {
241
- let value = dataView.getFloat32(position);
242
+ let value = dataView.getFloat32(position$1);
242
243
  if (currentDecoder.useFloat32 > 2) {
243
244
  // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
244
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
245
- position += 4;
245
+ let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
246
+ position$1 += 4;
246
247
  return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
247
248
  }
248
- position += 4;
249
+ position$1 += 4;
249
250
  return value
250
251
  }
251
- token = dataView.getUint32(position);
252
- position += 4;
252
+ token = dataView.getUint32(position$1);
253
+ position$1 += 4;
253
254
  break
254
255
  case 0x1b:
255
256
  if (majorType == 7) {
256
- let value = dataView.getFloat64(position);
257
- position += 8;
257
+ let value = dataView.getFloat64(position$1);
258
+ position$1 += 8;
258
259
  return value
259
260
  }
260
261
  if (majorType > 1) {
261
- if (dataView.getUint32(position) > 0)
262
+ if (dataView.getUint32(position$1) > 0)
262
263
  throw new Error('JavaScript does not support arrays, maps, or strings with length over 4294967295')
263
- token = dataView.getUint32(position + 4);
264
+ token = dataView.getUint32(position$1 + 4);
264
265
  } else if (currentDecoder.int64AsNumber) {
265
- token = dataView.getUint32(position) * 0x100000000;
266
- token += dataView.getUint32(position + 4);
266
+ token = dataView.getUint32(position$1) * 0x100000000;
267
+ token += dataView.getUint32(position$1 + 4);
267
268
  } else
268
- token = dataView.getBigUint64(position);
269
- position += 8;
269
+ token = dataView.getBigUint64(position$1);
270
+ position$1 += 8;
270
271
  break
271
272
  case 0x1f:
272
273
  // indefinite length
@@ -315,8 +316,8 @@
315
316
  case 2: // buffer
316
317
  return readBin(token)
317
318
  case 3: // string
318
- if (srcStringEnd >= position) {
319
- return srcString.slice(position - srcStringStart, (position += token) - srcStringStart)
319
+ if (srcStringEnd >= position$1) {
320
+ return srcString.slice(position$1 - srcStringStart, (position$1 += token) - srcStringStart)
320
321
  }
321
322
  if (srcStringEnd == 0 && srcEnd < 140 && token < 32) {
322
323
  // for small blocks, avoiding the overhead of the extract call is helpful
@@ -420,24 +421,24 @@
420
421
  function createStructureReader(structure) {
421
422
  function readObject() {
422
423
  // get the array size from the header
423
- let length = src[position++];
424
+ let length = src[position$1++];
424
425
  //let majorType = token >> 5
425
426
  length = length & 0x1f;
426
427
  if (length > 0x17) {
427
428
  switch (length) {
428
429
  case 0x18:
429
- length = src[position++];
430
+ length = src[position$1++];
430
431
  break
431
432
  case 0x19:
432
- length = dataView.getUint16(position);
433
- position += 2;
433
+ length = dataView.getUint16(position$1);
434
+ position$1 += 2;
434
435
  break
435
436
  case 0x1a:
436
- length = dataView.getUint32(position);
437
- position += 4;
437
+ length = dataView.getUint32(position$1);
438
+ position$1 += 4;
438
439
  break
439
440
  default:
440
- throw new Error('Expected array header, but got ' + src[position - 1])
441
+ throw new Error('Expected array header, but got ' + src[position$1 - 1])
441
442
  }
442
443
  }
443
444
  // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
@@ -484,29 +485,29 @@
484
485
  return result
485
486
  }
486
487
  if (length > 64 && decoder)
487
- return decoder.decode(src.subarray(position, position += length))
488
- const end = position + length;
488
+ return decoder.decode(src.subarray(position$1, position$1 += length))
489
+ const end = position$1 + length;
489
490
  const units = [];
490
491
  result = '';
491
- while (position < end) {
492
- const byte1 = src[position++];
492
+ while (position$1 < end) {
493
+ const byte1 = src[position$1++];
493
494
  if ((byte1 & 0x80) === 0) {
494
495
  // 1 byte
495
496
  units.push(byte1);
496
497
  } else if ((byte1 & 0xe0) === 0xc0) {
497
498
  // 2 bytes
498
- const byte2 = src[position++] & 0x3f;
499
+ const byte2 = src[position$1++] & 0x3f;
499
500
  units.push(((byte1 & 0x1f) << 6) | byte2);
500
501
  } else if ((byte1 & 0xf0) === 0xe0) {
501
502
  // 3 bytes
502
- const byte2 = src[position++] & 0x3f;
503
- const byte3 = src[position++] & 0x3f;
503
+ const byte2 = src[position$1++] & 0x3f;
504
+ const byte3 = src[position$1++] & 0x3f;
504
505
  units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
505
506
  } else if ((byte1 & 0xf8) === 0xf0) {
506
507
  // 4 bytes
507
- const byte2 = src[position++] & 0x3f;
508
- const byte3 = src[position++] & 0x3f;
509
- const byte4 = src[position++] & 0x3f;
508
+ const byte2 = src[position$1++] & 0x3f;
509
+ const byte3 = src[position$1++] & 0x3f;
510
+ const byte4 = src[position$1++] & 0x3f;
510
511
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
511
512
  if (unit > 0xffff) {
512
513
  unit -= 0x10000;
@@ -532,12 +533,12 @@
532
533
  }
533
534
  let fromCharCode = String.fromCharCode;
534
535
  function longStringInJS(length) {
535
- let start = position;
536
+ let start = position$1;
536
537
  let bytes = new Array(length);
537
538
  for (let i = 0; i < length; i++) {
538
- const byte = src[position++];
539
+ const byte = src[position$1++];
539
540
  if ((byte & 0x80) > 0) {
540
- position = start;
541
+ position$1 = start;
541
542
  return
542
543
  }
543
544
  bytes[i] = byte;
@@ -550,131 +551,131 @@
550
551
  if (length === 0)
551
552
  return ''
552
553
  else {
553
- let a = src[position++];
554
+ let a = src[position$1++];
554
555
  if ((a & 0x80) > 1) {
555
- position -= 1;
556
+ position$1 -= 1;
556
557
  return
557
558
  }
558
559
  return fromCharCode(a)
559
560
  }
560
561
  } else {
561
- let a = src[position++];
562
- let b = src[position++];
562
+ let a = src[position$1++];
563
+ let b = src[position$1++];
563
564
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
564
- position -= 2;
565
+ position$1 -= 2;
565
566
  return
566
567
  }
567
568
  if (length < 3)
568
569
  return fromCharCode(a, b)
569
- let c = src[position++];
570
+ let c = src[position$1++];
570
571
  if ((c & 0x80) > 0) {
571
- position -= 3;
572
+ position$1 -= 3;
572
573
  return
573
574
  }
574
575
  return fromCharCode(a, b, c)
575
576
  }
576
577
  } else {
577
- let a = src[position++];
578
- let b = src[position++];
579
- let c = src[position++];
580
- let d = src[position++];
578
+ let a = src[position$1++];
579
+ let b = src[position$1++];
580
+ let c = src[position$1++];
581
+ let d = src[position$1++];
581
582
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
582
- position -= 4;
583
+ position$1 -= 4;
583
584
  return
584
585
  }
585
586
  if (length < 6) {
586
587
  if (length === 4)
587
588
  return fromCharCode(a, b, c, d)
588
589
  else {
589
- let e = src[position++];
590
+ let e = src[position$1++];
590
591
  if ((e & 0x80) > 0) {
591
- position -= 5;
592
+ position$1 -= 5;
592
593
  return
593
594
  }
594
595
  return fromCharCode(a, b, c, d, e)
595
596
  }
596
597
  } else if (length < 8) {
597
- let e = src[position++];
598
- let f = src[position++];
598
+ let e = src[position$1++];
599
+ let f = src[position$1++];
599
600
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
600
- position -= 6;
601
+ position$1 -= 6;
601
602
  return
602
603
  }
603
604
  if (length < 7)
604
605
  return fromCharCode(a, b, c, d, e, f)
605
- let g = src[position++];
606
+ let g = src[position$1++];
606
607
  if ((g & 0x80) > 0) {
607
- position -= 7;
608
+ position$1 -= 7;
608
609
  return
609
610
  }
610
611
  return fromCharCode(a, b, c, d, e, f, g)
611
612
  } else {
612
- let e = src[position++];
613
- let f = src[position++];
614
- let g = src[position++];
615
- let h = src[position++];
613
+ let e = src[position$1++];
614
+ let f = src[position$1++];
615
+ let g = src[position$1++];
616
+ let h = src[position$1++];
616
617
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
617
- position -= 8;
618
+ position$1 -= 8;
618
619
  return
619
620
  }
620
621
  if (length < 10) {
621
622
  if (length === 8)
622
623
  return fromCharCode(a, b, c, d, e, f, g, h)
623
624
  else {
624
- let i = src[position++];
625
+ let i = src[position$1++];
625
626
  if ((i & 0x80) > 0) {
626
- position -= 9;
627
+ position$1 -= 9;
627
628
  return
628
629
  }
629
630
  return fromCharCode(a, b, c, d, e, f, g, h, i)
630
631
  }
631
632
  } else if (length < 12) {
632
- let i = src[position++];
633
- let j = src[position++];
633
+ let i = src[position$1++];
634
+ let j = src[position$1++];
634
635
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
635
- position -= 10;
636
+ position$1 -= 10;
636
637
  return
637
638
  }
638
639
  if (length < 11)
639
640
  return fromCharCode(a, b, c, d, e, f, g, h, i, j)
640
- let k = src[position++];
641
+ let k = src[position$1++];
641
642
  if ((k & 0x80) > 0) {
642
- position -= 11;
643
+ position$1 -= 11;
643
644
  return
644
645
  }
645
646
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
646
647
  } else {
647
- let i = src[position++];
648
- let j = src[position++];
649
- let k = src[position++];
650
- let l = src[position++];
648
+ let i = src[position$1++];
649
+ let j = src[position$1++];
650
+ let k = src[position$1++];
651
+ let l = src[position$1++];
651
652
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
652
- position -= 12;
653
+ position$1 -= 12;
653
654
  return
654
655
  }
655
656
  if (length < 14) {
656
657
  if (length === 12)
657
658
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
658
659
  else {
659
- let m = src[position++];
660
+ let m = src[position$1++];
660
661
  if ((m & 0x80) > 0) {
661
- position -= 13;
662
+ position$1 -= 13;
662
663
  return
663
664
  }
664
665
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
665
666
  }
666
667
  } else {
667
- let m = src[position++];
668
- let n = src[position++];
668
+ let m = src[position$1++];
669
+ let n = src[position$1++];
669
670
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
670
- position -= 14;
671
+ position$1 -= 14;
671
672
  return
672
673
  }
673
674
  if (length < 15)
674
675
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
675
- let o = src[position++];
676
+ let o = src[position$1++];
676
677
  if ((o & 0x80) > 0) {
677
- position -= 15;
678
+ position$1 -= 15;
678
679
  return
679
680
  }
680
681
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
@@ -687,14 +688,14 @@
687
688
  function readBin(length) {
688
689
  return currentDecoder.copyBuffers ?
689
690
  // specifically use the copying slice (not the node one)
690
- Uint8Array.prototype.slice.call(src, position, position += length) :
691
- src.subarray(position, position += length)
691
+ Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
692
+ src.subarray(position$1, position$1 += length)
692
693
  }
693
694
  let f32Array = new Float32Array(1);
694
695
  let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
695
696
  function getFloat16() {
696
- let byte0 = src[position++];
697
- let byte1 = src[position++];
697
+ let byte0 = src[position$1++];
698
+ let byte1 = src[position$1++];
698
699
  let exponent = (byte0 & 0x7f) >> 2;
699
700
  if (exponent === 0x1f) { // specials
700
701
  if (byte1 || (byte0 & 3))
@@ -716,7 +717,7 @@
716
717
  return f32Array[0];
717
718
  }
718
719
 
719
- let keyCache = new Array(4096);
720
+ new Array(4096);
720
721
 
721
722
  class Tag {
722
723
  constructor(value, tag) {
@@ -782,13 +783,13 @@
782
783
  };
783
784
  currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition;
784
785
  currentExtensions[14] = (value) => {
785
- if (bundledStrings)
786
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 += value)
786
+ if (bundledStrings$1)
787
+ return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
787
788
  return new Tag(value, 14)
788
789
  };
789
790
  currentExtensions[15] = (value) => {
790
- if (bundledStrings)
791
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
791
+ if (bundledStrings$1)
792
+ return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
792
793
  return new Tag(value, 15)
793
794
  };
794
795
  let glbl = { Error, RegExp };
@@ -796,7 +797,7 @@
796
797
  return (glbl[data[0]] || Error)(data[1], data[2])
797
798
  };
798
799
  const packedTable = (read) => {
799
- if (src[position++] != 0x84)
800
+ if (src[position$1++] != 0x84)
800
801
  throw new Error('Packed values structure must be followed by a 4 element array')
801
802
  let newPackedValues = read(); // packed values
802
803
  packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues;
@@ -818,18 +819,23 @@
818
819
  return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
819
820
  throw new Error('No support for non-integer packed references yet')
820
821
  };
821
- currentExtensions[25] = (id) => {
822
- return stringRefs[id]
823
- };
824
- currentExtensions[256] = (read) => {
825
- stringRefs = [];
826
- try {
827
- return read()
828
- } finally {
829
- stringRefs = null;
830
- }
831
- };
832
- currentExtensions[256].handlesRead = true;
822
+
823
+ // The following code is an incomplete implementation of http://cbor.schmorp.de/stringref
824
+ // the real thing would need to implemennt more logic to populate the stringRefs table and
825
+ // maintain a stack of stringRef "namespaces".
826
+ //
827
+ // currentExtensions[25] = (id) => {
828
+ // return stringRefs[id]
829
+ // }
830
+ // currentExtensions[256] = (read) => {
831
+ // stringRefs = []
832
+ // try {
833
+ // return read()
834
+ // } finally {
835
+ // stringRefs = null
836
+ // }
837
+ // }
838
+ // currentExtensions[256].handlesRead = true
833
839
 
834
840
  currentExtensions[28] = (read) => {
835
841
  // shareable http://cbor.schmorp.de/value-sharing (for structured clones)
@@ -838,7 +844,7 @@
838
844
  referenceMap.id = 0;
839
845
  }
840
846
  let id = referenceMap.id++;
841
- let token = src[position];
847
+ let token = src[position$1];
842
848
  let target;
843
849
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
844
850
  // ahead past references to record structure definitions
@@ -915,7 +921,7 @@
915
921
  return input
916
922
  });
917
923
 
918
- const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
924
+ const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
919
925
  const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
920
926
  typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
921
927
  typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
@@ -932,7 +938,7 @@
932
938
  if (!littleEndian && bytesPerElement == 1)
933
939
  continue
934
940
  let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : 3;
935
- currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine) ? (buffer) => {
941
+ currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
936
942
  if (!TypedArray)
937
943
  throw new Error('Could not find typed array for code ' + tag)
938
944
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
@@ -954,36 +960,36 @@
954
960
 
955
961
  function readBundleExt() {
956
962
  let length = readJustLength();
957
- let bundlePosition = position + read();
963
+ let bundlePosition = position$1 + read();
958
964
  for (let i = 2; i < length; i++) {
959
965
  // skip past bundles that were already read
960
966
  let bundleLength = readJustLength(); // this will increment position, so must add to position afterwards
961
- position += bundleLength;
967
+ position$1 += bundleLength;
962
968
  }
963
- let dataPosition = position;
964
- position = bundlePosition;
965
- bundledStrings = [readStringJS(readJustLength()), readStringJS(readJustLength())];
966
- bundledStrings.position0 = 0;
967
- bundledStrings.position1 = 0;
968
- bundledStrings.postBundlePosition = position;
969
- position = dataPosition;
969
+ let dataPosition = position$1;
970
+ position$1 = bundlePosition;
971
+ bundledStrings$1 = [readStringJS(readJustLength()), readStringJS(readJustLength())];
972
+ bundledStrings$1.position0 = 0;
973
+ bundledStrings$1.position1 = 0;
974
+ bundledStrings$1.postBundlePosition = position$1;
975
+ position$1 = dataPosition;
970
976
  return read()
971
977
  }
972
978
 
973
979
  function readJustLength() {
974
- let token = src[position++] & 0x1f;
980
+ let token = src[position$1++] & 0x1f;
975
981
  if (token > 0x17) {
976
982
  switch (token) {
977
983
  case 0x18:
978
- token = src[position++];
984
+ token = src[position$1++];
979
985
  break
980
986
  case 0x19:
981
- token = dataView.getUint16(position);
982
- position += 2;
987
+ token = dataView.getUint16(position$1);
988
+ position$1 += 2;
983
989
  break
984
990
  case 0x1a:
985
- token = dataView.getUint32(position);
986
- position += 4;
991
+ token = dataView.getUint32(position$1);
992
+ position$1 += 4;
987
993
  break
988
994
  }
989
995
  }
@@ -1009,12 +1015,12 @@
1009
1015
 
1010
1016
  function saveState(callback) {
1011
1017
  let savedSrcEnd = srcEnd;
1012
- let savedPosition = position;
1018
+ let savedPosition = position$1;
1013
1019
  let savedSrcStringStart = srcStringStart;
1014
1020
  let savedSrcStringEnd = srcStringEnd;
1015
1021
  let savedSrcString = srcString;
1016
1022
  let savedReferenceMap = referenceMap;
1017
- let savedBundledStrings = bundledStrings;
1023
+ let savedBundledStrings = bundledStrings$1;
1018
1024
 
1019
1025
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1020
1026
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1023,12 +1029,12 @@
1023
1029
  let savedSequentialMode = sequentialMode;
1024
1030
  let value = callback();
1025
1031
  srcEnd = savedSrcEnd;
1026
- position = savedPosition;
1032
+ position$1 = savedPosition;
1027
1033
  srcStringStart = savedSrcStringStart;
1028
1034
  srcStringEnd = savedSrcStringEnd;
1029
1035
  srcString = savedSrcString;
1030
1036
  referenceMap = savedReferenceMap;
1031
- bundledStrings = savedBundledStrings;
1037
+ bundledStrings$1 = savedBundledStrings;
1032
1038
  src = savedSrc;
1033
1039
  sequentialMode = savedSequentialMode;
1034
1040
  currentStructures = savedStructures;
@@ -1042,7 +1048,7 @@
1042
1048
  currentStructures = null;
1043
1049
  }
1044
1050
 
1045
- function addExtension(extension) {
1051
+ function addExtension$1(extension) {
1046
1052
  currentExtensions[extension.tag] = extension.decode;
1047
1053
  }
1048
1054
 
@@ -1073,13 +1079,15 @@
1073
1079
  const hasNodeBuffer = typeof Buffer !== 'undefined';
1074
1080
  const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
1075
1081
  const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1082
+ const BlobConstructor = typeof Blob === 'undefined' ? {} : Blob;
1076
1083
  const MAX_STRUCTURES = 0x100;
1077
1084
  const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1085
+ let throwOnIterable;
1078
1086
  let target;
1079
1087
  let targetView;
1080
- let position$1 = 0;
1088
+ let position = 0;
1081
1089
  let safeEnd;
1082
- let bundledStrings$1 = null;
1090
+ let bundledStrings = null;
1083
1091
  const MAX_BUNDLE_SIZE = 0xf000;
1084
1092
  const hasNonLatin = /[\u0080-\uFFFF]/;
1085
1093
  const RECORD_SYMBOL = Symbol('record-id');
@@ -1148,28 +1156,28 @@
1148
1156
  if (!target) {
1149
1157
  target = new ByteArrayAllocate(8192);
1150
1158
  targetView = new DataView(target.buffer, 0, 8192);
1151
- position$1 = 0;
1159
+ position = 0;
1152
1160
  }
1153
1161
  safeEnd = target.length - 10;
1154
- if (safeEnd - position$1 < 0x800) {
1162
+ if (safeEnd - position < 0x800) {
1155
1163
  // don't start too close to the end,
1156
1164
  target = new ByteArrayAllocate(target.length);
1157
1165
  targetView = new DataView(target.buffer, 0, target.length);
1158
1166
  safeEnd = target.length - 10;
1159
- position$1 = 0;
1167
+ position = 0;
1160
1168
  } else if (encodeOptions === REUSE_BUFFER_MODE)
1161
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1162
- start = position$1;
1169
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1170
+ start = position;
1163
1171
  if (encoder.useSelfDescribedHeader) {
1164
- targetView.setUint32(position$1, 0xd9d9f700); // tag two byte, then self-descriptive tag
1165
- position$1 += 3;
1172
+ targetView.setUint32(position, 0xd9d9f700); // tag two byte, then self-descriptive tag
1173
+ position += 3;
1166
1174
  }
1167
1175
  referenceMap = encoder.structuredClone ? new Map() : null;
1168
1176
  if (encoder.bundleStrings && typeof value !== 'string') {
1169
- bundledStrings$1 = [];
1170
- bundledStrings$1.size = Infinity; // force a new bundle start on first string
1177
+ bundledStrings = [];
1178
+ bundledStrings.size = Infinity; // force a new bundle start on first string
1171
1179
  } else
1172
- bundledStrings$1 = null;
1180
+ bundledStrings = null;
1173
1181
 
1174
1182
  sharedStructures = encoder.structures;
1175
1183
  if (sharedStructures) {
@@ -1225,8 +1233,8 @@
1225
1233
  packedValues.samplingPackedValues = samplingPackedValues;
1226
1234
  findRepetitiveStrings(value, packedValues);
1227
1235
  if (packedValues.values.length > 0) {
1228
- target[position$1++] = 0xd8; // one-byte tag
1229
- target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1236
+ target[position++] = 0xd8; // one-byte tag
1237
+ target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1230
1238
  writeArrayHeader(4);
1231
1239
  let valuesArray = packedValues.values;
1232
1240
  encode(valuesArray);
@@ -1238,27 +1246,30 @@
1238
1246
  }
1239
1247
  }
1240
1248
  }
1249
+ throwOnIterable = encodeOptions & THROW_ON_ITERABLE;
1241
1250
  try {
1251
+ if (throwOnIterable)
1252
+ return;
1242
1253
  encode(value);
1243
- if (bundledStrings$1) {
1254
+ if (bundledStrings) {
1244
1255
  writeBundles(start, encode);
1245
1256
  }
1246
- encoder.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1257
+ encoder.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1247
1258
  if (referenceMap && referenceMap.idsToInsert) {
1248
- position$1 += referenceMap.idsToInsert.length * 2;
1249
- if (position$1 > safeEnd)
1250
- makeRoom(position$1);
1251
- encoder.offset = position$1;
1252
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1259
+ position += referenceMap.idsToInsert.length * 2;
1260
+ if (position > safeEnd)
1261
+ makeRoom(position);
1262
+ encoder.offset = position;
1263
+ let serialized = insertIds(target.subarray(start, position), referenceMap.idsToInsert);
1253
1264
  referenceMap = null;
1254
1265
  return serialized
1255
1266
  }
1256
1267
  if (encodeOptions & REUSE_BUFFER_MODE) {
1257
1268
  target.start = start;
1258
- target.end = position$1;
1269
+ target.end = position;
1259
1270
  return target
1260
1271
  }
1261
- return target.subarray(start, position$1) // position can change if we call encode again in saveShared, so we get the buffer now
1272
+ return target.subarray(start, position) // position can change if we call encode again in saveShared, so we get the buffer now
1262
1273
  } finally {
1263
1274
  if (sharedStructures) {
1264
1275
  if (serializationsSinceTransitionRebuild < 10)
@@ -1285,13 +1296,13 @@
1285
1296
  encoder.structures = encoder.structures.slice(0, maxSharedStructures);
1286
1297
  }
1287
1298
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1288
- let returnBuffer = target.subarray(start, position$1);
1299
+ let returnBuffer = target.subarray(start, position);
1289
1300
  if (encoder.updateSharedData() === false)
1290
1301
  return encoder.encode(value) // re-encode if it fails
1291
1302
  return returnBuffer
1292
1303
  }
1293
1304
  if (encodeOptions & RESET_BUFFER_MODE)
1294
- position$1 = start;
1305
+ position = start;
1295
1306
  }
1296
1307
  };
1297
1308
  this.findCommonStringsToPack = () => {
@@ -1315,8 +1326,8 @@
1315
1326
  }
1316
1327
  };
1317
1328
  const encode = (value) => {
1318
- if (position$1 > safeEnd)
1319
- target = makeRoom(position$1);
1329
+ if (position > safeEnd)
1330
+ target = makeRoom(position);
1320
1331
 
1321
1332
  var type = typeof value;
1322
1333
  var length;
@@ -1325,9 +1336,9 @@
1325
1336
  let packedPosition = packedObjectMap[value];
1326
1337
  if (packedPosition >= 0) {
1327
1338
  if (packedPosition < 16)
1328
- target[position$1++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1339
+ target[position++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1329
1340
  else {
1330
- target[position$1++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1341
+ target[position++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1331
1342
  if (packedPosition & 1)
1332
1343
  encode((15 - packedPosition) >> 1);
1333
1344
  else
@@ -1359,30 +1370,30 @@
1359
1370
  }
1360
1371
  }
1361
1372
  let strLength = value.length;
1362
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x400) {
1363
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1373
+ if (bundledStrings && strLength >= 4 && strLength < 0x400) {
1374
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1364
1375
  let extStart;
1365
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1366
- if (position$1 + maxBytes > safeEnd)
1367
- target = makeRoom(position$1 + maxBytes);
1368
- target[position$1++] = 0xd9; // tag 16-bit
1369
- target[position$1++] = 0xdf; // tag 0xdff9
1370
- target[position$1++] = 0xf9;
1376
+ let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1377
+ if (position + maxBytes > safeEnd)
1378
+ target = makeRoom(position + maxBytes);
1379
+ target[position++] = 0xd9; // tag 16-bit
1380
+ target[position++] = 0xdf; // tag 0xdff9
1381
+ target[position++] = 0xf9;
1371
1382
  // TODO: If we only have one bundle with any string data, only write one string bundle
1372
- target[position$1++] = bundledStrings$1.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
1373
- target[position$1++] = 0x1a; // 32-bit unsigned int
1374
- extStart = position$1 - start;
1375
- position$1 += 4; // reserve for writing bundle reference
1376
- if (bundledStrings$1.position) {
1383
+ target[position++] = bundledStrings.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
1384
+ target[position++] = 0x1a; // 32-bit unsigned int
1385
+ extStart = position - start;
1386
+ position += 4; // reserve for writing bundle reference
1387
+ if (bundledStrings.position) {
1377
1388
  writeBundles(start, encode); // write the last bundles
1378
1389
  }
1379
- bundledStrings$1 = ['', '']; // create new ones
1380
- bundledStrings$1.size = 0;
1381
- bundledStrings$1.position = extStart;
1390
+ bundledStrings = ['', '']; // create new ones
1391
+ bundledStrings.size = 0;
1392
+ bundledStrings.position = extStart;
1382
1393
  }
1383
1394
  let twoByte = hasNonLatin.test(value);
1384
- bundledStrings$1[twoByte ? 0 : 1] += value;
1385
- target[position$1++] = twoByte ? 0xce : 0xcf;
1395
+ bundledStrings[twoByte ? 0 : 1] += value;
1396
+ target[position++] = twoByte ? 0xce : 0xcf;
1386
1397
  encode(strLength);
1387
1398
  return
1388
1399
  }
@@ -1398,11 +1409,11 @@
1398
1409
  headerSize = 5;
1399
1410
  }
1400
1411
  let maxBytes = strLength * 3;
1401
- if (position$1 + maxBytes > safeEnd)
1402
- target = makeRoom(position$1 + maxBytes);
1412
+ if (position + maxBytes > safeEnd)
1413
+ target = makeRoom(position + maxBytes);
1403
1414
 
1404
1415
  if (strLength < 0x40 || !encodeUtf8) {
1405
- let i, c1, c2, strPosition = position$1 + headerSize;
1416
+ let i, c1, c2, strPosition = position + headerSize;
1406
1417
  for (i = 0; i < strLength; i++) {
1407
1418
  c1 = value.charCodeAt(i);
1408
1419
  if (c1 < 0x80) {
@@ -1426,105 +1437,105 @@
1426
1437
  target[strPosition++] = c1 & 0x3f | 0x80;
1427
1438
  }
1428
1439
  }
1429
- length = strPosition - position$1 - headerSize;
1440
+ length = strPosition - position - headerSize;
1430
1441
  } else {
1431
- length = encodeUtf8(value, position$1 + headerSize, maxBytes);
1442
+ length = encodeUtf8(value, position + headerSize, maxBytes);
1432
1443
  }
1433
1444
 
1434
1445
  if (length < 0x18) {
1435
- target[position$1++] = 0x60 | length;
1446
+ target[position++] = 0x60 | length;
1436
1447
  } else if (length < 0x100) {
1437
1448
  if (headerSize < 2) {
1438
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1449
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1439
1450
  }
1440
- target[position$1++] = 0x78;
1441
- target[position$1++] = length;
1451
+ target[position++] = 0x78;
1452
+ target[position++] = length;
1442
1453
  } else if (length < 0x10000) {
1443
1454
  if (headerSize < 3) {
1444
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1455
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1445
1456
  }
1446
- target[position$1++] = 0x79;
1447
- target[position$1++] = length >> 8;
1448
- target[position$1++] = length & 0xff;
1457
+ target[position++] = 0x79;
1458
+ target[position++] = length >> 8;
1459
+ target[position++] = length & 0xff;
1449
1460
  } else {
1450
1461
  if (headerSize < 5) {
1451
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1462
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1452
1463
  }
1453
- target[position$1++] = 0x7a;
1454
- targetView.setUint32(position$1, length);
1455
- position$1 += 4;
1464
+ target[position++] = 0x7a;
1465
+ targetView.setUint32(position, length);
1466
+ position += 4;
1456
1467
  }
1457
- position$1 += length;
1468
+ position += length;
1458
1469
  } else if (type === 'number') {
1459
1470
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1460
1471
  // positive uint
1461
1472
  if (value < 0x18) {
1462
- target[position$1++] = value;
1473
+ target[position++] = value;
1463
1474
  } else if (value < 0x100) {
1464
- target[position$1++] = 0x18;
1465
- target[position$1++] = value;
1475
+ target[position++] = 0x18;
1476
+ target[position++] = value;
1466
1477
  } else if (value < 0x10000) {
1467
- target[position$1++] = 0x19;
1468
- target[position$1++] = value >> 8;
1469
- target[position$1++] = value & 0xff;
1478
+ target[position++] = 0x19;
1479
+ target[position++] = value >> 8;
1480
+ target[position++] = value & 0xff;
1470
1481
  } else {
1471
- target[position$1++] = 0x1a;
1472
- targetView.setUint32(position$1, value);
1473
- position$1 += 4;
1482
+ target[position++] = 0x1a;
1483
+ targetView.setUint32(position, value);
1484
+ position += 4;
1474
1485
  }
1475
1486
  } else if (value >> 0 === value) { // negative integer
1476
1487
  if (value >= -0x18) {
1477
- target[position$1++] = 0x1f - value;
1488
+ target[position++] = 0x1f - value;
1478
1489
  } else if (value >= -0x100) {
1479
- target[position$1++] = 0x38;
1480
- target[position$1++] = ~value;
1490
+ target[position++] = 0x38;
1491
+ target[position++] = ~value;
1481
1492
  } else if (value >= -0x10000) {
1482
- target[position$1++] = 0x39;
1483
- targetView.setUint16(position$1, ~value);
1484
- position$1 += 2;
1493
+ target[position++] = 0x39;
1494
+ targetView.setUint16(position, ~value);
1495
+ position += 2;
1485
1496
  } else {
1486
- target[position$1++] = 0x3a;
1487
- targetView.setUint32(position$1, ~value);
1488
- position$1 += 4;
1497
+ target[position++] = 0x3a;
1498
+ targetView.setUint32(position, ~value);
1499
+ position += 4;
1489
1500
  }
1490
1501
  } else {
1491
1502
  let useFloat32;
1492
1503
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1493
- target[position$1++] = 0xfa;
1494
- targetView.setFloat32(position$1, value);
1504
+ target[position++] = 0xfa;
1505
+ targetView.setFloat32(position, value);
1495
1506
  let xShifted;
1496
1507
  if (useFloat32 < 4 ||
1497
1508
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1498
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1499
- position$1 += 4;
1509
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1510
+ position += 4;
1500
1511
  return
1501
1512
  } else
1502
- position$1--; // move back into position for writing a double
1513
+ position--; // move back into position for writing a double
1503
1514
  }
1504
- target[position$1++] = 0xfb;
1505
- targetView.setFloat64(position$1, value);
1506
- position$1 += 8;
1515
+ target[position++] = 0xfb;
1516
+ targetView.setFloat64(position, value);
1517
+ position += 8;
1507
1518
  }
1508
1519
  } else if (type === 'object') {
1509
1520
  if (!value)
1510
- target[position$1++] = 0xf6;
1521
+ target[position++] = 0xf6;
1511
1522
  else {
1512
1523
  if (referenceMap) {
1513
1524
  let referee = referenceMap.get(value);
1514
1525
  if (referee) {
1515
- target[position$1++] = 0xd8;
1516
- target[position$1++] = 29; // http://cbor.schmorp.de/value-sharing
1517
- target[position$1++] = 0x19; // 16-bit uint
1526
+ target[position++] = 0xd8;
1527
+ target[position++] = 29; // http://cbor.schmorp.de/value-sharing
1528
+ target[position++] = 0x19; // 16-bit uint
1518
1529
  if (!referee.references) {
1519
1530
  let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1520
1531
  referee.references = [];
1521
1532
  idsToInsert.push(referee);
1522
1533
  }
1523
- referee.references.push(position$1 - start);
1524
- position$1 += 2; // TODO: also support 32-bit
1534
+ referee.references.push(position - start);
1535
+ position += 2; // TODO: also support 32-bit
1525
1536
  return
1526
1537
  } else
1527
- referenceMap.set(value, { offset: position$1 - start });
1538
+ referenceMap.set(value, { offset: position - start });
1528
1539
  }
1529
1540
  let constructor = value.constructor;
1530
1541
  if (constructor === Object) {
@@ -1532,7 +1543,7 @@
1532
1543
  } else if (constructor === Array) {
1533
1544
  length = value.length;
1534
1545
  if (length < 0x18) {
1535
- target[position$1++] = 0x80 | length;
1546
+ target[position++] = 0x80 | length;
1536
1547
  } else {
1537
1548
  writeArrayHeader(length);
1538
1549
  }
@@ -1542,24 +1553,24 @@
1542
1553
  } else if (constructor === Map) {
1543
1554
  if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
1544
1555
  // use Tag 259 (https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec--explicit-maps.md) for maps if the user wants it that way
1545
- target[position$1++] = 0xd9;
1546
- target[position$1++] = 1;
1547
- target[position$1++] = 3;
1556
+ target[position++] = 0xd9;
1557
+ target[position++] = 1;
1558
+ target[position++] = 3;
1548
1559
  }
1549
1560
  length = value.size;
1550
1561
  if (length < 0x18) {
1551
- target[position$1++] = 0xa0 | length;
1562
+ target[position++] = 0xa0 | length;
1552
1563
  } else if (length < 0x100) {
1553
- target[position$1++] = 0xb8;
1554
- target[position$1++] = length;
1564
+ target[position++] = 0xb8;
1565
+ target[position++] = length;
1555
1566
  } else if (length < 0x10000) {
1556
- target[position$1++] = 0xb9;
1557
- target[position$1++] = length >> 8;
1558
- target[position$1++] = length & 0xff;
1567
+ target[position++] = 0xb9;
1568
+ target[position++] = length >> 8;
1569
+ target[position++] = length & 0xff;
1559
1570
  } else {
1560
- target[position$1++] = 0xba;
1561
- targetView.setUint32(position$1, length);
1562
- position$1 += 4;
1571
+ target[position++] = 0xba;
1572
+ targetView.setUint32(position, length);
1573
+ position += 4;
1563
1574
  }
1564
1575
  if (encoder.keyMap) {
1565
1576
  for (let [ key, entryValue ] of value) {
@@ -1581,58 +1592,68 @@
1581
1592
  if (tag == undefined)
1582
1593
  tag = extension.getTag && extension.getTag.call(this, value);
1583
1594
  if (tag < 0x18) {
1584
- target[position$1++] = 0xc0 | tag;
1595
+ target[position++] = 0xc0 | tag;
1585
1596
  } else if (tag < 0x100) {
1586
- target[position$1++] = 0xd8;
1587
- target[position$1++] = tag;
1597
+ target[position++] = 0xd8;
1598
+ target[position++] = tag;
1588
1599
  } else if (tag < 0x10000) {
1589
- target[position$1++] = 0xd9;
1590
- target[position$1++] = tag >> 8;
1591
- target[position$1++] = tag & 0xff;
1600
+ target[position++] = 0xd9;
1601
+ target[position++] = tag >> 8;
1602
+ target[position++] = tag & 0xff;
1592
1603
  } else if (tag > -1) {
1593
- target[position$1++] = 0xda;
1594
- targetView.setUint32(position$1, tag);
1595
- position$1 += 4;
1604
+ target[position++] = 0xda;
1605
+ targetView.setUint32(position, tag);
1606
+ position += 4;
1596
1607
  } // else undefined, don't write tag
1597
1608
  extension.encode.call(this, value, encode, makeRoom);
1598
1609
  return
1599
1610
  }
1600
1611
  }
1601
1612
  if (value[Symbol.iterator]) {
1602
- target[position$1++] = 0x9f; // indefinite length array
1613
+ if (throwOnIterable) {
1614
+ let error = new Error('Iterable should be serialized as iterator');
1615
+ error.iteratorNotHandled = true;
1616
+ throw error;
1617
+ }
1618
+ target[position++] = 0x9f; // indefinite length array
1603
1619
  for (let entry of value) {
1604
1620
  encode(entry);
1605
1621
  }
1606
- target[position$1++] = 0xff; // stop-code
1622
+ target[position++] = 0xff; // stop-code
1607
1623
  return
1608
1624
  }
1625
+ if (value[Symbol.asyncIterator] || constructor === BlobConstructor) {
1626
+ let error = new Error('Iterable/blob should be serialized as iterator');
1627
+ error.iteratorNotHandled = true;
1628
+ throw error;
1629
+ }
1609
1630
  // no extension found, write as object
1610
1631
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1611
1632
  }
1612
1633
  }
1613
1634
  } else if (type === 'boolean') {
1614
- target[position$1++] = value ? 0xf5 : 0xf4;
1635
+ target[position++] = value ? 0xf5 : 0xf4;
1615
1636
  } else if (type === 'bigint') {
1616
1637
  if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
1617
1638
  // use an unsigned int as long as it fits
1618
- target[position$1++] = 0x1b;
1619
- targetView.setBigUint64(position$1, value);
1639
+ target[position++] = 0x1b;
1640
+ targetView.setBigUint64(position, value);
1620
1641
  } else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
1621
1642
  // if we can fit an unsigned int, use that
1622
- target[position$1++] = 0x3b;
1623
- targetView.setBigUint64(position$1, -value - BigInt(1));
1643
+ target[position++] = 0x3b;
1644
+ targetView.setBigUint64(position, -value - BigInt(1));
1624
1645
  } else {
1625
1646
  // overflow
1626
1647
  if (this.largeBigIntToFloat) {
1627
- target[position$1++] = 0xfb;
1628
- targetView.setFloat64(position$1, Number(value));
1648
+ target[position++] = 0xfb;
1649
+ targetView.setFloat64(position, Number(value));
1629
1650
  } else {
1630
1651
  throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1631
1652
  }
1632
1653
  }
1633
- position$1 += 8;
1654
+ position += 8;
1634
1655
  } else if (type === 'undefined') {
1635
- target[position$1++] = 0xf7;
1656
+ target[position++] = 0xf7;
1636
1657
  } else {
1637
1658
  throw new Error('Unknown type: ' + type)
1638
1659
  }
@@ -1644,18 +1665,18 @@
1644
1665
  let vals = Object.values(object);
1645
1666
  let length = keys.length;
1646
1667
  if (length < 0x18) {
1647
- target[position$1++] = 0xa0 | length;
1668
+ target[position++] = 0xa0 | length;
1648
1669
  } else if (length < 0x100) {
1649
- target[position$1++] = 0xb8;
1650
- target[position$1++] = length;
1670
+ target[position++] = 0xb8;
1671
+ target[position++] = length;
1651
1672
  } else if (length < 0x10000) {
1652
- target[position$1++] = 0xb9;
1653
- target[position$1++] = length >> 8;
1654
- target[position$1++] = length & 0xff;
1673
+ target[position++] = 0xb9;
1674
+ target[position++] = length >> 8;
1675
+ target[position++] = length & 0xff;
1655
1676
  } else {
1656
- target[position$1++] = 0xba;
1657
- targetView.setUint32(position$1, length);
1658
- position$1 += 4;
1677
+ target[position++] = 0xba;
1678
+ targetView.setUint32(position, length);
1679
+ position += 4;
1659
1680
  }
1660
1681
  if (encoder.keyMap) {
1661
1682
  for (let i = 0; i < length; i++) {
@@ -1670,9 +1691,9 @@
1670
1691
  }
1671
1692
  } :
1672
1693
  (object, safePrototype) => {
1673
- target[position$1++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
1674
- let objectOffset = position$1 - start;
1675
- position$1 += 2;
1694
+ target[position++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
1695
+ let objectOffset = position - start;
1696
+ position += 2;
1676
1697
  let size = 0;
1677
1698
  if (encoder.keyMap) {
1678
1699
  for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
@@ -1725,9 +1746,9 @@
1725
1746
  let recordId = transition[RECORD_SYMBOL];
1726
1747
  if (recordId !== undefined) {
1727
1748
  recordId &= 0xffff;
1728
- target[position$1++] = 0xd9;
1729
- target[position$1++] = (recordId >> 8) | 0xe0;
1730
- target[position$1++] = recordId & 0xff;
1749
+ target[position++] = 0xd9;
1750
+ target[position++] = (recordId >> 8) | 0xe0;
1751
+ target[position++] = recordId & 0xff;
1731
1752
  } else {
1732
1753
  if (!keys)
1733
1754
  keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
@@ -1745,9 +1766,9 @@
1745
1766
  }
1746
1767
  structures[recordId] = keys;
1747
1768
  if (recordId < maxSharedStructures) {
1748
- target[position$1++] = 0xd9;
1749
- target[position$1++] = (recordId >> 8) | 0xe0;
1750
- target[position$1++] = recordId & 0xff;
1769
+ target[position++] = 0xd9;
1770
+ target[position++] = (recordId >> 8) | 0xe0;
1771
+ target[position++] = recordId & 0xff;
1751
1772
  transition = structures.transitions;
1752
1773
  for (let i = 0; i < length; i++) {
1753
1774
  if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
@@ -1758,8 +1779,8 @@
1758
1779
  hasSharedUpdate = true;
1759
1780
  } else {
1760
1781
  transition[RECORD_SYMBOL] = recordId;
1761
- targetView.setUint32(position$1, 0xd9dfff00); // tag two byte, then record definition id
1762
- position$1 += 3;
1782
+ targetView.setUint32(position, 0xd9dfff00); // tag two byte, then record definition id
1783
+ position += 3;
1763
1784
  if (newTransitions)
1764
1785
  transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
1765
1786
  // record the removal of the id, we can maintain our shared structure
@@ -1769,15 +1790,19 @@
1769
1790
  writeArrayHeader(length + 2);
1770
1791
  encode(0xe000 + recordId);
1771
1792
  encode(keys);
1772
- for (let v of Object.values(object)) encode(v);
1793
+ if (safePrototype === null) return; // special exit for iterator
1794
+ for (let key in object)
1795
+ if (safePrototype || object.hasOwnProperty(key))
1796
+ encode(object[key]);
1773
1797
  return
1774
1798
  }
1775
1799
  }
1776
1800
  if (length < 0x18) { // write the array header
1777
- target[position$1++] = 0x80 | length;
1801
+ target[position++] = 0x80 | length;
1778
1802
  } else {
1779
1803
  writeArrayHeader(length);
1780
1804
  }
1805
+ if (safePrototype === null) return; // special exit for iterator
1781
1806
  for (let key in object)
1782
1807
  if (safePrototype || object.hasOwnProperty(key))
1783
1808
  encode(object[key]);
@@ -1798,17 +1823,141 @@
1798
1823
  target.copy(newBuffer, 0, start, end);
1799
1824
  else
1800
1825
  newBuffer.set(target.slice(start, end));
1801
- position$1 -= start;
1826
+ position -= start;
1802
1827
  start = 0;
1803
1828
  safeEnd = newBuffer.length - 10;
1804
1829
  return target = newBuffer
1805
1830
  };
1831
+ let chunkThreshold = 100;
1832
+ let continuedChunkThreshold = 1000;
1833
+ this.encodeAsIterable = function(value, options) {
1834
+ return startEncoding(value, options, encodeObjectAsIterable);
1835
+ };
1836
+ this.encodeAsAsyncIterable = function(value, options) {
1837
+ return startEncoding(value, options, encodeObjectAsAsyncIterable);
1838
+ };
1839
+
1840
+ function* encodeObjectAsIterable(object, iterateProperties, finalIterable) {
1841
+ let constructor = object.constructor;
1842
+ if (constructor === Object) {
1843
+ let useRecords = encoder.useRecords !== false;
1844
+ if (useRecords)
1845
+ writeObject(object, null); // write the record identifier
1846
+ else
1847
+ writeEntityLength(Object.keys(object).length, 0xa0);
1848
+ for (let key in object) {
1849
+ let value = object[key];
1850
+ if (!useRecords) encode(key);
1851
+ if (value && typeof value === 'object') {
1852
+ if (iterateProperties[key])
1853
+ yield* encodeObjectAsIterable(value, iterateProperties[key]);
1854
+ else
1855
+ yield* tryEncode(value, iterateProperties, key);
1856
+ } else encode(value);
1857
+ }
1858
+ } else if (constructor === Array) {
1859
+ writeArrayHeader(object.length);
1860
+ for (let i = 0; i < length; i++) {
1861
+ let value = object[i];
1862
+ if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
1863
+ if (iterateProperties.element)
1864
+ yield* encodeObjectAsIterable(value, iterateProperties.element);
1865
+ else
1866
+ yield* tryEncode(value, iterateProperties, 'element');
1867
+ } else encode(value);
1868
+ }
1869
+ } else if (object[Symbol.iterator]) {
1870
+ target[position++] = 0x9f; // start indefinite array
1871
+ for (let value of object) {
1872
+ if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
1873
+ if (iterateProperties.element)
1874
+ yield* encodeObjectAsIterable(value, iterateProperties.element);
1875
+ else
1876
+ yield* tryEncode(value, iterateProperties, 'element');
1877
+ } else encode(value);
1878
+ }
1879
+ target[position++] = 0xff; // stop byte
1880
+ } else if (constructor === BlobConstructor){
1881
+ writeEntityLength(object.size, 0x40); // encode as binary data
1882
+ yield target.subarray(start, position);
1883
+ yield object; // directly return blobs, they have to be encoded asynchronously
1884
+ restartEncoding();
1885
+ } else if (object[Symbol.asyncIterator]) {
1886
+ target[position++] = 0x9f; // start indefinite array
1887
+ yield target.subarray(start, position);
1888
+ yield object; // directly return async iterators, they have to be encoded asynchronously
1889
+ restartEncoding();
1890
+ target[position++] = 0xff; // stop byte
1891
+ } else {
1892
+ encode(object);
1893
+ }
1894
+ if (finalIterable && position > start) yield target.subarray(start, position);
1895
+ else if (position - start > chunkThreshold) {
1896
+ yield target.subarray(start, position);
1897
+ restartEncoding();
1898
+ }
1899
+ }
1900
+ function* tryEncode(value, iterateProperties, key) {
1901
+ let restart = position - start;
1902
+ try {
1903
+ encode(value);
1904
+ if (position - start > chunkThreshold) {
1905
+ yield target.subarray(start, position);
1906
+ restartEncoding();
1907
+ }
1908
+ } catch (error) {
1909
+ if (error.iteratorNotHandled) {
1910
+ iterateProperties[key] = {};
1911
+ position = start + restart; // restart our position so we don't have partial data from last encode
1912
+ yield* encodeObjectAsIterable.call(this, value, iterateProperties[key]);
1913
+ } else throw error;
1914
+ }
1915
+ }
1916
+ function restartEncoding() {
1917
+ chunkThreshold = continuedChunkThreshold;
1918
+ encoder.encode(null, THROW_ON_ITERABLE); // restart encoding
1919
+ }
1920
+ function startEncoding(value, options, encodeIterable) {
1921
+ if (options && options.chunkThreshold) // explicitly specified chunk sizes
1922
+ chunkThreshold = continuedChunkThreshold = options.chunkThreshold;
1923
+ else // we start with a smaller threshold to get initial bytes sent quickly
1924
+ chunkThreshold = 100;
1925
+ if (value && typeof value === 'object') {
1926
+ encoder.encode(null, THROW_ON_ITERABLE); // start encoding
1927
+ return encodeIterable(value, encoder.iterateProperties || (encoder.iterateProperties = {}), true);
1928
+ }
1929
+ return [encoder.encode(value)];
1930
+ }
1931
+
1932
+ async function* encodeObjectAsAsyncIterable(value, iterateProperties) {
1933
+ for (let encodedValue of encodeObjectAsIterable(value, iterateProperties, true)) {
1934
+ let constructor = encodedValue.constructor;
1935
+ if (constructor === ByteArray || constructor === Uint8Array)
1936
+ yield encodedValue;
1937
+ else if (constructor === BlobConstructor) {
1938
+ let reader = encodedValue.stream().getReader();
1939
+ let next;
1940
+ while (!(next = await reader.read()).done) {
1941
+ yield next.value;
1942
+ }
1943
+ } else if (encodedValue[Symbol.asyncIterator]) {
1944
+ for await (let asyncValue of encodedValue) {
1945
+ restartEncoding();
1946
+ if (asyncValue)
1947
+ yield* encodeObjectAsAsyncIterable(asyncValue, iterateProperties.async || (iterateProperties.async = {}));
1948
+ else yield encoder.encode(asyncValue);
1949
+ }
1950
+ } else {
1951
+ yield encodedValue;
1952
+ }
1953
+ }
1954
+ }
1806
1955
  }
1807
1956
  useBuffer(buffer) {
1808
1957
  // this means we are finished using our own buffer and we can write over it safely
1809
1958
  target = buffer;
1810
1959
  targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1811
- position$1 = 0;
1960
+ position = 0;
1812
1961
  }
1813
1962
  clearSharedData() {
1814
1963
  if (this.structures)
@@ -1838,6 +1987,23 @@
1838
1987
  return saveResults
1839
1988
  }
1840
1989
  }
1990
+ function writeEntityLength(length, majorValue) {
1991
+ if (length < 0x18)
1992
+ target[position++] = majorValue | length;
1993
+ else if (length < 0x100) {
1994
+ target[position++] = majorValue | 0x18;
1995
+ target[position++] = length;
1996
+ } else if (length < 0x10000) {
1997
+ target[position++] = majorValue | 0x19;
1998
+ target[position++] = length >> 8;
1999
+ target[position++] = length & 0xff;
2000
+ } else {
2001
+ target[position++] = majorValue | 0x1a;
2002
+ targetView.setUint32(position, length);
2003
+ position += 4;
2004
+ }
2005
+
2006
+ }
1841
2007
  class SharedData {
1842
2008
  constructor(structures, values, version) {
1843
2009
  this.structures = structures;
@@ -1848,18 +2014,18 @@
1848
2014
 
1849
2015
  function writeArrayHeader(length) {
1850
2016
  if (length < 0x18)
1851
- target[position$1++] = 0x80 | length;
2017
+ target[position++] = 0x80 | length;
1852
2018
  else if (length < 0x100) {
1853
- target[position$1++] = 0x98;
1854
- target[position$1++] = length;
2019
+ target[position++] = 0x98;
2020
+ target[position++] = length;
1855
2021
  } else if (length < 0x10000) {
1856
- target[position$1++] = 0x99;
1857
- target[position$1++] = length >> 8;
1858
- target[position$1++] = length & 0xff;
2022
+ target[position++] = 0x99;
2023
+ target[position++] = length >> 8;
2024
+ target[position++] = length & 0xff;
1859
2025
  } else {
1860
- target[position$1++] = 0x9a;
1861
- targetView.setUint32(position$1, length);
1862
- position$1 += 4;
2026
+ target[position++] = 0x9a;
2027
+ targetView.setUint32(position, length);
2028
+ position += 4;
1863
2029
  }
1864
2030
  }
1865
2031
 
@@ -1912,7 +2078,7 @@
1912
2078
  case 'function': console.log(value);
1913
2079
  }
1914
2080
  }
1915
- const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
2081
+ const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
1916
2082
  extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
1917
2083
  Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
1918
2084
  typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
@@ -1926,14 +2092,14 @@
1926
2092
  let seconds = date.getTime() / 1000;
1927
2093
  if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
1928
2094
  // Timestamp 32
1929
- target[position$1++] = 0x1a;
1930
- targetView.setUint32(position$1, seconds);
1931
- position$1 += 4;
2095
+ target[position++] = 0x1a;
2096
+ targetView.setUint32(position, seconds);
2097
+ position += 4;
1932
2098
  } else {
1933
2099
  // Timestamp float64
1934
- target[position$1++] = 0xfb;
1935
- targetView.setFloat64(position$1, seconds);
1936
- position$1 += 8;
2100
+ target[position++] = 0xfb;
2101
+ targetView.setFloat64(position, seconds);
2102
+ position += 8;
1937
2103
  }
1938
2104
  }
1939
2105
  }, { // Set
@@ -1989,8 +2155,8 @@
1989
2155
  let packedValues = sharedData.packedValues || [];
1990
2156
  let sharedStructures = sharedData.structures || [];
1991
2157
  if (packedValues.values.length > 0) {
1992
- target[position$1++] = 0xd8; // one-byte tag
1993
- target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
2158
+ target[position++] = 0xd8; // one-byte tag
2159
+ target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
1994
2160
  writeArrayHeader(4);
1995
2161
  let valuesArray = packedValues.values;
1996
2162
  encode(valuesArray);
@@ -2002,8 +2168,8 @@
2002
2168
  }
2003
2169
  }
2004
2170
  if (sharedStructures) {
2005
- targetView.setUint32(position$1, 0xd9dffe00);
2006
- position$1 += 3;
2171
+ targetView.setUint32(position, 0xd9dffe00);
2172
+ position += 3;
2007
2173
  let definitions = sharedStructures.slice(0);
2008
2174
  definitions.unshift(0xe000);
2009
2175
  definitions.push(new Tag(sharedData.version, 0x53687264));
@@ -2013,7 +2179,7 @@
2013
2179
  }
2014
2180
  }];
2015
2181
  function typedArrayEncoder(tag, size) {
2016
- if (!isLittleEndianMachine$1 && size > 1)
2182
+ if (!isLittleEndianMachine && size > 1)
2017
2183
  tag -= 4; // the big endian equivalents are 4 less
2018
2184
  return {
2019
2185
  tag: tag,
@@ -2029,26 +2195,26 @@
2029
2195
  function writeBuffer(buffer, makeRoom) {
2030
2196
  let length = buffer.byteLength;
2031
2197
  if (length < 0x18) {
2032
- target[position$1++] = 0x40 + length;
2198
+ target[position++] = 0x40 + length;
2033
2199
  } else if (length < 0x100) {
2034
- target[position$1++] = 0x58;
2035
- target[position$1++] = length;
2200
+ target[position++] = 0x58;
2201
+ target[position++] = length;
2036
2202
  } else if (length < 0x10000) {
2037
- target[position$1++] = 0x59;
2038
- target[position$1++] = length >> 8;
2039
- target[position$1++] = length & 0xff;
2203
+ target[position++] = 0x59;
2204
+ target[position++] = length >> 8;
2205
+ target[position++] = length & 0xff;
2040
2206
  } else {
2041
- target[position$1++] = 0x5a;
2042
- targetView.setUint32(position$1, length);
2043
- position$1 += 4;
2207
+ target[position++] = 0x5a;
2208
+ targetView.setUint32(position, length);
2209
+ position += 4;
2044
2210
  }
2045
- if (position$1 + length >= target.length) {
2046
- makeRoom(position$1 + length);
2211
+ if (position + length >= target.length) {
2212
+ makeRoom(position + length);
2047
2213
  }
2048
2214
  // if it is already a typed array (has an ArrayBuffer), use that, but if it is an ArrayBuffer itself,
2049
2215
  // must wrap it to set it.
2050
- target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position$1);
2051
- position$1 += length;
2216
+ target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position);
2217
+ position += length;
2052
2218
  }
2053
2219
 
2054
2220
  function insertIds(serialized, idsToInsert) {
@@ -2077,27 +2243,30 @@
2077
2243
  return serialized
2078
2244
  }
2079
2245
  function writeBundles(start, encode) {
2080
- targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start + 1); // the offset to bundle
2081
- let writeStrings = bundledStrings$1;
2082
- bundledStrings$1 = null;
2246
+ targetView.setUint32(bundledStrings.position + start, position - bundledStrings.position - start + 1); // the offset to bundle
2247
+ let writeStrings = bundledStrings;
2248
+ bundledStrings = null;
2083
2249
  encode(writeStrings[0]);
2084
2250
  encode(writeStrings[1]);
2085
2251
  }
2086
2252
 
2087
- function addExtension$1(extension) {
2253
+ function addExtension(extension) {
2088
2254
  if (extension.Class) {
2089
2255
  if (!extension.encode)
2090
2256
  throw new Error('Extension has no encode function')
2091
2257
  extensionClasses.unshift(extension.Class);
2092
2258
  extensions.unshift(extension);
2093
2259
  }
2094
- addExtension(extension);
2260
+ addExtension$1(extension);
2095
2261
  }
2096
2262
  let defaultEncoder = new Encoder({ useRecords: false });
2097
2263
  const encode = defaultEncoder.encode;
2264
+ const encodeAsIterable = defaultEncoder.encodeAsIterable;
2265
+ const encodeAsAsyncIterable = defaultEncoder.encodeAsAsyncIterable;
2098
2266
  const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
2099
2267
  const REUSE_BUFFER_MODE = 512;
2100
2268
  const RESET_BUFFER_MODE = 1024;
2269
+ const THROW_ON_ITERABLE = 2048;
2101
2270
 
2102
2271
  /**
2103
2272
  * Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer
@@ -2191,16 +2360,17 @@
2191
2360
  exports.NEVER = NEVER;
2192
2361
  exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
2193
2362
  exports.Tag = Tag;
2194
- exports.addExtension = addExtension$1;
2363
+ exports.addExtension = addExtension;
2195
2364
  exports.clearSource = clearSource;
2196
2365
  exports.decode = decode;
2197
2366
  exports.decodeIter = decodeIter;
2198
2367
  exports.decodeMultiple = decodeMultiple;
2199
2368
  exports.encode = encode;
2369
+ exports.encodeAsAsyncIterable = encodeAsAsyncIterable;
2370
+ exports.encodeAsIterable = encodeAsIterable;
2200
2371
  exports.encodeIter = encodeIter;
2201
2372
  exports.isNativeAccelerationEnabled = isNativeAccelerationEnabled;
2202
2373
  exports.roundFloat32 = roundFloat32;
2203
2374
 
2204
- Object.defineProperty(exports, '__esModule', { value: true });
2205
-
2206
- })));
2375
+ }));
2376
+ //# sourceMappingURL=index.js.map