cbor-x 1.5.9 → 1.6.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/test.js CHANGED
@@ -15,6 +15,10 @@
15
15
  const BUNDLED_STRINGS_ID = 0xdff9;
16
16
  const PACKED_REFERENCE_TAG_ID = 6;
17
17
  const STOP_CODE = {};
18
+ let maxArraySize = 112810000; // This is the maximum array size in V8. We would potentially detect and set it higher
19
+ // for JSC, but this is pretty large and should be sufficient for most use cases
20
+ let maxMapSize = 16810000; // JavaScript has a fixed maximum map size of about 16710000, but JS itself enforces this,
21
+ // many keys in an object, so also probably a reasonable choice there.
18
22
  let strings = EMPTY_ARRAY;
19
23
  let stringPosition = 0;
20
24
  let currentDecoder = {};
@@ -291,6 +295,7 @@
291
295
  let array = [];
292
296
  let value, i = 0;
293
297
  while ((value = read()) != STOP_CODE) {
298
+ if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
294
299
  array[i++] = value;
295
300
  }
296
301
  return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
@@ -298,8 +303,19 @@
298
303
  let key;
299
304
  if (currentDecoder.mapsAsObjects) {
300
305
  let object = {};
301
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[safeKey(currentDecoder.decodeKey(key))] = read();
302
- else while ((key = read()) != STOP_CODE) object[safeKey(key)] = read();
306
+ let i = 0;
307
+ if (currentDecoder.keyMap) {
308
+ while((key = read()) != STOP_CODE) {
309
+ if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
310
+ object[safeKey(currentDecoder.decodeKey(key))] = read();
311
+ }
312
+ }
313
+ else {
314
+ while ((key = read()) != STOP_CODE) {
315
+ if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
316
+ object[safeKey(key)] = read();
317
+ }
318
+ }
303
319
  return object
304
320
  } else {
305
321
  if (restoreMapsAsObject) {
@@ -307,8 +323,24 @@
307
323
  restoreMapsAsObject = false;
308
324
  }
309
325
  let map = new Map();
310
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) map.set(currentDecoder.decodeKey(key), read());
311
- else while ((key = read()) != STOP_CODE) map.set(key, read());
326
+ if (currentDecoder.keyMap) {
327
+ let i = 0;
328
+ while((key = read()) != STOP_CODE) {
329
+ if (i++ >= maxMapSize) {
330
+ throw new Error(`Map size exceeds ${maxMapSize}`);
331
+ }
332
+ map.set(currentDecoder.decodeKey(key), read());
333
+ }
334
+ }
335
+ else {
336
+ let i = 0;
337
+ while ((key = read()) != STOP_CODE) {
338
+ if (i++ >= maxMapSize) {
339
+ throw new Error(`Map size exceeds ${maxMapSize}`);
340
+ }
341
+ map.set(key, read());
342
+ }
343
+ }
312
344
  return map
313
345
  }
314
346
  case 7:
@@ -339,12 +371,14 @@
339
371
  }
340
372
  return readFixedString(token)
341
373
  case 4: // array
374
+ if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
342
375
  let array = new Array(token);
343
376
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
344
377
  //else
345
378
  for (let i = 0; i < token; i++) array[i] = read();
346
379
  return array
347
380
  case 5: // map
381
+ if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
348
382
  if (currentDecoder.mapsAsObjects) {
349
383
  let object = {};
350
384
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
@@ -446,6 +480,7 @@
446
480
  }
447
481
  const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
448
482
  function createStructureReader(structure) {
483
+ if (!structure) throw new Error('Structure is required in record definition');
449
484
  function readObject() {
450
485
  // get the array size from the header
451
486
  let length = src[position$1++];
@@ -804,7 +839,7 @@
804
839
  // bigint extension
805
840
  let value = BigInt(0);
806
841
  for (let i = 0, l = buffer.byteLength; i < l; i++) {
807
- value = BigInt(buffer[i]) + value << BigInt(8);
842
+ value = BigInt(buffer[i]) + (value << BigInt(8));
808
843
  }
809
844
  return value
810
845
  };
@@ -1752,7 +1787,19 @@
1752
1787
  target[position++] = 0xfb;
1753
1788
  targetView.setFloat64(position, Number(value));
1754
1789
  } else {
1755
- throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1790
+ if (value >= BigInt(0))
1791
+ target[position++] = 0xc2; // tag 2
1792
+ else {
1793
+ target[position++] = 0xc3; // tag 2
1794
+ value = BigInt(-1) - value;
1795
+ }
1796
+ let bytes = [];
1797
+ while (value) {
1798
+ bytes.push(Number(value & BigInt(0xff)));
1799
+ value >>= BigInt(8);
1800
+ }
1801
+ writeBuffer(new Uint8Array(bytes.reverse()), makeRoom);
1802
+ return;
1756
1803
  }
1757
1804
  }
1758
1805
  position += 8;
@@ -1799,7 +1846,7 @@
1799
1846
  let objectOffset = position - start;
1800
1847
  position += 2;
1801
1848
  let size = 0;
1802
- if (encoder.keyMap) {
1849
+ if (encoder.keyMap) {
1803
1850
  for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1804
1851
  encode(encoder.encodeKey(key));
1805
1852
  encode(object[key]);
@@ -1971,7 +2018,7 @@
1971
2018
  yield* tryEncode(value, iterateProperties, 'element');
1972
2019
  } else encode(value);
1973
2020
  }
1974
- } else if (object[Symbol.iterator]) {
2021
+ } else if (object[Symbol.iterator] && !object.buffer) { // iterator, but exclude typed arrays
1975
2022
  target[position++] = 0x9f; // start indefinite array
1976
2023
  for (let value of object) {
1977
2024
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
@@ -3060,23 +3107,27 @@
3060
3107
  var data = {
3061
3108
  bigintSmall: 352n,
3062
3109
  bigintSmallNegative: -333335252n,
3063
- bigintBig: 2n**64n - 1n, // biggest possible
3110
+ bigintBig: 2n**64n - 1n, // biggest 64-bit possible
3064
3111
  bigintBigNegative: -(2n**63n), // largest negative
3065
3112
  mixedWithNormal: 44,
3066
3113
  };
3067
3114
  var serialized = encode(data);
3068
3115
  var deserialized = decode(serialized);
3069
3116
  assert.deepEqual(deserialized, data);
3070
- var tooBigInt = {
3071
- tooBig: 2n**66n
3117
+ var evenBiggerInt = {
3118
+ big: 2n**66n,
3119
+ bigger: 53285732853728573289573289573289573289583725892358732859532n,
3120
+ negBig: -93025879203578903275903285903285903289502n,
3072
3121
  };
3073
- assert.throws(function(){ serialized = encode(tooBigInt); });
3122
+ var serialized = encode(evenBiggerInt);
3123
+ var deserialized = decode(serialized);
3124
+ assert.deepEqual(deserialized, evenBiggerInt);
3074
3125
  let encoder = new Encoder({
3075
3126
  largeBigIntToFloat: true
3076
3127
  });
3077
- serialized = encoder.encode(tooBigInt);
3128
+ serialized = encoder.encode(evenBiggerInt);
3078
3129
  deserialized = decode(serialized);
3079
- assert.isTrue(deserialized.tooBig > 2n**65n);
3130
+ assert.isTrue(deserialized.bigger > 2n**65n);
3080
3131
  });
3081
3132
 
3082
3133
  test('buffers', function() {