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
@@ -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 Error("Missing reference id"));
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 Error("Missing reference for id:" + id));
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 UnsupportedTypeError(current);
1594
+ throw new SerovalUnsupportedTypeError(current);
1531
1595
  }
1532
1596
  async parse(current) {
1533
- switch (typeof current) {
1534
- case "boolean":
1535
- return current ? TRUE_NODE : FALSE_NODE;
1536
- case "undefined":
1537
- return UNDEFINED_NODE;
1538
- case "string":
1539
- return createStringNode(current);
1540
- case "number":
1541
- return createNumberNode(current);
1542
- case "bigint":
1543
- return createBigIntNode(current);
1544
- case "object": {
1545
- if (current) {
1546
- const ref = this.getReference(current);
1547
- return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
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
- return NULL_NODE;
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
- case "symbol":
1552
- return this.parseWellKnownSymbol(current);
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 Error(`Unknown TypedArray "${name}"`);
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 Error('Missing plugin for tag "' + node.c + '".');
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 Error("Missing Promise instance."));
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 Error("Missing Promise instance."));
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 Error("Missing Stream instance."));
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 Error("Missing Stream instance."));
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 Error("Missing Stream instance."));
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
- switch (node.t) {
1829
- case 2 /* Constant */:
1830
- return CONSTANT_VAL[node.s];
1831
- case 0 /* Number */:
1832
- return node.s;
1833
- case 1 /* String */:
1834
- return deserializeString(node.s);
1835
- case 3 /* BigInt */:
1836
- return BigInt(node.s);
1837
- case 4 /* IndexedValue */:
1838
- return this.refs.get(node.i);
1839
- case 18 /* Reference */:
1840
- return this.deserializeReference(node);
1841
- case 9 /* Array */:
1842
- return this.deserializeArray(node);
1843
- case 10 /* Object */:
1844
- case 11 /* NullConstructor */:
1845
- return this.deserializeObject(node);
1846
- case 5 /* Date */:
1847
- return this.deserializeDate(node);
1848
- case 6 /* RegExp */:
1849
- return this.deserializeRegExp(node);
1850
- case 7 /* Set */:
1851
- return this.deserializeSet(node);
1852
- case 8 /* Map */:
1853
- return this.deserializeMap(node);
1854
- case 19 /* ArrayBuffer */:
1855
- return this.deserializeArrayBuffer(node);
1856
- case 16 /* BigIntTypedArray */:
1857
- case 15 /* TypedArray */:
1858
- return this.deserializeTypedArray(node);
1859
- case 20 /* DataView */:
1860
- return this.deserializeDataView(node);
1861
- case 14 /* AggregateError */:
1862
- return this.deserializeAggregateError(node);
1863
- case 13 /* Error */:
1864
- return this.deserializeError(node);
1865
- case 12 /* Promise */:
1866
- return this.deserializePromise(node);
1867
- case 17 /* WKSymbol */:
1868
- return SYMBOL_REF[node.s];
1869
- case 21 /* Boxed */:
1870
- return this.deserializeBoxed(node);
1871
- case 25 /* Plugin */:
1872
- return this.deserializePlugin(node);
1873
- case 22 /* PromiseConstructor */:
1874
- return this.deserializePromiseConstructor(node);
1875
- case 23 /* PromiseResolve */:
1876
- return this.deserializePromiseResolve(node);
1877
- case 24 /* PromiseReject */:
1878
- return this.deserializePromiseReject(node);
1879
- case 28 /* IteratorFactoryInstance */:
1880
- return this.deserializeIteratorFactoryInstance(node);
1881
- case 30 /* AsyncIteratorFactoryInstance */:
1882
- return this.deserializeAsyncIteratorFactoryInstance(node);
1883
- case 31 /* StreamConstructor */:
1884
- return this.deserializeStreamConstructor(node);
1885
- case 32 /* StreamNext */:
1886
- return this.deserializeStreamNext(node);
1887
- case 33 /* StreamThrow */:
1888
- return this.deserializeStreamThrow(node);
1889
- case 34 /* StreamReturn */:
1890
- return this.deserializeStreamReturn(node);
1891
- case 27 /* IteratorFactory */:
1892
- return this.deserializeIteratorFactory(node);
1893
- case 29 /* AsyncIteratorFactory */:
1894
- return this.deserializeAsyncIteratorFactory(node);
1895
- default:
1896
- throw new Error("invariant");
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 = check >= 0 || isValidIdentifier(key);
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 = check >= 0 || isValidIdentifier(key);
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 Error('Missing plugin for tag "' + node.c + '".');
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
- switch (node.t) {
2640
- case 2 /* Constant */:
2641
- return CONSTANT_STRING[node.s];
2642
- case 0 /* Number */:
2643
- return "" + node.s;
2644
- case 1 /* String */:
2645
- return '"' + node.s + '"';
2646
- case 3 /* BigInt */:
2647
- return node.s + "n";
2648
- case 4 /* IndexedValue */:
2649
- return this.getRefParam(node.i);
2650
- case 18 /* Reference */:
2651
- return this.serializeReference(node);
2652
- case 9 /* Array */:
2653
- return this.serializeArray(node);
2654
- case 10 /* Object */:
2655
- return this.serializeObject(node);
2656
- case 11 /* NullConstructor */:
2657
- return this.serializeNullConstructor(node);
2658
- case 5 /* Date */:
2659
- return this.serializeDate(node);
2660
- case 6 /* RegExp */:
2661
- return this.serializeRegExp(node);
2662
- case 7 /* Set */:
2663
- return this.serializeSet(node);
2664
- case 8 /* Map */:
2665
- return this.serializeMap(node);
2666
- case 19 /* ArrayBuffer */:
2667
- return this.serializeArrayBuffer(node);
2668
- case 16 /* BigIntTypedArray */:
2669
- case 15 /* TypedArray */:
2670
- return this.serializeTypedArray(node);
2671
- case 20 /* DataView */:
2672
- return this.serializeDataView(node);
2673
- case 14 /* AggregateError */:
2674
- return this.serializeAggregateError(node);
2675
- case 13 /* Error */:
2676
- return this.serializeError(node);
2677
- case 12 /* Promise */:
2678
- return this.serializePromise(node);
2679
- case 17 /* WKSymbol */:
2680
- return this.serializeWellKnownSymbol(node);
2681
- case 21 /* Boxed */:
2682
- return this.serializeBoxed(node);
2683
- case 22 /* PromiseConstructor */:
2684
- return this.serializePromiseConstructor(node);
2685
- case 23 /* PromiseResolve */:
2686
- return this.serializePromiseResolve(node);
2687
- case 24 /* PromiseReject */:
2688
- return this.serializePromiseReject(node);
2689
- case 25 /* Plugin */:
2690
- return this.serializePlugin(node);
2691
- case 26 /* SpecialReference */:
2692
- return this.serializeSpecialReference(node);
2693
- case 27 /* IteratorFactory */:
2694
- return this.serializeIteratorFactory(node);
2695
- case 28 /* IteratorFactoryInstance */:
2696
- return this.serializeIteratorFactoryInstance(node);
2697
- case 29 /* AsyncIteratorFactory */:
2698
- return this.serializeAsyncIteratorFactory(node);
2699
- case 30 /* AsyncIteratorFactoryInstance */:
2700
- return this.serializeAsyncIteratorFactoryInstance(node);
2701
- case 31 /* StreamConstructor */:
2702
- return this.serializeStreamConstructor(node);
2703
- case 32 /* StreamNext */:
2704
- return this.serializeStreamNext(node);
2705
- case 33 /* StreamThrow */:
2706
- return this.serializeStreamThrow(node);
2707
- case 34 /* StreamReturn */:
2708
- return this.serializeStreamReturn(node);
2709
- default:
2710
- throw new Error("invariant");
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 Error('Unsupported node type "' + node.t + '".');
2870
+ throw new SerovalUnsupportedNodeError(node);
2773
2871
  }
2774
2872
  serializePromiseResolve(node) {
2775
- throw new Error('Unsupported node type "' + node.t + '".');
2873
+ throw new SerovalUnsupportedNodeError(node);
2776
2874
  }
2777
2875
  serializePromiseReject(node) {
2778
- throw new Error('Unsupported node type "' + node.t + '".');
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 UnsupportedTypeError(current);
3127
+ throw new SerovalUnsupportedTypeError(current);
3030
3128
  }
3031
3129
  parse(current) {
3032
- switch (typeof current) {
3033
- case "boolean":
3034
- return current ? TRUE_NODE : FALSE_NODE;
3035
- case "undefined":
3036
- return UNDEFINED_NODE;
3037
- case "string":
3038
- return createStringNode(current);
3039
- case "number":
3040
- return createNumberNode(current);
3041
- case "bigint":
3042
- return createBigIntNode(current);
3043
- case "object": {
3044
- if (current) {
3045
- const ref = this.getReference(current);
3046
- return ref.type === 0 ? this.parseObject(ref.value, current) : ref.value;
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
- return NULL_NODE;
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
- case "symbol":
3051
- return this.parseWellKnownSymbol(current);
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,