seroval 1.3.2 → 1.4.0

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 (112) hide show
  1. package/dist/cjs/development/index.cjs +2640 -2683
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/production/index.cjs +3 -17
  4. package/dist/esm/development/index.mjs +2640 -2683
  5. package/dist/esm/development/index.mjs.map +4 -4
  6. package/dist/esm/production/index.mjs +3 -17
  7. package/dist/types/core/base-primitives.d.ts +1 -2
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +1 -1
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +1 -0
  12. package/dist/types/core/constants.d.ts.map +1 -1
  13. package/dist/types/core/constructors.d.ts +54 -0
  14. package/dist/types/core/constructors.d.ts.map +1 -0
  15. package/dist/types/core/context/async-parser.d.ts +19 -0
  16. package/dist/types/core/context/async-parser.d.ts.map +1 -0
  17. package/dist/types/core/context/deserializer.d.ts +35 -38
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts +32 -19
  20. package/dist/types/core/context/parser.d.ts.map +1 -1
  21. package/dist/types/core/context/serializer.d.ts +38 -99
  22. package/dist/types/core/context/serializer.d.ts.map +1 -1
  23. package/dist/types/core/context/sync-parser.d.ts +59 -0
  24. package/dist/types/core/context/sync-parser.d.ts.map +1 -0
  25. package/dist/types/core/cross/index.d.ts +12 -13
  26. package/dist/types/core/cross/index.d.ts.map +1 -1
  27. package/dist/types/core/errors.d.ts.map +1 -1
  28. package/dist/types/core/function-string.d.ts +2 -2
  29. package/dist/types/core/function-string.d.ts.map +1 -1
  30. package/dist/types/core/plugin.d.ts +13 -11
  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/special-reference.d.ts +3 -2
  34. package/dist/types/core/special-reference.d.ts.map +1 -1
  35. package/dist/types/core/stream.d.ts +1 -2
  36. package/dist/types/core/stream.d.ts.map +1 -1
  37. package/dist/types/core/symbols.d.ts +14 -0
  38. package/dist/types/core/symbols.d.ts.map +1 -0
  39. package/dist/types/core/tree/index.d.ts +3 -2
  40. package/dist/types/core/tree/index.d.ts.map +1 -1
  41. package/dist/types/core/types.d.ts +3 -1
  42. package/dist/types/core/types.d.ts.map +1 -1
  43. package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -1
  44. package/dist/types/index.d.ts +10 -5
  45. package/dist/types/index.d.ts.map +1 -1
  46. package/package.json +17 -19
  47. package/src/core/base-primitives.ts +0 -27
  48. package/src/core/compat.ts +2 -1
  49. package/src/core/constants.ts +28 -26
  50. package/src/core/constructors.ts +269 -0
  51. package/src/core/context/async-parser.ts +586 -0
  52. package/src/core/context/deserializer.ts +506 -316
  53. package/src/core/context/parser.ts +243 -180
  54. package/src/core/context/serializer.ts +1058 -827
  55. package/src/core/context/sync-parser.ts +833 -0
  56. package/src/core/cross/index.ts +63 -47
  57. package/src/core/errors.ts +73 -12
  58. package/src/core/function-string.ts +11 -10
  59. package/src/core/plugin.ts +20 -11
  60. package/src/core/reference.ts +8 -5
  61. package/src/core/special-reference.ts +18 -90
  62. package/src/core/stream.ts +18 -152
  63. package/src/core/symbols.ts +18 -0
  64. package/src/core/tree/index.ts +45 -31
  65. package/src/core/types.ts +7 -2
  66. package/src/core/utils/iterator-to-sequence.ts +6 -28
  67. package/src/index.ts +29 -11
  68. package/dist/types/core/context/parser/async.d.ts +0 -24
  69. package/dist/types/core/context/parser/async.d.ts.map +0 -1
  70. package/dist/types/core/context/parser/stream.d.ts +0 -43
  71. package/dist/types/core/context/parser/stream.d.ts.map +0 -1
  72. package/dist/types/core/context/parser/sync.d.ts +0 -30
  73. package/dist/types/core/context/parser/sync.d.ts.map +0 -1
  74. package/dist/types/core/cross/async.d.ts +0 -8
  75. package/dist/types/core/cross/async.d.ts.map +0 -1
  76. package/dist/types/core/cross/deserializer.d.ts +0 -9
  77. package/dist/types/core/cross/deserializer.d.ts.map +0 -1
  78. package/dist/types/core/cross/parser.d.ts +0 -6
  79. package/dist/types/core/cross/parser.d.ts.map +0 -1
  80. package/dist/types/core/cross/serializer.d.ts +0 -16
  81. package/dist/types/core/cross/serializer.d.ts.map +0 -1
  82. package/dist/types/core/cross/stream.d.ts +0 -8
  83. package/dist/types/core/cross/stream.d.ts.map +0 -1
  84. package/dist/types/core/cross/sync.d.ts +0 -8
  85. package/dist/types/core/cross/sync.d.ts.map +0 -1
  86. package/dist/types/core/tree/async.d.ts +0 -8
  87. package/dist/types/core/tree/async.d.ts.map +0 -1
  88. package/dist/types/core/tree/deserializer.d.ts +0 -13
  89. package/dist/types/core/tree/deserializer.d.ts.map +0 -1
  90. package/dist/types/core/tree/serializer.d.ts +0 -29
  91. package/dist/types/core/tree/serializer.d.ts.map +0 -1
  92. package/dist/types/core/tree/sync.d.ts +0 -8
  93. package/dist/types/core/tree/sync.d.ts.map +0 -1
  94. package/dist/types/core/utils/assert.d.ts +0 -2
  95. package/dist/types/core/utils/assert.d.ts.map +0 -1
  96. package/dist/types/core/utils/deferred.d.ts +0 -7
  97. package/dist/types/core/utils/deferred.d.ts.map +0 -1
  98. package/src/core/context/parser/async.ts +0 -495
  99. package/src/core/context/parser/stream.ts +0 -323
  100. package/src/core/context/parser/sync.ts +0 -404
  101. package/src/core/cross/async.ts +0 -9
  102. package/src/core/cross/deserializer.ts +0 -16
  103. package/src/core/cross/parser.ts +0 -7
  104. package/src/core/cross/serializer.ts +0 -72
  105. package/src/core/cross/stream.ts +0 -9
  106. package/src/core/cross/sync.ts +0 -9
  107. package/src/core/tree/async.ts +0 -9
  108. package/src/core/tree/deserializer.ts +0 -26
  109. package/src/core/tree/serializer.ts +0 -98
  110. package/src/core/tree/sync.ts +0 -9
  111. package/src/core/utils/assert.ts +0 -5
  112. package/src/core/utils/deferred.ts +0 -22
@@ -1,18 +1,34 @@
1
- import { resolvePlugins } from '../plugin';
1
+ import type { AsyncParserContextOptions } from '../context/async-parser';
2
+ import {
3
+ createAsyncParserContext,
4
+ parseTopAsync,
5
+ } from '../context/async-parser';
6
+ import type { CrossDeserializerContextOptions } from '../context/deserializer';
7
+ import {
8
+ createCrossDeserializerContext,
9
+ deserializeTop,
10
+ } from '../context/deserializer';
11
+ import type { CrossContextOptions } from '../context/serializer';
12
+ import {
13
+ createCrossSerializerContext,
14
+ serializeTopCross,
15
+ } from '../context/serializer';
16
+ import type {
17
+ StreamParserContextOptions,
18
+ SyncParserContextOptions,
19
+ } from '../context/sync-parser';
20
+ import {
21
+ createStreamParserContext,
22
+ createSyncParserContext,
23
+ destroyStreamParse,
24
+ parseTop,
25
+ startStreamParse,
26
+ } from '../context/sync-parser';
27
+ import { resolvePlugins, SerovalMode } from '../plugin';
2
28
  import type { SerovalNode } from '../types';
3
- import type { CrossAsyncParserContextOptions } from './async';
4
- import AsyncCrossParserContext from './async';
5
- import type { CrossDeserializerContextOptions } from './deserializer';
6
- import CrossDeserializerContext from './deserializer';
7
- import type { CrossContextOptions, CrossParserContextOptions } from './parser';
8
- import CrossSerializerContext from './serializer';
9
- import type { CrossStreamParserContextOptions } from './stream';
10
- import StreamCrossParserContext from './stream';
11
- import type { CrossSyncParserContextOptions } from './sync';
12
- import SyncCrossParserContext from './sync';
13
29
 
14
30
  export interface CrossSerializeOptions
15
- extends CrossSyncParserContextOptions,
31
+ extends SyncParserContextOptions,
16
32
  CrossContextOptions {}
17
33
 
18
34
  export function crossSerialize<T>(
@@ -20,23 +36,23 @@ export function crossSerialize<T>(
20
36
  options: CrossSerializeOptions = {},
21
37
  ): string {
22
38
  const plugins = resolvePlugins(options.plugins);
23
- const ctx = new SyncCrossParserContext({
39
+ const ctx = createSyncParserContext(SerovalMode.Cross, {
24
40
  plugins,
25
41
  disabledFeatures: options.disabledFeatures,
26
42
  refs: options.refs,
27
43
  });
28
- const tree = ctx.parseTop(source);
29
- const serial = new CrossSerializerContext({
44
+ const tree = parseTop(ctx, source);
45
+ const serial = createCrossSerializerContext({
30
46
  plugins,
31
- features: ctx.features,
47
+ features: ctx.base.features,
32
48
  scopeId: options.scopeId,
33
- markedRefs: ctx.marked,
49
+ markedRefs: ctx.base.marked,
34
50
  });
35
- return serial.serializeTop(tree);
51
+ return serializeTopCross(serial, tree);
36
52
  }
37
53
 
38
54
  export interface CrossSerializeAsyncOptions
39
- extends CrossAsyncParserContextOptions,
55
+ extends AsyncParserContextOptions,
40
56
  CrossContextOptions {}
41
57
 
42
58
  export async function crossSerializeAsync<T>(
@@ -44,53 +60,53 @@ export async function crossSerializeAsync<T>(
44
60
  options: CrossSerializeAsyncOptions = {},
45
61
  ): Promise<string> {
46
62
  const plugins = resolvePlugins(options.plugins);
47
- const ctx = new AsyncCrossParserContext({
63
+ const ctx = createAsyncParserContext(SerovalMode.Cross, {
48
64
  plugins,
49
65
  disabledFeatures: options.disabledFeatures,
50
66
  refs: options.refs,
51
67
  });
52
- const tree = await ctx.parseTop(source);
53
- const serial = new CrossSerializerContext({
68
+ const tree = await parseTopAsync(ctx, source);
69
+ const serial = createCrossSerializerContext({
54
70
  plugins,
55
- features: ctx.features,
71
+ features: ctx.base.features,
56
72
  scopeId: options.scopeId,
57
- markedRefs: ctx.marked,
73
+ markedRefs: ctx.base.marked,
58
74
  });
59
- return serial.serializeTop(tree);
75
+ return serializeTopCross(serial, tree);
60
76
  }
61
77
 
62
- export type ToCrossJSONOptions = CrossParserContextOptions;
78
+ export type ToCrossJSONOptions = SyncParserContextOptions;
63
79
 
64
80
  export function toCrossJSON<T>(
65
81
  source: T,
66
- options: CrossParserContextOptions = {},
82
+ options: ToCrossJSONOptions = {},
67
83
  ): SerovalNode {
68
84
  const plugins = resolvePlugins(options.plugins);
69
- const ctx = new SyncCrossParserContext({
85
+ const ctx = createSyncParserContext(SerovalMode.Cross, {
70
86
  plugins,
71
87
  disabledFeatures: options.disabledFeatures,
72
88
  refs: options.refs,
73
89
  });
74
- return ctx.parseTop(source);
90
+ return parseTop(ctx, source);
75
91
  }
76
92
 
77
- export type ToCrossJSONAsyncOptions = CrossParserContextOptions;
93
+ export type ToCrossJSONAsyncOptions = AsyncParserContextOptions;
78
94
 
79
95
  export async function toCrossJSONAsync<T>(
80
96
  source: T,
81
- options: CrossParserContextOptions = {},
97
+ options: ToCrossJSONAsyncOptions = {},
82
98
  ): Promise<SerovalNode> {
83
99
  const plugins = resolvePlugins(options.plugins);
84
- const ctx = new AsyncCrossParserContext({
100
+ const ctx = createAsyncParserContext(SerovalMode.Cross, {
85
101
  plugins,
86
102
  disabledFeatures: options.disabledFeatures,
87
103
  refs: options.refs,
88
104
  });
89
- return await ctx.parseTop(source);
105
+ return await parseTopAsync(ctx, source);
90
106
  }
91
107
 
92
108
  export interface CrossSerializeStreamOptions
93
- extends Omit<CrossStreamParserContextOptions, 'onParse'>,
109
+ extends Omit<StreamParserContextOptions, 'onParse'>,
94
110
  CrossContextOptions {
95
111
  onSerialize: (data: string, initial: boolean) => void;
96
112
  }
@@ -100,22 +116,22 @@ export function crossSerializeStream<T>(
100
116
  options: CrossSerializeStreamOptions,
101
117
  ): () => void {
102
118
  const plugins = resolvePlugins(options.plugins);
103
- const ctx = new StreamCrossParserContext({
119
+ const ctx = createStreamParserContext({
104
120
  plugins,
105
121
  refs: options.refs,
106
122
  disabledFeatures: options.disabledFeatures,
107
123
  onParse(node, initial): void {
108
- const serial = new CrossSerializerContext({
124
+ const serial = createCrossSerializerContext({
109
125
  plugins,
110
- features: ctx.features,
126
+ features: ctx.base.features,
111
127
  scopeId: options.scopeId,
112
- markedRefs: ctx.marked,
128
+ markedRefs: ctx.base.marked,
113
129
  });
114
130
 
115
131
  let serialized: string;
116
132
 
117
133
  try {
118
- serialized = serial.serializeTop(node);
134
+ serialized = serializeTopCross(serial, node);
119
135
  } catch (err) {
120
136
  if (options.onError) {
121
137
  options.onError(err);
@@ -129,19 +145,19 @@ export function crossSerializeStream<T>(
129
145
  onDone: options.onDone,
130
146
  });
131
147
 
132
- ctx.start(source);
148
+ startStreamParse(ctx, source);
133
149
 
134
- return ctx.destroy.bind(ctx);
150
+ return destroyStreamParse.bind(null, ctx);
135
151
  }
136
152
 
137
- export type ToCrossJSONStreamOptions = CrossStreamParserContextOptions;
153
+ export type ToCrossJSONStreamOptions = StreamParserContextOptions;
138
154
 
139
155
  export function toCrossJSONStream<T>(
140
156
  source: T,
141
157
  options: ToCrossJSONStreamOptions,
142
158
  ): () => void {
143
159
  const plugins = resolvePlugins(options.plugins);
144
- const ctx = new StreamCrossParserContext({
160
+ const ctx = createStreamParserContext({
145
161
  plugins,
146
162
  refs: options.refs,
147
163
  disabledFeatures: options.disabledFeatures,
@@ -150,9 +166,9 @@ export function toCrossJSONStream<T>(
150
166
  onDone: options.onDone,
151
167
  });
152
168
 
153
- ctx.start(source);
169
+ startStreamParse(ctx, source);
154
170
 
155
- return ctx.destroy.bind(ctx);
171
+ return destroyStreamParse.bind(null, ctx);
156
172
  }
157
173
 
158
174
  export type FromCrossJSONOptions = CrossDeserializerContextOptions;
@@ -162,9 +178,9 @@ export function fromCrossJSON<T>(
162
178
  options: FromCrossJSONOptions,
163
179
  ): T {
164
180
  const plugins = resolvePlugins(options.plugins);
165
- const ctx = new CrossDeserializerContext({
181
+ const ctx = createCrossDeserializerContext({
166
182
  plugins,
167
183
  refs: options.refs,
168
184
  });
169
- return ctx.deserializeTop(source) as T;
185
+ return deserializeTop(ctx, source) as T;
170
186
  }
@@ -1,9 +1,17 @@
1
+ /// <reference types="pridepack/env" />
2
+
1
3
  import { serializeString } from './string';
2
4
  import type { SerovalNode } from './types';
3
5
 
4
6
  const { toString: objectToString } = /* @__PURE__ */ Object.prototype;
5
7
 
6
- function getErrorMessage(type: string, cause: any): string {
8
+ const enum StepErrorCodes {
9
+ Parse = 1,
10
+ Serialize = 2,
11
+ Deserialize = 3,
12
+ }
13
+
14
+ function getErrorMessageDev(type: string, cause: any): string {
7
15
  if (cause instanceof Error) {
8
16
  return `Seroval caught an error during the ${type} process.
9
17
 
@@ -20,6 +28,21 @@ ${cause.message}
20
28
  For more information, please check the "cause" property of this error.`;
21
29
  }
22
30
 
31
+ const STEP_ERROR_CODES: Record<string, StepErrorCodes> = {
32
+ parsing: StepErrorCodes.Parse,
33
+ serialization: StepErrorCodes.Serialize,
34
+ deserialization: StepErrorCodes.Deserialize,
35
+ };
36
+
37
+ function getErrorMessageProd(type: string): string {
38
+ return `Seroval Error (step: ${STEP_ERROR_CODES[type]})`;
39
+ }
40
+
41
+ const getErrorMessage = (type: string, cause: any) =>
42
+ import.meta.env.PROD
43
+ ? getErrorMessageProd(type)
44
+ : getErrorMessageDev(type, cause);
45
+
23
46
  export class SerovalError extends Error {
24
47
  constructor(
25
48
  type: string,
@@ -47,10 +70,26 @@ export class SerovalDeserializationError extends SerovalError {
47
70
  }
48
71
  }
49
72
 
73
+ const enum SpecificErrorCodes {
74
+ UnsupportedType = 1,
75
+ UnsupportedNode = 2,
76
+ MissingPlugin = 3,
77
+ MissingInstance = 4,
78
+ MissingReference = 5,
79
+ MissingReferenceForId = 6,
80
+ UnknownTypedArray = 7,
81
+ }
82
+
83
+ function getSpecificErrorMessage(code: SpecificErrorCodes): string {
84
+ return `Seroval Error (specific: ${code})`;
85
+ }
86
+
50
87
  export class SerovalUnsupportedTypeError extends Error {
51
88
  constructor(public value: unknown) {
52
89
  super(
53
- `The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
90
+ import.meta.env.PROD
91
+ ? getSpecificErrorMessage(SpecificErrorCodes.UnsupportedType)
92
+ : `The value ${objectToString.call(value)} of type "${typeof value}" cannot be parsed/serialized.
54
93
 
55
94
  There are few workarounds for this problem:
56
95
  - Transform the value in a way that it can be serialized.
@@ -61,42 +100,64 @@ There are few workarounds for this problem:
61
100
 
62
101
  export class SerovalUnsupportedNodeError extends Error {
63
102
  constructor(node: SerovalNode) {
64
- super('Unsupported node type "' + node.t + '".');
103
+ super(
104
+ import.meta.env.PROD
105
+ ? getSpecificErrorMessage(SpecificErrorCodes.UnsupportedNode)
106
+ : 'Unsupported node type "' + node.t + '".',
107
+ );
65
108
  }
66
109
  }
67
110
 
68
111
  export class SerovalMissingPluginError extends Error {
69
112
  constructor(tag: string) {
70
- super('Missing plugin for tag "' + tag + '".');
113
+ super(
114
+ import.meta.env.PROD
115
+ ? getSpecificErrorMessage(SpecificErrorCodes.MissingPlugin)
116
+ : 'Missing plugin for tag "' + tag + '".',
117
+ );
71
118
  }
72
119
  }
73
120
 
74
121
  export class SerovalMissingInstanceError extends Error {
75
122
  constructor(tag: string) {
76
- super('Missing "' + tag + '" instance.');
123
+ super(
124
+ import.meta.env.PROD
125
+ ? getSpecificErrorMessage(SpecificErrorCodes.MissingInstance)
126
+ : 'Missing "' + tag + '" instance.',
127
+ );
77
128
  }
78
129
  }
79
130
 
80
131
  export class SerovalMissingReferenceError extends Error {
81
132
  constructor(public value: unknown) {
82
133
  super(
83
- 'Missing reference for the value "' +
84
- objectToString.call(value) +
85
- '" of type "' +
86
- typeof value +
87
- '"',
134
+ import.meta.env.PROD
135
+ ? getSpecificErrorMessage(SpecificErrorCodes.MissingReference)
136
+ : 'Missing reference for the value "' +
137
+ objectToString.call(value) +
138
+ '" of type "' +
139
+ typeof value +
140
+ '"',
88
141
  );
89
142
  }
90
143
  }
91
144
 
92
145
  export class SerovalMissingReferenceForIdError extends Error {
93
146
  constructor(id: string) {
94
- super('Missing reference for id "' + serializeString(id) + '"');
147
+ super(
148
+ import.meta.env.PROD
149
+ ? getSpecificErrorMessage(SpecificErrorCodes.MissingReferenceForId)
150
+ : 'Missing reference for id "' + serializeString(id) + '"',
151
+ );
95
152
  }
96
153
  }
97
154
 
98
155
  export class SerovalUnknownTypedArrayError extends Error {
99
156
  constructor(name: string) {
100
- super('Unknown TypedArray "' + name + '"');
157
+ super(
158
+ import.meta.env.PROD
159
+ ? getSpecificErrorMessage(SpecificErrorCodes.UnknownTypedArray)
160
+ : 'Unknown TypedArray "' + name + '"',
161
+ );
101
162
  }
102
163
  }
@@ -1,11 +1,13 @@
1
- import { Feature } from './compat';
2
1
 
3
- export function createFunction(
4
- features: number,
5
- parameters: string[],
6
- body: string,
7
- ): string {
8
- if (features & Feature.ArrowFunction) {
2
+ declare const T: unknown;
3
+
4
+ const RETURN = () => T;
5
+ const SERIALIZED_RETURN = /* @__PURE__ */ RETURN.toString();
6
+
7
+ const IS_MODERN = /* @__PURE__ */ /=>/.test(SERIALIZED_RETURN);
8
+
9
+ export function createFunction(parameters: string[], body: string): string {
10
+ if (IS_MODERN) {
9
11
  const joined =
10
12
  parameters.length === 1
11
13
  ? parameters[0]
@@ -16,11 +18,10 @@ export function createFunction(
16
18
  }
17
19
 
18
20
  export function createEffectfulFunction(
19
- features: number,
20
21
  parameters: string[],
21
22
  body: string,
22
23
  ): string {
23
- if (features & Feature.ArrowFunction) {
24
+ if (IS_MODERN) {
24
25
  const joined =
25
26
  parameters.length === 1
26
27
  ? parameters[0]
@@ -28,4 +29,4 @@ export function createEffectfulFunction(
28
29
  return joined + '=>{' + body + '}';
29
30
  }
30
31
  return 'function(' + parameters.join(',') + '){' + body + '}';
31
- }
32
+ }
@@ -1,10 +1,15 @@
1
- import type BaseDeserializerContext from './context/deserializer';
2
- import type BaseAsyncParserContext from './context/parser/async';
3
- import type BaseStreamParserContext from './context/parser/stream';
4
- import type BaseSyncParserContext from './context/parser/sync';
5
- import type BaseSerializerContext from './context/serializer';
1
+ import type { AsyncParsePluginContext } from './context/async-parser';
2
+ import type { DeserializePluginContext } from './context/deserializer';
3
+ import type { SerializePluginContext } from './context/serializer';
4
+ import type {
5
+ StreamParsePluginContext,
6
+ SyncParsePluginContext,
7
+ } from './context/sync-parser';
6
8
 
7
- export type SerovalMode = 'vanilla' | 'cross';
9
+ export const enum SerovalMode {
10
+ Vanilla = 1,
11
+ Cross = 2,
12
+ }
8
13
 
9
14
  export interface PluginData {
10
15
  id: number;
@@ -28,28 +33,32 @@ export interface Plugin<Value, Node> {
28
33
  * Parsing modes
29
34
  */
30
35
  parse: {
31
- sync?: (value: Value, ctx: BaseSyncParserContext, data: PluginData) => Node;
36
+ sync?: (
37
+ value: Value,
38
+ ctx: SyncParsePluginContext,
39
+ data: PluginData,
40
+ ) => Node;
32
41
  async?: (
33
42
  value: Value,
34
- ctx: BaseAsyncParserContext,
43
+ ctx: AsyncParsePluginContext,
35
44
  data: PluginData,
36
45
  ) => Promise<Node>;
37
46
  stream?: (
38
47
  value: Value,
39
- ctx: BaseStreamParserContext,
48
+ ctx: StreamParsePluginContext,
40
49
  data: PluginData,
41
50
  ) => Node;
42
51
  };
43
52
  /**
44
53
  * Convert the parsed node into a JS string
45
54
  */
46
- serialize(node: Node, ctx: BaseSerializerContext, data: PluginData): string;
55
+ serialize(node: Node, ctx: SerializePluginContext, data: PluginData): string;
47
56
  /**
48
57
  * Convert the parsed node into its runtime equivalent.
49
58
  */
50
59
  deserialize(
51
60
  node: Node,
52
- ctx: BaseDeserializerContext,
61
+ ctx: DeserializePluginContext,
53
62
  data: PluginData,
54
63
  ): Value;
55
64
  }
@@ -3,7 +3,6 @@ import {
3
3
  SerovalMissingReferenceForIdError,
4
4
  } from '..';
5
5
  import { REFERENCES_KEY } from './keys';
6
- import assert from './utils/assert';
7
6
 
8
7
  const REFERENCE = new Map<unknown, string>();
9
8
  const INV_REFERENCE = new Map<string, unknown>();
@@ -23,13 +22,17 @@ export function hasReference(id: string): boolean {
23
22
  }
24
23
 
25
24
  export function getReferenceID<T>(value: T): string {
26
- assert(hasReferenceID(value), new SerovalMissingReferenceError(value));
27
- return REFERENCE.get(value)!;
25
+ if (hasReferenceID(value)) {
26
+ return REFERENCE.get(value)!;
27
+ }
28
+ throw new SerovalMissingReferenceError(value);
28
29
  }
29
30
 
30
31
  export function getReference<T>(id: string): T {
31
- assert(hasReference(id), new SerovalMissingReferenceForIdError(id));
32
- return INV_REFERENCE.get(id) as T;
32
+ if (hasReference(id)) {
33
+ return INV_REFERENCE.get(id) as T;
34
+ }
35
+ throw new SerovalMissingReferenceForIdError(id);
33
36
  }
34
37
 
35
38
  if (typeof globalThis !== 'undefined') {
@@ -1,4 +1,10 @@
1
- import { createEffectfulFunction, createFunction } from './function-string';
1
+ import {
2
+ SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR,
3
+ SERIALIZED_PROMISE_CONSTRUCTOR,
4
+ SERIALIZED_PROMISE_FAILURE,
5
+ SERIALIZED_PROMISE_SUCCESS,
6
+ SERIALIZED_STREAM_CONSTRUCTOR,
7
+ } from './constructors';
2
8
 
3
9
  export const ITERATOR = {};
4
10
 
@@ -10,6 +16,7 @@ export const enum SpecialReference {
10
16
  PromiseSuccess = 2,
11
17
  PromiseFailure = 3,
12
18
  StreamConstructor = 4,
19
+ ArrayBufferConstructor = 5,
13
20
  }
14
21
 
15
22
  /**
@@ -21,94 +28,15 @@ export const SPECIAL_REFS: Record<SpecialReference, unknown> = {
21
28
  [SpecialReference.PromiseSuccess]: {},
22
29
  [SpecialReference.PromiseFailure]: {},
23
30
  [SpecialReference.StreamConstructor]: {},
31
+ [SpecialReference.ArrayBufferConstructor]: {},
24
32
  };
25
33
 
26
- function serializePromiseConstructor(features: number): string {
27
- return createFunction(
28
- features,
29
- ['r'],
30
- '(r.p=new Promise(' +
31
- createEffectfulFunction(features, ['s', 'f'], 'r.s=s,r.f=f') +
32
- '))',
33
- );
34
- }
35
-
36
- function serializePromiseSuccess(features: number): string {
37
- return createEffectfulFunction(
38
- features,
39
- ['r', 'd'],
40
- 'r.s(d),r.p.s=1,r.p.v=d',
41
- );
42
- }
43
-
44
- function serializePromiseFailure(features: number): string {
45
- return createEffectfulFunction(
46
- features,
47
- ['r', 'd'],
48
- 'r.f(d),r.p.s=2,r.p.v=d',
49
- );
50
- }
51
-
52
- function serializeStreamConstructor(features: number): string {
53
- return createFunction(
54
- features,
55
- ['b', 'a', 's', 'l', 'p', 'f', 'e', 'n'],
56
- '(b=[],a=!0,s=!1,l=[],p=0,f=' +
57
- createEffectfulFunction(
58
- features,
59
- ['v', 'm', 'x'],
60
- 'for(x=0;x<p;x++)l[x]&&l[x][m](v)',
61
- ) +
62
- ',n=' +
63
- createEffectfulFunction(
64
- features,
65
- ['o', 'x', 'z', 'c'],
66
- 'for(x=0,z=b.length;x<z;x++)(c=b[x],(!a&&x===z-1)?o[s?"return":"throw"](c):o.next(c))',
67
- ) +
68
- ',e=' +
69
- createFunction(
70
- features,
71
- ['o', 't'],
72
- '(a&&(l[t=p++]=o),n(o),' +
73
- createEffectfulFunction(features, [], 'a&&(l[t]=void 0)') +
74
- ')',
75
- ) +
76
- ',{__SEROVAL_STREAM__:!0,on:' +
77
- createFunction(features, ['o'], 'e(o)') +
78
- ',next:' +
79
- createEffectfulFunction(features, ['v'], 'a&&(b.push(v),f(v,"next"))') +
80
- ',throw:' +
81
- createEffectfulFunction(
82
- features,
83
- ['v'],
84
- 'a&&(b.push(v),f(v,"throw"),a=s=!1,l.length=0)',
85
- ) +
86
- ',return:' +
87
- createEffectfulFunction(
88
- features,
89
- ['v'],
90
- 'a&&(b.push(v),f(v,"return"),a=!1,s=!0,l.length=0)',
91
- ) +
92
- '})',
93
- );
94
- }
95
-
96
- export function serializeSpecialReferenceValue(
97
- features: number,
98
- ref: SpecialReference,
99
- ): string {
100
- switch (ref) {
101
- case SpecialReference.MapSentinel:
102
- return '[]';
103
- case SpecialReference.PromiseConstructor:
104
- return serializePromiseConstructor(features);
105
- case SpecialReference.PromiseSuccess:
106
- return serializePromiseSuccess(features);
107
- case SpecialReference.PromiseFailure:
108
- return serializePromiseFailure(features);
109
- case SpecialReference.StreamConstructor:
110
- return serializeStreamConstructor(features);
111
- default:
112
- return '';
113
- }
114
- }
34
+ export const SPECIAL_REF_STRING: Record<SpecialReference, string> = {
35
+ [SpecialReference.MapSentinel]: '[]',
36
+ [SpecialReference.PromiseConstructor]: SERIALIZED_PROMISE_CONSTRUCTOR,
37
+ [SpecialReference.PromiseSuccess]: SERIALIZED_PROMISE_SUCCESS,
38
+ [SpecialReference.PromiseFailure]: SERIALIZED_PROMISE_FAILURE,
39
+ [SpecialReference.StreamConstructor]: SERIALIZED_STREAM_CONSTRUCTOR,
40
+ [SpecialReference.ArrayBufferConstructor]:
41
+ SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR,
42
+ };