seroval 0.15.1 → 1.0.2

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 (114) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/development/index.cjs +1287 -1945
  3. package/dist/cjs/development/index.cjs.map +3 -3
  4. package/dist/cjs/production/index.cjs +3 -3
  5. package/dist/esm/development/index.mjs +1288 -1946
  6. package/dist/esm/development/index.mjs.map +3 -3
  7. package/dist/esm/production/index.mjs +3 -3
  8. package/dist/types/core/Serializer.d.ts +2 -1
  9. package/dist/types/core/Serializer.d.ts.map +1 -1
  10. package/dist/types/core/UnsupportedTypeError.d.ts.map +1 -1
  11. package/dist/types/core/base-primitives.d.ts +6 -2
  12. package/dist/types/core/base-primitives.d.ts.map +1 -1
  13. package/dist/types/core/compat.d.ts +5 -13
  14. package/dist/types/core/compat.d.ts.map +1 -1
  15. package/dist/types/core/constants.d.ts +17 -29
  16. package/dist/types/core/constants.d.ts.map +1 -1
  17. package/dist/types/core/context/deserializer.d.ts +6 -16
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser/async.d.ts +2 -11
  20. package/dist/types/core/context/parser/async.d.ts.map +1 -1
  21. package/dist/types/core/context/parser/stream.d.ts +10 -10
  22. package/dist/types/core/context/parser/stream.d.ts.map +1 -1
  23. package/dist/types/core/context/parser/sync.d.ts +5 -7
  24. package/dist/types/core/context/parser/sync.d.ts.map +1 -1
  25. package/dist/types/core/context/parser.d.ts +26 -5
  26. package/dist/types/core/context/parser.d.ts.map +1 -1
  27. package/dist/types/core/context/serializer.d.ts +21 -30
  28. package/dist/types/core/context/serializer.d.ts.map +1 -1
  29. package/dist/types/core/cross/async.d.ts.map +1 -1
  30. package/dist/types/core/cross/deserializer.d.ts.map +1 -1
  31. package/dist/types/core/cross/index.d.ts.map +1 -1
  32. package/dist/types/core/cross/parser.d.ts.map +1 -1
  33. package/dist/types/core/cross/serializer.d.ts +1 -1
  34. package/dist/types/core/cross/serializer.d.ts.map +1 -1
  35. package/dist/types/core/cross/stream.d.ts.map +1 -1
  36. package/dist/types/core/cross/sync.d.ts.map +1 -1
  37. package/dist/types/core/keys.d.ts.map +1 -1
  38. package/dist/types/core/literals.d.ts.map +1 -1
  39. package/dist/types/core/plugin.d.ts +21 -0
  40. package/dist/types/core/plugin.d.ts.map +1 -1
  41. package/dist/types/core/reference.d.ts.map +1 -1
  42. package/dist/types/core/special-reference.d.ts +4 -9
  43. package/dist/types/core/special-reference.d.ts.map +1 -1
  44. package/dist/types/core/stream.d.ts +18 -0
  45. package/dist/types/core/stream.d.ts.map +1 -0
  46. package/dist/types/core/string.d.ts.map +1 -1
  47. package/dist/types/core/tree/async.d.ts.map +1 -1
  48. package/dist/types/core/tree/deserializer.d.ts.map +1 -1
  49. package/dist/types/core/tree/index.d.ts +1 -1
  50. package/dist/types/core/tree/index.d.ts.map +1 -1
  51. package/dist/types/core/tree/serializer.d.ts +3 -11
  52. package/dist/types/core/tree/serializer.d.ts.map +1 -1
  53. package/dist/types/core/types.d.ts +28 -122
  54. package/dist/types/core/types.d.ts.map +1 -1
  55. package/dist/types/core/utils/deferred.d.ts +0 -7
  56. package/dist/types/core/utils/deferred.d.ts.map +1 -1
  57. package/dist/types/core/utils/error.d.ts +2 -1
  58. package/dist/types/core/utils/error.d.ts.map +1 -1
  59. package/dist/types/core/utils/get-identifier.d.ts.map +1 -1
  60. package/dist/types/core/utils/is-valid-identifier.d.ts.map +1 -1
  61. package/dist/types/core/utils/iterator-to-sequence.d.ts +0 -12
  62. package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -1
  63. package/dist/types/core/utils/typed-array.d.ts +2 -0
  64. package/dist/types/core/utils/typed-array.d.ts.map +1 -1
  65. package/dist/types/index.d.ts +2 -3
  66. package/dist/types/index.d.ts.map +1 -1
  67. package/package.json +7 -7
  68. package/src/core/Serializer.ts +44 -28
  69. package/src/core/UnsupportedTypeError.ts +2 -3
  70. package/src/core/base-primitives.ts +124 -38
  71. package/src/core/compat.ts +5 -14
  72. package/src/core/constants.ts +28 -39
  73. package/src/core/context/deserializer.ts +122 -308
  74. package/src/core/context/parser/async.ts +163 -375
  75. package/src/core/context/parser/stream.ts +110 -381
  76. package/src/core/context/parser/sync.ts +125 -266
  77. package/src/core/context/parser.ts +119 -68
  78. package/src/core/context/serializer.ts +396 -473
  79. package/src/core/cross/async.ts +1 -1
  80. package/src/core/cross/deserializer.ts +1 -4
  81. package/src/core/cross/index.ts +26 -20
  82. package/src/core/cross/parser.ts +1 -1
  83. package/src/core/cross/serializer.ts +30 -20
  84. package/src/core/cross/stream.ts +1 -1
  85. package/src/core/cross/sync.ts +1 -1
  86. package/src/core/keys.ts +3 -1
  87. package/src/core/literals.ts +22 -8
  88. package/src/core/plugin.ts +48 -10
  89. package/src/core/reference.ts +4 -5
  90. package/src/core/special-reference.ts +5 -15
  91. package/src/core/stream.ts +205 -0
  92. package/src/core/string.ts +53 -22
  93. package/src/core/tree/async.ts +1 -1
  94. package/src/core/tree/deserializer.ts +4 -9
  95. package/src/core/tree/index.ts +15 -10
  96. package/src/core/tree/serializer.ts +6 -50
  97. package/src/core/types.ts +39 -179
  98. package/src/core/utils/deferred.ts +0 -27
  99. package/src/core/utils/error.ts +26 -15
  100. package/src/core/utils/get-identifier.ts +5 -4
  101. package/src/core/utils/is-valid-identifier.ts +6 -5
  102. package/src/core/utils/iterator-to-sequence.ts +0 -174
  103. package/src/core/utils/typed-array.ts +35 -11
  104. package/src/index.ts +3 -12
  105. package/dist/types/core/utils/constructors.d.ts +0 -5
  106. package/dist/types/core/utils/constructors.d.ts.map +0 -1
  107. package/dist/types/core/web-api.d.ts +0 -8
  108. package/dist/types/core/web-api.d.ts.map +0 -1
  109. package/dist/types/types.d.ts +0 -21
  110. package/dist/types/types.d.ts.map +0 -1
  111. package/src/core/utils/constructors.ts +0 -49
  112. package/src/core/web-api.ts +0 -138
  113. package/src/example.ts +0 -25
  114. package/src/types.ts +0 -84
@@ -1,20 +1,17 @@
1
- import type { BigIntTypedArrayValue, TypedArrayValue } from '../../../types';
2
- import UnsupportedTypeError from '../../UnsupportedTypeError';
3
1
  import {
4
- createArrayBufferNode,
5
2
  createAsyncIteratorFactoryInstanceNode,
6
- createDateNode,
7
3
  createIteratorFactoryInstanceNode,
8
4
  createPluginNode,
9
- createRegExpNode,
5
+ createStreamConstructorNode,
6
+ createStreamNextNode,
7
+ createStreamReturnNode,
8
+ createStreamThrowNode,
10
9
  createStringNode,
11
10
  } from '../../base-primitives';
12
11
  import type { BaseSyncParserContextOptions } from './sync';
13
12
  import BaseSyncParserContext from './sync';
14
- import { BIGINT_FLAG, Feature } from '../../compat';
15
13
  import { SerovalNodeType } from '../../constants';
16
- import { createRequestOptions, createResponseOptions } from '../../utils/constructors';
17
- import { FALSE_NODE, NULL_NODE, TRUE_NODE } from '../../literals';
14
+ import { FALSE_NODE, TRUE_NODE } from '../../literals';
18
15
  import { serializeString } from '../../string';
19
16
  import type {
20
17
  SerovalNode,
@@ -22,15 +19,14 @@ import type {
22
19
  SerovalObjectRecordNode,
23
20
  SerovalPluginNode,
24
21
  SerovalPromiseConstructorNode,
25
- SerovalReadableStreamConstructorNode,
26
- SerovalRequestNode,
27
- SerovalResponseNode,
28
22
  } from '../../types';
29
- import { createDOMExceptionNode, createURLNode, createURLSearchParamsNode } from '../../web-api';
30
- import { asyncIteratorToReadableStream, iteratorToSequence } from '../../utils/iterator-to-sequence';
31
- import { SpecialReference, UNIVERSAL_SENTINEL } from '../../special-reference';
23
+ import { iteratorToSequence } from '../../utils/iterator-to-sequence';
24
+ import { SpecialReference } from '../../special-reference';
25
+ import type { Stream } from '../../stream';
26
+ import { createStreamFromAsyncIterable } from '../../stream';
32
27
 
33
- export interface BaseStreamParserContextOptions extends BaseSyncParserContextOptions {
28
+ export interface BaseStreamParserContextOptions
29
+ extends BaseSyncParserContextOptions {
34
30
  onParse: (node: SerovalNode, initial: boolean) => void;
35
31
  onError?: (error: unknown) => void;
36
32
  onDone?: () => void;
@@ -56,7 +52,11 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
56
52
  this.onDoneCallback = options.onDone;
57
53
  }
58
54
 
59
- private onParse(node: SerovalNode, initial: boolean): void {
55
+ private initial = true;
56
+
57
+ private buffer: SerovalNode[] = [];
58
+
59
+ private onParseInternal(node: SerovalNode, initial: boolean): void {
60
60
  try {
61
61
  this.onParseCallback(node, initial);
62
62
  } catch (error) {
@@ -64,6 +64,20 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
64
64
  }
65
65
  }
66
66
 
67
+ private flush(): void {
68
+ for (let i = 0, len = this.buffer.length; i < len; i++) {
69
+ this.onParseInternal(this.buffer[i], false);
70
+ }
71
+ }
72
+
73
+ private onParse(node: SerovalNode): void {
74
+ if (this.initial) {
75
+ this.buffer.push(node);
76
+ } else {
77
+ this.onParseInternal(node, false);
78
+ }
79
+ }
80
+
67
81
  private onError(error: unknown): void {
68
82
  if (this.onErrorCallback) {
69
83
  this.onErrorCallback(error);
@@ -78,18 +92,11 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
78
92
  }
79
93
  }
80
94
 
81
- push<T>(value: T): void {
82
- this.onParse(
83
- this.parse(value),
84
- false,
85
- );
86
- }
87
-
88
- pushPendingState(): void {
95
+ private pushPendingState(): void {
89
96
  this.pending++;
90
97
  }
91
98
 
92
- popPendingState(): void {
99
+ private popPendingState(): void {
93
100
  if (--this.pending <= 0) {
94
101
  this.onDone();
95
102
  }
@@ -101,57 +108,46 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
101
108
  const entries = Object.entries(properties);
102
109
  const keyNodes: SerovalObjectRecordKey[] = [];
103
110
  const valueNodes: SerovalNode[] = [];
104
- for (
105
- let i = 0, len = entries.length;
106
- i < len;
107
- i++
108
- ) {
111
+ for (let i = 0, len = entries.length; i < len; i++) {
109
112
  keyNodes.push(serializeString(entries[i][0]));
110
113
  valueNodes.push(this.parse(entries[i][1]));
111
114
  }
112
115
  // Check special properties, symbols in this case
113
- if (this.features & Feature.Symbol) {
114
- let symbol = Symbol.iterator;
115
- if (symbol in properties) {
116
- keyNodes.push(
117
- this.parseWKSymbol(symbol),
118
- );
119
- valueNodes.push(
120
- createIteratorFactoryInstanceNode(
121
- this.parseIteratorFactory(),
122
- this.parse(
123
- iteratorToSequence(properties as unknown as Iterable<unknown>),
124
- ),
116
+ let symbol = Symbol.iterator;
117
+ if (symbol in properties) {
118
+ keyNodes.push(this.parseWellKnownSymbol(symbol));
119
+ valueNodes.push(
120
+ createIteratorFactoryInstanceNode(
121
+ this.parseIteratorFactory(),
122
+ this.parse(
123
+ iteratorToSequence(properties as unknown as Iterable<unknown>),
125
124
  ),
126
- );
127
- }
128
- symbol = Symbol.asyncIterator;
129
- if (symbol in properties) {
130
- keyNodes.push(
131
- this.parseWKSymbol(symbol),
132
- );
133
- valueNodes.push(
134
- createAsyncIteratorFactoryInstanceNode(
135
- this.parseAsyncIteratorFactory(1),
136
- this.parse(
137
- asyncIteratorToReadableStream(
138
- properties as unknown as AsyncIterable<unknown>,
139
- this,
140
- ),
125
+ ),
126
+ );
127
+ }
128
+ symbol = Symbol.asyncIterator;
129
+ if (symbol in properties) {
130
+ keyNodes.push(this.parseWellKnownSymbol(symbol));
131
+ valueNodes.push(
132
+ createAsyncIteratorFactoryInstanceNode(
133
+ this.parseAsyncIteratorFactory(),
134
+ this.parse(
135
+ createStreamFromAsyncIterable(
136
+ properties as unknown as AsyncIterable<unknown>,
141
137
  ),
142
138
  ),
143
- );
144
- }
145
- symbol = Symbol.toStringTag;
146
- if (symbol in properties) {
147
- keyNodes.push(this.parseWKSymbol(symbol));
148
- valueNodes.push(createStringNode(properties[symbol] as string));
149
- }
150
- symbol = Symbol.isConcatSpreadable;
151
- if (symbol in properties) {
152
- keyNodes.push(this.parseWKSymbol(symbol));
153
- valueNodes.push(properties[symbol] ? TRUE_NODE : FALSE_NODE);
154
- }
139
+ ),
140
+ );
141
+ }
142
+ symbol = Symbol.toStringTag;
143
+ if (symbol in properties) {
144
+ keyNodes.push(this.parseWellKnownSymbol(symbol));
145
+ valueNodes.push(createStringNode(properties[symbol] as string));
146
+ }
147
+ symbol = Symbol.isConcatSpreadable;
148
+ if (symbol in properties) {
149
+ keyNodes.push(this.parseWellKnownSymbol(symbol));
150
+ valueNodes.push(properties[symbol] ? TRUE_NODE : FALSE_NODE);
155
151
  }
156
152
  return {
157
153
  k: keyNodes,
@@ -160,159 +156,12 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
160
156
  };
161
157
  }
162
158
 
163
- private pushReadableStreamReader(
164
- id: number,
165
- reader: ReadableStreamDefaultReader,
166
- ): void {
167
- reader.read().then(
168
- (data) => {
169
- if (this.alive) {
170
- if (data.done) {
171
- this.onParse({
172
- t: SerovalNodeType.ReadableStreamClose,
173
- i: id,
174
- s: undefined,
175
- l: undefined,
176
- c: undefined,
177
- m: undefined,
178
- p: undefined,
179
- e: undefined,
180
- a: undefined,
181
- f: this.parseSpecialReference(SpecialReference.ReadableStreamClose),
182
- b: undefined,
183
- o: undefined,
184
- }, false);
185
- this.popPendingState();
186
- } else {
187
- const parsed = this.parseWithError(data.value);
188
- if (parsed) {
189
- this.onParse({
190
- t: SerovalNodeType.ReadableStreamEnqueue,
191
- i: id,
192
- s: undefined,
193
- l: undefined,
194
- c: undefined,
195
- m: undefined,
196
- p: undefined,
197
- e: undefined,
198
- a: [
199
- this.parseSpecialReference(SpecialReference.ReadableStreamEnqueue),
200
- parsed,
201
- ],
202
- f: undefined,
203
- b: undefined,
204
- o: undefined,
205
- }, false);
206
- this.pushReadableStreamReader(id, reader);
207
- }
208
- }
209
- }
210
- },
211
- (value) => {
212
- if (this.alive) {
213
- const parsed = this.parseWithError(value);
214
- if (parsed) {
215
- this.onParse({
216
- t: SerovalNodeType.ReadableStreamError,
217
- i: id,
218
- s: undefined,
219
- l: undefined,
220
- c: undefined,
221
- m: undefined,
222
- p: undefined,
223
- e: undefined,
224
- a: [
225
- this.parseSpecialReference(SpecialReference.ReadableStreamError),
226
- parsed,
227
- ],
228
- f: undefined,
229
- b: undefined,
230
- o: undefined,
231
- }, false);
232
- }
233
- this.popPendingState();
234
- }
235
- },
236
- );
237
- }
238
-
239
- private parseReadableStream(
240
- id: number,
241
- current: ReadableStream<unknown>,
242
- ): SerovalReadableStreamConstructorNode {
243
- const reader = current.getReader();
244
- this.pushPendingState();
245
- this.pushReadableStreamReader(id, reader);
246
-
247
- return {
248
- t: SerovalNodeType.ReadableStreamConstructor,
249
- i: id,
250
- s: undefined,
251
- l: undefined,
252
- c: undefined,
253
- m: undefined,
254
- p: undefined,
255
- e: undefined,
256
- a: undefined,
257
- f: this.parseSpecialReference(SpecialReference.ReadableStreamConstructor),
258
- b: undefined,
259
- o: undefined,
260
- };
261
- }
262
-
263
- private parseRequest(
264
- id: number,
265
- current: Request,
266
- ): SerovalRequestNode {
267
- return {
268
- t: SerovalNodeType.Request,
269
- i: id,
270
- s: serializeString(current.url),
271
- l: undefined,
272
- c: undefined,
273
- m: undefined,
274
- p: undefined,
275
- e: undefined,
276
- f: this.parse(
277
- createRequestOptions(current, current.clone().body),
278
- ),
279
- a: undefined,
280
- b: undefined,
281
- o: undefined,
282
- };
283
- }
284
-
285
- private parseResponse(
286
- id: number,
287
- current: Response,
288
- ): SerovalResponseNode {
289
- return {
290
- t: SerovalNodeType.Response,
291
- i: id,
292
- s: undefined,
293
- l: undefined,
294
- c: undefined,
295
- m: undefined,
296
- p: undefined,
297
- e: undefined,
298
- f: undefined,
299
- a: [
300
- current.body
301
- ? this.parse(current.clone().body)
302
- : NULL_NODE,
303
- this.parse(createResponseOptions(current)),
304
- ],
305
- b: undefined,
306
- o: undefined,
307
- };
308
- }
309
-
310
- private parsePromise(
159
+ protected parsePromise(
311
160
  id: number,
312
161
  current: Promise<unknown>,
313
162
  ): SerovalPromiseConstructorNode {
314
163
  current.then(
315
- (data) => {
164
+ data => {
316
165
  const parsed = this.parseWithError(data);
317
166
  if (parsed) {
318
167
  this.onParse({
@@ -331,11 +180,11 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
331
180
  f: undefined,
332
181
  b: undefined,
333
182
  o: undefined,
334
- }, false);
183
+ });
335
184
  }
336
185
  this.popPendingState();
337
186
  },
338
- (data) => {
187
+ data => {
339
188
  if (this.alive) {
340
189
  const parsed = this.parseWithError(data);
341
190
  if (parsed) {
@@ -355,27 +204,14 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
355
204
  f: undefined,
356
205
  b: undefined,
357
206
  o: undefined,
358
- }, false);
207
+ });
359
208
  }
360
209
  }
361
210
  this.popPendingState();
362
211
  },
363
212
  );
364
213
  this.pushPendingState();
365
- return {
366
- t: SerovalNodeType.PromiseConstructor,
367
- i: id,
368
- s: undefined,
369
- l: undefined,
370
- c: undefined,
371
- m: undefined,
372
- p: undefined,
373
- e: undefined,
374
- a: undefined,
375
- f: this.parseSpecialReference(SpecialReference.PromiseConstructor),
376
- b: undefined,
377
- o: undefined,
378
- };
214
+ return this.createPromiseConstructorNode(id);
379
215
  }
380
216
 
381
217
  protected parsePlugin(
@@ -400,151 +236,42 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
400
236
  return undefined;
401
237
  }
402
238
 
403
- protected parseObject(
404
- id: number,
405
- current: object,
406
- ): SerovalNode {
407
- if (Array.isArray(current)) {
408
- return this.parseArray(id, current);
409
- }
410
- const currentClass = current.constructor;
411
- switch (currentClass) {
412
- case Object:
413
- return this.parsePlainObject(
414
- id,
415
- current as Record<string, unknown>,
416
- false,
417
- );
418
- case undefined:
419
- return this.parsePlainObject(
420
- id,
421
- current as Record<string, unknown>,
422
- true,
423
- );
424
- case Date:
425
- return createDateNode(id, current as unknown as Date);
426
- case RegExp:
427
- return createRegExpNode(id, current as unknown as RegExp);
428
- case Error:
429
- case EvalError:
430
- case RangeError:
431
- case ReferenceError:
432
- case SyntaxError:
433
- case TypeError:
434
- case URIError:
435
- return this.parseError(id, current as unknown as Error);
436
- case Number:
437
- case Boolean:
438
- case String:
439
- case BigInt:
440
- return this.parseBoxed(id, current);
441
- default:
442
- break;
443
- }
444
- const currentFeatures = this.features;
445
- // Promises
446
- if (
447
- (currentFeatures & Feature.Promise)
448
- && (currentClass === Promise || current instanceof Promise)
449
- ) {
450
- return this.parsePromise(id, current as unknown as Promise<unknown>);
451
- }
452
- // Typed Arrays
453
- if (currentFeatures & Feature.TypedArray) {
454
- switch (currentClass) {
455
- case ArrayBuffer:
456
- return createArrayBufferNode(id, current as unknown as ArrayBuffer);
457
- case Int8Array:
458
- case Int16Array:
459
- case Int32Array:
460
- case Uint8Array:
461
- case Uint16Array:
462
- case Uint32Array:
463
- case Uint8ClampedArray:
464
- case Float32Array:
465
- case Float64Array:
466
- return this.parseTypedArray(id, current as unknown as TypedArrayValue);
467
- case DataView:
468
- return this.parseDataView(id, current as unknown as DataView);
469
- default:
470
- break;
471
- }
472
- }
473
- // BigInt Typed Arrays
474
- if ((currentFeatures & BIGINT_FLAG) === BIGINT_FLAG) {
475
- switch (currentClass) {
476
- case BigInt64Array:
477
- case BigUint64Array:
478
- return this.parseBigIntTypedArray(id, current as unknown as BigIntTypedArrayValue);
479
- default:
480
- break;
481
- }
482
- }
483
- // ES Collection
484
- if (currentFeatures & Feature.Map && currentClass === Map) {
485
- return this.parseMap(
486
- id,
487
- current as unknown as Map<unknown, unknown>,
488
- );
489
- }
490
- if (currentFeatures & Feature.Set && currentClass === Set) {
491
- return this.parseSet(
492
- id,
493
- current as unknown as Set<unknown>,
494
- );
495
- }
496
- // Web APIs
497
- if (currentFeatures & Feature.WebAPI) {
498
- switch (currentClass) {
499
- case (typeof URL !== 'undefined' ? URL : UNIVERSAL_SENTINEL):
500
- return createURLNode(id, current as unknown as URL);
501
- case (typeof URLSearchParams !== 'undefined' ? URLSearchParams : UNIVERSAL_SENTINEL):
502
- return createURLSearchParamsNode(id, current as unknown as URLSearchParams);
503
- case (typeof Headers !== 'undefined' ? Headers : UNIVERSAL_SENTINEL):
504
- return this.parseHeaders(id, current as unknown as Headers);
505
- case (typeof FormData !== 'undefined' ? FormData : UNIVERSAL_SENTINEL):
506
- return this.parseFormData(id, current as unknown as FormData);
507
- case (typeof ReadableStream !== 'undefined' ? ReadableStream : UNIVERSAL_SENTINEL):
508
- return this.parseReadableStream(id, current as unknown as ReadableStream);
509
- case (typeof Request !== 'undefined' ? Request : UNIVERSAL_SENTINEL):
510
- return this.parseRequest(id, current as unknown as Request);
511
- case (typeof Response !== 'undefined' ? Response : UNIVERSAL_SENTINEL):
512
- return this.parseResponse(id, current as unknown as Response);
513
- case (typeof Event !== 'undefined' ? Event : UNIVERSAL_SENTINEL):
514
- return this.parseEvent(id, current as unknown as Event);
515
- case (typeof CustomEvent !== 'undefined' ? CustomEvent : UNIVERSAL_SENTINEL):
516
- return this.parseCustomEvent(id, current as unknown as CustomEvent);
517
- case (typeof DOMException !== 'undefined' ? DOMException : UNIVERSAL_SENTINEL):
518
- return createDOMExceptionNode(id, current as unknown as DOMException);
519
- default:
520
- break;
521
- }
522
- }
523
- const parsed = this.parsePlugin(id, current);
524
- if (parsed) {
525
- return parsed;
526
- }
527
- if (
528
- (currentFeatures & Feature.AggregateError)
529
- && typeof AggregateError !== 'undefined'
530
- && (currentClass === AggregateError || current instanceof AggregateError)
531
- ) {
532
- return this.parseAggregateError(id, current as unknown as AggregateError);
533
- }
534
- // Slow path. We only need to handle Errors and Iterators
535
- // since they have very broad implementations.
536
- if (current instanceof Error) {
537
- return this.parseError(id, current);
538
- }
539
- // Generator functions don't have a global constructor
540
- // despite existing
541
- if (
542
- currentFeatures & Feature.Symbol
543
- && (Symbol.iterator in current || Symbol.asyncIterator in current)
544
- ) {
545
- return this.parsePlainObject(id, current, !!currentClass);
546
- }
547
- throw new UnsupportedTypeError(current);
239
+ protected parseStream(id: number, current: Stream<unknown>): SerovalNode {
240
+ const result = createStreamConstructorNode(
241
+ id,
242
+ this.parseSpecialReference(SpecialReference.StreamConstructor),
243
+ [],
244
+ );
245
+ this.pushPendingState();
246
+ current.on({
247
+ next: value => {
248
+ if (this.alive) {
249
+ const parsed = this.parseWithError(value);
250
+ if (parsed) {
251
+ this.onParse(createStreamNextNode(id, parsed));
252
+ }
253
+ }
254
+ },
255
+ throw: value => {
256
+ if (this.alive) {
257
+ const parsed = this.parseWithError(value);
258
+ if (parsed) {
259
+ this.onParse(createStreamThrowNode(id, parsed));
260
+ }
261
+ }
262
+ this.popPendingState();
263
+ },
264
+ return: value => {
265
+ if (this.alive) {
266
+ const parsed = this.parseWithError(value);
267
+ if (parsed) {
268
+ this.onParse(createStreamReturnNode(id, parsed));
269
+ }
270
+ }
271
+ this.popPendingState();
272
+ },
273
+ });
274
+ return result;
548
275
  }
549
276
 
550
277
  private parseWithError<T>(current: T): SerovalNode | undefined {
@@ -562,7 +289,9 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont
562
289
  start<T>(current: T): void {
563
290
  const parsed = this.parseWithError(current);
564
291
  if (parsed) {
565
- this.onParse(parsed, true);
292
+ this.onParseInternal(parsed, true);
293
+ this.initial = false;
294
+ this.flush();
566
295
 
567
296
  // Check if there's any pending pushes
568
297
  if (this.pending <= 0) {