seroval 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist/cjs/development/index.cjs +553 -350
  2. package/dist/cjs/development/index.cjs.map +3 -3
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +553 -350
  5. package/dist/esm/development/index.mjs.map +3 -3
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/core/base-primitives.d.ts +2 -2
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +2 -1
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +2 -2
  12. package/dist/types/core/constants.d.ts.map +1 -1
  13. package/dist/types/core/constructors.d.ts +1 -1
  14. package/dist/types/core/constructors.d.ts.map +1 -1
  15. package/dist/types/core/context/async-parser.d.ts +5 -4
  16. package/dist/types/core/context/async-parser.d.ts.map +1 -1
  17. package/dist/types/core/context/deserializer.d.ts +7 -2
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts +3 -1
  20. package/dist/types/core/context/parser.d.ts.map +1 -1
  21. package/dist/types/core/context/serializer.d.ts.map +1 -1
  22. package/dist/types/core/context/sync-parser.d.ts +5 -5
  23. package/dist/types/core/context/sync-parser.d.ts.map +1 -1
  24. package/dist/types/core/cross/index.d.ts.map +1 -1
  25. package/dist/types/core/errors.d.ts +9 -0
  26. package/dist/types/core/errors.d.ts.map +1 -1
  27. package/dist/types/core/node.d.ts +1 -1
  28. package/dist/types/core/string.d.ts.map +1 -1
  29. package/dist/types/core/tree/index.d.ts +4 -1
  30. package/dist/types/core/tree/index.d.ts.map +1 -1
  31. package/dist/types/core/types.d.ts +6 -11
  32. package/dist/types/core/types.d.ts.map +1 -1
  33. package/package.json +1 -1
  34. package/src/core/base-primitives.ts +17 -18
  35. package/src/core/compat.ts +3 -1
  36. package/src/core/constants.ts +18 -4
  37. package/src/core/constructors.ts +2 -1
  38. package/src/core/context/async-parser.ts +102 -50
  39. package/src/core/context/deserializer.ts +269 -105
  40. package/src/core/context/parser.ts +12 -10
  41. package/src/core/context/serializer.ts +31 -36
  42. package/src/core/context/sync-parser.ts +149 -75
  43. package/src/core/cross/index.ts +2 -0
  44. package/src/core/errors.ts +33 -0
  45. package/src/core/node.ts +2 -2
  46. package/src/core/string.ts +3 -1
  47. package/src/core/tree/index.ts +10 -1
  48. package/src/core/types.ts +10 -19
@@ -5,9 +5,10 @@ var Feature = /* @__PURE__ */ ((Feature2) => {
5
5
  Feature2[Feature2["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
6
6
  Feature2[Feature2["ObjectAssign"] = 8] = "ObjectAssign";
7
7
  Feature2[Feature2["BigIntTypedArray"] = 16] = "BigIntTypedArray";
8
+ Feature2[Feature2["RegExp"] = 32] = "RegExp";
8
9
  return Feature2;
9
10
  })(Feature || {});
10
- var ALL_ENABLED = 1 /* AggregateError */ | 2 /* ArrowFunction */ | 4 /* ErrorPrototypeStack */ | 8 /* ObjectAssign */ | 16 /* BigIntTypedArray */;
11
+ var ALL_ENABLED = 1 /* AggregateError */ | 2 /* ArrowFunction */ | 4 /* ErrorPrototypeStack */ | 8 /* ObjectAssign */ | 16 /* BigIntTypedArray */ | 32 /* RegExp */;
11
12
 
12
13
  // src/core/symbols.ts
13
14
  var SYM_ASYNC_ITERATOR = Symbol.asyncIterator;
@@ -80,10 +81,11 @@ var CONSTANT_STRING = {
80
81
  [6 /* NegInf */]: "-1/0",
81
82
  [7 /* Nan */]: "0/0"
82
83
  };
84
+ var NIL = void 0;
83
85
  var CONSTANT_VAL = {
84
86
  [2 /* True */]: true,
85
87
  [3 /* False */]: false,
86
- [1 /* Undefined */]: void 0,
88
+ [1 /* Undefined */]: NIL,
87
89
  [0 /* Null */]: null,
88
90
  [4 /* NegZero */]: -0,
89
91
  [5 /* Inf */]: Number.POSITIVE_INFINITY,
@@ -108,15 +110,13 @@ var ERROR_CONSTRUCTOR = {
108
110
  [5 /* TypeError */]: TypeError,
109
111
  [6 /* URIError */]: URIError
110
112
  };
111
- var NIL = void 0;
112
113
 
113
114
  // src/core/node.ts
114
- function createSerovalNode(t, i, s, l, c, m, p, e, a, f, b, o) {
115
+ function createSerovalNode(t, i, s, c, m, p, e, a, f, b, o, l) {
115
116
  return {
116
117
  t,
117
118
  i,
118
119
  s,
119
- l,
120
120
  c,
121
121
  m,
122
122
  p,
@@ -124,7 +124,8 @@ function createSerovalNode(t, i, s, l, c, m, p, e, a, f, b, o) {
124
124
  a,
125
125
  f,
126
126
  b,
127
- o
127
+ o,
128
+ l
128
129
  };
129
130
  }
130
131
 
@@ -192,7 +193,7 @@ function serializeChar(str) {
192
193
  case "\u2029":
193
194
  return "\\u2029";
194
195
  default:
195
- return void 0;
196
+ return NIL;
196
197
  }
197
198
  }
198
199
  function serializeString(str) {
@@ -480,7 +481,6 @@ function createRegExpNode(id, current) {
480
481
  6 /* RegExp */,
481
482
  id,
482
483
  NIL,
483
- NIL,
484
484
  serializeString(current.source),
485
485
  current.flags,
486
486
  NIL,
@@ -488,6 +488,7 @@ function createRegExpNode(id, current) {
488
488
  NIL,
489
489
  NIL,
490
490
  NIL,
491
+ NIL,
491
492
  NIL
492
493
  );
493
494
  }
@@ -528,7 +529,6 @@ function createPluginNode(id, tag, value) {
528
529
  25 /* Plugin */,
529
530
  id,
530
531
  value,
531
- NIL,
532
532
  serializeString(tag),
533
533
  NIL,
534
534
  NIL,
@@ -536,6 +536,7 @@ function createPluginNode(id, tag, value) {
536
536
  NIL,
537
537
  NIL,
538
538
  NIL,
539
+ NIL,
539
540
  NIL
540
541
  );
541
542
  }
@@ -544,7 +545,6 @@ function createArrayNode(id, current, parsedItems) {
544
545
  9 /* Array */,
545
546
  id,
546
547
  NIL,
547
- current.length,
548
548
  NIL,
549
549
  NIL,
550
550
  NIL,
@@ -552,7 +552,8 @@ function createArrayNode(id, current, parsedItems) {
552
552
  parsedItems,
553
553
  NIL,
554
554
  NIL,
555
- getObjectFlag(current)
555
+ getObjectFlag(current),
556
+ NIL
556
557
  );
557
558
  }
558
559
  function createBoxedNode(id, boxed) {
@@ -565,9 +566,9 @@ function createBoxedNode(id, boxed) {
565
566
  NIL,
566
567
  NIL,
567
568
  NIL,
568
- NIL,
569
569
  boxed,
570
570
  NIL,
571
+ NIL,
571
572
  NIL
572
573
  );
573
574
  }
@@ -576,7 +577,6 @@ function createTypedArrayNode(id, current, buffer) {
576
577
  15 /* TypedArray */,
577
578
  id,
578
579
  NIL,
579
- current.length,
580
580
  current.constructor.name,
581
581
  NIL,
582
582
  NIL,
@@ -584,7 +584,8 @@ function createTypedArrayNode(id, current, buffer) {
584
584
  NIL,
585
585
  buffer,
586
586
  current.byteOffset,
587
- NIL
587
+ NIL,
588
+ current.length
588
589
  );
589
590
  }
590
591
  function createBigIntTypedArrayNode(id, current, buffer) {
@@ -592,7 +593,6 @@ function createBigIntTypedArrayNode(id, current, buffer) {
592
593
  16 /* BigIntTypedArray */,
593
594
  id,
594
595
  NIL,
595
- current.length,
596
596
  current.constructor.name,
597
597
  NIL,
598
598
  NIL,
@@ -600,7 +600,8 @@ function createBigIntTypedArrayNode(id, current, buffer) {
600
600
  NIL,
601
601
  buffer,
602
602
  current.byteOffset,
603
- NIL
603
+ NIL,
604
+ current.byteLength
604
605
  );
605
606
  }
606
607
  function createDataViewNode(id, current, buffer) {
@@ -608,7 +609,6 @@ function createDataViewNode(id, current, buffer) {
608
609
  20 /* DataView */,
609
610
  id,
610
611
  NIL,
611
- current.byteLength,
612
612
  NIL,
613
613
  NIL,
614
614
  NIL,
@@ -616,7 +616,8 @@ function createDataViewNode(id, current, buffer) {
616
616
  NIL,
617
617
  buffer,
618
618
  current.byteOffset,
619
- NIL
619
+ NIL,
620
+ current.byteLength
620
621
  );
621
622
  }
622
623
  function createErrorNode(id, current, options) {
@@ -625,13 +626,13 @@ function createErrorNode(id, current, options) {
625
626
  id,
626
627
  getErrorConstructor(current),
627
628
  NIL,
628
- NIL,
629
629
  serializeString(current.message),
630
630
  options,
631
631
  NIL,
632
632
  NIL,
633
633
  NIL,
634
634
  NIL,
635
+ NIL,
635
636
  NIL
636
637
  );
637
638
  }
@@ -641,22 +642,21 @@ function createAggregateErrorNode(id, current, options) {
641
642
  id,
642
643
  getErrorConstructor(current),
643
644
  NIL,
644
- NIL,
645
645
  serializeString(current.message),
646
646
  options,
647
647
  NIL,
648
648
  NIL,
649
649
  NIL,
650
650
  NIL,
651
+ NIL,
651
652
  NIL
652
653
  );
653
654
  }
654
- function createSetNode(id, size, items) {
655
+ function createSetNode(id, items) {
655
656
  return createSerovalNode(
656
657
  7 /* Set */,
657
658
  id,
658
659
  NIL,
659
- size,
660
660
  NIL,
661
661
  NIL,
662
662
  NIL,
@@ -664,6 +664,7 @@ function createSetNode(id, size, items) {
664
664
  items,
665
665
  NIL,
666
666
  NIL,
667
+ NIL,
667
668
  NIL
668
669
  );
669
670
  }
@@ -676,10 +677,10 @@ function createIteratorFactoryInstanceNode(factory, items) {
676
677
  NIL,
677
678
  NIL,
678
679
  NIL,
679
- NIL,
680
680
  [factory, items],
681
681
  NIL,
682
682
  NIL,
683
+ NIL,
683
684
  NIL
684
685
  );
685
686
  }
@@ -692,10 +693,10 @@ function createAsyncIteratorFactoryInstanceNode(factory, items) {
692
693
  NIL,
693
694
  NIL,
694
695
  NIL,
695
- NIL,
696
696
  [factory, items],
697
697
  NIL,
698
698
  NIL,
699
+ NIL,
699
700
  NIL
700
701
  );
701
702
  }
@@ -708,10 +709,10 @@ function createStreamConstructorNode(id, factory, sequence) {
708
709
  NIL,
709
710
  NIL,
710
711
  NIL,
711
- NIL,
712
712
  sequence,
713
713
  factory,
714
714
  NIL,
715
+ NIL,
715
716
  NIL
716
717
  );
717
718
  }
@@ -725,9 +726,9 @@ function createStreamNextNode(id, parsed) {
725
726
  NIL,
726
727
  NIL,
727
728
  NIL,
728
- NIL,
729
729
  parsed,
730
730
  NIL,
731
+ NIL,
731
732
  NIL
732
733
  );
733
734
  }
@@ -741,9 +742,9 @@ function createStreamThrowNode(id, parsed) {
741
742
  NIL,
742
743
  NIL,
743
744
  NIL,
744
- NIL,
745
745
  parsed,
746
746
  NIL,
747
+ NIL,
747
748
  NIL
748
749
  );
749
750
  }
@@ -757,9 +758,9 @@ function createStreamReturnNode(id, parsed) {
757
758
  NIL,
758
759
  NIL,
759
760
  NIL,
760
- NIL,
761
761
  parsed,
762
762
  NIL,
763
+ NIL,
763
764
  NIL
764
765
  );
765
766
  }
@@ -859,6 +860,27 @@ var SerovalUnknownTypedArrayError = class extends Error {
859
860
  );
860
861
  }
861
862
  };
863
+ var SerovalMalformedNodeError = class extends Error {
864
+ constructor(node) {
865
+ super(
866
+ false ? getSpecificErrorMessage(8 /* MalformedNode */) : 'Malformed node type "' + node.t + '".'
867
+ );
868
+ }
869
+ };
870
+ var SerovalConflictedNodeIdError = class extends Error {
871
+ constructor(node) {
872
+ super(
873
+ false ? getSpecificErrorMessage(9 /* ConflictedNodeId */) : 'Conflicted node id "' + node.i + '".'
874
+ );
875
+ }
876
+ };
877
+ var SerovalDepthLimitError = class extends Error {
878
+ constructor(limit) {
879
+ super(
880
+ false ? getSpecificErrorMessage(9 /* ConflictedNodeId */) : "Depth limit of " + limit + " reached"
881
+ );
882
+ }
883
+ };
862
884
 
863
885
  // src/core/opaque-reference.ts
864
886
  var OpaqueReference = class {
@@ -1068,8 +1090,9 @@ var ASYNC_ITERATOR_CONSTRUCTOR = (symbol, createPromise) => (stream) => () => {
1068
1090
  return instance;
1069
1091
  };
1070
1092
  var SERIALIZED_ASYNC_ITERATOR_CONSTRUCTOR = /* @__PURE__ */ ASYNC_ITERATOR_CONSTRUCTOR.toString();
1071
- var ARRAY_BUFFER_CONSTRUCTOR = (length, b64) => {
1093
+ var ARRAY_BUFFER_CONSTRUCTOR = (b64) => {
1072
1094
  const decoded = atob(b64);
1095
+ const length = decoded.length;
1073
1096
  const arr = new Uint8Array(length);
1074
1097
  for (let i = 0; i < length; i++) {
1075
1098
  arr[i] = decoded.charCodeAt(i);
@@ -1181,7 +1204,8 @@ function createBaseParserContext(mode, options) {
1181
1204
  mode,
1182
1205
  marked: /* @__PURE__ */ new Set(),
1183
1206
  features: ALL_ENABLED ^ (options.disabledFeatures || 0),
1184
- refs: options.refs || /* @__PURE__ */ new Map()
1207
+ refs: options.refs || /* @__PURE__ */ new Map(),
1208
+ depthLimit: options.depthLimit || 1e3
1185
1209
  };
1186
1210
  }
1187
1211
  function markParserRef(ctx, id) {
@@ -1263,9 +1287,9 @@ function parseIteratorFactory(ctx) {
1263
1287
  NIL,
1264
1288
  NIL,
1265
1289
  NIL,
1266
- NIL,
1267
1290
  parseWellKnownSymbol(ctx, SYM_ITERATOR),
1268
1291
  NIL,
1292
+ NIL,
1269
1293
  NIL
1270
1294
  );
1271
1295
  }
@@ -1282,13 +1306,13 @@ function parseAsyncIteratorFactory(ctx) {
1282
1306
  NIL,
1283
1307
  NIL,
1284
1308
  NIL,
1285
- NIL,
1286
1309
  [
1287
1310
  parseSpecialReference(ctx, 1 /* PromiseConstructor */),
1288
1311
  parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR)
1289
1312
  ],
1290
1313
  NIL,
1291
1314
  NIL,
1315
+ NIL,
1292
1316
  NIL
1293
1317
  );
1294
1318
  }
@@ -1299,16 +1323,16 @@ function createObjectNode(id, current, empty, record) {
1299
1323
  NIL,
1300
1324
  NIL,
1301
1325
  NIL,
1302
- NIL,
1303
1326
  record,
1304
1327
  NIL,
1305
1328
  NIL,
1306
1329
  NIL,
1307
1330
  NIL,
1308
- getObjectFlag(current)
1331
+ getObjectFlag(current),
1332
+ NIL
1309
1333
  );
1310
1334
  }
1311
- function createMapNode(ctx, id, k, v, s) {
1335
+ function createMapNode(ctx, id, k, v) {
1312
1336
  return createSerovalNode(
1313
1337
  8 /* Map */,
1314
1338
  id,
@@ -1316,11 +1340,11 @@ function createMapNode(ctx, id, k, v, s) {
1316
1340
  NIL,
1317
1341
  NIL,
1318
1342
  NIL,
1319
- NIL,
1320
- { k, v, s },
1343
+ { k, v },
1321
1344
  NIL,
1322
1345
  parseSpecialReference(ctx, 0 /* MapSentinel */),
1323
1346
  NIL,
1347
+ NIL,
1324
1348
  NIL
1325
1349
  );
1326
1350
  }
@@ -1334,24 +1358,22 @@ function createPromiseConstructorNode(ctx, id, resolver) {
1334
1358
  NIL,
1335
1359
  NIL,
1336
1360
  NIL,
1337
- NIL,
1338
1361
  parseSpecialReference(ctx, 1 /* PromiseConstructor */),
1339
1362
  NIL,
1363
+ NIL,
1340
1364
  NIL
1341
1365
  );
1342
1366
  }
1343
1367
  function createArrayBufferNode(ctx, id, current) {
1344
1368
  const bytes = new Uint8Array(current);
1345
- const len = bytes.length;
1346
1369
  let result = "";
1347
- for (let i = 0; i < len; i++) {
1370
+ for (let i = 0, len = bytes.length; i < len; i++) {
1348
1371
  result += String.fromCharCode(bytes[i]);
1349
1372
  }
1350
1373
  return createSerovalNode(
1351
1374
  19 /* ArrayBuffer */,
1352
1375
  id,
1353
1376
  serializeString(btoa(result)),
1354
- len,
1355
1377
  NIL,
1356
1378
  NIL,
1357
1379
  NIL,
@@ -1359,6 +1381,7 @@ function createArrayBufferNode(ctx, id, current) {
1359
1381
  NIL,
1360
1382
  parseSpecialReference(ctx, 5 /* ArrayBufferConstructor */),
1361
1383
  NIL,
1384
+ NIL,
1362
1385
  NIL
1363
1386
  );
1364
1387
  }
@@ -1371,32 +1394,35 @@ function createAsyncParserContext(mode, options) {
1371
1394
  };
1372
1395
  }
1373
1396
  var AsyncParsePluginContext = class {
1374
- constructor(_p) {
1397
+ constructor(_p, depth) {
1375
1398
  this._p = _p;
1399
+ this.depth = depth;
1376
1400
  }
1377
1401
  parse(current) {
1378
- return parseAsync(this._p, current);
1402
+ return parseAsync(this._p, this.depth, current);
1379
1403
  }
1380
1404
  };
1381
- async function parseItems(ctx, current) {
1405
+ async function parseItems(ctx, depth, current) {
1382
1406
  const nodes = [];
1383
1407
  for (let i = 0, len = current.length; i < len; i++) {
1384
1408
  if (i in current) {
1385
- nodes[i] = await parseAsync(ctx, current[i]);
1409
+ nodes[i] = await parseAsync(ctx, depth, current[i]);
1410
+ } else {
1411
+ nodes[i] = 0;
1386
1412
  }
1387
1413
  }
1388
1414
  return nodes;
1389
1415
  }
1390
- async function parseArray(ctx, id, current) {
1391
- return createArrayNode(id, current, await parseItems(ctx, current));
1416
+ async function parseArray(ctx, depth, id, current) {
1417
+ return createArrayNode(id, current, await parseItems(ctx, depth, current));
1392
1418
  }
1393
- async function parseProperties(ctx, properties) {
1419
+ async function parseProperties(ctx, depth, properties) {
1394
1420
  const entries = Object.entries(properties);
1395
1421
  const keyNodes = [];
1396
1422
  const valueNodes = [];
1397
1423
  for (let i = 0, len = entries.length; i < len; i++) {
1398
1424
  keyNodes.push(serializeString(entries[i][0]));
1399
- valueNodes.push(await parseAsync(ctx, entries[i][1]));
1425
+ valueNodes.push(await parseAsync(ctx, depth, entries[i][1]));
1400
1426
  }
1401
1427
  if (SYM_ITERATOR in properties) {
1402
1428
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
@@ -1405,6 +1431,7 @@ async function parseProperties(ctx, properties) {
1405
1431
  parseIteratorFactory(ctx.base),
1406
1432
  await parseAsync(
1407
1433
  ctx,
1434
+ depth,
1408
1435
  iteratorToSequence(properties)
1409
1436
  )
1410
1437
  )
@@ -1417,6 +1444,7 @@ async function parseProperties(ctx, properties) {
1417
1444
  parseAsyncIteratorFactory(ctx.base),
1418
1445
  await parseAsync(
1419
1446
  ctx,
1447
+ depth,
1420
1448
  createStreamFromAsyncIterable(
1421
1449
  properties
1422
1450
  )
@@ -1436,92 +1464,96 @@ async function parseProperties(ctx, properties) {
1436
1464
  }
1437
1465
  return {
1438
1466
  k: keyNodes,
1439
- v: valueNodes,
1440
- s: keyNodes.length
1467
+ v: valueNodes
1441
1468
  };
1442
1469
  }
1443
- async function parsePlainObject(ctx, id, current, empty) {
1470
+ async function parsePlainObject(ctx, depth, id, current, empty) {
1444
1471
  return createObjectNode(
1445
1472
  id,
1446
1473
  current,
1447
1474
  empty,
1448
- await parseProperties(ctx, current)
1475
+ await parseProperties(ctx, depth, current)
1449
1476
  );
1450
1477
  }
1451
- async function parseBoxed(ctx, id, current) {
1452
- return createBoxedNode(id, await parseAsync(ctx, current.valueOf()));
1478
+ async function parseBoxed(ctx, depth, id, current) {
1479
+ return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1453
1480
  }
1454
- async function parseTypedArray(ctx, id, current) {
1481
+ async function parseTypedArray(ctx, depth, id, current) {
1455
1482
  return createTypedArrayNode(
1456
1483
  id,
1457
1484
  current,
1458
- await parseAsync(ctx, current.buffer)
1485
+ await parseAsync(ctx, depth, current.buffer)
1459
1486
  );
1460
1487
  }
1461
- async function parseBigIntTypedArray(ctx, id, current) {
1488
+ async function parseBigIntTypedArray(ctx, depth, id, current) {
1462
1489
  return createBigIntTypedArrayNode(
1463
1490
  id,
1464
1491
  current,
1465
- await parseAsync(ctx, current.buffer)
1492
+ await parseAsync(ctx, depth, current.buffer)
1466
1493
  );
1467
1494
  }
1468
- async function parseDataView(ctx, id, current) {
1469
- return createDataViewNode(id, current, await parseAsync(ctx, current.buffer));
1495
+ async function parseDataView(ctx, depth, id, current) {
1496
+ return createDataViewNode(
1497
+ id,
1498
+ current,
1499
+ await parseAsync(ctx, depth, current.buffer)
1500
+ );
1470
1501
  }
1471
- async function parseError(ctx, id, current) {
1502
+ async function parseError(ctx, depth, id, current) {
1472
1503
  const options = getErrorOptions(current, ctx.base.features);
1473
1504
  return createErrorNode(
1474
1505
  id,
1475
1506
  current,
1476
- options ? await parseProperties(ctx, options) : NIL
1507
+ options ? await parseProperties(ctx, depth, options) : NIL
1477
1508
  );
1478
1509
  }
1479
- async function parseAggregateError(ctx, id, current) {
1510
+ async function parseAggregateError(ctx, depth, id, current) {
1480
1511
  const options = getErrorOptions(current, ctx.base.features);
1481
1512
  return createAggregateErrorNode(
1482
1513
  id,
1483
1514
  current,
1484
- options ? await parseProperties(ctx, options) : NIL
1515
+ options ? await parseProperties(ctx, depth, options) : NIL
1485
1516
  );
1486
1517
  }
1487
- async function parseMap(ctx, id, current) {
1518
+ async function parseMap(ctx, depth, id, current) {
1488
1519
  const keyNodes = [];
1489
1520
  const valueNodes = [];
1490
1521
  for (const [key, value] of current.entries()) {
1491
- keyNodes.push(await parseAsync(ctx, key));
1492
- valueNodes.push(await parseAsync(ctx, value));
1522
+ keyNodes.push(await parseAsync(ctx, depth, key));
1523
+ valueNodes.push(await parseAsync(ctx, depth, value));
1493
1524
  }
1494
- return createMapNode(ctx.base, id, keyNodes, valueNodes, current.size);
1525
+ return createMapNode(ctx.base, id, keyNodes, valueNodes);
1495
1526
  }
1496
- async function parseSet(ctx, id, current) {
1527
+ async function parseSet(ctx, depth, id, current) {
1497
1528
  const items = [];
1498
1529
  for (const item of current.keys()) {
1499
- items.push(await parseAsync(ctx, item));
1530
+ items.push(await parseAsync(ctx, depth, item));
1500
1531
  }
1501
- return createSetNode(id, current.size, items);
1532
+ return createSetNode(id, items);
1502
1533
  }
1503
- async function parsePlugin(ctx, id, current) {
1534
+ async function parsePlugin(ctx, depth, id, current) {
1504
1535
  const currentPlugins = ctx.base.plugins;
1505
1536
  if (currentPlugins) {
1506
1537
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
1507
1538
  const plugin = currentPlugins[i];
1508
1539
  if (plugin.parse.async && plugin.test(current)) {
1509
- if (ctx.child == null) {
1510
- ctx.child = new AsyncParsePluginContext(ctx);
1511
- }
1512
1540
  return createPluginNode(
1513
1541
  id,
1514
1542
  plugin.tag,
1515
- await plugin.parse.async(current, ctx.child, {
1516
- id
1517
- })
1543
+ await plugin.parse.async(
1544
+ current,
1545
+ new AsyncParsePluginContext(ctx, depth),
1546
+ {
1547
+ id
1548
+ }
1549
+ )
1518
1550
  );
1519
1551
  }
1520
1552
  }
1521
1553
  }
1522
1554
  return NIL;
1523
1555
  }
1524
- async function parsePromise(ctx, id, current) {
1556
+ async function parsePromise(ctx, depth, id, current) {
1525
1557
  const [status, result] = await promiseToResult(current);
1526
1558
  return createSerovalNode(
1527
1559
  12 /* Promise */,
@@ -1532,18 +1564,18 @@ async function parsePromise(ctx, id, current) {
1532
1564
  NIL,
1533
1565
  NIL,
1534
1566
  NIL,
1567
+ await parseAsync(ctx, depth, result),
1535
1568
  NIL,
1536
- await parseAsync(ctx, result),
1537
1569
  NIL,
1538
1570
  NIL
1539
1571
  );
1540
1572
  }
1541
- function parseStreamHandle(id, current, resolve, reject) {
1573
+ function parseStreamHandle(depth, id, current, resolve, reject) {
1542
1574
  const sequence = [];
1543
1575
  const cleanup = current.on({
1544
1576
  next: (value) => {
1545
1577
  markParserRef(this.base, id);
1546
- parseAsync(this, value).then(
1578
+ parseAsync(this, depth, value).then(
1547
1579
  (data) => {
1548
1580
  sequence.push(createStreamNextNode(id, data));
1549
1581
  },
@@ -1555,7 +1587,7 @@ function parseStreamHandle(id, current, resolve, reject) {
1555
1587
  },
1556
1588
  throw: (value) => {
1557
1589
  markParserRef(this.base, id);
1558
- parseAsync(this, value).then(
1590
+ parseAsync(this, depth, value).then(
1559
1591
  (data) => {
1560
1592
  sequence.push(createStreamThrowNode(id, data));
1561
1593
  resolve(sequence);
@@ -1569,7 +1601,7 @@ function parseStreamHandle(id, current, resolve, reject) {
1569
1601
  },
1570
1602
  return: (value) => {
1571
1603
  markParserRef(this.base, id);
1572
- parseAsync(this, value).then(
1604
+ parseAsync(this, depth, value).then(
1573
1605
  (data) => {
1574
1606
  sequence.push(createStreamReturnNode(id, data));
1575
1607
  resolve(sequence);
@@ -1583,28 +1615,31 @@ function parseStreamHandle(id, current, resolve, reject) {
1583
1615
  }
1584
1616
  });
1585
1617
  }
1586
- async function parseStream(ctx, id, current) {
1618
+ async function parseStream(ctx, depth, id, current) {
1587
1619
  return createStreamConstructorNode(
1588
1620
  id,
1589
1621
  parseSpecialReference(ctx.base, 4 /* StreamConstructor */),
1590
- await new Promise(parseStreamHandle.bind(ctx, id, current))
1622
+ await new Promise(
1623
+ parseStreamHandle.bind(ctx, depth, id, current)
1624
+ )
1591
1625
  );
1592
1626
  }
1593
- async function parseObjectAsync(ctx, id, current) {
1627
+ async function parseObjectAsync(ctx, depth, id, current) {
1594
1628
  if (Array.isArray(current)) {
1595
- return parseArray(ctx, id, current);
1629
+ return parseArray(ctx, depth, id, current);
1596
1630
  }
1597
1631
  if (isStream(current)) {
1598
- return parseStream(ctx, id, current);
1632
+ return parseStream(ctx, depth, id, current);
1599
1633
  }
1600
1634
  const currentClass = current.constructor;
1601
1635
  if (currentClass === OpaqueReference) {
1602
1636
  return parseAsync(
1603
1637
  ctx,
1638
+ depth,
1604
1639
  current.replacement
1605
1640
  );
1606
1641
  }
1607
- const parsed = await parsePlugin(ctx, id, current);
1642
+ const parsed = await parsePlugin(ctx, depth, id, current);
1608
1643
  if (parsed) {
1609
1644
  return parsed;
1610
1645
  }
@@ -1612,6 +1647,7 @@ async function parseObjectAsync(ctx, id, current) {
1612
1647
  case Object:
1613
1648
  return parsePlainObject(
1614
1649
  ctx,
1650
+ depth,
1615
1651
  id,
1616
1652
  current,
1617
1653
  false
@@ -1619,14 +1655,13 @@ async function parseObjectAsync(ctx, id, current) {
1619
1655
  case NIL:
1620
1656
  return parsePlainObject(
1621
1657
  ctx,
1658
+ depth,
1622
1659
  id,
1623
1660
  current,
1624
1661
  true
1625
1662
  );
1626
1663
  case Date:
1627
1664
  return createDateNode(id, current);
1628
- case RegExp:
1629
- return createRegExpNode(id, current);
1630
1665
  case Error:
1631
1666
  case EvalError:
1632
1667
  case RangeError:
@@ -1634,12 +1669,12 @@ async function parseObjectAsync(ctx, id, current) {
1634
1669
  case SyntaxError:
1635
1670
  case TypeError:
1636
1671
  case URIError:
1637
- return parseError(ctx, id, current);
1672
+ return parseError(ctx, depth, id, current);
1638
1673
  case Number:
1639
1674
  case Boolean:
1640
1675
  case String:
1641
1676
  case BigInt:
1642
- return parseBoxed(ctx, id, current);
1677
+ return parseBoxed(ctx, depth, id, current);
1643
1678
  case ArrayBuffer:
1644
1679
  return createArrayBufferNode(
1645
1680
  ctx.base,
@@ -1655,26 +1690,40 @@ async function parseObjectAsync(ctx, id, current) {
1655
1690
  case Uint8ClampedArray:
1656
1691
  case Float32Array:
1657
1692
  case Float64Array:
1658
- return parseTypedArray(ctx, id, current);
1693
+ return parseTypedArray(
1694
+ ctx,
1695
+ depth,
1696
+ id,
1697
+ current
1698
+ );
1659
1699
  case DataView:
1660
- return parseDataView(ctx, id, current);
1700
+ return parseDataView(ctx, depth, id, current);
1661
1701
  case Map:
1662
- return parseMap(ctx, id, current);
1702
+ return parseMap(
1703
+ ctx,
1704
+ depth,
1705
+ id,
1706
+ current
1707
+ );
1663
1708
  case Set:
1664
- return parseSet(ctx, id, current);
1709
+ return parseSet(ctx, depth, id, current);
1665
1710
  default:
1666
1711
  break;
1667
1712
  }
1668
1713
  if (currentClass === Promise || current instanceof Promise) {
1669
- return parsePromise(ctx, id, current);
1714
+ return parsePromise(ctx, depth, id, current);
1670
1715
  }
1671
1716
  const currentFeatures = ctx.base.features;
1717
+ if (currentFeatures & 32 /* RegExp */ && currentClass === RegExp) {
1718
+ return createRegExpNode(id, current);
1719
+ }
1672
1720
  if (currentFeatures & 16 /* BigIntTypedArray */) {
1673
1721
  switch (currentClass) {
1674
1722
  case BigInt64Array:
1675
1723
  case BigUint64Array:
1676
1724
  return parseBigIntTypedArray(
1677
1725
  ctx,
1726
+ depth,
1678
1727
  id,
1679
1728
  current
1680
1729
  );
@@ -1683,28 +1732,33 @@ async function parseObjectAsync(ctx, id, current) {
1683
1732
  }
1684
1733
  }
1685
1734
  if (currentFeatures & 1 /* AggregateError */ && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) {
1686
- return parseAggregateError(ctx, id, current);
1735
+ return parseAggregateError(
1736
+ ctx,
1737
+ depth,
1738
+ id,
1739
+ current
1740
+ );
1687
1741
  }
1688
1742
  if (current instanceof Error) {
1689
- return parseError(ctx, id, current);
1743
+ return parseError(ctx, depth, id, current);
1690
1744
  }
1691
1745
  if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) {
1692
- return parsePlainObject(ctx, id, current, !!currentClass);
1746
+ return parsePlainObject(ctx, depth, id, current, !!currentClass);
1693
1747
  }
1694
1748
  throw new SerovalUnsupportedTypeError(current);
1695
1749
  }
1696
- async function parseFunctionAsync(ctx, current) {
1750
+ async function parseFunctionAsync(ctx, depth, current) {
1697
1751
  const ref = getReferenceNode(ctx.base, current);
1698
1752
  if (ref.type !== 0 /* Fresh */) {
1699
1753
  return ref.value;
1700
1754
  }
1701
- const plugin = await parsePlugin(ctx, ref.value, current);
1755
+ const plugin = await parsePlugin(ctx, depth, ref.value, current);
1702
1756
  if (plugin) {
1703
1757
  return plugin;
1704
1758
  }
1705
1759
  throw new SerovalUnsupportedTypeError(current);
1706
1760
  }
1707
- async function parseAsync(ctx, current) {
1761
+ async function parseAsync(ctx, depth, current) {
1708
1762
  switch (typeof current) {
1709
1763
  case "boolean":
1710
1764
  return current ? TRUE_NODE : FALSE_NODE;
@@ -1719,21 +1773,21 @@ async function parseAsync(ctx, current) {
1719
1773
  case "object": {
1720
1774
  if (current) {
1721
1775
  const ref = getReferenceNode(ctx.base, current);
1722
- return ref.type === 0 ? await parseObjectAsync(ctx, ref.value, current) : ref.value;
1776
+ return ref.type === 0 ? await parseObjectAsync(ctx, depth + 1, ref.value, current) : ref.value;
1723
1777
  }
1724
1778
  return NULL_NODE;
1725
1779
  }
1726
1780
  case "symbol":
1727
1781
  return parseWellKnownSymbol(ctx.base, current);
1728
1782
  case "function":
1729
- return parseFunctionAsync(ctx, current);
1783
+ return parseFunctionAsync(ctx, depth, current);
1730
1784
  default:
1731
1785
  throw new SerovalUnsupportedTypeError(current);
1732
1786
  }
1733
1787
  }
1734
1788
  async function parseTopAsync(ctx, current) {
1735
1789
  try {
1736
- return await parseAsync(ctx, current);
1790
+ return await parseAsync(ctx, 0, current);
1737
1791
  } catch (error) {
1738
1792
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
1739
1793
  }
@@ -1799,6 +1853,9 @@ function getTypedArrayConstructor(name) {
1799
1853
  }
1800
1854
 
1801
1855
  // src/core/context/deserializer.ts
1856
+ var MAX_BASE64_LENGTH = 1e6;
1857
+ var MAX_BIGINT_LENGTH = 1e4;
1858
+ var MAX_REGEXP_SOURCE_LENGTH = 2e4;
1802
1859
  function applyObjectFlag(obj, flag) {
1803
1860
  switch (flag) {
1804
1861
  case 3 /* Frozen */:
@@ -1811,18 +1868,22 @@ function applyObjectFlag(obj, flag) {
1811
1868
  return obj;
1812
1869
  }
1813
1870
  }
1871
+ var DEFAULT_DEPTH_LIMIT = 1e3;
1814
1872
  function createBaseDeserializerContext(mode, options) {
1873
+ var _a;
1815
1874
  return {
1816
1875
  mode,
1817
1876
  plugins: options.plugins,
1818
- refs: options.refs || /* @__PURE__ */ new Map()
1877
+ refs: options.refs || /* @__PURE__ */ new Map(),
1878
+ features: (_a = options.features) != null ? _a : ALL_ENABLED ^ (options.disabledFeatures || 0),
1879
+ depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT
1819
1880
  };
1820
1881
  }
1821
1882
  function createVanillaDeserializerContext(options) {
1822
1883
  return {
1823
1884
  mode: 1 /* Vanilla */,
1824
1885
  base: createBaseDeserializerContext(1 /* Vanilla */, options),
1825
- child: void 0,
1886
+ child: NIL,
1826
1887
  state: {
1827
1888
  marked: new Set(options.markedRefs)
1828
1889
  }
@@ -1832,32 +1893,50 @@ function createCrossDeserializerContext(options) {
1832
1893
  return {
1833
1894
  mode: 2 /* Cross */,
1834
1895
  base: createBaseDeserializerContext(2 /* Cross */, options),
1835
- child: void 0
1896
+ child: NIL
1836
1897
  };
1837
1898
  }
1838
1899
  var DeserializePluginContext = class {
1839
- constructor(_p) {
1900
+ constructor(_p, depth) {
1840
1901
  this._p = _p;
1902
+ this.depth = depth;
1841
1903
  }
1842
1904
  deserialize(node) {
1843
- return deserialize(this._p, node);
1905
+ return deserialize(this._p, this.depth, node);
1844
1906
  }
1845
1907
  };
1908
+ function guardIndexedValue(ctx, id) {
1909
+ if (id < 0 || !Number.isFinite(id) || !Number.isInteger(id)) {
1910
+ throw new SerovalMalformedNodeError({
1911
+ t: 4 /* IndexedValue */,
1912
+ i: id
1913
+ });
1914
+ }
1915
+ if (ctx.refs.has(id)) {
1916
+ throw new Error("Conflicted ref id: " + id);
1917
+ }
1918
+ }
1846
1919
  function assignIndexedValueVanilla(ctx, id, value) {
1920
+ guardIndexedValue(ctx.base, id);
1847
1921
  if (ctx.state.marked.has(id)) {
1848
1922
  ctx.base.refs.set(id, value);
1849
1923
  }
1850
1924
  return value;
1851
1925
  }
1852
1926
  function assignIndexedValueCross(ctx, id, value) {
1853
- if (!ctx.base.refs.has(id)) {
1854
- ctx.base.refs.set(id, value);
1855
- }
1927
+ guardIndexedValue(ctx.base, id);
1928
+ ctx.base.refs.set(id, value);
1856
1929
  return value;
1857
1930
  }
1858
1931
  function assignIndexedValue(ctx, id, value) {
1859
1932
  return ctx.mode === 1 /* Vanilla */ ? assignIndexedValueVanilla(ctx, id, value) : assignIndexedValueCross(ctx, id, value);
1860
1933
  }
1934
+ function deserializeKnownValue(node, record, key) {
1935
+ if (Object.hasOwn(record, key)) {
1936
+ return record[key];
1937
+ }
1938
+ throw new SerovalMalformedNodeError(node);
1939
+ }
1861
1940
  function deserializeReference(ctx, node) {
1862
1941
  return assignIndexedValue(
1863
1942
  ctx,
@@ -1865,46 +1944,96 @@ function deserializeReference(ctx, node) {
1865
1944
  getReference(deserializeString(node.s))
1866
1945
  );
1867
1946
  }
1868
- function deserializeArray(ctx, node) {
1869
- const len = node.l;
1947
+ function deserializeArray(ctx, depth, node) {
1948
+ const items = node.a;
1949
+ const len = items.length;
1870
1950
  const result = assignIndexedValue(
1871
1951
  ctx,
1872
1952
  node.i,
1873
1953
  new Array(len)
1874
1954
  );
1875
- let item;
1876
- for (let i = 0; i < len; i++) {
1877
- item = node.a[i];
1955
+ for (let i = 0, item; i < len; i++) {
1956
+ item = items[i];
1878
1957
  if (item) {
1879
- result[i] = deserialize(ctx, item);
1958
+ result[i] = deserialize(ctx, depth, item);
1880
1959
  }
1881
1960
  }
1882
1961
  applyObjectFlag(result, node.o);
1883
1962
  return result;
1884
1963
  }
1885
- function deserializeProperties(ctx, node, result) {
1886
- const len = node.s;
1887
- if (len) {
1888
- const keys = node.k;
1889
- const vals = node.v;
1890
- for (let i = 0, key; i < len; i++) {
1891
- key = keys[i];
1892
- if (typeof key === "string") {
1893
- result[deserializeString(key)] = deserialize(ctx, vals[i]);
1894
- } else {
1895
- result[deserialize(ctx, key)] = deserialize(ctx, vals[i]);
1896
- }
1964
+ function isValidKey(key) {
1965
+ switch (key) {
1966
+ case "constructor":
1967
+ case "__proto__":
1968
+ case "prototype":
1969
+ case "__defineGetter__":
1970
+ case "__defineSetter__":
1971
+ case "__lookupGetter__":
1972
+ case "__lookupSetter__":
1973
+ return false;
1974
+ default:
1975
+ return true;
1976
+ }
1977
+ }
1978
+ function isValidSymbol(symbol) {
1979
+ switch (symbol) {
1980
+ case SYM_ASYNC_ITERATOR:
1981
+ case SYM_IS_CONCAT_SPREADABLE:
1982
+ case SYM_TO_STRING_TAG:
1983
+ case SYM_ITERATOR:
1984
+ return true;
1985
+ default:
1986
+ return false;
1987
+ }
1988
+ }
1989
+ function assignStringProperty(object, key, value) {
1990
+ if (isValidKey(key)) {
1991
+ object[key] = value;
1992
+ } else {
1993
+ Object.defineProperty(object, key, {
1994
+ value,
1995
+ configurable: true,
1996
+ enumerable: true,
1997
+ writable: true
1998
+ });
1999
+ }
2000
+ }
2001
+ function assignProperty(ctx, depth, object, key, value) {
2002
+ if (typeof key === "string") {
2003
+ assignStringProperty(object, key, deserialize(ctx, depth, value));
2004
+ } else {
2005
+ const actual = deserialize(ctx, depth, key);
2006
+ switch (typeof actual) {
2007
+ case "string":
2008
+ assignStringProperty(object, actual, deserialize(ctx, depth, value));
2009
+ break;
2010
+ case "symbol":
2011
+ if (isValidSymbol(actual)) {
2012
+ object[actual] = deserialize(ctx, depth, value);
2013
+ }
2014
+ break;
2015
+ default:
2016
+ throw new SerovalMalformedNodeError(key);
2017
+ }
2018
+ }
2019
+ }
2020
+ function deserializeProperties(ctx, depth, node, result) {
2021
+ const keys = node.k;
2022
+ const len = keys.length;
2023
+ if (len > 0) {
2024
+ for (let i = 0, vals = node.v, len2 = keys.length; i < len2; i++) {
2025
+ assignProperty(ctx, depth, result, keys[i], vals[i]);
1897
2026
  }
1898
2027
  }
1899
2028
  return result;
1900
2029
  }
1901
- function deserializeObject(ctx, node) {
2030
+ function deserializeObject(ctx, depth, node) {
1902
2031
  const result = assignIndexedValue(
1903
2032
  ctx,
1904
2033
  node.i,
1905
2034
  node.t === 10 /* Object */ ? {} : /* @__PURE__ */ Object.create(null)
1906
2035
  );
1907
- deserializeProperties(ctx, node.p, result);
2036
+ deserializeProperties(ctx, depth, node.p, result);
1908
2037
  applyObjectFlag(result, node.o);
1909
2038
  return result;
1910
2039
  }
@@ -1912,84 +2041,100 @@ function deserializeDate(ctx, node) {
1912
2041
  return assignIndexedValue(ctx, node.i, new Date(node.s));
1913
2042
  }
1914
2043
  function deserializeRegExp(ctx, node) {
1915
- return assignIndexedValue(
1916
- ctx,
1917
- node.i,
1918
- new RegExp(deserializeString(node.c), node.m)
1919
- );
2044
+ if (ctx.base.features & 32 /* RegExp */) {
2045
+ const source = deserializeString(node.c);
2046
+ if (source.length > MAX_REGEXP_SOURCE_LENGTH) {
2047
+ throw new SerovalMalformedNodeError(node);
2048
+ }
2049
+ return assignIndexedValue(ctx, node.i, new RegExp(source, node.m));
2050
+ }
2051
+ throw new SerovalUnsupportedNodeError(node);
1920
2052
  }
1921
- function deserializeSet(ctx, node) {
2053
+ function deserializeSet(ctx, depth, node) {
1922
2054
  const result = assignIndexedValue(ctx, node.i, /* @__PURE__ */ new Set());
1923
- const items = node.a;
1924
- for (let i = 0, len = node.l; i < len; i++) {
1925
- result.add(deserialize(ctx, items[i]));
2055
+ for (let i = 0, items = node.a, len = items.length; i < len; i++) {
2056
+ result.add(deserialize(ctx, depth, items[i]));
1926
2057
  }
1927
2058
  return result;
1928
2059
  }
1929
- function deserializeMap(ctx, node) {
2060
+ function deserializeMap(ctx, depth, node) {
1930
2061
  const result = assignIndexedValue(ctx, node.i, /* @__PURE__ */ new Map());
1931
- const keys = node.e.k;
1932
- const vals = node.e.v;
1933
- for (let i = 0, len = node.e.s; i < len; i++) {
1934
- result.set(deserialize(ctx, keys[i]), deserialize(ctx, vals[i]));
2062
+ for (let i = 0, keys = node.e.k, vals = node.e.v, len = keys.length; i < len; i++) {
2063
+ result.set(
2064
+ deserialize(ctx, depth, keys[i]),
2065
+ deserialize(ctx, depth, vals[i])
2066
+ );
1935
2067
  }
1936
2068
  return result;
1937
2069
  }
1938
2070
  function deserializeArrayBuffer(ctx, node) {
2071
+ if (node.s.length > MAX_BASE64_LENGTH) {
2072
+ throw new SerovalMalformedNodeError(node);
2073
+ }
1939
2074
  const result = assignIndexedValue(
1940
2075
  ctx,
1941
2076
  node.i,
1942
- ARRAY_BUFFER_CONSTRUCTOR(node.l, deserializeString(node.s))
2077
+ ARRAY_BUFFER_CONSTRUCTOR(deserializeString(node.s))
1943
2078
  );
1944
2079
  return result;
1945
2080
  }
1946
- function deserializeTypedArray(ctx, node) {
2081
+ function deserializeTypedArray(ctx, depth, node) {
2082
+ var _a;
1947
2083
  const construct = getTypedArrayConstructor(node.c);
1948
- const source = deserialize(ctx, node.f);
2084
+ const source = deserialize(ctx, depth, node.f);
2085
+ const offset = (_a = node.b) != null ? _a : 0;
2086
+ if (offset < 0 || offset > source.byteLength) {
2087
+ throw new SerovalMalformedNodeError(node);
2088
+ }
1949
2089
  const result = assignIndexedValue(
1950
2090
  ctx,
1951
2091
  node.i,
1952
- new construct(source, node.b, node.l)
2092
+ new construct(source, offset, node.l)
1953
2093
  );
1954
2094
  return result;
1955
2095
  }
1956
- function deserializeDataView(ctx, node) {
1957
- const source = deserialize(ctx, node.f);
2096
+ function deserializeDataView(ctx, depth, node) {
2097
+ var _a;
2098
+ const source = deserialize(ctx, depth, node.f);
2099
+ const offset = (_a = node.b) != null ? _a : 0;
2100
+ if (offset < 0 || offset > source.byteLength) {
2101
+ throw new SerovalMalformedNodeError(node);
2102
+ }
1958
2103
  const result = assignIndexedValue(
1959
2104
  ctx,
1960
2105
  node.i,
1961
- new DataView(source, node.b, node.l)
2106
+ new DataView(source, offset, node.l)
1962
2107
  );
1963
2108
  return result;
1964
2109
  }
1965
- function deserializeDictionary(ctx, node, result) {
2110
+ function deserializeDictionary(ctx, depth, node, result) {
1966
2111
  if (node.p) {
1967
- const fields = deserializeProperties(ctx, node.p, {});
1968
- Object.assign(result, fields);
2112
+ const fields = deserializeProperties(ctx, depth, node.p, {});
2113
+ Object.defineProperties(result, Object.getOwnPropertyDescriptors(fields));
1969
2114
  }
1970
2115
  return result;
1971
2116
  }
1972
- function deserializeAggregateError(ctx, node) {
2117
+ function deserializeAggregateError(ctx, depth, node) {
1973
2118
  const result = assignIndexedValue(
1974
2119
  ctx,
1975
2120
  node.i,
1976
2121
  new AggregateError([], deserializeString(node.m))
1977
2122
  );
1978
- return deserializeDictionary(ctx, node, result);
2123
+ return deserializeDictionary(ctx, depth, node, result);
1979
2124
  }
1980
- function deserializeError(ctx, node) {
1981
- const construct = ERROR_CONSTRUCTOR[node.s];
2125
+ function deserializeError(ctx, depth, node) {
2126
+ const construct = deserializeKnownValue(node, ERROR_CONSTRUCTOR, node.s);
1982
2127
  const result = assignIndexedValue(
1983
2128
  ctx,
1984
2129
  node.i,
1985
2130
  new construct(deserializeString(node.m))
1986
2131
  );
1987
- return deserializeDictionary(ctx, node, result);
2132
+ return deserializeDictionary(ctx, depth, node, result);
1988
2133
  }
1989
- function deserializePromise(ctx, node) {
2134
+ function deserializePromise(ctx, depth, node) {
1990
2135
  const deferred = PROMISE_CONSTRUCTOR();
1991
2136
  const result = assignIndexedValue(ctx, node.i, deferred.p);
1992
- const deserialized = deserialize(ctx, node.f);
2137
+ const deserialized = deserialize(ctx, depth, node.f);
1993
2138
  if (node.s) {
1994
2139
  deferred.s(deserialized);
1995
2140
  } else {
@@ -1997,23 +2142,25 @@ function deserializePromise(ctx, node) {
1997
2142
  }
1998
2143
  return result;
1999
2144
  }
2000
- function deserializeBoxed(ctx, node) {
2001
- return assignIndexedValue(ctx, node.i, Object(deserialize(ctx, node.f)));
2145
+ function deserializeBoxed(ctx, depth, node) {
2146
+ return assignIndexedValue(
2147
+ ctx,
2148
+ node.i,
2149
+ // biome-ignore lint/style/useConsistentBuiltinInstantiation: intended
2150
+ Object(deserialize(ctx, depth, node.f))
2151
+ );
2002
2152
  }
2003
- function deserializePlugin(ctx, node) {
2153
+ function deserializePlugin(ctx, depth, node) {
2004
2154
  const currentPlugins = ctx.base.plugins;
2005
2155
  if (currentPlugins) {
2006
2156
  const tag = deserializeString(node.c);
2007
2157
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
2008
2158
  const plugin = currentPlugins[i];
2009
2159
  if (plugin.tag === tag) {
2010
- if (ctx.child == null) {
2011
- ctx.child = new DeserializePluginContext(ctx);
2012
- }
2013
2160
  return assignIndexedValue(
2014
2161
  ctx,
2015
2162
  node.i,
2016
- plugin.deserialize(node.s, ctx.child, {
2163
+ plugin.deserialize(node.s, new DeserializePluginContext(ctx, depth), {
2017
2164
  id: node.i
2018
2165
  })
2019
2166
  );
@@ -2029,142 +2176,150 @@ function deserializePromiseConstructor(ctx, node) {
2029
2176
  assignIndexedValue(ctx, node.s, PROMISE_CONSTRUCTOR()).p
2030
2177
  );
2031
2178
  }
2032
- function deserializePromiseResolve(ctx, node) {
2179
+ function deserializePromiseResolve(ctx, depth, node) {
2033
2180
  const deferred = ctx.base.refs.get(node.i);
2034
2181
  if (deferred) {
2035
- deferred.s(deserialize(ctx, node.a[1]));
2036
- return void 0;
2182
+ deferred.s(deserialize(ctx, depth, node.a[1]));
2183
+ return NIL;
2037
2184
  }
2038
2185
  throw new SerovalMissingInstanceError("Promise");
2039
2186
  }
2040
- function deserializePromiseReject(ctx, node) {
2187
+ function deserializePromiseReject(ctx, depth, node) {
2041
2188
  const deferred = ctx.base.refs.get(node.i);
2042
2189
  if (deferred) {
2043
- deferred.f(deserialize(ctx, node.a[1]));
2044
- return void 0;
2190
+ deferred.f(deserialize(ctx, depth, node.a[1]));
2191
+ return NIL;
2045
2192
  }
2046
2193
  throw new SerovalMissingInstanceError("Promise");
2047
2194
  }
2048
- function deserializeIteratorFactoryInstance(ctx, node) {
2049
- deserialize(ctx, node.a[0]);
2050
- const source = deserialize(ctx, node.a[1]);
2195
+ function deserializeIteratorFactoryInstance(ctx, depth, node) {
2196
+ deserialize(ctx, depth, node.a[0]);
2197
+ const source = deserialize(ctx, depth, node.a[1]);
2051
2198
  return sequenceToIterator(source);
2052
2199
  }
2053
- function deserializeAsyncIteratorFactoryInstance(ctx, node) {
2054
- deserialize(ctx, node.a[0]);
2055
- const source = deserialize(ctx, node.a[1]);
2200
+ function deserializeAsyncIteratorFactoryInstance(ctx, depth, node) {
2201
+ deserialize(ctx, depth, node.a[0]);
2202
+ const source = deserialize(ctx, depth, node.a[1]);
2056
2203
  return streamToAsyncIterable(source);
2057
2204
  }
2058
- function deserializeStreamConstructor(ctx, node) {
2205
+ function deserializeStreamConstructor(ctx, depth, node) {
2059
2206
  const result = assignIndexedValue(ctx, node.i, createStream());
2060
- const len = node.a.length;
2207
+ const items = node.a;
2208
+ const len = items.length;
2061
2209
  if (len) {
2062
2210
  for (let i = 0; i < len; i++) {
2063
- deserialize(ctx, node.a[i]);
2211
+ deserialize(ctx, depth, items[i]);
2064
2212
  }
2065
2213
  }
2066
2214
  return result;
2067
2215
  }
2068
- function deserializeStreamNext(ctx, node) {
2216
+ function deserializeStreamNext(ctx, depth, node) {
2069
2217
  const deferred = ctx.base.refs.get(node.i);
2070
- if (deferred) {
2071
- deferred.next(deserialize(ctx, node.f));
2072
- return void 0;
2218
+ if (deferred && isStream(deferred)) {
2219
+ deferred.next(deserialize(ctx, depth, node.f));
2220
+ return NIL;
2073
2221
  }
2074
2222
  throw new SerovalMissingInstanceError("Stream");
2075
2223
  }
2076
- function deserializeStreamThrow(ctx, node) {
2224
+ function deserializeStreamThrow(ctx, depth, node) {
2077
2225
  const deferred = ctx.base.refs.get(node.i);
2078
- if (deferred) {
2079
- deferred.throw(deserialize(ctx, node.f));
2080
- return void 0;
2226
+ if (deferred && isStream(deferred)) {
2227
+ deferred.throw(deserialize(ctx, depth, node.f));
2228
+ return NIL;
2081
2229
  }
2082
2230
  throw new SerovalMissingInstanceError("Stream");
2083
2231
  }
2084
- function deserializeStreamReturn(ctx, node) {
2232
+ function deserializeStreamReturn(ctx, depth, node) {
2085
2233
  const deferred = ctx.base.refs.get(node.i);
2086
- if (deferred) {
2087
- deferred.return(deserialize(ctx, node.f));
2088
- return void 0;
2234
+ if (deferred && isStream(deferred)) {
2235
+ deferred.return(deserialize(ctx, depth, node.f));
2236
+ return NIL;
2089
2237
  }
2090
2238
  throw new SerovalMissingInstanceError("Stream");
2091
2239
  }
2092
- function deserializeIteratorFactory(ctx, node) {
2093
- deserialize(ctx, node.f);
2094
- return void 0;
2240
+ function deserializeIteratorFactory(ctx, depth, node) {
2241
+ deserialize(ctx, depth, node.f);
2242
+ return NIL;
2095
2243
  }
2096
- function deserializeAsyncIteratorFactory(ctx, node) {
2097
- deserialize(ctx, node.a[1]);
2098
- return void 0;
2244
+ function deserializeAsyncIteratorFactory(ctx, depth, node) {
2245
+ deserialize(ctx, depth, node.a[1]);
2246
+ return NIL;
2099
2247
  }
2100
- function deserialize(ctx, node) {
2248
+ function deserialize(ctx, depth, node) {
2249
+ if (depth > ctx.base.depthLimit) {
2250
+ throw new SerovalDepthLimitError(ctx.base.depthLimit);
2251
+ }
2252
+ depth += 1;
2101
2253
  switch (node.t) {
2102
2254
  case 2 /* Constant */:
2103
- return CONSTANT_VAL[node.s];
2255
+ return deserializeKnownValue(node, CONSTANT_VAL, node.s);
2104
2256
  case 0 /* Number */:
2105
- return node.s;
2257
+ return Number(node.s);
2106
2258
  case 1 /* String */:
2107
- return deserializeString(node.s);
2259
+ return deserializeString(String(node.s));
2108
2260
  case 3 /* BigInt */:
2261
+ if (String(node.s).length > MAX_BIGINT_LENGTH) {
2262
+ throw new SerovalMalformedNodeError(node);
2263
+ }
2109
2264
  return BigInt(node.s);
2110
2265
  case 4 /* IndexedValue */:
2111
2266
  return ctx.base.refs.get(node.i);
2112
2267
  case 18 /* Reference */:
2113
2268
  return deserializeReference(ctx, node);
2114
2269
  case 9 /* Array */:
2115
- return deserializeArray(ctx, node);
2270
+ return deserializeArray(ctx, depth, node);
2116
2271
  case 10 /* Object */:
2117
2272
  case 11 /* NullConstructor */:
2118
- return deserializeObject(ctx, node);
2273
+ return deserializeObject(ctx, depth, node);
2119
2274
  case 5 /* Date */:
2120
2275
  return deserializeDate(ctx, node);
2121
2276
  case 6 /* RegExp */:
2122
2277
  return deserializeRegExp(ctx, node);
2123
2278
  case 7 /* Set */:
2124
- return deserializeSet(ctx, node);
2279
+ return deserializeSet(ctx, depth, node);
2125
2280
  case 8 /* Map */:
2126
- return deserializeMap(ctx, node);
2281
+ return deserializeMap(ctx, depth, node);
2127
2282
  case 19 /* ArrayBuffer */:
2128
2283
  return deserializeArrayBuffer(ctx, node);
2129
2284
  case 16 /* BigIntTypedArray */:
2130
2285
  case 15 /* TypedArray */:
2131
- return deserializeTypedArray(ctx, node);
2286
+ return deserializeTypedArray(ctx, depth, node);
2132
2287
  case 20 /* DataView */:
2133
- return deserializeDataView(ctx, node);
2288
+ return deserializeDataView(ctx, depth, node);
2134
2289
  case 14 /* AggregateError */:
2135
- return deserializeAggregateError(ctx, node);
2290
+ return deserializeAggregateError(ctx, depth, node);
2136
2291
  case 13 /* Error */:
2137
- return deserializeError(ctx, node);
2292
+ return deserializeError(ctx, depth, node);
2138
2293
  case 12 /* Promise */:
2139
- return deserializePromise(ctx, node);
2294
+ return deserializePromise(ctx, depth, node);
2140
2295
  case 17 /* WKSymbol */:
2141
- return SYMBOL_REF[node.s];
2296
+ return deserializeKnownValue(node, SYMBOL_REF, node.s);
2142
2297
  case 21 /* Boxed */:
2143
- return deserializeBoxed(ctx, node);
2298
+ return deserializeBoxed(ctx, depth, node);
2144
2299
  case 25 /* Plugin */:
2145
- return deserializePlugin(ctx, node);
2300
+ return deserializePlugin(ctx, depth, node);
2146
2301
  case 22 /* PromiseConstructor */:
2147
2302
  return deserializePromiseConstructor(ctx, node);
2148
2303
  case 23 /* PromiseSuccess */:
2149
- return deserializePromiseResolve(ctx, node);
2304
+ return deserializePromiseResolve(ctx, depth, node);
2150
2305
  case 24 /* PromiseFailure */:
2151
- return deserializePromiseReject(ctx, node);
2306
+ return deserializePromiseReject(ctx, depth, node);
2152
2307
  case 28 /* IteratorFactoryInstance */:
2153
- return deserializeIteratorFactoryInstance(ctx, node);
2308
+ return deserializeIteratorFactoryInstance(ctx, depth, node);
2154
2309
  case 30 /* AsyncIteratorFactoryInstance */:
2155
- return deserializeAsyncIteratorFactoryInstance(ctx, node);
2310
+ return deserializeAsyncIteratorFactoryInstance(ctx, depth, node);
2156
2311
  case 31 /* StreamConstructor */:
2157
- return deserializeStreamConstructor(ctx, node);
2312
+ return deserializeStreamConstructor(ctx, depth, node);
2158
2313
  case 32 /* StreamNext */:
2159
- return deserializeStreamNext(ctx, node);
2314
+ return deserializeStreamNext(ctx, depth, node);
2160
2315
  case 33 /* StreamThrow */:
2161
- return deserializeStreamThrow(ctx, node);
2316
+ return deserializeStreamThrow(ctx, depth, node);
2162
2317
  case 34 /* StreamReturn */:
2163
- return deserializeStreamReturn(ctx, node);
2318
+ return deserializeStreamReturn(ctx, depth, node);
2164
2319
  case 27 /* IteratorFactory */:
2165
- return deserializeIteratorFactory(ctx, node);
2320
+ return deserializeIteratorFactory(ctx, depth, node);
2166
2321
  case 29 /* AsyncIteratorFactory */:
2167
- return deserializeAsyncIteratorFactory(ctx, node);
2322
+ return deserializeAsyncIteratorFactory(ctx, depth, node);
2168
2323
  // case SerovalNodeType.SpecialReference:
2169
2324
  default:
2170
2325
  throw new SerovalUnsupportedNodeError(node);
@@ -2172,7 +2327,7 @@ function deserialize(ctx, node) {
2172
2327
  }
2173
2328
  function deserializeTop(ctx, node) {
2174
2329
  try {
2175
- return deserialize(ctx, node);
2330
+ return deserialize(ctx, 0, node);
2176
2331
  } catch (error) {
2177
2332
  throw new SerovalDeserializationError(error);
2178
2333
  }
@@ -2320,7 +2475,7 @@ function createVanillaSerializerContext(options) {
2320
2475
  mode: 1 /* Vanilla */,
2321
2476
  base: createBaseSerializerContext(1 /* Vanilla */, options),
2322
2477
  state: createVanillaSerializerState(),
2323
- child: void 0
2478
+ child: NIL
2324
2479
  };
2325
2480
  }
2326
2481
  function createCrossSerializerContext(options) {
@@ -2328,7 +2483,7 @@ function createCrossSerializerContext(options) {
2328
2483
  mode: 2 /* Cross */,
2329
2484
  base: createBaseSerializerContext(2 /* Cross */, options),
2330
2485
  state: options,
2331
- child: void 0
2486
+ child: NIL
2332
2487
  };
2333
2488
  }
2334
2489
  var SerializePluginContext = class {
@@ -2460,12 +2615,13 @@ function serializeArrayItem(ctx, id, item, index) {
2460
2615
  }
2461
2616
  function serializeArray(ctx, node) {
2462
2617
  const id = node.i;
2463
- if (node.l) {
2618
+ const list = node.a;
2619
+ const len = list.length;
2620
+ if (len > 0) {
2464
2621
  ctx.base.stack.push(id);
2465
- const list = node.a;
2466
2622
  let values = serializeArrayItem(ctx, id, list[0], 0);
2467
2623
  let isHoley = values === "";
2468
- for (let i = 1, len = node.l, item; i < len; i++) {
2624
+ for (let i = 1, item; i < len; i++) {
2469
2625
  item = serializeArrayItem(ctx, id, list[i], i);
2470
2626
  values += "," + item;
2471
2627
  isHoley = item === "";
@@ -2511,9 +2667,9 @@ function serializeProperty(ctx, source, key, val) {
2511
2667
  return "[" + serialize(ctx, key) + "]:" + serialize(ctx, val);
2512
2668
  }
2513
2669
  function serializeProperties(ctx, source, record) {
2514
- const len = record.s;
2515
- if (len) {
2516
- const keys = record.k;
2670
+ const keys = record.k;
2671
+ const len = keys.length;
2672
+ if (len > 0) {
2517
2673
  const values = record.v;
2518
2674
  ctx.base.stack.push(source.i);
2519
2675
  let result = serializeProperty(ctx, source, keys[0], values[0]);
@@ -2596,10 +2752,10 @@ function serializeAssignment(ctx, source, mainAssignments, key, value) {
2596
2752
  }
2597
2753
  }
2598
2754
  function serializeAssignments(ctx, source, node) {
2599
- const len = node.s;
2600
- if (len) {
2755
+ const keys = node.k;
2756
+ const len = keys.length;
2757
+ if (len > 0) {
2601
2758
  const mainAssignments = [];
2602
- const keys = node.k;
2603
2759
  const values = node.v;
2604
2760
  ctx.base.stack.push(source.i);
2605
2761
  for (let i = 0; i < len; i++) {
@@ -2632,8 +2788,11 @@ function serializeNullConstructor(ctx, node) {
2632
2788
  function serializeDate(node) {
2633
2789
  return 'new Date("' + node.s + '")';
2634
2790
  }
2635
- function serializeRegExp(node) {
2636
- return "/" + node.c + "/" + node.m;
2791
+ function serializeRegExp(ctx, node) {
2792
+ if (ctx.base.features & 32 /* RegExp */) {
2793
+ return "/" + node.c + "/" + node.m;
2794
+ }
2795
+ throw new SerovalUnsupportedNodeError(node);
2637
2796
  }
2638
2797
  function serializeSetItem(ctx, id, item) {
2639
2798
  const base = ctx.base;
@@ -2650,10 +2809,10 @@ function serializeSetItem(ctx, id, item) {
2650
2809
  }
2651
2810
  function serializeSet(ctx, node) {
2652
2811
  let serialized = SET_CONSTRUCTOR;
2653
- const size = node.l;
2812
+ const items = node.a;
2813
+ const size = items.length;
2654
2814
  const id = node.i;
2655
- if (size) {
2656
- const items = node.a;
2815
+ if (size > 0) {
2657
2816
  ctx.base.stack.push(id);
2658
2817
  let result = serializeSetItem(ctx, id, items[0]);
2659
2818
  for (let i = 1, item = result; i < size; i++) {
@@ -2708,13 +2867,13 @@ function serializeMapEntry(ctx, id, key, val, sentinel) {
2708
2867
  }
2709
2868
  function serializeMap(ctx, node) {
2710
2869
  let serialized = MAP_CONSTRUCTOR;
2711
- const size = node.e.s;
2870
+ const keys = node.e.k;
2871
+ const size = keys.length;
2712
2872
  const id = node.i;
2713
2873
  const sentinel = node.f;
2714
2874
  const sentinelId = getRefParam(ctx, sentinel.i);
2715
2875
  const base = ctx.base;
2716
- if (size) {
2717
- const keys = node.e.k;
2876
+ if (size > 0) {
2718
2877
  const vals = node.e.v;
2719
2878
  base.stack.push(id);
2720
2879
  let result = serializeMapEntry(ctx, id, keys[0], vals[0], sentinelId);
@@ -2734,7 +2893,7 @@ function serializeMap(ctx, node) {
2734
2893
  return serialized;
2735
2894
  }
2736
2895
  function serializeArrayBuffer(ctx, node) {
2737
- return getConstructor(ctx, node.f) + "(" + node.l + ',"' + node.s + '")';
2896
+ return getConstructor(ctx, node.f) + '("' + node.s + '")';
2738
2897
  }
2739
2898
  function serializeTypedArray(ctx, node) {
2740
2899
  return "new " + node.c + "(" + serialize(ctx, node.f) + "," + node.b + "," + node.l + ")";
@@ -2914,7 +3073,7 @@ function serializeAssignable(ctx, node) {
2914
3073
  case 5 /* Date */:
2915
3074
  return serializeDate(node);
2916
3075
  case 6 /* RegExp */:
2917
- return serializeRegExp(node);
3076
+ return serializeRegExp(ctx, node);
2918
3077
  case 7 /* Set */:
2919
3078
  return serializeSet(ctx, node);
2920
3079
  case 8 /* Map */:
@@ -3026,26 +3185,28 @@ function createSyncParserContext(mode, options) {
3026
3185
  return {
3027
3186
  type: 1 /* Sync */,
3028
3187
  base: createBaseParserContext(mode, options),
3029
- child: void 0
3188
+ child: NIL
3030
3189
  };
3031
3190
  }
3032
3191
  var SyncParsePluginContext = class {
3033
- constructor(_p) {
3192
+ constructor(_p, depth) {
3034
3193
  this._p = _p;
3194
+ this.depth = depth;
3035
3195
  }
3036
3196
  parse(current) {
3037
- return parseSOS(this._p, current);
3197
+ return parseSOS(this._p, this.depth, current);
3038
3198
  }
3039
3199
  };
3040
3200
  var StreamParsePluginContext = class {
3041
- constructor(_p) {
3201
+ constructor(_p, depth) {
3042
3202
  this._p = _p;
3203
+ this.depth = depth;
3043
3204
  }
3044
3205
  parse(current) {
3045
- return parseSOS(this._p, current);
3206
+ return parseSOS(this._p, this.depth, current);
3046
3207
  }
3047
3208
  parseWithError(current) {
3048
- return parseWithError(this._p, current);
3209
+ return parseWithError(this._p, this.depth, current);
3049
3210
  }
3050
3211
  isAlive() {
3051
3212
  return this._p.state.alive;
@@ -3078,29 +3239,30 @@ function createStreamParserContext(options) {
3078
3239
  return {
3079
3240
  type: 2 /* Stream */,
3080
3241
  base: createBaseParserContext(2 /* Cross */, options),
3081
- child: void 0,
3082
3242
  state: createStreamParserState(options)
3083
3243
  };
3084
3244
  }
3085
- function parseItems2(ctx, current) {
3245
+ function parseItems2(ctx, depth, current) {
3086
3246
  const nodes = [];
3087
3247
  for (let i = 0, len = current.length; i < len; i++) {
3088
3248
  if (i in current) {
3089
- nodes[i] = parseSOS(ctx, current[i]);
3249
+ nodes[i] = parseSOS(ctx, depth, current[i]);
3250
+ } else {
3251
+ nodes[i] = 0;
3090
3252
  }
3091
3253
  }
3092
3254
  return nodes;
3093
3255
  }
3094
- function parseArray2(ctx, id, current) {
3095
- return createArrayNode(id, current, parseItems2(ctx, current));
3256
+ function parseArray2(ctx, depth, id, current) {
3257
+ return createArrayNode(id, current, parseItems2(ctx, depth, current));
3096
3258
  }
3097
- function parseProperties2(ctx, properties) {
3259
+ function parseProperties2(ctx, depth, properties) {
3098
3260
  const entries = Object.entries(properties);
3099
3261
  const keyNodes = [];
3100
3262
  const valueNodes = [];
3101
3263
  for (let i = 0, len = entries.length; i < len; i++) {
3102
3264
  keyNodes.push(serializeString(entries[i][0]));
3103
- valueNodes.push(parseSOS(ctx, entries[i][1]));
3265
+ valueNodes.push(parseSOS(ctx, depth, entries[i][1]));
3104
3266
  }
3105
3267
  if (SYM_ITERATOR in properties) {
3106
3268
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
@@ -3109,6 +3271,7 @@ function parseProperties2(ctx, properties) {
3109
3271
  parseIteratorFactory(ctx.base),
3110
3272
  parseSOS(
3111
3273
  ctx,
3274
+ depth,
3112
3275
  iteratorToSequence(properties)
3113
3276
  )
3114
3277
  )
@@ -3121,6 +3284,7 @@ function parseProperties2(ctx, properties) {
3121
3284
  parseAsyncIteratorFactory(ctx.base),
3122
3285
  parseSOS(
3123
3286
  ctx,
3287
+ depth,
3124
3288
  ctx.type === 1 /* Sync */ ? createStream() : createStreamFromAsyncIterable(
3125
3289
  properties
3126
3290
  )
@@ -3140,58 +3304,70 @@ function parseProperties2(ctx, properties) {
3140
3304
  }
3141
3305
  return {
3142
3306
  k: keyNodes,
3143
- v: valueNodes,
3144
- s: keyNodes.length
3307
+ v: valueNodes
3145
3308
  };
3146
3309
  }
3147
- function parsePlainObject2(ctx, id, current, empty) {
3148
- return createObjectNode(id, current, empty, parseProperties2(ctx, current));
3310
+ function parsePlainObject2(ctx, depth, id, current, empty) {
3311
+ return createObjectNode(
3312
+ id,
3313
+ current,
3314
+ empty,
3315
+ parseProperties2(ctx, depth, current)
3316
+ );
3149
3317
  }
3150
- function parseBoxed2(ctx, id, current) {
3151
- return createBoxedNode(id, parseSOS(ctx, current.valueOf()));
3318
+ function parseBoxed2(ctx, depth, id, current) {
3319
+ return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
3152
3320
  }
3153
- function parseTypedArray2(ctx, id, current) {
3154
- return createTypedArrayNode(id, current, parseSOS(ctx, current.buffer));
3321
+ function parseTypedArray2(ctx, depth, id, current) {
3322
+ return createTypedArrayNode(
3323
+ id,
3324
+ current,
3325
+ parseSOS(ctx, depth, current.buffer)
3326
+ );
3155
3327
  }
3156
- function parseBigIntTypedArray2(ctx, id, current) {
3157
- return createBigIntTypedArrayNode(id, current, parseSOS(ctx, current.buffer));
3328
+ function parseBigIntTypedArray2(ctx, depth, id, current) {
3329
+ return createBigIntTypedArrayNode(
3330
+ id,
3331
+ current,
3332
+ parseSOS(ctx, depth, current.buffer)
3333
+ );
3158
3334
  }
3159
- function parseDataView2(ctx, id, current) {
3160
- return createDataViewNode(id, current, parseSOS(ctx, current.buffer));
3335
+ function parseDataView2(ctx, depth, id, current) {
3336
+ return createDataViewNode(id, current, parseSOS(ctx, depth, current.buffer));
3161
3337
  }
3162
- function parseError2(ctx, id, current) {
3338
+ function parseError2(ctx, depth, id, current) {
3163
3339
  const options = getErrorOptions(current, ctx.base.features);
3164
3340
  return createErrorNode(
3165
3341
  id,
3166
3342
  current,
3167
- options ? parseProperties2(ctx, options) : NIL
3343
+ options ? parseProperties2(ctx, depth, options) : NIL
3168
3344
  );
3169
3345
  }
3170
- function parseAggregateError2(ctx, id, current) {
3346
+ function parseAggregateError2(ctx, depth, id, current) {
3171
3347
  const options = getErrorOptions(current, ctx.base.features);
3172
3348
  return createAggregateErrorNode(
3173
3349
  id,
3174
3350
  current,
3175
- options ? parseProperties2(ctx, options) : NIL
3351
+ options ? parseProperties2(ctx, depth, options) : NIL
3176
3352
  );
3177
3353
  }
3178
- function parseMap2(ctx, id, current) {
3354
+ function parseMap2(ctx, depth, id, current) {
3179
3355
  const keyNodes = [];
3180
3356
  const valueNodes = [];
3181
3357
  for (const [key, value] of current.entries()) {
3182
- keyNodes.push(parseSOS(ctx, key));
3183
- valueNodes.push(parseSOS(ctx, value));
3358
+ keyNodes.push(parseSOS(ctx, depth, key));
3359
+ valueNodes.push(parseSOS(ctx, depth, value));
3184
3360
  }
3185
- return createMapNode(ctx.base, id, keyNodes, valueNodes, current.size);
3361
+ return createMapNode(ctx.base, id, keyNodes, valueNodes);
3186
3362
  }
3187
- function parseSet2(ctx, id, current) {
3363
+ function parseSet2(ctx, depth, id, current) {
3188
3364
  const items = [];
3189
3365
  for (const item of current.keys()) {
3190
- items.push(parseSOS(ctx, item));
3366
+ items.push(parseSOS(ctx, depth, item));
3191
3367
  }
3192
- return createSetNode(id, current.size, items);
3368
+ return createSetNode(id, items);
3193
3369
  }
3194
- function parseStream2(ctx, id, current) {
3370
+ function parseStream2(ctx, depth, id, current) {
3195
3371
  const result = createStreamConstructorNode(
3196
3372
  id,
3197
3373
  parseSpecialReference(ctx.base, 4 /* StreamConstructor */),
@@ -3204,7 +3380,7 @@ function parseStream2(ctx, id, current) {
3204
3380
  current.on({
3205
3381
  next: (value) => {
3206
3382
  if (ctx.state.alive) {
3207
- const parsed = parseWithError(ctx, value);
3383
+ const parsed = parseWithError(ctx, depth, value);
3208
3384
  if (parsed) {
3209
3385
  onParse(ctx, createStreamNextNode(id, parsed));
3210
3386
  }
@@ -3212,7 +3388,7 @@ function parseStream2(ctx, id, current) {
3212
3388
  },
3213
3389
  throw: (value) => {
3214
3390
  if (ctx.state.alive) {
3215
- const parsed = parseWithError(ctx, value);
3391
+ const parsed = parseWithError(ctx, depth, value);
3216
3392
  if (parsed) {
3217
3393
  onParse(ctx, createStreamThrowNode(id, parsed));
3218
3394
  }
@@ -3221,7 +3397,7 @@ function parseStream2(ctx, id, current) {
3221
3397
  },
3222
3398
  return: (value) => {
3223
3399
  if (ctx.state.alive) {
3224
- const parsed = parseWithError(ctx, value);
3400
+ const parsed = parseWithError(ctx, depth, value);
3225
3401
  if (parsed) {
3226
3402
  onParse(ctx, createStreamReturnNode(id, parsed));
3227
3403
  }
@@ -3231,9 +3407,9 @@ function parseStream2(ctx, id, current) {
3231
3407
  });
3232
3408
  return result;
3233
3409
  }
3234
- function handlePromiseSuccess(id, data) {
3410
+ function handlePromiseSuccess(id, depth, data) {
3235
3411
  if (this.state.alive) {
3236
- const parsed = parseWithError(this, data);
3412
+ const parsed = parseWithError(this, depth, data);
3237
3413
  if (parsed) {
3238
3414
  onParse(
3239
3415
  this,
@@ -3245,13 +3421,13 @@ function handlePromiseSuccess(id, data) {
3245
3421
  NIL,
3246
3422
  NIL,
3247
3423
  NIL,
3248
- NIL,
3249
3424
  [
3250
3425
  parseSpecialReference(this.base, 2 /* PromiseSuccess */),
3251
3426
  parsed
3252
3427
  ],
3253
3428
  NIL,
3254
3429
  NIL,
3430
+ NIL,
3255
3431
  NIL
3256
3432
  )
3257
3433
  );
@@ -3259,9 +3435,9 @@ function handlePromiseSuccess(id, data) {
3259
3435
  popPendingState(this);
3260
3436
  }
3261
3437
  }
3262
- function handlePromiseFailure(id, data) {
3438
+ function handlePromiseFailure(id, depth, data) {
3263
3439
  if (this.state.alive) {
3264
- const parsed = parseWithError(this, data);
3440
+ const parsed = parseWithError(this, depth, data);
3265
3441
  if (parsed) {
3266
3442
  onParse(
3267
3443
  this,
@@ -3273,13 +3449,13 @@ function handlePromiseFailure(id, data) {
3273
3449
  NIL,
3274
3450
  NIL,
3275
3451
  NIL,
3276
- NIL,
3277
3452
  [
3278
3453
  parseSpecialReference(this.base, 3 /* PromiseFailure */),
3279
3454
  parsed
3280
3455
  ],
3281
3456
  NIL,
3282
3457
  NIL,
3458
+ NIL,
3283
3459
  NIL
3284
3460
  )
3285
3461
  );
@@ -3287,80 +3463,74 @@ function handlePromiseFailure(id, data) {
3287
3463
  }
3288
3464
  popPendingState(this);
3289
3465
  }
3290
- function parsePromise2(ctx, id, current) {
3466
+ function parsePromise2(ctx, depth, id, current) {
3291
3467
  const resolver = createIndexForValue(ctx.base, {});
3292
3468
  if (ctx.type === 2 /* Stream */) {
3293
3469
  pushPendingState(ctx);
3294
3470
  current.then(
3295
- handlePromiseSuccess.bind(ctx, resolver),
3296
- handlePromiseFailure.bind(ctx, resolver)
3471
+ handlePromiseSuccess.bind(ctx, resolver, depth),
3472
+ handlePromiseFailure.bind(ctx, resolver, depth)
3297
3473
  );
3298
3474
  }
3299
3475
  return createPromiseConstructorNode(ctx.base, id, resolver);
3300
3476
  }
3301
- function parsePluginSync(ctx, id, current, currentPlugins) {
3477
+ function parsePluginSync(ctx, depth, id, current, currentPlugins) {
3302
3478
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
3303
3479
  const plugin = currentPlugins[i];
3304
3480
  if (plugin.parse.sync && plugin.test(current)) {
3305
- if (ctx.child == null) {
3306
- ctx.child = new SyncParsePluginContext(ctx);
3307
- }
3308
3481
  return createPluginNode(
3309
3482
  id,
3310
3483
  plugin.tag,
3311
- plugin.parse.sync(current, ctx.child, {
3484
+ plugin.parse.sync(current, new SyncParsePluginContext(ctx, depth), {
3312
3485
  id
3313
3486
  })
3314
3487
  );
3315
3488
  }
3316
3489
  }
3317
- return void 0;
3490
+ return NIL;
3318
3491
  }
3319
- function parsePluginStream(ctx, id, current, currentPlugins) {
3492
+ function parsePluginStream(ctx, depth, id, current, currentPlugins) {
3320
3493
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
3321
3494
  const plugin = currentPlugins[i];
3322
3495
  if (plugin.parse.stream && plugin.test(current)) {
3323
- if (ctx.child == null) {
3324
- ctx.child = new StreamParsePluginContext(ctx);
3325
- }
3326
3496
  return createPluginNode(
3327
3497
  id,
3328
3498
  plugin.tag,
3329
- plugin.parse.stream(current, ctx.child, {
3499
+ plugin.parse.stream(current, new StreamParsePluginContext(ctx, depth), {
3330
3500
  id
3331
3501
  })
3332
3502
  );
3333
3503
  }
3334
3504
  }
3335
- return void 0;
3505
+ return NIL;
3336
3506
  }
3337
- function parsePlugin2(ctx, id, current) {
3507
+ function parsePlugin2(ctx, depth, id, current) {
3338
3508
  const currentPlugins = ctx.base.plugins;
3339
3509
  if (currentPlugins) {
3340
- return ctx.type === 1 /* Sync */ ? parsePluginSync(ctx, id, current, currentPlugins) : parsePluginStream(ctx, id, current, currentPlugins);
3510
+ return ctx.type === 1 /* Sync */ ? parsePluginSync(ctx, depth, id, current, currentPlugins) : parsePluginStream(ctx, depth, id, current, currentPlugins);
3341
3511
  }
3342
- return void 0;
3512
+ return NIL;
3343
3513
  }
3344
- function parseObjectPhase2(ctx, id, current, currentClass) {
3514
+ function parseObjectPhase2(ctx, depth, id, current, currentClass) {
3345
3515
  switch (currentClass) {
3346
3516
  case Object:
3347
3517
  return parsePlainObject2(
3348
3518
  ctx,
3519
+ depth,
3349
3520
  id,
3350
3521
  current,
3351
3522
  false
3352
3523
  );
3353
- case void 0:
3524
+ case NIL:
3354
3525
  return parsePlainObject2(
3355
3526
  ctx,
3527
+ depth,
3356
3528
  id,
3357
3529
  current,
3358
3530
  true
3359
3531
  );
3360
3532
  case Date:
3361
3533
  return createDateNode(id, current);
3362
- case RegExp:
3363
- return createRegExpNode(id, current);
3364
3534
  case Error:
3365
3535
  case EvalError:
3366
3536
  case RangeError:
@@ -3368,12 +3538,12 @@ function parseObjectPhase2(ctx, id, current, currentClass) {
3368
3538
  case SyntaxError:
3369
3539
  case TypeError:
3370
3540
  case URIError:
3371
- return parseError2(ctx, id, current);
3541
+ return parseError2(ctx, depth, id, current);
3372
3542
  case Number:
3373
3543
  case Boolean:
3374
3544
  case String:
3375
3545
  case BigInt:
3376
- return parseBoxed2(ctx, id, current);
3546
+ return parseBoxed2(ctx, depth, id, current);
3377
3547
  case ArrayBuffer:
3378
3548
  return createArrayBufferNode(
3379
3549
  ctx.base,
@@ -3389,26 +3559,40 @@ function parseObjectPhase2(ctx, id, current, currentClass) {
3389
3559
  case Uint8ClampedArray:
3390
3560
  case Float32Array:
3391
3561
  case Float64Array:
3392
- return parseTypedArray2(ctx, id, current);
3562
+ return parseTypedArray2(
3563
+ ctx,
3564
+ depth,
3565
+ id,
3566
+ current
3567
+ );
3393
3568
  case DataView:
3394
- return parseDataView2(ctx, id, current);
3569
+ return parseDataView2(ctx, depth, id, current);
3395
3570
  case Map:
3396
- return parseMap2(ctx, id, current);
3571
+ return parseMap2(
3572
+ ctx,
3573
+ depth,
3574
+ id,
3575
+ current
3576
+ );
3397
3577
  case Set:
3398
- return parseSet2(ctx, id, current);
3578
+ return parseSet2(ctx, depth, id, current);
3399
3579
  default:
3400
3580
  break;
3401
3581
  }
3402
3582
  if (currentClass === Promise || current instanceof Promise) {
3403
- return parsePromise2(ctx, id, current);
3583
+ return parsePromise2(ctx, depth, id, current);
3404
3584
  }
3405
3585
  const currentFeatures = ctx.base.features;
3586
+ if (currentFeatures & 32 /* RegExp */ && currentClass === RegExp) {
3587
+ return createRegExpNode(id, current);
3588
+ }
3406
3589
  if (currentFeatures & 16 /* BigIntTypedArray */) {
3407
3590
  switch (currentClass) {
3408
3591
  case BigInt64Array:
3409
3592
  case BigUint64Array:
3410
3593
  return parseBigIntTypedArray2(
3411
3594
  ctx,
3595
+ depth,
3412
3596
  id,
3413
3597
  current
3414
3598
  );
@@ -3417,48 +3601,57 @@ function parseObjectPhase2(ctx, id, current, currentClass) {
3417
3601
  }
3418
3602
  }
3419
3603
  if (currentFeatures & 1 /* AggregateError */ && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) {
3420
- return parseAggregateError2(ctx, id, current);
3604
+ return parseAggregateError2(
3605
+ ctx,
3606
+ depth,
3607
+ id,
3608
+ current
3609
+ );
3421
3610
  }
3422
3611
  if (current instanceof Error) {
3423
- return parseError2(ctx, id, current);
3612
+ return parseError2(ctx, depth, id, current);
3424
3613
  }
3425
3614
  if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) {
3426
- return parsePlainObject2(ctx, id, current, !!currentClass);
3615
+ return parsePlainObject2(ctx, depth, id, current, !!currentClass);
3427
3616
  }
3428
3617
  throw new SerovalUnsupportedTypeError(current);
3429
3618
  }
3430
- function parseObject(ctx, id, current) {
3619
+ function parseObject(ctx, depth, id, current) {
3431
3620
  if (Array.isArray(current)) {
3432
- return parseArray2(ctx, id, current);
3621
+ return parseArray2(ctx, depth, id, current);
3433
3622
  }
3434
3623
  if (isStream(current)) {
3435
- return parseStream2(ctx, id, current);
3624
+ return parseStream2(ctx, depth, id, current);
3436
3625
  }
3437
3626
  const currentClass = current.constructor;
3438
3627
  if (currentClass === OpaqueReference) {
3439
3628
  return parseSOS(
3440
3629
  ctx,
3630
+ depth,
3441
3631
  current.replacement
3442
3632
  );
3443
3633
  }
3444
- const parsed = parsePlugin2(ctx, id, current);
3634
+ const parsed = parsePlugin2(ctx, depth, id, current);
3445
3635
  if (parsed) {
3446
3636
  return parsed;
3447
3637
  }
3448
- return parseObjectPhase2(ctx, id, current, currentClass);
3638
+ return parseObjectPhase2(ctx, depth, id, current, currentClass);
3449
3639
  }
3450
- function parseFunction(ctx, current) {
3640
+ function parseFunction(ctx, depth, current) {
3451
3641
  const ref = getReferenceNode(ctx.base, current);
3452
3642
  if (ref.type !== 0 /* Fresh */) {
3453
3643
  return ref.value;
3454
3644
  }
3455
- const plugin = parsePlugin2(ctx, ref.value, current);
3645
+ const plugin = parsePlugin2(ctx, depth, ref.value, current);
3456
3646
  if (plugin) {
3457
3647
  return plugin;
3458
3648
  }
3459
3649
  throw new SerovalUnsupportedTypeError(current);
3460
3650
  }
3461
- function parseSOS(ctx, current) {
3651
+ function parseSOS(ctx, depth, current) {
3652
+ if (depth >= ctx.base.depthLimit) {
3653
+ throw new SerovalDepthLimitError(ctx.base.depthLimit);
3654
+ }
3462
3655
  switch (typeof current) {
3463
3656
  case "boolean":
3464
3657
  return current ? TRUE_NODE : FALSE_NODE;
@@ -3473,14 +3666,14 @@ function parseSOS(ctx, current) {
3473
3666
  case "object": {
3474
3667
  if (current) {
3475
3668
  const ref = getReferenceNode(ctx.base, current);
3476
- return ref.type === 0 /* Fresh */ ? parseObject(ctx, ref.value, current) : ref.value;
3669
+ return ref.type === 0 /* Fresh */ ? parseObject(ctx, depth + 1, ref.value, current) : ref.value;
3477
3670
  }
3478
3671
  return NULL_NODE;
3479
3672
  }
3480
3673
  case "symbol":
3481
3674
  return parseWellKnownSymbol(ctx.base, current);
3482
3675
  case "function": {
3483
- return parseFunction(ctx, current);
3676
+ return parseFunction(ctx, depth, current);
3484
3677
  }
3485
3678
  default:
3486
3679
  throw new SerovalUnsupportedTypeError(current);
@@ -3488,7 +3681,7 @@ function parseSOS(ctx, current) {
3488
3681
  }
3489
3682
  function parseTop(ctx, current) {
3490
3683
  try {
3491
- return parseSOS(ctx, current);
3684
+ return parseSOS(ctx, 0, current);
3492
3685
  } catch (error) {
3493
3686
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3494
3687
  }
@@ -3527,16 +3720,16 @@ function popPendingState(ctx) {
3527
3720
  onDone(ctx);
3528
3721
  }
3529
3722
  }
3530
- function parseWithError(ctx, current) {
3723
+ function parseWithError(ctx, depth, current) {
3531
3724
  try {
3532
- return parseSOS(ctx, current);
3725
+ return parseSOS(ctx, depth, current);
3533
3726
  } catch (err) {
3534
3727
  onError(ctx, err);
3535
3728
  return NIL;
3536
3729
  }
3537
3730
  }
3538
3731
  function startStreamParse(ctx, current) {
3539
- const parsed = parseWithError(ctx, current);
3732
+ const parsed = parseWithError(ctx, 0, current);
3540
3733
  if (parsed) {
3541
3734
  onParseInternal(ctx, parsed, true);
3542
3735
  ctx.state.initial = false;
@@ -3656,7 +3849,9 @@ function fromCrossJSON(source, options) {
3656
3849
  const plugins = resolvePlugins(options.plugins);
3657
3850
  const ctx = createCrossDeserializerContext({
3658
3851
  plugins,
3659
- refs: options.refs
3852
+ refs: options.refs,
3853
+ features: options.features,
3854
+ disabledFeatures: options.disabledFeatures
3660
3855
  });
3661
3856
  return deserializeTop(ctx, source);
3662
3857
  }
@@ -3806,10 +4001,15 @@ function compileJSON(source, options = {}) {
3806
4001
  return serializeTopVanilla(ctx, source.t);
3807
4002
  }
3808
4003
  function fromJSON(source, options = {}) {
4004
+ var _a;
3809
4005
  const plugins = resolvePlugins(options.plugins);
4006
+ const disabledFeatures = options.disabledFeatures || 0;
4007
+ const sourceFeatures = (_a = source.f) != null ? _a : ALL_ENABLED;
3810
4008
  const ctx = createVanillaDeserializerContext({
3811
4009
  plugins,
3812
- markedRefs: source.m
4010
+ markedRefs: source.m,
4011
+ features: sourceFeatures & ~disabledFeatures,
4012
+ disabledFeatures
3813
4013
  });
3814
4014
  return deserializeTop(ctx, source.t);
3815
4015
  }
@@ -3817,8 +4017,11 @@ export {
3817
4017
  Feature,
3818
4018
  OpaqueReference,
3819
4019
  Serializer,
4020
+ SerovalConflictedNodeIdError,
4021
+ SerovalDepthLimitError,
3820
4022
  SerovalDeserializationError,
3821
4023
  SerovalError,
4024
+ SerovalMalformedNodeError,
3822
4025
  SerovalMissingInstanceError,
3823
4026
  SerovalMissingPluginError,
3824
4027
  SerovalMissingReferenceError,