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.
Files changed (34) hide show
  1. package/dist/cjs/development/index.cjs +339 -226
  2. package/dist/cjs/development/index.cjs.map +3 -3
  3. package/dist/cjs/production/index.cjs +17 -3
  4. package/dist/esm/development/index.mjs +339 -226
  5. package/dist/esm/development/index.mjs.map +3 -3
  6. package/dist/esm/production/index.mjs +17 -3
  7. package/dist/types/core/base-primitives.d.ts.map +1 -1
  8. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  9. package/dist/types/core/context/parser/async.d.ts.map +1 -1
  10. package/dist/types/core/context/parser/sync.d.ts.map +1 -1
  11. package/dist/types/core/context/parser.d.ts.map +1 -1
  12. package/dist/types/core/context/serializer.d.ts.map +1 -1
  13. package/dist/types/core/errors.d.ts +38 -0
  14. package/dist/types/core/errors.d.ts.map +1 -0
  15. package/dist/types/core/reference.d.ts.map +1 -1
  16. package/dist/types/core/tree/serializer.d.ts.map +1 -1
  17. package/dist/types/core/utils/typed-array.d.ts.map +1 -1
  18. package/dist/types/index.d.ts +1 -0
  19. package/dist/types/index.d.ts.map +1 -1
  20. package/package.json +2 -2
  21. package/src/core/base-primitives.ts +0 -5
  22. package/src/core/context/deserializer.ts +86 -76
  23. package/src/core/context/parser/async.ts +30 -26
  24. package/src/core/context/parser/sync.ts +30 -26
  25. package/src/core/context/parser.ts +3 -10
  26. package/src/core/context/serializer.ts +113 -80
  27. package/src/core/errors.ts +99 -0
  28. package/src/core/reference.ts +6 -2
  29. package/src/core/tree/serializer.ts +4 -3
  30. package/src/core/utils/typed-array.ts +3 -1
  31. package/src/index.ts +1 -0
  32. package/dist/types/core/UnsupportedTypeError.d.ts +0 -5
  33. package/dist/types/core/UnsupportedTypeError.d.ts.map +0 -1
  34. 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 Error("Missing reference id"));
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 Error("Missing reference for id:" + id));
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 UnsupportedTypeError(current);
1651
+ throw new SerovalUnsupportedTypeError(current);
1577
1652
  }
1578
1653
  async parse(current) {
1579
- switch (typeof current) {
1580
- case "boolean":
1581
- return current ? TRUE_NODE : FALSE_NODE;
1582
- case "undefined":
1583
- return UNDEFINED_NODE;
1584
- case "string":
1585
- return createStringNode(current);
1586
- case "number":
1587
- return createNumberNode(current);
1588
- case "bigint":
1589
- return createBigIntNode(current);
1590
- case "object": {
1591
- if (current) {
1592
- const ref = this.getReference(current);
1593
- return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
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
- return NULL_NODE;
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
- case "symbol":
1598
- return this.parseWellKnownSymbol(current);
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 Error(`Unknown TypedArray "${name}"`);
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 Error('Missing plugin for tag "' + node.c + '".');
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 Error("Missing Promise instance."));
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 Error("Missing Promise instance."));
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 Error("Missing Stream instance."));
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 Error("Missing Stream instance."));
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 Error("Missing Stream instance."));
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
- switch (node.t) {
1875
- case 2 /* Constant */:
1876
- return CONSTANT_VAL[node.s];
1877
- case 0 /* Number */:
1878
- return node.s;
1879
- case 1 /* String */:
1880
- return deserializeString(node.s);
1881
- case 3 /* BigInt */:
1882
- return BigInt(node.s);
1883
- case 4 /* IndexedValue */:
1884
- return this.refs.get(node.i);
1885
- case 18 /* Reference */:
1886
- return this.deserializeReference(node);
1887
- case 9 /* Array */:
1888
- return this.deserializeArray(node);
1889
- case 10 /* Object */:
1890
- case 11 /* NullConstructor */:
1891
- return this.deserializeObject(node);
1892
- case 5 /* Date */:
1893
- return this.deserializeDate(node);
1894
- case 6 /* RegExp */:
1895
- return this.deserializeRegExp(node);
1896
- case 7 /* Set */:
1897
- return this.deserializeSet(node);
1898
- case 8 /* Map */:
1899
- return this.deserializeMap(node);
1900
- case 19 /* ArrayBuffer */:
1901
- return this.deserializeArrayBuffer(node);
1902
- case 16 /* BigIntTypedArray */:
1903
- case 15 /* TypedArray */:
1904
- return this.deserializeTypedArray(node);
1905
- case 20 /* DataView */:
1906
- return this.deserializeDataView(node);
1907
- case 14 /* AggregateError */:
1908
- return this.deserializeAggregateError(node);
1909
- case 13 /* Error */:
1910
- return this.deserializeError(node);
1911
- case 12 /* Promise */:
1912
- return this.deserializePromise(node);
1913
- case 17 /* WKSymbol */:
1914
- return SYMBOL_REF[node.s];
1915
- case 21 /* Boxed */:
1916
- return this.deserializeBoxed(node);
1917
- case 25 /* Plugin */:
1918
- return this.deserializePlugin(node);
1919
- case 22 /* PromiseConstructor */:
1920
- return this.deserializePromiseConstructor(node);
1921
- case 23 /* PromiseResolve */:
1922
- return this.deserializePromiseResolve(node);
1923
- case 24 /* PromiseReject */:
1924
- return this.deserializePromiseReject(node);
1925
- case 28 /* IteratorFactoryInstance */:
1926
- return this.deserializeIteratorFactoryInstance(node);
1927
- case 30 /* AsyncIteratorFactoryInstance */:
1928
- return this.deserializeAsyncIteratorFactoryInstance(node);
1929
- case 31 /* StreamConstructor */:
1930
- return this.deserializeStreamConstructor(node);
1931
- case 32 /* StreamNext */:
1932
- return this.deserializeStreamNext(node);
1933
- case 33 /* StreamThrow */:
1934
- return this.deserializeStreamThrow(node);
1935
- case 34 /* StreamReturn */:
1936
- return this.deserializeStreamReturn(node);
1937
- case 27 /* IteratorFactory */:
1938
- return this.deserializeIteratorFactory(node);
1939
- case 29 /* AsyncIteratorFactory */:
1940
- return this.deserializeAsyncIteratorFactory(node);
1941
- default:
1942
- throw new Error("invariant");
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 = check >= 0 || isValidIdentifier(key);
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 = check >= 0 || isValidIdentifier(key);
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 Error('Missing plugin for tag "' + node.c + '".');
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
- switch (node.t) {
2686
- case 2 /* Constant */:
2687
- return CONSTANT_STRING[node.s];
2688
- case 0 /* Number */:
2689
- return "" + node.s;
2690
- case 1 /* String */:
2691
- return '"' + node.s + '"';
2692
- case 3 /* BigInt */:
2693
- return node.s + "n";
2694
- case 4 /* IndexedValue */:
2695
- return this.getRefParam(node.i);
2696
- case 18 /* Reference */:
2697
- return this.serializeReference(node);
2698
- case 9 /* Array */:
2699
- return this.serializeArray(node);
2700
- case 10 /* Object */:
2701
- return this.serializeObject(node);
2702
- case 11 /* NullConstructor */:
2703
- return this.serializeNullConstructor(node);
2704
- case 5 /* Date */:
2705
- return this.serializeDate(node);
2706
- case 6 /* RegExp */:
2707
- return this.serializeRegExp(node);
2708
- case 7 /* Set */:
2709
- return this.serializeSet(node);
2710
- case 8 /* Map */:
2711
- return this.serializeMap(node);
2712
- case 19 /* ArrayBuffer */:
2713
- return this.serializeArrayBuffer(node);
2714
- case 16 /* BigIntTypedArray */:
2715
- case 15 /* TypedArray */:
2716
- return this.serializeTypedArray(node);
2717
- case 20 /* DataView */:
2718
- return this.serializeDataView(node);
2719
- case 14 /* AggregateError */:
2720
- return this.serializeAggregateError(node);
2721
- case 13 /* Error */:
2722
- return this.serializeError(node);
2723
- case 12 /* Promise */:
2724
- return this.serializePromise(node);
2725
- case 17 /* WKSymbol */:
2726
- return this.serializeWellKnownSymbol(node);
2727
- case 21 /* Boxed */:
2728
- return this.serializeBoxed(node);
2729
- case 22 /* PromiseConstructor */:
2730
- return this.serializePromiseConstructor(node);
2731
- case 23 /* PromiseResolve */:
2732
- return this.serializePromiseResolve(node);
2733
- case 24 /* PromiseReject */:
2734
- return this.serializePromiseReject(node);
2735
- case 25 /* Plugin */:
2736
- return this.serializePlugin(node);
2737
- case 26 /* SpecialReference */:
2738
- return this.serializeSpecialReference(node);
2739
- case 27 /* IteratorFactory */:
2740
- return this.serializeIteratorFactory(node);
2741
- case 28 /* IteratorFactoryInstance */:
2742
- return this.serializeIteratorFactoryInstance(node);
2743
- case 29 /* AsyncIteratorFactory */:
2744
- return this.serializeAsyncIteratorFactory(node);
2745
- case 30 /* AsyncIteratorFactoryInstance */:
2746
- return this.serializeAsyncIteratorFactoryInstance(node);
2747
- case 31 /* StreamConstructor */:
2748
- return this.serializeStreamConstructor(node);
2749
- case 32 /* StreamNext */:
2750
- return this.serializeStreamNext(node);
2751
- case 33 /* StreamThrow */:
2752
- return this.serializeStreamThrow(node);
2753
- case 34 /* StreamReturn */:
2754
- return this.serializeStreamReturn(node);
2755
- default:
2756
- throw new Error("invariant");
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 Error('Unsupported node type "' + node.t + '".');
2927
+ throw new SerovalUnsupportedNodeError(node);
2819
2928
  }
2820
2929
  serializePromiseResolve(node) {
2821
- throw new Error('Unsupported node type "' + node.t + '".');
2930
+ throw new SerovalUnsupportedNodeError(node);
2822
2931
  }
2823
2932
  serializePromiseReject(node) {
2824
- throw new Error('Unsupported node type "' + node.t + '".');
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 UnsupportedTypeError(current);
3184
+ throw new SerovalUnsupportedTypeError(current);
3076
3185
  }
3077
3186
  parse(current) {
3078
- switch (typeof current) {
3079
- case "boolean":
3080
- return current ? TRUE_NODE : FALSE_NODE;
3081
- case "undefined":
3082
- return UNDEFINED_NODE;
3083
- case "string":
3084
- return createStringNode(current);
3085
- case "number":
3086
- return createNumberNode(current);
3087
- case "bigint":
3088
- return createBigIntNode(current);
3089
- case "object": {
3090
- if (current) {
3091
- const ref = this.getReference(current);
3092
- return ref.type === 0 ? this.parseObject(ref.value, current) : ref.value;
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
- return NULL_NODE;
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
- case "symbol":
3097
- return this.parseWellKnownSymbol(current);
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
  };