seroval 1.0.2 → 1.0.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/dist/cjs/development/index.cjs +339 -226
- package/dist/cjs/development/index.cjs.map +3 -3
- package/dist/cjs/production/index.cjs +17 -3
- package/dist/esm/development/index.mjs +339 -226
- package/dist/esm/development/index.mjs.map +3 -3
- package/dist/esm/production/index.mjs +17 -3
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/parser/async.d.ts.map +1 -1
- package/dist/types/core/context/parser/sync.d.ts.map +1 -1
- package/dist/types/core/context/parser.d.ts.map +1 -1
- package/dist/types/core/context/serializer.d.ts.map +1 -1
- package/dist/types/core/errors.d.ts +38 -0
- package/dist/types/core/errors.d.ts.map +1 -0
- package/dist/types/core/reference.d.ts.map +1 -1
- package/dist/types/core/tree/serializer.d.ts.map +1 -1
- package/dist/types/core/utils/typed-array.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/core/base-primitives.ts +0 -5
- package/src/core/context/deserializer.ts +86 -76
- package/src/core/context/parser/async.ts +30 -26
- package/src/core/context/parser/sync.ts +30 -26
- package/src/core/context/parser.ts +3 -10
- package/src/core/context/serializer.ts +113 -80
- package/src/core/errors.ts +99 -0
- package/src/core/reference.ts +6 -2
- package/src/core/tree/serializer.ts +4 -3
- package/src/core/utils/typed-array.ts +3 -1
- package/src/index.ts +1 -0
- package/dist/types/core/UnsupportedTypeError.d.ts +0 -5
- package/dist/types/core/UnsupportedTypeError.d.ts.map +0 -1
- package/src/core/UnsupportedTypeError.ts +0 -7
|
@@ -122,11 +122,11 @@ function hasReference(id) {
|
|
|
122
122
|
return INV_REFERENCE.has(id);
|
|
123
123
|
}
|
|
124
124
|
function getReferenceID(value) {
|
|
125
|
-
assert(hasReferenceID(value), new
|
|
125
|
+
assert(hasReferenceID(value), new SerovalMissingReferenceError(value));
|
|
126
126
|
return REFERENCE.get(value);
|
|
127
127
|
}
|
|
128
128
|
function getReference(id) {
|
|
129
|
-
assert(hasReference(id), new
|
|
129
|
+
assert(hasReference(id), new SerovalMissingReferenceForIdError(id));
|
|
130
130
|
return INV_REFERENCE.get(id);
|
|
131
131
|
}
|
|
132
132
|
if (typeof globalThis !== "undefined") {
|
|
@@ -183,15 +183,6 @@ function resolvePlugins(plugins) {
|
|
|
183
183
|
return void 0;
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
// src/core/UnsupportedTypeError.ts
|
|
187
|
-
var { toString: objectToString } = Object.prototype;
|
|
188
|
-
var UnsupportedTypeError = class extends Error {
|
|
189
|
-
constructor(value) {
|
|
190
|
-
super('Unsupported type "' + objectToString.call(value) + '"');
|
|
191
|
-
this.value = value;
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
|
|
195
186
|
// src/core/constants.ts
|
|
196
187
|
var SYMBOL_STRING = {
|
|
197
188
|
[0 /* AsyncIterator */]: "Symbol.asyncIterator",
|
|
@@ -515,10 +506,6 @@ function createArrayBufferNode(id, current) {
|
|
|
515
506
|
};
|
|
516
507
|
}
|
|
517
508
|
function createWKSymbolNode(id, current) {
|
|
518
|
-
assert(
|
|
519
|
-
current in INV_SYMBOL_REF,
|
|
520
|
-
new Error("Only well-known symbols are supported.")
|
|
521
|
-
);
|
|
522
509
|
return {
|
|
523
510
|
t: 17 /* WKSymbol */,
|
|
524
511
|
i: id,
|
|
@@ -855,6 +842,91 @@ var SPECIAL_REFS = {
|
|
|
855
842
|
[4 /* StreamConstructor */]: {}
|
|
856
843
|
};
|
|
857
844
|
|
|
845
|
+
// src/core/errors.ts
|
|
846
|
+
var { toString: objectToString } = Object.prototype;
|
|
847
|
+
function getErrorMessage(type, cause) {
|
|
848
|
+
if (cause instanceof Error) {
|
|
849
|
+
return `Seroval caught an error during the ${type} process.
|
|
850
|
+
|
|
851
|
+
${cause.name}
|
|
852
|
+
${cause.message}
|
|
853
|
+
|
|
854
|
+
- For more information, please check the "cause" property of this error.
|
|
855
|
+
- If you believe this is an error in Seroval, please submit an issue at https://github.com/lxsmnsyc/seroval/issues/new`;
|
|
856
|
+
}
|
|
857
|
+
return `Seroval caught an error during the ${type} process.
|
|
858
|
+
|
|
859
|
+
"${objectToString.call(cause)}"
|
|
860
|
+
|
|
861
|
+
For more information, please check the "cause" property of this error.`;
|
|
862
|
+
}
|
|
863
|
+
var SerovalError = class extends Error {
|
|
864
|
+
constructor(type, cause) {
|
|
865
|
+
super(getErrorMessage(type, cause));
|
|
866
|
+
this.cause = cause;
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
var SerovalParserError = class extends SerovalError {
|
|
870
|
+
constructor(cause) {
|
|
871
|
+
super("parsing", cause);
|
|
872
|
+
}
|
|
873
|
+
};
|
|
874
|
+
var SerovalSerializationError = class extends SerovalError {
|
|
875
|
+
constructor(cause) {
|
|
876
|
+
super("serialization", cause);
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
var SerovalDeserializationError = class extends SerovalError {
|
|
880
|
+
constructor(cause) {
|
|
881
|
+
super("deserialization", cause);
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
var SerovalUnsupportedTypeError = class extends Error {
|
|
885
|
+
constructor(value) {
|
|
886
|
+
super(
|
|
887
|
+
`The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
|
|
888
|
+
|
|
889
|
+
There are few workarounds for this problem:
|
|
890
|
+
- Transform the value in a way that it can be serialized.
|
|
891
|
+
- If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.`
|
|
892
|
+
);
|
|
893
|
+
this.value = value;
|
|
894
|
+
}
|
|
895
|
+
};
|
|
896
|
+
var SerovalUnsupportedNodeError = class extends Error {
|
|
897
|
+
constructor(node) {
|
|
898
|
+
super('Unsupported node type "' + node.t + '".');
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
var SerovalMissingPluginError = class extends Error {
|
|
902
|
+
constructor(tag) {
|
|
903
|
+
super('Missing plugin for tag "' + tag + '".');
|
|
904
|
+
}
|
|
905
|
+
};
|
|
906
|
+
var SerovalMissingInstanceError = class extends Error {
|
|
907
|
+
constructor(tag) {
|
|
908
|
+
super('Missing "' + tag + '" instance.');
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
var SerovalMissingReferenceError = class extends Error {
|
|
912
|
+
constructor(value) {
|
|
913
|
+
super(
|
|
914
|
+
'Missing reference for the value "' + objectToString.call(value) + '" of type "' + typeof value + '"'
|
|
915
|
+
);
|
|
916
|
+
this.value = value;
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
var SerovalMissingReferenceForIdError = class extends Error {
|
|
920
|
+
constructor(id) {
|
|
921
|
+
super('Missing reference for id "' + serializeString(id) + '"');
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
var SerovalUnknownTypedArrayError = class extends Error {
|
|
925
|
+
constructor(name) {
|
|
926
|
+
super('Unknown TypedArray "' + name + '"');
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
|
|
858
930
|
// src/core/context/parser.ts
|
|
859
931
|
var BaseParserContext = class {
|
|
860
932
|
constructor(options) {
|
|
@@ -899,12 +971,7 @@ var BaseParserContext = class {
|
|
|
899
971
|
return indexed;
|
|
900
972
|
}
|
|
901
973
|
getStrictReference(current) {
|
|
902
|
-
assert(
|
|
903
|
-
hasReferenceID(current),
|
|
904
|
-
new Error(
|
|
905
|
-
"Cannot serialize " + typeof current + " without reference ID."
|
|
906
|
-
)
|
|
907
|
-
);
|
|
974
|
+
assert(hasReferenceID(current), new SerovalUnsupportedTypeError(current));
|
|
908
975
|
const result = this.getIndexedValue(current);
|
|
909
976
|
if (result.type === 1 /* Indexed */) {
|
|
910
977
|
return result.value;
|
|
@@ -919,10 +986,7 @@ var BaseParserContext = class {
|
|
|
919
986
|
if (ref.type !== 0 /* Fresh */) {
|
|
920
987
|
return ref.value;
|
|
921
988
|
}
|
|
922
|
-
assert(
|
|
923
|
-
current in INV_SYMBOL_REF,
|
|
924
|
-
new Error("Cannot serialized unsupported symbol.")
|
|
925
|
-
);
|
|
989
|
+
assert(current in INV_SYMBOL_REF, new SerovalUnsupportedTypeError(current));
|
|
926
990
|
return createWKSymbolNode(ref.value, current);
|
|
927
991
|
}
|
|
928
992
|
parseSpecialReference(ref) {
|
|
@@ -1527,33 +1591,37 @@ var BaseAsyncParserContext = class extends BaseParserContext {
|
|
|
1527
1591
|
if (Symbol.iterator in current || Symbol.asyncIterator in current) {
|
|
1528
1592
|
return this.parsePlainObject(id, current, !!currentClass);
|
|
1529
1593
|
}
|
|
1530
|
-
throw new
|
|
1594
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
1531
1595
|
}
|
|
1532
1596
|
async parse(current) {
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1597
|
+
try {
|
|
1598
|
+
switch (typeof current) {
|
|
1599
|
+
case "boolean":
|
|
1600
|
+
return current ? TRUE_NODE : FALSE_NODE;
|
|
1601
|
+
case "undefined":
|
|
1602
|
+
return UNDEFINED_NODE;
|
|
1603
|
+
case "string":
|
|
1604
|
+
return createStringNode(current);
|
|
1605
|
+
case "number":
|
|
1606
|
+
return createNumberNode(current);
|
|
1607
|
+
case "bigint":
|
|
1608
|
+
return createBigIntNode(current);
|
|
1609
|
+
case "object": {
|
|
1610
|
+
if (current) {
|
|
1611
|
+
const ref = this.getReference(current);
|
|
1612
|
+
return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
|
|
1613
|
+
}
|
|
1614
|
+
return NULL_NODE;
|
|
1548
1615
|
}
|
|
1549
|
-
|
|
1616
|
+
case "symbol":
|
|
1617
|
+
return this.parseWellKnownSymbol(current);
|
|
1618
|
+
case "function":
|
|
1619
|
+
return this.parseFunction(current);
|
|
1620
|
+
default:
|
|
1621
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
1550
1622
|
}
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
case "function":
|
|
1554
|
-
return this.parseFunction(current);
|
|
1555
|
-
default:
|
|
1556
|
-
throw new UnsupportedTypeError(current);
|
|
1623
|
+
} catch (error) {
|
|
1624
|
+
throw new SerovalParserError(error);
|
|
1557
1625
|
}
|
|
1558
1626
|
}
|
|
1559
1627
|
};
|
|
@@ -1592,7 +1660,7 @@ function getTypedArrayConstructor(name) {
|
|
|
1592
1660
|
case "BigUint64Array":
|
|
1593
1661
|
return BigUint64Array;
|
|
1594
1662
|
default:
|
|
1595
|
-
throw new
|
|
1663
|
+
throw new SerovalUnknownTypedArrayError(name);
|
|
1596
1664
|
}
|
|
1597
1665
|
}
|
|
1598
1666
|
|
|
@@ -1761,20 +1829,20 @@ var BaseDeserializerContext = class {
|
|
|
1761
1829
|
}
|
|
1762
1830
|
}
|
|
1763
1831
|
}
|
|
1764
|
-
throw new
|
|
1832
|
+
throw new SerovalMissingPluginError(node.c);
|
|
1765
1833
|
}
|
|
1766
1834
|
deserializePromiseConstructor(node) {
|
|
1767
1835
|
return this.assignIndexedValue(node.i, createDeferred()).promise;
|
|
1768
1836
|
}
|
|
1769
1837
|
deserializePromiseResolve(node) {
|
|
1770
1838
|
const deferred = this.refs.get(node.i);
|
|
1771
|
-
assert(deferred, new
|
|
1839
|
+
assert(deferred, new SerovalMissingInstanceError("Promise"));
|
|
1772
1840
|
deferred.resolve(this.deserialize(node.a[1]));
|
|
1773
1841
|
return void 0;
|
|
1774
1842
|
}
|
|
1775
1843
|
deserializePromiseReject(node) {
|
|
1776
1844
|
const deferred = this.refs.get(node.i);
|
|
1777
|
-
assert(deferred, new
|
|
1845
|
+
assert(deferred, new SerovalMissingInstanceError("Promise"));
|
|
1778
1846
|
deferred.reject(this.deserialize(node.a[1]));
|
|
1779
1847
|
return void 0;
|
|
1780
1848
|
}
|
|
@@ -1800,19 +1868,19 @@ var BaseDeserializerContext = class {
|
|
|
1800
1868
|
}
|
|
1801
1869
|
deserializeStreamNext(node) {
|
|
1802
1870
|
const deferred = this.refs.get(node.i);
|
|
1803
|
-
assert(deferred, new
|
|
1871
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1804
1872
|
deferred.next(this.deserialize(node.f));
|
|
1805
1873
|
return void 0;
|
|
1806
1874
|
}
|
|
1807
1875
|
deserializeStreamThrow(node) {
|
|
1808
1876
|
const deferred = this.refs.get(node.i);
|
|
1809
|
-
assert(deferred, new
|
|
1877
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1810
1878
|
deferred.throw(this.deserialize(node.f));
|
|
1811
1879
|
return void 0;
|
|
1812
1880
|
}
|
|
1813
1881
|
deserializeStreamReturn(node) {
|
|
1814
1882
|
const deferred = this.refs.get(node.i);
|
|
1815
|
-
assert(deferred, new
|
|
1883
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1816
1884
|
deferred.return(this.deserialize(node.f));
|
|
1817
1885
|
return void 0;
|
|
1818
1886
|
}
|
|
@@ -1825,75 +1893,79 @@ var BaseDeserializerContext = class {
|
|
|
1825
1893
|
return void 0;
|
|
1826
1894
|
}
|
|
1827
1895
|
deserialize(node) {
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1896
|
+
try {
|
|
1897
|
+
switch (node.t) {
|
|
1898
|
+
case 2 /* Constant */:
|
|
1899
|
+
return CONSTANT_VAL[node.s];
|
|
1900
|
+
case 0 /* Number */:
|
|
1901
|
+
return node.s;
|
|
1902
|
+
case 1 /* String */:
|
|
1903
|
+
return deserializeString(node.s);
|
|
1904
|
+
case 3 /* BigInt */:
|
|
1905
|
+
return BigInt(node.s);
|
|
1906
|
+
case 4 /* IndexedValue */:
|
|
1907
|
+
return this.refs.get(node.i);
|
|
1908
|
+
case 18 /* Reference */:
|
|
1909
|
+
return this.deserializeReference(node);
|
|
1910
|
+
case 9 /* Array */:
|
|
1911
|
+
return this.deserializeArray(node);
|
|
1912
|
+
case 10 /* Object */:
|
|
1913
|
+
case 11 /* NullConstructor */:
|
|
1914
|
+
return this.deserializeObject(node);
|
|
1915
|
+
case 5 /* Date */:
|
|
1916
|
+
return this.deserializeDate(node);
|
|
1917
|
+
case 6 /* RegExp */:
|
|
1918
|
+
return this.deserializeRegExp(node);
|
|
1919
|
+
case 7 /* Set */:
|
|
1920
|
+
return this.deserializeSet(node);
|
|
1921
|
+
case 8 /* Map */:
|
|
1922
|
+
return this.deserializeMap(node);
|
|
1923
|
+
case 19 /* ArrayBuffer */:
|
|
1924
|
+
return this.deserializeArrayBuffer(node);
|
|
1925
|
+
case 16 /* BigIntTypedArray */:
|
|
1926
|
+
case 15 /* TypedArray */:
|
|
1927
|
+
return this.deserializeTypedArray(node);
|
|
1928
|
+
case 20 /* DataView */:
|
|
1929
|
+
return this.deserializeDataView(node);
|
|
1930
|
+
case 14 /* AggregateError */:
|
|
1931
|
+
return this.deserializeAggregateError(node);
|
|
1932
|
+
case 13 /* Error */:
|
|
1933
|
+
return this.deserializeError(node);
|
|
1934
|
+
case 12 /* Promise */:
|
|
1935
|
+
return this.deserializePromise(node);
|
|
1936
|
+
case 17 /* WKSymbol */:
|
|
1937
|
+
return SYMBOL_REF[node.s];
|
|
1938
|
+
case 21 /* Boxed */:
|
|
1939
|
+
return this.deserializeBoxed(node);
|
|
1940
|
+
case 25 /* Plugin */:
|
|
1941
|
+
return this.deserializePlugin(node);
|
|
1942
|
+
case 22 /* PromiseConstructor */:
|
|
1943
|
+
return this.deserializePromiseConstructor(node);
|
|
1944
|
+
case 23 /* PromiseResolve */:
|
|
1945
|
+
return this.deserializePromiseResolve(node);
|
|
1946
|
+
case 24 /* PromiseReject */:
|
|
1947
|
+
return this.deserializePromiseReject(node);
|
|
1948
|
+
case 28 /* IteratorFactoryInstance */:
|
|
1949
|
+
return this.deserializeIteratorFactoryInstance(node);
|
|
1950
|
+
case 30 /* AsyncIteratorFactoryInstance */:
|
|
1951
|
+
return this.deserializeAsyncIteratorFactoryInstance(node);
|
|
1952
|
+
case 31 /* StreamConstructor */:
|
|
1953
|
+
return this.deserializeStreamConstructor(node);
|
|
1954
|
+
case 32 /* StreamNext */:
|
|
1955
|
+
return this.deserializeStreamNext(node);
|
|
1956
|
+
case 33 /* StreamThrow */:
|
|
1957
|
+
return this.deserializeStreamThrow(node);
|
|
1958
|
+
case 34 /* StreamReturn */:
|
|
1959
|
+
return this.deserializeStreamReturn(node);
|
|
1960
|
+
case 27 /* IteratorFactory */:
|
|
1961
|
+
return this.deserializeIteratorFactory(node);
|
|
1962
|
+
case 29 /* AsyncIteratorFactory */:
|
|
1963
|
+
return this.deserializeAsyncIteratorFactory(node);
|
|
1964
|
+
default:
|
|
1965
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
1966
|
+
}
|
|
1967
|
+
} catch (error) {
|
|
1968
|
+
throw new SerovalDeserializationError(error);
|
|
1897
1969
|
}
|
|
1898
1970
|
}
|
|
1899
1971
|
};
|
|
@@ -2164,7 +2236,18 @@ var BaseSerializerContext = class {
|
|
|
2164
2236
|
serializeProperty(source, key, val) {
|
|
2165
2237
|
if (typeof key === "string") {
|
|
2166
2238
|
const check = Number(key);
|
|
2167
|
-
const isIdentifier =
|
|
2239
|
+
const isIdentifier = (
|
|
2240
|
+
// Test if key is a valid positive number or JS identifier
|
|
2241
|
+
// so that we don't have to serialize the key and wrap with brackets
|
|
2242
|
+
check >= 0 && // It's also important to consider that if the key is
|
|
2243
|
+
// indeed numeric, we need to make sure that when
|
|
2244
|
+
// converted back into a string, it's still the same
|
|
2245
|
+
// to the original key. This allows us to differentiate
|
|
2246
|
+
// keys that has numeric formats but in a different
|
|
2247
|
+
// format, which can cause unintentional key declaration
|
|
2248
|
+
// Example: { 0x1: 1 } vs { '0x1': 1 }
|
|
2249
|
+
check.toString() === key || isValidIdentifier(key)
|
|
2250
|
+
);
|
|
2168
2251
|
if (this.isIndexedValueInStack(val)) {
|
|
2169
2252
|
const refParam = this.getRefParam(val.i);
|
|
2170
2253
|
this.markRef(source.i);
|
|
@@ -2216,7 +2299,18 @@ var BaseSerializerContext = class {
|
|
|
2216
2299
|
serializeStringKeyAssignment(source, mainAssignments, key, value) {
|
|
2217
2300
|
const serialized = this.serialize(value);
|
|
2218
2301
|
const check = Number(key);
|
|
2219
|
-
const isIdentifier =
|
|
2302
|
+
const isIdentifier = (
|
|
2303
|
+
// Test if key is a valid positive number or JS identifier
|
|
2304
|
+
// so that we don't have to serialize the key and wrap with brackets
|
|
2305
|
+
check >= 0 && // It's also important to consider that if the key is
|
|
2306
|
+
// indeed numeric, we need to make sure that when
|
|
2307
|
+
// converted back into a string, it's still the same
|
|
2308
|
+
// to the original key. This allows us to differentiate
|
|
2309
|
+
// keys that has numeric formats but in a different
|
|
2310
|
+
// format, which can cause unintentional key declaration
|
|
2311
|
+
// Example: { 0x1: 1 } vs { '0x1': 1 }
|
|
2312
|
+
check.toString() === key || isValidIdentifier(key)
|
|
2313
|
+
);
|
|
2220
2314
|
if (this.isIndexedValueInStack(value)) {
|
|
2221
2315
|
if (isIdentifier && check !== check) {
|
|
2222
2316
|
this.createObjectAssign(source.i, key, serialized);
|
|
@@ -2230,7 +2324,7 @@ var BaseSerializerContext = class {
|
|
|
2230
2324
|
} else {
|
|
2231
2325
|
const parentAssignment = this.assignments;
|
|
2232
2326
|
this.assignments = mainAssignments;
|
|
2233
|
-
if (isIdentifier) {
|
|
2327
|
+
if (isIdentifier && check !== check) {
|
|
2234
2328
|
this.createObjectAssign(source.i, key, serialized);
|
|
2235
2329
|
} else {
|
|
2236
2330
|
this.createArrayAssign(
|
|
@@ -2470,7 +2564,7 @@ var BaseSerializerContext = class {
|
|
|
2470
2564
|
}
|
|
2471
2565
|
}
|
|
2472
2566
|
}
|
|
2473
|
-
throw new
|
|
2567
|
+
throw new SerovalMissingPluginError(node.c);
|
|
2474
2568
|
}
|
|
2475
2569
|
getConstructor(node) {
|
|
2476
2570
|
const current = this.serialize(node);
|
|
@@ -2636,78 +2730,82 @@ var BaseSerializerContext = class {
|
|
|
2636
2730
|
return this.getRefParam(node.i) + ".return(" + this.serialize(node.f) + ")";
|
|
2637
2731
|
}
|
|
2638
2732
|
serialize(node) {
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2733
|
+
try {
|
|
2734
|
+
switch (node.t) {
|
|
2735
|
+
case 2 /* Constant */:
|
|
2736
|
+
return CONSTANT_STRING[node.s];
|
|
2737
|
+
case 0 /* Number */:
|
|
2738
|
+
return "" + node.s;
|
|
2739
|
+
case 1 /* String */:
|
|
2740
|
+
return '"' + node.s + '"';
|
|
2741
|
+
case 3 /* BigInt */:
|
|
2742
|
+
return node.s + "n";
|
|
2743
|
+
case 4 /* IndexedValue */:
|
|
2744
|
+
return this.getRefParam(node.i);
|
|
2745
|
+
case 18 /* Reference */:
|
|
2746
|
+
return this.serializeReference(node);
|
|
2747
|
+
case 9 /* Array */:
|
|
2748
|
+
return this.serializeArray(node);
|
|
2749
|
+
case 10 /* Object */:
|
|
2750
|
+
return this.serializeObject(node);
|
|
2751
|
+
case 11 /* NullConstructor */:
|
|
2752
|
+
return this.serializeNullConstructor(node);
|
|
2753
|
+
case 5 /* Date */:
|
|
2754
|
+
return this.serializeDate(node);
|
|
2755
|
+
case 6 /* RegExp */:
|
|
2756
|
+
return this.serializeRegExp(node);
|
|
2757
|
+
case 7 /* Set */:
|
|
2758
|
+
return this.serializeSet(node);
|
|
2759
|
+
case 8 /* Map */:
|
|
2760
|
+
return this.serializeMap(node);
|
|
2761
|
+
case 19 /* ArrayBuffer */:
|
|
2762
|
+
return this.serializeArrayBuffer(node);
|
|
2763
|
+
case 16 /* BigIntTypedArray */:
|
|
2764
|
+
case 15 /* TypedArray */:
|
|
2765
|
+
return this.serializeTypedArray(node);
|
|
2766
|
+
case 20 /* DataView */:
|
|
2767
|
+
return this.serializeDataView(node);
|
|
2768
|
+
case 14 /* AggregateError */:
|
|
2769
|
+
return this.serializeAggregateError(node);
|
|
2770
|
+
case 13 /* Error */:
|
|
2771
|
+
return this.serializeError(node);
|
|
2772
|
+
case 12 /* Promise */:
|
|
2773
|
+
return this.serializePromise(node);
|
|
2774
|
+
case 17 /* WKSymbol */:
|
|
2775
|
+
return this.serializeWellKnownSymbol(node);
|
|
2776
|
+
case 21 /* Boxed */:
|
|
2777
|
+
return this.serializeBoxed(node);
|
|
2778
|
+
case 22 /* PromiseConstructor */:
|
|
2779
|
+
return this.serializePromiseConstructor(node);
|
|
2780
|
+
case 23 /* PromiseResolve */:
|
|
2781
|
+
return this.serializePromiseResolve(node);
|
|
2782
|
+
case 24 /* PromiseReject */:
|
|
2783
|
+
return this.serializePromiseReject(node);
|
|
2784
|
+
case 25 /* Plugin */:
|
|
2785
|
+
return this.serializePlugin(node);
|
|
2786
|
+
case 26 /* SpecialReference */:
|
|
2787
|
+
return this.serializeSpecialReference(node);
|
|
2788
|
+
case 27 /* IteratorFactory */:
|
|
2789
|
+
return this.serializeIteratorFactory(node);
|
|
2790
|
+
case 28 /* IteratorFactoryInstance */:
|
|
2791
|
+
return this.serializeIteratorFactoryInstance(node);
|
|
2792
|
+
case 29 /* AsyncIteratorFactory */:
|
|
2793
|
+
return this.serializeAsyncIteratorFactory(node);
|
|
2794
|
+
case 30 /* AsyncIteratorFactoryInstance */:
|
|
2795
|
+
return this.serializeAsyncIteratorFactoryInstance(node);
|
|
2796
|
+
case 31 /* StreamConstructor */:
|
|
2797
|
+
return this.serializeStreamConstructor(node);
|
|
2798
|
+
case 32 /* StreamNext */:
|
|
2799
|
+
return this.serializeStreamNext(node);
|
|
2800
|
+
case 33 /* StreamThrow */:
|
|
2801
|
+
return this.serializeStreamThrow(node);
|
|
2802
|
+
case 34 /* StreamReturn */:
|
|
2803
|
+
return this.serializeStreamReturn(node);
|
|
2804
|
+
default:
|
|
2805
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2806
|
+
}
|
|
2807
|
+
} catch (error) {
|
|
2808
|
+
throw new SerovalSerializationError(error);
|
|
2711
2809
|
}
|
|
2712
2810
|
}
|
|
2713
2811
|
};
|
|
@@ -2769,13 +2867,13 @@ var VanillaSerializerContext = class extends BaseSerializerContext {
|
|
|
2769
2867
|
return value;
|
|
2770
2868
|
}
|
|
2771
2869
|
serializePromiseConstructor(node) {
|
|
2772
|
-
throw new
|
|
2870
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2773
2871
|
}
|
|
2774
2872
|
serializePromiseResolve(node) {
|
|
2775
|
-
throw new
|
|
2873
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2776
2874
|
}
|
|
2777
2875
|
serializePromiseReject(node) {
|
|
2778
|
-
throw new
|
|
2876
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2779
2877
|
}
|
|
2780
2878
|
serializeTop(tree) {
|
|
2781
2879
|
const result = this.serialize(tree);
|
|
@@ -3026,33 +3124,37 @@ var BaseSyncParserContext = class extends BaseParserContext {
|
|
|
3026
3124
|
if (Symbol.iterator in current || Symbol.asyncIterator in current) {
|
|
3027
3125
|
return this.parsePlainObject(id, current, !!currentClass);
|
|
3028
3126
|
}
|
|
3029
|
-
throw new
|
|
3127
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
3030
3128
|
}
|
|
3031
3129
|
parse(current) {
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3130
|
+
try {
|
|
3131
|
+
switch (typeof current) {
|
|
3132
|
+
case "boolean":
|
|
3133
|
+
return current ? TRUE_NODE : FALSE_NODE;
|
|
3134
|
+
case "undefined":
|
|
3135
|
+
return UNDEFINED_NODE;
|
|
3136
|
+
case "string":
|
|
3137
|
+
return createStringNode(current);
|
|
3138
|
+
case "number":
|
|
3139
|
+
return createNumberNode(current);
|
|
3140
|
+
case "bigint":
|
|
3141
|
+
return createBigIntNode(current);
|
|
3142
|
+
case "object": {
|
|
3143
|
+
if (current) {
|
|
3144
|
+
const ref = this.getReference(current);
|
|
3145
|
+
return ref.type === 0 ? this.parseObject(ref.value, current) : ref.value;
|
|
3146
|
+
}
|
|
3147
|
+
return NULL_NODE;
|
|
3047
3148
|
}
|
|
3048
|
-
|
|
3149
|
+
case "symbol":
|
|
3150
|
+
return this.parseWellKnownSymbol(current);
|
|
3151
|
+
case "function":
|
|
3152
|
+
return this.parseFunction(current);
|
|
3153
|
+
default:
|
|
3154
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
3049
3155
|
}
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
case "function":
|
|
3053
|
-
return this.parseFunction(current);
|
|
3054
|
-
default:
|
|
3055
|
-
throw new UnsupportedTypeError(current);
|
|
3156
|
+
} catch (error) {
|
|
3157
|
+
throw new SerovalParserError(error);
|
|
3056
3158
|
}
|
|
3057
3159
|
}
|
|
3058
3160
|
};
|
|
@@ -3644,6 +3746,17 @@ var Serializer = class {
|
|
|
3644
3746
|
export {
|
|
3645
3747
|
Feature,
|
|
3646
3748
|
Serializer,
|
|
3749
|
+
SerovalDeserializationError,
|
|
3750
|
+
SerovalError,
|
|
3751
|
+
SerovalMissingInstanceError,
|
|
3752
|
+
SerovalMissingPluginError,
|
|
3753
|
+
SerovalMissingReferenceError,
|
|
3754
|
+
SerovalMissingReferenceForIdError,
|
|
3755
|
+
SerovalParserError,
|
|
3756
|
+
SerovalSerializationError,
|
|
3757
|
+
SerovalUnknownTypedArrayError,
|
|
3758
|
+
SerovalUnsupportedNodeError,
|
|
3759
|
+
SerovalUnsupportedTypeError,
|
|
3647
3760
|
compileJSON,
|
|
3648
3761
|
createPlugin,
|
|
3649
3762
|
createReference,
|