cbor-x 1.5.9 → 1.6.3

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/encode.js CHANGED
@@ -31,8 +31,8 @@ export class Encoder extends Decoder {
31
31
  let structures
32
32
  let referenceMap
33
33
  options = options || {}
34
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
35
- return target.utf8Write(string, position, maxBytes)
34
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
35
+ return target.utf8Write(string, position, target.byteLength - position)
36
36
  } : (textEncoder && textEncoder.encodeInto) ?
37
37
  function(string, position) {
38
38
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -413,7 +413,7 @@ export class Encoder extends Decoder {
413
413
  targetView.setUint32(position, value)
414
414
  position += 4
415
415
  }
416
- } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
416
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer, 31-bit or less
417
417
  if (value >= -0x18) {
418
418
  target[position++] = 0x1f - value
419
419
  } else if (value >= -0x100) {
@@ -428,6 +428,11 @@ export class Encoder extends Decoder {
428
428
  targetView.setUint32(position, ~value)
429
429
  position += 4
430
430
  }
431
+ } else if (!this.alwaysUseFloat && value < 0 && value >= -0x100000000 && Math.floor(value) === value) {
432
+ // negative integer, 32-bit or less
433
+ target[position++] = 0x3a
434
+ targetView.setUint32(position, -1 - value)
435
+ position += 4
431
436
  } else {
432
437
  let useFloat32
433
438
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
@@ -469,6 +474,9 @@ export class Encoder extends Decoder {
469
474
  }
470
475
  let constructor = value.constructor
471
476
  if (constructor === Object) {
477
+ if (this.skipFunction === true) {
478
+ value = Object.fromEntries([...Object.keys(value).filter(x => typeof value[x] !== "function").map(x => [x, value[x]])]);
479
+ }
472
480
  writeObject(value)
473
481
  } else if (constructor === Array) {
474
482
  length = value.length
@@ -585,7 +593,19 @@ export class Encoder extends Decoder {
585
593
  target[position++] = 0xfb
586
594
  targetView.setFloat64(position, Number(value))
587
595
  } else {
588
- throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
596
+ if (value >= BigInt(0))
597
+ target[position++] = 0xc2 // tag 2
598
+ else {
599
+ target[position++] = 0xc3 // tag 2
600
+ value = BigInt(-1) - value;
601
+ }
602
+ let bytes = [];
603
+ while (value) {
604
+ bytes.push(Number(value & BigInt(0xff)));
605
+ value >>= BigInt(8);
606
+ }
607
+ writeBuffer(new Uint8Array(bytes.reverse()), makeRoom);
608
+ return;
589
609
  }
590
610
  }
591
611
  position += 8
@@ -633,7 +653,7 @@ export class Encoder extends Decoder {
633
653
  let objectOffset = position - start
634
654
  position += 2
635
655
  let size = 0
636
- if (encoder.keyMap) {
656
+ if (encoder.keyMap) {
637
657
  for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
638
658
  encode(encoder.encodeKey(key))
639
659
  encode(object[key])
@@ -805,7 +825,7 @@ export class Encoder extends Decoder {
805
825
  yield* tryEncode(value, iterateProperties, 'element');
806
826
  } else encode(value);
807
827
  }
808
- } else if (object[Symbol.iterator]) {
828
+ } else if (object[Symbol.iterator] && !object.buffer) { // iterator, but exclude typed arrays
809
829
  target[position++] = 0x9f; // start indefinite array
810
830
  for (let value of object) {
811
831
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
package/index.d.ts CHANGED
@@ -4,7 +4,11 @@ export enum FLOAT32_OPTIONS {
4
4
  DECIMAL_ROUND = 3,
5
5
  DECIMAL_FIT = 4
6
6
  }
7
-
7
+ export interface SizeLimitOptions {
8
+ maxArraySize: number;
9
+ maxMapSize: number;
10
+ maxObjectSize: number;
11
+ }
8
12
  export interface Options {
9
13
  alwaysUseFloat?: boolean
10
14
  useFloat32?: FLOAT32_OPTIONS
@@ -43,6 +47,7 @@ export class Decoder {
43
47
  decode(messagePack: Buffer | Uint8Array): any
44
48
  decodeMultiple(messagePack: Buffer | Uint8Array, forEach?: (value: any) => any): [] | void
45
49
  }
50
+ export function setMaxLimits(options: SizeLimitOptions): void
46
51
  export function decode(messagePack: Buffer | Uint8Array): any
47
52
  export function decodeMultiple(messagePack: Buffer | Uint8Array, forEach?: (value: any) => any): [] | void
48
53
  export function addExtension<T, R>(extension: Extension<T, R>): void
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { Encoder, addExtension, encode, encodeAsIterable, encodeAsAsyncIterable, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './encode.js'
2
- export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
2
+ export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled, setSizeLimits } from './decode.js'
3
3
  export { decodeIter, encodeIter } from './iterators.js'
package/node-index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { Encoder, addExtension, encode, encodeAsIterable, encodeAsAsyncIterable, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './encode.js'
2
- export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
2
+ export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled, setSizeLimits } from './decode.js'
3
3
  export { EncoderStream, DecoderStream } from './stream.js'
4
4
  export { decodeIter, encodeIter } from './iterators.js'
5
5
  export const useRecords = false
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cbor-x",
3
3
  "author": "Kris Zyp",
4
- "version": "1.5.9",
4
+ "version": "1.6.3",
5
5
  "description": "Ultra-fast and conformant CBOR (RFC 8949) implementation with support for numerous tag extensions including records and structured cloning",
6
6
  "license": "MIT",
7
7
  "types": "./index.d.ts",
@@ -75,7 +75,7 @@
75
75
  "node:buffer": false
76
76
  },
77
77
  "optionalDependencies": {
78
- "cbor-extract": "^2.2.0"
78
+ "cbor-extract": "^2.2.1"
79
79
  },
80
80
  "devDependencies": {
81
81
  "@rollup/plugin-json": "^5.0.1",