msgpackr 1.9.2 → 1.9.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
@@ -984,10 +984,10 @@
984
984
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
985
985
  currentExtensions[0].noBuffer = true;
986
986
 
987
- let glbl = typeof globalThis === 'object' ? globalThis : window;
987
+ let errors = { Error, TypeError, ReferenceError };
988
988
  currentExtensions[0x65] = () => {
989
989
  let data = read();
990
- return (glbl[data[0]] || Error)(data[1])
990
+ return (errors[data[0]] || Error)(data[1])
991
991
  };
992
992
 
993
993
  currentExtensions[0x69] = (data) => {
@@ -1025,6 +1025,7 @@
1025
1025
 
1026
1026
  const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
1027
1027
 
1028
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
1028
1029
  currentExtensions[0x74] = (data) => {
1029
1030
  let typeCode = data[0];
1030
1031
  let typedArrayName = typedArrays[typeCode];
@@ -1529,7 +1530,7 @@
1529
1530
  targetView.setFloat64(position, value);
1530
1531
  position += 8;
1531
1532
  }
1532
- } else if (type === 'object') {
1533
+ } else if (type === 'object' || type === 'function') {
1533
1534
  if (!value)
1534
1535
  target[position++] = 0xc0;
1535
1536
  else {
@@ -1636,6 +1637,11 @@
1636
1637
  } else {
1637
1638
  if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1638
1639
  return pack(value.toJSON());
1640
+
1641
+ // if there is a writeFunction, use it, otherwise just encode as undefined
1642
+ if (type === 'function')
1643
+ return pack(this.writeFunction && this.writeFunction(value));
1644
+
1639
1645
  // no extension found, write as object
1640
1646
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1641
1647
  }
@@ -1670,14 +1676,12 @@
1670
1676
  target[position++] = 0;
1671
1677
  target[position++] = 0;
1672
1678
  }
1673
- } else if (type === 'function') {
1674
- pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
1675
1679
  } else {
1676
1680
  throw new Error('Unknown type: ' + type)
1677
1681
  }
1678
1682
  };
1679
1683
 
1680
- const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
1684
+ const writePlainObject = this.variableMapSize ? (object) => {
1681
1685
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1682
1686
  let keys = Object.keys(object);
1683
1687
  let length = keys.length;
@@ -1712,7 +1716,9 @@
1712
1716
  }
1713
1717
  target[objectOffset++ + start] = size >> 8;
1714
1718
  target[objectOffset + start] = size & 0xff;
1715
- } :
1719
+ };
1720
+
1721
+ const writeRecord = this.useRecords === false ? writePlainObject :
1716
1722
  (options.progressiveRecords && !useTwoByteRecords) ? // this is about 2% faster for highly stable structures, since it only requires one for-in loop (but much more expensive when new structure needs to be written)
1717
1723
  (object, safePrototype) => {
1718
1724
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
@@ -1784,6 +1790,14 @@
1784
1790
  if (safePrototype || object.hasOwnProperty(key))
1785
1791
  pack(object[key]);
1786
1792
  };
1793
+
1794
+ // craete reference to useRecords if useRecords is a function
1795
+ const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1796
+
1797
+ const writeObject = checkUseRecords ? (object, safePrototype) => {
1798
+ checkUseRecords(object) ? writeRecord(object,safePrototype) : writePlainObject(object,safePrototype);
1799
+ } : writeRecord;
1800
+
1787
1801
  const makeRoom = (end) => {
1788
1802
  let newSize;
1789
1803
  if (end > 0x1000000) {
@@ -3159,8 +3173,30 @@
3159
3173
  var deserialized = packr.unpack(serialized);
3160
3174
  assert.deepEqual(deserialized, data);
3161
3175
  });
3162
-
3163
3176
  }
3177
+
3178
+ test('pack/unpack sample data with useRecords function', function () {
3179
+ var data = [
3180
+ {id: 1, type: 1, labels: {a: 1, b: 2}},
3181
+ {id: 2, type: 1, labels: {b: 1, c: 2}},
3182
+ {id: 3, type: 1, labels: {d: 1, e: 2}}
3183
+ ];
3184
+
3185
+ var alternatives = [
3186
+ {useRecords: false}, // 88 bytes
3187
+ {useRecords: true}, // 58 bytes
3188
+ {mapsAsObjects: true, useRecords: (v)=>!!v.id}, // 55 bytes
3189
+ {mapsAsObjects: true, variableMapSize: true, useRecords: (v)=>!!v.id} // 49 bytes
3190
+ ];
3191
+
3192
+ for(let o of alternatives) {
3193
+ let packr = new Packr(o);
3194
+ var serialized = packr.pack(data);
3195
+ var deserialized = packr.unpack(serialized);
3196
+ assert.deepEqual(deserialized, data);
3197
+ }
3198
+ });
3199
+
3164
3200
  test('mapAsEmptyObject combination', function () {
3165
3201
  const msgpackr = new Packr({ useRecords: false, encodeUndefinedAsNil: true, variableMapSize: true, mapAsEmptyObject: true, setAsEmptyObject: true });
3166
3202
 
@@ -3337,7 +3373,6 @@
3337
3373
  assert.deepEqual(data, deserialized);
3338
3374
  });
3339
3375
 
3340
-
3341
3376
  test('unregistered extended Array class read/write', function(){
3342
3377
  var instance = new ExtendArray2();
3343
3378
  instance.push(0);
@@ -3490,6 +3525,58 @@
3490
3525
  assert.equal(deserialized.extendedInstance[0], 0);
3491
3526
  });
3492
3527
 
3528
+ test('extended class pack/unpack proxied', function(){
3529
+ function Extended() {
3530
+
3531
+ }
3532
+ Extended.prototype.__call__ = function(){
3533
+ return this.value * 4
3534
+ };
3535
+ Extended.prototype.getDouble = function() {
3536
+ return this.value * 2
3537
+ };
3538
+
3539
+ var instance = function() { instance.__call__();/* callable stuff */ };
3540
+ Object.setPrototypeOf(instance,Extended.prototype);
3541
+
3542
+ instance.value = 4;
3543
+ var data = instance;
3544
+
3545
+ let packr = new Packr();
3546
+ addExtension({
3547
+ Class: Extended,
3548
+ type: 15,
3549
+ unpack: function(buffer) {
3550
+ var e = function() { e.__call__(); };
3551
+ Object.setPrototypeOf(e,Extended.prototype);
3552
+ e.value = packr.unpack(buffer);
3553
+ return e
3554
+ },
3555
+ pack: function(instance) {
3556
+ return packr.pack(instance.value)
3557
+ }
3558
+ });
3559
+ var serialized = pack(data);
3560
+ var deserialized = unpack(serialized);
3561
+ assert.equal(deserialized.getDouble(), 8);
3562
+ });
3563
+
3564
+ test.skip('convert Date to string', function(){
3565
+ var data = {
3566
+ aDate: new Date(),
3567
+ };
3568
+ new Packr();
3569
+ addExtension({
3570
+ Class: Date,
3571
+ write(date) {
3572
+ return date.toString()
3573
+ }
3574
+ });
3575
+ var serialized = pack(data);
3576
+ var deserialized = unpack(serialized);
3577
+ assert.equal(deserialized.aDate, data.aDate.toString());
3578
+ });
3579
+
3493
3580
  test('proto handling', function() {
3494
3581
  var objectWithProto = JSON.parse('{"__proto__":{"foo":3}}');
3495
3582
  var decoded = unpack(pack(objectWithProto));