seroval 1.3.2 → 1.4.1

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 (125) hide show
  1. package/dist/cjs/development/index.cjs +2817 -2700
  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 +2817 -2700
  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 +3 -4
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +3 -2
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +2 -1
  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 +20 -0
  16. package/dist/types/core/context/async-parser.d.ts.map +1 -0
  17. package/dist/types/core/context/deserializer.d.ts +40 -38
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts +34 -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 +9 -0
  28. package/dist/types/core/errors.d.ts.map +1 -1
  29. package/dist/types/core/function-string.d.ts +2 -2
  30. package/dist/types/core/function-string.d.ts.map +1 -1
  31. package/dist/types/core/literals.d.ts.map +1 -1
  32. package/dist/types/core/node.d.ts +1 -1
  33. package/dist/types/core/node.d.ts.map +1 -1
  34. package/dist/types/core/plugin.d.ts +13 -11
  35. package/dist/types/core/plugin.d.ts.map +1 -1
  36. package/dist/types/core/reference.d.ts.map +1 -1
  37. package/dist/types/core/special-reference.d.ts +3 -2
  38. package/dist/types/core/special-reference.d.ts.map +1 -1
  39. package/dist/types/core/stream.d.ts +1 -2
  40. package/dist/types/core/stream.d.ts.map +1 -1
  41. package/dist/types/core/string.d.ts.map +1 -1
  42. package/dist/types/core/symbols.d.ts +14 -0
  43. package/dist/types/core/symbols.d.ts.map +1 -0
  44. package/dist/types/core/tree/index.d.ts +7 -3
  45. package/dist/types/core/tree/index.d.ts.map +1 -1
  46. package/dist/types/core/types.d.ts +4 -11
  47. package/dist/types/core/types.d.ts.map +1 -1
  48. package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -1
  49. package/dist/types/index.d.ts +10 -5
  50. package/dist/types/index.d.ts.map +1 -1
  51. package/package.json +18 -21
  52. package/dist/types/core/context/parser/async.d.ts +0 -24
  53. package/dist/types/core/context/parser/async.d.ts.map +0 -1
  54. package/dist/types/core/context/parser/stream.d.ts +0 -43
  55. package/dist/types/core/context/parser/stream.d.ts.map +0 -1
  56. package/dist/types/core/context/parser/sync.d.ts +0 -30
  57. package/dist/types/core/context/parser/sync.d.ts.map +0 -1
  58. package/dist/types/core/cross/async.d.ts +0 -8
  59. package/dist/types/core/cross/async.d.ts.map +0 -1
  60. package/dist/types/core/cross/deserializer.d.ts +0 -9
  61. package/dist/types/core/cross/deserializer.d.ts.map +0 -1
  62. package/dist/types/core/cross/parser.d.ts +0 -6
  63. package/dist/types/core/cross/parser.d.ts.map +0 -1
  64. package/dist/types/core/cross/serializer.d.ts +0 -16
  65. package/dist/types/core/cross/serializer.d.ts.map +0 -1
  66. package/dist/types/core/cross/stream.d.ts +0 -8
  67. package/dist/types/core/cross/stream.d.ts.map +0 -1
  68. package/dist/types/core/cross/sync.d.ts +0 -8
  69. package/dist/types/core/cross/sync.d.ts.map +0 -1
  70. package/dist/types/core/tree/async.d.ts +0 -8
  71. package/dist/types/core/tree/async.d.ts.map +0 -1
  72. package/dist/types/core/tree/deserializer.d.ts +0 -13
  73. package/dist/types/core/tree/deserializer.d.ts.map +0 -1
  74. package/dist/types/core/tree/serializer.d.ts +0 -29
  75. package/dist/types/core/tree/serializer.d.ts.map +0 -1
  76. package/dist/types/core/tree/sync.d.ts +0 -8
  77. package/dist/types/core/tree/sync.d.ts.map +0 -1
  78. package/dist/types/core/utils/assert.d.ts +0 -2
  79. package/dist/types/core/utils/assert.d.ts.map +0 -1
  80. package/dist/types/core/utils/deferred.d.ts +0 -7
  81. package/dist/types/core/utils/deferred.d.ts.map +0 -1
  82. package/src/core/Serializer.ts +0 -119
  83. package/src/core/base-primitives.ts +0 -542
  84. package/src/core/compat.ts +0 -20
  85. package/src/core/constants.ts +0 -185
  86. package/src/core/context/deserializer.ts +0 -456
  87. package/src/core/context/parser/async.ts +0 -495
  88. package/src/core/context/parser/stream.ts +0 -323
  89. package/src/core/context/parser/sync.ts +0 -404
  90. package/src/core/context/parser.ts +0 -270
  91. package/src/core/context/serializer.ts +0 -1180
  92. package/src/core/cross/async.ts +0 -9
  93. package/src/core/cross/deserializer.ts +0 -16
  94. package/src/core/cross/index.ts +0 -170
  95. package/src/core/cross/parser.ts +0 -7
  96. package/src/core/cross/serializer.ts +0 -72
  97. package/src/core/cross/stream.ts +0 -9
  98. package/src/core/cross/sync.ts +0 -9
  99. package/src/core/errors.ts +0 -102
  100. package/src/core/function-string.ts +0 -31
  101. package/src/core/keys.ts +0 -17
  102. package/src/core/literals.ts +0 -43
  103. package/src/core/node.ts +0 -40
  104. package/src/core/opaque-reference.ts +0 -9
  105. package/src/core/plugin.ts +0 -91
  106. package/src/core/reference.ts +0 -63
  107. package/src/core/special-reference.ts +0 -114
  108. package/src/core/stream.ts +0 -205
  109. package/src/core/string.ts +0 -84
  110. package/src/core/tree/async.ts +0 -9
  111. package/src/core/tree/deserializer.ts +0 -26
  112. package/src/core/tree/index.ts +0 -111
  113. package/src/core/tree/serializer.ts +0 -98
  114. package/src/core/tree/sync.ts +0 -9
  115. package/src/core/types.ts +0 -370
  116. package/src/core/utils/assert.ts +0 -5
  117. package/src/core/utils/deferred.ts +0 -22
  118. package/src/core/utils/error.ts +0 -73
  119. package/src/core/utils/get-identifier.ts +0 -18
  120. package/src/core/utils/get-object-flag.ts +0 -14
  121. package/src/core/utils/is-valid-identifier.ts +0 -12
  122. package/src/core/utils/iterator-to-sequence.ts +0 -66
  123. package/src/core/utils/promise-to-result.ts +0 -9
  124. package/src/core/utils/typed-array.ts +0 -56
  125. package/src/index.ts +0 -18
@@ -1,495 +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
- createStreamNextNode,
19
- createStreamReturnNode,
20
- createStreamThrowNode,
21
- createStringNode,
22
- createTypedArrayNode,
23
- } from '../../base-primitives';
24
- import { Feature } from '../../compat';
25
- import { NIL, SerovalNodeType } from '../../constants';
26
- import { SerovalParserError, SerovalUnsupportedTypeError } from '../../errors';
27
- import {
28
- FALSE_NODE,
29
- NULL_NODE,
30
- TRUE_NODE,
31
- UNDEFINED_NODE,
32
- } from '../../literals';
33
- import { createSerovalNode } from '../../node';
34
- import { OpaqueReference } from '../../opaque-reference';
35
- import { SpecialReference } from '../../special-reference';
36
- import type { Stream } from '../../stream';
37
- import { createStreamFromAsyncIterable, isStream } from '../../stream';
38
- import { serializeString } from '../../string';
39
- import type {
40
- SerovalAggregateErrorNode,
41
- SerovalArrayNode,
42
- SerovalBigIntTypedArrayNode,
43
- SerovalBoxedNode,
44
- SerovalDataViewNode,
45
- SerovalErrorNode,
46
- SerovalMapNode,
47
- SerovalNode,
48
- SerovalNullConstructorNode,
49
- SerovalObjectNode,
50
- SerovalObjectRecordKey,
51
- SerovalObjectRecordNode,
52
- SerovalPluginNode,
53
- SerovalPromiseNode,
54
- SerovalSetNode,
55
- SerovalStreamConstructorNode,
56
- SerovalTypedArrayNode,
57
- } from '../../types';
58
- import { getErrorOptions } from '../../utils/error';
59
- import { iteratorToSequence } from '../../utils/iterator-to-sequence';
60
- import promiseToResult from '../../utils/promise-to-result';
61
- import type {
62
- BigIntTypedArrayValue,
63
- TypedArrayValue,
64
- } from '../../utils/typed-array';
65
- import { BaseParserContext, ParserNodeType } from '../parser';
66
-
67
- type ObjectLikeNode =
68
- | SerovalObjectNode
69
- | SerovalNullConstructorNode
70
- | SerovalPromiseNode;
71
-
72
- export default abstract class BaseAsyncParserContext extends BaseParserContext {
73
- private async parseItems(current: unknown[]): Promise<SerovalNode[]> {
74
- const nodes = [];
75
- for (let i = 0, len = current.length; i < len; i++) {
76
- // For consistency in holes
77
- if (i in current) {
78
- nodes[i] = await this.parse(current[i]);
79
- }
80
- }
81
- return nodes;
82
- }
83
-
84
- private async parseArray(
85
- id: number,
86
- current: unknown[],
87
- ): Promise<SerovalArrayNode> {
88
- return createArrayNode(id, current, await this.parseItems(current));
89
- }
90
-
91
- private async parseProperties(
92
- properties: Record<string | symbol, unknown>,
93
- ): Promise<SerovalObjectRecordNode> {
94
- const entries = Object.entries(properties);
95
- const keyNodes: SerovalObjectRecordKey[] = [];
96
- const valueNodes: SerovalNode[] = [];
97
- for (let i = 0, len = entries.length; i < len; i++) {
98
- keyNodes.push(serializeString(entries[i][0]));
99
- valueNodes.push(await this.parse(entries[i][1]));
100
- }
101
- // Check special properties
102
- let symbol = Symbol.iterator;
103
- if (symbol in properties) {
104
- keyNodes.push(this.parseWellKnownSymbol(symbol));
105
- valueNodes.push(
106
- createIteratorFactoryInstanceNode(
107
- this.parseIteratorFactory(),
108
- await this.parse(
109
- iteratorToSequence(properties as unknown as Iterable<unknown>),
110
- ),
111
- ),
112
- );
113
- }
114
- symbol = Symbol.asyncIterator;
115
- if (symbol in properties) {
116
- keyNodes.push(this.parseWellKnownSymbol(symbol));
117
- valueNodes.push(
118
- createAsyncIteratorFactoryInstanceNode(
119
- this.parseAsyncIteratorFactory(),
120
- await this.parse(
121
- createStreamFromAsyncIterable(
122
- properties as unknown as AsyncIterable<unknown>,
123
- ),
124
- ),
125
- ),
126
- );
127
- }
128
- symbol = Symbol.toStringTag;
129
- if (symbol in properties) {
130
- keyNodes.push(this.parseWellKnownSymbol(symbol));
131
- valueNodes.push(createStringNode(properties[symbol] as string));
132
- }
133
- symbol = Symbol.isConcatSpreadable;
134
- if (symbol in properties) {
135
- keyNodes.push(this.parseWellKnownSymbol(symbol));
136
- valueNodes.push(properties[symbol] ? TRUE_NODE : FALSE_NODE);
137
- }
138
- return {
139
- k: keyNodes,
140
- v: valueNodes,
141
- s: keyNodes.length,
142
- };
143
- }
144
-
145
- private async parsePlainObject(
146
- id: number,
147
- current: Record<string, unknown>,
148
- empty: boolean,
149
- ): Promise<ObjectLikeNode> {
150
- return this.createObjectNode(
151
- id,
152
- current,
153
- empty,
154
- await this.parseProperties(current),
155
- );
156
- }
157
-
158
- private async parseBoxed(
159
- id: number,
160
- current: object,
161
- ): Promise<SerovalBoxedNode> {
162
- return createBoxedNode(id, await this.parse(current.valueOf()));
163
- }
164
-
165
- private async parseTypedArray(
166
- id: number,
167
- current: TypedArrayValue,
168
- ): Promise<SerovalTypedArrayNode> {
169
- return createTypedArrayNode(id, current, await this.parse(current.buffer));
170
- }
171
-
172
- private async parseBigIntTypedArray(
173
- id: number,
174
- current: BigIntTypedArrayValue,
175
- ): Promise<SerovalBigIntTypedArrayNode> {
176
- return createBigIntTypedArrayNode(
177
- id,
178
- current,
179
- await this.parse(current.buffer),
180
- );
181
- }
182
-
183
- private async parseDataView(
184
- id: number,
185
- current: DataView,
186
- ): Promise<SerovalDataViewNode> {
187
- return createDataViewNode(id, current, await this.parse(current.buffer));
188
- }
189
-
190
- private async parseError(
191
- id: number,
192
- current: Error,
193
- ): Promise<SerovalErrorNode> {
194
- const options = getErrorOptions(current, this.features);
195
- return createErrorNode(
196
- id,
197
- current,
198
- options ? await this.parseProperties(options) : NIL,
199
- );
200
- }
201
-
202
- private async parseAggregateError(
203
- id: number,
204
- current: AggregateError,
205
- ): Promise<SerovalAggregateErrorNode> {
206
- const options = getErrorOptions(current, this.features);
207
- return createAggregateErrorNode(
208
- id,
209
- current,
210
- options ? await this.parseProperties(options) : NIL,
211
- );
212
- }
213
-
214
- private async parseMap(
215
- id: number,
216
- current: Map<unknown, unknown>,
217
- ): Promise<SerovalMapNode> {
218
- const keyNodes: SerovalNode[] = [];
219
- const valueNodes: SerovalNode[] = [];
220
- for (const [key, value] of current.entries()) {
221
- keyNodes.push(await this.parse(key));
222
- valueNodes.push(await this.parse(value));
223
- }
224
- return this.createMapNode(id, keyNodes, valueNodes, current.size);
225
- }
226
-
227
- private async parseSet(
228
- id: number,
229
- current: Set<unknown>,
230
- ): Promise<SerovalSetNode> {
231
- const items: SerovalNode[] = [];
232
- for (const item of current.keys()) {
233
- items.push(await this.parse(item));
234
- }
235
- return createSetNode(id, current.size, items);
236
- }
237
-
238
- private async parsePromise(
239
- id: number,
240
- current: Promise<unknown>,
241
- ): Promise<SerovalPromiseNode> {
242
- const [status, result] = await promiseToResult(current);
243
-
244
- return createSerovalNode(
245
- SerovalNodeType.Promise,
246
- id,
247
- status,
248
- NIL,
249
- NIL,
250
- NIL,
251
- NIL,
252
- NIL,
253
- NIL,
254
- await this.parse(result),
255
- NIL,
256
- NIL,
257
- );
258
- }
259
-
260
- private async parsePlugin(
261
- id: number,
262
- current: unknown,
263
- ): Promise<SerovalPluginNode | undefined> {
264
- const currentPlugins = this.plugins;
265
- if (currentPlugins) {
266
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
267
- const plugin = currentPlugins[i];
268
- if (plugin.parse.async && plugin.test(current)) {
269
- return createPluginNode(
270
- id,
271
- plugin.tag,
272
- await plugin.parse.async(current, this, {
273
- id,
274
- }),
275
- );
276
- }
277
- }
278
- }
279
- return NIL;
280
- }
281
-
282
- private async parseStream(
283
- id: number,
284
- current: Stream<unknown>,
285
- ): Promise<SerovalStreamConstructorNode> {
286
- return createStreamConstructorNode(
287
- id,
288
- this.parseSpecialReference(SpecialReference.StreamConstructor),
289
- await new Promise<SerovalNode[]>((resolve, reject) => {
290
- const sequence: SerovalNode[] = [];
291
- const cleanup = current.on({
292
- next: value => {
293
- this.markRef(id);
294
- this.parse(value).then(
295
- data => {
296
- sequence.push(createStreamNextNode(id, data));
297
- },
298
- data => {
299
- reject(data);
300
- cleanup();
301
- },
302
- );
303
- },
304
- throw: value => {
305
- this.markRef(id);
306
- this.parse(value).then(
307
- data => {
308
- sequence.push(createStreamThrowNode(id, data));
309
- resolve(sequence);
310
- cleanup();
311
- },
312
- data => {
313
- reject(data);
314
- cleanup();
315
- },
316
- );
317
- },
318
- return: value => {
319
- this.markRef(id);
320
- this.parse(value).then(
321
- data => {
322
- sequence.push(createStreamReturnNode(id, data));
323
- resolve(sequence);
324
- cleanup();
325
- },
326
- data => {
327
- reject(data);
328
- cleanup();
329
- },
330
- );
331
- },
332
- });
333
- }),
334
- );
335
- }
336
-
337
- // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: ehh
338
- private async parseObject(id: number, current: object): Promise<SerovalNode> {
339
- if (Array.isArray(current)) {
340
- return this.parseArray(id, current);
341
- }
342
- if (isStream(current)) {
343
- return this.parseStream(id, current);
344
- }
345
- const currentClass = current.constructor;
346
- if (currentClass === OpaqueReference) {
347
- return this.parse(
348
- (current as OpaqueReference<unknown, unknown>).replacement,
349
- );
350
- }
351
- const parsed = await this.parsePlugin(id, current);
352
- if (parsed) {
353
- return parsed;
354
- }
355
- switch (currentClass) {
356
- case Object:
357
- return this.parsePlainObject(
358
- id,
359
- current as Record<string, unknown>,
360
- false,
361
- );
362
- case NIL:
363
- return this.parsePlainObject(
364
- id,
365
- current as Record<string, unknown>,
366
- true,
367
- );
368
- case Date:
369
- return createDateNode(id, current as unknown as Date);
370
- case RegExp:
371
- return createRegExpNode(id, current as unknown as RegExp);
372
- case Error:
373
- case EvalError:
374
- case RangeError:
375
- case ReferenceError:
376
- case SyntaxError:
377
- case TypeError:
378
- case URIError:
379
- return this.parseError(id, current as unknown as Error);
380
- case Number:
381
- case Boolean:
382
- case String:
383
- case BigInt:
384
- return this.parseBoxed(id, current);
385
- case ArrayBuffer:
386
- return createArrayBufferNode(id, current as unknown as ArrayBuffer);
387
- case Int8Array:
388
- case Int16Array:
389
- case Int32Array:
390
- case Uint8Array:
391
- case Uint16Array:
392
- case Uint32Array:
393
- case Uint8ClampedArray:
394
- case Float32Array:
395
- case Float64Array:
396
- return this.parseTypedArray(id, current as unknown as TypedArrayValue);
397
- case DataView:
398
- return this.parseDataView(id, current as unknown as DataView);
399
- case Map:
400
- return this.parseMap(id, current as unknown as Map<unknown, unknown>);
401
- case Set:
402
- return this.parseSet(id, current as unknown as Set<unknown>);
403
- default:
404
- break;
405
- }
406
- // Promises
407
- if (currentClass === Promise || current instanceof Promise) {
408
- return this.parsePromise(id, current as unknown as Promise<unknown>);
409
- }
410
- const currentFeatures = this.features;
411
- // BigInt Typed Arrays
412
- if (currentFeatures & Feature.BigIntTypedArray) {
413
- switch (currentClass) {
414
- case BigInt64Array:
415
- case BigUint64Array:
416
- return this.parseBigIntTypedArray(
417
- id,
418
- current as unknown as BigIntTypedArrayValue,
419
- );
420
- default:
421
- break;
422
- }
423
- }
424
- if (
425
- currentFeatures & Feature.AggregateError &&
426
- typeof AggregateError !== 'undefined' &&
427
- (currentClass === AggregateError || current instanceof AggregateError)
428
- ) {
429
- return this.parseAggregateError(id, current as unknown as AggregateError);
430
- }
431
- // Slow path. We only need to handle Errors and Iterators
432
- // since they have very broad implementations.
433
- if (current instanceof Error) {
434
- return this.parseError(id, current);
435
- }
436
- // Generator functions don't have a global constructor
437
- // despite existing
438
- if (Symbol.iterator in current || Symbol.asyncIterator in current) {
439
- return this.parsePlainObject(id, current, !!currentClass);
440
- }
441
- throw new SerovalUnsupportedTypeError(current);
442
- }
443
-
444
- protected async parseFunction(current: unknown): Promise<SerovalNode> {
445
- const ref = this.getReference(current);
446
- if (ref.type !== ParserNodeType.Fresh) {
447
- return ref.value;
448
- }
449
- const plugin = await this.parsePlugin(ref.value, current);
450
- if (plugin) {
451
- return plugin;
452
- }
453
- throw new SerovalUnsupportedTypeError(current);
454
- }
455
-
456
- async parse<T>(current: T): Promise<SerovalNode> {
457
- switch (typeof current) {
458
- case 'boolean':
459
- return current ? TRUE_NODE : FALSE_NODE;
460
- case 'undefined':
461
- return UNDEFINED_NODE;
462
- case 'string':
463
- return createStringNode(current as string);
464
- case 'number':
465
- return createNumberNode(current as number);
466
- case 'bigint':
467
- return createBigIntNode(current as bigint);
468
- case 'object': {
469
- if (current) {
470
- const ref = this.getReference(current);
471
- return ref.type === 0
472
- ? await this.parseObject(ref.value, current as object)
473
- : ref.value;
474
- }
475
- return NULL_NODE;
476
- }
477
- case 'symbol':
478
- return this.parseWellKnownSymbol(current);
479
- case 'function':
480
- return this.parseFunction(current);
481
- default:
482
- throw new SerovalUnsupportedTypeError(current);
483
- }
484
- }
485
-
486
- async parseTop<T>(current: T): Promise<SerovalNode> {
487
- try {
488
- return await this.parse(current);
489
- } catch (error) {
490
- throw error instanceof SerovalParserError
491
- ? error
492
- : new SerovalParserError(error);
493
- }
494
- }
495
- }