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
|
@@ -22,6 +22,17 @@ var src_exports = {};
|
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
Feature: () => Feature,
|
|
24
24
|
Serializer: () => Serializer,
|
|
25
|
+
SerovalDeserializationError: () => SerovalDeserializationError,
|
|
26
|
+
SerovalError: () => SerovalError,
|
|
27
|
+
SerovalMissingInstanceError: () => SerovalMissingInstanceError,
|
|
28
|
+
SerovalMissingPluginError: () => SerovalMissingPluginError,
|
|
29
|
+
SerovalMissingReferenceError: () => SerovalMissingReferenceError,
|
|
30
|
+
SerovalMissingReferenceForIdError: () => SerovalMissingReferenceForIdError,
|
|
31
|
+
SerovalParserError: () => SerovalParserError,
|
|
32
|
+
SerovalSerializationError: () => SerovalSerializationError,
|
|
33
|
+
SerovalUnknownTypedArrayError: () => SerovalUnknownTypedArrayError,
|
|
34
|
+
SerovalUnsupportedNodeError: () => SerovalUnsupportedNodeError,
|
|
35
|
+
SerovalUnsupportedTypeError: () => SerovalUnsupportedTypeError,
|
|
25
36
|
compileJSON: () => compileJSON,
|
|
26
37
|
createPlugin: () => createPlugin,
|
|
27
38
|
createReference: () => createReference,
|
|
@@ -168,11 +179,11 @@ function hasReference(id) {
|
|
|
168
179
|
return INV_REFERENCE.has(id);
|
|
169
180
|
}
|
|
170
181
|
function getReferenceID(value) {
|
|
171
|
-
assert(hasReferenceID(value), new
|
|
182
|
+
assert(hasReferenceID(value), new SerovalMissingReferenceError(value));
|
|
172
183
|
return REFERENCE.get(value);
|
|
173
184
|
}
|
|
174
185
|
function getReference(id) {
|
|
175
|
-
assert(hasReference(id), new
|
|
186
|
+
assert(hasReference(id), new SerovalMissingReferenceForIdError(id));
|
|
176
187
|
return INV_REFERENCE.get(id);
|
|
177
188
|
}
|
|
178
189
|
if (typeof globalThis !== "undefined") {
|
|
@@ -229,15 +240,6 @@ function resolvePlugins(plugins) {
|
|
|
229
240
|
return void 0;
|
|
230
241
|
}
|
|
231
242
|
|
|
232
|
-
// src/core/UnsupportedTypeError.ts
|
|
233
|
-
var { toString: objectToString } = Object.prototype;
|
|
234
|
-
var UnsupportedTypeError = class extends Error {
|
|
235
|
-
constructor(value) {
|
|
236
|
-
super('Unsupported type "' + objectToString.call(value) + '"');
|
|
237
|
-
this.value = value;
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
|
|
241
243
|
// src/core/constants.ts
|
|
242
244
|
var SYMBOL_STRING = {
|
|
243
245
|
[0 /* AsyncIterator */]: "Symbol.asyncIterator",
|
|
@@ -561,10 +563,6 @@ function createArrayBufferNode(id, current) {
|
|
|
561
563
|
};
|
|
562
564
|
}
|
|
563
565
|
function createWKSymbolNode(id, current) {
|
|
564
|
-
assert(
|
|
565
|
-
current in INV_SYMBOL_REF,
|
|
566
|
-
new Error("Only well-known symbols are supported.")
|
|
567
|
-
);
|
|
568
566
|
return {
|
|
569
567
|
t: 17 /* WKSymbol */,
|
|
570
568
|
i: id,
|
|
@@ -901,6 +899,91 @@ var SPECIAL_REFS = {
|
|
|
901
899
|
[4 /* StreamConstructor */]: {}
|
|
902
900
|
};
|
|
903
901
|
|
|
902
|
+
// src/core/errors.ts
|
|
903
|
+
var { toString: objectToString } = Object.prototype;
|
|
904
|
+
function getErrorMessage(type, cause) {
|
|
905
|
+
if (cause instanceof Error) {
|
|
906
|
+
return `Seroval caught an error during the ${type} process.
|
|
907
|
+
|
|
908
|
+
${cause.name}
|
|
909
|
+
${cause.message}
|
|
910
|
+
|
|
911
|
+
- For more information, please check the "cause" property of this error.
|
|
912
|
+
- If you believe this is an error in Seroval, please submit an issue at https://github.com/lxsmnsyc/seroval/issues/new`;
|
|
913
|
+
}
|
|
914
|
+
return `Seroval caught an error during the ${type} process.
|
|
915
|
+
|
|
916
|
+
"${objectToString.call(cause)}"
|
|
917
|
+
|
|
918
|
+
For more information, please check the "cause" property of this error.`;
|
|
919
|
+
}
|
|
920
|
+
var SerovalError = class extends Error {
|
|
921
|
+
constructor(type, cause) {
|
|
922
|
+
super(getErrorMessage(type, cause));
|
|
923
|
+
this.cause = cause;
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
var SerovalParserError = class extends SerovalError {
|
|
927
|
+
constructor(cause) {
|
|
928
|
+
super("parsing", cause);
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
var SerovalSerializationError = class extends SerovalError {
|
|
932
|
+
constructor(cause) {
|
|
933
|
+
super("serialization", cause);
|
|
934
|
+
}
|
|
935
|
+
};
|
|
936
|
+
var SerovalDeserializationError = class extends SerovalError {
|
|
937
|
+
constructor(cause) {
|
|
938
|
+
super("deserialization", cause);
|
|
939
|
+
}
|
|
940
|
+
};
|
|
941
|
+
var SerovalUnsupportedTypeError = class extends Error {
|
|
942
|
+
constructor(value) {
|
|
943
|
+
super(
|
|
944
|
+
`The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
|
|
945
|
+
|
|
946
|
+
There are few workarounds for this problem:
|
|
947
|
+
- Transform the value in a way that it can be serialized.
|
|
948
|
+
- If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.`
|
|
949
|
+
);
|
|
950
|
+
this.value = value;
|
|
951
|
+
}
|
|
952
|
+
};
|
|
953
|
+
var SerovalUnsupportedNodeError = class extends Error {
|
|
954
|
+
constructor(node) {
|
|
955
|
+
super('Unsupported node type "' + node.t + '".');
|
|
956
|
+
}
|
|
957
|
+
};
|
|
958
|
+
var SerovalMissingPluginError = class extends Error {
|
|
959
|
+
constructor(tag) {
|
|
960
|
+
super('Missing plugin for tag "' + tag + '".');
|
|
961
|
+
}
|
|
962
|
+
};
|
|
963
|
+
var SerovalMissingInstanceError = class extends Error {
|
|
964
|
+
constructor(tag) {
|
|
965
|
+
super('Missing "' + tag + '" instance.');
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
var SerovalMissingReferenceError = class extends Error {
|
|
969
|
+
constructor(value) {
|
|
970
|
+
super(
|
|
971
|
+
'Missing reference for the value "' + objectToString.call(value) + '" of type "' + typeof value + '"'
|
|
972
|
+
);
|
|
973
|
+
this.value = value;
|
|
974
|
+
}
|
|
975
|
+
};
|
|
976
|
+
var SerovalMissingReferenceForIdError = class extends Error {
|
|
977
|
+
constructor(id) {
|
|
978
|
+
super('Missing reference for id "' + serializeString(id) + '"');
|
|
979
|
+
}
|
|
980
|
+
};
|
|
981
|
+
var SerovalUnknownTypedArrayError = class extends Error {
|
|
982
|
+
constructor(name) {
|
|
983
|
+
super('Unknown TypedArray "' + name + '"');
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
|
|
904
987
|
// src/core/context/parser.ts
|
|
905
988
|
var BaseParserContext = class {
|
|
906
989
|
constructor(options) {
|
|
@@ -945,12 +1028,7 @@ var BaseParserContext = class {
|
|
|
945
1028
|
return indexed;
|
|
946
1029
|
}
|
|
947
1030
|
getStrictReference(current) {
|
|
948
|
-
assert(
|
|
949
|
-
hasReferenceID(current),
|
|
950
|
-
new Error(
|
|
951
|
-
"Cannot serialize " + typeof current + " without reference ID."
|
|
952
|
-
)
|
|
953
|
-
);
|
|
1031
|
+
assert(hasReferenceID(current), new SerovalUnsupportedTypeError(current));
|
|
954
1032
|
const result = this.getIndexedValue(current);
|
|
955
1033
|
if (result.type === 1 /* Indexed */) {
|
|
956
1034
|
return result.value;
|
|
@@ -965,10 +1043,7 @@ var BaseParserContext = class {
|
|
|
965
1043
|
if (ref.type !== 0 /* Fresh */) {
|
|
966
1044
|
return ref.value;
|
|
967
1045
|
}
|
|
968
|
-
assert(
|
|
969
|
-
current in INV_SYMBOL_REF,
|
|
970
|
-
new Error("Cannot serialized unsupported symbol.")
|
|
971
|
-
);
|
|
1046
|
+
assert(current in INV_SYMBOL_REF, new SerovalUnsupportedTypeError(current));
|
|
972
1047
|
return createWKSymbolNode(ref.value, current);
|
|
973
1048
|
}
|
|
974
1049
|
parseSpecialReference(ref) {
|
|
@@ -1573,33 +1648,37 @@ var BaseAsyncParserContext = class extends BaseParserContext {
|
|
|
1573
1648
|
if (Symbol.iterator in current || Symbol.asyncIterator in current) {
|
|
1574
1649
|
return this.parsePlainObject(id, current, !!currentClass);
|
|
1575
1650
|
}
|
|
1576
|
-
throw new
|
|
1651
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
1577
1652
|
}
|
|
1578
1653
|
async parse(current) {
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1654
|
+
try {
|
|
1655
|
+
switch (typeof current) {
|
|
1656
|
+
case "boolean":
|
|
1657
|
+
return current ? TRUE_NODE : FALSE_NODE;
|
|
1658
|
+
case "undefined":
|
|
1659
|
+
return UNDEFINED_NODE;
|
|
1660
|
+
case "string":
|
|
1661
|
+
return createStringNode(current);
|
|
1662
|
+
case "number":
|
|
1663
|
+
return createNumberNode(current);
|
|
1664
|
+
case "bigint":
|
|
1665
|
+
return createBigIntNode(current);
|
|
1666
|
+
case "object": {
|
|
1667
|
+
if (current) {
|
|
1668
|
+
const ref = this.getReference(current);
|
|
1669
|
+
return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
|
|
1670
|
+
}
|
|
1671
|
+
return NULL_NODE;
|
|
1594
1672
|
}
|
|
1595
|
-
|
|
1673
|
+
case "symbol":
|
|
1674
|
+
return this.parseWellKnownSymbol(current);
|
|
1675
|
+
case "function":
|
|
1676
|
+
return this.parseFunction(current);
|
|
1677
|
+
default:
|
|
1678
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
1596
1679
|
}
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
case "function":
|
|
1600
|
-
return this.parseFunction(current);
|
|
1601
|
-
default:
|
|
1602
|
-
throw new UnsupportedTypeError(current);
|
|
1680
|
+
} catch (error) {
|
|
1681
|
+
throw new SerovalParserError(error);
|
|
1603
1682
|
}
|
|
1604
1683
|
}
|
|
1605
1684
|
};
|
|
@@ -1638,7 +1717,7 @@ function getTypedArrayConstructor(name) {
|
|
|
1638
1717
|
case "BigUint64Array":
|
|
1639
1718
|
return BigUint64Array;
|
|
1640
1719
|
default:
|
|
1641
|
-
throw new
|
|
1720
|
+
throw new SerovalUnknownTypedArrayError(name);
|
|
1642
1721
|
}
|
|
1643
1722
|
}
|
|
1644
1723
|
|
|
@@ -1807,20 +1886,20 @@ var BaseDeserializerContext = class {
|
|
|
1807
1886
|
}
|
|
1808
1887
|
}
|
|
1809
1888
|
}
|
|
1810
|
-
throw new
|
|
1889
|
+
throw new SerovalMissingPluginError(node.c);
|
|
1811
1890
|
}
|
|
1812
1891
|
deserializePromiseConstructor(node) {
|
|
1813
1892
|
return this.assignIndexedValue(node.i, createDeferred()).promise;
|
|
1814
1893
|
}
|
|
1815
1894
|
deserializePromiseResolve(node) {
|
|
1816
1895
|
const deferred = this.refs.get(node.i);
|
|
1817
|
-
assert(deferred, new
|
|
1896
|
+
assert(deferred, new SerovalMissingInstanceError("Promise"));
|
|
1818
1897
|
deferred.resolve(this.deserialize(node.a[1]));
|
|
1819
1898
|
return void 0;
|
|
1820
1899
|
}
|
|
1821
1900
|
deserializePromiseReject(node) {
|
|
1822
1901
|
const deferred = this.refs.get(node.i);
|
|
1823
|
-
assert(deferred, new
|
|
1902
|
+
assert(deferred, new SerovalMissingInstanceError("Promise"));
|
|
1824
1903
|
deferred.reject(this.deserialize(node.a[1]));
|
|
1825
1904
|
return void 0;
|
|
1826
1905
|
}
|
|
@@ -1846,19 +1925,19 @@ var BaseDeserializerContext = class {
|
|
|
1846
1925
|
}
|
|
1847
1926
|
deserializeStreamNext(node) {
|
|
1848
1927
|
const deferred = this.refs.get(node.i);
|
|
1849
|
-
assert(deferred, new
|
|
1928
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1850
1929
|
deferred.next(this.deserialize(node.f));
|
|
1851
1930
|
return void 0;
|
|
1852
1931
|
}
|
|
1853
1932
|
deserializeStreamThrow(node) {
|
|
1854
1933
|
const deferred = this.refs.get(node.i);
|
|
1855
|
-
assert(deferred, new
|
|
1934
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1856
1935
|
deferred.throw(this.deserialize(node.f));
|
|
1857
1936
|
return void 0;
|
|
1858
1937
|
}
|
|
1859
1938
|
deserializeStreamReturn(node) {
|
|
1860
1939
|
const deferred = this.refs.get(node.i);
|
|
1861
|
-
assert(deferred, new
|
|
1940
|
+
assert(deferred, new SerovalMissingInstanceError("Stream"));
|
|
1862
1941
|
deferred.return(this.deserialize(node.f));
|
|
1863
1942
|
return void 0;
|
|
1864
1943
|
}
|
|
@@ -1871,75 +1950,79 @@ var BaseDeserializerContext = class {
|
|
|
1871
1950
|
return void 0;
|
|
1872
1951
|
}
|
|
1873
1952
|
deserialize(node) {
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1953
|
+
try {
|
|
1954
|
+
switch (node.t) {
|
|
1955
|
+
case 2 /* Constant */:
|
|
1956
|
+
return CONSTANT_VAL[node.s];
|
|
1957
|
+
case 0 /* Number */:
|
|
1958
|
+
return node.s;
|
|
1959
|
+
case 1 /* String */:
|
|
1960
|
+
return deserializeString(node.s);
|
|
1961
|
+
case 3 /* BigInt */:
|
|
1962
|
+
return BigInt(node.s);
|
|
1963
|
+
case 4 /* IndexedValue */:
|
|
1964
|
+
return this.refs.get(node.i);
|
|
1965
|
+
case 18 /* Reference */:
|
|
1966
|
+
return this.deserializeReference(node);
|
|
1967
|
+
case 9 /* Array */:
|
|
1968
|
+
return this.deserializeArray(node);
|
|
1969
|
+
case 10 /* Object */:
|
|
1970
|
+
case 11 /* NullConstructor */:
|
|
1971
|
+
return this.deserializeObject(node);
|
|
1972
|
+
case 5 /* Date */:
|
|
1973
|
+
return this.deserializeDate(node);
|
|
1974
|
+
case 6 /* RegExp */:
|
|
1975
|
+
return this.deserializeRegExp(node);
|
|
1976
|
+
case 7 /* Set */:
|
|
1977
|
+
return this.deserializeSet(node);
|
|
1978
|
+
case 8 /* Map */:
|
|
1979
|
+
return this.deserializeMap(node);
|
|
1980
|
+
case 19 /* ArrayBuffer */:
|
|
1981
|
+
return this.deserializeArrayBuffer(node);
|
|
1982
|
+
case 16 /* BigIntTypedArray */:
|
|
1983
|
+
case 15 /* TypedArray */:
|
|
1984
|
+
return this.deserializeTypedArray(node);
|
|
1985
|
+
case 20 /* DataView */:
|
|
1986
|
+
return this.deserializeDataView(node);
|
|
1987
|
+
case 14 /* AggregateError */:
|
|
1988
|
+
return this.deserializeAggregateError(node);
|
|
1989
|
+
case 13 /* Error */:
|
|
1990
|
+
return this.deserializeError(node);
|
|
1991
|
+
case 12 /* Promise */:
|
|
1992
|
+
return this.deserializePromise(node);
|
|
1993
|
+
case 17 /* WKSymbol */:
|
|
1994
|
+
return SYMBOL_REF[node.s];
|
|
1995
|
+
case 21 /* Boxed */:
|
|
1996
|
+
return this.deserializeBoxed(node);
|
|
1997
|
+
case 25 /* Plugin */:
|
|
1998
|
+
return this.deserializePlugin(node);
|
|
1999
|
+
case 22 /* PromiseConstructor */:
|
|
2000
|
+
return this.deserializePromiseConstructor(node);
|
|
2001
|
+
case 23 /* PromiseResolve */:
|
|
2002
|
+
return this.deserializePromiseResolve(node);
|
|
2003
|
+
case 24 /* PromiseReject */:
|
|
2004
|
+
return this.deserializePromiseReject(node);
|
|
2005
|
+
case 28 /* IteratorFactoryInstance */:
|
|
2006
|
+
return this.deserializeIteratorFactoryInstance(node);
|
|
2007
|
+
case 30 /* AsyncIteratorFactoryInstance */:
|
|
2008
|
+
return this.deserializeAsyncIteratorFactoryInstance(node);
|
|
2009
|
+
case 31 /* StreamConstructor */:
|
|
2010
|
+
return this.deserializeStreamConstructor(node);
|
|
2011
|
+
case 32 /* StreamNext */:
|
|
2012
|
+
return this.deserializeStreamNext(node);
|
|
2013
|
+
case 33 /* StreamThrow */:
|
|
2014
|
+
return this.deserializeStreamThrow(node);
|
|
2015
|
+
case 34 /* StreamReturn */:
|
|
2016
|
+
return this.deserializeStreamReturn(node);
|
|
2017
|
+
case 27 /* IteratorFactory */:
|
|
2018
|
+
return this.deserializeIteratorFactory(node);
|
|
2019
|
+
case 29 /* AsyncIteratorFactory */:
|
|
2020
|
+
return this.deserializeAsyncIteratorFactory(node);
|
|
2021
|
+
default:
|
|
2022
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2023
|
+
}
|
|
2024
|
+
} catch (error) {
|
|
2025
|
+
throw new SerovalDeserializationError(error);
|
|
1943
2026
|
}
|
|
1944
2027
|
}
|
|
1945
2028
|
};
|
|
@@ -2210,7 +2293,18 @@ var BaseSerializerContext = class {
|
|
|
2210
2293
|
serializeProperty(source, key, val) {
|
|
2211
2294
|
if (typeof key === "string") {
|
|
2212
2295
|
const check = Number(key);
|
|
2213
|
-
const isIdentifier =
|
|
2296
|
+
const isIdentifier = (
|
|
2297
|
+
// Test if key is a valid positive number or JS identifier
|
|
2298
|
+
// so that we don't have to serialize the key and wrap with brackets
|
|
2299
|
+
check >= 0 && // It's also important to consider that if the key is
|
|
2300
|
+
// indeed numeric, we need to make sure that when
|
|
2301
|
+
// converted back into a string, it's still the same
|
|
2302
|
+
// to the original key. This allows us to differentiate
|
|
2303
|
+
// keys that has numeric formats but in a different
|
|
2304
|
+
// format, which can cause unintentional key declaration
|
|
2305
|
+
// Example: { 0x1: 1 } vs { '0x1': 1 }
|
|
2306
|
+
check.toString() === key || isValidIdentifier(key)
|
|
2307
|
+
);
|
|
2214
2308
|
if (this.isIndexedValueInStack(val)) {
|
|
2215
2309
|
const refParam = this.getRefParam(val.i);
|
|
2216
2310
|
this.markRef(source.i);
|
|
@@ -2262,7 +2356,18 @@ var BaseSerializerContext = class {
|
|
|
2262
2356
|
serializeStringKeyAssignment(source, mainAssignments, key, value) {
|
|
2263
2357
|
const serialized = this.serialize(value);
|
|
2264
2358
|
const check = Number(key);
|
|
2265
|
-
const isIdentifier =
|
|
2359
|
+
const isIdentifier = (
|
|
2360
|
+
// Test if key is a valid positive number or JS identifier
|
|
2361
|
+
// so that we don't have to serialize the key and wrap with brackets
|
|
2362
|
+
check >= 0 && // It's also important to consider that if the key is
|
|
2363
|
+
// indeed numeric, we need to make sure that when
|
|
2364
|
+
// converted back into a string, it's still the same
|
|
2365
|
+
// to the original key. This allows us to differentiate
|
|
2366
|
+
// keys that has numeric formats but in a different
|
|
2367
|
+
// format, which can cause unintentional key declaration
|
|
2368
|
+
// Example: { 0x1: 1 } vs { '0x1': 1 }
|
|
2369
|
+
check.toString() === key || isValidIdentifier(key)
|
|
2370
|
+
);
|
|
2266
2371
|
if (this.isIndexedValueInStack(value)) {
|
|
2267
2372
|
if (isIdentifier && check !== check) {
|
|
2268
2373
|
this.createObjectAssign(source.i, key, serialized);
|
|
@@ -2276,7 +2381,7 @@ var BaseSerializerContext = class {
|
|
|
2276
2381
|
} else {
|
|
2277
2382
|
const parentAssignment = this.assignments;
|
|
2278
2383
|
this.assignments = mainAssignments;
|
|
2279
|
-
if (isIdentifier) {
|
|
2384
|
+
if (isIdentifier && check !== check) {
|
|
2280
2385
|
this.createObjectAssign(source.i, key, serialized);
|
|
2281
2386
|
} else {
|
|
2282
2387
|
this.createArrayAssign(
|
|
@@ -2516,7 +2621,7 @@ var BaseSerializerContext = class {
|
|
|
2516
2621
|
}
|
|
2517
2622
|
}
|
|
2518
2623
|
}
|
|
2519
|
-
throw new
|
|
2624
|
+
throw new SerovalMissingPluginError(node.c);
|
|
2520
2625
|
}
|
|
2521
2626
|
getConstructor(node) {
|
|
2522
2627
|
const current = this.serialize(node);
|
|
@@ -2682,78 +2787,82 @@ var BaseSerializerContext = class {
|
|
|
2682
2787
|
return this.getRefParam(node.i) + ".return(" + this.serialize(node.f) + ")";
|
|
2683
2788
|
}
|
|
2684
2789
|
serialize(node) {
|
|
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
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2790
|
+
try {
|
|
2791
|
+
switch (node.t) {
|
|
2792
|
+
case 2 /* Constant */:
|
|
2793
|
+
return CONSTANT_STRING[node.s];
|
|
2794
|
+
case 0 /* Number */:
|
|
2795
|
+
return "" + node.s;
|
|
2796
|
+
case 1 /* String */:
|
|
2797
|
+
return '"' + node.s + '"';
|
|
2798
|
+
case 3 /* BigInt */:
|
|
2799
|
+
return node.s + "n";
|
|
2800
|
+
case 4 /* IndexedValue */:
|
|
2801
|
+
return this.getRefParam(node.i);
|
|
2802
|
+
case 18 /* Reference */:
|
|
2803
|
+
return this.serializeReference(node);
|
|
2804
|
+
case 9 /* Array */:
|
|
2805
|
+
return this.serializeArray(node);
|
|
2806
|
+
case 10 /* Object */:
|
|
2807
|
+
return this.serializeObject(node);
|
|
2808
|
+
case 11 /* NullConstructor */:
|
|
2809
|
+
return this.serializeNullConstructor(node);
|
|
2810
|
+
case 5 /* Date */:
|
|
2811
|
+
return this.serializeDate(node);
|
|
2812
|
+
case 6 /* RegExp */:
|
|
2813
|
+
return this.serializeRegExp(node);
|
|
2814
|
+
case 7 /* Set */:
|
|
2815
|
+
return this.serializeSet(node);
|
|
2816
|
+
case 8 /* Map */:
|
|
2817
|
+
return this.serializeMap(node);
|
|
2818
|
+
case 19 /* ArrayBuffer */:
|
|
2819
|
+
return this.serializeArrayBuffer(node);
|
|
2820
|
+
case 16 /* BigIntTypedArray */:
|
|
2821
|
+
case 15 /* TypedArray */:
|
|
2822
|
+
return this.serializeTypedArray(node);
|
|
2823
|
+
case 20 /* DataView */:
|
|
2824
|
+
return this.serializeDataView(node);
|
|
2825
|
+
case 14 /* AggregateError */:
|
|
2826
|
+
return this.serializeAggregateError(node);
|
|
2827
|
+
case 13 /* Error */:
|
|
2828
|
+
return this.serializeError(node);
|
|
2829
|
+
case 12 /* Promise */:
|
|
2830
|
+
return this.serializePromise(node);
|
|
2831
|
+
case 17 /* WKSymbol */:
|
|
2832
|
+
return this.serializeWellKnownSymbol(node);
|
|
2833
|
+
case 21 /* Boxed */:
|
|
2834
|
+
return this.serializeBoxed(node);
|
|
2835
|
+
case 22 /* PromiseConstructor */:
|
|
2836
|
+
return this.serializePromiseConstructor(node);
|
|
2837
|
+
case 23 /* PromiseResolve */:
|
|
2838
|
+
return this.serializePromiseResolve(node);
|
|
2839
|
+
case 24 /* PromiseReject */:
|
|
2840
|
+
return this.serializePromiseReject(node);
|
|
2841
|
+
case 25 /* Plugin */:
|
|
2842
|
+
return this.serializePlugin(node);
|
|
2843
|
+
case 26 /* SpecialReference */:
|
|
2844
|
+
return this.serializeSpecialReference(node);
|
|
2845
|
+
case 27 /* IteratorFactory */:
|
|
2846
|
+
return this.serializeIteratorFactory(node);
|
|
2847
|
+
case 28 /* IteratorFactoryInstance */:
|
|
2848
|
+
return this.serializeIteratorFactoryInstance(node);
|
|
2849
|
+
case 29 /* AsyncIteratorFactory */:
|
|
2850
|
+
return this.serializeAsyncIteratorFactory(node);
|
|
2851
|
+
case 30 /* AsyncIteratorFactoryInstance */:
|
|
2852
|
+
return this.serializeAsyncIteratorFactoryInstance(node);
|
|
2853
|
+
case 31 /* StreamConstructor */:
|
|
2854
|
+
return this.serializeStreamConstructor(node);
|
|
2855
|
+
case 32 /* StreamNext */:
|
|
2856
|
+
return this.serializeStreamNext(node);
|
|
2857
|
+
case 33 /* StreamThrow */:
|
|
2858
|
+
return this.serializeStreamThrow(node);
|
|
2859
|
+
case 34 /* StreamReturn */:
|
|
2860
|
+
return this.serializeStreamReturn(node);
|
|
2861
|
+
default:
|
|
2862
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2863
|
+
}
|
|
2864
|
+
} catch (error) {
|
|
2865
|
+
throw new SerovalSerializationError(error);
|
|
2757
2866
|
}
|
|
2758
2867
|
}
|
|
2759
2868
|
};
|
|
@@ -2815,13 +2924,13 @@ var VanillaSerializerContext = class extends BaseSerializerContext {
|
|
|
2815
2924
|
return value;
|
|
2816
2925
|
}
|
|
2817
2926
|
serializePromiseConstructor(node) {
|
|
2818
|
-
throw new
|
|
2927
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2819
2928
|
}
|
|
2820
2929
|
serializePromiseResolve(node) {
|
|
2821
|
-
throw new
|
|
2930
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2822
2931
|
}
|
|
2823
2932
|
serializePromiseReject(node) {
|
|
2824
|
-
throw new
|
|
2933
|
+
throw new SerovalUnsupportedNodeError(node);
|
|
2825
2934
|
}
|
|
2826
2935
|
serializeTop(tree) {
|
|
2827
2936
|
const result = this.serialize(tree);
|
|
@@ -3072,33 +3181,37 @@ var BaseSyncParserContext = class extends BaseParserContext {
|
|
|
3072
3181
|
if (Symbol.iterator in current || Symbol.asyncIterator in current) {
|
|
3073
3182
|
return this.parsePlainObject(id, current, !!currentClass);
|
|
3074
3183
|
}
|
|
3075
|
-
throw new
|
|
3184
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
3076
3185
|
}
|
|
3077
3186
|
parse(current) {
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3187
|
+
try {
|
|
3188
|
+
switch (typeof current) {
|
|
3189
|
+
case "boolean":
|
|
3190
|
+
return current ? TRUE_NODE : FALSE_NODE;
|
|
3191
|
+
case "undefined":
|
|
3192
|
+
return UNDEFINED_NODE;
|
|
3193
|
+
case "string":
|
|
3194
|
+
return createStringNode(current);
|
|
3195
|
+
case "number":
|
|
3196
|
+
return createNumberNode(current);
|
|
3197
|
+
case "bigint":
|
|
3198
|
+
return createBigIntNode(current);
|
|
3199
|
+
case "object": {
|
|
3200
|
+
if (current) {
|
|
3201
|
+
const ref = this.getReference(current);
|
|
3202
|
+
return ref.type === 0 ? this.parseObject(ref.value, current) : ref.value;
|
|
3203
|
+
}
|
|
3204
|
+
return NULL_NODE;
|
|
3093
3205
|
}
|
|
3094
|
-
|
|
3206
|
+
case "symbol":
|
|
3207
|
+
return this.parseWellKnownSymbol(current);
|
|
3208
|
+
case "function":
|
|
3209
|
+
return this.parseFunction(current);
|
|
3210
|
+
default:
|
|
3211
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
3095
3212
|
}
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
case "function":
|
|
3099
|
-
return this.parseFunction(current);
|
|
3100
|
-
default:
|
|
3101
|
-
throw new UnsupportedTypeError(current);
|
|
3213
|
+
} catch (error) {
|
|
3214
|
+
throw new SerovalParserError(error);
|
|
3102
3215
|
}
|
|
3103
3216
|
}
|
|
3104
3217
|
};
|