cbor-x 1.6.0 → 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/dist/test.js CHANGED
@@ -267,6 +267,7 @@
267
267
  }
268
268
  token = dataView.getUint32(position$1);
269
269
  position$1 += 4;
270
+ if (majorType === 1) return -1 - token; // can't safely use negation operator here
270
271
  break
271
272
  case 0x1b:
272
273
  if (majorType == 7) {
@@ -281,8 +282,7 @@
281
282
  } else if (currentDecoder.int64AsNumber) {
282
283
  token = dataView.getUint32(position$1) * 0x100000000;
283
284
  token += dataView.getUint32(position$1 + 4);
284
- } else
285
- token = dataView.getBigUint64(position$1);
285
+ } else token = dataView.getBigUint64(position$1);
286
286
  position$1 += 8;
287
287
  break
288
288
  case 0x1f:
@@ -1233,8 +1233,8 @@
1233
1233
  let structures;
1234
1234
  let referenceMap;
1235
1235
  options = options || {};
1236
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
1237
- return target.utf8Write(string, position, maxBytes)
1236
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1237
+ return target.utf8Write(string, position, target.byteLength - position)
1238
1238
  } : (textEncoder && textEncoder.encodeInto) ?
1239
1239
  function(string, position) {
1240
1240
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -1615,7 +1615,7 @@
1615
1615
  targetView.setUint32(position, value);
1616
1616
  position += 4;
1617
1617
  }
1618
- } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
1618
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer, 31-bit or less
1619
1619
  if (value >= -0x18) {
1620
1620
  target[position++] = 0x1f - value;
1621
1621
  } else if (value >= -0x100) {
@@ -1630,6 +1630,11 @@
1630
1630
  targetView.setUint32(position, ~value);
1631
1631
  position += 4;
1632
1632
  }
1633
+ } else if (!this.alwaysUseFloat && value < 0 && value >= -0x100000000 && Math.floor(value) === value) {
1634
+ // negative integer, 32-bit or less
1635
+ target[position++] = 0x3a;
1636
+ targetView.setUint32(position, -1 - value);
1637
+ position += 4;
1633
1638
  } else {
1634
1639
  let useFloat32;
1635
1640
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
@@ -1671,6 +1676,9 @@
1671
1676
  }
1672
1677
  let constructor = value.constructor;
1673
1678
  if (constructor === Object) {
1679
+ if (this.skipFunction === true) {
1680
+ value = Object.fromEntries([...Object.keys(value).filter(x => typeof value[x] !== "function").map(x => [x, value[x]])]);
1681
+ }
1674
1682
  writeObject(value);
1675
1683
  } else if (constructor === Array) {
1676
1684
  length = value.length;
@@ -2595,6 +2603,10 @@
2595
2603
  assert.equal(deserialized, data);
2596
2604
  });
2597
2605
 
2606
+ test('string overflow', function() {
2607
+ assert.throws(() => decode$1(Buffer.from('7a10000000', 'hex')));
2608
+ });
2609
+
2598
2610
  test('encode/decode sample data', function(){
2599
2611
  var data = sampleData;
2600
2612
  var serialized = encode$1(data);
@@ -3082,7 +3094,12 @@
3082
3094
  var deserialized = decode(serialized);
3083
3095
  assert.deepEqual(deserialized, data);
3084
3096
  });
3085
-
3097
+ test('numbers are compact', function(){
3098
+ assert.equal(encode(-256).length, 2);
3099
+ let encoding = encode(-4294967296);
3100
+ assert.equal(encoding.length, 5);
3101
+ assert.equal(decode(encoding), -4294967296);
3102
+ });
3086
3103
  test('encode ArrayBuffer', function() {
3087
3104
  let ua = new Uint8Array([3, 4, 5]);
3088
3105
  let encoded = encode(ua.buffer);
@@ -3181,6 +3198,20 @@
3181
3198
  decodeMultiple(new Uint8Array([1, 2, 3, 4]), value => values.push(value));
3182
3199
  assert.deepEqual(values, [1, 2, 3, 4]);
3183
3200
  });
3201
+ test('skipFunction', () => {
3202
+ var data = {
3203
+ a: 325283295382932843n,
3204
+ f: () => {},
3205
+ };
3206
+ let encoder = new Encoder({
3207
+ int64AsNumber: true,
3208
+ skipFunction: true,
3209
+ });
3210
+ var serialized = encoder.encode(data);
3211
+ var deserialized = encoder.decode(serialized);
3212
+ assert.deepEqual(deserialized.a, 325283295382932843);
3213
+ assert.equal(Object.hasOwn(deserialized, "f"), false);
3214
+ });
3184
3215
  test('bad input', function() {
3185
3216
  let badInput = Buffer.from('7b2273657269616c6e6f223a2265343a30222c226970223a223139322e3136382e312e3335222c226b6579223a226770735f736563726574227d', 'hex');
3186
3217
  assert.throws(function(){ decode(badInput); }); // should throw, not crash