cbor-x 1.5.0 → 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/index.js CHANGED
@@ -357,13 +357,28 @@
357
357
  return structure.read()
358
358
  }
359
359
  if (token < 0x10000) {
360
- 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)
361
- return recordDefinition(read())
360
+ if (token == RECORD_INLINE_ID) { // we do a special check for this so that we can keep the
361
+ // currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
362
+ let length = readJustLength();
363
+ let id = read();
364
+ let structure = read();
365
+ recordDefinition(id, structure);
366
+ let object = {};
367
+ if (currentDecoder.keyMap) for (let i = 2; i < length; i++) {
368
+ let key = currentDecoder.decodeKey(structure[i - 2]);
369
+ object[safeKey(key)] = read();
370
+ }
371
+ else for (let i = 2; i < length; i++) {
372
+ let key = structure[i - 2];
373
+ object[safeKey(key)] = read();
374
+ }
375
+ return object
376
+ }
362
377
  else if (token == RECORD_DEFINITIONS_ID) {
363
378
  let length = readJustLength();
364
379
  let id = read();
365
380
  for (let i = 2; i < length; i++) {
366
- recordDefinition([id++, read()]);
381
+ recordDefinition(id++, read());
367
382
  }
368
383
  return read()
369
384
  } else if (token == BUNDLED_STRINGS_ID) {
@@ -760,9 +775,8 @@
760
775
  };
761
776
 
762
777
  // the registration of the record definition extension
763
- const recordDefinition = (definition) => {
764
- let id = definition[0] - 0xe000;
765
- let structure = definition[1];
778
+ const recordDefinition = (id, structure) => {
779
+ id = id - 0xe000;
766
780
  let existingStructure = currentStructures[id];
767
781
  if (existingStructure && existingStructure.isShared) {
768
782
  (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
@@ -770,18 +784,18 @@
770
784
  currentStructures[id] = structure;
771
785
 
772
786
  structure.read = createStructureReader(structure);
787
+ };
788
+ currentExtensions[LEGACY_RECORD_INLINE_ID] = (data) => {
789
+ let length = data.length;
790
+ let structure = data[1];
791
+ recordDefinition(data[0], structure);
773
792
  let object = {};
774
- if (currentDecoder.keyMap) for (let i = 2,l = definition.length; i < l; i++) {
775
- let key = currentDecoder.decodeKey(structure[i - 2]);
776
- object[safeKey(key)] = definition[i];
777
- }
778
- else for (let i = 2,l = definition.length; i < l; i++) {
779
- let key = structure[i - 2];
780
- object[safeKey(key)] = definition[i];
781
- }
793
+ for (let i = 2; i < length; i++) {
794
+ let key = structure[i - 2];
795
+ object[safeKey(key)] = data[i];
796
+ }
782
797
  return object
783
798
  };
784
- currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition;
785
799
  currentExtensions[14] = (value) => {
786
800
  if (bundledStrings$1)
787
801
  return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
@@ -1076,10 +1090,10 @@
1076
1090
  textEncoder = new TextEncoder();
1077
1091
  } catch (error) {}
1078
1092
  let extensions, extensionClasses;
1079
- const hasNodeBuffer = typeof Buffer !== 'undefined';
1080
- const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
1081
- const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1082
- const BlobConstructor = typeof Blob === 'undefined' ? {} : Blob;
1093
+ const Buffer$1 = globalThis.Buffer;
1094
+ const hasNodeBuffer = typeof Buffer$1 !== 'undefined';
1095
+ const ByteArrayAllocate = hasNodeBuffer ? Buffer$1.allocUnsafeSlow : Uint8Array;
1096
+ const ByteArray = hasNodeBuffer ? Buffer$1 : Uint8Array;
1083
1097
  const MAX_STRUCTURES = 0x100;
1084
1098
  const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1085
1099
  let throwOnIterable;
@@ -1622,7 +1636,7 @@
1622
1636
  target[position++] = 0xff; // stop-code
1623
1637
  return
1624
1638
  }
1625
- if (value[Symbol.asyncIterator] || constructor === BlobConstructor) {
1639
+ if (value[Symbol.asyncIterator] || isBlob(value)) {
1626
1640
  let error = new Error('Iterable/blob should be serialized as iterator');
1627
1641
  error.iteratorNotHandled = true;
1628
1642
  throw error;
@@ -1856,7 +1870,8 @@
1856
1870
  } else encode(value);
1857
1871
  }
1858
1872
  } else if (constructor === Array) {
1859
- writeArrayHeader(object.length);
1873
+ let length = object.length;
1874
+ writeArrayHeader(length);
1860
1875
  for (let i = 0; i < length; i++) {
1861
1876
  let value = object[i];
1862
1877
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
@@ -1877,7 +1892,7 @@
1877
1892
  } else encode(value);
1878
1893
  }
1879
1894
  target[position++] = 0xff; // stop byte
1880
- } else if (constructor === BlobConstructor){
1895
+ } else if (isBlob(object)){
1881
1896
  writeEntityLength(object.size, 0x40); // encode as binary data
1882
1897
  yield target.subarray(start, position);
1883
1898
  yield object; // directly return blobs, they have to be encoded asynchronously
@@ -1934,7 +1949,7 @@
1934
1949
  let constructor = encodedValue.constructor;
1935
1950
  if (constructor === ByteArray || constructor === Uint8Array)
1936
1951
  yield encodedValue;
1937
- else if (constructor === BlobConstructor) {
1952
+ else if (isBlob(encodedValue)) {
1938
1953
  let reader = encodedValue.stream().getReader();
1939
1954
  let next;
1940
1955
  while (!(next = await reader.read()).done) {
@@ -2029,6 +2044,13 @@
2029
2044
  }
2030
2045
  }
2031
2046
 
2047
+ const BlobConstructor = typeof Blob === 'undefined' ? function(){} : Blob;
2048
+ function isBlob(object) {
2049
+ if (object instanceof BlobConstructor)
2050
+ return true;
2051
+ let tag = object[Symbol.toStringTag];
2052
+ return tag === 'Blob' || tag === 'File';
2053
+ }
2032
2054
  function findRepetitiveStrings(value, packedValues) {
2033
2055
  switch(typeof value) {
2034
2056
  case 'string':
@@ -2187,7 +2209,7 @@
2187
2209
  let length = typedArray.byteLength;
2188
2210
  let offset = typedArray.byteOffset || 0;
2189
2211
  let buffer = typedArray.buffer || typedArray;
2190
- encode(hasNodeBuffer ? Buffer.from(buffer, offset, length) :
2212
+ encode(hasNodeBuffer ? Buffer$1.from(buffer, offset, length) :
2191
2213
  new Uint8Array(buffer, offset, length));
2192
2214
  }
2193
2215
  }