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/dist/index-no-eval.cjs +21 -6
- package/dist/index-no-eval.cjs.map +1 -1
- package/dist/index-no-eval.min.js +1 -1
- package/dist/index-no-eval.min.js.map +1 -1
- package/dist/index.js +21 -6
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +31 -11
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +49 -11
- package/dist/test.js.map +1 -1
- package/index.d.cts +2 -1
- package/index.d.ts +2 -1
- package/pack.js +21 -6
- package/package.json +1 -1
- package/struct.js +10 -5
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
|
-
|
|
492
|
-
|
|
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
|
-
|
|
554
|
-
|
|
555
|
-
|
|
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
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
|
-
|
|
721
|
-
|
|
722
|
-
|
|
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) {
|