msgpackr 1.9.3 → 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/index.d.cts CHANGED
@@ -7,13 +7,14 @@ export enum FLOAT32_OPTIONS {
7
7
 
8
8
  export interface Options {
9
9
  useFloat32?: FLOAT32_OPTIONS
10
- useRecords?: boolean | (value:any)=> boolean
10
+ useRecords?: boolean | ((value:any)=> boolean)
11
11
  structures?: {}[]
12
12
  moreTypes?: boolean
13
13
  sequential?: boolean
14
14
  structuredClone?: boolean
15
15
  mapsAsObjects?: boolean
16
16
  variableMapSize?: boolean
17
+ coercibleKeyAsNumber?: boolean
17
18
  copyBuffers?: boolean
18
19
  bundleStrings?: boolean
19
20
  useTimestamp32?: boolean
package/index.d.ts CHANGED
@@ -7,13 +7,14 @@ export enum FLOAT32_OPTIONS {
7
7
 
8
8
  export interface Options {
9
9
  useFloat32?: FLOAT32_OPTIONS
10
- useRecords?: boolean | (value:any)=> boolean
10
+ useRecords?: boolean | ((value:any)=> boolean)
11
11
  structures?: {}[]
12
12
  moreTypes?: boolean
13
13
  sequential?: boolean
14
14
  structuredClone?: boolean
15
15
  mapsAsObjects?: boolean
16
16
  variableMapSize?: boolean
17
+ coercibleKeyAsNumber?: boolean
17
18
  copyBuffers?: boolean
18
19
  bundleStrings?: boolean
19
20
  useTimestamp32?: boolean
package/pack.js CHANGED
@@ -488,8 +488,13 @@ export class Packr extends Unpackr {
488
488
  if (Array.isArray(value)) {
489
489
  packArray(value)
490
490
  } else {
491
- if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
492
- return pack(value.toJSON());
491
+ // use this as an alternate mechanism for expressing how to serialize
492
+ if (value.toJSON) {
493
+ const json = value.toJSON()
494
+ // if for some reason value.toJSON returns itself it'll loop forever
495
+ if (json !== value)
496
+ return pack(json)
497
+ }
493
498
 
494
499
  // if there is a writeFunction, use it, otherwise just encode as undefined
495
500
  if (type === 'function')
@@ -534,7 +539,7 @@ export class Packr extends Unpackr {
534
539
  }
535
540
  }
536
541
 
537
- const writePlainObject = this.variableMapSize ? (object) => {
542
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
538
543
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
539
544
  let keys = Object.keys(object)
540
545
  let length = keys.length
@@ -550,9 +555,19 @@ export class Packr extends Unpackr {
550
555
  position += 4
551
556
  }
552
557
  let key
553
- for (let i = 0; i < length; i++) {
554
- pack(key = keys[i])
555
- pack(object[key])
558
+ if (this.coercibleKeyAsNumber) {
559
+ for (let i = 0; i < length; i++) {
560
+ key = keys[i]
561
+ let num = Number(key)
562
+ pack(isNaN(num) ? key : num)
563
+ pack(object[key])
564
+ }
565
+
566
+ } else {
567
+ for (let i = 0; i < length; i++) {
568
+ pack(key = keys[i])
569
+ pack(object[key])
570
+ }
556
571
  }
557
572
  } :
558
573
  (object, safePrototype) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpackr",
3
3
  "author": "Kris Zyp",
4
- "version": "1.9.3",
4
+ "version": "1.9.5-debug.1",
5
5
  "description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning",
6
6
  "license": "MIT",
7
7
  "types": "./index.d.ts",
package/struct.js CHANGED
@@ -717,11 +717,16 @@ function readStruct(src, position, srcEnd, unpackr) {
717
717
  objectLiteralProperties.push('__proto__:this');
718
718
  }
719
719
  let toObject = (new Function(...args, 'return function(s){return{' + objectLiteralProperties.join(',') + '}}')).apply(null, properties.map(prop => prop.get));
720
- Object.defineProperty(prototype, 'toJSON', {
721
- value(omitUnderscoredProperties) {
722
- return toObject.call(this, this[sourceSymbol]);
723
- }
724
- });
720
+ try {
721
+ Object.defineProperty(prototype, 'toJSON', {
722
+ value(omitUnderscoredProperties) {
723
+ return toObject.call(this, this[sourceSymbol]);
724
+ }
725
+ });
726
+ } catch(error) {
727
+ error.message += ' setting properties ' + JSON.stringify(properties);
728
+ throw error;
729
+ }
725
730
  } else {
726
731
  Object.defineProperty(prototype, 'toJSON', {
727
732
  value(omitUnderscoredProperties) {