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