msgpackr 1.11.2 → 1.11.4
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/README.md +1 -0
- package/dist/index-no-eval.cjs +42 -21
- 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 +42 -21
- 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 +49 -24
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +105 -26
- package/dist/test.js.map +1 -1
- package/dist/unpack-no-eval.cjs +31 -17
- package/dist/unpack-no-eval.cjs.map +1 -1
- package/index.d.cts +1 -0
- package/index.d.ts +1 -0
- package/pack.js +11 -4
- package/package.json +5 -1
- package/struct.js +7 -3
- package/unpack.js +31 -17
package/struct.js
CHANGED
|
@@ -515,10 +515,14 @@ function readStruct(src, position, srcEnd, unpackr) {
|
|
|
515
515
|
throw new Error('Could not find typed structure ' + recordId);
|
|
516
516
|
}
|
|
517
517
|
var construct = structure.construct;
|
|
518
|
+
var fullConstruct = structure.fullConstruct;
|
|
518
519
|
if (!construct) {
|
|
519
520
|
construct = structure.construct = function LazyObject() {
|
|
520
521
|
}
|
|
521
|
-
|
|
522
|
+
fullConstruct = structure.fullConstruct = function LoadedObject() {
|
|
523
|
+
}
|
|
524
|
+
fullConstruct.prototype = unpackr.structPrototype ?? {};
|
|
525
|
+
var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
|
|
522
526
|
let properties = [];
|
|
523
527
|
let currentOffset = 0;
|
|
524
528
|
let lastRefProperty;
|
|
@@ -719,12 +723,12 @@ function readStruct(src, position, srcEnd, unpackr) {
|
|
|
719
723
|
Object.defineProperty(prototype, property.key, { get: withSource(property.get), enumerable: true });
|
|
720
724
|
let valueFunction = 'v' + i++;
|
|
721
725
|
args.push(valueFunction);
|
|
722
|
-
objectLiteralProperties.push('[' + JSON.stringify(property.key) + ']
|
|
726
|
+
objectLiteralProperties.push('o[' + JSON.stringify(property.key) + ']=' + valueFunction + '(s)');
|
|
723
727
|
}
|
|
724
728
|
if (hasInheritedProperties) {
|
|
725
729
|
objectLiteralProperties.push('__proto__:this');
|
|
726
730
|
}
|
|
727
|
-
let toObject = (new Function(...args, 'return function(s){
|
|
731
|
+
let toObject = (new Function(...args, 'var c=this;return function(s){var o=new c();' + objectLiteralProperties.join(';') + ';return o;}')).apply(fullConstruct, properties.map(prop => prop.get));
|
|
728
732
|
Object.defineProperty(prototype, 'toJSON', {
|
|
729
733
|
value(omitUnderscoredProperties) {
|
|
730
734
|
return toObject.call(this, this[sourceSymbol]);
|
package/unpack.js
CHANGED
|
@@ -971,7 +971,10 @@ function asSafeString(property) {
|
|
|
971
971
|
if (typeof property === 'string') return property;
|
|
972
972
|
if (typeof property === 'number' || typeof property === 'boolean' || typeof property === 'bigint') return property.toString();
|
|
973
973
|
if (property == null) return property + '';
|
|
974
|
-
|
|
974
|
+
if (currentUnpackr.allowArraysInMapKeys && Array.isArray(property) && property.flat().every(item => ['string', 'number', 'boolean', 'bigint'].includes(typeof item))) {
|
|
975
|
+
return property.flat().toString();
|
|
976
|
+
}
|
|
977
|
+
throw new Error(`Invalid property type for record: ${typeof property}`);
|
|
975
978
|
}
|
|
976
979
|
// the registration of the record definition extension (as "r")
|
|
977
980
|
const recordDefinition = (id, highByte) => {
|
|
@@ -1021,20 +1024,33 @@ currentExtensions[0x69] = (data) => {
|
|
|
1021
1024
|
referenceMap = new Map()
|
|
1022
1025
|
let token = src[position]
|
|
1023
1026
|
let target
|
|
1024
|
-
// TODO: handle
|
|
1025
|
-
// ahead past references to record structure definitions
|
|
1027
|
+
// TODO: handle any other types that can cycle and make the code more robust if there are other extensions
|
|
1026
1028
|
if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
|
|
1027
1029
|
target = []
|
|
1030
|
+
else if (token >= 0x80 && token < 0x90 || token == 0xde || token == 0xdf)
|
|
1031
|
+
target = new Map()
|
|
1032
|
+
else if ((token >= 0xc7 && token <= 0xc9 || token >= 0xd4 && token <= 0xd8) && src[position + 1] === 0x73)
|
|
1033
|
+
target = new Set()
|
|
1028
1034
|
else
|
|
1029
1035
|
target = {}
|
|
1030
1036
|
|
|
1031
1037
|
let refEntry = { target } // a placeholder object
|
|
1032
1038
|
referenceMap.set(id, refEntry)
|
|
1033
1039
|
let targetProperties = read() // read the next value as the target object to id
|
|
1034
|
-
if (refEntry.used)
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1040
|
+
if (!refEntry.used) {
|
|
1041
|
+
// no cycle, can just use the returned read object
|
|
1042
|
+
return refEntry.target = targetProperties // replace the placeholder with the real one
|
|
1043
|
+
} else {
|
|
1044
|
+
// there is a cycle, so we have to assign properties to original target
|
|
1045
|
+
Object.assign(target, targetProperties)
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// copy over map/set entries if we're able to
|
|
1049
|
+
if (target instanceof Map)
|
|
1050
|
+
for (let [k, v] of targetProperties.entries()) target.set(k, v)
|
|
1051
|
+
if (target instanceof Set)
|
|
1052
|
+
for (let i of Array.from(targetProperties)) target.add(i)
|
|
1053
|
+
return target
|
|
1038
1054
|
}
|
|
1039
1055
|
|
|
1040
1056
|
currentExtensions[0x70] = (data) => {
|
|
@@ -1053,18 +1069,16 @@ export const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int3
|
|
|
1053
1069
|
let glbl = typeof globalThis === 'object' ? globalThis : window;
|
|
1054
1070
|
currentExtensions[0x74] = (data) => {
|
|
1055
1071
|
let typeCode = data[0]
|
|
1072
|
+
// we always have to slice to get a new ArrayBuffer that is aligned
|
|
1073
|
+
let buffer = Uint8Array.prototype.slice.call(data, 1).buffer
|
|
1074
|
+
|
|
1056
1075
|
let typedArrayName = typedArrays[typeCode]
|
|
1057
1076
|
if (!typedArrayName) {
|
|
1058
|
-
if (typeCode === 16)
|
|
1059
|
-
|
|
1060
|
-
let u8 = new Uint8Array(ab)
|
|
1061
|
-
u8.set(data.subarray(1))
|
|
1062
|
-
return ab;
|
|
1063
|
-
}
|
|
1077
|
+
if (typeCode === 16) return buffer
|
|
1078
|
+
if (typeCode === 17) return new DataView(buffer)
|
|
1064
1079
|
throw new Error('Could not find typed array for code ' + typeCode)
|
|
1065
1080
|
}
|
|
1066
|
-
|
|
1067
|
-
return new glbl[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
|
|
1081
|
+
return new glbl[typedArrayName](buffer)
|
|
1068
1082
|
}
|
|
1069
1083
|
currentExtensions[0x78] = () => {
|
|
1070
1084
|
let data = read()
|
|
@@ -1092,13 +1106,13 @@ currentExtensions[0xff] = (data) => {
|
|
|
1092
1106
|
return new Date(
|
|
1093
1107
|
((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
|
|
1094
1108
|
((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
|
|
1095
|
-
else if (data.length == 12)
|
|
1109
|
+
else if (data.length == 12)
|
|
1096
1110
|
return new Date(
|
|
1097
1111
|
((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
|
|
1098
1112
|
(((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
|
|
1099
1113
|
else
|
|
1100
1114
|
return new Date('invalid')
|
|
1101
|
-
}
|
|
1115
|
+
}
|
|
1102
1116
|
// registration of bulk record definition?
|
|
1103
1117
|
// currentExtensions[0x52] = () =>
|
|
1104
1118
|
|