seroval 0.7.0 → 0.9.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 (42) hide show
  1. package/dist/cjs/development/index.cjs +1247 -1069
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +1247 -1069
  5. package/dist/esm/development/index.mjs.map +4 -4
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/assert.d.ts +1 -1
  8. package/dist/types/context.d.ts +8 -2
  9. package/dist/types/index.d.ts +3 -4
  10. package/dist/types/string.d.ts +1 -1
  11. package/dist/types/tree/UnsupportedTypeError.d.ts +3 -0
  12. package/dist/types/tree/async.d.ts +3 -3
  13. package/dist/types/tree/constants.d.ts +11 -0
  14. package/dist/types/tree/deserialize.d.ts +2 -2
  15. package/dist/types/tree/primitives.d.ts +9 -15
  16. package/dist/types/tree/serialize.d.ts +2 -2
  17. package/dist/types/tree/shared.d.ts +12 -8
  18. package/dist/types/tree/sync.d.ts +3 -3
  19. package/dist/types/tree/types.d.ts +74 -71
  20. package/dist/types/tree/web-api.d.ts +2 -2
  21. package/dist/types/types.d.ts +4 -4
  22. package/package.json +11 -9
  23. package/src/assert.ts +2 -2
  24. package/src/compat.ts +0 -1
  25. package/src/context.ts +20 -7
  26. package/src/get-identifier.ts +1 -1
  27. package/src/index.ts +35 -26
  28. package/src/string.ts +2 -2
  29. package/src/tree/UnsupportedTypeError.ts +8 -0
  30. package/src/tree/async.ts +256 -233
  31. package/src/tree/constants.ts +73 -0
  32. package/src/tree/deserialize.ts +112 -93
  33. package/src/tree/primitives.ts +93 -134
  34. package/src/tree/reference.ts +3 -3
  35. package/src/tree/serialize.ts +307 -201
  36. package/src/tree/shared.ts +45 -22
  37. package/src/tree/symbols.ts +13 -13
  38. package/src/tree/sync.ts +236 -198
  39. package/src/tree/types.ts +87 -94
  40. package/src/tree/web-api.ts +14 -7
  41. package/src/types.ts +7 -3
  42. package/src/tree/iterator.ts +0 -14
@@ -75,7 +75,7 @@ var DEFAULT_OPTIONS = {
75
75
  disabledFeatures: 0
76
76
  };
77
77
  function createParserContext(options = {}) {
78
- const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
78
+ const result = Object.assign({}, DEFAULT_OPTIONS, options);
79
79
  return {
80
80
  markedRefs: /* @__PURE__ */ new Set(),
81
81
  refs: /* @__PURE__ */ new Map(),
@@ -91,7 +91,8 @@ function createSerializationContext(options) {
91
91
  refSize: 0,
92
92
  features: options.features,
93
93
  markedRefs: new Set(options.markedRefs),
94
- valueMap: /* @__PURE__ */ new Map()
94
+ valueMap: /* @__PURE__ */ new Map(),
95
+ flags: []
95
96
  };
96
97
  }
97
98
  function markRef(ctx, current) {
@@ -131,7 +132,7 @@ function createIndexedValue(ctx, current) {
131
132
  // src/assert.ts
132
133
  function assert(cond, error) {
133
134
  if (!cond) {
134
- throw new Error(error);
135
+ throw error;
135
136
  }
136
137
  }
137
138
 
@@ -184,6 +185,83 @@ function deserializeString(str) {
184
185
  return str.replace(/\\"/g, '"').replace(/\\\\/g, "\\").replace(/\\n/g, "\n").replace(/\\r/g, "\r").replace(/\\b/g, "\b").replace(/\\t/g, " ").replace(/\\f/g, "\f").replace(/\\x3C/g, "<").replace(/\\u2028/g, "\u2028").replace(/\\u2029/g, "\u2029");
185
186
  }
186
187
 
188
+ // src/tree/UnsupportedTypeError.ts
189
+ var { toString } = Object.prototype;
190
+ var UnsupportedTypeError = class extends Error {
191
+ constructor(value) {
192
+ super('Unsupported type "' + toString.call(value) + '"');
193
+ }
194
+ };
195
+
196
+ // src/tree/constants.ts
197
+ function createConstantNode(value) {
198
+ return {
199
+ t: 2 /* Constant */,
200
+ i: void 0,
201
+ s: value,
202
+ l: void 0,
203
+ c: void 0,
204
+ m: void 0,
205
+ d: void 0,
206
+ a: void 0,
207
+ f: void 0,
208
+ b: void 0,
209
+ o: void 0
210
+ };
211
+ }
212
+ var TRUE_NODE = createConstantNode(2 /* True */);
213
+ var FALSE_NODE = createConstantNode(3 /* False */);
214
+ var UNDEFINED_NODE = createConstantNode(1 /* Undefined */);
215
+ var NULL_NODE = createConstantNode(0 /* Null */);
216
+ var NEG_ZERO_NODE = createConstantNode(4 /* NegativeZero */);
217
+ var INFINITY_NODE = createConstantNode(5 /* Infinity */);
218
+ var NEG_INFINITY_NODE = createConstantNode(6 /* NegativeInfinity */);
219
+ var NAN_NODE = createConstantNode(7 /* NaN */);
220
+ function serializeConstant(node) {
221
+ switch (node.s) {
222
+ case 2 /* True */:
223
+ return "!0";
224
+ case 3 /* False */:
225
+ return "!1";
226
+ case 1 /* Undefined */:
227
+ return "void 0";
228
+ case 0 /* Null */:
229
+ return "null";
230
+ case 4 /* NegativeZero */:
231
+ return "-0";
232
+ case 5 /* Infinity */:
233
+ return "1/0";
234
+ case 6 /* NegativeInfinity */:
235
+ return "-1/0";
236
+ case 7 /* NaN */:
237
+ return "NaN";
238
+ default:
239
+ throw new Error("invariant");
240
+ }
241
+ }
242
+ function deserializeConstant(node) {
243
+ switch (node.s) {
244
+ case 2 /* True */:
245
+ return true;
246
+ case 3 /* False */:
247
+ return false;
248
+ case 1 /* Undefined */:
249
+ return void 0;
250
+ case 0 /* Null */:
251
+ return null;
252
+ case 4 /* NegativeZero */:
253
+ return -0;
254
+ case 5 /* Infinity */:
255
+ return Infinity;
256
+ case 6 /* NegativeInfinity */:
257
+ return -Infinity;
258
+ case 7 /* NaN */:
259
+ return NaN;
260
+ default:
261
+ throw new Error("invariant");
262
+ }
263
+ }
264
+
187
265
  // src/tree/reference.ts
188
266
  var REFERENCE = /* @__PURE__ */ new Map();
189
267
  var INV_REFERENCE = /* @__PURE__ */ new Map();
@@ -199,11 +277,11 @@ function hasReference(id) {
199
277
  return INV_REFERENCE.has(id);
200
278
  }
201
279
  function getReferenceID(value) {
202
- assert(hasReferenceID(value), "Missing reference id");
280
+ assert(hasReferenceID(value), new Error("Missing reference id"));
203
281
  return REFERENCE.get(value);
204
282
  }
205
283
  function getReference(id) {
206
- assert(hasReference(id), "Missing reference for id:" + id);
284
+ assert(hasReference(id), new Error("Missing reference for id:" + id));
207
285
  return INV_REFERENCE.get(id);
208
286
  }
209
287
  var GLOBAL_KEY = "__SEROVAL__";
@@ -285,115 +363,33 @@ var SYMBOL_REF = {
285
363
  };
286
364
 
287
365
  // src/tree/primitives.ts
288
- var TRUE_NODE = {
289
- t: 2 /* Boolean */,
290
- i: void 0,
291
- s: true,
292
- l: void 0,
293
- c: void 0,
294
- m: void 0,
295
- d: void 0,
296
- a: void 0,
297
- f: void 0,
298
- b: void 0
299
- };
300
- var FALSE_NODE = {
301
- t: 2 /* Boolean */,
302
- i: void 0,
303
- s: false,
304
- l: void 0,
305
- c: void 0,
306
- m: void 0,
307
- d: void 0,
308
- a: void 0,
309
- f: void 0,
310
- b: void 0
311
- };
312
- var UNDEFINED_NODE = {
313
- t: 4 /* Undefined */,
314
- i: void 0,
315
- s: void 0,
316
- l: void 0,
317
- c: void 0,
318
- m: void 0,
319
- d: void 0,
320
- a: void 0,
321
- f: void 0,
322
- b: void 0
323
- };
324
- var NULL_NODE = {
325
- t: 3 /* Null */,
326
- i: void 0,
327
- s: void 0,
328
- l: void 0,
329
- c: void 0,
330
- m: void 0,
331
- d: void 0,
332
- a: void 0,
333
- f: void 0,
334
- b: void 0
335
- };
336
- var NEG_ZERO_NODE = {
337
- t: 5 /* NegativeZero */,
338
- i: void 0,
339
- s: void 0,
340
- l: void 0,
341
- c: void 0,
342
- m: void 0,
343
- d: void 0,
344
- a: void 0,
345
- f: void 0,
346
- b: void 0
347
- };
348
- var INFINITY_NODE = {
349
- t: 6 /* Infinity */,
350
- i: void 0,
351
- s: void 0,
352
- l: void 0,
353
- c: void 0,
354
- m: void 0,
355
- d: void 0,
356
- a: void 0,
357
- f: void 0,
358
- b: void 0
359
- };
360
- var NEG_INFINITY_NODE = {
361
- t: 7 /* NegativeInfinity */,
362
- i: void 0,
363
- s: void 0,
364
- l: void 0,
365
- c: void 0,
366
- m: void 0,
367
- d: void 0,
368
- a: void 0,
369
- f: void 0,
370
- b: void 0
371
- };
372
- var NAN_NODE = {
373
- t: 8 /* NaN */,
374
- i: void 0,
375
- s: void 0,
376
- l: void 0,
377
- c: void 0,
378
- m: void 0,
379
- d: void 0,
380
- a: void 0,
381
- f: void 0,
382
- b: void 0
383
- };
384
366
  function createNumberNode(value) {
385
- return {
386
- t: 0 /* Number */,
387
- i: void 0,
388
- s: value,
389
- l: void 0,
390
- c: void 0,
391
- m: void 0,
392
- d: void 0,
393
- a: void 0,
394
- f: void 0,
395
- b: void 0
396
- };
367
+ switch (value) {
368
+ case Infinity:
369
+ return INFINITY_NODE;
370
+ case -Infinity:
371
+ return NEG_INFINITY_NODE;
372
+ default:
373
+ if (value !== value) {
374
+ return NAN_NODE;
375
+ }
376
+ if (Object.is(value, -0)) {
377
+ return NEG_ZERO_NODE;
378
+ }
379
+ return {
380
+ t: 0 /* Number */,
381
+ i: void 0,
382
+ s: value,
383
+ l: void 0,
384
+ c: void 0,
385
+ m: void 0,
386
+ d: void 0,
387
+ a: void 0,
388
+ f: void 0,
389
+ b: void 0,
390
+ o: void 0
391
+ };
392
+ }
397
393
  }
398
394
  function createStringNode(value) {
399
395
  return {
@@ -406,13 +402,14 @@ function createStringNode(value) {
406
402
  d: void 0,
407
403
  a: void 0,
408
404
  f: void 0,
409
- b: void 0
405
+ b: void 0,
406
+ o: void 0
410
407
  };
411
408
  }
412
409
  function createBigIntNode(ctx, current) {
413
- assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
410
+ assert(ctx.features & 8 /* BigInt */, new UnsupportedTypeError(current));
414
411
  return {
415
- t: 9 /* BigInt */,
412
+ t: 3 /* BigInt */,
416
413
  i: void 0,
417
414
  s: "" + current,
418
415
  l: void 0,
@@ -421,12 +418,13 @@ function createBigIntNode(ctx, current) {
421
418
  d: void 0,
422
419
  a: void 0,
423
420
  f: void 0,
424
- b: void 0
421
+ b: void 0,
422
+ o: void 0
425
423
  };
426
424
  }
427
425
  function createIndexedValueNode(id) {
428
426
  return {
429
- t: 10 /* IndexedValue */,
427
+ t: 4 /* IndexedValue */,
430
428
  i: id,
431
429
  s: void 0,
432
430
  l: void 0,
@@ -435,12 +433,13 @@ function createIndexedValueNode(id) {
435
433
  d: void 0,
436
434
  a: void 0,
437
435
  f: void 0,
438
- b: void 0
436
+ b: void 0,
437
+ o: void 0
439
438
  };
440
439
  }
441
440
  function createDateNode(id, current) {
442
441
  return {
443
- t: 11 /* Date */,
442
+ t: 5 /* Date */,
444
443
  i: id,
445
444
  s: current.toISOString(),
446
445
  l: void 0,
@@ -449,12 +448,13 @@ function createDateNode(id, current) {
449
448
  d: void 0,
450
449
  f: void 0,
451
450
  a: void 0,
452
- b: void 0
451
+ b: void 0,
452
+ o: void 0
453
453
  };
454
454
  }
455
455
  function createRegExpNode(id, current) {
456
456
  return {
457
- t: 12 /* RegExp */,
457
+ t: 6 /* RegExp */,
458
458
  i: id,
459
459
  s: void 0,
460
460
  l: void 0,
@@ -463,7 +463,8 @@ function createRegExpNode(id, current) {
463
463
  d: void 0,
464
464
  a: void 0,
465
465
  f: void 0,
466
- b: void 0
466
+ b: void 0,
467
+ o: void 0
467
468
  };
468
469
  }
469
470
  function createArrayBufferNode(id, current) {
@@ -474,7 +475,7 @@ function createArrayBufferNode(id, current) {
474
475
  values[i] = bytes[i];
475
476
  }
476
477
  return {
477
- t: 28 /* ArrayBuffer */,
478
+ t: 21 /* ArrayBuffer */,
478
479
  i: id,
479
480
  s: values,
480
481
  l: void 0,
@@ -483,7 +484,8 @@ function createArrayBufferNode(id, current) {
483
484
  d: void 0,
484
485
  a: void 0,
485
486
  f: void 0,
486
- b: void 0
487
+ b: void 0,
488
+ o: void 0
487
489
  };
488
490
  }
489
491
  function serializeArrayBuffer(ctx, current) {
@@ -494,47 +496,47 @@ function serializeArrayBuffer(ctx, current) {
494
496
  return createArrayBufferNode(id, current);
495
497
  }
496
498
  function createTypedArrayNode(ctx, id, current) {
497
- const constructor = current.constructor.name;
498
- assert(ctx.features & 2048 /* TypedArray */, `Unsupported value type "${constructor}"`);
499
+ assert(ctx.features & 2048 /* TypedArray */, new UnsupportedTypeError(current));
499
500
  return {
500
- t: 22 /* TypedArray */,
501
+ t: 15 /* TypedArray */,
501
502
  i: id,
502
503
  s: void 0,
503
504
  l: current.length,
504
- c: constructor,
505
+ c: current.constructor.name,
505
506
  m: void 0,
506
507
  d: void 0,
507
508
  a: void 0,
508
509
  f: serializeArrayBuffer(ctx, current.buffer),
509
- b: current.byteOffset
510
+ b: current.byteOffset,
511
+ o: void 0
510
512
  };
511
513
  }
512
514
  var BIGINT_FLAG = 4096 /* BigIntTypedArray */ | 8 /* BigInt */;
513
515
  function createBigIntTypedArrayNode(ctx, id, current) {
514
- const constructor = current.constructor.name;
515
516
  assert(
516
517
  (ctx.features & BIGINT_FLAG) === BIGINT_FLAG,
517
- `Unsupported value type "${constructor}"`
518
+ new UnsupportedTypeError(current)
518
519
  );
519
520
  return {
520
- t: 23 /* BigIntTypedArray */,
521
+ t: 16 /* BigIntTypedArray */,
521
522
  i: id,
522
523
  s: void 0,
523
524
  l: current.length,
524
- c: constructor,
525
+ c: current.constructor.name,
525
526
  m: void 0,
526
527
  d: void 0,
527
528
  a: void 0,
528
529
  f: serializeArrayBuffer(ctx, current.buffer),
529
- b: current.byteOffset
530
+ b: current.byteOffset,
531
+ o: void 0
530
532
  };
531
533
  }
532
- function createWKSymbolNode(ctx, current) {
533
- assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "symbol"');
534
- assert(current in INV_SYMBOL_REF, "seroval only supports well-known symbols");
534
+ function createWKSymbolNode(ctx, id, current) {
535
+ assert(ctx.features & 1024 /* Symbol */, new UnsupportedTypeError(current));
536
+ assert(current in INV_SYMBOL_REF, new Error("Only well-known symbols are supported."));
535
537
  return {
536
- t: 24 /* WKSymbol */,
537
- i: void 0,
538
+ t: 17 /* WKSymbol */,
539
+ i: id,
538
540
  s: INV_SYMBOL_REF[current],
539
541
  l: void 0,
540
542
  c: void 0,
@@ -542,12 +544,13 @@ function createWKSymbolNode(ctx, current) {
542
544
  d: void 0,
543
545
  a: void 0,
544
546
  f: void 0,
545
- b: void 0
547
+ b: void 0,
548
+ o: void 0
546
549
  };
547
550
  }
548
551
  function createReferenceNode(id, ref) {
549
552
  return {
550
- t: 27 /* Reference */,
553
+ t: 20 /* Reference */,
551
554
  i: id,
552
555
  s: serializeString(getReferenceID(ref)),
553
556
  l: void 0,
@@ -556,12 +559,13 @@ function createReferenceNode(id, ref) {
556
559
  d: void 0,
557
560
  a: void 0,
558
561
  f: void 0,
559
- b: void 0
562
+ b: void 0,
563
+ o: void 0
560
564
  };
561
565
  }
562
566
  function createDataViewNode(ctx, id, current) {
563
567
  return {
564
- t: 29 /* DataView */,
568
+ t: 22 /* DataView */,
565
569
  i: id,
566
570
  s: void 0,
567
571
  l: current.byteLength,
@@ -570,9 +574,28 @@ function createDataViewNode(ctx, id, current) {
570
574
  d: void 0,
571
575
  a: void 0,
572
576
  f: serializeArrayBuffer(ctx, current.buffer),
573
- b: current.byteOffset
577
+ b: current.byteOffset,
578
+ o: void 0
574
579
  };
575
580
  }
581
+ function createSymbolNode(ctx, current) {
582
+ const id = createIndexedValue(ctx, current);
583
+ if (ctx.markedRefs.has(id)) {
584
+ return createIndexedValueNode(id);
585
+ }
586
+ if (hasReferenceID(current)) {
587
+ return createReferenceNode(id, current);
588
+ }
589
+ return createWKSymbolNode(ctx, id, current);
590
+ }
591
+ function createFunctionNode(ctx, current) {
592
+ assert(hasReferenceID(current), new Error("Cannot serialize function without reference ID."));
593
+ const id = createIndexedValue(ctx, current);
594
+ if (ctx.markedRefs.has(id)) {
595
+ return createIndexedValueNode(id);
596
+ }
597
+ return createReferenceNode(id, current);
598
+ }
576
599
 
577
600
  // src/tree/shared.ts
578
601
  function getErrorConstructorName(error) {
@@ -640,17 +663,6 @@ function getErrorOptions(ctx, error) {
640
663
  }
641
664
  return options;
642
665
  }
643
- function getIterableOptions(obj) {
644
- const names = Object.getOwnPropertyNames(obj);
645
- if (names.length) {
646
- const options = {};
647
- for (const name of names) {
648
- options[name] = obj[name];
649
- }
650
- return options;
651
- }
652
- return void 0;
653
- }
654
666
  function isIterable(value) {
655
667
  if (!value || typeof value !== "object") {
656
668
  return false;
@@ -711,12 +723,24 @@ function isValidIdentifier(name) {
711
723
  const char = name[0];
712
724
  return (char === "$" || char === "_" || char >= "A" && char <= "Z" || char >= "a" && char <= "z") && IDENTIFIER_CHECK.test(name);
713
725
  }
726
+ function getObjectFlag(obj) {
727
+ if (Object.isFrozen(obj)) {
728
+ return 3 /* Frozen */;
729
+ }
730
+ if (Object.isSealed(obj)) {
731
+ return 2 /* Sealed */;
732
+ }
733
+ if (Object.isExtensible(obj)) {
734
+ return 0 /* None */;
735
+ }
736
+ return 1 /* NonExtensible */;
737
+ }
714
738
 
715
739
  // src/tree/web-api.ts
716
740
  function createURLNode(ctx, id, current) {
717
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "URL"');
741
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
718
742
  return {
719
- t: 25 /* URL */,
743
+ t: 18 /* URL */,
720
744
  i: id,
721
745
  s: serializeString(current.href),
722
746
  l: void 0,
@@ -725,13 +749,14 @@ function createURLNode(ctx, id, current) {
725
749
  d: void 0,
726
750
  f: void 0,
727
751
  a: void 0,
728
- b: void 0
752
+ b: void 0,
753
+ o: void 0
729
754
  };
730
755
  }
731
756
  function createURLSearchParamsNode(ctx, id, current) {
732
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "URLSearchParams"');
757
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
733
758
  return {
734
- t: 26 /* URLSearchParams */,
759
+ t: 19 /* URLSearchParams */,
735
760
  i: id,
736
761
  s: serializeString(current.toString()),
737
762
  l: void 0,
@@ -740,13 +765,14 @@ function createURLSearchParamsNode(ctx, id, current) {
740
765
  d: void 0,
741
766
  f: void 0,
742
767
  a: void 0,
743
- b: void 0
768
+ b: void 0,
769
+ o: void 0
744
770
  };
745
771
  }
746
772
  async function createBlobNode(ctx, id, current) {
747
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "Blob"');
773
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
748
774
  return {
749
- t: 30 /* Blob */,
775
+ t: 23 /* Blob */,
750
776
  i: id,
751
777
  s: void 0,
752
778
  l: void 0,
@@ -755,13 +781,14 @@ async function createBlobNode(ctx, id, current) {
755
781
  d: void 0,
756
782
  f: serializeArrayBuffer(ctx, await current.arrayBuffer()),
757
783
  a: void 0,
758
- b: void 0
784
+ b: void 0,
785
+ o: void 0
759
786
  };
760
787
  }
761
788
  async function createFileNode(ctx, id, current) {
762
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "File"');
789
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
763
790
  return {
764
- t: 31 /* File */,
791
+ t: 24 /* File */,
765
792
  i: id,
766
793
  s: void 0,
767
794
  l: void 0,
@@ -770,12 +797,13 @@ async function createFileNode(ctx, id, current) {
770
797
  d: void 0,
771
798
  f: serializeArrayBuffer(ctx, await current.arrayBuffer()),
772
799
  a: void 0,
773
- b: current.lastModified
800
+ b: current.lastModified,
801
+ o: void 0
774
802
  };
775
803
  }
776
804
 
777
- // src/tree/async.ts
778
- async function generateNodeList(ctx, current) {
805
+ // src/tree/sync.ts
806
+ function generateNodeList(ctx, current) {
779
807
  const size = current.length;
780
808
  const nodes = new Array(size);
781
809
  const deferred = new Array(size);
@@ -786,33 +814,34 @@ async function generateNodeList(ctx, current) {
786
814
  if (isIterable(item)) {
787
815
  deferred[i] = item;
788
816
  } else {
789
- nodes[i] = await parse(ctx, item);
817
+ nodes[i] = parseSync(ctx, item);
790
818
  }
791
819
  }
792
820
  }
793
821
  for (let i = 0; i < size; i++) {
794
822
  if (i in deferred) {
795
- nodes[i] = await parse(ctx, deferred[i]);
823
+ nodes[i] = parseSync(ctx, deferred[i]);
796
824
  }
797
825
  }
798
826
  return nodes;
799
827
  }
800
- async function generateArrayNode(ctx, id, current) {
828
+ function generateArrayNode(ctx, id, current) {
801
829
  return {
802
- t: 15 /* Array */,
830
+ t: 9 /* Array */,
803
831
  i: id,
804
832
  s: void 0,
805
833
  l: current.length,
806
834
  c: void 0,
807
835
  m: void 0,
808
836
  d: void 0,
809
- a: await generateNodeList(ctx, current),
837
+ a: generateNodeList(ctx, current),
810
838
  f: void 0,
811
- b: void 0
839
+ b: void 0,
840
+ o: getObjectFlag(current)
812
841
  };
813
842
  }
814
- async function generateMapNode(ctx, id, current) {
815
- assert(ctx.features & 32 /* Map */, 'Unsupported type "Map"');
843
+ function generateMapNode(ctx, id, current) {
844
+ assert(ctx.features & 32 /* Map */, new UnsupportedTypeError(current));
816
845
  const len = current.size;
817
846
  const keyNodes = new Array(len);
818
847
  const valueNodes = new Array(len);
@@ -826,17 +855,17 @@ async function generateMapNode(ctx, id, current) {
826
855
  deferredValue[deferredSize] = value;
827
856
  deferredSize++;
828
857
  } else {
829
- keyNodes[nodeSize] = await parse(ctx, key);
830
- valueNodes[nodeSize] = await parse(ctx, value);
858
+ keyNodes[nodeSize] = parseSync(ctx, key);
859
+ valueNodes[nodeSize] = parseSync(ctx, value);
831
860
  nodeSize++;
832
861
  }
833
862
  }
834
863
  for (let i = 0; i < deferredSize; i++) {
835
- keyNodes[nodeSize + i] = await parse(ctx, deferredKey[i]);
836
- valueNodes[nodeSize + i] = await parse(ctx, deferredValue[i]);
864
+ keyNodes[nodeSize + i] = parseSync(ctx, deferredKey[i]);
865
+ valueNodes[nodeSize + i] = parseSync(ctx, deferredValue[i]);
837
866
  }
838
867
  return {
839
- t: 14 /* Map */,
868
+ t: 8 /* Map */,
840
869
  i: id,
841
870
  s: void 0,
842
871
  l: void 0,
@@ -845,11 +874,12 @@ async function generateMapNode(ctx, id, current) {
845
874
  d: { k: keyNodes, v: valueNodes, s: len },
846
875
  a: void 0,
847
876
  f: void 0,
848
- b: void 0
877
+ b: void 0,
878
+ o: void 0
849
879
  };
850
880
  }
851
- async function generateSetNode(ctx, id, current) {
852
- assert(ctx.features & 512 /* Set */, 'Unsupported type "Set"');
881
+ function generateSetNode(ctx, id, current) {
882
+ assert(ctx.features & 512 /* Set */, new UnsupportedTypeError(current));
853
883
  const len = current.size;
854
884
  const nodes = new Array(len);
855
885
  const deferred = new Array(len);
@@ -859,14 +889,14 @@ async function generateSetNode(ctx, id, current) {
859
889
  if (isIterable(item)) {
860
890
  deferred[deferredSize++] = item;
861
891
  } else {
862
- nodes[nodeSize++] = await parse(ctx, item);
892
+ nodes[nodeSize++] = parseSync(ctx, item);
863
893
  }
864
894
  }
865
895
  for (let i = 0; i < deferredSize; i++) {
866
- nodes[nodeSize + i] = await parse(ctx, deferred[i]);
896
+ nodes[nodeSize + i] = parseSync(ctx, deferred[i]);
867
897
  }
868
898
  return {
869
- t: 13 /* Set */,
899
+ t: 7 /* Set */,
870
900
  i: id,
871
901
  s: void 0,
872
902
  l: len,
@@ -875,12 +905,13 @@ async function generateSetNode(ctx, id, current) {
875
905
  d: void 0,
876
906
  a: nodes,
877
907
  f: void 0,
878
- b: void 0
908
+ b: void 0,
909
+ o: void 0
879
910
  };
880
911
  }
881
- async function generateProperties(ctx, properties) {
912
+ function generateProperties(ctx, properties) {
882
913
  const keys = Object.keys(properties);
883
- const size = keys.length;
914
+ let size = keys.length;
884
915
  const keyNodes = new Array(size);
885
916
  const valueNodes = new Array(size);
886
917
  const deferredKeys = new Array(size);
@@ -898,13 +929,22 @@ async function generateProperties(ctx, properties) {
898
929
  deferredSize++;
899
930
  } else {
900
931
  keyNodes[nodesSize] = escaped;
901
- valueNodes[nodesSize] = await parse(ctx, item);
932
+ valueNodes[nodesSize] = parseSync(ctx, item);
902
933
  nodesSize++;
903
934
  }
904
935
  }
905
936
  for (let i = 0; i < deferredSize; i++) {
906
937
  keyNodes[nodesSize + i] = deferredKeys[i];
907
- valueNodes[nodesSize + i] = await parse(ctx, deferredValues[i]);
938
+ valueNodes[nodesSize + i] = parseSync(ctx, deferredValues[i]);
939
+ }
940
+ if (ctx.features & 1024 /* Symbol */) {
941
+ if (Symbol.iterator in properties) {
942
+ keyNodes[size] = 0 /* SymbolIterator */;
943
+ const items = Array.from(properties);
944
+ const id = createIndexedValue(ctx, items);
945
+ valueNodes[size] = generateArrayNode(ctx, id, items);
946
+ size++;
947
+ }
908
948
  }
909
949
  return {
910
950
  k: keyNodes,
@@ -912,65 +952,60 @@ async function generateProperties(ctx, properties) {
912
952
  s: size
913
953
  };
914
954
  }
915
- async function generateIterableNode(ctx, id, current) {
916
- assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "Iterable"');
917
- const options = getIterableOptions(current);
918
- const array = Array.from(current);
955
+ function generatePlainProperties(ctx, properties) {
956
+ const keys = Object.keys(properties);
957
+ const size = keys.length;
958
+ const keyNodes = new Array(size);
959
+ const valueNodes = new Array(size);
960
+ const deferredKeys = new Array(size);
961
+ const deferredValues = new Array(size);
962
+ let deferredSize = 0;
963
+ let nodesSize = 0;
964
+ let item;
965
+ let escaped;
966
+ for (const key of keys) {
967
+ item = properties[key];
968
+ escaped = serializeString(key);
969
+ if (isIterable(item)) {
970
+ deferredKeys[deferredSize] = escaped;
971
+ deferredValues[deferredSize] = item;
972
+ deferredSize++;
973
+ } else {
974
+ keyNodes[nodesSize] = escaped;
975
+ valueNodes[nodesSize] = parseSync(ctx, item);
976
+ nodesSize++;
977
+ }
978
+ }
979
+ for (let i = 0; i < deferredSize; i++) {
980
+ keyNodes[nodesSize + i] = deferredKeys[i];
981
+ valueNodes[nodesSize + i] = parseSync(ctx, deferredValues[i]);
982
+ }
919
983
  return {
920
- t: 21 /* Iterable */,
921
- i: id,
922
- s: void 0,
923
- l: array.length,
924
- c: void 0,
925
- m: void 0,
926
- // Parse options first before the items
927
- d: options ? await generateProperties(ctx, options) : void 0,
928
- a: await generateNodeList(ctx, array),
929
- f: void 0,
930
- b: void 0
984
+ k: keyNodes,
985
+ v: valueNodes,
986
+ s: size
931
987
  };
932
988
  }
933
- async function generatePromiseNode(ctx, id, current) {
934
- assert(ctx.features & 256 /* Promise */, 'Unsupported type "Promise"');
935
- return current.then(async (value) => ({
936
- t: 18 /* Promise */,
937
- i: id,
938
- s: void 0,
939
- l: void 0,
940
- c: void 0,
941
- m: void 0,
942
- // Parse options first before the items
943
- d: void 0,
944
- a: void 0,
945
- f: await parse(ctx, value),
946
- b: void 0
947
- }));
948
- }
949
- async function generateObjectNode(ctx, id, current, empty) {
950
- if (Symbol.iterator in current) {
951
- return generateIterableNode(ctx, id, current);
952
- }
953
- if ("then" in current && typeof current.then === "function") {
954
- return generatePromiseNode(ctx, id, current);
955
- }
989
+ function generateObjectNode(ctx, id, current, empty) {
956
990
  return {
957
- t: empty ? 17 /* NullConstructor */ : 16 /* Object */,
991
+ t: empty ? 11 /* NullConstructor */ : 10 /* Object */,
958
992
  i: id,
959
993
  s: void 0,
960
994
  l: void 0,
961
995
  c: void 0,
962
996
  m: void 0,
963
- d: await generateProperties(ctx, current),
997
+ d: generateProperties(ctx, current),
964
998
  a: void 0,
965
999
  f: void 0,
966
- b: void 0
1000
+ b: void 0,
1001
+ o: getObjectFlag(current)
967
1002
  };
968
1003
  }
969
- async function generateAggregateErrorNode(ctx, id, current) {
1004
+ function generateAggregateErrorNode(ctx, id, current) {
970
1005
  const options = getErrorOptions(ctx, current);
971
- const optionsNode = options ? await generateProperties(ctx, options) : void 0;
1006
+ const optionsNode = options ? generateProperties(ctx, options) : void 0;
972
1007
  return {
973
- t: 20 /* AggregateError */,
1008
+ t: 14 /* AggregateError */,
974
1009
  i: id,
975
1010
  s: void 0,
976
1011
  l: void 0,
@@ -979,14 +1014,15 @@ async function generateAggregateErrorNode(ctx, id, current) {
979
1014
  d: optionsNode,
980
1015
  a: void 0,
981
1016
  f: void 0,
982
- b: void 0
1017
+ b: void 0,
1018
+ o: void 0
983
1019
  };
984
1020
  }
985
- async function generateErrorNode(ctx, id, current) {
1021
+ function generateErrorNode(ctx, id, current) {
986
1022
  const options = getErrorOptions(ctx, current);
987
- const optionsNode = options ? await generateProperties(ctx, options) : void 0;
1023
+ const optionsNode = options ? generateProperties(ctx, options) : void 0;
988
1024
  return {
989
- t: 19 /* Error */,
1025
+ t: 13 /* Error */,
990
1026
  i: id,
991
1027
  s: void 0,
992
1028
  l: void 0,
@@ -995,48 +1031,165 @@ async function generateErrorNode(ctx, id, current) {
995
1031
  d: optionsNode,
996
1032
  a: void 0,
997
1033
  f: void 0,
998
- b: void 0
1034
+ b: void 0,
1035
+ o: void 0
999
1036
  };
1000
1037
  }
1001
- async function generateHeadersNode(ctx, id, current) {
1002
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "Headers"');
1038
+ function generateHeadersNode(ctx, id, current) {
1039
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
1003
1040
  const items = {};
1004
1041
  current.forEach((value, key) => {
1005
1042
  items[key] = value;
1006
1043
  });
1007
1044
  return {
1008
- t: 32 /* Headers */,
1045
+ t: 25 /* Headers */,
1009
1046
  i: id,
1010
1047
  s: void 0,
1011
1048
  l: void 0,
1012
1049
  c: void 0,
1013
1050
  m: void 0,
1014
- d: await generateProperties(ctx, items),
1051
+ d: generatePlainProperties(ctx, items),
1015
1052
  a: void 0,
1016
1053
  f: void 0,
1017
- b: void 0
1054
+ b: void 0,
1055
+ o: void 0
1018
1056
  };
1019
1057
  }
1020
- async function generateFormDataNode(ctx, id, current) {
1021
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "FormData"');
1058
+ function generateFormDataNode(ctx, id, current) {
1059
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
1022
1060
  const items = {};
1023
1061
  current.forEach((value, key) => {
1024
1062
  items[key] = value;
1025
1063
  });
1026
1064
  return {
1027
- t: 33 /* FormData */,
1065
+ t: 26 /* FormData */,
1028
1066
  i: id,
1029
1067
  s: void 0,
1030
1068
  l: void 0,
1031
1069
  c: void 0,
1032
1070
  m: void 0,
1033
- d: await generateProperties(ctx, items),
1071
+ d: generatePlainProperties(ctx, items),
1034
1072
  a: void 0,
1035
1073
  f: void 0,
1036
- b: void 0
1074
+ b: void 0,
1075
+ o: void 0
1076
+ };
1077
+ }
1078
+ function generateBoxedNode(ctx, id, current) {
1079
+ return {
1080
+ t: 27 /* Boxed */,
1081
+ i: id,
1082
+ s: void 0,
1083
+ l: void 0,
1084
+ c: void 0,
1085
+ m: void 0,
1086
+ d: void 0,
1087
+ a: void 0,
1088
+ f: parseSync(ctx, current.valueOf()),
1089
+ b: void 0,
1090
+ o: void 0
1037
1091
  };
1038
1092
  }
1039
- async function parse(ctx, current) {
1093
+ function parseObject(ctx, current) {
1094
+ if (!current) {
1095
+ return NULL_NODE;
1096
+ }
1097
+ const id = createIndexedValue(ctx, current);
1098
+ if (ctx.markedRefs.has(id)) {
1099
+ return createIndexedValueNode(id);
1100
+ }
1101
+ if (hasReferenceID(current)) {
1102
+ return createReferenceNode(id, current);
1103
+ }
1104
+ if (Array.isArray(current)) {
1105
+ return generateArrayNode(ctx, id, current);
1106
+ }
1107
+ switch (current.constructor) {
1108
+ case Number:
1109
+ case Boolean:
1110
+ case String:
1111
+ case BigInt:
1112
+ case Function:
1113
+ case Symbol:
1114
+ return generateBoxedNode(ctx, id, current);
1115
+ case Date:
1116
+ return createDateNode(id, current);
1117
+ case RegExp:
1118
+ return createRegExpNode(id, current);
1119
+ case ArrayBuffer:
1120
+ return createArrayBufferNode(id, current);
1121
+ case Int8Array:
1122
+ case Int16Array:
1123
+ case Int32Array:
1124
+ case Uint8Array:
1125
+ case Uint16Array:
1126
+ case Uint32Array:
1127
+ case Uint8ClampedArray:
1128
+ case Float32Array:
1129
+ case Float64Array:
1130
+ return createTypedArrayNode(ctx, id, current);
1131
+ case BigInt64Array:
1132
+ case BigUint64Array:
1133
+ return createBigIntTypedArrayNode(ctx, id, current);
1134
+ case DataView:
1135
+ return createDataViewNode(ctx, id, current);
1136
+ case Map:
1137
+ return generateMapNode(ctx, id, current);
1138
+ case Set:
1139
+ return generateSetNode(ctx, id, current);
1140
+ case Object:
1141
+ return generateObjectNode(
1142
+ ctx,
1143
+ id,
1144
+ current,
1145
+ false
1146
+ );
1147
+ case void 0:
1148
+ return generateObjectNode(
1149
+ ctx,
1150
+ id,
1151
+ current,
1152
+ true
1153
+ );
1154
+ case AggregateError:
1155
+ if (ctx.features & 1 /* AggregateError */) {
1156
+ return generateAggregateErrorNode(ctx, id, current);
1157
+ }
1158
+ return generateErrorNode(ctx, id, current);
1159
+ case Error:
1160
+ case EvalError:
1161
+ case RangeError:
1162
+ case ReferenceError:
1163
+ case SyntaxError:
1164
+ case TypeError:
1165
+ case URIError:
1166
+ return generateErrorNode(ctx, id, current);
1167
+ case URL:
1168
+ return createURLNode(ctx, id, current);
1169
+ case URLSearchParams:
1170
+ return createURLSearchParamsNode(ctx, id, current);
1171
+ case Headers:
1172
+ return generateHeadersNode(ctx, id, current);
1173
+ case FormData:
1174
+ return generateFormDataNode(ctx, id, current);
1175
+ default:
1176
+ break;
1177
+ }
1178
+ if (current instanceof AggregateError) {
1179
+ if (ctx.features & 1 /* AggregateError */) {
1180
+ return generateAggregateErrorNode(ctx, id, current);
1181
+ }
1182
+ return generateErrorNode(ctx, id, current);
1183
+ }
1184
+ if (current instanceof Error) {
1185
+ return generateErrorNode(ctx, id, current);
1186
+ }
1187
+ if (Symbol.iterator in current) {
1188
+ return generateObjectNode(ctx, id, current, !!current.constructor);
1189
+ }
1190
+ throw new UnsupportedTypeError(current);
1191
+ }
1192
+ function parseSync(ctx, current) {
1040
1193
  switch (typeof current) {
1041
1194
  case "boolean":
1042
1195
  return current ? TRUE_NODE : FALSE_NODE;
@@ -1045,210 +1198,543 @@ async function parse(ctx, current) {
1045
1198
  case "string":
1046
1199
  return createStringNode(current);
1047
1200
  case "number":
1048
- switch (current) {
1049
- case Infinity:
1050
- return INFINITY_NODE;
1051
- case -Infinity:
1052
- return NEG_INFINITY_NODE;
1053
- default:
1054
- if (current !== current) {
1055
- return NAN_NODE;
1056
- }
1057
- if (Object.is(current, -0)) {
1058
- return NEG_ZERO_NODE;
1059
- }
1060
- return createNumberNode(current);
1061
- }
1201
+ return createNumberNode(current);
1062
1202
  case "bigint":
1063
1203
  return createBigIntNode(ctx, current);
1064
- case "object": {
1065
- if (!current) {
1066
- return NULL_NODE;
1067
- }
1068
- const id = createIndexedValue(ctx, current);
1069
- if (ctx.markedRefs.has(id)) {
1070
- return createIndexedValueNode(id);
1071
- }
1072
- if (hasReferenceID(current)) {
1073
- return createReferenceNode(id, current);
1074
- }
1075
- if (Array.isArray(current)) {
1076
- return generateArrayNode(ctx, id, current);
1077
- }
1078
- switch (current.constructor) {
1079
- case Date:
1080
- return createDateNode(id, current);
1081
- case RegExp:
1082
- return createRegExpNode(id, current);
1083
- case Promise:
1084
- return generatePromiseNode(ctx, id, current);
1085
- case ArrayBuffer:
1086
- return createArrayBufferNode(id, current);
1087
- case Int8Array:
1088
- case Int16Array:
1089
- case Int32Array:
1090
- case Uint8Array:
1091
- case Uint16Array:
1092
- case Uint32Array:
1093
- case Uint8ClampedArray:
1094
- case Float32Array:
1095
- case Float64Array:
1096
- return createTypedArrayNode(ctx, id, current);
1097
- case BigInt64Array:
1098
- case BigUint64Array:
1099
- return createBigIntTypedArrayNode(ctx, id, current);
1100
- case DataView:
1101
- return createDataViewNode(ctx, id, current);
1102
- case Map:
1103
- return generateMapNode(
1104
- ctx,
1105
- id,
1106
- current
1107
- );
1108
- case Set:
1109
- return generateSetNode(
1110
- ctx,
1111
- id,
1112
- current
1113
- );
1114
- case Object:
1115
- return generateObjectNode(
1116
- ctx,
1117
- id,
1118
- current,
1119
- false
1120
- );
1121
- case void 0:
1122
- return generateObjectNode(
1123
- ctx,
1124
- id,
1125
- current,
1126
- true
1127
- );
1128
- case AggregateError:
1129
- if (ctx.features & 1 /* AggregateError */) {
1130
- return generateAggregateErrorNode(ctx, id, current);
1131
- }
1132
- return generateErrorNode(ctx, id, current);
1133
- case Error:
1134
- case EvalError:
1135
- case RangeError:
1136
- case ReferenceError:
1137
- case SyntaxError:
1138
- case TypeError:
1139
- case URIError:
1140
- return generateErrorNode(ctx, id, current);
1141
- case URL:
1142
- return createURLNode(ctx, id, current);
1143
- case URLSearchParams:
1144
- return createURLSearchParamsNode(ctx, id, current);
1145
- case Blob:
1146
- return createBlobNode(ctx, id, current);
1147
- case File:
1148
- return createFileNode(ctx, id, current);
1149
- case Headers:
1150
- return generateHeadersNode(ctx, id, current);
1151
- case FormData:
1152
- return generateFormDataNode(ctx, id, current);
1153
- default:
1154
- break;
1155
- }
1156
- if (current instanceof AggregateError) {
1157
- if (ctx.features & 1 /* AggregateError */) {
1158
- return generateAggregateErrorNode(ctx, id, current);
1159
- }
1160
- return generateErrorNode(ctx, id, current);
1161
- }
1162
- if (current instanceof Error) {
1163
- return generateErrorNode(ctx, id, current);
1164
- }
1165
- if (current instanceof Promise) {
1166
- return generatePromiseNode(ctx, id, current);
1167
- }
1168
- if (Symbol.iterator in current) {
1169
- return generateIterableNode(ctx, id, current);
1170
- }
1171
- if ("then" in current && typeof current.then === "function") {
1172
- return generatePromiseNode(ctx, id, current);
1173
- }
1174
- throw new Error("Unsupported type");
1175
- }
1204
+ case "object":
1205
+ return parseObject(ctx, current);
1176
1206
  case "symbol":
1177
- if (hasReferenceID(current)) {
1178
- const id = createIndexedValue(ctx, current);
1179
- if (ctx.markedRefs.has(id)) {
1180
- return createIndexedValueNode(id);
1181
- }
1182
- return createReferenceNode(id, current);
1183
- }
1184
- return createWKSymbolNode(ctx, current);
1185
- case "function": {
1186
- assert(hasReferenceID(current), "Cannot serialize function without reference ID.");
1187
- const id = createIndexedValue(ctx, current);
1188
- if (ctx.markedRefs.has(id)) {
1189
- return createIndexedValueNode(id);
1190
- }
1191
- return createReferenceNode(id, current);
1192
- }
1207
+ return createSymbolNode(ctx, current);
1208
+ case "function":
1209
+ return createFunctionNode(ctx, current);
1193
1210
  default:
1194
- throw new Error("Unsupported type");
1211
+ throw new UnsupportedTypeError(current);
1195
1212
  }
1196
1213
  }
1197
- async function parseAsync(ctx, current) {
1198
- const result = await parse(ctx, current);
1199
- const isObject = result.t === 16 /* Object */ || result.t === 21 /* Iterable */;
1200
- return [result, getRootID(ctx, current), isObject];
1201
- }
1202
1214
 
1203
- // src/tree/deserialize.ts
1204
- function assignIndexedValue(ctx, index, value) {
1205
- if (ctx.markedRefs.has(index)) {
1206
- ctx.valueMap.set(index, value);
1207
- }
1208
- return value;
1209
- }
1210
- function deserializeArray(ctx, node) {
1211
- const result = assignIndexedValue(
1212
- ctx,
1213
- node.i,
1214
- new Array(node.l)
1215
- );
1215
+ // src/tree/async.ts
1216
+ async function generateNodeList2(ctx, current) {
1217
+ const size = current.length;
1218
+ const nodes = new Array(size);
1219
+ const deferred = new Array(size);
1216
1220
  let item;
1217
- for (let i = 0, len = node.l; i < len; i++) {
1218
- item = node.a[i];
1219
- if (item) {
1220
- result[i] = deserializeTree(ctx, item);
1221
+ for (let i = 0; i < size; i++) {
1222
+ if (i in current) {
1223
+ item = current[i];
1224
+ if (isIterable(item)) {
1225
+ deferred[i] = item;
1226
+ } else {
1227
+ nodes[i] = await parseAsync(ctx, item);
1228
+ }
1221
1229
  }
1222
1230
  }
1223
- return result;
1224
- }
1225
- function deserializeProperties(ctx, node, result) {
1226
- if (node.s === 0) {
1227
- return {};
1228
- }
1229
- for (let i = 0; i < node.s; i++) {
1230
- result[deserializeString(node.k[i])] = deserializeTree(ctx, node.v[i]);
1231
+ for (let i = 0; i < size; i++) {
1232
+ if (i in deferred) {
1233
+ nodes[i] = await parseAsync(ctx, deferred[i]);
1234
+ }
1231
1235
  }
1232
- return result;
1233
- }
1234
- function deserializeNullConstructor(ctx, node) {
1235
- const result = assignIndexedValue(
1236
- ctx,
1237
- node.i,
1238
- /* @__PURE__ */ Object.create(null)
1239
- );
1240
- deserializeProperties(ctx, node.d, result);
1241
- return result;
1236
+ return nodes;
1242
1237
  }
1243
- function deserializeObject(ctx, node) {
1244
- const result = assignIndexedValue(ctx, node.i, {});
1245
- deserializeProperties(ctx, node.d, result);
1246
- return result;
1238
+ async function generateArrayNode2(ctx, id, current) {
1239
+ return {
1240
+ t: 9 /* Array */,
1241
+ i: id,
1242
+ s: void 0,
1243
+ l: current.length,
1244
+ c: void 0,
1245
+ m: void 0,
1246
+ d: void 0,
1247
+ a: await generateNodeList2(ctx, current),
1248
+ f: void 0,
1249
+ b: void 0,
1250
+ o: getObjectFlag(current)
1251
+ };
1252
+ }
1253
+ async function generateMapNode2(ctx, id, current) {
1254
+ assert(ctx.features & 32 /* Map */, new UnsupportedTypeError(current));
1255
+ const len = current.size;
1256
+ const keyNodes = new Array(len);
1257
+ const valueNodes = new Array(len);
1258
+ const deferredKey = new Array(len);
1259
+ const deferredValue = new Array(len);
1260
+ let deferredSize = 0;
1261
+ let nodeSize = 0;
1262
+ for (const [key, value] of current.entries()) {
1263
+ if (isIterable(key) || isIterable(value)) {
1264
+ deferredKey[deferredSize] = key;
1265
+ deferredValue[deferredSize] = value;
1266
+ deferredSize++;
1267
+ } else {
1268
+ keyNodes[nodeSize] = await parseAsync(ctx, key);
1269
+ valueNodes[nodeSize] = await parseAsync(ctx, value);
1270
+ nodeSize++;
1271
+ }
1272
+ }
1273
+ for (let i = 0; i < deferredSize; i++) {
1274
+ keyNodes[nodeSize + i] = await parseAsync(ctx, deferredKey[i]);
1275
+ valueNodes[nodeSize + i] = await parseAsync(ctx, deferredValue[i]);
1276
+ }
1277
+ return {
1278
+ t: 8 /* Map */,
1279
+ i: id,
1280
+ s: void 0,
1281
+ l: void 0,
1282
+ c: void 0,
1283
+ m: void 0,
1284
+ d: { k: keyNodes, v: valueNodes, s: len },
1285
+ a: void 0,
1286
+ f: void 0,
1287
+ b: void 0,
1288
+ o: void 0
1289
+ };
1290
+ }
1291
+ async function generateSetNode2(ctx, id, current) {
1292
+ assert(ctx.features & 512 /* Set */, new UnsupportedTypeError(current));
1293
+ const len = current.size;
1294
+ const nodes = new Array(len);
1295
+ const deferred = new Array(len);
1296
+ let deferredSize = 0;
1297
+ let nodeSize = 0;
1298
+ for (const item of current.keys()) {
1299
+ if (isIterable(item)) {
1300
+ deferred[deferredSize++] = item;
1301
+ } else {
1302
+ nodes[nodeSize++] = await parseAsync(ctx, item);
1303
+ }
1304
+ }
1305
+ for (let i = 0; i < deferredSize; i++) {
1306
+ nodes[nodeSize + i] = await parseAsync(ctx, deferred[i]);
1307
+ }
1308
+ return {
1309
+ t: 7 /* Set */,
1310
+ i: id,
1311
+ s: void 0,
1312
+ l: len,
1313
+ c: void 0,
1314
+ m: void 0,
1315
+ d: void 0,
1316
+ a: nodes,
1317
+ f: void 0,
1318
+ b: void 0,
1319
+ o: void 0
1320
+ };
1321
+ }
1322
+ async function generateProperties2(ctx, properties) {
1323
+ const keys = Object.keys(properties);
1324
+ let size = keys.length;
1325
+ const keyNodes = new Array(size);
1326
+ const valueNodes = new Array(size);
1327
+ const deferredKeys = new Array(size);
1328
+ const deferredValues = new Array(size);
1329
+ let deferredSize = 0;
1330
+ let nodesSize = 0;
1331
+ let item;
1332
+ let escaped;
1333
+ for (const key of keys) {
1334
+ item = properties[key];
1335
+ escaped = serializeString(key);
1336
+ if (isIterable(item)) {
1337
+ deferredKeys[deferredSize] = escaped;
1338
+ deferredValues[deferredSize] = item;
1339
+ deferredSize++;
1340
+ } else {
1341
+ keyNodes[nodesSize] = escaped;
1342
+ valueNodes[nodesSize] = await parseAsync(ctx, item);
1343
+ nodesSize++;
1344
+ }
1345
+ }
1346
+ for (let i = 0; i < deferredSize; i++) {
1347
+ keyNodes[nodesSize + i] = deferredKeys[i];
1348
+ valueNodes[nodesSize + i] = await parseAsync(ctx, deferredValues[i]);
1349
+ }
1350
+ if (ctx.features & 1024 /* Symbol */) {
1351
+ if (Symbol.iterator in properties) {
1352
+ keyNodes[size] = 0 /* SymbolIterator */;
1353
+ const items = Array.from(properties);
1354
+ valueNodes[size] = await generateArrayNode2(
1355
+ ctx,
1356
+ createIndexedValue(ctx, items),
1357
+ items
1358
+ );
1359
+ size++;
1360
+ }
1361
+ }
1362
+ return {
1363
+ k: keyNodes,
1364
+ v: valueNodes,
1365
+ s: size
1366
+ };
1367
+ }
1368
+ async function generatePlainProperties2(ctx, properties) {
1369
+ const keys = Object.keys(properties);
1370
+ const size = keys.length;
1371
+ const keyNodes = new Array(size);
1372
+ const valueNodes = new Array(size);
1373
+ const deferredKeys = new Array(size);
1374
+ const deferredValues = new Array(size);
1375
+ let deferredSize = 0;
1376
+ let nodesSize = 0;
1377
+ let item;
1378
+ let escaped;
1379
+ for (const key of keys) {
1380
+ item = properties[key];
1381
+ escaped = serializeString(key);
1382
+ if (isIterable(item)) {
1383
+ deferredKeys[deferredSize] = escaped;
1384
+ deferredValues[deferredSize] = item;
1385
+ deferredSize++;
1386
+ } else {
1387
+ keyNodes[nodesSize] = escaped;
1388
+ valueNodes[nodesSize] = await parseAsync(ctx, item);
1389
+ nodesSize++;
1390
+ }
1391
+ }
1392
+ for (let i = 0; i < deferredSize; i++) {
1393
+ keyNodes[nodesSize + i] = deferredKeys[i];
1394
+ valueNodes[nodesSize + i] = await parseAsync(ctx, deferredValues[i]);
1395
+ }
1396
+ return {
1397
+ k: keyNodes,
1398
+ v: valueNodes,
1399
+ s: size
1400
+ };
1401
+ }
1402
+ async function generatePromiseNode(ctx, id, current) {
1403
+ assert(ctx.features & 256 /* Promise */, new UnsupportedTypeError(current));
1404
+ return {
1405
+ t: 12 /* Promise */,
1406
+ i: id,
1407
+ s: void 0,
1408
+ l: void 0,
1409
+ c: void 0,
1410
+ m: void 0,
1411
+ // Parse options first before the items
1412
+ d: void 0,
1413
+ a: void 0,
1414
+ f: await parseAsync(ctx, await current),
1415
+ b: void 0,
1416
+ o: void 0
1417
+ };
1418
+ }
1419
+ async function generateObjectNode2(ctx, id, current, empty) {
1420
+ return {
1421
+ t: empty ? 11 /* NullConstructor */ : 10 /* Object */,
1422
+ i: id,
1423
+ s: void 0,
1424
+ l: void 0,
1425
+ c: void 0,
1426
+ m: void 0,
1427
+ d: await generateProperties2(ctx, current),
1428
+ a: void 0,
1429
+ f: void 0,
1430
+ b: void 0,
1431
+ o: getObjectFlag(current)
1432
+ };
1433
+ }
1434
+ async function generateAggregateErrorNode2(ctx, id, current) {
1435
+ const options = getErrorOptions(ctx, current);
1436
+ const optionsNode = options ? await generateProperties2(ctx, options) : void 0;
1437
+ return {
1438
+ t: 14 /* AggregateError */,
1439
+ i: id,
1440
+ s: void 0,
1441
+ l: void 0,
1442
+ c: void 0,
1443
+ m: serializeString(current.message),
1444
+ d: optionsNode,
1445
+ a: void 0,
1446
+ f: void 0,
1447
+ b: void 0,
1448
+ o: void 0
1449
+ };
1450
+ }
1451
+ async function generateErrorNode2(ctx, id, current) {
1452
+ const options = getErrorOptions(ctx, current);
1453
+ const optionsNode = options ? await generateProperties2(ctx, options) : void 0;
1454
+ return {
1455
+ t: 13 /* Error */,
1456
+ i: id,
1457
+ s: void 0,
1458
+ l: void 0,
1459
+ c: getErrorConstructorName(current),
1460
+ m: serializeString(current.message),
1461
+ d: optionsNode,
1462
+ a: void 0,
1463
+ f: void 0,
1464
+ b: void 0,
1465
+ o: void 0
1466
+ };
1467
+ }
1468
+ async function generateHeadersNode2(ctx, id, current) {
1469
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
1470
+ const items = {};
1471
+ current.forEach((value, key) => {
1472
+ items[key] = value;
1473
+ });
1474
+ return {
1475
+ t: 25 /* Headers */,
1476
+ i: id,
1477
+ s: void 0,
1478
+ l: void 0,
1479
+ c: void 0,
1480
+ m: void 0,
1481
+ d: await generatePlainProperties2(ctx, items),
1482
+ a: void 0,
1483
+ f: void 0,
1484
+ b: void 0,
1485
+ o: void 0
1486
+ };
1487
+ }
1488
+ async function generateFormDataNode2(ctx, id, current) {
1489
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
1490
+ const items = {};
1491
+ current.forEach((value, key) => {
1492
+ items[key] = value;
1493
+ });
1494
+ return {
1495
+ t: 26 /* FormData */,
1496
+ i: id,
1497
+ s: void 0,
1498
+ l: void 0,
1499
+ c: void 0,
1500
+ m: void 0,
1501
+ d: await generatePlainProperties2(ctx, items),
1502
+ a: void 0,
1503
+ f: void 0,
1504
+ b: void 0,
1505
+ o: void 0
1506
+ };
1507
+ }
1508
+ async function generateBoxedNode2(ctx, id, current) {
1509
+ return {
1510
+ t: 27 /* Boxed */,
1511
+ i: id,
1512
+ s: void 0,
1513
+ l: void 0,
1514
+ c: void 0,
1515
+ m: void 0,
1516
+ d: void 0,
1517
+ a: void 0,
1518
+ f: await parseAsync(ctx, current.valueOf()),
1519
+ b: void 0,
1520
+ o: void 0
1521
+ };
1522
+ }
1523
+ async function parseObject2(ctx, current) {
1524
+ if (!current) {
1525
+ return NULL_NODE;
1526
+ }
1527
+ const id = createIndexedValue(ctx, current);
1528
+ if (ctx.markedRefs.has(id)) {
1529
+ return createIndexedValueNode(id);
1530
+ }
1531
+ if (hasReferenceID(current)) {
1532
+ return createReferenceNode(id, current);
1533
+ }
1534
+ if (Array.isArray(current)) {
1535
+ return generateArrayNode2(ctx, id, current);
1536
+ }
1537
+ switch (current.constructor) {
1538
+ case Number:
1539
+ case Boolean:
1540
+ case String:
1541
+ case BigInt:
1542
+ case Function:
1543
+ case Symbol:
1544
+ return generateBoxedNode2(ctx, id, current);
1545
+ case Date:
1546
+ return createDateNode(id, current);
1547
+ case RegExp:
1548
+ return createRegExpNode(id, current);
1549
+ case Promise:
1550
+ return generatePromiseNode(ctx, id, current);
1551
+ case ArrayBuffer:
1552
+ return createArrayBufferNode(id, current);
1553
+ case Int8Array:
1554
+ case Int16Array:
1555
+ case Int32Array:
1556
+ case Uint8Array:
1557
+ case Uint16Array:
1558
+ case Uint32Array:
1559
+ case Uint8ClampedArray:
1560
+ case Float32Array:
1561
+ case Float64Array:
1562
+ return createTypedArrayNode(ctx, id, current);
1563
+ case BigInt64Array:
1564
+ case BigUint64Array:
1565
+ return createBigIntTypedArrayNode(ctx, id, current);
1566
+ case DataView:
1567
+ return createDataViewNode(ctx, id, current);
1568
+ case Map:
1569
+ return generateMapNode2(
1570
+ ctx,
1571
+ id,
1572
+ current
1573
+ );
1574
+ case Set:
1575
+ return generateSetNode2(
1576
+ ctx,
1577
+ id,
1578
+ current
1579
+ );
1580
+ case Object:
1581
+ return generateObjectNode2(
1582
+ ctx,
1583
+ id,
1584
+ current,
1585
+ false
1586
+ );
1587
+ case void 0:
1588
+ return generateObjectNode2(
1589
+ ctx,
1590
+ id,
1591
+ current,
1592
+ true
1593
+ );
1594
+ case AggregateError:
1595
+ if (ctx.features & 1 /* AggregateError */) {
1596
+ return generateAggregateErrorNode2(ctx, id, current);
1597
+ }
1598
+ return generateErrorNode2(ctx, id, current);
1599
+ case Error:
1600
+ case EvalError:
1601
+ case RangeError:
1602
+ case ReferenceError:
1603
+ case SyntaxError:
1604
+ case TypeError:
1605
+ case URIError:
1606
+ return generateErrorNode2(ctx, id, current);
1607
+ case URL:
1608
+ return createURLNode(ctx, id, current);
1609
+ case URLSearchParams:
1610
+ return createURLSearchParamsNode(ctx, id, current);
1611
+ case Blob:
1612
+ return createBlobNode(ctx, id, current);
1613
+ case File:
1614
+ return createFileNode(ctx, id, current);
1615
+ case Headers:
1616
+ return generateHeadersNode2(ctx, id, current);
1617
+ case FormData:
1618
+ return generateFormDataNode2(ctx, id, current);
1619
+ default:
1620
+ break;
1621
+ }
1622
+ if (current instanceof AggregateError) {
1623
+ if (ctx.features & 1 /* AggregateError */) {
1624
+ return generateAggregateErrorNode2(ctx, id, current);
1625
+ }
1626
+ return generateErrorNode2(ctx, id, current);
1627
+ }
1628
+ if (current instanceof Error) {
1629
+ return generateErrorNode2(ctx, id, current);
1630
+ }
1631
+ if (current instanceof Promise) {
1632
+ return generatePromiseNode(ctx, id, current);
1633
+ }
1634
+ if (Symbol.iterator in current) {
1635
+ return generateObjectNode2(ctx, id, current, !!current.constructor);
1636
+ }
1637
+ throw new UnsupportedTypeError(current);
1638
+ }
1639
+ async function parseAsync(ctx, current) {
1640
+ switch (typeof current) {
1641
+ case "boolean":
1642
+ return current ? TRUE_NODE : FALSE_NODE;
1643
+ case "undefined":
1644
+ return UNDEFINED_NODE;
1645
+ case "string":
1646
+ return createStringNode(current);
1647
+ case "number":
1648
+ return createNumberNode(current);
1649
+ case "bigint":
1650
+ return createBigIntNode(ctx, current);
1651
+ case "object":
1652
+ return parseObject2(ctx, current);
1653
+ case "symbol":
1654
+ return createSymbolNode(ctx, current);
1655
+ case "function":
1656
+ return createFunctionNode(ctx, current);
1657
+ default:
1658
+ throw new UnsupportedTypeError(current);
1659
+ }
1660
+ }
1661
+
1662
+ // src/tree/deserialize.ts
1663
+ function applyObjectFlag(obj, flag) {
1664
+ switch (flag) {
1665
+ case 3 /* Frozen */:
1666
+ return Object.freeze(obj);
1667
+ case 1 /* NonExtensible */:
1668
+ return Object.preventExtensions(obj);
1669
+ case 2 /* Sealed */:
1670
+ return Object.seal(obj);
1671
+ default:
1672
+ return obj;
1673
+ }
1674
+ }
1675
+ function assignIndexedValue(ctx, index, value) {
1676
+ if (ctx.markedRefs.has(index)) {
1677
+ ctx.valueMap.set(index, value);
1678
+ }
1679
+ return value;
1680
+ }
1681
+ function deserializeArray(ctx, node) {
1682
+ const len = node.l;
1683
+ const result = assignIndexedValue(
1684
+ ctx,
1685
+ node.i,
1686
+ new Array(len)
1687
+ );
1688
+ let item;
1689
+ for (let i = 0; i < len; i++) {
1690
+ item = node.a[i];
1691
+ if (item) {
1692
+ result[i] = deserializeTree(ctx, item);
1693
+ }
1694
+ }
1695
+ applyObjectFlag(result, node.o);
1696
+ return result;
1697
+ }
1698
+ function deserializeProperties(ctx, node, result) {
1699
+ const len = node.s;
1700
+ if (len) {
1701
+ let key;
1702
+ let value;
1703
+ const keys = node.k;
1704
+ const vals = node.v;
1705
+ for (let i = 0; i < len; i++) {
1706
+ key = keys[i];
1707
+ value = deserializeTree(ctx, vals[i]);
1708
+ switch (key) {
1709
+ case 0 /* SymbolIterator */:
1710
+ {
1711
+ const current = value;
1712
+ result[Symbol.iterator] = () => current.values();
1713
+ }
1714
+ break;
1715
+ default:
1716
+ result[deserializeString(key)] = value;
1717
+ break;
1718
+ }
1719
+ }
1720
+ }
1721
+ return result;
1722
+ }
1723
+ function deserializeObject(ctx, node) {
1724
+ const result = assignIndexedValue(
1725
+ ctx,
1726
+ node.i,
1727
+ node.t === 10 /* Object */ ? {} : /* @__PURE__ */ Object.create(null)
1728
+ );
1729
+ deserializeProperties(ctx, node.d, result);
1730
+ applyObjectFlag(result, node.o);
1731
+ return result;
1247
1732
  }
1248
1733
  function deserializeSet(ctx, node) {
1249
1734
  const result = assignIndexedValue(ctx, node.i, /* @__PURE__ */ new Set());
1735
+ const items = node.a;
1250
1736
  for (let i = 0, len = node.l; i < len; i++) {
1251
- result.add(deserializeTree(ctx, node.a[i]));
1737
+ result.add(deserializeTree(ctx, items[i]));
1252
1738
  }
1253
1739
  return result;
1254
1740
  }
@@ -1258,10 +1744,12 @@ function deserializeMap(ctx, node) {
1258
1744
  node.i,
1259
1745
  /* @__PURE__ */ new Map()
1260
1746
  );
1747
+ const keys = node.d.k;
1748
+ const vals = node.d.v;
1261
1749
  for (let i = 0, len = node.d.s; i < len; i++) {
1262
1750
  result.set(
1263
- deserializeTree(ctx, node.d.k[i]),
1264
- deserializeTree(ctx, node.d.v[i])
1751
+ deserializeTree(ctx, keys[i]),
1752
+ deserializeTree(ctx, vals[i])
1265
1753
  );
1266
1754
  }
1267
1755
  return result;
@@ -1297,7 +1785,7 @@ function createDeferred() {
1297
1785
  })
1298
1786
  };
1299
1787
  }
1300
- function deserializePromise(ctx, node) {
1788
+ async function deserializePromise(ctx, node) {
1301
1789
  const deferred = createDeferred();
1302
1790
  const result = assignIndexedValue(ctx, node.i, deferred.promise);
1303
1791
  deferred.resolve(deserializeTree(ctx, node.f));
@@ -1318,20 +1806,6 @@ function deserializeTypedArray(ctx, node) {
1318
1806
  ));
1319
1807
  return result;
1320
1808
  }
1321
- function deserializeIterable(ctx, node) {
1322
- const values = [];
1323
- let item;
1324
- for (let i = 0, len = node.l; i < len; i++) {
1325
- item = node.a[i];
1326
- if (item) {
1327
- values[i] = deserializeTree(ctx, item);
1328
- }
1329
- }
1330
- const result = assignIndexedValue(ctx, node.i, {
1331
- [Symbol.iterator]: () => values.values()
1332
- });
1333
- return deserializeDictionary(ctx, node, result);
1334
- }
1335
1809
  function deserializeDate(ctx, node) {
1336
1810
  return assignIndexedValue(ctx, node.i, new Date(node.s));
1337
1811
  }
@@ -1375,94 +1849,93 @@ function deserializeFile(ctx, node) {
1375
1849
  }
1376
1850
  function deserializeHeaders(ctx, node) {
1377
1851
  const result = assignIndexedValue(ctx, node.i, new Headers());
1852
+ const keys = node.d.k;
1853
+ const vals = node.d.v;
1378
1854
  for (let i = 0, len = node.d.s; i < len; i++) {
1379
1855
  result.set(
1380
- deserializeString(node.d.k[i]),
1381
- deserializeTree(ctx, node.d.v[i])
1856
+ deserializeString(keys[i]),
1857
+ deserializeTree(ctx, vals[i])
1382
1858
  );
1383
1859
  }
1384
1860
  return result;
1385
1861
  }
1386
1862
  function deserializeFormData(ctx, node) {
1387
1863
  const result = assignIndexedValue(ctx, node.i, new FormData());
1864
+ const keys = node.d.k;
1865
+ const vals = node.d.v;
1388
1866
  for (let i = 0, len = node.d.s; i < len; i++) {
1389
1867
  result.set(
1390
- deserializeString(node.d.k[i]),
1391
- deserializeTree(ctx, node.d.v[i])
1868
+ deserializeString(keys[i]),
1869
+ deserializeTree(ctx, vals[i])
1392
1870
  );
1393
1871
  }
1394
1872
  return result;
1395
1873
  }
1874
+ function deserializeBoxed(ctx, node) {
1875
+ return assignIndexedValue(
1876
+ ctx,
1877
+ node.i,
1878
+ Object(deserializeTree(ctx, node.f))
1879
+ );
1880
+ }
1396
1881
  function deserializeTree(ctx, node) {
1397
1882
  switch (node.t) {
1883
+ case 2 /* Constant */:
1884
+ return deserializeConstant(node);
1398
1885
  case 0 /* Number */:
1399
- case 2 /* Boolean */:
1400
1886
  return node.s;
1401
1887
  case 1 /* String */:
1402
1888
  return deserializeString(node.s);
1403
- case 4 /* Undefined */:
1404
- return void 0;
1405
- case 3 /* Null */:
1406
- return null;
1407
- case 5 /* NegativeZero */:
1408
- return -0;
1409
- case 6 /* Infinity */:
1410
- return Infinity;
1411
- case 7 /* NegativeInfinity */:
1412
- return -Infinity;
1413
- case 8 /* NaN */:
1414
- return NaN;
1415
- case 9 /* BigInt */:
1889
+ case 3 /* BigInt */:
1416
1890
  return BigInt(node.s);
1417
- case 10 /* IndexedValue */:
1891
+ case 4 /* IndexedValue */:
1418
1892
  return ctx.valueMap.get(node.i);
1419
- case 15 /* Array */:
1893
+ case 9 /* Array */:
1420
1894
  return deserializeArray(ctx, node);
1421
- case 16 /* Object */:
1895
+ case 10 /* Object */:
1896
+ case 11 /* NullConstructor */:
1422
1897
  return deserializeObject(ctx, node);
1423
- case 17 /* NullConstructor */:
1424
- return deserializeNullConstructor(ctx, node);
1425
- case 11 /* Date */:
1898
+ case 5 /* Date */:
1426
1899
  return deserializeDate(ctx, node);
1427
- case 12 /* RegExp */:
1900
+ case 6 /* RegExp */:
1428
1901
  return deserializeRegExp(ctx, node);
1429
- case 13 /* Set */:
1902
+ case 7 /* Set */:
1430
1903
  return deserializeSet(ctx, node);
1431
- case 14 /* Map */:
1904
+ case 8 /* Map */:
1432
1905
  return deserializeMap(ctx, node);
1433
- case 28 /* ArrayBuffer */:
1906
+ case 21 /* ArrayBuffer */:
1434
1907
  return deserializeArrayBuffer(ctx, node);
1435
- case 23 /* BigIntTypedArray */:
1436
- case 22 /* TypedArray */:
1908
+ case 16 /* BigIntTypedArray */:
1909
+ case 15 /* TypedArray */:
1437
1910
  return deserializeTypedArray(ctx, node);
1438
- case 29 /* DataView */:
1911
+ case 22 /* DataView */:
1439
1912
  return deserializeDataView(ctx, node);
1440
- case 20 /* AggregateError */:
1913
+ case 14 /* AggregateError */:
1441
1914
  return deserializeAggregateError(ctx, node);
1442
- case 19 /* Error */:
1915
+ case 13 /* Error */:
1443
1916
  return deserializeError(ctx, node);
1444
- case 21 /* Iterable */:
1445
- return deserializeIterable(ctx, node);
1446
- case 18 /* Promise */:
1917
+ case 12 /* Promise */:
1447
1918
  return deserializePromise(ctx, node);
1448
- case 24 /* WKSymbol */:
1919
+ case 17 /* WKSymbol */:
1449
1920
  return SYMBOL_REF[node.s];
1450
- case 25 /* URL */:
1921
+ case 18 /* URL */:
1451
1922
  return deserializeURL(ctx, node);
1452
- case 26 /* URLSearchParams */:
1923
+ case 19 /* URLSearchParams */:
1453
1924
  return deserializeURLSearchParams(ctx, node);
1454
- case 27 /* Reference */:
1925
+ case 20 /* Reference */:
1455
1926
  return deserializeReference(ctx, node);
1456
- case 30 /* Blob */:
1927
+ case 23 /* Blob */:
1457
1928
  return deserializeBlob(ctx, node);
1458
- case 31 /* File */:
1929
+ case 24 /* File */:
1459
1930
  return deserializeFile(ctx, node);
1460
- case 32 /* Headers */:
1931
+ case 25 /* Headers */:
1461
1932
  return deserializeHeaders(ctx, node);
1462
- case 33 /* FormData */:
1933
+ case 26 /* FormData */:
1463
1934
  return deserializeFormData(ctx, node);
1935
+ case 27 /* Boxed */:
1936
+ return deserializeBoxed(ctx, node);
1464
1937
  default:
1465
- throw new Error("Unsupported type");
1938
+ throw new Error("invariant");
1466
1939
  }
1467
1940
  }
1468
1941
 
@@ -1481,6 +1954,12 @@ function getAssignmentExpression(assignment) {
1481
1954
  return "";
1482
1955
  }
1483
1956
  }
1957
+ var OBJECT_FLAG_CONSTRUCTOR = {
1958
+ [3 /* Frozen */]: "Object.freeze",
1959
+ [2 /* Sealed */]: "Object.seal",
1960
+ [1 /* NonExtensible */]: "Object.preventExtensions",
1961
+ [0 /* None */]: void 0
1962
+ };
1484
1963
  function mergeAssignments(assignments) {
1485
1964
  const newAssignments = [];
1486
1965
  let current = assignments[0];
@@ -1556,8 +2035,33 @@ function resolveAssignments(assignments) {
1556
2035
  }
1557
2036
  return void 0;
1558
2037
  }
2038
+ function pushObjectFlag(ctx, flag, id) {
2039
+ if (flag !== 0 /* None */) {
2040
+ markRef(ctx, id);
2041
+ ctx.flags.push({
2042
+ type: flag,
2043
+ value: getRefParam(ctx, id)
2044
+ });
2045
+ }
2046
+ }
2047
+ function resolveFlags(ctx) {
2048
+ let result = "";
2049
+ for (let i = 0, len = ctx.flags.length; i < len; i++) {
2050
+ const flag = ctx.flags[i];
2051
+ result += OBJECT_FLAG_CONSTRUCTOR[flag.type] + "(" + flag.value + "),";
2052
+ }
2053
+ return result;
2054
+ }
1559
2055
  function resolvePatches(ctx) {
1560
- return resolveAssignments(ctx.assignments);
2056
+ const assignments = resolveAssignments(ctx.assignments);
2057
+ const flags = resolveFlags(ctx);
2058
+ if (assignments) {
2059
+ if (flags) {
2060
+ return assignments + flags;
2061
+ }
2062
+ return assignments;
2063
+ }
2064
+ return flags;
1561
2065
  }
1562
2066
  function createAssignment(ctx, source, value) {
1563
2067
  ctx.assignments.push({
@@ -1605,22 +2109,24 @@ function assignIndexedValue2(ctx, index, value) {
1605
2109
  return value;
1606
2110
  }
1607
2111
  function isIndexedValueInStack(ctx, node) {
1608
- return node.t === 10 /* IndexedValue */ && ctx.stack.includes(node.i);
2112
+ return node.t === 4 /* IndexedValue */ && ctx.stack.includes(node.i);
1609
2113
  }
1610
- function serializeNodeList(ctx, node) {
1611
- const size = node.l;
2114
+ function serializeArray(ctx, node) {
2115
+ const id = node.i;
2116
+ ctx.stack.push(id);
1612
2117
  let values = "";
1613
2118
  let item;
1614
2119
  let isHoley = false;
1615
- for (let i = 0; i < size; i++) {
2120
+ const list = node.a;
2121
+ for (let i = 0, len = node.l; i < len; i++) {
1616
2122
  if (i !== 0) {
1617
2123
  values += ",";
1618
2124
  }
1619
- item = node.a[i];
2125
+ item = list[i];
1620
2126
  if (item) {
1621
2127
  if (isIndexedValueInStack(ctx, item)) {
1622
- markRef(ctx, node.i);
1623
- createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
2128
+ markRef(ctx, id);
2129
+ createArrayAssign(ctx, id, i, getRefParam(ctx, item.i));
1624
2130
  isHoley = true;
1625
2131
  } else {
1626
2132
  values += serializeTree(ctx, item);
@@ -1630,16 +2136,30 @@ function serializeNodeList(ctx, node) {
1630
2136
  isHoley = true;
1631
2137
  }
1632
2138
  }
1633
- return "[" + values + (isHoley ? ",]" : "]");
1634
- }
1635
- function serializeArray(ctx, node) {
1636
- ctx.stack.push(node.i);
1637
- const result = serializeNodeList(ctx, node);
1638
2139
  ctx.stack.pop();
1639
- return assignIndexedValue2(ctx, node.i, result);
2140
+ pushObjectFlag(ctx, node.o, node.i);
2141
+ return assignIndexedValue2(ctx, id, "[" + values + (isHoley ? ",]" : "]"));
2142
+ }
2143
+ function getIterableAccess(ctx) {
2144
+ return ctx.features & 2 /* ArrayPrototypeValues */ ? ".values()" : "[Symbol.iterator]()";
2145
+ }
2146
+ function serializeIterable(ctx, node) {
2147
+ const parent = ctx.stack;
2148
+ ctx.stack = [];
2149
+ let serialized = serializeTree(ctx, node) + getIterableAccess(ctx);
2150
+ ctx.stack = parent;
2151
+ if (ctx.features & 4 /* ArrowFunction */) {
2152
+ serialized = "[Symbol.iterator]:()=>" + serialized;
2153
+ } else if (ctx.features & 64 /* MethodShorthand */) {
2154
+ serialized = "[Symbol.iterator](){return " + serialized + "}";
2155
+ } else {
2156
+ serialized = "[Symbol.iterator]:function(){return " + serialized + "}";
2157
+ }
2158
+ return serialized;
1640
2159
  }
1641
2160
  function serializeProperties(ctx, sourceID, node) {
1642
- if (node.s === 0) {
2161
+ const len = node.s;
2162
+ if (len === 0) {
1643
2163
  return "{}";
1644
2164
  }
1645
2165
  let result = "";
@@ -1652,23 +2172,30 @@ function serializeProperties(ctx, sourceID, node) {
1652
2172
  let hasPrev = false;
1653
2173
  const keys = node.k;
1654
2174
  const values = node.v;
1655
- for (let i = 0, len = node.s; i < len; i++) {
2175
+ for (let i = 0; i < len; i++) {
1656
2176
  key = keys[i];
1657
2177
  val = values[i];
1658
- check = Number(key);
1659
- isIdentifier = check >= 0 || isValidIdentifier(key);
1660
- if (isIndexedValueInStack(ctx, val)) {
1661
- refParam = getRefParam(ctx, val.i);
1662
- if (isIdentifier && Number.isNaN(check)) {
1663
- markRef(ctx, sourceID);
1664
- createObjectAssign(ctx, sourceID, key, refParam);
1665
- } else {
1666
- markRef(ctx, sourceID);
1667
- createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', refParam);
1668
- }
1669
- } else {
1670
- result += (hasPrev ? "," : "") + (isIdentifier ? key : '"' + key + '"') + ":" + serializeTree(ctx, val);
1671
- hasPrev = true;
2178
+ switch (key) {
2179
+ case 0 /* SymbolIterator */:
2180
+ result += (hasPrev ? "," : "") + serializeIterable(ctx, val);
2181
+ hasPrev = true;
2182
+ break;
2183
+ default:
2184
+ check = Number(key);
2185
+ isIdentifier = check >= 0 || isValidIdentifier(key);
2186
+ if (isIndexedValueInStack(ctx, val)) {
2187
+ refParam = getRefParam(ctx, val.i);
2188
+ markRef(ctx, sourceID);
2189
+ if (isIdentifier && Number.isNaN(check)) {
2190
+ createObjectAssign(ctx, sourceID, key, refParam);
2191
+ } else {
2192
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', refParam);
2193
+ }
2194
+ } else {
2195
+ result += (hasPrev ? "," : "") + (isIdentifier ? key : '"' + key + '"') + ":" + serializeTree(ctx, val);
2196
+ hasPrev = true;
2197
+ }
2198
+ break;
1672
2199
  }
1673
2200
  }
1674
2201
  ctx.stack.pop();
@@ -1684,30 +2211,54 @@ function serializeWithObjectAssign(ctx, value, id, serialized) {
1684
2211
  function serializeAssignments(ctx, sourceID, node) {
1685
2212
  ctx.stack.push(sourceID);
1686
2213
  const mainAssignments = [];
1687
- let parentStack;
1688
- let refParam;
1689
2214
  let key;
1690
- let check;
2215
+ let value;
1691
2216
  let parentAssignment;
1692
- let isIdentifier;
1693
2217
  const keys = node.k;
1694
2218
  const values = node.v;
1695
2219
  for (let i = 0, len = node.s; i < len; i++) {
1696
- parentStack = ctx.stack;
1697
- ctx.stack = [];
1698
- refParam = serializeTree(ctx, values[i]);
1699
- ctx.stack = parentStack;
1700
2220
  key = keys[i];
1701
- check = Number(key);
1702
- parentAssignment = ctx.assignments;
1703
- ctx.assignments = mainAssignments;
1704
- isIdentifier = check >= 0 || isValidIdentifier(key);
1705
- if (isIdentifier && Number.isNaN(check)) {
1706
- createObjectAssign(ctx, sourceID, key, refParam);
1707
- } else {
1708
- createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', refParam);
2221
+ value = values[i];
2222
+ switch (key) {
2223
+ case 0 /* SymbolIterator */:
2224
+ {
2225
+ const parent = ctx.stack;
2226
+ ctx.stack = [];
2227
+ const serialized = serializeTree(ctx, value) + getIterableAccess(ctx);
2228
+ ctx.stack = parent;
2229
+ parentAssignment = ctx.assignments;
2230
+ ctx.assignments = mainAssignments;
2231
+ createArrayAssign(
2232
+ ctx,
2233
+ sourceID,
2234
+ "Symbol.iterator",
2235
+ ctx.features & 4 /* ArrowFunction */ ? "()=>" + serialized : "function(){return " + serialized + "}"
2236
+ );
2237
+ ctx.assignments = parentAssignment;
2238
+ }
2239
+ break;
2240
+ default: {
2241
+ const serialized = serializeTree(ctx, value);
2242
+ const check = Number(key);
2243
+ const isIdentifier = check >= 0 || isValidIdentifier(key);
2244
+ if (isIndexedValueInStack(ctx, value)) {
2245
+ if (isIdentifier && Number.isNaN(check)) {
2246
+ createObjectAssign(ctx, sourceID, key, serialized);
2247
+ } else {
2248
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', serialized);
2249
+ }
2250
+ } else {
2251
+ parentAssignment = ctx.assignments;
2252
+ ctx.assignments = mainAssignments;
2253
+ if (isIdentifier && Number.isNaN(check)) {
2254
+ createObjectAssign(ctx, sourceID, key, serialized);
2255
+ } else {
2256
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', serialized);
2257
+ }
2258
+ ctx.assignments = parentAssignment;
2259
+ }
2260
+ }
1709
2261
  }
1710
- ctx.assignments = parentAssignment;
1711
2262
  }
1712
2263
  ctx.stack.pop();
1713
2264
  return resolveAssignments(mainAssignments);
@@ -1726,25 +2277,30 @@ function serializeDictionary(ctx, i, d, init) {
1726
2277
  }
1727
2278
  return assignIndexedValue2(ctx, i, init);
1728
2279
  }
2280
+ var NULL_CONSTRUCTOR = "Object.create(null)";
1729
2281
  function serializeNullConstructor(ctx, node) {
1730
- return serializeDictionary(ctx, node.i, node.d, "Object.create(null)");
2282
+ pushObjectFlag(ctx, node.o, node.i);
2283
+ return serializeDictionary(ctx, node.i, node.d, NULL_CONSTRUCTOR);
1731
2284
  }
1732
2285
  function serializeObject(ctx, node) {
2286
+ pushObjectFlag(ctx, node.o, node.i);
1733
2287
  return assignIndexedValue2(ctx, node.i, serializeProperties(ctx, node.i, node.d));
1734
2288
  }
1735
2289
  function serializeSet(ctx, node) {
1736
2290
  let serialized = "new Set";
1737
2291
  const size = node.l;
2292
+ const id = node.i;
1738
2293
  if (size) {
1739
2294
  let result = "";
1740
- ctx.stack.push(node.i);
1741
2295
  let item;
1742
2296
  let hasPrev = false;
2297
+ const items = node.a;
2298
+ ctx.stack.push(id);
1743
2299
  for (let i = 0; i < size; i++) {
1744
- item = node.a[i];
2300
+ item = items[i];
1745
2301
  if (isIndexedValueInStack(ctx, item)) {
1746
- markRef(ctx, node.i);
1747
- createAddAssignment(ctx, node.i, getRefParam(ctx, item.i));
2302
+ markRef(ctx, id);
2303
+ createAddAssignment(ctx, id, getRefParam(ctx, item.i));
1748
2304
  } else {
1749
2305
  result += (hasPrev ? "," : "") + serializeTree(ctx, item);
1750
2306
  hasPrev = true;
@@ -1755,40 +2311,44 @@ function serializeSet(ctx, node) {
1755
2311
  serialized += "([" + result + "])";
1756
2312
  }
1757
2313
  }
1758
- return assignIndexedValue2(ctx, node.i, serialized);
2314
+ return assignIndexedValue2(ctx, id, serialized);
1759
2315
  }
1760
2316
  function serializeMap(ctx, node) {
1761
2317
  let serialized = "new Map";
1762
- if (node.d.s) {
2318
+ const size = node.d.s;
2319
+ const id = node.i;
2320
+ if (size) {
1763
2321
  let result = "";
1764
- ctx.stack.push(node.i);
1765
2322
  let key;
1766
2323
  let val;
1767
2324
  let keyRef;
1768
2325
  let valueRef;
1769
2326
  let parent;
1770
2327
  let hasPrev = false;
1771
- for (let i = 0; i < node.d.s; i++) {
1772
- key = node.d.k[i];
1773
- val = node.d.v[i];
2328
+ const keys = node.d.k;
2329
+ const vals = node.d.v;
2330
+ ctx.stack.push(id);
2331
+ for (let i = 0; i < size; i++) {
2332
+ key = keys[i];
2333
+ val = vals[i];
1774
2334
  if (isIndexedValueInStack(ctx, key)) {
1775
- keyRef = getRefParam(ctx, key.i);
1776
- markRef(ctx, node.i);
2335
+ keyRef = getRefParam(ctx, id);
2336
+ markRef(ctx, id);
1777
2337
  if (isIndexedValueInStack(ctx, val)) {
1778
2338
  valueRef = getRefParam(ctx, val.i);
1779
- createSetAssignment(ctx, node.i, keyRef, valueRef);
2339
+ createSetAssignment(ctx, id, keyRef, valueRef);
1780
2340
  } else {
1781
2341
  parent = ctx.stack;
1782
2342
  ctx.stack = [];
1783
- createSetAssignment(ctx, node.i, keyRef, serializeTree(ctx, val));
2343
+ createSetAssignment(ctx, id, keyRef, serializeTree(ctx, val));
1784
2344
  ctx.stack = parent;
1785
2345
  }
1786
2346
  } else if (isIndexedValueInStack(ctx, val)) {
1787
2347
  valueRef = getRefParam(ctx, val.i);
1788
- markRef(ctx, node.i);
2348
+ markRef(ctx, id);
1789
2349
  parent = ctx.stack;
1790
2350
  ctx.stack = [];
1791
- createSetAssignment(ctx, node.i, serializeTree(ctx, key), valueRef);
2351
+ createSetAssignment(ctx, id, serializeTree(ctx, key), valueRef);
1792
2352
  ctx.stack = parent;
1793
2353
  } else {
1794
2354
  result += (hasPrev ? ",[" : "[") + serializeTree(ctx, key) + "," + serializeTree(ctx, val) + "]";
@@ -1800,69 +2360,56 @@ function serializeMap(ctx, node) {
1800
2360
  serialized += "([" + result + "])";
1801
2361
  }
1802
2362
  }
1803
- return assignIndexedValue2(ctx, node.i, serialized);
2363
+ return assignIndexedValue2(ctx, id, serialized);
1804
2364
  }
1805
2365
  function serializeAggregateError(ctx, node) {
1806
- ctx.stack.push(node.i);
2366
+ const id = node.i;
2367
+ ctx.stack.push(id);
1807
2368
  const serialized = 'new AggregateError([],"' + node.m + '")';
1808
2369
  ctx.stack.pop();
1809
- return serializeDictionary(ctx, node.i, node.d, serialized);
2370
+ return serializeDictionary(ctx, id, node.d, serialized);
1810
2371
  }
1811
2372
  function serializeError(ctx, node) {
1812
- const serialized = "new " + node.c + '("' + node.m + '")';
1813
- return serializeDictionary(ctx, node.i, node.d, serialized);
2373
+ return serializeDictionary(ctx, node.i, node.d, "new " + node.c + '("' + node.m + '")');
1814
2374
  }
1815
2375
  function serializePromise(ctx, node) {
1816
2376
  let serialized;
1817
- if (isIndexedValueInStack(ctx, node.f)) {
1818
- const ref = getRefParam(ctx, node.f.i);
2377
+ const fulfilled = node.f;
2378
+ const id = node.i;
2379
+ if (isIndexedValueInStack(ctx, fulfilled)) {
2380
+ const ref = getRefParam(ctx, fulfilled.i);
1819
2381
  if (ctx.features & 4 /* ArrowFunction */) {
1820
2382
  serialized = "Promise.resolve().then(()=>" + ref + ")";
1821
2383
  } else {
1822
2384
  serialized = "Promise.resolve().then(function(){return " + ref + "})";
1823
2385
  }
1824
2386
  } else {
1825
- ctx.stack.push(node.i);
1826
- const result = serializeTree(ctx, node.f);
2387
+ ctx.stack.push(id);
2388
+ const result = serializeTree(ctx, fulfilled);
1827
2389
  ctx.stack.pop();
1828
2390
  serialized = "Promise.resolve(" + result + ")";
1829
2391
  }
1830
- return assignIndexedValue2(ctx, node.i, serialized);
2392
+ return assignIndexedValue2(ctx, id, serialized);
1831
2393
  }
1832
2394
  function serializeArrayBuffer2(ctx, node) {
1833
2395
  let result = "new Uint8Array(";
1834
- if (node.s.length) {
2396
+ const buffer = node.s;
2397
+ const len = buffer.length;
2398
+ if (len) {
1835
2399
  result += "[";
1836
- for (let i = 0, len = node.s.length; i < len; i++) {
1837
- result += (i > 0 ? "," : "") + node.s[i];
2400
+ for (let i = 0; i < len; i++) {
2401
+ result += (i > 0 ? "," : "") + buffer[i];
1838
2402
  }
1839
2403
  result += "]";
1840
2404
  }
1841
2405
  return assignIndexedValue2(ctx, node.i, result + ").buffer");
1842
2406
  }
1843
2407
  function serializeTypedArray(ctx, node) {
1844
- const args = serializeTree(ctx, node.f) + "," + node.b + "," + node.l;
1845
- return assignIndexedValue2(ctx, node.i, "new " + node.c + "(" + args + ")");
1846
- }
1847
- function serializeIterable(ctx, node) {
1848
- const parent = ctx.stack;
1849
- ctx.stack = [];
1850
- const values = serializeNodeList(ctx, node);
1851
- ctx.stack = parent;
1852
- let serialized = values;
1853
- if (ctx.features & 2 /* ArrayPrototypeValues */) {
1854
- serialized += ".values()";
1855
- } else {
1856
- serialized += "[Symbol.iterator]()";
1857
- }
1858
- if (ctx.features & 4 /* ArrowFunction */) {
1859
- serialized = "{[Symbol.iterator]:()=>" + serialized + "}";
1860
- } else if (ctx.features & 64 /* MethodShorthand */) {
1861
- serialized = "{[Symbol.iterator](){return " + serialized + "}}";
1862
- } else {
1863
- serialized = "{[Symbol.iterator]:function(){return " + serialized + "}}";
1864
- }
1865
- return serializeDictionary(ctx, node.i, node.d, serialized);
2408
+ return assignIndexedValue2(
2409
+ ctx,
2410
+ node.i,
2411
+ "new " + node.c + "(" + serializeTree(ctx, node.f) + "," + node.b + "," + node.l + ")"
2412
+ );
1866
2413
  }
1867
2414
  function serializeDate(ctx, node) {
1868
2415
  return assignIndexedValue2(ctx, node.i, 'new Date("' + node.s + '")');
@@ -1874,518 +2421,143 @@ function serializeURL(ctx, node) {
1874
2421
  return assignIndexedValue2(ctx, node.i, 'new URL("' + node.s + '")');
1875
2422
  }
1876
2423
  function serializeURLSearchParams(ctx, node) {
1877
- return assignIndexedValue2(ctx, node.i, node.s ? 'new URLSearchParams("' + node.s + '")' : "new URLSearchParams");
2424
+ return assignIndexedValue2(
2425
+ ctx,
2426
+ node.i,
2427
+ node.s ? 'new URLSearchParams("' + node.s + '")' : "new URLSearchParams"
2428
+ );
1878
2429
  }
1879
2430
  function serializeReference(ctx, node) {
1880
2431
  return assignIndexedValue2(ctx, node.i, GLOBAL_KEY + '.get("' + node.s + '")');
1881
2432
  }
1882
2433
  function serializeDataView(ctx, node) {
1883
- const args = serializeTree(ctx, node.f) + "," + node.b + "," + node.l;
1884
- return assignIndexedValue2(ctx, node.i, "new DataView(" + args + ")");
2434
+ return assignIndexedValue2(
2435
+ ctx,
2436
+ node.i,
2437
+ "new DataView(" + serializeTree(ctx, node.f) + "," + node.b + "," + node.l + ")"
2438
+ );
1885
2439
  }
1886
2440
  function serializeBlob(ctx, node) {
1887
- const args = "[" + serializeTree(ctx, node.f) + '],{type:"' + node.c + '"}';
1888
- return assignIndexedValue2(ctx, node.i, "new Blob(" + args + ")");
2441
+ return assignIndexedValue2(
2442
+ ctx,
2443
+ node.i,
2444
+ "new Blob([" + serializeTree(ctx, node.f) + '],{type:"' + node.c + '"})'
2445
+ );
1889
2446
  }
1890
2447
  function serializeFile(ctx, node) {
1891
- const options = '{type:"' + node.c + '",lastModified:' + node.b + "}";
1892
- const args = "[" + serializeTree(ctx, node.f) + '],"' + node.m + '",' + options;
1893
- return assignIndexedValue2(ctx, node.i, "new File(" + args + ")");
2448
+ return assignIndexedValue2(
2449
+ ctx,
2450
+ node.i,
2451
+ "new File([" + serializeTree(ctx, node.f) + '],"' + node.m + '",{type:"' + node.c + '",lastModified:' + node.b + "})"
2452
+ );
1894
2453
  }
1895
2454
  function serializeHeaders(ctx, node) {
1896
- return assignIndexedValue2(ctx, node.i, "new Headers(" + serializeProperties(ctx, node.i, node.d) + ")");
2455
+ return assignIndexedValue2(
2456
+ ctx,
2457
+ node.i,
2458
+ "new Headers(" + serializeProperties(ctx, node.i, node.d) + ")"
2459
+ );
1897
2460
  }
1898
2461
  function serializeFormDataEntries(ctx, node) {
1899
- ctx.stack.push(node.i);
1900
- const mainAssignments = [];
1901
- let parentStack;
1902
2462
  let value;
1903
2463
  let key;
2464
+ const keys = node.d.k;
2465
+ const vals = node.d.v;
2466
+ const id = node.i;
2467
+ const mainAssignments = [];
1904
2468
  let parentAssignment;
1905
- for (let i = 0; i < node.d.s; i++) {
1906
- parentStack = ctx.stack;
1907
- ctx.stack = [];
1908
- value = serializeTree(ctx, node.d.v[i]);
1909
- key = node.d.k[i];
1910
- ctx.stack = parentStack;
2469
+ ctx.stack.push(id);
2470
+ for (let i = 0, len = node.d.s; i < len; i++) {
2471
+ key = keys[i];
2472
+ value = serializeTree(ctx, vals[i]);
1911
2473
  parentAssignment = ctx.assignments;
1912
2474
  ctx.assignments = mainAssignments;
1913
- createAppendAssignment(ctx, node.i, '"' + key + '"', value);
2475
+ createAppendAssignment(ctx, id, '"' + key + '"', value);
1914
2476
  ctx.assignments = parentAssignment;
1915
2477
  }
1916
2478
  ctx.stack.pop();
1917
2479
  return resolveAssignments(mainAssignments);
1918
2480
  }
1919
2481
  function serializeFormData(ctx, node) {
1920
- if (node.d.s) {
1921
- markRef(ctx, node.i);
2482
+ const size = node.d.s;
2483
+ const id = node.i;
2484
+ if (size) {
2485
+ markRef(ctx, id);
1922
2486
  }
1923
- const result = assignIndexedValue2(ctx, node.i, "new FormData()");
1924
- if (node.d.s) {
1925
- return "(" + result + "," + serializeFormDataEntries(ctx, node) + getRefParam(ctx, node.i) + ")";
2487
+ const result = assignIndexedValue2(ctx, id, "new FormData()");
2488
+ if (size) {
2489
+ const entries = serializeFormDataEntries(ctx, node);
2490
+ return "(" + result + "," + (entries == null ? "" : entries) + getRefParam(ctx, id) + ")";
1926
2491
  }
1927
2492
  return result;
1928
2493
  }
2494
+ function serializeBoxed(ctx, node) {
2495
+ return assignIndexedValue2(ctx, node.i, "Object(" + serializeTree(ctx, node.f) + ")");
2496
+ }
2497
+ function serializeWKSymbol(ctx, node) {
2498
+ return assignIndexedValue2(ctx, node.i, SYMBOL_STRING[node.s]);
2499
+ }
1929
2500
  function serializeTree(ctx, node) {
1930
2501
  switch (node.t) {
1931
2502
  case 0 /* Number */:
1932
2503
  return "" + node.s;
1933
2504
  case 1 /* String */:
1934
2505
  return '"' + node.s + '"';
1935
- case 2 /* Boolean */:
1936
- return node.s ? "!0" : "!1";
1937
- case 4 /* Undefined */:
1938
- return "void 0";
1939
- case 3 /* Null */:
1940
- return "null";
1941
- case 5 /* NegativeZero */:
1942
- return "-0";
1943
- case 6 /* Infinity */:
1944
- return "1/0";
1945
- case 7 /* NegativeInfinity */:
1946
- return "-1/0";
1947
- case 8 /* NaN */:
1948
- return "NaN";
1949
- case 9 /* BigInt */:
2506
+ case 2 /* Constant */:
2507
+ return serializeConstant(node);
2508
+ case 3 /* BigInt */:
1950
2509
  return node.s + "n";
1951
- case 10 /* IndexedValue */:
2510
+ case 4 /* IndexedValue */:
1952
2511
  return getRefParam(ctx, node.i);
1953
- case 15 /* Array */:
2512
+ case 9 /* Array */:
1954
2513
  return serializeArray(ctx, node);
1955
- case 16 /* Object */:
2514
+ case 10 /* Object */:
1956
2515
  return serializeObject(ctx, node);
1957
- case 17 /* NullConstructor */:
2516
+ case 11 /* NullConstructor */:
1958
2517
  return serializeNullConstructor(ctx, node);
1959
- case 11 /* Date */:
2518
+ case 5 /* Date */:
1960
2519
  return serializeDate(ctx, node);
1961
- case 12 /* RegExp */:
2520
+ case 6 /* RegExp */:
1962
2521
  return serializeRegExp(ctx, node);
1963
- case 13 /* Set */:
2522
+ case 7 /* Set */:
1964
2523
  return serializeSet(ctx, node);
1965
- case 14 /* Map */:
2524
+ case 8 /* Map */:
1966
2525
  return serializeMap(ctx, node);
1967
- case 28 /* ArrayBuffer */:
2526
+ case 21 /* ArrayBuffer */:
1968
2527
  return serializeArrayBuffer2(ctx, node);
1969
- case 23 /* BigIntTypedArray */:
1970
- case 22 /* TypedArray */:
2528
+ case 16 /* BigIntTypedArray */:
2529
+ case 15 /* TypedArray */:
1971
2530
  return serializeTypedArray(ctx, node);
1972
- case 29 /* DataView */:
2531
+ case 22 /* DataView */:
1973
2532
  return serializeDataView(ctx, node);
1974
- case 20 /* AggregateError */:
2533
+ case 14 /* AggregateError */:
1975
2534
  return serializeAggregateError(ctx, node);
1976
- case 19 /* Error */:
2535
+ case 13 /* Error */:
1977
2536
  return serializeError(ctx, node);
1978
- case 21 /* Iterable */:
1979
- return serializeIterable(ctx, node);
1980
- case 18 /* Promise */:
2537
+ case 12 /* Promise */:
1981
2538
  return serializePromise(ctx, node);
1982
- case 24 /* WKSymbol */:
1983
- return SYMBOL_STRING[node.s];
1984
- case 25 /* URL */:
2539
+ case 17 /* WKSymbol */:
2540
+ return serializeWKSymbol(ctx, node);
2541
+ case 18 /* URL */:
1985
2542
  return serializeURL(ctx, node);
1986
- case 26 /* URLSearchParams */:
2543
+ case 19 /* URLSearchParams */:
1987
2544
  return serializeURLSearchParams(ctx, node);
1988
- case 27 /* Reference */:
2545
+ case 20 /* Reference */:
1989
2546
  return serializeReference(ctx, node);
1990
- case 30 /* Blob */:
2547
+ case 23 /* Blob */:
1991
2548
  return serializeBlob(ctx, node);
1992
- case 31 /* File */:
2549
+ case 24 /* File */:
1993
2550
  return serializeFile(ctx, node);
1994
- case 32 /* Headers */:
2551
+ case 25 /* Headers */:
1995
2552
  return serializeHeaders(ctx, node);
1996
- case 33 /* FormData */:
2553
+ case 26 /* FormData */:
1997
2554
  return serializeFormData(ctx, node);
2555
+ case 27 /* Boxed */:
2556
+ return serializeBoxed(ctx, node);
1998
2557
  default:
1999
- throw new Error("Unsupported type");
2000
- }
2001
- }
2002
-
2003
- // src/tree/sync.ts
2004
- function generateNodeList2(ctx, current) {
2005
- const size = current.length;
2006
- const nodes = new Array(size);
2007
- const deferred = new Array(size);
2008
- let item;
2009
- for (let i = 0; i < size; i++) {
2010
- if (i in current) {
2011
- item = current[i];
2012
- if (isIterable(item)) {
2013
- deferred[i] = item;
2014
- } else {
2015
- nodes[i] = parse2(ctx, item);
2016
- }
2017
- }
2018
- }
2019
- for (let i = 0; i < size; i++) {
2020
- if (i in deferred) {
2021
- nodes[i] = parse2(ctx, deferred[i]);
2022
- }
2023
- }
2024
- return nodes;
2025
- }
2026
- function generateArrayNode2(ctx, id, current) {
2027
- return {
2028
- t: 15 /* Array */,
2029
- i: id,
2030
- s: void 0,
2031
- l: current.length,
2032
- c: void 0,
2033
- m: void 0,
2034
- d: void 0,
2035
- a: generateNodeList2(ctx, current),
2036
- f: void 0,
2037
- b: void 0
2038
- };
2039
- }
2040
- function generateMapNode2(ctx, id, current) {
2041
- assert(ctx.features & 32 /* Map */, 'Unsupported type "Map"');
2042
- const len = current.size;
2043
- const keyNodes = new Array(len);
2044
- const valueNodes = new Array(len);
2045
- const deferredKey = new Array(len);
2046
- const deferredValue = new Array(len);
2047
- let deferredSize = 0;
2048
- let nodeSize = 0;
2049
- for (const [key, value] of current.entries()) {
2050
- if (isIterable(key) || isIterable(value)) {
2051
- deferredKey[deferredSize] = key;
2052
- deferredValue[deferredSize] = value;
2053
- deferredSize++;
2054
- } else {
2055
- keyNodes[nodeSize] = parse2(ctx, key);
2056
- valueNodes[nodeSize] = parse2(ctx, value);
2057
- nodeSize++;
2058
- }
2059
- }
2060
- for (let i = 0; i < deferredSize; i++) {
2061
- keyNodes[nodeSize + i] = parse2(ctx, deferredKey[i]);
2062
- valueNodes[nodeSize + i] = parse2(ctx, deferredValue[i]);
2063
- }
2064
- return {
2065
- t: 14 /* Map */,
2066
- i: id,
2067
- s: void 0,
2068
- l: void 0,
2069
- c: void 0,
2070
- m: void 0,
2071
- d: { k: keyNodes, v: valueNodes, s: len },
2072
- a: void 0,
2073
- f: void 0,
2074
- b: void 0
2075
- };
2076
- }
2077
- function generateSetNode2(ctx, id, current) {
2078
- assert(ctx.features & 512 /* Set */, 'Unsupported type "Set"');
2079
- const len = current.size;
2080
- const nodes = new Array(len);
2081
- const deferred = new Array(len);
2082
- let deferredSize = 0;
2083
- let nodeSize = 0;
2084
- for (const item of current.keys()) {
2085
- if (isIterable(item)) {
2086
- deferred[deferredSize++] = item;
2087
- } else {
2088
- nodes[nodeSize++] = parse2(ctx, item);
2089
- }
2090
- }
2091
- for (let i = 0; i < deferredSize; i++) {
2092
- nodes[nodeSize + i] = parse2(ctx, deferred[i]);
2093
- }
2094
- return {
2095
- t: 13 /* Set */,
2096
- i: id,
2097
- s: void 0,
2098
- l: len,
2099
- c: void 0,
2100
- m: void 0,
2101
- d: void 0,
2102
- a: nodes,
2103
- f: void 0,
2104
- b: void 0
2105
- };
2106
- }
2107
- function generateProperties2(ctx, properties) {
2108
- const keys = Object.keys(properties);
2109
- const size = keys.length;
2110
- const keyNodes = new Array(size);
2111
- const valueNodes = new Array(size);
2112
- const deferredKeys = new Array(size);
2113
- const deferredValues = new Array(size);
2114
- let deferredSize = 0;
2115
- let nodesSize = 0;
2116
- let item;
2117
- let escaped;
2118
- for (const key of keys) {
2119
- item = properties[key];
2120
- escaped = serializeString(key);
2121
- if (isIterable(item)) {
2122
- deferredKeys[deferredSize] = escaped;
2123
- deferredValues[deferredSize] = item;
2124
- deferredSize++;
2125
- } else {
2126
- keyNodes[nodesSize] = escaped;
2127
- valueNodes[nodesSize] = parse2(ctx, item);
2128
- nodesSize++;
2129
- }
2130
- }
2131
- for (let i = 0; i < deferredSize; i++) {
2132
- keyNodes[nodesSize + i] = deferredKeys[i];
2133
- valueNodes[nodesSize + i] = parse2(ctx, deferredValues[i]);
2134
- }
2135
- return {
2136
- k: keyNodes,
2137
- v: valueNodes,
2138
- s: size
2139
- };
2140
- }
2141
- function generateIterableNode2(ctx, id, current) {
2142
- assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "Iterable"');
2143
- const options = getIterableOptions(current);
2144
- const array = Array.from(current);
2145
- return {
2146
- t: 21 /* Iterable */,
2147
- i: id,
2148
- s: void 0,
2149
- l: array.length,
2150
- c: void 0,
2151
- m: void 0,
2152
- // Parse options first before the items
2153
- d: options ? generateProperties2(ctx, options) : void 0,
2154
- a: generateNodeList2(ctx, array),
2155
- f: void 0,
2156
- b: void 0
2157
- };
2158
- }
2159
- function generateObjectNode2(ctx, id, current, empty) {
2160
- if (Symbol.iterator in current) {
2161
- return generateIterableNode2(ctx, id, current);
2162
- }
2163
- return {
2164
- t: empty ? 17 /* NullConstructor */ : 16 /* Object */,
2165
- i: id,
2166
- s: void 0,
2167
- l: void 0,
2168
- c: void 0,
2169
- m: void 0,
2170
- d: generateProperties2(ctx, current),
2171
- a: void 0,
2172
- f: void 0,
2173
- b: void 0
2174
- };
2175
- }
2176
- function generateAggregateErrorNode2(ctx, id, current) {
2177
- const options = getErrorOptions(ctx, current);
2178
- const optionsNode = options ? generateProperties2(ctx, options) : void 0;
2179
- return {
2180
- t: 20 /* AggregateError */,
2181
- i: id,
2182
- s: void 0,
2183
- l: void 0,
2184
- c: void 0,
2185
- m: serializeString(current.message),
2186
- d: optionsNode,
2187
- a: void 0,
2188
- f: void 0,
2189
- b: void 0
2190
- };
2191
- }
2192
- function generateErrorNode2(ctx, id, current) {
2193
- const options = getErrorOptions(ctx, current);
2194
- const optionsNode = options ? generateProperties2(ctx, options) : void 0;
2195
- return {
2196
- t: 19 /* Error */,
2197
- i: id,
2198
- s: void 0,
2199
- l: void 0,
2200
- c: getErrorConstructorName(current),
2201
- m: serializeString(current.message),
2202
- d: optionsNode,
2203
- a: void 0,
2204
- f: void 0,
2205
- b: void 0
2206
- };
2207
- }
2208
- function generateHeadersNode2(ctx, id, current) {
2209
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "Headers"');
2210
- const items = {};
2211
- current.forEach((value, key) => {
2212
- items[key] = value;
2213
- });
2214
- return {
2215
- t: 32 /* Headers */,
2216
- i: id,
2217
- s: void 0,
2218
- l: void 0,
2219
- c: void 0,
2220
- m: void 0,
2221
- d: generateProperties2(ctx, items),
2222
- a: void 0,
2223
- f: void 0,
2224
- b: void 0
2225
- };
2226
- }
2227
- function generateFormDataNode2(ctx, id, current) {
2228
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "FormData"');
2229
- const items = {};
2230
- current.forEach((value, key) => {
2231
- items[key] = value;
2232
- });
2233
- return {
2234
- t: 33 /* FormData */,
2235
- i: id,
2236
- s: void 0,
2237
- l: void 0,
2238
- c: void 0,
2239
- m: void 0,
2240
- d: generateProperties2(ctx, items),
2241
- a: void 0,
2242
- f: void 0,
2243
- b: void 0
2244
- };
2245
- }
2246
- function parse2(ctx, current) {
2247
- switch (typeof current) {
2248
- case "boolean":
2249
- return current ? TRUE_NODE : FALSE_NODE;
2250
- case "undefined":
2251
- return UNDEFINED_NODE;
2252
- case "string":
2253
- return createStringNode(current);
2254
- case "number":
2255
- switch (current) {
2256
- case Infinity:
2257
- return INFINITY_NODE;
2258
- case -Infinity:
2259
- return NEG_INFINITY_NODE;
2260
- default:
2261
- if (current !== current) {
2262
- return NAN_NODE;
2263
- }
2264
- if (Object.is(current, -0)) {
2265
- return NEG_ZERO_NODE;
2266
- }
2267
- return createNumberNode(current);
2268
- }
2269
- case "bigint":
2270
- return createBigIntNode(ctx, current);
2271
- case "object": {
2272
- if (!current) {
2273
- return NULL_NODE;
2274
- }
2275
- const id = createIndexedValue(ctx, current);
2276
- if (ctx.markedRefs.has(id)) {
2277
- return createIndexedValueNode(id);
2278
- }
2279
- if (hasReferenceID(current)) {
2280
- return createReferenceNode(id, current);
2281
- }
2282
- if (Array.isArray(current)) {
2283
- return generateArrayNode2(ctx, id, current);
2284
- }
2285
- switch (current.constructor) {
2286
- case Date:
2287
- return createDateNode(id, current);
2288
- case RegExp:
2289
- return createRegExpNode(id, current);
2290
- case ArrayBuffer:
2291
- return createArrayBufferNode(id, current);
2292
- case Int8Array:
2293
- case Int16Array:
2294
- case Int32Array:
2295
- case Uint8Array:
2296
- case Uint16Array:
2297
- case Uint32Array:
2298
- case Uint8ClampedArray:
2299
- case Float32Array:
2300
- case Float64Array:
2301
- return createTypedArrayNode(ctx, id, current);
2302
- case BigInt64Array:
2303
- case BigUint64Array:
2304
- return createBigIntTypedArrayNode(ctx, id, current);
2305
- case DataView:
2306
- return createDataViewNode(ctx, id, current);
2307
- case Map:
2308
- return generateMapNode2(ctx, id, current);
2309
- case Set:
2310
- return generateSetNode2(ctx, id, current);
2311
- case Object:
2312
- return generateObjectNode2(
2313
- ctx,
2314
- id,
2315
- current,
2316
- false
2317
- );
2318
- case void 0:
2319
- return generateObjectNode2(
2320
- ctx,
2321
- id,
2322
- current,
2323
- true
2324
- );
2325
- case AggregateError:
2326
- if (ctx.features & 1 /* AggregateError */) {
2327
- return generateAggregateErrorNode2(ctx, id, current);
2328
- }
2329
- return generateErrorNode2(ctx, id, current);
2330
- case Error:
2331
- case EvalError:
2332
- case RangeError:
2333
- case ReferenceError:
2334
- case SyntaxError:
2335
- case TypeError:
2336
- case URIError:
2337
- return generateErrorNode2(ctx, id, current);
2338
- case URL:
2339
- return createURLNode(ctx, id, current);
2340
- case URLSearchParams:
2341
- return createURLSearchParamsNode(ctx, id, current);
2342
- case Headers:
2343
- return generateHeadersNode2(ctx, id, current);
2344
- case FormData:
2345
- return generateFormDataNode2(ctx, id, current);
2346
- default:
2347
- break;
2348
- }
2349
- if (current instanceof AggregateError) {
2350
- if (ctx.features & 1 /* AggregateError */) {
2351
- return generateAggregateErrorNode2(ctx, id, current);
2352
- }
2353
- return generateErrorNode2(ctx, id, current);
2354
- }
2355
- if (current instanceof Error) {
2356
- return generateErrorNode2(ctx, id, current);
2357
- }
2358
- if (Symbol.iterator in current) {
2359
- return generateIterableNode2(ctx, id, current);
2360
- }
2361
- throw new Error("Unsupported type");
2362
- }
2363
- case "symbol":
2364
- if (hasReferenceID(current)) {
2365
- const id = createIndexedValue(ctx, current);
2366
- if (ctx.markedRefs.has(id)) {
2367
- return createIndexedValueNode(id);
2368
- }
2369
- return createReferenceNode(id, current);
2370
- }
2371
- return createWKSymbolNode(ctx, current);
2372
- case "function": {
2373
- assert(hasReferenceID(current), "Cannot serialize function without reference ID.");
2374
- const id = createIndexedValue(ctx, current);
2375
- if (ctx.markedRefs.has(id)) {
2376
- return createIndexedValueNode(id);
2377
- }
2378
- return createReferenceNode(id, current);
2379
- }
2380
- default:
2381
- throw new Error("Unsupported type");
2558
+ throw new Error("invariant");
2382
2559
  }
2383
2560
  }
2384
- function parseSync(ctx, current) {
2385
- const result = parse2(ctx, current);
2386
- const isObject = result.t === 16 /* Object */ || result.t === 21 /* Iterable */;
2387
- return [result, getRootID(ctx, current), isObject];
2388
- }
2389
2561
 
2390
2562
  // src/index.ts
2391
2563
  function finalize(ctx, rootID, isObject, result) {
@@ -2413,39 +2585,45 @@ function finalize(ctx, rootID, isObject, result) {
2413
2585
  }
2414
2586
  function serialize(source, options) {
2415
2587
  const ctx = createParserContext(options);
2416
- const [tree, rootID, isObject] = parseSync(ctx, source);
2588
+ const tree = parseSync(ctx, source);
2417
2589
  const serial = createSerializationContext(ctx);
2418
2590
  const result = serializeTree(serial, tree);
2419
- return finalize(serial, rootID, isObject, result);
2591
+ return finalize(
2592
+ serial,
2593
+ getRootID(ctx, source),
2594
+ tree.t === 10 /* Object */,
2595
+ result
2596
+ );
2420
2597
  }
2421
2598
  async function serializeAsync(source, options) {
2422
2599
  const ctx = createParserContext(options);
2423
- const [tree, rootID, isObject] = await parseAsync(ctx, source);
2600
+ const tree = await parseAsync(ctx, source);
2424
2601
  const serial = createSerializationContext(ctx);
2425
2602
  const result = serializeTree(serial, tree);
2426
- return finalize(serial, rootID, isObject, result);
2603
+ return finalize(
2604
+ serial,
2605
+ getRootID(ctx, source),
2606
+ tree.t === 10 /* Object */,
2607
+ result
2608
+ );
2427
2609
  }
2428
2610
  function deserialize(source) {
2429
2611
  return (0, eval)(source);
2430
2612
  }
2431
2613
  function toJSON(source, options) {
2432
2614
  const ctx = createParserContext(options);
2433
- const [tree, root, isObject] = parseSync(ctx, source);
2434
2615
  return {
2435
- t: tree,
2436
- r: root,
2437
- i: isObject,
2616
+ t: parseSync(ctx, source),
2617
+ r: getRootID(ctx, source),
2438
2618
  f: ctx.features,
2439
2619
  m: Array.from(ctx.markedRefs)
2440
2620
  };
2441
2621
  }
2442
2622
  async function toJSONAsync(source, options) {
2443
2623
  const ctx = createParserContext(options);
2444
- const [tree, root, isObject] = await parseAsync(ctx, source);
2445
2624
  return {
2446
- t: tree,
2447
- r: root,
2448
- i: isObject,
2625
+ t: await parseAsync(ctx, source),
2626
+ r: getRootID(ctx, source),
2449
2627
  f: ctx.features,
2450
2628
  m: Array.from(ctx.markedRefs)
2451
2629
  };
@@ -2456,7 +2634,7 @@ function compileJSON(source) {
2456
2634
  markedRefs: source.m
2457
2635
  });
2458
2636
  const result = serializeTree(serial, source.t);
2459
- return finalize(serial, source.r, source.i, result);
2637
+ return finalize(serial, source.r, source.t.i === 10 /* Object */, result);
2460
2638
  }
2461
2639
  function fromJSON(source) {
2462
2640
  const serial = createSerializationContext({