seroval 1.0.3 → 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 +330 -225
  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 +330 -225
  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 +104 -95
  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
  };
@@ -2167,7 +2239,14 @@ var BaseSerializerContext = class {
2167
2239
  const isIdentifier = (
2168
2240
  // Test if key is a valid positive number or JS identifier
2169
2241
  // so that we don't have to serialize the key and wrap with brackets
2170
- check >= 0 && check.toString() === key || isValidIdentifier(key)
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)
2171
2250
  );
2172
2251
  if (this.isIndexedValueInStack(val)) {
2173
2252
  const refParam = this.getRefParam(val.i);
@@ -2223,7 +2302,14 @@ var BaseSerializerContext = class {
2223
2302
  const isIdentifier = (
2224
2303
  // Test if key is a valid positive number or JS identifier
2225
2304
  // so that we don't have to serialize the key and wrap with brackets
2226
- check >= 0 && check.toString() === key || isValidIdentifier(key)
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)
2227
2313
  );
2228
2314
  if (this.isIndexedValueInStack(value)) {
2229
2315
  if (isIdentifier && check !== check) {
@@ -2478,7 +2564,7 @@ var BaseSerializerContext = class {
2478
2564
  }
2479
2565
  }
2480
2566
  }
2481
- throw new Error('Missing plugin for tag "' + node.c + '".');
2567
+ throw new SerovalMissingPluginError(node.c);
2482
2568
  }
2483
2569
  getConstructor(node) {
2484
2570
  const current = this.serialize(node);
@@ -2644,78 +2730,82 @@ var BaseSerializerContext = class {
2644
2730
  return this.getRefParam(node.i) + ".return(" + this.serialize(node.f) + ")";
2645
2731
  }
2646
2732
  serialize(node) {
2647
- switch (node.t) {
2648
- case 2 /* Constant */:
2649
- return CONSTANT_STRING[node.s];
2650
- case 0 /* Number */:
2651
- return "" + node.s;
2652
- case 1 /* String */:
2653
- return '"' + node.s + '"';
2654
- case 3 /* BigInt */:
2655
- return node.s + "n";
2656
- case 4 /* IndexedValue */:
2657
- return this.getRefParam(node.i);
2658
- case 18 /* Reference */:
2659
- return this.serializeReference(node);
2660
- case 9 /* Array */:
2661
- return this.serializeArray(node);
2662
- case 10 /* Object */:
2663
- return this.serializeObject(node);
2664
- case 11 /* NullConstructor */:
2665
- return this.serializeNullConstructor(node);
2666
- case 5 /* Date */:
2667
- return this.serializeDate(node);
2668
- case 6 /* RegExp */:
2669
- return this.serializeRegExp(node);
2670
- case 7 /* Set */:
2671
- return this.serializeSet(node);
2672
- case 8 /* Map */:
2673
- return this.serializeMap(node);
2674
- case 19 /* ArrayBuffer */:
2675
- return this.serializeArrayBuffer(node);
2676
- case 16 /* BigIntTypedArray */:
2677
- case 15 /* TypedArray */:
2678
- return this.serializeTypedArray(node);
2679
- case 20 /* DataView */:
2680
- return this.serializeDataView(node);
2681
- case 14 /* AggregateError */:
2682
- return this.serializeAggregateError(node);
2683
- case 13 /* Error */:
2684
- return this.serializeError(node);
2685
- case 12 /* Promise */:
2686
- return this.serializePromise(node);
2687
- case 17 /* WKSymbol */:
2688
- return this.serializeWellKnownSymbol(node);
2689
- case 21 /* Boxed */:
2690
- return this.serializeBoxed(node);
2691
- case 22 /* PromiseConstructor */:
2692
- return this.serializePromiseConstructor(node);
2693
- case 23 /* PromiseResolve */:
2694
- return this.serializePromiseResolve(node);
2695
- case 24 /* PromiseReject */:
2696
- return this.serializePromiseReject(node);
2697
- case 25 /* Plugin */:
2698
- return this.serializePlugin(node);
2699
- case 26 /* SpecialReference */:
2700
- return this.serializeSpecialReference(node);
2701
- case 27 /* IteratorFactory */:
2702
- return this.serializeIteratorFactory(node);
2703
- case 28 /* IteratorFactoryInstance */:
2704
- return this.serializeIteratorFactoryInstance(node);
2705
- case 29 /* AsyncIteratorFactory */:
2706
- return this.serializeAsyncIteratorFactory(node);
2707
- case 30 /* AsyncIteratorFactoryInstance */:
2708
- return this.serializeAsyncIteratorFactoryInstance(node);
2709
- case 31 /* StreamConstructor */:
2710
- return this.serializeStreamConstructor(node);
2711
- case 32 /* StreamNext */:
2712
- return this.serializeStreamNext(node);
2713
- case 33 /* StreamThrow */:
2714
- return this.serializeStreamThrow(node);
2715
- case 34 /* StreamReturn */:
2716
- return this.serializeStreamReturn(node);
2717
- default:
2718
- 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);
2719
2809
  }
2720
2810
  }
2721
2811
  };
@@ -2777,13 +2867,13 @@ var VanillaSerializerContext = class extends BaseSerializerContext {
2777
2867
  return value;
2778
2868
  }
2779
2869
  serializePromiseConstructor(node) {
2780
- throw new Error('Unsupported node type "' + node.t + '".');
2870
+ throw new SerovalUnsupportedNodeError(node);
2781
2871
  }
2782
2872
  serializePromiseResolve(node) {
2783
- throw new Error('Unsupported node type "' + node.t + '".');
2873
+ throw new SerovalUnsupportedNodeError(node);
2784
2874
  }
2785
2875
  serializePromiseReject(node) {
2786
- throw new Error('Unsupported node type "' + node.t + '".');
2876
+ throw new SerovalUnsupportedNodeError(node);
2787
2877
  }
2788
2878
  serializeTop(tree) {
2789
2879
  const result = this.serialize(tree);
@@ -3034,33 +3124,37 @@ var BaseSyncParserContext = class extends BaseParserContext {
3034
3124
  if (Symbol.iterator in current || Symbol.asyncIterator in current) {
3035
3125
  return this.parsePlainObject(id, current, !!currentClass);
3036
3126
  }
3037
- throw new UnsupportedTypeError(current);
3127
+ throw new SerovalUnsupportedTypeError(current);
3038
3128
  }
3039
3129
  parse(current) {
3040
- switch (typeof current) {
3041
- case "boolean":
3042
- return current ? TRUE_NODE : FALSE_NODE;
3043
- case "undefined":
3044
- return UNDEFINED_NODE;
3045
- case "string":
3046
- return createStringNode(current);
3047
- case "number":
3048
- return createNumberNode(current);
3049
- case "bigint":
3050
- return createBigIntNode(current);
3051
- case "object": {
3052
- if (current) {
3053
- const ref = this.getReference(current);
3054
- 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;
3055
3148
  }
3056
- 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);
3057
3155
  }
3058
- case "symbol":
3059
- return this.parseWellKnownSymbol(current);
3060
- case "function":
3061
- return this.parseFunction(current);
3062
- default:
3063
- throw new UnsupportedTypeError(current);
3156
+ } catch (error) {
3157
+ throw new SerovalParserError(error);
3064
3158
  }
3065
3159
  }
3066
3160
  };
@@ -3652,6 +3746,17 @@ var Serializer = class {
3652
3746
  export {
3653
3747
  Feature,
3654
3748
  Serializer,
3749
+ SerovalDeserializationError,
3750
+ SerovalError,
3751
+ SerovalMissingInstanceError,
3752
+ SerovalMissingPluginError,
3753
+ SerovalMissingReferenceError,
3754
+ SerovalMissingReferenceForIdError,
3755
+ SerovalParserError,
3756
+ SerovalSerializationError,
3757
+ SerovalUnknownTypedArrayError,
3758
+ SerovalUnsupportedNodeError,
3759
+ SerovalUnsupportedTypeError,
3655
3760
  compileJSON,
3656
3761
  createPlugin,
3657
3762
  createReference,