seroval 0.4.0-alpha.11 → 0.4.0-alpha.13

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.
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
7
  var __export = (target, all) => {
7
8
  for (var name in all)
8
9
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
16
17
  return to;
17
18
  };
18
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => {
21
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+ return value;
23
+ };
19
24
 
20
25
  // src/index.ts
21
26
  var src_exports = {};
@@ -275,25 +280,33 @@ function getIdentifier(index) {
275
280
  var DEFAULT_OPTIONS = {
276
281
  target: "es2023"
277
282
  };
278
- function createParserContext(options = {}) {
279
- const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
280
- return {
281
- markedRefs: {},
282
- refs: /* @__PURE__ */ new Map(),
283
- features: parseTargets(result.target)
284
- };
285
- }
286
- function createSerializationContext(options) {
287
- return {
288
- stack: [],
289
- vars: [],
290
- assignments: [],
291
- validRefs: [],
292
- refSize: 0,
293
- features: options.features,
294
- markedRefs: options.markedRefs
295
- };
296
- }
283
+ var ParserContext = class {
284
+ constructor(options = {}) {
285
+ __publicField(this, "refs", /* @__PURE__ */ new Map());
286
+ __publicField(this, "markedRefs", {});
287
+ __publicField(this, "features");
288
+ const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
289
+ this.features = parseTargets(result.target);
290
+ }
291
+ };
292
+ var SerializationContext = class {
293
+ constructor(options) {
294
+ __publicField(this, "stack", []);
295
+ // Map tree refs to actual refs
296
+ __publicField(this, "validRefs", []);
297
+ __publicField(this, "refSize", 0);
298
+ // Variables
299
+ __publicField(this, "vars", []);
300
+ // Array of assignments to be done (used for recursion)
301
+ __publicField(this, "assignments", []);
302
+ // Supported features
303
+ __publicField(this, "features");
304
+ // Refs that are...referenced
305
+ __publicField(this, "markedRefs");
306
+ this.features = options.features;
307
+ this.markedRefs = options.markedRefs;
308
+ }
309
+ };
297
310
  function markRef(ctx, current) {
298
311
  ctx.markedRefs[current] = 1;
299
312
  }
@@ -377,119 +390,198 @@ function quote(str) {
377
390
  return `"${result}"`;
378
391
  }
379
392
 
393
+ // src/tree/types.ts
394
+ var SerovalObjectRecordNode = class {
395
+ constructor(k, v, s) {
396
+ __publicField(this, "k");
397
+ __publicField(this, "v");
398
+ __publicField(this, "s");
399
+ this.k = k;
400
+ this.v = v;
401
+ this.s = s;
402
+ }
403
+ };
404
+ var SerovalMapRecordNode = class {
405
+ constructor(k, v, s) {
406
+ __publicField(this, "k");
407
+ __publicField(this, "v");
408
+ __publicField(this, "s");
409
+ this.k = k;
410
+ this.v = v;
411
+ this.s = s;
412
+ }
413
+ };
414
+ var SerovalPrimitiveNode = class {
415
+ constructor(current) {
416
+ __publicField(this, "t", 0 /* Primitive */);
417
+ __publicField(this, "s");
418
+ this.s = current;
419
+ }
420
+ };
421
+ var SerovalReferenceNode = class {
422
+ constructor(id) {
423
+ __publicField(this, "t", 2 /* Reference */);
424
+ __publicField(this, "i");
425
+ this.i = id;
426
+ }
427
+ };
428
+ var SerovalBigIntNode = class {
429
+ constructor(current) {
430
+ __publicField(this, "t", 1 /* BigInt */);
431
+ __publicField(this, "s");
432
+ this.s = `${current}n`;
433
+ }
434
+ };
435
+ var SerovalDateNode = class {
436
+ constructor(id, current) {
437
+ __publicField(this, "t", 3 /* Date */);
438
+ __publicField(this, "i");
439
+ __publicField(this, "s");
440
+ this.i = id;
441
+ this.s = current.toISOString();
442
+ }
443
+ };
444
+ var SerovalRegExpNode = class {
445
+ constructor(id, current) {
446
+ __publicField(this, "t", 4 /* RegExp */);
447
+ __publicField(this, "i");
448
+ __publicField(this, "s");
449
+ this.i = id;
450
+ this.s = String(current);
451
+ }
452
+ };
453
+ var SerovalTypedArrayNode = class {
454
+ constructor(id, current) {
455
+ __publicField(this, "t", 14 /* TypedArray */);
456
+ __publicField(this, "i");
457
+ __publicField(this, "s");
458
+ __publicField(this, "l");
459
+ __publicField(this, "c");
460
+ this.i = id;
461
+ this.s = current.toString();
462
+ this.l = current.byteOffset;
463
+ this.c = current.constructor.name;
464
+ }
465
+ };
466
+ var SerovalBigIntTypedArrayNode = class {
467
+ constructor(id, current) {
468
+ __publicField(this, "t", 15 /* BigIntTypedArray */);
469
+ __publicField(this, "i");
470
+ __publicField(this, "s");
471
+ __publicField(this, "l");
472
+ __publicField(this, "c");
473
+ let result = "";
474
+ const cap = current.length - 1;
475
+ for (let i = 0; i < cap; i++) {
476
+ result += `${current[i]}n,`;
477
+ }
478
+ result += `"${current[cap]}"`;
479
+ this.i = id;
480
+ this.s = result;
481
+ this.l = current.byteOffset;
482
+ this.c = current.constructor.name;
483
+ }
484
+ };
485
+ var SerovalSetNode = class {
486
+ constructor(id, a) {
487
+ __publicField(this, "t", 5 /* Set */);
488
+ __publicField(this, "i");
489
+ __publicField(this, "a");
490
+ this.i = id;
491
+ this.a = a;
492
+ }
493
+ };
494
+ var SerovalMapNode = class {
495
+ constructor(id, d) {
496
+ __publicField(this, "t", 6 /* Map */);
497
+ __publicField(this, "i");
498
+ __publicField(this, "d");
499
+ this.i = id;
500
+ this.d = d;
501
+ }
502
+ };
503
+ var SerovalArrayNode = class {
504
+ constructor(id, a) {
505
+ __publicField(this, "t", 7 /* Array */);
506
+ __publicField(this, "i");
507
+ __publicField(this, "a");
508
+ this.i = id;
509
+ this.a = a;
510
+ }
511
+ };
512
+ var SerovalObjectNode = class {
513
+ constructor(id, d) {
514
+ __publicField(this, "t", 8 /* Object */);
515
+ __publicField(this, "i");
516
+ __publicField(this, "d");
517
+ this.i = id;
518
+ this.d = d;
519
+ }
520
+ };
521
+ var SerovalNullConstructorNode = class {
522
+ constructor(id, d) {
523
+ __publicField(this, "t", 9 /* NullConstructor */);
524
+ __publicField(this, "i");
525
+ __publicField(this, "d");
526
+ this.i = id;
527
+ this.d = d;
528
+ }
529
+ };
530
+ var SerovalPromiseNode = class {
531
+ constructor(id, n) {
532
+ __publicField(this, "t", 10 /* Promise */);
533
+ __publicField(this, "i");
534
+ __publicField(this, "n");
535
+ this.i = id;
536
+ this.n = n;
537
+ }
538
+ };
539
+ var SerovalErrorNode = class {
540
+ constructor(id, c, m, d) {
541
+ __publicField(this, "t", 11 /* Error */);
542
+ __publicField(this, "i");
543
+ __publicField(this, "c");
544
+ __publicField(this, "m");
545
+ __publicField(this, "d");
546
+ this.i = id;
547
+ this.c = c;
548
+ this.m = m;
549
+ this.d = d;
550
+ }
551
+ };
552
+ var SerovalAggregateErrorNode = class {
553
+ constructor(id, m, d, n) {
554
+ __publicField(this, "t", 12 /* AggregateError */);
555
+ __publicField(this, "i");
556
+ __publicField(this, "m");
557
+ __publicField(this, "d");
558
+ __publicField(this, "n");
559
+ this.i = id;
560
+ this.m = m;
561
+ this.d = d;
562
+ this.n = n;
563
+ }
564
+ };
565
+ var SerovalIterableNode = class {
566
+ constructor(id, d, a) {
567
+ __publicField(this, "t", 13 /* Iterable */);
568
+ __publicField(this, "i");
569
+ __publicField(this, "d");
570
+ __publicField(this, "a");
571
+ this.i = id;
572
+ this.d = d;
573
+ this.a = a;
574
+ }
575
+ };
576
+
380
577
  // src/tree/primitives.ts
381
- function createPrimitiveNode(value) {
382
- return {
383
- t: 0 /* Primitive */,
384
- i: void 0,
385
- s: value,
386
- l: void 0,
387
- m: void 0,
388
- c: void 0,
389
- d: void 0,
390
- a: void 0,
391
- n: void 0
392
- };
393
- }
394
- var TRUE_NODE = createPrimitiveNode("!0");
395
- var FALSE_NODE = createPrimitiveNode("!1");
396
- var UNDEFINED_NODE = createPrimitiveNode("void 0");
397
- var NULL_NODE = createPrimitiveNode(null);
398
- var NEG_ZERO_NODE = createPrimitiveNode("-0");
399
- var INFINITY_NODE = createPrimitiveNode("1/0");
400
- var NEG_INFINITY_NODE = createPrimitiveNode("-1/0");
401
- function createBigIntNode(ctx, current) {
402
- assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
403
- return {
404
- t: 1 /* BigInt */,
405
- i: void 0,
406
- a: void 0,
407
- s: `${current}n`,
408
- l: void 0,
409
- m: void 0,
410
- c: void 0,
411
- d: void 0,
412
- n: void 0
413
- };
414
- }
415
- function createReferenceNode(id) {
416
- return {
417
- t: 2 /* Reference */,
418
- i: id,
419
- a: void 0,
420
- s: void 0,
421
- l: void 0,
422
- m: void 0,
423
- c: void 0,
424
- d: void 0,
425
- n: void 0
426
- };
427
- }
428
- function createDateNode(id, current) {
429
- return {
430
- t: 3 /* Date */,
431
- i: id,
432
- a: void 0,
433
- s: current.toISOString(),
434
- l: void 0,
435
- m: void 0,
436
- c: void 0,
437
- d: void 0,
438
- n: void 0
439
- };
440
- }
441
- function createRegExpNode(id, current) {
442
- return {
443
- t: 4 /* RegExp */,
444
- i: id,
445
- a: void 0,
446
- s: String(current),
447
- l: void 0,
448
- m: void 0,
449
- c: void 0,
450
- d: void 0,
451
- n: void 0
452
- };
453
- }
454
- function createTypedArrayNode(ctx, id, current) {
455
- const constructor = current.constructor.name;
456
- assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
457
- return {
458
- t: 14 /* TypedArray */,
459
- i: id,
460
- a: void 0,
461
- s: current.toString(),
462
- l: current.byteOffset,
463
- m: void 0,
464
- c: constructor,
465
- d: void 0,
466
- n: void 0
467
- };
468
- }
469
- function createBigIntTypedArrayNode(ctx, id, current) {
470
- const constructor = current.constructor.name;
471
- assert(
472
- ctx.features & 2048 /* BigIntTypedArray */,
473
- `Unsupported value type "${constructor}"`
474
- );
475
- let result = "";
476
- const cap = current.length - 1;
477
- for (let i = 0; i < cap; i++) {
478
- result += `${current[i]}n,`;
479
- }
480
- result += `"${current[cap]}"`;
481
- return {
482
- t: 15 /* BigIntTypedArray */,
483
- i: id,
484
- a: void 0,
485
- s: result,
486
- l: current.byteOffset,
487
- m: void 0,
488
- c: constructor,
489
- d: void 0,
490
- n: void 0
491
- };
492
- }
578
+ var TRUE_NODE = new SerovalPrimitiveNode("!0");
579
+ var FALSE_NODE = new SerovalPrimitiveNode("!1");
580
+ var UNDEFINED_NODE = new SerovalPrimitiveNode("void 0");
581
+ var NULL_NODE = new SerovalPrimitiveNode(null);
582
+ var NEG_ZERO_NODE = new SerovalPrimitiveNode("-0");
583
+ var INFINITY_NODE = new SerovalPrimitiveNode("1/0");
584
+ var NEG_INFINITY_NODE = new SerovalPrimitiveNode("-1/0");
493
585
 
494
586
  // src/tree/shared.ts
495
587
  function getErrorConstructor(error) {
@@ -573,317 +665,257 @@ function isIterable(value) {
573
665
  }
574
666
 
575
667
  // src/tree/async.ts
576
- async function serializePropertiesAsync(ctx, properties) {
577
- const keys = Object.keys(properties);
578
- const size = keys.length;
579
- const keyNodes = new Array(size);
580
- const valueNodes = new Array(size);
581
- const deferredKeys = new Array(size);
582
- const deferredValues = new Array(size);
583
- let deferredSize = 0;
584
- let nodesSize = 0;
585
- let item;
586
- for (const key of keys) {
587
- item = properties[key];
588
- if (isIterable(item)) {
589
- deferredKeys[deferredSize] = key;
590
- deferredValues[deferredSize] = item;
591
- deferredSize++;
592
- } else {
593
- keyNodes[nodesSize] = key;
594
- valueNodes[nodesSize] = await generateTreeAsync(ctx, item);
595
- nodesSize++;
668
+ var AsyncParser = class {
669
+ constructor(ctx) {
670
+ __publicField(this, "ctx");
671
+ this.ctx = ctx;
672
+ }
673
+ async generateNodeList(current) {
674
+ const size = current.length;
675
+ const nodes = new Array(size);
676
+ const deferred = new Array(size);
677
+ let item;
678
+ for (let i = 0; i < size; i++) {
679
+ if (i in current) {
680
+ item = current[i];
681
+ if (isIterable(item)) {
682
+ deferred[i] = item;
683
+ } else {
684
+ nodes[i] = await this.parse(item);
685
+ }
686
+ }
596
687
  }
597
- }
598
- for (let i = 0; i < deferredSize; i++) {
599
- keyNodes[nodesSize + i] = deferredKeys[i];
600
- valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
601
- }
602
- return {
603
- k: keyNodes,
604
- v: valueNodes,
605
- s: size
606
- };
607
- }
608
- async function generateSetNode(ctx, current, id) {
609
- assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
610
- const len = current.size;
611
- const nodes = new Array(len);
612
- const deferred = new Array(len);
613
- let deferredSize = 0;
614
- let nodeSize = 0;
615
- for (const item of current.keys()) {
616
- if (isIterable(item)) {
617
- deferred[deferredSize++] = item;
618
- } else {
619
- nodes[nodeSize++] = await generateTreeAsync(ctx, item);
688
+ for (let i = 0; i < size; i++) {
689
+ if (i in deferred) {
690
+ nodes[i] = await this.parse(deferred[i]);
691
+ }
620
692
  }
621
- }
622
- for (let i = 0; i < deferredSize; i++) {
623
- nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
624
- }
625
- return {
626
- t: 5 /* Set */,
627
- i: id,
628
- a: nodes,
629
- s: void 0,
630
- l: void 0,
631
- m: void 0,
632
- c: void 0,
633
- d: void 0,
634
- n: void 0
635
- };
636
- }
637
- async function generateMapNode(ctx, current, id) {
638
- assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
639
- const len = current.size;
640
- const keyNodes = new Array(len);
641
- const valueNodes = new Array(len);
642
- const deferredKey = new Array(len);
643
- const deferredValue = new Array(len);
644
- let deferredSize = 0;
645
- let nodeSize = 0;
646
- for (const [key, value] of current.entries()) {
647
- if (isIterable(key) || isIterable(value)) {
648
- deferredKey[deferredSize] = key;
649
- deferredValue[deferredSize] = value;
650
- deferredSize++;
651
- } else {
652
- keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
653
- valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
654
- nodeSize++;
693
+ return nodes;
694
+ }
695
+ async generateArrayNode(id, current) {
696
+ return new SerovalArrayNode(id, await this.generateNodeList(current));
697
+ }
698
+ async generateMapNode(id, current) {
699
+ assert(this.ctx.features & 16 /* Map */, 'Unsupported type "Map"');
700
+ const len = current.size;
701
+ const keyNodes = new Array(len);
702
+ const valueNodes = new Array(len);
703
+ const deferredKey = new Array(len);
704
+ const deferredValue = new Array(len);
705
+ let deferredSize = 0;
706
+ let nodeSize = 0;
707
+ for (const [key, value] of current.entries()) {
708
+ if (isIterable(key) || isIterable(value)) {
709
+ deferredKey[deferredSize] = key;
710
+ deferredValue[deferredSize] = value;
711
+ deferredSize++;
712
+ } else {
713
+ keyNodes[nodeSize] = await this.parse(key);
714
+ valueNodes[nodeSize] = await this.parse(value);
715
+ nodeSize++;
716
+ }
655
717
  }
656
- }
657
- for (let i = 0; i < deferredSize; i++) {
658
- keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
659
- valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
660
- }
661
- return {
662
- t: 6 /* Map */,
663
- i: id,
664
- a: void 0,
665
- s: void 0,
666
- l: void 0,
667
- m: void 0,
668
- c: void 0,
669
- d: { k: keyNodes, v: valueNodes, s: len },
670
- n: void 0
671
- };
672
- }
673
- async function generateNodeList(ctx, current) {
674
- const size = current.length;
675
- const nodes = new Array(size);
676
- const deferred = new Array(size);
677
- let item;
678
- for (let i = 0; i < size; i++) {
679
- if (i in current) {
680
- item = current[i];
718
+ for (let i = 0; i < deferredSize; i++) {
719
+ keyNodes[nodeSize + i] = await this.parse(deferredKey[i]);
720
+ valueNodes[nodeSize + i] = await this.parse(deferredValue[i]);
721
+ }
722
+ return new SerovalMapNode(id, new SerovalMapRecordNode(keyNodes, valueNodes, len));
723
+ }
724
+ async generateSetNode(id, current) {
725
+ assert(this.ctx.features & 256 /* Set */, 'Unsupported type "Set"');
726
+ const len = current.size;
727
+ const nodes = new Array(len);
728
+ const deferred = new Array(len);
729
+ let deferredSize = 0;
730
+ let nodeSize = 0;
731
+ for (const item of current.keys()) {
681
732
  if (isIterable(item)) {
682
- deferred[i] = item;
733
+ deferred[deferredSize++] = item;
683
734
  } else {
684
- nodes[i] = await generateTreeAsync(ctx, item);
735
+ nodes[nodeSize++] = await this.parse(item);
685
736
  }
686
737
  }
687
- }
688
- for (let i = 0; i < size; i++) {
689
- if (i in deferred) {
690
- nodes[i] = await generateTreeAsync(ctx, deferred[i]);
738
+ for (let i = 0; i < deferredSize; i++) {
739
+ nodes[nodeSize + i] = await this.parse(deferred[i]);
691
740
  }
692
- }
693
- return nodes;
694
- }
695
- async function generateArrayNode(ctx, current, id) {
696
- return {
697
- t: 7 /* Array */,
698
- i: id,
699
- a: await generateNodeList(ctx, current),
700
- s: void 0,
701
- l: void 0,
702
- m: void 0,
703
- c: void 0,
704
- d: void 0,
705
- n: void 0
706
- };
707
- }
708
- async function generateAggregateErrorNode(ctx, current, id) {
709
- const options = getErrorOptions(current);
710
- const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
711
- return {
712
- t: 12 /* AggregateError */,
713
- i: id,
714
- a: void 0,
715
- s: void 0,
716
- l: void 0,
717
- m: current.message,
718
- c: void 0,
719
- d: optionsNode,
720
- n: await generateTreeAsync(ctx, current.errors)
721
- };
722
- }
723
- async function generateErrorNode(ctx, current, id) {
724
- const options = getErrorOptions(current);
725
- const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
726
- return {
727
- t: 11 /* Error */,
728
- i: id,
729
- a: void 0,
730
- s: void 0,
731
- l: void 0,
732
- m: current.message,
733
- c: getErrorConstructor(current),
734
- d: optionsNode,
735
- n: void 0
736
- };
737
- }
738
- async function generateIterableNode(ctx, current, id) {
739
- assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
740
- const options = getIterableOptions(current);
741
- return {
742
- t: 13 /* Iterable */,
743
- i: id,
744
- s: void 0,
745
- l: void 0,
746
- m: void 0,
747
- c: void 0,
748
- // Parse options first before the items
749
- d: options ? await serializePropertiesAsync(ctx, options) : void 0,
750
- a: await generateNodeList(ctx, Array.from(current)),
751
- n: void 0
752
- };
753
- }
754
- async function generateObjectNode(ctx, current, id, empty) {
755
- if (Symbol.iterator in current) {
756
- return generateIterableNode(ctx, current, id);
757
- }
758
- if ("then" in current && typeof current.then === "function") {
759
- return generatePromiseNode(ctx, current, id);
760
- }
761
- return {
762
- t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
763
- i: id,
764
- s: void 0,
765
- l: void 0,
766
- m: void 0,
767
- c: void 0,
768
- d: await serializePropertiesAsync(ctx, current),
769
- a: void 0,
770
- n: void 0
771
- };
772
- }
773
- async function generatePromiseNode(ctx, current, id) {
774
- assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
775
- return current.then(async (value) => ({
776
- t: 10 /* Promise */,
777
- i: id,
778
- s: void 0,
779
- l: void 0,
780
- m: void 0,
781
- c: void 0,
782
- // Parse options first before the items
783
- d: void 0,
784
- a: void 0,
785
- n: await generateTreeAsync(ctx, value)
786
- }));
787
- }
788
- async function generateTreeAsync(ctx, current) {
789
- switch (typeof current) {
790
- case "boolean":
791
- return current ? TRUE_NODE : FALSE_NODE;
792
- case "undefined":
793
- return UNDEFINED_NODE;
794
- case "string":
795
- return createPrimitiveNode(quote(current));
796
- case "number":
797
- if (Object.is(current, -0)) {
798
- return NEG_ZERO_NODE;
799
- }
800
- if (Object.is(current, Infinity)) {
801
- return createPrimitiveNode("1/0");
802
- }
803
- if (Object.is(current, -Infinity)) {
804
- return createPrimitiveNode("-1/0");
805
- }
806
- return createPrimitiveNode(current);
807
- case "bigint":
808
- return createBigIntNode(ctx, current);
809
- case "object": {
810
- if (!current) {
811
- return NULL_NODE;
812
- }
813
- const id = createRef(ctx, current, true);
814
- if (ctx.markedRefs[id]) {
815
- return createReferenceNode(id);
816
- }
817
- if (Array.isArray(current)) {
818
- return generateArrayNode(ctx, current, id);
819
- }
820
- switch (current.constructor) {
821
- case Date:
822
- return createDateNode(id, current);
823
- case RegExp:
824
- return createRegExpNode(id, current);
825
- case Promise:
826
- return generatePromiseNode(ctx, current, id);
827
- case Map:
828
- return generateMapNode(ctx, current, id);
829
- case Set:
830
- return generateSetNode(ctx, current, id);
831
- case Object:
832
- return generateObjectNode(ctx, current, id, false);
833
- case void 0:
834
- return generateObjectNode(ctx, current, id, true);
835
- case Int8Array:
836
- case Int16Array:
837
- case Int32Array:
838
- case Uint8Array:
839
- case Uint16Array:
840
- case Uint32Array:
841
- case Uint8ClampedArray:
842
- case Float32Array:
843
- case Float64Array:
844
- return createTypedArrayNode(ctx, id, current);
845
- case BigInt64Array:
846
- case BigUint64Array:
847
- return createBigIntTypedArrayNode(ctx, id, current);
848
- case AggregateError:
849
- if (ctx.features & 1 /* AggregateError */) {
850
- return generateAggregateErrorNode(ctx, current, id);
851
- }
852
- return generateErrorNode(ctx, current, id);
853
- case Error:
854
- case EvalError:
855
- case RangeError:
856
- case ReferenceError:
857
- case SyntaxError:
858
- case TypeError:
859
- case URIError:
860
- return generateErrorNode(ctx, current, id);
861
- default:
862
- break;
863
- }
864
- if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
865
- return generateAggregateErrorNode(ctx, current, id);
866
- }
867
- if (current instanceof Error) {
868
- return generateErrorNode(ctx, current, id);
869
- }
870
- if (current instanceof Promise) {
871
- return generatePromiseNode(ctx, current, id);
872
- }
873
- if (Symbol.iterator in current) {
874
- return generateIterableNode(ctx, current, id);
741
+ return new SerovalSetNode(id, nodes);
742
+ }
743
+ async generateProperties(properties) {
744
+ const keys = Object.keys(properties);
745
+ const size = keys.length;
746
+ const keyNodes = new Array(size);
747
+ const valueNodes = new Array(size);
748
+ const deferredKeys = new Array(size);
749
+ const deferredValues = new Array(size);
750
+ let deferredSize = 0;
751
+ let nodesSize = 0;
752
+ let item;
753
+ for (const key of keys) {
754
+ item = properties[key];
755
+ if (isIterable(item)) {
756
+ deferredKeys[deferredSize] = key;
757
+ deferredValues[deferredSize] = item;
758
+ deferredSize++;
759
+ } else {
760
+ keyNodes[nodesSize] = key;
761
+ valueNodes[nodesSize] = await this.parse(item);
762
+ nodesSize++;
875
763
  }
876
- if ("then" in current && typeof current.then === "function") {
877
- return generatePromiseNode(ctx, current, id);
764
+ }
765
+ for (let i = 0; i < deferredSize; i++) {
766
+ keyNodes[nodesSize + i] = deferredKeys[i];
767
+ valueNodes[nodesSize + i] = await this.parse(deferredValues[i]);
768
+ }
769
+ return new SerovalObjectRecordNode(keyNodes, valueNodes, size);
770
+ }
771
+ async generateIterableNode(id, current) {
772
+ assert(this.ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
773
+ const options = getIterableOptions(current);
774
+ return new SerovalIterableNode(
775
+ id,
776
+ // Parse options first before the items
777
+ options ? await this.generateProperties(options) : void 0,
778
+ await this.generateNodeList(Array.from(current))
779
+ );
780
+ }
781
+ async generatePromiseNode(id, current) {
782
+ assert(this.ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
783
+ return current.then(async (value) => new SerovalPromiseNode(id, await this.parse(value)));
784
+ }
785
+ async generateObjectNode(id, current, empty) {
786
+ if (Symbol.iterator in current) {
787
+ return this.generateIterableNode(id, current);
788
+ }
789
+ if ("then" in current && typeof current.then === "function") {
790
+ return this.generatePromiseNode(id, current);
791
+ }
792
+ const properties = await this.generateProperties(current);
793
+ if (empty) {
794
+ return new SerovalNullConstructorNode(id, properties);
795
+ }
796
+ return new SerovalObjectNode(id, properties);
797
+ }
798
+ async generateAggregateErrorNode(id, current) {
799
+ const options = getErrorOptions(current);
800
+ const optionsNode = options ? await this.generateProperties(options) : void 0;
801
+ return new SerovalAggregateErrorNode(
802
+ id,
803
+ current.message,
804
+ optionsNode,
805
+ await this.parse(current.errors)
806
+ );
807
+ }
808
+ async generateErrorNode(id, current) {
809
+ const options = getErrorOptions(current);
810
+ const optionsNode = options ? await this.generateProperties(options) : void 0;
811
+ return new SerovalErrorNode(id, getErrorConstructor(current), current.message, optionsNode);
812
+ }
813
+ async parse(current) {
814
+ switch (typeof current) {
815
+ case "boolean":
816
+ return current ? TRUE_NODE : FALSE_NODE;
817
+ case "undefined":
818
+ return UNDEFINED_NODE;
819
+ case "string":
820
+ return new SerovalPrimitiveNode(quote(current));
821
+ case "number":
822
+ if (Object.is(current, -0)) {
823
+ return NEG_ZERO_NODE;
824
+ }
825
+ if (Object.is(current, Infinity)) {
826
+ return INFINITY_NODE;
827
+ }
828
+ if (Object.is(current, -Infinity)) {
829
+ return NEG_INFINITY_NODE;
830
+ }
831
+ return new SerovalPrimitiveNode(current);
832
+ case "bigint":
833
+ assert(this.ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
834
+ return new SerovalBigIntNode(current);
835
+ case "object": {
836
+ if (!current) {
837
+ return NULL_NODE;
838
+ }
839
+ const id = createRef(this.ctx, current, true);
840
+ if (this.ctx.markedRefs[id]) {
841
+ return new SerovalReferenceNode(id);
842
+ }
843
+ if (Array.isArray(current)) {
844
+ return this.generateArrayNode(id, current);
845
+ }
846
+ switch (current.constructor) {
847
+ case Date:
848
+ return new SerovalDateNode(id, current);
849
+ case RegExp:
850
+ return new SerovalRegExpNode(id, current);
851
+ case Promise:
852
+ return this.generatePromiseNode(id, current);
853
+ case Int8Array:
854
+ case Int16Array:
855
+ case Int32Array:
856
+ case Uint8Array:
857
+ case Uint16Array:
858
+ case Uint32Array:
859
+ case Uint8ClampedArray:
860
+ case Float32Array:
861
+ case Float64Array:
862
+ assert(this.ctx.features & 1024 /* TypedArray */, `Unsupported value type "${current.constructor.name}"`);
863
+ return new SerovalTypedArrayNode(id, current);
864
+ case BigInt64Array:
865
+ case BigUint64Array:
866
+ assert(
867
+ this.ctx.features & 2048 /* BigIntTypedArray */,
868
+ `Unsupported value type "${current.constructor.name}"`
869
+ );
870
+ return new SerovalBigIntTypedArrayNode(id, current);
871
+ case Map:
872
+ return this.generateMapNode(id, current);
873
+ case Set:
874
+ return this.generateSetNode(id, current);
875
+ case Object:
876
+ return this.generateObjectNode(id, current, false);
877
+ case void 0:
878
+ return this.generateObjectNode(id, current, true);
879
+ case AggregateError:
880
+ if (this.ctx.features & 1 /* AggregateError */) {
881
+ return this.generateAggregateErrorNode(id, current);
882
+ }
883
+ return this.generateErrorNode(id, current);
884
+ case Error:
885
+ case EvalError:
886
+ case RangeError:
887
+ case ReferenceError:
888
+ case SyntaxError:
889
+ case TypeError:
890
+ case URIError:
891
+ return this.generateErrorNode(id, current);
892
+ default:
893
+ break;
894
+ }
895
+ if (current instanceof AggregateError && this.ctx.features & 1 /* AggregateError */) {
896
+ return this.generateAggregateErrorNode(id, current);
897
+ }
898
+ if (current instanceof Error) {
899
+ return this.generateErrorNode(id, current);
900
+ }
901
+ if (current instanceof Promise) {
902
+ return this.generatePromiseNode(id, current);
903
+ }
904
+ if (Symbol.iterator in current) {
905
+ return this.generateIterableNode(id, current);
906
+ }
907
+ if ("then" in current && typeof current.then === "function") {
908
+ return this.generatePromiseNode(id, current);
909
+ }
910
+ throw new Error("Unsupported type");
878
911
  }
879
- throw new Error("Unsupported type");
912
+ default:
913
+ throw new Error("Unsupported type");
880
914
  }
881
- default:
882
- throw new Error("Unsupported type");
883
915
  }
884
- }
916
+ };
885
917
  async function parseAsync(ctx, current) {
886
- const result = await generateTreeAsync(ctx, current);
918
+ const result = await new AsyncParser(ctx).parse(current);
887
919
  const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
888
920
  return [result, createRef(ctx, current, false), isObject];
889
921
  }
@@ -1270,291 +1302,245 @@ function serializeTree(ctx, node) {
1270
1302
  }
1271
1303
 
1272
1304
  // src/tree/sync.ts
1273
- function serializePropertiesSync(ctx, properties) {
1274
- const keys = Object.keys(properties);
1275
- const size = keys.length;
1276
- const keyNodes = new Array(size);
1277
- const valueNodes = new Array(size);
1278
- const deferredKeys = new Array(size);
1279
- const deferredValues = new Array(size);
1280
- let deferredSize = 0;
1281
- let nodesSize = 0;
1282
- let item;
1283
- for (const key of keys) {
1284
- item = properties[key];
1285
- if (isIterable(item)) {
1286
- deferredKeys[deferredSize] = key;
1287
- deferredValues[deferredSize] = item;
1288
- deferredSize++;
1289
- } else {
1290
- keyNodes[nodesSize] = key;
1291
- valueNodes[nodesSize] = generateTreeSync(ctx, item);
1292
- nodesSize++;
1305
+ var SyncParser = class {
1306
+ constructor(ctx) {
1307
+ __publicField(this, "ctx");
1308
+ this.ctx = ctx;
1309
+ }
1310
+ generateNodeList(current) {
1311
+ const size = current.length;
1312
+ const nodes = new Array(size);
1313
+ const deferred = new Array(size);
1314
+ let item;
1315
+ for (let i = 0; i < size; i++) {
1316
+ if (i in current) {
1317
+ item = current[i];
1318
+ if (isIterable(item)) {
1319
+ deferred[i] = item;
1320
+ } else {
1321
+ nodes[i] = this.parse(item);
1322
+ }
1323
+ }
1293
1324
  }
1294
- }
1295
- for (let i = 0; i < deferredSize; i++) {
1296
- keyNodes[nodesSize + i] = deferredKeys[i];
1297
- valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
1298
- }
1299
- return {
1300
- k: keyNodes,
1301
- v: valueNodes,
1302
- s: size
1303
- };
1304
- }
1305
- function generateSetNode2(ctx, current, id) {
1306
- assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
1307
- const len = current.size;
1308
- const nodes = new Array(len);
1309
- const deferred = new Array(len);
1310
- let deferredSize = 0;
1311
- let nodeSize = 0;
1312
- for (const item of current.keys()) {
1313
- if (isIterable(item)) {
1314
- deferred[deferredSize++] = item;
1315
- } else {
1316
- nodes[nodeSize++] = generateTreeSync(ctx, item);
1325
+ for (let i = 0; i < size; i++) {
1326
+ if (i in deferred) {
1327
+ nodes[i] = this.parse(deferred[i]);
1328
+ }
1317
1329
  }
1318
- }
1319
- for (let i = 0; i < deferredSize; i++) {
1320
- nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
1321
- }
1322
- return {
1323
- t: 5 /* Set */,
1324
- i: id,
1325
- a: nodes,
1326
- s: void 0,
1327
- l: void 0,
1328
- m: void 0,
1329
- c: void 0,
1330
- d: void 0,
1331
- n: void 0
1332
- };
1333
- }
1334
- function generateMapNode2(ctx, current, id) {
1335
- assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
1336
- const len = current.size;
1337
- const keyNodes = new Array(len);
1338
- const valueNodes = new Array(len);
1339
- const deferredKey = new Array(len);
1340
- const deferredValue = new Array(len);
1341
- let deferredSize = 0;
1342
- let nodeSize = 0;
1343
- for (const [key, value] of current.entries()) {
1344
- if (isIterable(key) || isIterable(value)) {
1345
- deferredKey[deferredSize] = key;
1346
- deferredValue[deferredSize] = value;
1347
- deferredSize++;
1348
- } else {
1349
- keyNodes[nodeSize] = generateTreeSync(ctx, key);
1350
- valueNodes[nodeSize] = generateTreeSync(ctx, value);
1351
- nodeSize++;
1330
+ return nodes;
1331
+ }
1332
+ generateArrayNode(id, current) {
1333
+ return new SerovalArrayNode(id, this.generateNodeList(current));
1334
+ }
1335
+ generateMapNode(id, current) {
1336
+ assert(this.ctx.features & 16 /* Map */, 'Unsupported type "Map"');
1337
+ const len = current.size;
1338
+ const keyNodes = new Array(len);
1339
+ const valueNodes = new Array(len);
1340
+ const deferredKey = new Array(len);
1341
+ const deferredValue = new Array(len);
1342
+ let deferredSize = 0;
1343
+ let nodeSize = 0;
1344
+ for (const [key, value] of current.entries()) {
1345
+ if (isIterable(key) || isIterable(value)) {
1346
+ deferredKey[deferredSize] = key;
1347
+ deferredValue[deferredSize] = value;
1348
+ deferredSize++;
1349
+ } else {
1350
+ keyNodes[nodeSize] = this.parse(key);
1351
+ valueNodes[nodeSize] = this.parse(value);
1352
+ nodeSize++;
1353
+ }
1352
1354
  }
1353
- }
1354
- for (let i = 0; i < deferredSize; i++) {
1355
- keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
1356
- valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
1357
- }
1358
- return {
1359
- t: 6 /* Map */,
1360
- i: id,
1361
- a: void 0,
1362
- s: void 0,
1363
- l: void 0,
1364
- m: void 0,
1365
- c: void 0,
1366
- d: { k: keyNodes, v: valueNodes, s: len },
1367
- n: void 0
1368
- };
1369
- }
1370
- function generateNodeList2(ctx, current) {
1371
- const size = current.length;
1372
- const nodes = new Array(size);
1373
- const deferred = new Array(size);
1374
- let item;
1375
- for (let i = 0; i < size; i++) {
1376
- if (i in current) {
1377
- item = current[i];
1355
+ for (let i = 0; i < deferredSize; i++) {
1356
+ keyNodes[nodeSize + i] = this.parse(deferredKey[i]);
1357
+ valueNodes[nodeSize + i] = this.parse(deferredValue[i]);
1358
+ }
1359
+ return new SerovalMapNode(id, new SerovalMapRecordNode(keyNodes, valueNodes, len));
1360
+ }
1361
+ generateSetNode(id, current) {
1362
+ assert(this.ctx.features & 256 /* Set */, 'Unsupported type "Set"');
1363
+ const len = current.size;
1364
+ const nodes = new Array(len);
1365
+ const deferred = new Array(len);
1366
+ let deferredSize = 0;
1367
+ let nodeSize = 0;
1368
+ for (const item of current.keys()) {
1378
1369
  if (isIterable(item)) {
1379
- deferred[i] = item;
1370
+ deferred[deferredSize++] = item;
1380
1371
  } else {
1381
- nodes[i] = generateTreeSync(ctx, item);
1372
+ nodes[nodeSize++] = this.parse(item);
1382
1373
  }
1383
1374
  }
1384
- }
1385
- for (let i = 0; i < size; i++) {
1386
- if (i in deferred) {
1387
- nodes[i] = generateTreeSync(ctx, deferred[i]);
1375
+ for (let i = 0; i < deferredSize; i++) {
1376
+ nodes[nodeSize + i] = this.parse(deferred[i]);
1388
1377
  }
1389
- }
1390
- return nodes;
1391
- }
1392
- function generateArrayNode2(ctx, current, id) {
1393
- return {
1394
- t: 7 /* Array */,
1395
- i: id,
1396
- a: generateNodeList2(ctx, current),
1397
- s: void 0,
1398
- l: void 0,
1399
- m: void 0,
1400
- c: void 0,
1401
- d: void 0,
1402
- n: void 0
1403
- };
1404
- }
1405
- function generateAggregateErrorNode2(ctx, current, id) {
1406
- const options = getErrorOptions(current);
1407
- const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1408
- return {
1409
- t: 12 /* AggregateError */,
1410
- i: id,
1411
- a: void 0,
1412
- s: void 0,
1413
- l: void 0,
1414
- m: current.message,
1415
- c: void 0,
1416
- d: optionsNode,
1417
- n: generateTreeSync(ctx, current.errors)
1418
- };
1419
- }
1420
- function generateErrorNode2(ctx, current, id) {
1421
- const options = getErrorOptions(current);
1422
- const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1423
- return {
1424
- t: 11 /* Error */,
1425
- i: id,
1426
- a: void 0,
1427
- s: void 0,
1428
- l: void 0,
1429
- m: current.message,
1430
- c: getErrorConstructor(current),
1431
- d: optionsNode,
1432
- n: void 0
1433
- };
1434
- }
1435
- function generateIterableNode2(ctx, current, id) {
1436
- assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
1437
- const options = getIterableOptions(current);
1438
- return {
1439
- t: 13 /* Iterable */,
1440
- i: id,
1441
- s: void 0,
1442
- l: void 0,
1443
- m: void 0,
1444
- c: void 0,
1445
- // Parse options first before the items
1446
- d: options ? serializePropertiesSync(ctx, options) : void 0,
1447
- a: generateNodeList2(ctx, Array.from(current)),
1448
- n: void 0
1449
- };
1450
- }
1451
- function generateObjectNode2(ctx, current, id, empty) {
1452
- if (Symbol.iterator in current) {
1453
- return generateIterableNode2(ctx, current, id);
1454
- }
1455
- return {
1456
- t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
1457
- i: id,
1458
- s: void 0,
1459
- l: void 0,
1460
- m: void 0,
1461
- c: void 0,
1462
- d: serializePropertiesSync(ctx, current),
1463
- a: void 0,
1464
- n: void 0
1465
- };
1466
- }
1467
- function generateTreeSync(ctx, current) {
1468
- switch (typeof current) {
1469
- case "boolean":
1470
- return current ? TRUE_NODE : FALSE_NODE;
1471
- case "undefined":
1472
- return UNDEFINED_NODE;
1473
- case "string":
1474
- return createPrimitiveNode(quote(current));
1475
- case "number":
1476
- if (Object.is(current, -0)) {
1477
- return NEG_ZERO_NODE;
1478
- }
1479
- if (Object.is(current, Infinity)) {
1480
- return createPrimitiveNode("1/0");
1481
- }
1482
- if (Object.is(current, -Infinity)) {
1483
- return createPrimitiveNode("-1/0");
1484
- }
1485
- return createPrimitiveNode(current);
1486
- case "bigint":
1487
- return createBigIntNode(ctx, current);
1488
- case "object": {
1489
- if (!current) {
1490
- return NULL_NODE;
1491
- }
1492
- const id = createRef(ctx, current, true);
1493
- if (ctx.markedRefs[id]) {
1494
- return createReferenceNode(id);
1495
- }
1496
- if (Array.isArray(current)) {
1497
- return generateArrayNode2(ctx, current, id);
1498
- }
1499
- switch (current.constructor) {
1500
- case Date:
1501
- return createDateNode(id, current);
1502
- case RegExp:
1503
- return createRegExpNode(id, current);
1504
- case Map:
1505
- return generateMapNode2(ctx, current, id);
1506
- case Set:
1507
- return generateSetNode2(ctx, current, id);
1508
- case Object:
1509
- return generateObjectNode2(ctx, current, id, false);
1510
- case void 0:
1511
- return generateObjectNode2(ctx, current, id, true);
1512
- case Int8Array:
1513
- case Int16Array:
1514
- case Int32Array:
1515
- case Uint8Array:
1516
- case Uint16Array:
1517
- case Uint32Array:
1518
- case Uint8ClampedArray:
1519
- case Float32Array:
1520
- case Float64Array:
1521
- return createTypedArrayNode(ctx, id, current);
1522
- case BigInt64Array:
1523
- case BigUint64Array:
1524
- return createBigIntTypedArrayNode(ctx, id, current);
1525
- case AggregateError:
1526
- if (ctx.features & 1 /* AggregateError */) {
1527
- return generateAggregateErrorNode2(ctx, current, id);
1528
- }
1529
- return generateErrorNode2(ctx, current, id);
1530
- case Error:
1531
- case EvalError:
1532
- case RangeError:
1533
- case ReferenceError:
1534
- case SyntaxError:
1535
- case TypeError:
1536
- case URIError:
1537
- return generateErrorNode2(ctx, current, id);
1538
- default:
1539
- break;
1540
- }
1541
- if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
1542
- return generateAggregateErrorNode2(ctx, current, id);
1543
- }
1544
- if (current instanceof Error) {
1545
- return generateErrorNode2(ctx, current, id);
1378
+ return new SerovalSetNode(id, nodes);
1379
+ }
1380
+ generateProperties(properties) {
1381
+ const keys = Object.keys(properties);
1382
+ const size = keys.length;
1383
+ const keyNodes = new Array(size);
1384
+ const valueNodes = new Array(size);
1385
+ const deferredKeys = new Array(size);
1386
+ const deferredValues = new Array(size);
1387
+ let deferredSize = 0;
1388
+ let nodesSize = 0;
1389
+ let item;
1390
+ for (const key of keys) {
1391
+ item = properties[key];
1392
+ if (isIterable(item)) {
1393
+ deferredKeys[deferredSize] = key;
1394
+ deferredValues[deferredSize] = item;
1395
+ deferredSize++;
1396
+ } else {
1397
+ keyNodes[nodesSize] = key;
1398
+ valueNodes[nodesSize] = this.parse(item);
1399
+ nodesSize++;
1546
1400
  }
1547
- if (Symbol.iterator in current) {
1548
- return generateIterableNode2(ctx, current, id);
1401
+ }
1402
+ for (let i = 0; i < deferredSize; i++) {
1403
+ keyNodes[nodesSize + i] = deferredKeys[i];
1404
+ valueNodes[nodesSize + i] = this.parse(deferredValues[i]);
1405
+ }
1406
+ return {
1407
+ k: keyNodes,
1408
+ v: valueNodes,
1409
+ s: size
1410
+ };
1411
+ }
1412
+ generateIterableNode(id, current) {
1413
+ assert(this.ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
1414
+ const options = getIterableOptions(current);
1415
+ return new SerovalIterableNode(
1416
+ id,
1417
+ // Parse options first before the items
1418
+ options ? this.generateProperties(options) : void 0,
1419
+ this.generateNodeList(Array.from(current))
1420
+ );
1421
+ }
1422
+ generateObjectNode(id, current, empty) {
1423
+ if (Symbol.iterator in current) {
1424
+ return this.generateIterableNode(id, current);
1425
+ }
1426
+ if (empty) {
1427
+ return new SerovalNullConstructorNode(id, this.generateProperties(current));
1428
+ }
1429
+ return new SerovalObjectNode(id, this.generateProperties(current));
1430
+ }
1431
+ generateAggregateErrorNode(id, current) {
1432
+ const options = getErrorOptions(current);
1433
+ const optionsNode = options ? this.generateProperties(options) : void 0;
1434
+ return new SerovalAggregateErrorNode(
1435
+ id,
1436
+ current.message,
1437
+ optionsNode,
1438
+ this.parse(current.errors)
1439
+ );
1440
+ }
1441
+ generateErrorNode(id, current) {
1442
+ const options = getErrorOptions(current);
1443
+ const optionsNode = options ? this.generateProperties(options) : void 0;
1444
+ return new SerovalErrorNode(id, getErrorConstructor(current), current.message, optionsNode);
1445
+ }
1446
+ parse(current) {
1447
+ switch (typeof current) {
1448
+ case "boolean":
1449
+ return current ? TRUE_NODE : FALSE_NODE;
1450
+ case "undefined":
1451
+ return UNDEFINED_NODE;
1452
+ case "string":
1453
+ return new SerovalPrimitiveNode(quote(current));
1454
+ case "number":
1455
+ if (Object.is(current, -0)) {
1456
+ return NEG_ZERO_NODE;
1457
+ }
1458
+ if (Object.is(current, Infinity)) {
1459
+ return INFINITY_NODE;
1460
+ }
1461
+ if (Object.is(current, -Infinity)) {
1462
+ return NEG_INFINITY_NODE;
1463
+ }
1464
+ return new SerovalPrimitiveNode(current);
1465
+ case "bigint":
1466
+ assert(this.ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
1467
+ return new SerovalBigIntNode(current);
1468
+ case "object": {
1469
+ if (!current) {
1470
+ return NULL_NODE;
1471
+ }
1472
+ const id = createRef(this.ctx, current, true);
1473
+ if (this.ctx.markedRefs[id]) {
1474
+ return new SerovalReferenceNode(id);
1475
+ }
1476
+ if (Array.isArray(current)) {
1477
+ return this.generateArrayNode(id, current);
1478
+ }
1479
+ switch (current.constructor) {
1480
+ case Date:
1481
+ return new SerovalDateNode(id, current);
1482
+ case RegExp:
1483
+ return new SerovalRegExpNode(id, current);
1484
+ case Int8Array:
1485
+ case Int16Array:
1486
+ case Int32Array:
1487
+ case Uint8Array:
1488
+ case Uint16Array:
1489
+ case Uint32Array:
1490
+ case Uint8ClampedArray:
1491
+ case Float32Array:
1492
+ case Float64Array:
1493
+ assert(this.ctx.features & 1024 /* TypedArray */, `Unsupported value type "${current.constructor.name}"`);
1494
+ return new SerovalTypedArrayNode(id, current);
1495
+ case BigInt64Array:
1496
+ case BigUint64Array:
1497
+ assert(
1498
+ this.ctx.features & 2048 /* BigIntTypedArray */,
1499
+ `Unsupported value type "${current.constructor.name}"`
1500
+ );
1501
+ return new SerovalBigIntTypedArrayNode(id, current);
1502
+ case Map:
1503
+ return this.generateMapNode(id, current);
1504
+ case Set:
1505
+ return this.generateSetNode(id, current);
1506
+ case Object:
1507
+ return this.generateObjectNode(id, current, false);
1508
+ case void 0:
1509
+ return this.generateObjectNode(id, current, true);
1510
+ case AggregateError:
1511
+ if (this.ctx.features & 1 /* AggregateError */) {
1512
+ return this.generateAggregateErrorNode(id, current);
1513
+ }
1514
+ return this.generateErrorNode(id, current);
1515
+ case Error:
1516
+ case EvalError:
1517
+ case RangeError:
1518
+ case ReferenceError:
1519
+ case SyntaxError:
1520
+ case TypeError:
1521
+ case URIError:
1522
+ return this.generateErrorNode(id, current);
1523
+ default:
1524
+ break;
1525
+ }
1526
+ if (current instanceof AggregateError && this.ctx.features & 1 /* AggregateError */) {
1527
+ return this.generateAggregateErrorNode(id, current);
1528
+ }
1529
+ if (current instanceof Error) {
1530
+ return this.generateErrorNode(id, current);
1531
+ }
1532
+ if (Symbol.iterator in current) {
1533
+ return this.generateIterableNode(id, current);
1534
+ }
1535
+ throw new Error("Unsupported type");
1549
1536
  }
1550
- throw new Error("Unsupported type");
1537
+ default:
1538
+ throw new Error("Unsupported type");
1551
1539
  }
1552
- default:
1553
- throw new Error("Unsupported type");
1554
1540
  }
1555
- }
1541
+ };
1556
1542
  function parseSync(ctx, current) {
1557
- const result = generateTreeSync(ctx, current);
1543
+ const result = new SyncParser(ctx).parse(current);
1558
1544
  const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
1559
1545
  return [result, createRef(ctx, current, false), isObject];
1560
1546
  }
@@ -1585,16 +1571,16 @@ function finalize(ctx, rootID, isObject, result) {
1585
1571
  return result;
1586
1572
  }
1587
1573
  function serialize(source, options) {
1588
- const ctx = createParserContext(options);
1574
+ const ctx = new ParserContext(options);
1589
1575
  const [tree, rootID, isObject] = parseSync(ctx, source);
1590
- const serial = createSerializationContext(ctx);
1576
+ const serial = new SerializationContext(ctx);
1591
1577
  const result = serializeTree(serial, tree);
1592
1578
  return finalize(serial, rootID, isObject, result);
1593
1579
  }
1594
1580
  async function serializeAsync(source, options) {
1595
- const ctx = createParserContext(options);
1581
+ const ctx = new ParserContext(options);
1596
1582
  const [tree, rootID, isObject] = await parseAsync(ctx, source);
1597
- const serial = createSerializationContext(ctx);
1583
+ const serial = new SerializationContext(ctx);
1598
1584
  const result = serializeTree(serial, tree);
1599
1585
  return finalize(serial, rootID, isObject, result);
1600
1586
  }
@@ -1602,7 +1588,7 @@ function deserialize(source) {
1602
1588
  return (0, eval)(source);
1603
1589
  }
1604
1590
  function toJSON(source, options) {
1605
- const ctx = createParserContext(options);
1591
+ const ctx = new ParserContext(options);
1606
1592
  const [tree, root, isObject] = parseSync(ctx, source);
1607
1593
  return JSON.stringify([
1608
1594
  tree,
@@ -1613,7 +1599,7 @@ function toJSON(source, options) {
1613
1599
  ]);
1614
1600
  }
1615
1601
  async function toJSONAsync(source, options) {
1616
- const ctx = createParserContext(options);
1602
+ const ctx = new ParserContext(options);
1617
1603
  const [tree, root, isObject] = await parseAsync(ctx, source);
1618
1604
  return JSON.stringify([
1619
1605
  tree,
@@ -1625,7 +1611,7 @@ async function toJSONAsync(source, options) {
1625
1611
  }
1626
1612
  function compileJSON(source) {
1627
1613
  const parsed = JSON.parse(source);
1628
- const serial = createSerializationContext({
1614
+ const serial = new SerializationContext({
1629
1615
  features: parsed[3],
1630
1616
  markedRefs: parsed[4]
1631
1617
  });