seroval 1.3.1 → 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 +2637 -2682
  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 +2637 -2682
  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 -37
  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 +504 -307
  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 -499
  99. package/src/core/context/parser/stream.ts +0 -323
  100. package/src/core/context/parser/sync.ts +0 -408
  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,408 +0,0 @@
1
- import {
2
- createAggregateErrorNode,
3
- createArrayBufferNode,
4
- createArrayNode,
5
- createAsyncIteratorFactoryInstanceNode,
6
- createBigIntNode,
7
- createBigIntTypedArrayNode,
8
- createBoxedNode,
9
- createDataViewNode,
10
- createDateNode,
11
- createErrorNode,
12
- createIteratorFactoryInstanceNode,
13
- createNumberNode,
14
- createPluginNode,
15
- createRegExpNode,
16
- createSetNode,
17
- createStreamConstructorNode,
18
- createStringNode,
19
- createTypedArrayNode,
20
- } from '../../base-primitives';
21
- import { Feature } from '../../compat';
22
- import { NIL } from '../../constants';
23
- import { SerovalParserError, SerovalUnsupportedTypeError } from '../../errors';
24
- import {
25
- FALSE_NODE,
26
- NULL_NODE,
27
- TRUE_NODE,
28
- UNDEFINED_NODE,
29
- } from '../../literals';
30
- import { OpaqueReference } from '../../opaque-reference';
31
- import { SpecialReference } from '../../special-reference';
32
- import type { Stream } from '../../stream';
33
- import { createStream, isStream } from '../../stream';
34
- import { serializeString } from '../../string';
35
- import type {
36
- SerovalAggregateErrorNode,
37
- SerovalArrayNode,
38
- SerovalBigIntTypedArrayNode,
39
- SerovalBoxedNode,
40
- SerovalDataViewNode,
41
- SerovalErrorNode,
42
- SerovalMapNode,
43
- SerovalNode,
44
- SerovalNullConstructorNode,
45
- SerovalObjectNode,
46
- SerovalObjectRecordKey,
47
- SerovalObjectRecordNode,
48
- SerovalPluginNode,
49
- SerovalPromiseConstructorNode,
50
- SerovalSetNode,
51
- SerovalTypedArrayNode,
52
- } from '../../types';
53
- import { getErrorOptions } from '../../utils/error';
54
- import { iteratorToSequence } from '../../utils/iterator-to-sequence';
55
- import type {
56
- BigIntTypedArrayValue,
57
- TypedArrayValue,
58
- } from '../../utils/typed-array';
59
- import type { BaseParserContextOptions } from '../parser';
60
- import { BaseParserContext, ParserNodeType } from '../parser';
61
-
62
- type ObjectLikeNode = SerovalObjectNode | SerovalNullConstructorNode;
63
-
64
- export type BaseSyncParserContextOptions = BaseParserContextOptions;
65
-
66
- export default abstract class BaseSyncParserContext extends BaseParserContext {
67
- protected parseItems(current: unknown[]): SerovalNode[] {
68
- const nodes = [];
69
- for (let i = 0, len = current.length; i < len; i++) {
70
- if (i in current) {
71
- nodes[i] = this.parseTop(current[i]);
72
- }
73
- }
74
- return nodes;
75
- }
76
-
77
- protected parseArray(id: number, current: unknown[]): SerovalArrayNode {
78
- return createArrayNode(id, current, this.parseItems(current));
79
- }
80
-
81
- protected parseProperties(
82
- properties: Record<string | symbol, unknown>,
83
- ): SerovalObjectRecordNode {
84
- const entries = Object.entries(properties);
85
- const keyNodes: SerovalObjectRecordKey[] = [];
86
- const valueNodes: SerovalNode[] = [];
87
- for (let i = 0, len = entries.length; i < len; i++) {
88
- keyNodes.push(serializeString(entries[i][0]));
89
- valueNodes.push(this.parseTop(entries[i][1]));
90
- }
91
- // Check special properties, symbols in this case
92
- let symbol = Symbol.iterator;
93
- if (symbol in properties) {
94
- keyNodes.push(this.parseWellKnownSymbol(symbol));
95
- valueNodes.push(
96
- createIteratorFactoryInstanceNode(
97
- this.parseIteratorFactory(),
98
- this.parseTop(
99
- iteratorToSequence(properties as unknown as Iterable<unknown>),
100
- ),
101
- ),
102
- );
103
- }
104
- symbol = Symbol.asyncIterator;
105
- if (symbol in properties) {
106
- keyNodes.push(this.parseWellKnownSymbol(symbol));
107
- valueNodes.push(
108
- createAsyncIteratorFactoryInstanceNode(
109
- this.parseAsyncIteratorFactory(),
110
- this.parseTop(createStream()),
111
- ),
112
- );
113
- }
114
- symbol = Symbol.toStringTag;
115
- if (symbol in properties) {
116
- keyNodes.push(this.parseWellKnownSymbol(symbol));
117
- valueNodes.push(createStringNode(properties[symbol] as string));
118
- }
119
- symbol = Symbol.isConcatSpreadable;
120
- if (symbol in properties) {
121
- keyNodes.push(this.parseWellKnownSymbol(symbol));
122
- valueNodes.push(properties[symbol] ? TRUE_NODE : FALSE_NODE);
123
- }
124
- return {
125
- k: keyNodes,
126
- v: valueNodes,
127
- s: keyNodes.length,
128
- };
129
- }
130
-
131
- protected parsePlainObject(
132
- id: number,
133
- current: Record<string, unknown>,
134
- empty: boolean,
135
- ): ObjectLikeNode {
136
- return this.createObjectNode(
137
- id,
138
- current,
139
- empty,
140
- this.parseProperties(current),
141
- );
142
- }
143
-
144
- protected parseBoxed(id: number, current: object): SerovalBoxedNode {
145
- return createBoxedNode(id, this.parseTop(current.valueOf()));
146
- }
147
-
148
- protected parseTypedArray(
149
- id: number,
150
- current: TypedArrayValue,
151
- ): SerovalTypedArrayNode {
152
- return createTypedArrayNode(id, current, this.parseTop(current.buffer));
153
- }
154
-
155
- protected parseBigIntTypedArray(
156
- id: number,
157
- current: BigIntTypedArrayValue,
158
- ): SerovalBigIntTypedArrayNode {
159
- return createBigIntTypedArrayNode(
160
- id,
161
- current,
162
- this.parseTop(current.buffer),
163
- );
164
- }
165
-
166
- protected parseDataView(id: number, current: DataView): SerovalDataViewNode {
167
- return createDataViewNode(id, current, this.parseTop(current.buffer));
168
- }
169
-
170
- protected parseError(id: number, current: Error): SerovalErrorNode {
171
- const options = getErrorOptions(current, this.features);
172
- return createErrorNode(
173
- id,
174
- current,
175
- options ? this.parseProperties(options) : NIL,
176
- );
177
- }
178
-
179
- protected parseAggregateError(
180
- id: number,
181
- current: AggregateError,
182
- ): SerovalAggregateErrorNode {
183
- const options = getErrorOptions(current, this.features);
184
- return createAggregateErrorNode(
185
- id,
186
- current,
187
- options ? this.parseProperties(options) : NIL,
188
- );
189
- }
190
-
191
- protected parseMap(
192
- id: number,
193
- current: Map<unknown, unknown>,
194
- ): SerovalMapNode {
195
- const keyNodes: SerovalNode[] = [];
196
- const valueNodes: SerovalNode[] = [];
197
- for (const [key, value] of current.entries()) {
198
- keyNodes.push(this.parseTop(key));
199
- valueNodes.push(this.parseTop(value));
200
- }
201
- return this.createMapNode(id, keyNodes, valueNodes, current.size);
202
- }
203
-
204
- protected parseSet(id: number, current: Set<unknown>): SerovalSetNode {
205
- const items: SerovalNode[] = [];
206
- for (const item of current.keys()) {
207
- items.push(this.parseTop(item));
208
- }
209
- return createSetNode(id, current.size, items);
210
- }
211
-
212
- protected parsePlugin(
213
- id: number,
214
- current: unknown,
215
- ): SerovalPluginNode | undefined {
216
- const currentPlugins = this.plugins;
217
- if (currentPlugins) {
218
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
219
- const plugin = currentPlugins[i];
220
- if (plugin.parse.sync && plugin.test(current)) {
221
- return createPluginNode(
222
- id,
223
- plugin.tag,
224
- plugin.parse.sync(current, this, {
225
- id,
226
- }),
227
- );
228
- }
229
- }
230
- }
231
- return undefined;
232
- }
233
-
234
- protected parseStream(id: number, _current: Stream<unknown>): SerovalNode {
235
- return createStreamConstructorNode(
236
- id,
237
- this.parseSpecialReference(SpecialReference.StreamConstructor),
238
- [],
239
- );
240
- }
241
-
242
- protected parsePromise(
243
- id: number,
244
- _current: Promise<unknown>,
245
- ): SerovalPromiseConstructorNode {
246
- return this.createPromiseConstructorNode(id, this.createIndex({}));
247
- }
248
-
249
- // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: ehh
250
- protected parseObject(id: number, current: object): SerovalNode {
251
- if (Array.isArray(current)) {
252
- return this.parseArray(id, current);
253
- }
254
- if (isStream(current)) {
255
- return this.parseStream(id, current);
256
- }
257
- const currentClass = current.constructor;
258
- if (currentClass === OpaqueReference) {
259
- return this.parseTop(
260
- (current as OpaqueReference<unknown, unknown>).replacement,
261
- );
262
- }
263
- const parsed = this.parsePlugin(id, current);
264
- if (parsed) {
265
- return parsed;
266
- }
267
- switch (currentClass) {
268
- case Object:
269
- return this.parsePlainObject(
270
- id,
271
- current as Record<string, unknown>,
272
- false,
273
- );
274
- case undefined:
275
- return this.parsePlainObject(
276
- id,
277
- current as Record<string, unknown>,
278
- true,
279
- );
280
- case Date:
281
- return createDateNode(id, current as unknown as Date);
282
- case RegExp:
283
- return createRegExpNode(id, current as unknown as RegExp);
284
- case Error:
285
- case EvalError:
286
- case RangeError:
287
- case ReferenceError:
288
- case SyntaxError:
289
- case TypeError:
290
- case URIError:
291
- return this.parseError(id, current as unknown as Error);
292
- case Number:
293
- case Boolean:
294
- case String:
295
- case BigInt:
296
- return this.parseBoxed(id, current);
297
- case ArrayBuffer:
298
- return createArrayBufferNode(id, current as unknown as ArrayBuffer);
299
- case Int8Array:
300
- case Int16Array:
301
- case Int32Array:
302
- case Uint8Array:
303
- case Uint16Array:
304
- case Uint32Array:
305
- case Uint8ClampedArray:
306
- case Float32Array:
307
- case Float64Array:
308
- return this.parseTypedArray(id, current as unknown as TypedArrayValue);
309
- case DataView:
310
- return this.parseDataView(id, current as unknown as DataView);
311
- case Map:
312
- return this.parseMap(id, current as unknown as Map<unknown, unknown>);
313
- case Set:
314
- return this.parseSet(id, current as unknown as Set<unknown>);
315
- default:
316
- break;
317
- }
318
- // Promises
319
- if (currentClass === Promise || current instanceof Promise) {
320
- return this.parsePromise(id, current as unknown as Promise<unknown>);
321
- }
322
- const currentFeatures = this.features;
323
- // BigInt Typed Arrays
324
- if (currentFeatures & Feature.BigIntTypedArray) {
325
- switch (currentClass) {
326
- case BigInt64Array:
327
- case BigUint64Array:
328
- return this.parseBigIntTypedArray(
329
- id,
330
- current as unknown as BigIntTypedArrayValue,
331
- );
332
- default:
333
- break;
334
- }
335
- }
336
- if (
337
- currentFeatures & Feature.AggregateError &&
338
- typeof AggregateError !== 'undefined' &&
339
- (currentClass === AggregateError || current instanceof AggregateError)
340
- ) {
341
- return this.parseAggregateError(id, current as unknown as AggregateError);
342
- }
343
- // Slow path. We only need to handle Errors and Iterators
344
- // since they have very broad implementations.
345
- if (current instanceof Error) {
346
- return this.parseError(id, current);
347
- }
348
- // Generator functions don't have a global constructor
349
- // despite existing
350
- if (Symbol.iterator in current || Symbol.asyncIterator in current) {
351
- return this.parsePlainObject(id, current, !!currentClass);
352
- }
353
- throw new SerovalUnsupportedTypeError(current);
354
- }
355
-
356
- protected parseFunction(current: unknown): SerovalNode {
357
- const ref = this.getReference(current);
358
- if (ref.type !== ParserNodeType.Fresh) {
359
- return ref.value;
360
- }
361
- const plugin = this.parsePlugin(ref.value, current);
362
- if (plugin) {
363
- return plugin;
364
- }
365
- throw new SerovalUnsupportedTypeError(current);
366
- }
367
-
368
- parseTop<T>(current: T): SerovalNode {
369
- switch (typeof current) {
370
- case 'boolean':
371
- return current ? TRUE_NODE : FALSE_NODE;
372
- case 'undefined':
373
- return UNDEFINED_NODE;
374
- case 'string':
375
- return createStringNode(current as string);
376
- case 'number':
377
- return createNumberNode(current as number);
378
- case 'bigint':
379
- return createBigIntNode(current as bigint);
380
- case 'object': {
381
- if (current) {
382
- const ref = this.getReference(current);
383
- return ref.type === ParserNodeType.Fresh
384
- ? this.parseObject(ref.value, current as object)
385
- : ref.value;
386
- }
387
- return NULL_NODE;
388
- }
389
- case 'symbol':
390
- return this.parseWellKnownSymbol(current);
391
- case 'function': {
392
- return this.parseFunction(current);
393
- }
394
- default:
395
- throw new SerovalUnsupportedTypeError(current);
396
- }
397
- }
398
-
399
- parse<T>(current: T): SerovalNode {
400
- try {
401
- return this.parseTop(current);
402
- } catch (error) {
403
- throw error instanceof SerovalParserError
404
- ? error
405
- : new SerovalParserError(error);
406
- }
407
- }
408
- }
@@ -1,9 +0,0 @@
1
- import BaseAsyncParserContext from '../context/parser/async';
2
- import type { SerovalMode } from '../plugin';
3
- import type { CrossParserContextOptions } from './parser';
4
-
5
- export type CrossAsyncParserContextOptions = CrossParserContextOptions;
6
-
7
- export default class CrossAsyncParserContext extends BaseAsyncParserContext {
8
- readonly mode: SerovalMode = 'cross';
9
- }
@@ -1,16 +0,0 @@
1
- import type { BaseDeserializerOptions } from '../context/deserializer';
2
- import BaseDeserializerContext from '../context/deserializer';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export type CrossDeserializerContextOptions = BaseDeserializerOptions;
6
-
7
- export default class CrossDeserializerContext extends BaseDeserializerContext {
8
- readonly mode: SerovalMode = 'cross';
9
-
10
- assignIndexedValue<T>(index: number, value: T): T {
11
- if (!this.refs.has(index)) {
12
- this.refs.set(index, value);
13
- }
14
- return value;
15
- }
16
- }
@@ -1,7 +0,0 @@
1
- import type { BaseParserContextOptions } from '../context/parser';
2
-
3
- export type CrossParserContextOptions = BaseParserContextOptions;
4
-
5
- export interface CrossContextOptions {
6
- scopeId?: string;
7
- }
@@ -1,72 +0,0 @@
1
- import { SerovalNodeType } from '../constants';
2
- import type { BaseSerializerContextOptions } from '../context/serializer';
3
- import BaseSerializerContext from '../context/serializer';
4
- import { GLOBAL_CONTEXT_REFERENCES } from '../keys';
5
- import type { SerovalMode } from '../plugin';
6
- import { serializeString } from '../string';
7
- import type { SerovalNode } from '../types';
8
- import type { CrossContextOptions } from './parser';
9
-
10
- export interface CrossSerializerContextOptions
11
- extends BaseSerializerContextOptions,
12
- CrossContextOptions {}
13
-
14
- export default class CrossSerializerContext extends BaseSerializerContext {
15
- readonly mode: SerovalMode = 'cross';
16
-
17
- scopeId?: string;
18
-
19
- constructor(options: CrossSerializerContextOptions) {
20
- super(options);
21
- this.scopeId = options.scopeId;
22
- }
23
-
24
- getRefParam(id: number): string {
25
- return GLOBAL_CONTEXT_REFERENCES + '[' + id + ']';
26
- }
27
-
28
- protected assignIndexedValue(index: number, value: string): string {
29
- // In cross-reference, we have to assume that
30
- // every reference are going to be referenced
31
- // in the future, and so we need to store
32
- // all of it into the reference array.
33
- return this.getRefParam(index) + '=' + value;
34
- }
35
-
36
- serializeTop(tree: SerovalNode): string {
37
- // Get the serialized result
38
- const result = this.serialize(tree);
39
- // If the node is a non-reference, return
40
- // the result immediately
41
- const id = tree.i;
42
- if (id == null) {
43
- return result;
44
- }
45
- // Get the patches
46
- const patches = this.resolvePatches();
47
- // Get the variable that represents the root
48
- const ref = this.getRefParam(id);
49
- // Parameters needed for scoping
50
- const params = this.scopeId == null ? '' : GLOBAL_CONTEXT_REFERENCES;
51
- // If there are patches, append it after the result
52
- const body = patches ? '(' + result + ',' + patches + ref + ')' : result;
53
- // If there are no params, there's no need to generate a function
54
- if (params === '') {
55
- if (tree.t === SerovalNodeType.Object && !patches) {
56
- return '(' + body + ')';
57
- }
58
- return body;
59
- }
60
- // Get the arguments for the IIFE
61
- const args =
62
- this.scopeId == null
63
- ? '()'
64
- : '(' +
65
- GLOBAL_CONTEXT_REFERENCES +
66
- '["' +
67
- serializeString(this.scopeId) +
68
- '"])';
69
- // Create the IIFE
70
- return '(' + this.createFunction([params], body) + ')' + args;
71
- }
72
- }
@@ -1,9 +0,0 @@
1
- import type { BaseStreamParserContextOptions } from '../context/parser/stream';
2
- import BaseStreamParserContext from '../context/parser/stream';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export type CrossStreamParserContextOptions = BaseStreamParserContextOptions;
6
-
7
- export default class CrossStreamParserContext extends BaseStreamParserContext {
8
- readonly mode: SerovalMode = 'cross';
9
- }
@@ -1,9 +0,0 @@
1
- import BaseSyncParserContext from '../context/parser/sync';
2
- import type { SerovalMode } from '../plugin';
3
- import type { CrossParserContextOptions } from './parser';
4
-
5
- export type CrossSyncParserContextOptions = CrossParserContextOptions;
6
-
7
- export default class CrossSyncParserContext extends BaseSyncParserContext {
8
- readonly mode: SerovalMode = 'cross';
9
- }
@@ -1,9 +0,0 @@
1
- import type { BaseParserContextOptions } from '../context/parser';
2
- import BaseAsyncParserContext from '../context/parser/async';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export type AsyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
6
-
7
- export default class AsyncParserContext extends BaseAsyncParserContext {
8
- readonly mode: SerovalMode = 'vanilla';
9
- }
@@ -1,26 +0,0 @@
1
- import type { BaseDeserializerOptions } from '../context/deserializer';
2
- import BaseDeserializerContext from '../context/deserializer';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export interface VanillaDeserializerContextOptions
6
- extends Omit<BaseDeserializerOptions, 'refs'> {
7
- markedRefs: number[] | Set<number>;
8
- }
9
-
10
- export default class VanillaDeserializerContext extends BaseDeserializerContext {
11
- readonly mode: SerovalMode = 'vanilla';
12
-
13
- marked: Set<number>;
14
-
15
- constructor(options: VanillaDeserializerContextOptions) {
16
- super(options);
17
- this.marked = new Set(options.markedRefs);
18
- }
19
-
20
- assignIndexedValue<T>(index: number, value: T): T {
21
- if (this.marked.has(index)) {
22
- this.refs.set(index, value);
23
- }
24
- return value;
25
- }
26
- }
@@ -1,98 +0,0 @@
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';
6
- import type {
7
- SerovalNode,
8
- SerovalPromiseConstructorNode,
9
- SerovalPromiseRejectNode,
10
- SerovalPromiseResolveNode,
11
- } from '../types';
12
- import getIdentifier from '../utils/get-identifier';
13
-
14
- export type VanillaSerializerContextOptions = BaseSerializerContextOptions;
15
-
16
- export default class VanillaSerializerContext extends BaseSerializerContext {
17
- readonly mode: SerovalMode = 'vanilla';
18
-
19
- /**
20
- * Map tree refs to actual refs
21
- * @private
22
- */
23
- valid = new Map<number, number>();
24
-
25
- /**
26
- * Variables
27
- * @private
28
- */
29
- vars: string[] = [];
30
-
31
- /**
32
- * Creates the reference param (identifier) from the given reference ID
33
- * Calling this function means the value has been referenced somewhere
34
- */
35
- getRefParam(index: number): string {
36
- /**
37
- * Creates a new reference ID from a given reference ID
38
- * This new reference ID means that the reference itself
39
- * has been referenced at least once, and is used to generate
40
- * the variables
41
- */
42
- let actualIndex = this.valid.get(index);
43
- if (actualIndex == null) {
44
- actualIndex = this.valid.size;
45
- this.valid.set(index, actualIndex);
46
- }
47
- let identifier = this.vars[actualIndex];
48
- if (identifier == null) {
49
- identifier = getIdentifier(actualIndex);
50
- this.vars[actualIndex] = identifier;
51
- }
52
- return identifier;
53
- }
54
-
55
- protected assignIndexedValue(index: number, value: string): string {
56
- if (this.isMarked(index)) {
57
- return this.getRefParam(index) + '=' + value;
58
- }
59
- return value;
60
- }
61
-
62
- protected serializePromiseConstructor(
63
- node: SerovalPromiseConstructorNode,
64
- ): string {
65
- throw new SerovalUnsupportedNodeError(node);
66
- }
67
-
68
- protected serializePromiseResolve(node: SerovalPromiseResolveNode): string {
69
- throw new SerovalUnsupportedNodeError(node);
70
- }
71
-
72
- protected serializePromiseReject(node: SerovalPromiseRejectNode): string {
73
- throw new SerovalUnsupportedNodeError(node);
74
- }
75
-
76
- serializeTop(tree: SerovalNode): string {
77
- const result = this.serialize(tree);
78
- // Shared references detected
79
- if (tree.i != null && this.vars.length) {
80
- const patches = this.resolvePatches();
81
- let body = result;
82
- if (patches) {
83
- // Get (or create) a ref from the source
84
- const index = this.getRefParam(tree.i);
85
- body = result + ',' + patches + index;
86
- if (!result.startsWith(index + '=')) {
87
- body = index + '=' + body;
88
- }
89
- body = '(' + body + ')';
90
- }
91
- return '(' + this.createFunction(this.vars, body) + ')()';
92
- }
93
- if (tree.t === SerovalNodeType.Object) {
94
- return '(' + result + ')';
95
- }
96
- return result;
97
- }
98
- }
@@ -1,9 +0,0 @@
1
- import BaseSyncParserContext from '../context/parser/sync';
2
- import type { BaseParserContextOptions } from '../context/parser';
3
- import type { SerovalMode } from '../plugin';
4
-
5
- export type SyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
6
-
7
- export default class SyncParserContext extends BaseSyncParserContext {
8
- readonly mode: SerovalMode = 'vanilla';
9
- }
@@ -1,5 +0,0 @@
1
- export default function assert(cond: unknown, error: Error): asserts cond {
2
- if (!cond) {
3
- throw error;
4
- }
5
- }