seroval 0.15.0 → 1.0.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 (114) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/development/index.cjs +1278 -1929
  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 +1278 -1929
  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 +19 -4
  26. package/dist/types/core/context/parser.d.ts.map +1 -1
  27. package/dist/types/core/context/serializer.d.ts +11 -25
  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 +3 -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 +109 -367
  76. package/src/core/context/parser/sync.ts +139 -266
  77. package/src/core/context/parser.ts +93 -68
  78. package/src/core/context/serializer.ts +370 -439
  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 +28 -31
  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 +5 -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
@@ -30,33 +30,21 @@ import type {
30
30
  SerovalErrorNode,
31
31
  SerovalPromiseNode,
32
32
  SerovalWKSymbolNode,
33
- SerovalURLNode,
34
- SerovalURLSearchParamsNode,
35
- SerovalBlobNode,
36
- SerovalFileNode,
37
- SerovalHeadersNode,
38
- SerovalFormDataNode,
39
33
  SerovalBoxedNode,
40
- SerovalRequestNode,
41
- SerovalResponseNode,
42
- SerovalEventNode,
43
- SerovalCustomEventNode,
44
- SerovalDOMExceptionNode,
45
34
  SerovalPluginNode,
46
35
  SerovalPromiseConstructorNode,
47
36
  SerovalPromiseResolveNode,
48
37
  SerovalPromiseRejectNode,
49
- SerovalReadableStreamConstructorNode,
50
- SerovalReadableStreamEnqueueNode,
51
- SerovalReadableStreamErrorNode,
52
- SerovalReadableStreamCloseNode,
53
38
  SerovalIteratorFactoryInstanceNode,
54
39
  SerovalIteratorFactoryNode,
55
40
  SerovalAsyncIteratorFactoryInstanceNode,
56
41
  SerovalAsyncIteratorFactoryNode,
57
- SerovalReadableStreamNode,
58
42
  SerovalSpecialReferenceNode,
59
43
  SerovalNodeWithID,
44
+ SerovalStreamConstructorNode,
45
+ SerovalStreamNextNode,
46
+ SerovalStreamThrowNode,
47
+ SerovalStreamReturnNode,
60
48
  } from '../types';
61
49
  import { isValidIdentifier } from '../utils/is-valid-identifier';
62
50
 
@@ -118,7 +106,11 @@ function getAssignmentExpression(assignment: Assignment): string {
118
106
  function mergeAssignments(assignments: Assignment[]): Assignment[] {
119
107
  const newAssignments: Assignment[] = [];
120
108
  let current = assignments[0];
121
- for (let i = 1, len = assignments.length, item: Assignment, prev = current; i < len; i++) {
109
+ for (
110
+ let i = 1, len = assignments.length, item: Assignment, prev = current;
111
+ i < len;
112
+ i++
113
+ ) {
122
114
  item = assignments[i];
123
115
  if (item.t === 'index' && item.v === prev.v) {
124
116
  // Merge if the right-hand value is the same
@@ -185,26 +177,28 @@ const MAP_CONSTRUCTOR = 'new Map';
185
177
  const PROMISE_RESOLVE = 'Promise.resolve';
186
178
  const PROMISE_REJECT = 'Promise.reject';
187
179
 
188
- const OBJECT_FLAG_CONSTRUCTOR: Record<SerovalObjectFlags, string | undefined> = {
189
- [SerovalObjectFlags.Frozen]: 'Object.freeze',
190
- [SerovalObjectFlags.Sealed]: 'Object.seal',
191
- [SerovalObjectFlags.NonExtensible]: 'Object.preventExtensions',
192
- [SerovalObjectFlags.None]: undefined,
193
- };
180
+ const OBJECT_FLAG_CONSTRUCTOR: Record<SerovalObjectFlags, string | undefined> =
181
+ {
182
+ [SerovalObjectFlags.Frozen]: 'Object.freeze',
183
+ [SerovalObjectFlags.Sealed]: 'Object.seal',
184
+ [SerovalObjectFlags.NonExtensible]: 'Object.preventExtensions',
185
+ [SerovalObjectFlags.None]: undefined,
186
+ };
194
187
 
195
188
  type SerovalNodeWithProperties =
196
189
  | SerovalObjectNode
197
190
  | SerovalNullConstructorNode
198
191
  | SerovalAggregateErrorNode
199
- | SerovalErrorNode
200
- | SerovalHeadersNode;
192
+ | SerovalErrorNode;
201
193
 
202
194
  export interface BaseSerializerContextOptions extends PluginAccessOptions {
203
195
  features: number;
204
196
  markedRefs: number[] | Set<number>;
205
197
  }
206
198
 
207
- export default abstract class BaseSerializerContext implements PluginAccessOptions {
199
+ export default abstract class BaseSerializerContext
200
+ implements PluginAccessOptions
201
+ {
208
202
  /**
209
203
  * @private
210
204
  */
@@ -244,27 +238,23 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
244
238
 
245
239
  abstract readonly mode: SerovalMode;
246
240
 
247
- protected createFunction(
248
- parameters: string[],
249
- body: string,
250
- ): string {
241
+ createFunction(parameters: string[], body: string): string {
251
242
  if (this.features & Feature.ArrowFunction) {
252
- const joined = parameters.length === 1
253
- ? parameters[0]
254
- : '(' + parameters.join(',') + ')';
243
+ const joined =
244
+ parameters.length === 1
245
+ ? parameters[0]
246
+ : '(' + parameters.join(',') + ')';
255
247
  return joined + '=>' + body;
256
248
  }
257
249
  return 'function(' + parameters.join(',') + '){return ' + body + '}';
258
250
  }
259
251
 
260
- protected createEffectfulFunction(
261
- parameters: string[],
262
- body: string,
263
- ): string {
252
+ createEffectfulFunction(parameters: string[], body: string): string {
264
253
  if (this.features & Feature.ArrowFunction) {
265
- const joined = parameters.length === 1
266
- ? parameters[0]
267
- : '(' + parameters.join(',') + ')';
254
+ const joined =
255
+ parameters.length === 1
256
+ ? parameters[0]
257
+ : '(' + parameters.join(',') + ')';
268
258
  return joined + '=>{' + body + '}';
269
259
  }
270
260
  return 'function(' + parameters.join(',') + '){' + body + '}';
@@ -289,7 +279,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
289
279
  * that is used to refer to the object instance in the
290
280
  * generated script.
291
281
  */
292
- abstract getRefParam(id: number | string): string;
282
+ abstract getRefParam(id: number): string;
293
283
 
294
284
  protected pushObjectFlag(flag: SerovalObjectFlags, id: number): void {
295
285
  if (flag !== SerovalObjectFlags.None) {
@@ -327,10 +317,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
327
317
  * This is different from the assignments array as this one
328
318
  * signifies creation rather than mutation
329
319
  */
330
- protected createAssignment(
331
- source: string,
332
- value: string,
333
- ): void {
320
+ protected createAssignment(source: string, value: string): void {
334
321
  this.assignments.push({
335
322
  t: 'index',
336
323
  s: source,
@@ -339,10 +326,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
339
326
  });
340
327
  }
341
328
 
342
- protected createAddAssignment(
343
- ref: number,
344
- value: string,
345
- ): void {
329
+ protected createAddAssignment(ref: number, value: string): void {
346
330
  this.assignments.push({
347
331
  t: 'add',
348
332
  s: this.getRefParam(ref),
@@ -351,11 +335,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
351
335
  });
352
336
  }
353
337
 
354
- protected createSetAssignment(
355
- ref: number,
356
- key: string,
357
- value: string,
358
- ): void {
338
+ protected createSetAssignment(ref: number, key: string, value: string): void {
359
339
  this.assignments.push({
360
340
  t: 'set',
361
341
  s: this.getRefParam(ref),
@@ -364,10 +344,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
364
344
  });
365
345
  }
366
346
 
367
- protected createDeleteAssignment(
368
- ref: number,
369
- key: string,
370
- ): void {
347
+ protected createDeleteAssignment(ref: number, key: string): void {
371
348
  this.assignments.push({
372
349
  t: 'delete',
373
350
  s: this.getRefParam(ref),
@@ -384,11 +361,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
384
361
  this.createAssignment(this.getRefParam(ref) + '[' + index + ']', value);
385
362
  }
386
363
 
387
- protected createObjectAssign(
388
- ref: number,
389
- key: string,
390
- value: string,
391
- ): void {
364
+ protected createObjectAssign(ref: number, key: string, value: string): void {
392
365
  this.createAssignment(this.getRefParam(ref) + '.' + key, value);
393
366
  }
394
367
 
@@ -396,10 +369,10 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
396
369
  * Checks if the value is in the stack. Stack here is a reference
397
370
  * structure to know if a object is to be accessed in a TDZ.
398
371
  */
399
- isIndexedValueInStack(
400
- node: SerovalNode,
401
- ): boolean {
402
- return node.t === SerovalNodeType.IndexedValue && this.stack.includes(node.i);
372
+ isIndexedValueInStack(node: SerovalNode): boolean {
373
+ return (
374
+ node.t === SerovalNodeType.IndexedValue && this.stack.includes(node.i)
375
+ );
403
376
  }
404
377
 
405
378
  /**
@@ -408,15 +381,13 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
408
381
  * return the reference parameter directly or assign a value to
409
382
  * it.
410
383
  */
411
- protected abstract assignIndexedValue(
412
- id: number,
413
- value: string,
414
- ): string;
384
+ protected abstract assignIndexedValue(id: number, value: string): string;
415
385
 
416
- protected serializeReference(
417
- node: SerovalReferenceNode,
418
- ): string {
419
- return this.assignIndexedValue(node.i, REFERENCES_KEY + '.get("' + node.s + '")');
386
+ protected serializeReference(node: SerovalReferenceNode): string {
387
+ return this.assignIndexedValue(
388
+ node.i,
389
+ REFERENCES_KEY + '.get("' + node.s + '")',
390
+ );
420
391
  }
421
392
 
422
393
  protected serializeArrayItem(
@@ -429,7 +400,11 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
429
400
  // Check if item is a parent
430
401
  if (this.isIndexedValueInStack(item)) {
431
402
  this.markRef(id);
432
- this.createArrayAssign(id, index, this.getRefParam((item as SerovalIndexedValueNode).i));
403
+ this.createArrayAssign(
404
+ id,
405
+ index,
406
+ this.getRefParam((item as SerovalIndexedValueNode).i),
407
+ );
433
408
  return '';
434
409
  }
435
410
  return this.serialize(item);
@@ -437,9 +412,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
437
412
  return '';
438
413
  }
439
414
 
440
- protected serializeArray(
441
- node: SerovalArrayNode,
442
- ): string {
415
+ protected serializeArray(node: SerovalArrayNode): string {
443
416
  const id = node.i;
444
417
  if (node.l) {
445
418
  this.stack.push(id);
@@ -474,15 +447,18 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
474
447
  if (this.isIndexedValueInStack(val)) {
475
448
  const refParam = this.getRefParam((val as SerovalIndexedValueNode).i);
476
449
  this.markRef(source.i);
477
- // eslint-disable-next-line no-self-compare
478
450
  if (isIdentifier && check !== check) {
479
451
  this.createObjectAssign(source.i, key, refParam);
480
452
  } else {
481
- this.createArrayAssign(source.i, isIdentifier ? key : ('"' + key + '"'), refParam);
453
+ this.createArrayAssign(
454
+ source.i,
455
+ isIdentifier ? key : '"' + key + '"',
456
+ refParam,
457
+ );
482
458
  }
483
459
  return '';
484
460
  }
485
- return (isIdentifier ? key : ('"' + key + '"')) + ':' + this.serialize(val);
461
+ return (isIdentifier ? key : '"' + key + '"') + ':' + this.serialize(val);
486
462
  }
487
463
  return '[' + this.serialize(key) + ']:' + this.serialize(val);
488
464
  }
@@ -507,11 +483,12 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
507
483
  return '{}';
508
484
  }
509
485
 
510
- protected serializeObject(
511
- node: SerovalObjectNode,
512
- ): string {
486
+ protected serializeObject(node: SerovalObjectNode): string {
513
487
  this.pushObjectFlag(node.o, node.i);
514
- return this.assignIndexedValue(node.i, this.serializeProperties(node, node.p));
488
+ return this.assignIndexedValue(
489
+ node.i,
490
+ this.serializeProperties(node, node.p),
491
+ );
515
492
  }
516
493
 
517
494
  protected serializeWithObjectAssign(
@@ -526,6 +503,43 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
526
503
  return serialized;
527
504
  }
528
505
 
506
+ private serializeStringKeyAssignment(
507
+ source: SerovalNodeWithProperties,
508
+ mainAssignments: Assignment[],
509
+ key: string,
510
+ value: SerovalNode,
511
+ ): void {
512
+ const serialized = this.serialize(value);
513
+ const check = Number(key);
514
+ // Test if key is a valid number or JS identifier
515
+ // so that we don't have to serialize the key and wrap with brackets
516
+ const isIdentifier = check >= 0 || isValidIdentifier(key);
517
+ if (this.isIndexedValueInStack(value)) {
518
+ if (isIdentifier && check !== check) {
519
+ this.createObjectAssign(source.i, key, serialized);
520
+ } else {
521
+ this.createArrayAssign(
522
+ source.i,
523
+ isIdentifier ? key : '"' + key + '"',
524
+ serialized,
525
+ );
526
+ }
527
+ } else {
528
+ const parentAssignment = this.assignments;
529
+ this.assignments = mainAssignments;
530
+ if (isIdentifier) {
531
+ this.createObjectAssign(source.i, key, serialized);
532
+ } else {
533
+ this.createArrayAssign(
534
+ source.i,
535
+ isIdentifier ? key : '"' + key + '"',
536
+ serialized,
537
+ );
538
+ }
539
+ this.assignments = parentAssignment;
540
+ }
541
+ }
542
+
529
543
  protected serializeAssignment(
530
544
  source: SerovalNodeWithProperties,
531
545
  mainAssignments: Assignment[],
@@ -533,28 +547,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
533
547
  value: SerovalNode,
534
548
  ): void {
535
549
  if (typeof key === 'string') {
536
- const serialized = this.serialize(value);
537
- const check = Number(key);
538
- // Test if key is a valid number or JS identifier
539
- // so that we don't have to serialize the key and wrap with brackets
540
- const isIdentifier = check >= 0 || isValidIdentifier(key);
541
- if (this.isIndexedValueInStack(value)) {
542
- // eslint-disable-next-line no-self-compare
543
- if (isIdentifier && check !== check) {
544
- this.createObjectAssign(source.i, key, serialized);
545
- } else {
546
- this.createArrayAssign(source.i, isIdentifier ? key : ('"' + key + '"'), serialized);
547
- }
548
- } else {
549
- const parentAssignment = this.assignments;
550
- this.assignments = mainAssignments;
551
- if (isIdentifier) {
552
- this.createObjectAssign(source.i, key, serialized);
553
- } else {
554
- this.createArrayAssign(source.i, isIdentifier ? key : ('"' + key + '"'), serialized);
555
- }
556
- this.assignments = parentAssignment;
557
- }
550
+ this.serializeStringKeyAssignment(source, mainAssignments, key, value);
558
551
  } else {
559
552
  const parent = this.stack;
560
553
  this.stack = [];
@@ -562,11 +555,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
562
555
  this.stack = parent;
563
556
  const parentAssignment = this.assignments;
564
557
  this.assignments = mainAssignments;
565
- this.createArrayAssign(
566
- source.i,
567
- this.serialize(key),
568
- serialized,
569
- );
558
+ this.createArrayAssign(source.i, this.serialize(key), serialized);
570
559
  this.assignments = parentAssignment;
571
560
  }
572
561
  }
@@ -601,47 +590,46 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
601
590
  this.markRef(node.i);
602
591
  const assignments = this.serializeAssignments(node, node.p);
603
592
  if (assignments) {
604
- return '(' + this.assignIndexedValue(node.i, init) + ',' + assignments + this.getRefParam(node.i) + ')';
593
+ return (
594
+ '(' +
595
+ this.assignIndexedValue(node.i, init) +
596
+ ',' +
597
+ assignments +
598
+ this.getRefParam(node.i) +
599
+ ')'
600
+ );
605
601
  }
606
602
  }
607
603
  }
608
604
  return this.assignIndexedValue(node.i, init);
609
605
  }
610
606
 
611
- protected serializeNullConstructor(
612
- node: SerovalNullConstructorNode,
613
- ): string {
607
+ protected serializeNullConstructor(node: SerovalNullConstructorNode): string {
614
608
  this.pushObjectFlag(node.o, node.i);
615
609
  return this.serializeDictionary(node, NULL_CONSTRUCTOR);
616
610
  }
617
611
 
618
- protected serializeDate(
619
- node: SerovalDateNode,
620
- ): string {
612
+ protected serializeDate(node: SerovalDateNode): string {
621
613
  return this.assignIndexedValue(node.i, 'new Date("' + node.s + '")');
622
614
  }
623
615
 
624
- protected serializeRegExp(
625
- node: SerovalRegExpNode,
626
- ): string {
616
+ protected serializeRegExp(node: SerovalRegExpNode): string {
627
617
  return this.assignIndexedValue(node.i, '/' + node.c + '/' + node.m);
628
618
  }
629
619
 
630
- protected serializeSetItem(
631
- id: number,
632
- item: SerovalNode,
633
- ): string {
620
+ protected serializeSetItem(id: number, item: SerovalNode): string {
634
621
  if (this.isIndexedValueInStack(item)) {
635
622
  this.markRef(id);
636
- this.createAddAssignment(id, this.getRefParam((item as SerovalIndexedValueNode).i));
623
+ this.createAddAssignment(
624
+ id,
625
+ this.getRefParam((item as SerovalIndexedValueNode).i),
626
+ );
637
627
  return '';
638
628
  }
639
629
  return this.serialize(item);
640
630
  }
641
631
 
642
- protected serializeSet(
643
- node: SerovalSetNode,
644
- ): string {
632
+ protected serializeSet(node: SerovalSetNode): string {
645
633
  let serialized = SET_CONSTRUCTOR;
646
634
  const size = node.l;
647
635
  const id = node.i;
@@ -685,12 +673,17 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
685
673
  // value is no longer part of the expression
686
674
  // tree and has been moved to the deferred
687
675
  // assignment
688
- if (val.t !== SerovalNodeType.IndexedValue && val.i != null && this.isMarked(val.i)) {
676
+ if (
677
+ val.t !== SerovalNodeType.IndexedValue &&
678
+ val.i != null &&
679
+ this.isMarked(val.i)
680
+ ) {
689
681
  // We use a trick here using sequence (or comma) expressions
690
682
  // basically we serialize the intended object in place WITHOUT
691
683
  // actually returning it, this is by returning a placeholder
692
684
  // value that we will remove sometime after.
693
- const serialized = '(' + this.serialize(val) + ',[' + sentinel + ',' + sentinel + '])';
685
+ const serialized =
686
+ '(' + this.serialize(val) + ',[' + sentinel + ',' + sentinel + '])';
694
687
  this.createSetAssignment(id, keyRef, this.getRefParam(val.i));
695
688
  this.createDeleteAssignment(id, sentinel);
696
689
  return serialized;
@@ -705,8 +698,13 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
705
698
  // Create ref for the Map instance
706
699
  const valueRef = this.getRefParam((val as SerovalIndexedValueNode).i);
707
700
  this.markRef(id);
708
- if (key.t !== SerovalNodeType.IndexedValue && key.i != null && this.isMarked(key.i)) {
709
- const serialized = '(' + this.serialize(key) + ',[' + sentinel + ',' + sentinel + '])';
701
+ if (
702
+ key.t !== SerovalNodeType.IndexedValue &&
703
+ key.i != null &&
704
+ this.isMarked(key.i)
705
+ ) {
706
+ const serialized =
707
+ '(' + this.serialize(key) + ',[' + sentinel + ',' + sentinel + '])';
710
708
  this.createSetAssignment(id, this.getRefParam(key.i), valueRef);
711
709
  this.createDeleteAssignment(id, sentinel);
712
710
  return serialized;
@@ -722,21 +720,19 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
722
720
  return '[' + this.serialize(key) + ',' + this.serialize(val) + ']';
723
721
  }
724
722
 
725
- protected serializeMap(
726
- node: SerovalMapNode,
727
- ): string {
723
+ protected serializeMap(node: SerovalMapNode): string {
728
724
  let serialized = MAP_CONSTRUCTOR;
729
725
  const size = node.e.s;
730
726
  const id = node.i;
731
727
  const sentinel = node.f;
732
- const sentinelID = this.getRefParam(sentinel.i);
728
+ const sentinelId = this.getRefParam(sentinel.i);
733
729
  if (size) {
734
730
  const keys = node.e.k;
735
731
  const vals = node.e.v;
736
732
  this.stack.push(id);
737
- let result = this.serializeMapEntry(id, keys[0], vals[0], sentinelID);
733
+ let result = this.serializeMapEntry(id, keys[0], vals[0], sentinelId);
738
734
  for (let i = 1, item = result; i < size; i++) {
739
- item = this.serializeMapEntry(id, keys[i], vals[i], sentinelID);
735
+ item = this.serializeMapEntry(id, keys[i], vals[i], sentinelId);
740
736
  result += (item && result && ',') + item;
741
737
  }
742
738
  this.stack.pop();
@@ -754,9 +750,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
754
750
  return this.assignIndexedValue(id, serialized);
755
751
  }
756
752
 
757
- protected serializeArrayBuffer(
758
- node: SerovalArrayBufferNode,
759
- ): string {
753
+ protected serializeArrayBuffer(node: SerovalArrayBufferNode): string {
760
754
  let result = 'new Uint8Array(';
761
755
  const buffer = node.s;
762
756
  const len = buffer.length;
@@ -775,23 +769,33 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
775
769
  ): string {
776
770
  return this.assignIndexedValue(
777
771
  node.i,
778
- 'new ' + node.c + '(' + this.serialize(node.f) + ',' + node.b + ',' + node.l + ')',
772
+ 'new ' +
773
+ node.c +
774
+ '(' +
775
+ this.serialize(node.f) +
776
+ ',' +
777
+ node.b +
778
+ ',' +
779
+ node.l +
780
+ ')',
779
781
  );
780
782
  }
781
783
 
782
- protected serializeDataView(
783
- node: SerovalDataViewNode,
784
- ): string {
784
+ protected serializeDataView(node: SerovalDataViewNode): string {
785
785
  return this.assignIndexedValue(
786
786
  node.i,
787
- 'new DataView(' + this.serialize(node.f) + ',' + node.b + ',' + node.l + ')',
787
+ 'new DataView(' +
788
+ this.serialize(node.f) +
789
+ ',' +
790
+ node.b +
791
+ ',' +
792
+ node.l +
793
+ ')',
788
794
  );
789
795
  }
790
796
 
791
- protected serializeAggregateError(
792
- node: SerovalAggregateErrorNode,
793
- ): string {
794
- // Serialize the required arguments
797
+ protected serializeAggregateError(node: SerovalAggregateErrorNode): string {
798
+ // Serialize the required arguments
795
799
  const id = node.i;
796
800
  this.stack.push(id);
797
801
  const serialized = 'new AggregateError([],"' + node.m + '")';
@@ -802,174 +806,69 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
802
806
  return this.serializeDictionary(node, serialized);
803
807
  }
804
808
 
805
- protected serializeError(
806
- node: SerovalErrorNode,
807
- ): string {
808
- return this.serializeDictionary(node, 'new ' + ERROR_CONSTRUCTOR_STRING[node.s] + '("' + node.m + '")');
809
+ protected serializeError(node: SerovalErrorNode): string {
810
+ return this.serializeDictionary(
811
+ node,
812
+ 'new ' + ERROR_CONSTRUCTOR_STRING[node.s] + '("' + node.m + '")',
813
+ );
809
814
  }
810
815
 
811
- protected serializePromise(
812
- node: SerovalPromiseNode,
813
- ): string {
816
+ protected serializePromise(node: SerovalPromiseNode): string {
814
817
  let serialized: string;
815
818
  // Check if resolved value is a parent expression
816
819
  const fulfilled = node.f;
817
820
  const id = node.i;
818
- const constructor = node.s ? PROMISE_RESOLVE : PROMISE_REJECT;
821
+ const promiseConstructor = node.s ? PROMISE_RESOLVE : PROMISE_REJECT;
819
822
  if (this.isIndexedValueInStack(fulfilled)) {
820
823
  // A Promise trick, reference the value
821
824
  // inside the `then` expression so that
822
825
  // the Promise evaluates after the parent
823
826
  // has initialized
824
827
  const ref = this.getRefParam((fulfilled as SerovalIndexedValueNode).i);
825
- serialized = constructor + (
826
- node.s
828
+ serialized =
829
+ promiseConstructor +
830
+ (node.s
827
831
  ? '().then(' + this.createFunction([], ref) + ')'
828
- : '().catch(' + this.createEffectfulFunction([], 'throw ' + ref) + ')'
829
- );
832
+ : '().catch(' +
833
+ this.createEffectfulFunction([], 'throw ' + ref) +
834
+ ')');
830
835
  } else {
831
836
  this.stack.push(id);
832
837
  const result = this.serialize(fulfilled);
833
838
  this.stack.pop();
834
839
  // just inline the value/reference here
835
- serialized = constructor + '(' + result + ')';
840
+ serialized = promiseConstructor + '(' + result + ')';
836
841
  }
837
842
  return this.assignIndexedValue(id, serialized);
838
843
  }
839
844
 
840
- protected serializeWKSymbol(
841
- node: SerovalWKSymbolNode,
842
- ): string {
845
+ protected serializeWellKnownSymbol(node: SerovalWKSymbolNode): string {
843
846
  return this.assignIndexedValue(node.i, SYMBOL_STRING[node.s]);
844
847
  }
845
848
 
846
- protected serializeURL(
847
- node: SerovalURLNode,
848
- ): string {
849
- return this.assignIndexedValue(node.i, 'new URL("' + node.s + '")');
850
- }
851
-
852
- protected serializeURLSearchParams(
853
- node: SerovalURLSearchParamsNode,
854
- ): string {
855
- return this.assignIndexedValue(
856
- node.i,
857
- node.s ? 'new URLSearchParams("' + node.s + '")' : 'new URLSearchParams',
858
- );
859
- }
860
-
861
- protected serializeBlob(
862
- node: SerovalBlobNode,
863
- ): string {
864
- return this.assignIndexedValue(
865
- node.i,
866
- 'new Blob([' + this.serialize(node.f) + '],{type:"' + node.c + '"})',
867
- );
868
- }
869
-
870
- protected serializeFile(
871
- node: SerovalFileNode,
872
- ): string {
873
- return this.assignIndexedValue(
874
- node.i,
875
- 'new File([' + this.serialize(node.f) + '],"' + node.m + '",{type:"' + node.c + '",lastModified:' + node.b + '})',
876
- );
877
- }
878
-
879
- protected serializeHeaders(
880
- node: SerovalHeadersNode,
881
- ): string {
882
- return this.assignIndexedValue(
883
- node.i,
884
- 'new Headers(' + this.serializeProperties(node, node.e) + ')',
885
- );
886
- }
887
-
888
- protected serializeFormDataEntry(id: number, key: string, value: SerovalNode): string {
889
- return this.getRefParam(id) + '.append("' + key + '",' + this.serialize(value) + ')';
890
- }
891
-
892
- protected serializeFormDataEntries(
893
- node: SerovalFormDataNode,
894
- size: number,
895
- ): string {
896
- const keys = node.e.k;
897
- const vals = node.e.v;
898
- const id = node.i;
899
- let result = this.serializeFormDataEntry(id, keys[0], vals[0]);
900
- for (let i = 1; i < size; i++) {
901
- result += ',' + this.serializeFormDataEntry(id, keys[i], vals[i]);
902
- }
903
- return result;
904
- }
905
-
906
- protected serializeFormData(
907
- node: SerovalFormDataNode,
908
- ): string {
909
- const size = node.e.s;
910
- const id = node.i;
911
- if (size) {
912
- this.markRef(id);
913
- }
914
- const result = this.assignIndexedValue(id, 'new FormData()');
915
- if (size) {
916
- const entries = this.serializeFormDataEntries(node, size);
917
- return '(' + result + ',' + (entries ? entries + ',' : '') + this.getRefParam(id) + ')';
918
- }
919
- return result;
920
- }
921
-
922
- protected serializeBoxed(
923
- node: SerovalBoxedNode,
924
- ): string {
925
- return this.assignIndexedValue(node.i, 'Object(' + this.serialize(node.f) + ')');
926
- }
927
-
928
- protected serializeRequest(
929
- node: SerovalRequestNode,
930
- ): string {
931
- return this.assignIndexedValue(node.i, 'new Request("' + node.s + '",' + this.serialize(node.f) + ')');
932
- }
933
-
934
- protected serializeResponse(
935
- node: SerovalResponseNode,
936
- ): string {
937
- return this.assignIndexedValue(
938
- node.i,
939
- 'new Response(' + this.serialize(node.a[0]) + ',' + this.serialize(node.a[1]) + ')',
940
- );
941
- }
942
-
943
- protected serializeEvent(
944
- node: SerovalEventNode,
945
- ): string {
946
- return this.assignIndexedValue(
947
- node.i,
948
- 'new Event("' + node.s + '",' + this.serialize(node.f) + ')',
949
- );
950
- }
951
-
952
- protected serializeCustomEvent(
953
- node: SerovalCustomEventNode,
849
+ protected serializeFormDataEntry(
850
+ id: number,
851
+ key: string,
852
+ value: SerovalNode,
954
853
  ): string {
955
- return this.assignIndexedValue(
956
- node.i,
957
- 'new CustomEvent("' + node.s + '",' + this.serialize(node.f) + ')',
854
+ return (
855
+ this.getRefParam(id) +
856
+ '.append("' +
857
+ key +
858
+ '",' +
859
+ this.serialize(value) +
860
+ ')'
958
861
  );
959
862
  }
960
863
 
961
- protected serializeDOMException(
962
- node: SerovalDOMExceptionNode,
963
- ): string {
864
+ protected serializeBoxed(node: SerovalBoxedNode): string {
964
865
  return this.assignIndexedValue(
965
866
  node.i,
966
- 'new DOMException("' + node.s + '","' + node.c + '")',
867
+ 'Object(' + this.serialize(node.f) + ')',
967
868
  );
968
869
  }
969
870
 
970
- protected serializePlugin(
971
- node: SerovalPluginNode,
972
- ): string {
871
+ protected serializePlugin(node: SerovalPluginNode): string {
973
872
  const currentPlugins = this.plugins;
974
873
  if (currentPlugins) {
975
874
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
@@ -988,77 +887,48 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
988
887
  }
989
888
 
990
889
  private getConstructor(node: SerovalNodeWithID): string {
991
- const constructor = this.serialize(node);
992
- return (constructor === this.getRefParam(node.i)) ? constructor : ('(' + constructor + ')');
890
+ const current = this.serialize(node);
891
+ return current === this.getRefParam(node.i) ? current : '(' + current + ')';
993
892
  }
994
893
 
995
894
  protected serializePromiseConstructor(
996
895
  node: SerovalPromiseConstructorNode,
997
896
  ): string {
998
- return this.assignIndexedValue(
999
- node.i,
1000
- this.getConstructor(node.f) + '()',
1001
- );
1002
- }
1003
-
1004
- protected serializePromiseResolve(
1005
- node: SerovalPromiseResolveNode,
1006
- ): string {
1007
- return this.getConstructor(node.a[0]) + '(' + this.getRefParam(node.i) + ',' + this.serialize(node.a[1]) + ')';
1008
- }
1009
-
1010
- protected serializePromiseReject(
1011
- node: SerovalPromiseRejectNode,
1012
- ): string {
1013
- return this.getConstructor(node.a[0]) + '(' + this.getRefParam(node.i) + ',' + this.serialize(node.a[1]) + ')';
897
+ return this.assignIndexedValue(node.i, this.getConstructor(node.f) + '()');
1014
898
  }
1015
899
 
1016
- protected serializeReadableStreamConstructor(
1017
- node: SerovalReadableStreamConstructorNode,
1018
- ): string {
1019
- return this.assignIndexedValue(
1020
- node.i,
1021
- this.getConstructor(node.f) + '()',
900
+ protected serializePromiseResolve(node: SerovalPromiseResolveNode): string {
901
+ return (
902
+ this.getConstructor(node.a[0]) +
903
+ '(' +
904
+ this.getRefParam(node.i) +
905
+ ',' +
906
+ this.serialize(node.a[1]) +
907
+ ')'
1022
908
  );
1023
909
  }
1024
910
 
1025
- protected serializeReadableStreamEnqueue(
1026
- node: SerovalReadableStreamEnqueueNode,
1027
- ): string {
1028
- return this.getConstructor(node.a[0]) + '(' + this.getRefParam(node.i) + ',' + this.serialize(node.a[1]) + ')';
1029
- }
1030
-
1031
- protected serializeReadableStreamError(
1032
- node: SerovalReadableStreamErrorNode,
1033
- ): string {
1034
- return this.getConstructor(node.a[0]) + '(' + this.getRefParam(node.i) + ',' + this.serialize(node.a[1]) + ')';
1035
- }
1036
-
1037
- protected serializeReadableStreamClose(
1038
- node: SerovalReadableStreamCloseNode,
1039
- ): string {
1040
- return this.getConstructor(node.f) + '(' + this.getRefParam(node.i) + ')';
911
+ protected serializePromiseReject(node: SerovalPromiseRejectNode): string {
912
+ return (
913
+ this.getConstructor(node.a[0]) +
914
+ '(' +
915
+ this.getRefParam(node.i) +
916
+ ',' +
917
+ this.serialize(node.a[1]) +
918
+ ')'
919
+ );
1041
920
  }
1042
921
 
1043
922
  private serializeSpecialReferenceValue(ref: SpecialReference): string {
1044
923
  switch (ref) {
1045
924
  case SpecialReference.MapSentinel:
1046
925
  return '[]';
1047
- case SpecialReference.ReadableStream:
1048
- return this.createFunction(
1049
- ['s'],
1050
- 'new ReadableStream({start:' + this.createFunction(
1051
- ['c'],
1052
- 'Promise.resolve().then(' + this.createEffectfulFunction(
1053
- ['i', 'v'],
1054
- 'for(i=0;i<s.d;i++)c.enqueue(s.v[i]);(s.t===-1)?c.close():c.error(s.v[i])',
1055
- ) + ')',
1056
- ) + '})',
1057
- );
1058
926
  case SpecialReference.PromiseConstructor:
1059
927
  return this.createFunction(
1060
928
  ['s', 'f', 'p'],
1061
- '((p=new Promise(' + this.createEffectfulFunction(['a', 'b'], 's=a,f=b') + ')).s=s,p.f=f,p)',
929
+ '((p=new Promise(' +
930
+ this.createEffectfulFunction(['a', 'b'], 's=a,f=b') +
931
+ ')).s=s,p.f=f,p)',
1062
932
  );
1063
933
  case SpecialReference.PromiseResolve:
1064
934
  return this.createEffectfulFunction(
@@ -1070,32 +940,50 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
1070
940
  ['p', 'd'],
1071
941
  'p.f(d),p.status="failure",p.value=d;delete p.s;delete p.f',
1072
942
  );
1073
- case SpecialReference.ReadableStreamConstructor:
943
+ case SpecialReference.StreamConstructor:
1074
944
  return this.createFunction(
1075
- ['s', 'c'],
1076
- '((s=new ReadableStream({start:' + this.createEffectfulFunction(['x'], 'c=x') + '})).c=c,s)',
1077
- );
1078
- case SpecialReference.ReadableStreamEnqueue:
1079
- return this.createEffectfulFunction(
1080
- ['s', 'd'],
1081
- 's.c.enqueue(d)',
1082
- );
1083
- case SpecialReference.ReadableStreamError:
1084
- return this.createEffectfulFunction(
1085
- ['s', 'd'],
1086
- 's.c.error(d);delete s.c',
1087
- );
1088
- case SpecialReference.ReadableStreamClose:
1089
- return this.createEffectfulFunction(
1090
- ['s'],
1091
- 's.c.close();delete s.c',
945
+ ['b', 'a', 's', 'l', 'p', 'f', 'e', 'n'],
946
+ '(b=[],a=!0,s=!1,l=[],s=0,f=' +
947
+ this.createEffectfulFunction(
948
+ ['v', 'm', 'x'],
949
+ 'for(x=0;x<s;x++)l[x]&&l[x][m](v)',
950
+ ) +
951
+ ',n=' +
952
+ this.createEffectfulFunction(
953
+ ['o', 'x', 'z', 'c'],
954
+ 'for(x=0,z=b.length;x<z;x++)(c=b[x],x===z-1?o[s?"return":"throw"](c):o.next(c))',
955
+ ) +
956
+ ',e=' +
957
+ this.createFunction(
958
+ ['o', 't'],
959
+ '(a&&(l[t=p++]=o),n(o),' +
960
+ this.createEffectfulFunction([], 'a&&(l[t]=void 0)') +
961
+ ')',
962
+ ) +
963
+ ',{__SEROVAL_STREAM__:!0,on:' +
964
+ this.createFunction(['o'], 'e(o)') +
965
+ ',next:' +
966
+ this.createEffectfulFunction(['v'], 'a&&(b.push(v),f(v,"next"))') +
967
+ ',throw:' +
968
+ this.createEffectfulFunction(
969
+ ['v'],
970
+ 'a&&(b.push(v),f(v,"throw"),a=s=!1,l.length=0)',
971
+ ) +
972
+ ',return:' +
973
+ this.createEffectfulFunction(
974
+ ['v'],
975
+ 'a&&(b.push(v),f(v,"return"),a=!1,s=!0,l.length=0)',
976
+ ) +
977
+ '})',
1092
978
  );
1093
979
  default:
1094
980
  return '';
1095
981
  }
1096
982
  }
1097
983
 
1098
- protected serializeSpecialReference(node: SerovalSpecialReferenceNode): string {
984
+ protected serializeSpecialReference(
985
+ node: SerovalSpecialReferenceNode,
986
+ ): string {
1099
987
  return this.assignIndexedValue(
1100
988
  node.i,
1101
989
  this.serializeSpecialReferenceValue(node.s),
@@ -1103,84 +991,151 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
1103
991
  }
1104
992
 
1105
993
  protected serializeIteratorFactory(node: SerovalIteratorFactoryNode): string {
1106
- return this.assignIndexedValue(
994
+ let result = '';
995
+ let initialized = false;
996
+ if (node.f.t !== SerovalNodeType.IndexedValue) {
997
+ this.markRef(node.f.i);
998
+ result = '(' + this.serialize(node.f) + ',';
999
+ initialized = true;
1000
+ }
1001
+ result += this.assignIndexedValue(
1107
1002
  node.i,
1108
1003
  this.createFunction(
1109
1004
  ['s'],
1110
1005
  this.createFunction(
1111
1006
  ['i', 'c', 'd', 't'],
1112
- '(i=0,t={[' + this.serialize(node.f) + ']:' + this.createFunction([], 't') + ','
1113
- + 'next:' + this.createEffectfulFunction([], 'if(i>s.d)return{done:!0,value:void 0};c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}') + '})',
1007
+ '(i=0,t={[' +
1008
+ this.getRefParam(node.f.i) +
1009
+ ']:' +
1010
+ this.createFunction([], 't') +
1011
+ ',next:' +
1012
+ this.createEffectfulFunction(
1013
+ [],
1014
+ 'if(i>s.d)return{done:!0,value:void 0};if(d=s.v[c=i++],c===s.t)throw d;return{done:c===s.d,value:d}',
1015
+ ) +
1016
+ '})',
1114
1017
  ),
1115
1018
  ),
1116
1019
  );
1020
+ if (initialized) {
1021
+ result += ')';
1022
+ }
1023
+ return result;
1117
1024
  }
1118
1025
 
1119
1026
  protected serializeIteratorFactoryInstance(
1120
1027
  node: SerovalIteratorFactoryInstanceNode,
1121
1028
  ): string {
1122
- return this.getConstructor(node.a[0]) + '(' + this.serialize(node.a[1]) + ')';
1123
- }
1124
-
1125
- private getStreamingAsyncIteratorFactory(node: SerovalAsyncIteratorFactoryNode): string {
1126
- return this.createFunction(
1127
- ['s'],
1128
- this.createFunction(
1129
- ['b', 't'],
1130
- '(b=s.tee(),s=b[0],b=b[1].getReader(),t={[' + this.serialize(node.f) + ']:' + this.createFunction([], 't') + ','
1131
- + 'next:' + this.createFunction(
1132
- [],
1133
- 'b.read().then(' + this.createEffectfulFunction(
1134
- ['d'],
1135
- 'if(d.done)return{done:!0,value:void 0};d=d.value;if(d[0]===1)throw d[1];return{done:d[0]===2,value:d[1]}',
1136
- ) + ')',
1137
- ) + '})',
1138
- ),
1029
+ return (
1030
+ this.getConstructor(node.a[0]) + '(' + this.serialize(node.a[1]) + ')'
1139
1031
  );
1140
1032
  }
1141
1033
 
1142
- private getBlockingAsyncIteratorFactory(node: SerovalAsyncIteratorFactoryNode): string {
1143
- return this.createFunction(
1144
- ['s'],
1034
+ protected serializeAsyncIteratorFactory(
1035
+ node: SerovalAsyncIteratorFactoryNode,
1036
+ ): string {
1037
+ const promise = node.a[0];
1038
+ const symbol = node.a[1];
1039
+
1040
+ let result = '';
1041
+
1042
+ if (promise.t !== SerovalNodeType.IndexedValue) {
1043
+ this.markRef(promise.i);
1044
+ result += '(' + this.serialize(promise);
1045
+ }
1046
+ if (symbol.t !== SerovalNodeType.IndexedValue) {
1047
+ this.markRef(symbol.i);
1048
+ result += (result ? ',' : '(') + this.serialize(symbol);
1049
+ }
1050
+ if (result) {
1051
+ result += ',';
1052
+ }
1053
+
1054
+ const iterator = this.assignIndexedValue(
1055
+ node.i,
1145
1056
  this.createFunction(
1146
- ['i', 't'],
1147
- '(i=0,t={[' + this.serialize(node.f) + ']:' + this.createFunction([], 't') + ','
1148
- + 'next:' + this.createFunction(
1149
- [],
1150
- 'Promise.resolve().then(' + this.createEffectfulFunction(
1151
- ['c', 'd'],
1152
- 'if(i>s.d)return{done:!0,value:void 0};c=i++,d=s.v[c];if(c===s.t)throw d;return{done:c===s.d,value:d}',
1153
- ) + ')',
1154
- ) + '})',
1057
+ ['s'],
1058
+ this.createFunction(
1059
+ ['b', 'c', 'p', 'd', 'e', 't', 'f'],
1060
+ '(b=[],c=0,p=[],d=-1,e=!1,f=' +
1061
+ this.createEffectfulFunction(
1062
+ ['i', 'l'],
1063
+ 'for(i=0,l=p.length;i<l;i++)p[i].s({done:!0,value:void 0})',
1064
+ ) +
1065
+ ',s.on({next:' +
1066
+ this.createEffectfulFunction(
1067
+ ['v', 't'],
1068
+ 'if(t=p.shift())t.s({done:!1,value:v});b.push(v)',
1069
+ ) +
1070
+ ',throw:' +
1071
+ this.createEffectfulFunction(
1072
+ ['v', 't'],
1073
+ 'if(t=p.shift())t.f(v);f(),d=b.length,e=!0,b.push(v)',
1074
+ ) +
1075
+ ',return:' +
1076
+ this.createEffectfulFunction(
1077
+ ['v', 't'],
1078
+ 'if(t=p.shift())t.s({done:!0,value:v});f(),d=b.length,b.push(v)',
1079
+ ) +
1080
+ '}),t={[' +
1081
+ this.getRefParam(symbol.i) +
1082
+ ']:' +
1083
+ this.createFunction([], 't') +
1084
+ ',next:' +
1085
+ this.createEffectfulFunction(
1086
+ ['i', 't', 'v'],
1087
+ 'if(d===-1){return((i=c++)>=b.length)?(p.push(t=' +
1088
+ this.getRefParam(promise.i) +
1089
+ '()),t):{done:!0,value:b[i]}}if(c>d)return{done:!0,value:void 0};if(v=b[i=c++],i!==d)return{done:!1,value:v};if(e)throw v;return{done:!0,value:v}',
1090
+ ) +
1091
+ '})',
1092
+ ),
1155
1093
  ),
1156
1094
  );
1157
- }
1158
1095
 
1159
- protected serializeAsyncIteratorFactory(node: SerovalAsyncIteratorFactoryNode): string {
1160
- return this.assignIndexedValue(
1161
- node.i,
1162
- node.s
1163
- ? this.getStreamingAsyncIteratorFactory(node)
1164
- : this.getBlockingAsyncIteratorFactory(node),
1165
- );
1096
+ if (result) {
1097
+ return result + iterator + ')';
1098
+ }
1099
+
1100
+ return iterator;
1166
1101
  }
1167
1102
 
1168
1103
  protected serializeAsyncIteratorFactoryInstance(
1169
1104
  node: SerovalAsyncIteratorFactoryInstanceNode,
1170
1105
  ): string {
1171
- return this.getConstructor(node.a[0]) + '(' + this.serialize(node.a[1]) + ')';
1106
+ return (
1107
+ this.getConstructor(node.a[0]) + '(' + this.serialize(node.a[1]) + ')'
1108
+ );
1172
1109
  }
1173
1110
 
1174
- protected serializeReadableStream(
1175
- node: SerovalReadableStreamNode,
1111
+ protected serializeStreamConstructor(
1112
+ node: SerovalStreamConstructorNode,
1176
1113
  ): string {
1177
- this.stack.push(node.i);
1178
- const result = this.getConstructor(node.a[0]) + '(' + this.serialize(node.a[1]) + ')';
1179
- this.stack.pop();
1180
- return this.assignIndexedValue(
1114
+ const result = this.assignIndexedValue(
1181
1115
  node.i,
1182
- result,
1116
+ this.getConstructor(node.f) + '()',
1183
1117
  );
1118
+ const len = node.a.length;
1119
+ if (len) {
1120
+ let values = this.serialize(node.a[0]);
1121
+ for (let i = 1; i < len; i++) {
1122
+ values += ',' + this.serialize(node.a[i]);
1123
+ }
1124
+ return '(' + result + ',' + values + ',' + this.getRefParam(node.i) + ')';
1125
+ }
1126
+ return result;
1127
+ }
1128
+
1129
+ protected serializeStreamNext(node: SerovalStreamNextNode): string {
1130
+ return this.getRefParam(node.i) + '.next(' + this.serialize(node.f) + ')';
1131
+ }
1132
+
1133
+ protected serializeStreamThrow(node: SerovalStreamThrowNode): string {
1134
+ return this.getRefParam(node.i) + '.throw(' + this.serialize(node.f) + ')';
1135
+ }
1136
+
1137
+ protected serializeStreamReturn(node: SerovalStreamReturnNode): string {
1138
+ return this.getRefParam(node.i) + '.return(' + this.serialize(node.f) + ')';
1184
1139
  }
1185
1140
 
1186
1141
  serialize(node: SerovalNode): string {
@@ -1225,19 +1180,7 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
1225
1180
  case SerovalNodeType.Promise:
1226
1181
  return this.serializePromise(node);
1227
1182
  case SerovalNodeType.WKSymbol:
1228
- return this.serializeWKSymbol(node);
1229
- case SerovalNodeType.URL:
1230
- return this.serializeURL(node);
1231
- case SerovalNodeType.URLSearchParams:
1232
- return this.serializeURLSearchParams(node);
1233
- case SerovalNodeType.Blob:
1234
- return this.serializeBlob(node);
1235
- case SerovalNodeType.File:
1236
- return this.serializeFile(node);
1237
- case SerovalNodeType.Headers:
1238
- return this.serializeHeaders(node);
1239
- case SerovalNodeType.FormData:
1240
- return this.serializeFormData(node);
1183
+ return this.serializeWellKnownSymbol(node);
1241
1184
  case SerovalNodeType.Boxed:
1242
1185
  return this.serializeBoxed(node);
1243
1186
  case SerovalNodeType.PromiseConstructor:
@@ -1246,24 +1189,6 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
1246
1189
  return this.serializePromiseResolve(node);
1247
1190
  case SerovalNodeType.PromiseReject:
1248
1191
  return this.serializePromiseReject(node);
1249
- case SerovalNodeType.ReadableStreamConstructor:
1250
- return this.serializeReadableStreamConstructor(node);
1251
- case SerovalNodeType.ReadableStreamEnqueue:
1252
- return this.serializeReadableStreamEnqueue(node);
1253
- case SerovalNodeType.ReadableStreamError:
1254
- return this.serializeReadableStreamError(node);
1255
- case SerovalNodeType.ReadableStreamClose:
1256
- return this.serializeReadableStreamClose(node);
1257
- case SerovalNodeType.Request:
1258
- return this.serializeRequest(node);
1259
- case SerovalNodeType.Response:
1260
- return this.serializeResponse(node);
1261
- case SerovalNodeType.Event:
1262
- return this.serializeEvent(node);
1263
- case SerovalNodeType.CustomEvent:
1264
- return this.serializeCustomEvent(node);
1265
- case SerovalNodeType.DOMException:
1266
- return this.serializeDOMException(node);
1267
1192
  case SerovalNodeType.Plugin:
1268
1193
  return this.serializePlugin(node);
1269
1194
  case SerovalNodeType.SpecialReference:
@@ -1276,8 +1201,14 @@ export default abstract class BaseSerializerContext implements PluginAccessOptio
1276
1201
  return this.serializeAsyncIteratorFactory(node);
1277
1202
  case SerovalNodeType.AsyncIteratorFactoryInstance:
1278
1203
  return this.serializeAsyncIteratorFactoryInstance(node);
1279
- case SerovalNodeType.ReadableStream:
1280
- return this.serializeReadableStream(node);
1204
+ case SerovalNodeType.StreamConstructor:
1205
+ return this.serializeStreamConstructor(node);
1206
+ case SerovalNodeType.StreamNext:
1207
+ return this.serializeStreamNext(node);
1208
+ case SerovalNodeType.StreamThrow:
1209
+ return this.serializeStreamThrow(node);
1210
+ case SerovalNodeType.StreamReturn:
1211
+ return this.serializeStreamReturn(node);
1281
1212
  default:
1282
1213
  throw new Error('invariant');
1283
1214
  }