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/node.cjs CHANGED
@@ -1640,8 +1640,13 @@ class Packr extends Unpackr {
1640
1640
  if (Array.isArray(value)) {
1641
1641
  packArray(value);
1642
1642
  } else {
1643
- if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1644
- return pack(value.toJSON());
1643
+ // use this as an alternate mechanism for expressing how to serialize
1644
+ if (value.toJSON) {
1645
+ const json = value.toJSON();
1646
+ // if for some reason value.toJSON returns itself it'll loop forever
1647
+ if (json !== value)
1648
+ return pack(json)
1649
+ }
1645
1650
 
1646
1651
  // if there is a writeFunction, use it, otherwise just encode as undefined
1647
1652
  if (type === 'function')
@@ -2865,11 +2870,16 @@ function readStruct(src, position, srcEnd, unpackr) {
2865
2870
  objectLiteralProperties.push('__proto__:this');
2866
2871
  }
2867
2872
  let toObject = (new Function(...args, 'return function(s){return{' + objectLiteralProperties.join(',') + '}}')).apply(null, properties.map(prop => prop.get));
2868
- Object.defineProperty(prototype, 'toJSON', {
2869
- value(omitUnderscoredProperties) {
2870
- return toObject.call(this, this[sourceSymbol]);
2871
- }
2872
- });
2873
+ try {
2874
+ Object.defineProperty(prototype, 'toJSON', {
2875
+ value(omitUnderscoredProperties) {
2876
+ return toObject.call(this, this[sourceSymbol]);
2877
+ }
2878
+ });
2879
+ } catch(error) {
2880
+ error.message += ' setting properties ' + JSON.stringify(properties);
2881
+ throw error;
2882
+ }
2873
2883
  } else {
2874
2884
  Object.defineProperty(prototype, 'toJSON', {
2875
2885
  value(omitUnderscoredProperties) {