msgpackr 1.9.4 → 1.9.5-debug.1

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
@@ -1635,8 +1635,13 @@
1635
1635
  if (Array.isArray(value)) {
1636
1636
  packArray(value);
1637
1637
  } else {
1638
- if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1639
- return pack(value.toJSON());
1638
+ // use this as an alternate mechanism for expressing how to serialize
1639
+ if (value.toJSON) {
1640
+ const json = value.toJSON();
1641
+ // if for some reason value.toJSON returns itself it'll loop forever
1642
+ if (json !== value)
1643
+ return pack(json)
1644
+ }
1640
1645
 
1641
1646
  // if there is a writeFunction, use it, otherwise just encode as undefined
1642
1647
  if (type === 'function')
@@ -2858,11 +2863,16 @@
2858
2863
  objectLiteralProperties.push('__proto__:this');
2859
2864
  }
2860
2865
  let toObject = (new Function(...args, 'return function(s){return{' + objectLiteralProperties.join(',') + '}}')).apply(null, properties.map(prop => prop.get));
2861
- Object.defineProperty(prototype, 'toJSON', {
2862
- value(omitUnderscoredProperties) {
2863
- return toObject.call(this, this[sourceSymbol]);
2864
- }
2865
- });
2866
+ try {
2867
+ Object.defineProperty(prototype, 'toJSON', {
2868
+ value(omitUnderscoredProperties) {
2869
+ return toObject.call(this, this[sourceSymbol]);
2870
+ }
2871
+ });
2872
+ } catch(error) {
2873
+ error.message += ' setting properties ' + JSON.stringify(properties);
2874
+ throw error;
2875
+ }
2866
2876
  } else {
2867
2877
  Object.defineProperty(prototype, 'toJSON', {
2868
2878
  value(omitUnderscoredProperties) {
@@ -4098,6 +4108,17 @@
4098
4108
  assert.deepEqual(values, [[1,0,1], [2,1,2], [3,2,3], [4,3,4]]);
4099
4109
  });
4100
4110
 
4111
+ test('pack toJSON returning this', () => {
4112
+ class Serializable {
4113
+ someData = [1, 2, 3, 4]
4114
+ toJSON() {
4115
+ return this
4116
+ }
4117
+ }
4118
+ const serialized = pack(new Serializable);
4119
+ const deserialized = unpack(serialized);
4120
+ assert.deepStrictEqual(deserialized, { someData: [1, 2, 3, 4] });
4121
+ });
4101
4122
  });
4102
4123
  suite('msgpackr performance tests', function(){
4103
4124
  test('performance JSON.parse', function() {