seroval 1.0.3 → 1.0.5

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 (71) hide show
  1. package/dist/cjs/development/index.cjs +556 -451
  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 +556 -451
  5. package/dist/esm/development/index.mjs.map +3 -3
  6. package/dist/esm/production/index.mjs +17 -3
  7. package/dist/types/core/Serializer.d.ts.map +1 -1
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/constants.d.ts +4 -4
  10. package/dist/types/core/constants.d.ts.map +1 -1
  11. package/dist/types/core/context/deserializer.d.ts +1 -1
  12. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  13. package/dist/types/core/context/parser/async.d.ts +1 -1
  14. package/dist/types/core/context/parser/async.d.ts.map +1 -1
  15. package/dist/types/core/context/parser/stream.d.ts +2 -2
  16. package/dist/types/core/context/parser/stream.d.ts.map +1 -1
  17. package/dist/types/core/context/parser/sync.d.ts +3 -3
  18. package/dist/types/core/context/parser/sync.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts.map +1 -1
  20. package/dist/types/core/context/serializer.d.ts +1 -1
  21. package/dist/types/core/context/serializer.d.ts.map +1 -1
  22. package/dist/types/core/cross/async.d.ts +1 -1
  23. package/dist/types/core/cross/async.d.ts.map +1 -1
  24. package/dist/types/core/cross/deserializer.d.ts +1 -1
  25. package/dist/types/core/cross/deserializer.d.ts.map +1 -1
  26. package/dist/types/core/cross/serializer.d.ts +1 -1
  27. package/dist/types/core/cross/serializer.d.ts.map +1 -1
  28. package/dist/types/core/errors.d.ts +38 -0
  29. package/dist/types/core/errors.d.ts.map +1 -0
  30. package/dist/types/core/plugin.d.ts +1 -1
  31. package/dist/types/core/plugin.d.ts.map +1 -1
  32. package/dist/types/core/reference.d.ts.map +1 -1
  33. package/dist/types/core/tree/async.d.ts +1 -1
  34. package/dist/types/core/tree/async.d.ts.map +1 -1
  35. package/dist/types/core/tree/deserializer.d.ts +1 -1
  36. package/dist/types/core/tree/deserializer.d.ts.map +1 -1
  37. package/dist/types/core/tree/index.d.ts.map +1 -1
  38. package/dist/types/core/tree/serializer.d.ts +1 -1
  39. package/dist/types/core/tree/serializer.d.ts.map +1 -1
  40. package/dist/types/core/types.d.ts +1 -1
  41. package/dist/types/core/types.d.ts.map +1 -1
  42. package/dist/types/core/utils/typed-array.d.ts.map +1 -1
  43. package/dist/types/index.d.ts +1 -0
  44. package/dist/types/index.d.ts.map +1 -1
  45. package/package.json +4 -5
  46. package/src/core/Serializer.ts +5 -5
  47. package/src/core/base-primitives.ts +4 -9
  48. package/src/core/constants.ts +12 -12
  49. package/src/core/context/deserializer.ts +100 -90
  50. package/src/core/context/parser/async.ts +56 -52
  51. package/src/core/context/parser/stream.ts +5 -5
  52. package/src/core/context/parser/sync.ts +45 -41
  53. package/src/core/context/parser.ts +5 -12
  54. package/src/core/context/serializer.ts +127 -118
  55. package/src/core/cross/async.ts +1 -1
  56. package/src/core/cross/deserializer.ts +1 -1
  57. package/src/core/cross/serializer.ts +2 -2
  58. package/src/core/errors.ts +102 -0
  59. package/src/core/literals.ts +4 -4
  60. package/src/core/plugin.ts +1 -1
  61. package/src/core/reference.ts +7 -3
  62. package/src/core/tree/async.ts +1 -1
  63. package/src/core/tree/deserializer.ts +1 -1
  64. package/src/core/tree/index.ts +8 -2
  65. package/src/core/tree/serializer.ts +8 -7
  66. package/src/core/types.ts +2 -2
  67. package/src/core/utils/typed-array.ts +3 -1
  68. package/src/index.ts +1 -0
  69. package/dist/types/core/UnsupportedTypeError.d.ts +0 -5
  70. package/dist/types/core/UnsupportedTypeError.d.ts.map +0 -1
  71. package/src/core/UnsupportedTypeError.ts +0 -7
@@ -6,45 +6,50 @@ import {
6
6
  SerovalNodeType,
7
7
  SerovalObjectFlags,
8
8
  } from '../constants';
9
+ import {
10
+ SerovalMissingPluginError,
11
+ SerovalSerializationError,
12
+ SerovalUnsupportedNodeError,
13
+ } from '../errors';
9
14
  import { REFERENCES_KEY } from '../keys';
10
15
  import type { Plugin, PluginAccessOptions, SerovalMode } from '../plugin';
11
16
  import { SpecialReference } from '../special-reference';
12
17
  import type {
18
+ SerovalAggregateErrorNode,
19
+ SerovalArrayBufferNode,
13
20
  SerovalArrayNode,
21
+ SerovalAsyncIteratorFactoryInstanceNode,
22
+ SerovalAsyncIteratorFactoryNode,
23
+ SerovalBigIntTypedArrayNode,
24
+ SerovalBoxedNode,
25
+ SerovalDataViewNode,
26
+ SerovalDateNode,
27
+ SerovalErrorNode,
14
28
  SerovalIndexedValueNode,
29
+ SerovalIteratorFactoryInstanceNode,
30
+ SerovalIteratorFactoryNode,
31
+ SerovalMapNode,
15
32
  SerovalNode,
33
+ SerovalNodeWithID,
34
+ SerovalNullConstructorNode,
35
+ SerovalObjectNode,
16
36
  SerovalObjectRecordKey,
17
37
  SerovalObjectRecordNode,
18
- SerovalReferenceNode,
19
- SerovalObjectNode,
20
- SerovalNullConstructorNode,
21
- SerovalRegExpNode,
22
- SerovalDateNode,
23
- SerovalSetNode,
24
- SerovalMapNode,
25
- SerovalArrayBufferNode,
26
- SerovalTypedArrayNode,
27
- SerovalBigIntTypedArrayNode,
28
- SerovalDataViewNode,
29
- SerovalAggregateErrorNode,
30
- SerovalErrorNode,
31
- SerovalPromiseNode,
32
- SerovalWKSymbolNode,
33
- SerovalBoxedNode,
34
38
  SerovalPluginNode,
35
39
  SerovalPromiseConstructorNode,
36
- SerovalPromiseResolveNode,
40
+ SerovalPromiseNode,
37
41
  SerovalPromiseRejectNode,
38
- SerovalIteratorFactoryInstanceNode,
39
- SerovalIteratorFactoryNode,
40
- SerovalAsyncIteratorFactoryInstanceNode,
41
- SerovalAsyncIteratorFactoryNode,
42
+ SerovalPromiseResolveNode,
43
+ SerovalReferenceNode,
44
+ SerovalRegExpNode,
45
+ SerovalSetNode,
42
46
  SerovalSpecialReferenceNode,
43
- SerovalNodeWithID,
44
47
  SerovalStreamConstructorNode,
45
48
  SerovalStreamNextNode,
46
- SerovalStreamThrowNode,
47
49
  SerovalStreamReturnNode,
50
+ SerovalStreamThrowNode,
51
+ SerovalTypedArrayNode,
52
+ SerovalWKSymbolNode,
48
53
  } from '../types';
49
54
  import { isValidIdentifier } from '../utils/is-valid-identifier';
50
55
 
@@ -446,19 +451,19 @@ export default abstract class BaseSerializerContext
446
451
  ): string {
447
452
  if (typeof key === 'string') {
448
453
  const check = Number(key);
449
- const isIdentifier = (
454
+ const isIdentifier =
450
455
  // Test if key is a valid positive number or JS identifier
451
456
  // so that we don't have to serialize the key and wrap with brackets
452
- check >= 0
453
- // It's also important to consider that if the key is
454
- // indeed numeric, we need to make sure that when
455
- // converted back into a string, it's still the same
456
- // to the original key. This allows us to differentiate
457
- // keys that has numeric formats but in a different
458
- // format, which can cause unintentional key declaration
459
- // Example: { 0x1: 1 } vs { '0x1': 1 }
460
- && check.toString() === key
461
- ) || isValidIdentifier(key);
457
+ (check >= 0 &&
458
+ // It's also important to consider that if the key is
459
+ // indeed numeric, we need to make sure that when
460
+ // converted back into a string, it's still the same
461
+ // to the original key. This allows us to differentiate
462
+ // keys that has numeric formats but in a different
463
+ // format, which can cause unintentional key declaration
464
+ // Example: { 0x1: 1 } vs { '0x1': 1 }
465
+ check.toString() === key) ||
466
+ isValidIdentifier(key);
462
467
  if (this.isIndexedValueInStack(val)) {
463
468
  const refParam = this.getRefParam((val as SerovalIndexedValueNode).i);
464
469
  this.markRef(source.i);
@@ -528,19 +533,19 @@ export default abstract class BaseSerializerContext
528
533
  ): void {
529
534
  const serialized = this.serialize(value);
530
535
  const check = Number(key);
531
- const isIdentifier = (
536
+ const isIdentifier =
532
537
  // Test if key is a valid positive number or JS identifier
533
538
  // so that we don't have to serialize the key and wrap with brackets
534
- check >= 0
535
- // It's also important to consider that if the key is
536
- // indeed numeric, we need to make sure that when
537
- // converted back into a string, it's still the same
538
- // to the original key. This allows us to differentiate
539
- // keys that has numeric formats but in a different
540
- // format, which can cause unintentional key declaration
541
- // Example: { 0x1: 1 } vs { '0x1': 1 }
542
- && check.toString() === key
543
- ) || isValidIdentifier(key);
539
+ (check >= 0 &&
540
+ // It's also important to consider that if the key is
541
+ // indeed numeric, we need to make sure that when
542
+ // converted back into a string, it's still the same
543
+ // to the original key. This allows us to differentiate
544
+ // keys that has numeric formats but in a different
545
+ // format, which can cause unintentional key declaration
546
+ // Example: { 0x1: 1 } vs { '0x1': 1 }
547
+ check.toString() === key) ||
548
+ isValidIdentifier(key);
544
549
  if (this.isIndexedValueInStack(value)) {
545
550
  // Strict identifier check, make sure
546
551
  // that it isn't numeric (except NaN)
@@ -899,7 +904,7 @@ export default abstract class BaseSerializerContext
899
904
  }
900
905
  }
901
906
  }
902
- throw new Error('Missing plugin for tag "' + node.c + '".');
907
+ throw new SerovalMissingPluginError(node.c);
903
908
  }
904
909
 
905
910
  private getConstructor(node: SerovalNodeWithID): string {
@@ -1155,78 +1160,82 @@ export default abstract class BaseSerializerContext
1155
1160
  }
1156
1161
 
1157
1162
  serialize(node: SerovalNode): string {
1158
- switch (node.t) {
1159
- case SerovalNodeType.Constant:
1160
- return CONSTANT_STRING[node.s];
1161
- case SerovalNodeType.Number:
1162
- return '' + node.s;
1163
- case SerovalNodeType.String:
1164
- return '"' + node.s + '"';
1165
- case SerovalNodeType.BigInt:
1166
- return node.s + 'n';
1167
- case SerovalNodeType.IndexedValue:
1168
- return this.getRefParam(node.i);
1169
- case SerovalNodeType.Reference:
1170
- return this.serializeReference(node);
1171
- case SerovalNodeType.Array:
1172
- return this.serializeArray(node);
1173
- case SerovalNodeType.Object:
1174
- return this.serializeObject(node);
1175
- case SerovalNodeType.NullConstructor:
1176
- return this.serializeNullConstructor(node);
1177
- case SerovalNodeType.Date:
1178
- return this.serializeDate(node);
1179
- case SerovalNodeType.RegExp:
1180
- return this.serializeRegExp(node);
1181
- case SerovalNodeType.Set:
1182
- return this.serializeSet(node);
1183
- case SerovalNodeType.Map:
1184
- return this.serializeMap(node);
1185
- case SerovalNodeType.ArrayBuffer:
1186
- return this.serializeArrayBuffer(node);
1187
- case SerovalNodeType.BigIntTypedArray:
1188
- case SerovalNodeType.TypedArray:
1189
- return this.serializeTypedArray(node);
1190
- case SerovalNodeType.DataView:
1191
- return this.serializeDataView(node);
1192
- case SerovalNodeType.AggregateError:
1193
- return this.serializeAggregateError(node);
1194
- case SerovalNodeType.Error:
1195
- return this.serializeError(node);
1196
- case SerovalNodeType.Promise:
1197
- return this.serializePromise(node);
1198
- case SerovalNodeType.WKSymbol:
1199
- return this.serializeWellKnownSymbol(node);
1200
- case SerovalNodeType.Boxed:
1201
- return this.serializeBoxed(node);
1202
- case SerovalNodeType.PromiseConstructor:
1203
- return this.serializePromiseConstructor(node);
1204
- case SerovalNodeType.PromiseResolve:
1205
- return this.serializePromiseResolve(node);
1206
- case SerovalNodeType.PromiseReject:
1207
- return this.serializePromiseReject(node);
1208
- case SerovalNodeType.Plugin:
1209
- return this.serializePlugin(node);
1210
- case SerovalNodeType.SpecialReference:
1211
- return this.serializeSpecialReference(node);
1212
- case SerovalNodeType.IteratorFactory:
1213
- return this.serializeIteratorFactory(node);
1214
- case SerovalNodeType.IteratorFactoryInstance:
1215
- return this.serializeIteratorFactoryInstance(node);
1216
- case SerovalNodeType.AsyncIteratorFactory:
1217
- return this.serializeAsyncIteratorFactory(node);
1218
- case SerovalNodeType.AsyncIteratorFactoryInstance:
1219
- return this.serializeAsyncIteratorFactoryInstance(node);
1220
- case SerovalNodeType.StreamConstructor:
1221
- return this.serializeStreamConstructor(node);
1222
- case SerovalNodeType.StreamNext:
1223
- return this.serializeStreamNext(node);
1224
- case SerovalNodeType.StreamThrow:
1225
- return this.serializeStreamThrow(node);
1226
- case SerovalNodeType.StreamReturn:
1227
- return this.serializeStreamReturn(node);
1228
- default:
1229
- throw new Error('invariant');
1163
+ try {
1164
+ switch (node.t) {
1165
+ case SerovalNodeType.Constant:
1166
+ return CONSTANT_STRING[node.s];
1167
+ case SerovalNodeType.Number:
1168
+ return '' + node.s;
1169
+ case SerovalNodeType.String:
1170
+ return '"' + node.s + '"';
1171
+ case SerovalNodeType.BigInt:
1172
+ return node.s + 'n';
1173
+ case SerovalNodeType.IndexedValue:
1174
+ return this.getRefParam(node.i);
1175
+ case SerovalNodeType.Reference:
1176
+ return this.serializeReference(node);
1177
+ case SerovalNodeType.Array:
1178
+ return this.serializeArray(node);
1179
+ case SerovalNodeType.Object:
1180
+ return this.serializeObject(node);
1181
+ case SerovalNodeType.NullConstructor:
1182
+ return this.serializeNullConstructor(node);
1183
+ case SerovalNodeType.Date:
1184
+ return this.serializeDate(node);
1185
+ case SerovalNodeType.RegExp:
1186
+ return this.serializeRegExp(node);
1187
+ case SerovalNodeType.Set:
1188
+ return this.serializeSet(node);
1189
+ case SerovalNodeType.Map:
1190
+ return this.serializeMap(node);
1191
+ case SerovalNodeType.ArrayBuffer:
1192
+ return this.serializeArrayBuffer(node);
1193
+ case SerovalNodeType.BigIntTypedArray:
1194
+ case SerovalNodeType.TypedArray:
1195
+ return this.serializeTypedArray(node);
1196
+ case SerovalNodeType.DataView:
1197
+ return this.serializeDataView(node);
1198
+ case SerovalNodeType.AggregateError:
1199
+ return this.serializeAggregateError(node);
1200
+ case SerovalNodeType.Error:
1201
+ return this.serializeError(node);
1202
+ case SerovalNodeType.Promise:
1203
+ return this.serializePromise(node);
1204
+ case SerovalNodeType.WKSymbol:
1205
+ return this.serializeWellKnownSymbol(node);
1206
+ case SerovalNodeType.Boxed:
1207
+ return this.serializeBoxed(node);
1208
+ case SerovalNodeType.PromiseConstructor:
1209
+ return this.serializePromiseConstructor(node);
1210
+ case SerovalNodeType.PromiseResolve:
1211
+ return this.serializePromiseResolve(node);
1212
+ case SerovalNodeType.PromiseReject:
1213
+ return this.serializePromiseReject(node);
1214
+ case SerovalNodeType.Plugin:
1215
+ return this.serializePlugin(node);
1216
+ case SerovalNodeType.SpecialReference:
1217
+ return this.serializeSpecialReference(node);
1218
+ case SerovalNodeType.IteratorFactory:
1219
+ return this.serializeIteratorFactory(node);
1220
+ case SerovalNodeType.IteratorFactoryInstance:
1221
+ return this.serializeIteratorFactoryInstance(node);
1222
+ case SerovalNodeType.AsyncIteratorFactory:
1223
+ return this.serializeAsyncIteratorFactory(node);
1224
+ case SerovalNodeType.AsyncIteratorFactoryInstance:
1225
+ return this.serializeAsyncIteratorFactoryInstance(node);
1226
+ case SerovalNodeType.StreamConstructor:
1227
+ return this.serializeStreamConstructor(node);
1228
+ case SerovalNodeType.StreamNext:
1229
+ return this.serializeStreamNext(node);
1230
+ case SerovalNodeType.StreamThrow:
1231
+ return this.serializeStreamThrow(node);
1232
+ case SerovalNodeType.StreamReturn:
1233
+ return this.serializeStreamReturn(node);
1234
+ default:
1235
+ throw new SerovalUnsupportedNodeError(node);
1236
+ }
1237
+ } catch (error) {
1238
+ throw new SerovalSerializationError(error);
1230
1239
  }
1231
1240
  }
1232
1241
  }
@@ -1,6 +1,6 @@
1
1
  import BaseAsyncParserContext from '../context/parser/async';
2
- import type { CrossParserContextOptions } from './parser';
3
2
  import type { SerovalMode } from '../plugin';
3
+ import type { CrossParserContextOptions } from './parser';
4
4
 
5
5
  export type CrossAsyncParserContextOptions = CrossParserContextOptions;
6
6
 
@@ -1,5 +1,5 @@
1
- import BaseDeserializerContext from '../context/deserializer';
2
1
  import type { BaseDeserializerOptions } from '../context/deserializer';
2
+ import BaseDeserializerContext from '../context/deserializer';
3
3
  import type { SerovalMode } from '../plugin';
4
4
 
5
5
  export type CrossDeserializerContextOptions = BaseDeserializerOptions;
@@ -1,9 +1,9 @@
1
- import type { SerovalNode } from '../types';
2
- import { GLOBAL_CONTEXT_REFERENCES } from '../keys';
3
1
  import type { BaseSerializerContextOptions } from '../context/serializer';
4
2
  import BaseSerializerContext from '../context/serializer';
3
+ import { GLOBAL_CONTEXT_REFERENCES } from '../keys';
5
4
  import type { SerovalMode } from '../plugin';
6
5
  import { serializeString } from '../string';
6
+ import type { SerovalNode } from '../types';
7
7
  import type { CrossContextOptions } from './parser';
8
8
 
9
9
  export interface CrossSerializerContextOptions
@@ -0,0 +1,102 @@
1
+ import { serializeString } from './string';
2
+ import type { SerovalNode } from './types';
3
+
4
+ const { toString: objectToString } = /* @__PURE__ */ Object.prototype;
5
+
6
+ function getErrorMessage(type: string, cause: any): string {
7
+ if (cause instanceof Error) {
8
+ return `Seroval caught an error during the ${type} process.
9
+
10
+ ${cause.name}
11
+ ${cause.message}
12
+
13
+ - For more information, please check the "cause" property of this error.
14
+ - If you believe this is an error in Seroval, please submit an issue at https://github.com/lxsmnsyc/seroval/issues/new`;
15
+ }
16
+ return `Seroval caught an error during the ${type} process.
17
+
18
+ "${objectToString.call(cause)}"
19
+
20
+ For more information, please check the "cause" property of this error.`;
21
+ }
22
+
23
+ export class SerovalError extends Error {
24
+ constructor(
25
+ type: string,
26
+ public cause: any,
27
+ ) {
28
+ super(getErrorMessage(type, cause));
29
+ }
30
+ }
31
+
32
+ export class SerovalParserError extends SerovalError {
33
+ constructor(cause: any) {
34
+ super('parsing', cause);
35
+ }
36
+ }
37
+
38
+ export class SerovalSerializationError extends SerovalError {
39
+ constructor(cause: any) {
40
+ super('serialization', cause);
41
+ }
42
+ }
43
+
44
+ export class SerovalDeserializationError extends SerovalError {
45
+ constructor(cause: any) {
46
+ super('deserialization', cause);
47
+ }
48
+ }
49
+
50
+ export class SerovalUnsupportedTypeError extends Error {
51
+ constructor(public value: unknown) {
52
+ super(
53
+ `The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
54
+
55
+ There are few workarounds for this problem:
56
+ - Transform the value in a way that it can be serialized.
57
+ - If the reference is present on multiple runtimes (isomorphic), you can use the Reference API to map the references.`,
58
+ );
59
+ }
60
+ }
61
+
62
+ export class SerovalUnsupportedNodeError extends Error {
63
+ constructor(node: SerovalNode) {
64
+ super('Unsupported node type "' + node.t + '".');
65
+ }
66
+ }
67
+
68
+ export class SerovalMissingPluginError extends Error {
69
+ constructor(tag: string) {
70
+ super('Missing plugin for tag "' + tag + '".');
71
+ }
72
+ }
73
+
74
+ export class SerovalMissingInstanceError extends Error {
75
+ constructor(tag: string) {
76
+ super('Missing "' + tag + '" instance.');
77
+ }
78
+ }
79
+
80
+ export class SerovalMissingReferenceError extends Error {
81
+ constructor(public value: unknown) {
82
+ super(
83
+ 'Missing reference for the value "' +
84
+ objectToString.call(value) +
85
+ '" of type "' +
86
+ typeof value +
87
+ '"',
88
+ );
89
+ }
90
+ }
91
+
92
+ export class SerovalMissingReferenceForIdError extends Error {
93
+ constructor(id: string) {
94
+ super('Missing reference for id "' + serializeString(id) + '"');
95
+ }
96
+ }
97
+
98
+ export class SerovalUnknownTypedArrayError extends Error {
99
+ constructor(name: string) {
100
+ super('Unknown TypedArray "' + name + '"');
101
+ }
102
+ }
@@ -31,12 +31,12 @@ export const NULL_NODE = /* @__PURE__ */ createConstantNode(
31
31
  SerovalConstant.Null,
32
32
  );
33
33
  export const NEG_ZERO_NODE = /* @__PURE__ */ createConstantNode(
34
- SerovalConstant.NegativeZero,
34
+ SerovalConstant.NegZero,
35
35
  );
36
36
  export const INFINITY_NODE = /* @__PURE__ */ createConstantNode(
37
- SerovalConstant.Infinity,
37
+ SerovalConstant.Inf,
38
38
  );
39
39
  export const NEG_INFINITY_NODE = /* @__PURE__ */ createConstantNode(
40
- SerovalConstant.NegativeInfinity,
40
+ SerovalConstant.NegInf,
41
41
  );
42
- export const NAN_NODE = /* @__PURE__ */ createConstantNode(SerovalConstant.NaN);
42
+ export const NAN_NODE = /* @__PURE__ */ createConstantNode(SerovalConstant.Nan);
@@ -1,8 +1,8 @@
1
+ import type BaseDeserializerContext from './context/deserializer';
1
2
  import type BaseAsyncParserContext from './context/parser/async';
2
3
  import type BaseStreamParserContext from './context/parser/stream';
3
4
  import type BaseSyncParserContext from './context/parser/sync';
4
5
  import type BaseSerializerContext from './context/serializer';
5
- import type BaseDeserializerContext from './context/deserializer';
6
6
 
7
7
  export type SerovalMode = 'vanilla' | 'cross';
8
8
 
@@ -1,5 +1,9 @@
1
- import assert from './utils/assert';
1
+ import {
2
+ SerovalMissingReferenceError,
3
+ SerovalMissingReferenceForIdError,
4
+ } from '..';
2
5
  import { REFERENCES_KEY } from './keys';
6
+ import assert from './utils/assert';
3
7
 
4
8
  const REFERENCE = new Map<unknown, string>();
5
9
  const INV_REFERENCE = new Map<string, unknown>();
@@ -19,12 +23,12 @@ export function hasReference(id: string): boolean {
19
23
  }
20
24
 
21
25
  export function getReferenceID<T>(value: T): string {
22
- assert(hasReferenceID(value), new Error('Missing reference id'));
26
+ assert(hasReferenceID(value), new SerovalMissingReferenceError(value));
23
27
  return REFERENCE.get(value)!;
24
28
  }
25
29
 
26
30
  export function getReference<T>(id: string): T {
27
- assert(hasReference(id), new Error('Missing reference for id:' + id));
31
+ assert(hasReference(id), new SerovalMissingReferenceForIdError(id));
28
32
  return INV_REFERENCE.get(id) as T;
29
33
  }
30
34
 
@@ -1,5 +1,5 @@
1
- import BaseAsyncParserContext from '../context/parser/async';
2
1
  import type { BaseParserContextOptions } from '../context/parser';
2
+ import BaseAsyncParserContext from '../context/parser/async';
3
3
  import type { SerovalMode } from '../plugin';
4
4
 
5
5
  export type AsyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
@@ -1,5 +1,5 @@
1
- import BaseDeserializerContext from '../context/deserializer';
2
1
  import type { BaseDeserializerOptions } from '../context/deserializer';
2
+ import BaseDeserializerContext from '../context/deserializer';
3
3
  import type { SerovalMode } from '../plugin';
4
4
 
5
5
  export interface VanillaDeserializerContextOptions
@@ -85,7 +85,10 @@ export async function toJSONAsync<T>(
85
85
  };
86
86
  }
87
87
 
88
- export function compileJSON(source: SerovalJSON, options: PluginAccessOptions = {}): string {
88
+ export function compileJSON(
89
+ source: SerovalJSON,
90
+ options: PluginAccessOptions = {},
91
+ ): string {
89
92
  const plugins = resolvePlugins(options.plugins);
90
93
  const ctx = new VanillaSerializerContext({
91
94
  plugins,
@@ -95,7 +98,10 @@ export function compileJSON(source: SerovalJSON, options: PluginAccessOptions =
95
98
  return ctx.serializeTop(source.t);
96
99
  }
97
100
 
98
- export function fromJSON<T>(source: SerovalJSON, options: PluginAccessOptions = {}): T {
101
+ export function fromJSON<T>(
102
+ source: SerovalJSON,
103
+ options: PluginAccessOptions = {},
104
+ ): T {
99
105
  const plugins = resolvePlugins(options.plugins);
100
106
  const ctx = new VanillaDeserializerContext({
101
107
  plugins,
@@ -1,14 +1,15 @@
1
+ import { SerovalNodeType } from '../constants';
2
+ import type { BaseSerializerContextOptions } from '../context/serializer';
3
+ import BaseSerializerContext from '../context/serializer';
4
+ import { SerovalUnsupportedNodeError } from '../errors';
5
+ import type { SerovalMode } from '../plugin';
1
6
  import type {
2
7
  SerovalNode,
3
8
  SerovalPromiseConstructorNode,
4
9
  SerovalPromiseRejectNode,
5
10
  SerovalPromiseResolveNode,
6
11
  } from '../types';
7
- import type { BaseSerializerContextOptions } from '../context/serializer';
8
- import BaseSerializerContext from '../context/serializer';
9
12
  import getIdentifier from '../utils/get-identifier';
10
- import type { SerovalMode } from '../plugin';
11
- import { SerovalNodeType } from '../constants';
12
13
 
13
14
  export type VanillaSerializerContextOptions = BaseSerializerContextOptions;
14
15
 
@@ -61,15 +62,15 @@ export default class VanillaSerializerContext extends BaseSerializerContext {
61
62
  protected serializePromiseConstructor(
62
63
  node: SerovalPromiseConstructorNode,
63
64
  ): string {
64
- throw new Error('Unsupported node type "' + node.t + '".');
65
+ throw new SerovalUnsupportedNodeError(node);
65
66
  }
66
67
 
67
68
  protected serializePromiseResolve(node: SerovalPromiseResolveNode): string {
68
- throw new Error('Unsupported node type "' + node.t + '".');
69
+ throw new SerovalUnsupportedNodeError(node);
69
70
  }
70
71
 
71
72
  protected serializePromiseReject(node: SerovalPromiseRejectNode): string {
72
- throw new Error('Unsupported node type "' + node.t + '".');
73
+ throw new SerovalUnsupportedNodeError(node);
73
74
  }
74
75
 
75
76
  serializeTop(tree: SerovalNode): string {
package/src/core/types.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type {
2
+ ErrorConstructorTag,
3
+ SerovalConstant,
2
4
  SerovalNodeType,
3
5
  SerovalObjectFlags,
4
- SerovalConstant,
5
6
  Symbols,
6
- ErrorConstructorTag,
7
7
  } from './constants';
8
8
  import type { SpecialReference } from './special-reference';
9
9
 
@@ -1,3 +1,5 @@
1
+ import { SerovalUnknownTypedArrayError } from '../errors';
2
+
1
3
  type TypedArrayConstructor =
2
4
  | Int8ArrayConstructor
3
5
  | Int16ArrayConstructor
@@ -49,6 +51,6 @@ export function getTypedArrayConstructor(name: string): TypedArrayConstructor {
49
51
  case 'BigUint64Array':
50
52
  return BigUint64Array;
51
53
  default:
52
- throw new Error(`Unknown TypedArray "${name}"`);
54
+ throw new SerovalUnknownTypedArrayError(name);
53
55
  }
54
56
  }
package/src/index.ts CHANGED
@@ -13,3 +13,4 @@ export type { Stream } from './core/stream';
13
13
  export { createStream } from './core/stream';
14
14
 
15
15
  export type { SerovalNode } from './core/types';
16
+ export * from './core/errors';
@@ -1,5 +0,0 @@
1
- export default class UnsupportedTypeError extends Error {
2
- value: unknown;
3
- constructor(value: unknown);
4
- }
5
- //# sourceMappingURL=UnsupportedTypeError.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"UnsupportedTypeError.d.ts","sourceRoot":"","sources":["../../../src/core/UnsupportedTypeError.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,OAAO,oBAAqB,SAAQ,KAAK;IAClC,KAAK,EAAE,OAAO;gBAAd,KAAK,EAAE,OAAO;CAGlC"}
@@ -1,7 +0,0 @@
1
- const { toString: objectToString } = /* @__PURE__ */ Object.prototype;
2
-
3
- export default class UnsupportedTypeError extends Error {
4
- constructor(public value: unknown) {
5
- super('Unsupported type "' + objectToString.call(value) + '"');
6
- }
7
- }