cbor-x 1.5.8 → 1.5.9
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/decode.js +18 -4
- package/dist/decode-no-eval.cjs +18 -4
- package/dist/decode-no-eval.cjs.map +1 -1
- package/dist/index-no-eval.cjs +31 -17
- 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 +31 -17
- 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 -17
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +84 -28
- package/dist/test.js.map +1 -1
- package/encode.js +13 -13
- package/package.json +9 -3
package/dist/node.cjs
CHANGED
|
@@ -503,7 +503,8 @@ function createStructureReader(structure) {
|
|
|
503
503
|
function safeKey(key) {
|
|
504
504
|
// protect against prototype pollution
|
|
505
505
|
if (typeof key === 'string') return key === '__proto__' ? '__proto_' : key
|
|
506
|
-
if (typeof key
|
|
506
|
+
if (typeof key === 'number' || typeof key === 'boolean' || typeof key === 'bigint') return key.toString();
|
|
507
|
+
if (key == null) return key + '';
|
|
507
508
|
// protect against expensive (DoS) string conversions
|
|
508
509
|
throw new Error('Invalid property name type ' + typeof key);
|
|
509
510
|
}
|
|
@@ -923,6 +924,7 @@ currentExtensions[28] = (read) => {
|
|
|
923
924
|
referenceMap.id = 0;
|
|
924
925
|
}
|
|
925
926
|
let id = referenceMap.id++;
|
|
927
|
+
let startingPosition = position$1;
|
|
926
928
|
let token = src[position$1];
|
|
927
929
|
let target;
|
|
928
930
|
// TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
|
|
@@ -935,8 +937,20 @@ currentExtensions[28] = (read) => {
|
|
|
935
937
|
let refEntry = { target }; // a placeholder object
|
|
936
938
|
referenceMap.set(id, refEntry);
|
|
937
939
|
let targetProperties = read(); // read the next value as the target object to id
|
|
938
|
-
if (refEntry.used) // there is a cycle, so we have to assign properties to original target
|
|
940
|
+
if (refEntry.used) {// there is a cycle, so we have to assign properties to original target
|
|
941
|
+
if (Object.getPrototypeOf(target) !== Object.getPrototypeOf(targetProperties)) {
|
|
942
|
+
// this means that the returned target does not match the targetProperties, so we need rerun the read to
|
|
943
|
+
// have the correctly create instance be assigned as a reference, then we do the copy the properties back to the
|
|
944
|
+
// target
|
|
945
|
+
// reset the position so that the read can be repeated
|
|
946
|
+
position$1 = startingPosition;
|
|
947
|
+
// the returned instance is our new target for references
|
|
948
|
+
target = targetProperties;
|
|
949
|
+
referenceMap.set(id, { target });
|
|
950
|
+
targetProperties = read();
|
|
951
|
+
}
|
|
939
952
|
return Object.assign(target, targetProperties)
|
|
953
|
+
}
|
|
940
954
|
refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
|
|
941
955
|
return targetProperties // no cycle, can just use the returned read object
|
|
942
956
|
};
|
|
@@ -1018,7 +1032,7 @@ function registerTypedArray(TypedArray, tag) {
|
|
|
1018
1032
|
for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
|
|
1019
1033
|
if (!littleEndian && bytesPerElement == 1)
|
|
1020
1034
|
continue
|
|
1021
|
-
let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : 3;
|
|
1035
|
+
let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : bytesPerElement == 8 ? 3 : 0;
|
|
1022
1036
|
currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
|
|
1023
1037
|
if (!TypedArray)
|
|
1024
1038
|
throw new Error('Could not find typed array for code ' + tag)
|
|
@@ -1028,7 +1042,7 @@ function registerTypedArray(TypedArray, tag) {
|
|
|
1028
1042
|
bytesPerElement === 2 && !(buffer.byteOffset & 1) ||
|
|
1029
1043
|
bytesPerElement === 4 && !(buffer.byteOffset & 3) ||
|
|
1030
1044
|
bytesPerElement === 8 && !(buffer.byteOffset & 7))
|
|
1031
|
-
return new TypedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
1045
|
+
return new TypedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength >> sizeShift);
|
|
1032
1046
|
}
|
|
1033
1047
|
// we have to slice/copy here to get a new ArrayBuffer, if we are not word/byte aligned
|
|
1034
1048
|
return new TypedArray(Uint8Array.prototype.slice.call(buffer, 0).buffer)
|
|
@@ -1632,7 +1646,7 @@ class Encoder extends Decoder {
|
|
|
1632
1646
|
}
|
|
1633
1647
|
let constructor = value.constructor;
|
|
1634
1648
|
if (constructor === Object) {
|
|
1635
|
-
writeObject(value
|
|
1649
|
+
writeObject(value);
|
|
1636
1650
|
} else if (constructor === Array) {
|
|
1637
1651
|
length = value.length;
|
|
1638
1652
|
if (length < 0x18) {
|
|
@@ -1727,8 +1741,8 @@ class Encoder extends Decoder {
|
|
|
1727
1741
|
return encode(json)
|
|
1728
1742
|
}
|
|
1729
1743
|
|
|
1730
|
-
// no extension found, write as object
|
|
1731
|
-
writeObject(value
|
|
1744
|
+
// no extension found, write as a plain object
|
|
1745
|
+
writeObject(value);
|
|
1732
1746
|
}
|
|
1733
1747
|
}
|
|
1734
1748
|
} else if (type === 'boolean') {
|
|
@@ -1790,19 +1804,19 @@ class Encoder extends Decoder {
|
|
|
1790
1804
|
}
|
|
1791
1805
|
}
|
|
1792
1806
|
} :
|
|
1793
|
-
(object
|
|
1807
|
+
(object) => {
|
|
1794
1808
|
target[position++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
|
|
1795
1809
|
let objectOffset = position - start;
|
|
1796
1810
|
position += 2;
|
|
1797
1811
|
let size = 0;
|
|
1798
1812
|
if (encoder.keyMap) {
|
|
1799
|
-
for (let key in object) if (
|
|
1813
|
+
for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
1800
1814
|
encode(encoder.encodeKey(key));
|
|
1801
1815
|
encode(object[key]);
|
|
1802
1816
|
size++;
|
|
1803
1817
|
}
|
|
1804
1818
|
} else {
|
|
1805
|
-
for (let key in object) if (
|
|
1819
|
+
for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
1806
1820
|
encode(key);
|
|
1807
1821
|
encode(object[key]);
|
|
1808
1822
|
size++;
|
|
@@ -1811,7 +1825,7 @@ class Encoder extends Decoder {
|
|
|
1811
1825
|
target[objectOffset++ + start] = size >> 8;
|
|
1812
1826
|
target[objectOffset + start] = size & 0xff;
|
|
1813
1827
|
} :
|
|
1814
|
-
(object,
|
|
1828
|
+
(object, skipValues) => {
|
|
1815
1829
|
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
1816
1830
|
let newTransitions = 0;
|
|
1817
1831
|
let length = 0;
|
|
@@ -1830,7 +1844,7 @@ class Encoder extends Decoder {
|
|
|
1830
1844
|
transition = nextTransition;
|
|
1831
1845
|
}
|
|
1832
1846
|
} else {
|
|
1833
|
-
for (let key in object) if (
|
|
1847
|
+
for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
1834
1848
|
nextTransition = transition[key];
|
|
1835
1849
|
if (!nextTransition) {
|
|
1836
1850
|
if (transition[RECORD_SYMBOL] & 0x100000) {// this indicates it is a brancheable/extendable terminal node, so we will use this record id and extend it
|
|
@@ -1890,9 +1904,9 @@ class Encoder extends Decoder {
|
|
|
1890
1904
|
writeArrayHeader(length + 2);
|
|
1891
1905
|
encode(0xe000 + recordId);
|
|
1892
1906
|
encode(keys);
|
|
1893
|
-
if (
|
|
1907
|
+
if (skipValues) return; // special exit for iterator
|
|
1894
1908
|
for (let key in object)
|
|
1895
|
-
if (
|
|
1909
|
+
if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key))
|
|
1896
1910
|
encode(object[key]);
|
|
1897
1911
|
return
|
|
1898
1912
|
}
|
|
@@ -1902,9 +1916,9 @@ class Encoder extends Decoder {
|
|
|
1902
1916
|
} else {
|
|
1903
1917
|
writeArrayHeader(length);
|
|
1904
1918
|
}
|
|
1905
|
-
if (
|
|
1919
|
+
if (skipValues) return; // special exit for iterator
|
|
1906
1920
|
for (let key in object)
|
|
1907
|
-
if (
|
|
1921
|
+
if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key))
|
|
1908
1922
|
encode(object[key]);
|
|
1909
1923
|
};
|
|
1910
1924
|
const makeRoom = (end) => {
|
|
@@ -1942,7 +1956,7 @@ class Encoder extends Decoder {
|
|
|
1942
1956
|
if (constructor === Object) {
|
|
1943
1957
|
let useRecords = encoder.useRecords !== false;
|
|
1944
1958
|
if (useRecords)
|
|
1945
|
-
writeObject(object,
|
|
1959
|
+
writeObject(object, true); // write the record identifier
|
|
1946
1960
|
else
|
|
1947
1961
|
writeEntityLength(Object.keys(object).length, 0xa0);
|
|
1948
1962
|
for (let key in object) {
|