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