cbor-x 1.5.0 → 1.5.2

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
@@ -361,13 +361,28 @@ function read() {
361
361
  return structure.read()
362
362
  }
363
363
  if (token < 0x10000) {
364
- 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)
365
- 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
+ }
366
381
  else if (token == RECORD_DEFINITIONS_ID) {
367
382
  let length = readJustLength();
368
383
  let id = read();
369
384
  for (let i = 2; i < length; i++) {
370
- recordDefinition([id++, read()]);
385
+ recordDefinition(id++, read());
371
386
  }
372
387
  return read()
373
388
  } else if (token == BUNDLED_STRINGS_ID) {
@@ -799,9 +814,8 @@ currentExtensions[5] = (fraction) => {
799
814
  };
800
815
 
801
816
  // the registration of the record definition extension
802
- const recordDefinition = (definition) => {
803
- let id = definition[0] - 0xe000;
804
- let structure = definition[1];
817
+ const recordDefinition = (id, structure) => {
818
+ id = id - 0xe000;
805
819
  let existingStructure = currentStructures[id];
806
820
  if (existingStructure && existingStructure.isShared) {
807
821
  (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
@@ -809,18 +823,18 @@ const recordDefinition = (definition) => {
809
823
  currentStructures[id] = structure;
810
824
 
811
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);
812
831
  let object = {};
813
- if (currentDecoder.keyMap) for (let i = 2,l = definition.length; i < l; i++) {
814
- let key = currentDecoder.decodeKey(structure[i - 2]);
815
- object[safeKey(key)] = definition[i];
816
- }
817
- else for (let i = 2,l = definition.length; i < l; i++) {
818
- let key = structure[i - 2];
819
- object[safeKey(key)] = definition[i];
820
- }
832
+ for (let i = 2; i < length; i++) {
833
+ let key = structure[i - 2];
834
+ object[safeKey(key)] = data[i];
835
+ }
821
836
  return object
822
837
  };
823
- currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition;
824
838
  currentExtensions[14] = (value) => {
825
839
  if (bundledStrings$1)
826
840
  return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
@@ -970,9 +984,11 @@ for (let i = 0; i < typedArrays.length; i++) {
970
984
  }
971
985
  function registerTypedArray(TypedArray, tag) {
972
986
  let dvMethod = 'get' + TypedArray.name.slice(0, -5);
973
- if (typeof TypedArray !== 'function')
987
+ let bytesPerElement;
988
+ if (typeof TypedArray === 'function')
989
+ bytesPerElement = TypedArray.BYTES_PER_ELEMENT;
990
+ else
974
991
  TypedArray = null;
975
- let bytesPerElement = TypedArray.BYTES_PER_ELEMENT;
976
992
  for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
977
993
  if (!littleEndian && bytesPerElement == 1)
978
994
  continue
@@ -1119,10 +1135,10 @@ try {
1119
1135
  textEncoder = new TextEncoder();
1120
1136
  } catch (error) {}
1121
1137
  let extensions, extensionClasses;
1122
- const hasNodeBuffer = typeof Buffer !== 'undefined';
1123
- const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
1124
- const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1125
- const BlobConstructor = typeof Blob === 'undefined' ? {} : Blob;
1138
+ const Buffer$1 = globalThis.Buffer;
1139
+ const hasNodeBuffer = typeof Buffer$1 !== 'undefined';
1140
+ const ByteArrayAllocate = hasNodeBuffer ? Buffer$1.allocUnsafeSlow : Uint8Array;
1141
+ const ByteArray = hasNodeBuffer ? Buffer$1 : Uint8Array;
1126
1142
  const MAX_STRUCTURES = 0x100;
1127
1143
  const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1128
1144
  let throwOnIterable;
@@ -1510,7 +1526,7 @@ class Encoder extends Decoder {
1510
1526
  }
1511
1527
  position += length;
1512
1528
  } else if (type === 'number') {
1513
- if (value >>> 0 === value) {// positive integer, 32-bit or less
1529
+ if (!this.alwaysUseFloat && value >>> 0 === value) {// positive integer, 32-bit or less
1514
1530
  // positive uint
1515
1531
  if (value < 0x18) {
1516
1532
  target[position++] = value;
@@ -1526,7 +1542,7 @@ class Encoder extends Decoder {
1526
1542
  targetView.setUint32(position, value);
1527
1543
  position += 4;
1528
1544
  }
1529
- } else if (value >> 0 === value) { // negative integer
1545
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
1530
1546
  if (value >= -0x18) {
1531
1547
  target[position++] = 0x1f - value;
1532
1548
  } else if (value >= -0x100) {
@@ -1665,7 +1681,7 @@ class Encoder extends Decoder {
1665
1681
  target[position++] = 0xff; // stop-code
1666
1682
  return
1667
1683
  }
1668
- if (value[Symbol.asyncIterator] || constructor === BlobConstructor) {
1684
+ if (value[Symbol.asyncIterator] || isBlob(value)) {
1669
1685
  let error = new Error('Iterable/blob should be serialized as iterator');
1670
1686
  error.iteratorNotHandled = true;
1671
1687
  throw error;
@@ -1899,7 +1915,8 @@ class Encoder extends Decoder {
1899
1915
  } else encode(value);
1900
1916
  }
1901
1917
  } else if (constructor === Array) {
1902
- writeArrayHeader(object.length);
1918
+ let length = object.length;
1919
+ writeArrayHeader(length);
1903
1920
  for (let i = 0; i < length; i++) {
1904
1921
  let value = object[i];
1905
1922
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
@@ -1920,7 +1937,7 @@ class Encoder extends Decoder {
1920
1937
  } else encode(value);
1921
1938
  }
1922
1939
  target[position++] = 0xff; // stop byte
1923
- } else if (constructor === BlobConstructor){
1940
+ } else if (isBlob(object)){
1924
1941
  writeEntityLength(object.size, 0x40); // encode as binary data
1925
1942
  yield target.subarray(start, position);
1926
1943
  yield object; // directly return blobs, they have to be encoded asynchronously
@@ -1977,7 +1994,7 @@ class Encoder extends Decoder {
1977
1994
  let constructor = encodedValue.constructor;
1978
1995
  if (constructor === ByteArray || constructor === Uint8Array)
1979
1996
  yield encodedValue;
1980
- else if (constructor === BlobConstructor) {
1997
+ else if (isBlob(encodedValue)) {
1981
1998
  let reader = encodedValue.stream().getReader();
1982
1999
  let next;
1983
2000
  while (!(next = await reader.read()).done) {
@@ -2072,6 +2089,13 @@ function writeArrayHeader(length) {
2072
2089
  }
2073
2090
  }
2074
2091
 
2092
+ const BlobConstructor = typeof Blob === 'undefined' ? function(){} : Blob;
2093
+ function isBlob(object) {
2094
+ if (object instanceof BlobConstructor)
2095
+ return true;
2096
+ let tag = object[Symbol.toStringTag];
2097
+ return tag === 'Blob' || tag === 'File';
2098
+ }
2075
2099
  function findRepetitiveStrings(value, packedValues) {
2076
2100
  switch(typeof value) {
2077
2101
  case 'string':
@@ -2230,7 +2254,7 @@ function typedArrayEncoder(tag, size) {
2230
2254
  let length = typedArray.byteLength;
2231
2255
  let offset = typedArray.byteOffset || 0;
2232
2256
  let buffer = typedArray.buffer || typedArray;
2233
- encode(hasNodeBuffer ? Buffer.from(buffer, offset, length) :
2257
+ encode(hasNodeBuffer ? Buffer$1.from(buffer, offset, length) :
2234
2258
  new Uint8Array(buffer, offset, length));
2235
2259
  }
2236
2260
  }