seroval 1.4.0 → 1.4.1

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 (68) hide show
  1. package/dist/cjs/development/index.cjs +524 -364
  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 +524 -364
  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/literals.d.ts.map +1 -1
  28. package/dist/types/core/node.d.ts +1 -1
  29. package/dist/types/core/node.d.ts.map +1 -1
  30. package/dist/types/core/string.d.ts.map +1 -1
  31. package/dist/types/core/tree/index.d.ts +4 -1
  32. package/dist/types/core/tree/index.d.ts.map +1 -1
  33. package/dist/types/core/types.d.ts +2 -11
  34. package/dist/types/core/types.d.ts.map +1 -1
  35. package/package.json +2 -3
  36. package/src/core/Serializer.ts +0 -119
  37. package/src/core/base-primitives.ts +0 -515
  38. package/src/core/compat.ts +0 -21
  39. package/src/core/constants.ts +0 -187
  40. package/src/core/constructors.ts +0 -269
  41. package/src/core/context/async-parser.ts +0 -586
  42. package/src/core/context/deserializer.ts +0 -646
  43. package/src/core/context/parser.ts +0 -333
  44. package/src/core/context/serializer.ts +0 -1411
  45. package/src/core/context/sync-parser.ts +0 -833
  46. package/src/core/cross/index.ts +0 -186
  47. package/src/core/errors.ts +0 -163
  48. package/src/core/function-string.ts +0 -32
  49. package/src/core/keys.ts +0 -17
  50. package/src/core/literals.ts +0 -43
  51. package/src/core/node.ts +0 -40
  52. package/src/core/opaque-reference.ts +0 -9
  53. package/src/core/plugin.ts +0 -100
  54. package/src/core/reference.ts +0 -66
  55. package/src/core/special-reference.ts +0 -42
  56. package/src/core/stream.ts +0 -71
  57. package/src/core/string.ts +0 -84
  58. package/src/core/symbols.ts +0 -18
  59. package/src/core/tree/index.ts +0 -125
  60. package/src/core/types.ts +0 -375
  61. package/src/core/utils/error.ts +0 -73
  62. package/src/core/utils/get-identifier.ts +0 -18
  63. package/src/core/utils/get-object-flag.ts +0 -14
  64. package/src/core/utils/is-valid-identifier.ts +0 -12
  65. package/src/core/utils/iterator-to-sequence.ts +0 -44
  66. package/src/core/utils/promise-to-result.ts +0 -9
  67. package/src/core/utils/typed-array.ts +0 -56
  68. package/src/index.ts +0 -36
@@ -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) {
115
116
  return {
116
117
  t,
117
118
  i,
118
119
  s,
119
- l,
120
120
  c,
121
121
  m,
122
122
  p,
@@ -141,7 +141,6 @@ function createConstantNode(value) {
141
141
  NIL,
142
142
  NIL,
143
143
  NIL,
144
- NIL,
145
144
  NIL
146
145
  );
147
146
  }
@@ -192,7 +191,7 @@ function serializeChar(str) {
192
191
  case "\u2029":
193
192
  return "\\u2029";
194
193
  default:
195
- return void 0;
194
+ return NIL;
196
195
  }
197
196
  }
198
197
  function serializeString(str) {
@@ -406,7 +405,6 @@ function createNumberNode(value) {
406
405
  NIL,
407
406
  NIL,
408
407
  NIL,
409
- NIL,
410
408
  NIL
411
409
  );
412
410
  }
@@ -422,7 +420,6 @@ function createStringNode(value) {
422
420
  NIL,
423
421
  NIL,
424
422
  NIL,
425
- NIL,
426
423
  NIL
427
424
  );
428
425
  }
@@ -438,7 +435,6 @@ function createBigIntNode(current) {
438
435
  NIL,
439
436
  NIL,
440
437
  NIL,
441
- NIL,
442
438
  NIL
443
439
  );
444
440
  }
@@ -454,7 +450,6 @@ function createIndexedValueNode(id) {
454
450
  NIL,
455
451
  NIL,
456
452
  NIL,
457
- NIL,
458
453
  NIL
459
454
  );
460
455
  }
@@ -471,7 +466,6 @@ function createDateNode(id, current) {
471
466
  NIL,
472
467
  NIL,
473
468
  NIL,
474
- NIL,
475
469
  NIL
476
470
  );
477
471
  }
@@ -480,7 +474,6 @@ function createRegExpNode(id, current) {
480
474
  6 /* RegExp */,
481
475
  id,
482
476
  NIL,
483
- NIL,
484
477
  serializeString(current.source),
485
478
  current.flags,
486
479
  NIL,
@@ -503,7 +496,6 @@ function createWKSymbolNode(id, current) {
503
496
  NIL,
504
497
  NIL,
505
498
  NIL,
506
- NIL,
507
499
  NIL
508
500
  );
509
501
  }
@@ -519,7 +511,6 @@ function createReferenceNode(id, ref) {
519
511
  NIL,
520
512
  NIL,
521
513
  NIL,
522
- NIL,
523
514
  NIL
524
515
  );
525
516
  }
@@ -528,7 +519,6 @@ function createPluginNode(id, tag, value) {
528
519
  25 /* Plugin */,
529
520
  id,
530
521
  value,
531
- NIL,
532
522
  serializeString(tag),
533
523
  NIL,
534
524
  NIL,
@@ -544,7 +534,6 @@ function createArrayNode(id, current, parsedItems) {
544
534
  9 /* Array */,
545
535
  id,
546
536
  NIL,
547
- current.length,
548
537
  NIL,
549
538
  NIL,
550
539
  NIL,
@@ -565,7 +554,6 @@ function createBoxedNode(id, boxed) {
565
554
  NIL,
566
555
  NIL,
567
556
  NIL,
568
- NIL,
569
557
  boxed,
570
558
  NIL,
571
559
  NIL
@@ -576,7 +564,6 @@ function createTypedArrayNode(id, current, buffer) {
576
564
  15 /* TypedArray */,
577
565
  id,
578
566
  NIL,
579
- current.length,
580
567
  current.constructor.name,
581
568
  NIL,
582
569
  NIL,
@@ -592,7 +579,6 @@ function createBigIntTypedArrayNode(id, current, buffer) {
592
579
  16 /* BigIntTypedArray */,
593
580
  id,
594
581
  NIL,
595
- current.length,
596
582
  current.constructor.name,
597
583
  NIL,
598
584
  NIL,
@@ -608,7 +594,6 @@ function createDataViewNode(id, current, buffer) {
608
594
  20 /* DataView */,
609
595
  id,
610
596
  NIL,
611
- current.byteLength,
612
597
  NIL,
613
598
  NIL,
614
599
  NIL,
@@ -625,7 +610,6 @@ function createErrorNode(id, current, options) {
625
610
  id,
626
611
  getErrorConstructor(current),
627
612
  NIL,
628
- NIL,
629
613
  serializeString(current.message),
630
614
  options,
631
615
  NIL,
@@ -641,7 +625,6 @@ function createAggregateErrorNode(id, current, options) {
641
625
  id,
642
626
  getErrorConstructor(current),
643
627
  NIL,
644
- NIL,
645
628
  serializeString(current.message),
646
629
  options,
647
630
  NIL,
@@ -651,12 +634,11 @@ function createAggregateErrorNode(id, current, options) {
651
634
  NIL
652
635
  );
653
636
  }
654
- function createSetNode(id, size, items) {
637
+ function createSetNode(id, items) {
655
638
  return createSerovalNode(
656
639
  7 /* Set */,
657
640
  id,
658
641
  NIL,
659
- size,
660
642
  NIL,
661
643
  NIL,
662
644
  NIL,
@@ -676,7 +658,6 @@ function createIteratorFactoryInstanceNode(factory, items) {
676
658
  NIL,
677
659
  NIL,
678
660
  NIL,
679
- NIL,
680
661
  [factory, items],
681
662
  NIL,
682
663
  NIL,
@@ -692,7 +673,6 @@ function createAsyncIteratorFactoryInstanceNode(factory, items) {
692
673
  NIL,
693
674
  NIL,
694
675
  NIL,
695
- NIL,
696
676
  [factory, items],
697
677
  NIL,
698
678
  NIL,
@@ -708,7 +688,6 @@ function createStreamConstructorNode(id, factory, sequence) {
708
688
  NIL,
709
689
  NIL,
710
690
  NIL,
711
- NIL,
712
691
  sequence,
713
692
  factory,
714
693
  NIL,
@@ -725,7 +704,6 @@ function createStreamNextNode(id, parsed) {
725
704
  NIL,
726
705
  NIL,
727
706
  NIL,
728
- NIL,
729
707
  parsed,
730
708
  NIL,
731
709
  NIL
@@ -741,7 +719,6 @@ function createStreamThrowNode(id, parsed) {
741
719
  NIL,
742
720
  NIL,
743
721
  NIL,
744
- NIL,
745
722
  parsed,
746
723
  NIL,
747
724
  NIL
@@ -757,7 +734,6 @@ function createStreamReturnNode(id, parsed) {
757
734
  NIL,
758
735
  NIL,
759
736
  NIL,
760
- NIL,
761
737
  parsed,
762
738
  NIL,
763
739
  NIL
@@ -859,6 +835,27 @@ var SerovalUnknownTypedArrayError = class extends Error {
859
835
  );
860
836
  }
861
837
  };
838
+ var SerovalMalformedNodeError = class extends Error {
839
+ constructor(node) {
840
+ super(
841
+ false ? getSpecificErrorMessage(8 /* MalformedNode */) : 'Malformed node type "' + node.t + '".'
842
+ );
843
+ }
844
+ };
845
+ var SerovalConflictedNodeIdError = class extends Error {
846
+ constructor(node) {
847
+ super(
848
+ false ? getSpecificErrorMessage(9 /* ConflictedNodeId */) : 'Conflicted node id "' + node.i + '".'
849
+ );
850
+ }
851
+ };
852
+ var SerovalDepthLimitError = class extends Error {
853
+ constructor(limit) {
854
+ super(
855
+ false ? getSpecificErrorMessage(9 /* ConflictedNodeId */) : "Depth limit of " + limit + " reached"
856
+ );
857
+ }
858
+ };
862
859
 
863
860
  // src/core/opaque-reference.ts
864
861
  var OpaqueReference = class {
@@ -1068,8 +1065,9 @@ var ASYNC_ITERATOR_CONSTRUCTOR = (symbol, createPromise) => (stream) => () => {
1068
1065
  return instance;
1069
1066
  };
1070
1067
  var SERIALIZED_ASYNC_ITERATOR_CONSTRUCTOR = /* @__PURE__ */ ASYNC_ITERATOR_CONSTRUCTOR.toString();
1071
- var ARRAY_BUFFER_CONSTRUCTOR = (length, b64) => {
1068
+ var ARRAY_BUFFER_CONSTRUCTOR = (b64) => {
1072
1069
  const decoded = atob(b64);
1070
+ const length = decoded.length;
1073
1071
  const arr = new Uint8Array(length);
1074
1072
  for (let i = 0; i < length; i++) {
1075
1073
  arr[i] = decoded.charCodeAt(i);
@@ -1181,7 +1179,8 @@ function createBaseParserContext(mode, options) {
1181
1179
  mode,
1182
1180
  marked: /* @__PURE__ */ new Set(),
1183
1181
  features: ALL_ENABLED ^ (options.disabledFeatures || 0),
1184
- refs: options.refs || /* @__PURE__ */ new Map()
1182
+ refs: options.refs || /* @__PURE__ */ new Map(),
1183
+ depthLimit: options.depthLimit || 1e3
1185
1184
  };
1186
1185
  }
1187
1186
  function markParserRef(ctx, id) {
@@ -1245,7 +1244,6 @@ function parseSpecialReference(ctx, ref) {
1245
1244
  NIL,
1246
1245
  NIL,
1247
1246
  NIL,
1248
- NIL,
1249
1247
  NIL
1250
1248
  );
1251
1249
  }
@@ -1263,7 +1261,6 @@ function parseIteratorFactory(ctx) {
1263
1261
  NIL,
1264
1262
  NIL,
1265
1263
  NIL,
1266
- NIL,
1267
1264
  parseWellKnownSymbol(ctx, SYM_ITERATOR),
1268
1265
  NIL,
1269
1266
  NIL
@@ -1282,7 +1279,6 @@ function parseAsyncIteratorFactory(ctx) {
1282
1279
  NIL,
1283
1280
  NIL,
1284
1281
  NIL,
1285
- NIL,
1286
1282
  [
1287
1283
  parseSpecialReference(ctx, 1 /* PromiseConstructor */),
1288
1284
  parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR)
@@ -1299,7 +1295,6 @@ function createObjectNode(id, current, empty, record) {
1299
1295
  NIL,
1300
1296
  NIL,
1301
1297
  NIL,
1302
- NIL,
1303
1298
  record,
1304
1299
  NIL,
1305
1300
  NIL,
@@ -1308,7 +1303,7 @@ function createObjectNode(id, current, empty, record) {
1308
1303
  getObjectFlag(current)
1309
1304
  );
1310
1305
  }
1311
- function createMapNode(ctx, id, k, v, s) {
1306
+ function createMapNode(ctx, id, k, v) {
1312
1307
  return createSerovalNode(
1313
1308
  8 /* Map */,
1314
1309
  id,
@@ -1316,8 +1311,7 @@ function createMapNode(ctx, id, k, v, s) {
1316
1311
  NIL,
1317
1312
  NIL,
1318
1313
  NIL,
1319
- NIL,
1320
- { k, v, s },
1314
+ { k, v },
1321
1315
  NIL,
1322
1316
  parseSpecialReference(ctx, 0 /* MapSentinel */),
1323
1317
  NIL,
@@ -1334,7 +1328,6 @@ function createPromiseConstructorNode(ctx, id, resolver) {
1334
1328
  NIL,
1335
1329
  NIL,
1336
1330
  NIL,
1337
- NIL,
1338
1331
  parseSpecialReference(ctx, 1 /* PromiseConstructor */),
1339
1332
  NIL,
1340
1333
  NIL
@@ -1342,16 +1335,14 @@ function createPromiseConstructorNode(ctx, id, resolver) {
1342
1335
  }
1343
1336
  function createArrayBufferNode(ctx, id, current) {
1344
1337
  const bytes = new Uint8Array(current);
1345
- const len = bytes.length;
1346
1338
  let result = "";
1347
- for (let i = 0; i < len; i++) {
1339
+ for (let i = 0, len = bytes.length; i < len; i++) {
1348
1340
  result += String.fromCharCode(bytes[i]);
1349
1341
  }
1350
1342
  return createSerovalNode(
1351
1343
  19 /* ArrayBuffer */,
1352
1344
  id,
1353
1345
  serializeString(btoa(result)),
1354
- len,
1355
1346
  NIL,
1356
1347
  NIL,
1357
1348
  NIL,
@@ -1371,32 +1362,35 @@ function createAsyncParserContext(mode, options) {
1371
1362
  };
1372
1363
  }
1373
1364
  var AsyncParsePluginContext = class {
1374
- constructor(_p) {
1365
+ constructor(_p, depth) {
1375
1366
  this._p = _p;
1367
+ this.depth = depth;
1376
1368
  }
1377
1369
  parse(current) {
1378
- return parseAsync(this._p, current);
1370
+ return parseAsync(this._p, this.depth, current);
1379
1371
  }
1380
1372
  };
1381
- async function parseItems(ctx, current) {
1373
+ async function parseItems(ctx, depth, current) {
1382
1374
  const nodes = [];
1383
1375
  for (let i = 0, len = current.length; i < len; i++) {
1384
1376
  if (i in current) {
1385
- nodes[i] = await parseAsync(ctx, current[i]);
1377
+ nodes[i] = await parseAsync(ctx, depth, current[i]);
1378
+ } else {
1379
+ nodes[i] = 0;
1386
1380
  }
1387
1381
  }
1388
1382
  return nodes;
1389
1383
  }
1390
- async function parseArray(ctx, id, current) {
1391
- return createArrayNode(id, current, await parseItems(ctx, current));
1384
+ async function parseArray(ctx, depth, id, current) {
1385
+ return createArrayNode(id, current, await parseItems(ctx, depth, current));
1392
1386
  }
1393
- async function parseProperties(ctx, properties) {
1387
+ async function parseProperties(ctx, depth, properties) {
1394
1388
  const entries = Object.entries(properties);
1395
1389
  const keyNodes = [];
1396
1390
  const valueNodes = [];
1397
1391
  for (let i = 0, len = entries.length; i < len; i++) {
1398
1392
  keyNodes.push(serializeString(entries[i][0]));
1399
- valueNodes.push(await parseAsync(ctx, entries[i][1]));
1393
+ valueNodes.push(await parseAsync(ctx, depth, entries[i][1]));
1400
1394
  }
1401
1395
  if (SYM_ITERATOR in properties) {
1402
1396
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
@@ -1405,6 +1399,7 @@ async function parseProperties(ctx, properties) {
1405
1399
  parseIteratorFactory(ctx.base),
1406
1400
  await parseAsync(
1407
1401
  ctx,
1402
+ depth,
1408
1403
  iteratorToSequence(properties)
1409
1404
  )
1410
1405
  )
@@ -1417,6 +1412,7 @@ async function parseProperties(ctx, properties) {
1417
1412
  parseAsyncIteratorFactory(ctx.base),
1418
1413
  await parseAsync(
1419
1414
  ctx,
1415
+ depth,
1420
1416
  createStreamFromAsyncIterable(
1421
1417
  properties
1422
1418
  )
@@ -1436,92 +1432,96 @@ async function parseProperties(ctx, properties) {
1436
1432
  }
1437
1433
  return {
1438
1434
  k: keyNodes,
1439
- v: valueNodes,
1440
- s: keyNodes.length
1435
+ v: valueNodes
1441
1436
  };
1442
1437
  }
1443
- async function parsePlainObject(ctx, id, current, empty) {
1438
+ async function parsePlainObject(ctx, depth, id, current, empty) {
1444
1439
  return createObjectNode(
1445
1440
  id,
1446
1441
  current,
1447
1442
  empty,
1448
- await parseProperties(ctx, current)
1443
+ await parseProperties(ctx, depth, current)
1449
1444
  );
1450
1445
  }
1451
- async function parseBoxed(ctx, id, current) {
1452
- return createBoxedNode(id, await parseAsync(ctx, current.valueOf()));
1446
+ async function parseBoxed(ctx, depth, id, current) {
1447
+ return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1453
1448
  }
1454
- async function parseTypedArray(ctx, id, current) {
1449
+ async function parseTypedArray(ctx, depth, id, current) {
1455
1450
  return createTypedArrayNode(
1456
1451
  id,
1457
1452
  current,
1458
- await parseAsync(ctx, current.buffer)
1453
+ await parseAsync(ctx, depth, current.buffer)
1459
1454
  );
1460
1455
  }
1461
- async function parseBigIntTypedArray(ctx, id, current) {
1456
+ async function parseBigIntTypedArray(ctx, depth, id, current) {
1462
1457
  return createBigIntTypedArrayNode(
1463
1458
  id,
1464
1459
  current,
1465
- await parseAsync(ctx, current.buffer)
1460
+ await parseAsync(ctx, depth, current.buffer)
1466
1461
  );
1467
1462
  }
1468
- async function parseDataView(ctx, id, current) {
1469
- return createDataViewNode(id, current, await parseAsync(ctx, current.buffer));
1463
+ async function parseDataView(ctx, depth, id, current) {
1464
+ return createDataViewNode(
1465
+ id,
1466
+ current,
1467
+ await parseAsync(ctx, depth, current.buffer)
1468
+ );
1470
1469
  }
1471
- async function parseError(ctx, id, current) {
1470
+ async function parseError(ctx, depth, id, current) {
1472
1471
  const options = getErrorOptions(current, ctx.base.features);
1473
1472
  return createErrorNode(
1474
1473
  id,
1475
1474
  current,
1476
- options ? await parseProperties(ctx, options) : NIL
1475
+ options ? await parseProperties(ctx, depth, options) : NIL
1477
1476
  );
1478
1477
  }
1479
- async function parseAggregateError(ctx, id, current) {
1478
+ async function parseAggregateError(ctx, depth, id, current) {
1480
1479
  const options = getErrorOptions(current, ctx.base.features);
1481
1480
  return createAggregateErrorNode(
1482
1481
  id,
1483
1482
  current,
1484
- options ? await parseProperties(ctx, options) : NIL
1483
+ options ? await parseProperties(ctx, depth, options) : NIL
1485
1484
  );
1486
1485
  }
1487
- async function parseMap(ctx, id, current) {
1486
+ async function parseMap(ctx, depth, id, current) {
1488
1487
  const keyNodes = [];
1489
1488
  const valueNodes = [];
1490
1489
  for (const [key, value] of current.entries()) {
1491
- keyNodes.push(await parseAsync(ctx, key));
1492
- valueNodes.push(await parseAsync(ctx, value));
1490
+ keyNodes.push(await parseAsync(ctx, depth, key));
1491
+ valueNodes.push(await parseAsync(ctx, depth, value));
1493
1492
  }
1494
- return createMapNode(ctx.base, id, keyNodes, valueNodes, current.size);
1493
+ return createMapNode(ctx.base, id, keyNodes, valueNodes);
1495
1494
  }
1496
- async function parseSet(ctx, id, current) {
1495
+ async function parseSet(ctx, depth, id, current) {
1497
1496
  const items = [];
1498
1497
  for (const item of current.keys()) {
1499
- items.push(await parseAsync(ctx, item));
1498
+ items.push(await parseAsync(ctx, depth, item));
1500
1499
  }
1501
- return createSetNode(id, current.size, items);
1500
+ return createSetNode(id, items);
1502
1501
  }
1503
- async function parsePlugin(ctx, id, current) {
1502
+ async function parsePlugin(ctx, depth, id, current) {
1504
1503
  const currentPlugins = ctx.base.plugins;
1505
1504
  if (currentPlugins) {
1506
1505
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
1507
1506
  const plugin = currentPlugins[i];
1508
1507
  if (plugin.parse.async && plugin.test(current)) {
1509
- if (ctx.child == null) {
1510
- ctx.child = new AsyncParsePluginContext(ctx);
1511
- }
1512
1508
  return createPluginNode(
1513
1509
  id,
1514
1510
  plugin.tag,
1515
- await plugin.parse.async(current, ctx.child, {
1516
- id
1517
- })
1511
+ await plugin.parse.async(
1512
+ current,
1513
+ new AsyncParsePluginContext(ctx, depth),
1514
+ {
1515
+ id
1516
+ }
1517
+ )
1518
1518
  );
1519
1519
  }
1520
1520
  }
1521
1521
  }
1522
1522
  return NIL;
1523
1523
  }
1524
- async function parsePromise(ctx, id, current) {
1524
+ async function parsePromise(ctx, depth, id, current) {
1525
1525
  const [status, result] = await promiseToResult(current);
1526
1526
  return createSerovalNode(
1527
1527
  12 /* Promise */,
@@ -1532,18 +1532,17 @@ async function parsePromise(ctx, id, current) {
1532
1532
  NIL,
1533
1533
  NIL,
1534
1534
  NIL,
1535
- NIL,
1536
- await parseAsync(ctx, result),
1535
+ await parseAsync(ctx, depth, result),
1537
1536
  NIL,
1538
1537
  NIL
1539
1538
  );
1540
1539
  }
1541
- function parseStreamHandle(id, current, resolve, reject) {
1540
+ function parseStreamHandle(depth, id, current, resolve, reject) {
1542
1541
  const sequence = [];
1543
1542
  const cleanup = current.on({
1544
1543
  next: (value) => {
1545
1544
  markParserRef(this.base, id);
1546
- parseAsync(this, value).then(
1545
+ parseAsync(this, depth, value).then(
1547
1546
  (data) => {
1548
1547
  sequence.push(createStreamNextNode(id, data));
1549
1548
  },
@@ -1555,7 +1554,7 @@ function parseStreamHandle(id, current, resolve, reject) {
1555
1554
  },
1556
1555
  throw: (value) => {
1557
1556
  markParserRef(this.base, id);
1558
- parseAsync(this, value).then(
1557
+ parseAsync(this, depth, value).then(
1559
1558
  (data) => {
1560
1559
  sequence.push(createStreamThrowNode(id, data));
1561
1560
  resolve(sequence);
@@ -1569,7 +1568,7 @@ function parseStreamHandle(id, current, resolve, reject) {
1569
1568
  },
1570
1569
  return: (value) => {
1571
1570
  markParserRef(this.base, id);
1572
- parseAsync(this, value).then(
1571
+ parseAsync(this, depth, value).then(
1573
1572
  (data) => {
1574
1573
  sequence.push(createStreamReturnNode(id, data));
1575
1574
  resolve(sequence);
@@ -1583,28 +1582,31 @@ function parseStreamHandle(id, current, resolve, reject) {
1583
1582
  }
1584
1583
  });
1585
1584
  }
1586
- async function parseStream(ctx, id, current) {
1585
+ async function parseStream(ctx, depth, id, current) {
1587
1586
  return createStreamConstructorNode(
1588
1587
  id,
1589
1588
  parseSpecialReference(ctx.base, 4 /* StreamConstructor */),
1590
- await new Promise(parseStreamHandle.bind(ctx, id, current))
1589
+ await new Promise(
1590
+ parseStreamHandle.bind(ctx, depth, id, current)
1591
+ )
1591
1592
  );
1592
1593
  }
1593
- async function parseObjectAsync(ctx, id, current) {
1594
+ async function parseObjectAsync(ctx, depth, id, current) {
1594
1595
  if (Array.isArray(current)) {
1595
- return parseArray(ctx, id, current);
1596
+ return parseArray(ctx, depth, id, current);
1596
1597
  }
1597
1598
  if (isStream(current)) {
1598
- return parseStream(ctx, id, current);
1599
+ return parseStream(ctx, depth, id, current);
1599
1600
  }
1600
1601
  const currentClass = current.constructor;
1601
1602
  if (currentClass === OpaqueReference) {
1602
1603
  return parseAsync(
1603
1604
  ctx,
1605
+ depth,
1604
1606
  current.replacement
1605
1607
  );
1606
1608
  }
1607
- const parsed = await parsePlugin(ctx, id, current);
1609
+ const parsed = await parsePlugin(ctx, depth, id, current);
1608
1610
  if (parsed) {
1609
1611
  return parsed;
1610
1612
  }
@@ -1612,6 +1614,7 @@ async function parseObjectAsync(ctx, id, current) {
1612
1614
  case Object:
1613
1615
  return parsePlainObject(
1614
1616
  ctx,
1617
+ depth,
1615
1618
  id,
1616
1619
  current,
1617
1620
  false
@@ -1619,14 +1622,13 @@ async function parseObjectAsync(ctx, id, current) {
1619
1622
  case NIL:
1620
1623
  return parsePlainObject(
1621
1624
  ctx,
1625
+ depth,
1622
1626
  id,
1623
1627
  current,
1624
1628
  true
1625
1629
  );
1626
1630
  case Date:
1627
1631
  return createDateNode(id, current);
1628
- case RegExp:
1629
- return createRegExpNode(id, current);
1630
1632
  case Error:
1631
1633
  case EvalError:
1632
1634
  case RangeError:
@@ -1634,12 +1636,12 @@ async function parseObjectAsync(ctx, id, current) {
1634
1636
  case SyntaxError:
1635
1637
  case TypeError:
1636
1638
  case URIError:
1637
- return parseError(ctx, id, current);
1639
+ return parseError(ctx, depth, id, current);
1638
1640
  case Number:
1639
1641
  case Boolean:
1640
1642
  case String:
1641
1643
  case BigInt:
1642
- return parseBoxed(ctx, id, current);
1644
+ return parseBoxed(ctx, depth, id, current);
1643
1645
  case ArrayBuffer:
1644
1646
  return createArrayBufferNode(
1645
1647
  ctx.base,
@@ -1655,26 +1657,40 @@ async function parseObjectAsync(ctx, id, current) {
1655
1657
  case Uint8ClampedArray:
1656
1658
  case Float32Array:
1657
1659
  case Float64Array:
1658
- return parseTypedArray(ctx, id, current);
1660
+ return parseTypedArray(
1661
+ ctx,
1662
+ depth,
1663
+ id,
1664
+ current
1665
+ );
1659
1666
  case DataView:
1660
- return parseDataView(ctx, id, current);
1667
+ return parseDataView(ctx, depth, id, current);
1661
1668
  case Map:
1662
- return parseMap(ctx, id, current);
1669
+ return parseMap(
1670
+ ctx,
1671
+ depth,
1672
+ id,
1673
+ current
1674
+ );
1663
1675
  case Set:
1664
- return parseSet(ctx, id, current);
1676
+ return parseSet(ctx, depth, id, current);
1665
1677
  default:
1666
1678
  break;
1667
1679
  }
1668
1680
  if (currentClass === Promise || current instanceof Promise) {
1669
- return parsePromise(ctx, id, current);
1681
+ return parsePromise(ctx, depth, id, current);
1670
1682
  }
1671
1683
  const currentFeatures = ctx.base.features;
1684
+ if (currentFeatures & 32 /* RegExp */ && currentClass === RegExp) {
1685
+ return createRegExpNode(id, current);
1686
+ }
1672
1687
  if (currentFeatures & 16 /* BigIntTypedArray */) {
1673
1688
  switch (currentClass) {
1674
1689
  case BigInt64Array:
1675
1690
  case BigUint64Array:
1676
1691
  return parseBigIntTypedArray(
1677
1692
  ctx,
1693
+ depth,
1678
1694
  id,
1679
1695
  current
1680
1696
  );
@@ -1683,28 +1699,33 @@ async function parseObjectAsync(ctx, id, current) {
1683
1699
  }
1684
1700
  }
1685
1701
  if (currentFeatures & 1 /* AggregateError */ && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) {
1686
- return parseAggregateError(ctx, id, current);
1702
+ return parseAggregateError(
1703
+ ctx,
1704
+ depth,
1705
+ id,
1706
+ current
1707
+ );
1687
1708
  }
1688
1709
  if (current instanceof Error) {
1689
- return parseError(ctx, id, current);
1710
+ return parseError(ctx, depth, id, current);
1690
1711
  }
1691
1712
  if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) {
1692
- return parsePlainObject(ctx, id, current, !!currentClass);
1713
+ return parsePlainObject(ctx, depth, id, current, !!currentClass);
1693
1714
  }
1694
1715
  throw new SerovalUnsupportedTypeError(current);
1695
1716
  }
1696
- async function parseFunctionAsync(ctx, current) {
1717
+ async function parseFunctionAsync(ctx, depth, current) {
1697
1718
  const ref = getReferenceNode(ctx.base, current);
1698
1719
  if (ref.type !== 0 /* Fresh */) {
1699
1720
  return ref.value;
1700
1721
  }
1701
- const plugin = await parsePlugin(ctx, ref.value, current);
1722
+ const plugin = await parsePlugin(ctx, depth, ref.value, current);
1702
1723
  if (plugin) {
1703
1724
  return plugin;
1704
1725
  }
1705
1726
  throw new SerovalUnsupportedTypeError(current);
1706
1727
  }
1707
- async function parseAsync(ctx, current) {
1728
+ async function parseAsync(ctx, depth, current) {
1708
1729
  switch (typeof current) {
1709
1730
  case "boolean":
1710
1731
  return current ? TRUE_NODE : FALSE_NODE;
@@ -1719,21 +1740,21 @@ async function parseAsync(ctx, current) {
1719
1740
  case "object": {
1720
1741
  if (current) {
1721
1742
  const ref = getReferenceNode(ctx.base, current);
1722
- return ref.type === 0 ? await parseObjectAsync(ctx, ref.value, current) : ref.value;
1743
+ return ref.type === 0 ? await parseObjectAsync(ctx, depth + 1, ref.value, current) : ref.value;
1723
1744
  }
1724
1745
  return NULL_NODE;
1725
1746
  }
1726
1747
  case "symbol":
1727
1748
  return parseWellKnownSymbol(ctx.base, current);
1728
1749
  case "function":
1729
- return parseFunctionAsync(ctx, current);
1750
+ return parseFunctionAsync(ctx, depth, current);
1730
1751
  default:
1731
1752
  throw new SerovalUnsupportedTypeError(current);
1732
1753
  }
1733
1754
  }
1734
1755
  async function parseTopAsync(ctx, current) {
1735
1756
  try {
1736
- return await parseAsync(ctx, current);
1757
+ return await parseAsync(ctx, 0, current);
1737
1758
  } catch (error) {
1738
1759
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
1739
1760
  }
@@ -1799,6 +1820,9 @@ function getTypedArrayConstructor(name) {
1799
1820
  }
1800
1821
 
1801
1822
  // src/core/context/deserializer.ts
1823
+ var MAX_BASE64_LENGTH = 1e6;
1824
+ var MAX_BIGINT_LENGTH = 1e4;
1825
+ var MAX_REGEXP_SOURCE_LENGTH = 2e4;
1802
1826
  function applyObjectFlag(obj, flag) {
1803
1827
  switch (flag) {
1804
1828
  case 3 /* Frozen */:
@@ -1811,18 +1835,22 @@ function applyObjectFlag(obj, flag) {
1811
1835
  return obj;
1812
1836
  }
1813
1837
  }
1838
+ var DEFAULT_DEPTH_LIMIT = 1e3;
1814
1839
  function createBaseDeserializerContext(mode, options) {
1840
+ var _a;
1815
1841
  return {
1816
1842
  mode,
1817
1843
  plugins: options.plugins,
1818
- refs: options.refs || /* @__PURE__ */ new Map()
1844
+ refs: options.refs || /* @__PURE__ */ new Map(),
1845
+ features: (_a = options.features) != null ? _a : ALL_ENABLED ^ (options.disabledFeatures || 0),
1846
+ depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT
1819
1847
  };
1820
1848
  }
1821
1849
  function createVanillaDeserializerContext(options) {
1822
1850
  return {
1823
1851
  mode: 1 /* Vanilla */,
1824
1852
  base: createBaseDeserializerContext(1 /* Vanilla */, options),
1825
- child: void 0,
1853
+ child: NIL,
1826
1854
  state: {
1827
1855
  marked: new Set(options.markedRefs)
1828
1856
  }
@@ -1832,32 +1860,50 @@ function createCrossDeserializerContext(options) {
1832
1860
  return {
1833
1861
  mode: 2 /* Cross */,
1834
1862
  base: createBaseDeserializerContext(2 /* Cross */, options),
1835
- child: void 0
1863
+ child: NIL
1836
1864
  };
1837
1865
  }
1838
1866
  var DeserializePluginContext = class {
1839
- constructor(_p) {
1867
+ constructor(_p, depth) {
1840
1868
  this._p = _p;
1869
+ this.depth = depth;
1841
1870
  }
1842
1871
  deserialize(node) {
1843
- return deserialize(this._p, node);
1872
+ return deserialize(this._p, this.depth, node);
1844
1873
  }
1845
1874
  };
1875
+ function guardIndexedValue(ctx, id) {
1876
+ if (id < 0 || !Number.isFinite(id) || !Number.isInteger(id)) {
1877
+ throw new SerovalMalformedNodeError({
1878
+ t: 4 /* IndexedValue */,
1879
+ i: id
1880
+ });
1881
+ }
1882
+ if (ctx.refs.has(id)) {
1883
+ throw new Error("Conflicted ref id: " + id);
1884
+ }
1885
+ }
1846
1886
  function assignIndexedValueVanilla(ctx, id, value) {
1887
+ guardIndexedValue(ctx.base, id);
1847
1888
  if (ctx.state.marked.has(id)) {
1848
1889
  ctx.base.refs.set(id, value);
1849
1890
  }
1850
1891
  return value;
1851
1892
  }
1852
1893
  function assignIndexedValueCross(ctx, id, value) {
1853
- if (!ctx.base.refs.has(id)) {
1854
- ctx.base.refs.set(id, value);
1855
- }
1894
+ guardIndexedValue(ctx.base, id);
1895
+ ctx.base.refs.set(id, value);
1856
1896
  return value;
1857
1897
  }
1858
1898
  function assignIndexedValue(ctx, id, value) {
1859
1899
  return ctx.mode === 1 /* Vanilla */ ? assignIndexedValueVanilla(ctx, id, value) : assignIndexedValueCross(ctx, id, value);
1860
1900
  }
1901
+ function deserializeKnownValue(node, record, key) {
1902
+ if (Object.hasOwn(record, key)) {
1903
+ return record[key];
1904
+ }
1905
+ throw new SerovalMalformedNodeError(node);
1906
+ }
1861
1907
  function deserializeReference(ctx, node) {
1862
1908
  return assignIndexedValue(
1863
1909
  ctx,
@@ -1865,46 +1911,96 @@ function deserializeReference(ctx, node) {
1865
1911
  getReference(deserializeString(node.s))
1866
1912
  );
1867
1913
  }
1868
- function deserializeArray(ctx, node) {
1869
- const len = node.l;
1914
+ function deserializeArray(ctx, depth, node) {
1915
+ const items = node.a;
1916
+ const len = items.length;
1870
1917
  const result = assignIndexedValue(
1871
1918
  ctx,
1872
1919
  node.i,
1873
1920
  new Array(len)
1874
1921
  );
1875
- let item;
1876
- for (let i = 0; i < len; i++) {
1877
- item = node.a[i];
1922
+ for (let i = 0, item; i < len; i++) {
1923
+ item = items[i];
1878
1924
  if (item) {
1879
- result[i] = deserialize(ctx, item);
1925
+ result[i] = deserialize(ctx, depth, item);
1880
1926
  }
1881
1927
  }
1882
1928
  applyObjectFlag(result, node.o);
1883
1929
  return result;
1884
1930
  }
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
- }
1931
+ function isValidKey(key) {
1932
+ switch (key) {
1933
+ case "constructor":
1934
+ case "__proto__":
1935
+ case "prototype":
1936
+ case "__defineGetter__":
1937
+ case "__defineSetter__":
1938
+ case "__lookupGetter__":
1939
+ case "__lookupSetter__":
1940
+ return false;
1941
+ default:
1942
+ return true;
1943
+ }
1944
+ }
1945
+ function isValidSymbol(symbol) {
1946
+ switch (symbol) {
1947
+ case SYM_ASYNC_ITERATOR:
1948
+ case SYM_IS_CONCAT_SPREADABLE:
1949
+ case SYM_TO_STRING_TAG:
1950
+ case SYM_ITERATOR:
1951
+ return true;
1952
+ default:
1953
+ return false;
1954
+ }
1955
+ }
1956
+ function assignStringProperty(object, key, value) {
1957
+ if (isValidKey(key)) {
1958
+ object[key] = value;
1959
+ } else {
1960
+ Object.defineProperty(object, key, {
1961
+ value,
1962
+ configurable: true,
1963
+ enumerable: true,
1964
+ writable: true
1965
+ });
1966
+ }
1967
+ }
1968
+ function assignProperty(ctx, depth, object, key, value) {
1969
+ if (typeof key === "string") {
1970
+ assignStringProperty(object, key, deserialize(ctx, depth, value));
1971
+ } else {
1972
+ const actual = deserialize(ctx, depth, key);
1973
+ switch (typeof actual) {
1974
+ case "string":
1975
+ assignStringProperty(object, actual, deserialize(ctx, depth, value));
1976
+ break;
1977
+ case "symbol":
1978
+ if (isValidSymbol(actual)) {
1979
+ object[actual] = deserialize(ctx, depth, value);
1980
+ }
1981
+ break;
1982
+ default:
1983
+ throw new SerovalMalformedNodeError(key);
1984
+ }
1985
+ }
1986
+ }
1987
+ function deserializeProperties(ctx, depth, node, result) {
1988
+ const keys = node.k;
1989
+ const len = keys.length;
1990
+ if (len > 0) {
1991
+ for (let i = 0, vals = node.v, len2 = keys.length; i < len2; i++) {
1992
+ assignProperty(ctx, depth, result, keys[i], vals[i]);
1897
1993
  }
1898
1994
  }
1899
1995
  return result;
1900
1996
  }
1901
- function deserializeObject(ctx, node) {
1997
+ function deserializeObject(ctx, depth, node) {
1902
1998
  const result = assignIndexedValue(
1903
1999
  ctx,
1904
2000
  node.i,
1905
2001
  node.t === 10 /* Object */ ? {} : /* @__PURE__ */ Object.create(null)
1906
2002
  );
1907
- deserializeProperties(ctx, node.p, result);
2003
+ deserializeProperties(ctx, depth, node.p, result);
1908
2004
  applyObjectFlag(result, node.o);
1909
2005
  return result;
1910
2006
  }
@@ -1912,84 +2008,92 @@ function deserializeDate(ctx, node) {
1912
2008
  return assignIndexedValue(ctx, node.i, new Date(node.s));
1913
2009
  }
1914
2010
  function deserializeRegExp(ctx, node) {
1915
- return assignIndexedValue(
1916
- ctx,
1917
- node.i,
1918
- new RegExp(deserializeString(node.c), node.m)
1919
- );
2011
+ if (ctx.base.features & 32 /* RegExp */) {
2012
+ const source = deserializeString(node.c);
2013
+ if (source.length > MAX_REGEXP_SOURCE_LENGTH) {
2014
+ throw new SerovalMalformedNodeError(node);
2015
+ }
2016
+ return assignIndexedValue(ctx, node.i, new RegExp(source, node.m));
2017
+ }
2018
+ throw new SerovalUnsupportedNodeError(node);
1920
2019
  }
1921
- function deserializeSet(ctx, node) {
2020
+ function deserializeSet(ctx, depth, node) {
1922
2021
  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]));
2022
+ for (let i = 0, items = node.a, len = items.length; i < len; i++) {
2023
+ result.add(deserialize(ctx, depth, items[i]));
1926
2024
  }
1927
2025
  return result;
1928
2026
  }
1929
- function deserializeMap(ctx, node) {
2027
+ function deserializeMap(ctx, depth, node) {
1930
2028
  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]));
2029
+ for (let i = 0, keys = node.e.k, vals = node.e.v, len = keys.length; i < len; i++) {
2030
+ result.set(
2031
+ deserialize(ctx, depth, keys[i]),
2032
+ deserialize(ctx, depth, vals[i])
2033
+ );
1935
2034
  }
1936
2035
  return result;
1937
2036
  }
1938
2037
  function deserializeArrayBuffer(ctx, node) {
2038
+ if (node.s.length > MAX_BASE64_LENGTH) {
2039
+ throw new SerovalMalformedNodeError(node);
2040
+ }
1939
2041
  const result = assignIndexedValue(
1940
2042
  ctx,
1941
2043
  node.i,
1942
- ARRAY_BUFFER_CONSTRUCTOR(node.l, deserializeString(node.s))
2044
+ ARRAY_BUFFER_CONSTRUCTOR(deserializeString(node.s))
1943
2045
  );
1944
2046
  return result;
1945
2047
  }
1946
- function deserializeTypedArray(ctx, node) {
2048
+ function deserializeTypedArray(ctx, depth, node) {
2049
+ var _a;
1947
2050
  const construct = getTypedArrayConstructor(node.c);
1948
- const source = deserialize(ctx, node.f);
1949
- const result = assignIndexedValue(
1950
- ctx,
1951
- node.i,
1952
- new construct(source, node.b, node.l)
1953
- );
2051
+ const source = deserialize(ctx, depth, node.f);
2052
+ const offset = (_a = node.b) != null ? _a : 0;
2053
+ if (offset < 0 || offset > source.byteLength) {
2054
+ throw new SerovalMalformedNodeError(node);
2055
+ }
2056
+ const result = assignIndexedValue(ctx, node.i, new construct(source, offset));
1954
2057
  return result;
1955
2058
  }
1956
- function deserializeDataView(ctx, node) {
1957
- const source = deserialize(ctx, node.f);
1958
- const result = assignIndexedValue(
1959
- ctx,
1960
- node.i,
1961
- new DataView(source, node.b, node.l)
1962
- );
2059
+ function deserializeDataView(ctx, depth, node) {
2060
+ var _a;
2061
+ const source = deserialize(ctx, depth, node.f);
2062
+ const offset = (_a = node.b) != null ? _a : 0;
2063
+ if (offset < 0 || offset > source.byteLength) {
2064
+ throw new SerovalMalformedNodeError(node);
2065
+ }
2066
+ const result = assignIndexedValue(ctx, node.i, new DataView(source, offset));
1963
2067
  return result;
1964
2068
  }
1965
- function deserializeDictionary(ctx, node, result) {
2069
+ function deserializeDictionary(ctx, depth, node, result) {
1966
2070
  if (node.p) {
1967
- const fields = deserializeProperties(ctx, node.p, {});
1968
- Object.assign(result, fields);
2071
+ const fields = deserializeProperties(ctx, depth, node.p, {});
2072
+ Object.defineProperties(result, Object.getOwnPropertyDescriptors(fields));
1969
2073
  }
1970
2074
  return result;
1971
2075
  }
1972
- function deserializeAggregateError(ctx, node) {
2076
+ function deserializeAggregateError(ctx, depth, node) {
1973
2077
  const result = assignIndexedValue(
1974
2078
  ctx,
1975
2079
  node.i,
1976
2080
  new AggregateError([], deserializeString(node.m))
1977
2081
  );
1978
- return deserializeDictionary(ctx, node, result);
2082
+ return deserializeDictionary(ctx, depth, node, result);
1979
2083
  }
1980
- function deserializeError(ctx, node) {
1981
- const construct = ERROR_CONSTRUCTOR[node.s];
2084
+ function deserializeError(ctx, depth, node) {
2085
+ const construct = deserializeKnownValue(node, ERROR_CONSTRUCTOR, node.s);
1982
2086
  const result = assignIndexedValue(
1983
2087
  ctx,
1984
2088
  node.i,
1985
2089
  new construct(deserializeString(node.m))
1986
2090
  );
1987
- return deserializeDictionary(ctx, node, result);
2091
+ return deserializeDictionary(ctx, depth, node, result);
1988
2092
  }
1989
- function deserializePromise(ctx, node) {
2093
+ function deserializePromise(ctx, depth, node) {
1990
2094
  const deferred = PROMISE_CONSTRUCTOR();
1991
2095
  const result = assignIndexedValue(ctx, node.i, deferred.p);
1992
- const deserialized = deserialize(ctx, node.f);
2096
+ const deserialized = deserialize(ctx, depth, node.f);
1993
2097
  if (node.s) {
1994
2098
  deferred.s(deserialized);
1995
2099
  } else {
@@ -1997,23 +2101,25 @@ function deserializePromise(ctx, node) {
1997
2101
  }
1998
2102
  return result;
1999
2103
  }
2000
- function deserializeBoxed(ctx, node) {
2001
- return assignIndexedValue(ctx, node.i, Object(deserialize(ctx, node.f)));
2104
+ function deserializeBoxed(ctx, depth, node) {
2105
+ return assignIndexedValue(
2106
+ ctx,
2107
+ node.i,
2108
+ // biome-ignore lint/style/useConsistentBuiltinInstantiation: intended
2109
+ Object(deserialize(ctx, depth, node.f))
2110
+ );
2002
2111
  }
2003
- function deserializePlugin(ctx, node) {
2112
+ function deserializePlugin(ctx, depth, node) {
2004
2113
  const currentPlugins = ctx.base.plugins;
2005
2114
  if (currentPlugins) {
2006
2115
  const tag = deserializeString(node.c);
2007
2116
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
2008
2117
  const plugin = currentPlugins[i];
2009
2118
  if (plugin.tag === tag) {
2010
- if (ctx.child == null) {
2011
- ctx.child = new DeserializePluginContext(ctx);
2012
- }
2013
2119
  return assignIndexedValue(
2014
2120
  ctx,
2015
2121
  node.i,
2016
- plugin.deserialize(node.s, ctx.child, {
2122
+ plugin.deserialize(node.s, new DeserializePluginContext(ctx, depth), {
2017
2123
  id: node.i
2018
2124
  })
2019
2125
  );
@@ -2029,142 +2135,150 @@ function deserializePromiseConstructor(ctx, node) {
2029
2135
  assignIndexedValue(ctx, node.s, PROMISE_CONSTRUCTOR()).p
2030
2136
  );
2031
2137
  }
2032
- function deserializePromiseResolve(ctx, node) {
2138
+ function deserializePromiseResolve(ctx, depth, node) {
2033
2139
  const deferred = ctx.base.refs.get(node.i);
2034
2140
  if (deferred) {
2035
- deferred.s(deserialize(ctx, node.a[1]));
2036
- return void 0;
2141
+ deferred.s(deserialize(ctx, depth, node.a[1]));
2142
+ return NIL;
2037
2143
  }
2038
2144
  throw new SerovalMissingInstanceError("Promise");
2039
2145
  }
2040
- function deserializePromiseReject(ctx, node) {
2146
+ function deserializePromiseReject(ctx, depth, node) {
2041
2147
  const deferred = ctx.base.refs.get(node.i);
2042
2148
  if (deferred) {
2043
- deferred.f(deserialize(ctx, node.a[1]));
2044
- return void 0;
2149
+ deferred.f(deserialize(ctx, depth, node.a[1]));
2150
+ return NIL;
2045
2151
  }
2046
2152
  throw new SerovalMissingInstanceError("Promise");
2047
2153
  }
2048
- function deserializeIteratorFactoryInstance(ctx, node) {
2049
- deserialize(ctx, node.a[0]);
2050
- const source = deserialize(ctx, node.a[1]);
2154
+ function deserializeIteratorFactoryInstance(ctx, depth, node) {
2155
+ deserialize(ctx, depth, node.a[0]);
2156
+ const source = deserialize(ctx, depth, node.a[1]);
2051
2157
  return sequenceToIterator(source);
2052
2158
  }
2053
- function deserializeAsyncIteratorFactoryInstance(ctx, node) {
2054
- deserialize(ctx, node.a[0]);
2055
- const source = deserialize(ctx, node.a[1]);
2159
+ function deserializeAsyncIteratorFactoryInstance(ctx, depth, node) {
2160
+ deserialize(ctx, depth, node.a[0]);
2161
+ const source = deserialize(ctx, depth, node.a[1]);
2056
2162
  return streamToAsyncIterable(source);
2057
2163
  }
2058
- function deserializeStreamConstructor(ctx, node) {
2164
+ function deserializeStreamConstructor(ctx, depth, node) {
2059
2165
  const result = assignIndexedValue(ctx, node.i, createStream());
2060
- const len = node.a.length;
2166
+ const items = node.a;
2167
+ const len = items.length;
2061
2168
  if (len) {
2062
2169
  for (let i = 0; i < len; i++) {
2063
- deserialize(ctx, node.a[i]);
2170
+ deserialize(ctx, depth, items[i]);
2064
2171
  }
2065
2172
  }
2066
2173
  return result;
2067
2174
  }
2068
- function deserializeStreamNext(ctx, node) {
2175
+ function deserializeStreamNext(ctx, depth, node) {
2069
2176
  const deferred = ctx.base.refs.get(node.i);
2070
- if (deferred) {
2071
- deferred.next(deserialize(ctx, node.f));
2072
- return void 0;
2177
+ if (deferred && isStream(deferred)) {
2178
+ deferred.next(deserialize(ctx, depth, node.f));
2179
+ return NIL;
2073
2180
  }
2074
2181
  throw new SerovalMissingInstanceError("Stream");
2075
2182
  }
2076
- function deserializeStreamThrow(ctx, node) {
2183
+ function deserializeStreamThrow(ctx, depth, node) {
2077
2184
  const deferred = ctx.base.refs.get(node.i);
2078
- if (deferred) {
2079
- deferred.throw(deserialize(ctx, node.f));
2080
- return void 0;
2185
+ if (deferred && isStream(deferred)) {
2186
+ deferred.throw(deserialize(ctx, depth, node.f));
2187
+ return NIL;
2081
2188
  }
2082
2189
  throw new SerovalMissingInstanceError("Stream");
2083
2190
  }
2084
- function deserializeStreamReturn(ctx, node) {
2191
+ function deserializeStreamReturn(ctx, depth, node) {
2085
2192
  const deferred = ctx.base.refs.get(node.i);
2086
- if (deferred) {
2087
- deferred.return(deserialize(ctx, node.f));
2088
- return void 0;
2193
+ if (deferred && isStream(deferred)) {
2194
+ deferred.return(deserialize(ctx, depth, node.f));
2195
+ return NIL;
2089
2196
  }
2090
2197
  throw new SerovalMissingInstanceError("Stream");
2091
2198
  }
2092
- function deserializeIteratorFactory(ctx, node) {
2093
- deserialize(ctx, node.f);
2094
- return void 0;
2199
+ function deserializeIteratorFactory(ctx, depth, node) {
2200
+ deserialize(ctx, depth, node.f);
2201
+ return NIL;
2095
2202
  }
2096
- function deserializeAsyncIteratorFactory(ctx, node) {
2097
- deserialize(ctx, node.a[1]);
2098
- return void 0;
2203
+ function deserializeAsyncIteratorFactory(ctx, depth, node) {
2204
+ deserialize(ctx, depth, node.a[1]);
2205
+ return NIL;
2099
2206
  }
2100
- function deserialize(ctx, node) {
2207
+ function deserialize(ctx, depth, node) {
2208
+ if (depth > ctx.base.depthLimit) {
2209
+ throw new SerovalDepthLimitError(ctx.base.depthLimit);
2210
+ }
2211
+ depth += 1;
2101
2212
  switch (node.t) {
2102
2213
  case 2 /* Constant */:
2103
- return CONSTANT_VAL[node.s];
2214
+ return deserializeKnownValue(node, CONSTANT_VAL, node.s);
2104
2215
  case 0 /* Number */:
2105
- return node.s;
2216
+ return Number(node.s);
2106
2217
  case 1 /* String */:
2107
- return deserializeString(node.s);
2218
+ return deserializeString(String(node.s));
2108
2219
  case 3 /* BigInt */:
2220
+ if (String(node.s).length > MAX_BIGINT_LENGTH) {
2221
+ throw new SerovalMalformedNodeError(node);
2222
+ }
2109
2223
  return BigInt(node.s);
2110
2224
  case 4 /* IndexedValue */:
2111
2225
  return ctx.base.refs.get(node.i);
2112
2226
  case 18 /* Reference */:
2113
2227
  return deserializeReference(ctx, node);
2114
2228
  case 9 /* Array */:
2115
- return deserializeArray(ctx, node);
2229
+ return deserializeArray(ctx, depth, node);
2116
2230
  case 10 /* Object */:
2117
2231
  case 11 /* NullConstructor */:
2118
- return deserializeObject(ctx, node);
2232
+ return deserializeObject(ctx, depth, node);
2119
2233
  case 5 /* Date */:
2120
2234
  return deserializeDate(ctx, node);
2121
2235
  case 6 /* RegExp */:
2122
2236
  return deserializeRegExp(ctx, node);
2123
2237
  case 7 /* Set */:
2124
- return deserializeSet(ctx, node);
2238
+ return deserializeSet(ctx, depth, node);
2125
2239
  case 8 /* Map */:
2126
- return deserializeMap(ctx, node);
2240
+ return deserializeMap(ctx, depth, node);
2127
2241
  case 19 /* ArrayBuffer */:
2128
2242
  return deserializeArrayBuffer(ctx, node);
2129
2243
  case 16 /* BigIntTypedArray */:
2130
2244
  case 15 /* TypedArray */:
2131
- return deserializeTypedArray(ctx, node);
2245
+ return deserializeTypedArray(ctx, depth, node);
2132
2246
  case 20 /* DataView */:
2133
- return deserializeDataView(ctx, node);
2247
+ return deserializeDataView(ctx, depth, node);
2134
2248
  case 14 /* AggregateError */:
2135
- return deserializeAggregateError(ctx, node);
2249
+ return deserializeAggregateError(ctx, depth, node);
2136
2250
  case 13 /* Error */:
2137
- return deserializeError(ctx, node);
2251
+ return deserializeError(ctx, depth, node);
2138
2252
  case 12 /* Promise */:
2139
- return deserializePromise(ctx, node);
2253
+ return deserializePromise(ctx, depth, node);
2140
2254
  case 17 /* WKSymbol */:
2141
- return SYMBOL_REF[node.s];
2255
+ return deserializeKnownValue(node, SYMBOL_REF, node.s);
2142
2256
  case 21 /* Boxed */:
2143
- return deserializeBoxed(ctx, node);
2257
+ return deserializeBoxed(ctx, depth, node);
2144
2258
  case 25 /* Plugin */:
2145
- return deserializePlugin(ctx, node);
2259
+ return deserializePlugin(ctx, depth, node);
2146
2260
  case 22 /* PromiseConstructor */:
2147
2261
  return deserializePromiseConstructor(ctx, node);
2148
2262
  case 23 /* PromiseSuccess */:
2149
- return deserializePromiseResolve(ctx, node);
2263
+ return deserializePromiseResolve(ctx, depth, node);
2150
2264
  case 24 /* PromiseFailure */:
2151
- return deserializePromiseReject(ctx, node);
2265
+ return deserializePromiseReject(ctx, depth, node);
2152
2266
  case 28 /* IteratorFactoryInstance */:
2153
- return deserializeIteratorFactoryInstance(ctx, node);
2267
+ return deserializeIteratorFactoryInstance(ctx, depth, node);
2154
2268
  case 30 /* AsyncIteratorFactoryInstance */:
2155
- return deserializeAsyncIteratorFactoryInstance(ctx, node);
2269
+ return deserializeAsyncIteratorFactoryInstance(ctx, depth, node);
2156
2270
  case 31 /* StreamConstructor */:
2157
- return deserializeStreamConstructor(ctx, node);
2271
+ return deserializeStreamConstructor(ctx, depth, node);
2158
2272
  case 32 /* StreamNext */:
2159
- return deserializeStreamNext(ctx, node);
2273
+ return deserializeStreamNext(ctx, depth, node);
2160
2274
  case 33 /* StreamThrow */:
2161
- return deserializeStreamThrow(ctx, node);
2275
+ return deserializeStreamThrow(ctx, depth, node);
2162
2276
  case 34 /* StreamReturn */:
2163
- return deserializeStreamReturn(ctx, node);
2277
+ return deserializeStreamReturn(ctx, depth, node);
2164
2278
  case 27 /* IteratorFactory */:
2165
- return deserializeIteratorFactory(ctx, node);
2279
+ return deserializeIteratorFactory(ctx, depth, node);
2166
2280
  case 29 /* AsyncIteratorFactory */:
2167
- return deserializeAsyncIteratorFactory(ctx, node);
2281
+ return deserializeAsyncIteratorFactory(ctx, depth, node);
2168
2282
  // case SerovalNodeType.SpecialReference:
2169
2283
  default:
2170
2284
  throw new SerovalUnsupportedNodeError(node);
@@ -2172,7 +2286,7 @@ function deserialize(ctx, node) {
2172
2286
  }
2173
2287
  function deserializeTop(ctx, node) {
2174
2288
  try {
2175
- return deserialize(ctx, node);
2289
+ return deserialize(ctx, 0, node);
2176
2290
  } catch (error) {
2177
2291
  throw new SerovalDeserializationError(error);
2178
2292
  }
@@ -2320,7 +2434,7 @@ function createVanillaSerializerContext(options) {
2320
2434
  mode: 1 /* Vanilla */,
2321
2435
  base: createBaseSerializerContext(1 /* Vanilla */, options),
2322
2436
  state: createVanillaSerializerState(),
2323
- child: void 0
2437
+ child: NIL
2324
2438
  };
2325
2439
  }
2326
2440
  function createCrossSerializerContext(options) {
@@ -2328,7 +2442,7 @@ function createCrossSerializerContext(options) {
2328
2442
  mode: 2 /* Cross */,
2329
2443
  base: createBaseSerializerContext(2 /* Cross */, options),
2330
2444
  state: options,
2331
- child: void 0
2445
+ child: NIL
2332
2446
  };
2333
2447
  }
2334
2448
  var SerializePluginContext = class {
@@ -2460,12 +2574,13 @@ function serializeArrayItem(ctx, id, item, index) {
2460
2574
  }
2461
2575
  function serializeArray(ctx, node) {
2462
2576
  const id = node.i;
2463
- if (node.l) {
2577
+ const list = node.a;
2578
+ const len = list.length;
2579
+ if (len > 0) {
2464
2580
  ctx.base.stack.push(id);
2465
- const list = node.a;
2466
2581
  let values = serializeArrayItem(ctx, id, list[0], 0);
2467
2582
  let isHoley = values === "";
2468
- for (let i = 1, len = node.l, item; i < len; i++) {
2583
+ for (let i = 1, item; i < len; i++) {
2469
2584
  item = serializeArrayItem(ctx, id, list[i], i);
2470
2585
  values += "," + item;
2471
2586
  isHoley = item === "";
@@ -2511,9 +2626,9 @@ function serializeProperty(ctx, source, key, val) {
2511
2626
  return "[" + serialize(ctx, key) + "]:" + serialize(ctx, val);
2512
2627
  }
2513
2628
  function serializeProperties(ctx, source, record) {
2514
- const len = record.s;
2515
- if (len) {
2516
- const keys = record.k;
2629
+ const keys = record.k;
2630
+ const len = keys.length;
2631
+ if (len > 0) {
2517
2632
  const values = record.v;
2518
2633
  ctx.base.stack.push(source.i);
2519
2634
  let result = serializeProperty(ctx, source, keys[0], values[0]);
@@ -2596,10 +2711,10 @@ function serializeAssignment(ctx, source, mainAssignments, key, value) {
2596
2711
  }
2597
2712
  }
2598
2713
  function serializeAssignments(ctx, source, node) {
2599
- const len = node.s;
2600
- if (len) {
2714
+ const keys = node.k;
2715
+ const len = keys.length;
2716
+ if (len > 0) {
2601
2717
  const mainAssignments = [];
2602
- const keys = node.k;
2603
2718
  const values = node.v;
2604
2719
  ctx.base.stack.push(source.i);
2605
2720
  for (let i = 0; i < len; i++) {
@@ -2632,8 +2747,11 @@ function serializeNullConstructor(ctx, node) {
2632
2747
  function serializeDate(node) {
2633
2748
  return 'new Date("' + node.s + '")';
2634
2749
  }
2635
- function serializeRegExp(node) {
2636
- return "/" + node.c + "/" + node.m;
2750
+ function serializeRegExp(ctx, node) {
2751
+ if (ctx.base.features & 32 /* RegExp */) {
2752
+ return "/" + node.c + "/" + node.m;
2753
+ }
2754
+ throw new SerovalUnsupportedNodeError(node);
2637
2755
  }
2638
2756
  function serializeSetItem(ctx, id, item) {
2639
2757
  const base = ctx.base;
@@ -2650,10 +2768,10 @@ function serializeSetItem(ctx, id, item) {
2650
2768
  }
2651
2769
  function serializeSet(ctx, node) {
2652
2770
  let serialized = SET_CONSTRUCTOR;
2653
- const size = node.l;
2771
+ const items = node.a;
2772
+ const size = items.length;
2654
2773
  const id = node.i;
2655
- if (size) {
2656
- const items = node.a;
2774
+ if (size > 0) {
2657
2775
  ctx.base.stack.push(id);
2658
2776
  let result = serializeSetItem(ctx, id, items[0]);
2659
2777
  for (let i = 1, item = result; i < size; i++) {
@@ -2708,13 +2826,13 @@ function serializeMapEntry(ctx, id, key, val, sentinel) {
2708
2826
  }
2709
2827
  function serializeMap(ctx, node) {
2710
2828
  let serialized = MAP_CONSTRUCTOR;
2711
- const size = node.e.s;
2829
+ const keys = node.e.k;
2830
+ const size = keys.length;
2712
2831
  const id = node.i;
2713
2832
  const sentinel = node.f;
2714
2833
  const sentinelId = getRefParam(ctx, sentinel.i);
2715
2834
  const base = ctx.base;
2716
- if (size) {
2717
- const keys = node.e.k;
2835
+ if (size > 0) {
2718
2836
  const vals = node.e.v;
2719
2837
  base.stack.push(id);
2720
2838
  let result = serializeMapEntry(ctx, id, keys[0], vals[0], sentinelId);
@@ -2734,13 +2852,13 @@ function serializeMap(ctx, node) {
2734
2852
  return serialized;
2735
2853
  }
2736
2854
  function serializeArrayBuffer(ctx, node) {
2737
- return getConstructor(ctx, node.f) + "(" + node.l + ',"' + node.s + '")';
2855
+ return getConstructor(ctx, node.f) + '("' + node.s + '")';
2738
2856
  }
2739
2857
  function serializeTypedArray(ctx, node) {
2740
- return "new " + node.c + "(" + serialize(ctx, node.f) + "," + node.b + "," + node.l + ")";
2858
+ return "new " + node.c + "(" + serialize(ctx, node.f) + "," + node.b + ")";
2741
2859
  }
2742
2860
  function serializeDataView(ctx, node) {
2743
- return "new DataView(" + serialize(ctx, node.f) + "," + node.b + "," + node.l + ")";
2861
+ return "new DataView(" + serialize(ctx, node.f) + "," + node.b + ")";
2744
2862
  }
2745
2863
  function serializeAggregateError(ctx, node) {
2746
2864
  const id = node.i;
@@ -2914,7 +3032,7 @@ function serializeAssignable(ctx, node) {
2914
3032
  case 5 /* Date */:
2915
3033
  return serializeDate(node);
2916
3034
  case 6 /* RegExp */:
2917
- return serializeRegExp(node);
3035
+ return serializeRegExp(ctx, node);
2918
3036
  case 7 /* Set */:
2919
3037
  return serializeSet(ctx, node);
2920
3038
  case 8 /* Map */:
@@ -3026,26 +3144,28 @@ function createSyncParserContext(mode, options) {
3026
3144
  return {
3027
3145
  type: 1 /* Sync */,
3028
3146
  base: createBaseParserContext(mode, options),
3029
- child: void 0
3147
+ child: NIL
3030
3148
  };
3031
3149
  }
3032
3150
  var SyncParsePluginContext = class {
3033
- constructor(_p) {
3151
+ constructor(_p, depth) {
3034
3152
  this._p = _p;
3153
+ this.depth = depth;
3035
3154
  }
3036
3155
  parse(current) {
3037
- return parseSOS(this._p, current);
3156
+ return parseSOS(this._p, this.depth, current);
3038
3157
  }
3039
3158
  };
3040
3159
  var StreamParsePluginContext = class {
3041
- constructor(_p) {
3160
+ constructor(_p, depth) {
3042
3161
  this._p = _p;
3162
+ this.depth = depth;
3043
3163
  }
3044
3164
  parse(current) {
3045
- return parseSOS(this._p, current);
3165
+ return parseSOS(this._p, this.depth, current);
3046
3166
  }
3047
3167
  parseWithError(current) {
3048
- return parseWithError(this._p, current);
3168
+ return parseWithError(this._p, this.depth, current);
3049
3169
  }
3050
3170
  isAlive() {
3051
3171
  return this._p.state.alive;
@@ -3078,29 +3198,30 @@ function createStreamParserContext(options) {
3078
3198
  return {
3079
3199
  type: 2 /* Stream */,
3080
3200
  base: createBaseParserContext(2 /* Cross */, options),
3081
- child: void 0,
3082
3201
  state: createStreamParserState(options)
3083
3202
  };
3084
3203
  }
3085
- function parseItems2(ctx, current) {
3204
+ function parseItems2(ctx, depth, current) {
3086
3205
  const nodes = [];
3087
3206
  for (let i = 0, len = current.length; i < len; i++) {
3088
3207
  if (i in current) {
3089
- nodes[i] = parseSOS(ctx, current[i]);
3208
+ nodes[i] = parseSOS(ctx, depth, current[i]);
3209
+ } else {
3210
+ nodes[i] = 0;
3090
3211
  }
3091
3212
  }
3092
3213
  return nodes;
3093
3214
  }
3094
- function parseArray2(ctx, id, current) {
3095
- return createArrayNode(id, current, parseItems2(ctx, current));
3215
+ function parseArray2(ctx, depth, id, current) {
3216
+ return createArrayNode(id, current, parseItems2(ctx, depth, current));
3096
3217
  }
3097
- function parseProperties2(ctx, properties) {
3218
+ function parseProperties2(ctx, depth, properties) {
3098
3219
  const entries = Object.entries(properties);
3099
3220
  const keyNodes = [];
3100
3221
  const valueNodes = [];
3101
3222
  for (let i = 0, len = entries.length; i < len; i++) {
3102
3223
  keyNodes.push(serializeString(entries[i][0]));
3103
- valueNodes.push(parseSOS(ctx, entries[i][1]));
3224
+ valueNodes.push(parseSOS(ctx, depth, entries[i][1]));
3104
3225
  }
3105
3226
  if (SYM_ITERATOR in properties) {
3106
3227
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
@@ -3109,6 +3230,7 @@ function parseProperties2(ctx, properties) {
3109
3230
  parseIteratorFactory(ctx.base),
3110
3231
  parseSOS(
3111
3232
  ctx,
3233
+ depth,
3112
3234
  iteratorToSequence(properties)
3113
3235
  )
3114
3236
  )
@@ -3121,6 +3243,7 @@ function parseProperties2(ctx, properties) {
3121
3243
  parseAsyncIteratorFactory(ctx.base),
3122
3244
  parseSOS(
3123
3245
  ctx,
3246
+ depth,
3124
3247
  ctx.type === 1 /* Sync */ ? createStream() : createStreamFromAsyncIterable(
3125
3248
  properties
3126
3249
  )
@@ -3140,58 +3263,70 @@ function parseProperties2(ctx, properties) {
3140
3263
  }
3141
3264
  return {
3142
3265
  k: keyNodes,
3143
- v: valueNodes,
3144
- s: keyNodes.length
3266
+ v: valueNodes
3145
3267
  };
3146
3268
  }
3147
- function parsePlainObject2(ctx, id, current, empty) {
3148
- return createObjectNode(id, current, empty, parseProperties2(ctx, current));
3269
+ function parsePlainObject2(ctx, depth, id, current, empty) {
3270
+ return createObjectNode(
3271
+ id,
3272
+ current,
3273
+ empty,
3274
+ parseProperties2(ctx, depth, current)
3275
+ );
3149
3276
  }
3150
- function parseBoxed2(ctx, id, current) {
3151
- return createBoxedNode(id, parseSOS(ctx, current.valueOf()));
3277
+ function parseBoxed2(ctx, depth, id, current) {
3278
+ return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
3152
3279
  }
3153
- function parseTypedArray2(ctx, id, current) {
3154
- return createTypedArrayNode(id, current, parseSOS(ctx, current.buffer));
3280
+ function parseTypedArray2(ctx, depth, id, current) {
3281
+ return createTypedArrayNode(
3282
+ id,
3283
+ current,
3284
+ parseSOS(ctx, depth, current.buffer)
3285
+ );
3155
3286
  }
3156
- function parseBigIntTypedArray2(ctx, id, current) {
3157
- return createBigIntTypedArrayNode(id, current, parseSOS(ctx, current.buffer));
3287
+ function parseBigIntTypedArray2(ctx, depth, id, current) {
3288
+ return createBigIntTypedArrayNode(
3289
+ id,
3290
+ current,
3291
+ parseSOS(ctx, depth, current.buffer)
3292
+ );
3158
3293
  }
3159
- function parseDataView2(ctx, id, current) {
3160
- return createDataViewNode(id, current, parseSOS(ctx, current.buffer));
3294
+ function parseDataView2(ctx, depth, id, current) {
3295
+ return createDataViewNode(id, current, parseSOS(ctx, depth, current.buffer));
3161
3296
  }
3162
- function parseError2(ctx, id, current) {
3297
+ function parseError2(ctx, depth, id, current) {
3163
3298
  const options = getErrorOptions(current, ctx.base.features);
3164
3299
  return createErrorNode(
3165
3300
  id,
3166
3301
  current,
3167
- options ? parseProperties2(ctx, options) : NIL
3302
+ options ? parseProperties2(ctx, depth, options) : NIL
3168
3303
  );
3169
3304
  }
3170
- function parseAggregateError2(ctx, id, current) {
3305
+ function parseAggregateError2(ctx, depth, id, current) {
3171
3306
  const options = getErrorOptions(current, ctx.base.features);
3172
3307
  return createAggregateErrorNode(
3173
3308
  id,
3174
3309
  current,
3175
- options ? parseProperties2(ctx, options) : NIL
3310
+ options ? parseProperties2(ctx, depth, options) : NIL
3176
3311
  );
3177
3312
  }
3178
- function parseMap2(ctx, id, current) {
3313
+ function parseMap2(ctx, depth, id, current) {
3179
3314
  const keyNodes = [];
3180
3315
  const valueNodes = [];
3181
3316
  for (const [key, value] of current.entries()) {
3182
- keyNodes.push(parseSOS(ctx, key));
3183
- valueNodes.push(parseSOS(ctx, value));
3317
+ keyNodes.push(parseSOS(ctx, depth, key));
3318
+ valueNodes.push(parseSOS(ctx, depth, value));
3184
3319
  }
3185
- return createMapNode(ctx.base, id, keyNodes, valueNodes, current.size);
3320
+ return createMapNode(ctx.base, id, keyNodes, valueNodes);
3186
3321
  }
3187
- function parseSet2(ctx, id, current) {
3322
+ function parseSet2(ctx, depth, id, current) {
3188
3323
  const items = [];
3189
3324
  for (const item of current.keys()) {
3190
- items.push(parseSOS(ctx, item));
3325
+ items.push(parseSOS(ctx, depth, item));
3191
3326
  }
3192
- return createSetNode(id, current.size, items);
3327
+ return createSetNode(id, items);
3193
3328
  }
3194
- function parseStream2(ctx, id, current) {
3329
+ function parseStream2(ctx, depth, id, current) {
3195
3330
  const result = createStreamConstructorNode(
3196
3331
  id,
3197
3332
  parseSpecialReference(ctx.base, 4 /* StreamConstructor */),
@@ -3204,7 +3339,7 @@ function parseStream2(ctx, id, current) {
3204
3339
  current.on({
3205
3340
  next: (value) => {
3206
3341
  if (ctx.state.alive) {
3207
- const parsed = parseWithError(ctx, value);
3342
+ const parsed = parseWithError(ctx, depth, value);
3208
3343
  if (parsed) {
3209
3344
  onParse(ctx, createStreamNextNode(id, parsed));
3210
3345
  }
@@ -3212,7 +3347,7 @@ function parseStream2(ctx, id, current) {
3212
3347
  },
3213
3348
  throw: (value) => {
3214
3349
  if (ctx.state.alive) {
3215
- const parsed = parseWithError(ctx, value);
3350
+ const parsed = parseWithError(ctx, depth, value);
3216
3351
  if (parsed) {
3217
3352
  onParse(ctx, createStreamThrowNode(id, parsed));
3218
3353
  }
@@ -3221,7 +3356,7 @@ function parseStream2(ctx, id, current) {
3221
3356
  },
3222
3357
  return: (value) => {
3223
3358
  if (ctx.state.alive) {
3224
- const parsed = parseWithError(ctx, value);
3359
+ const parsed = parseWithError(ctx, depth, value);
3225
3360
  if (parsed) {
3226
3361
  onParse(ctx, createStreamReturnNode(id, parsed));
3227
3362
  }
@@ -3231,9 +3366,9 @@ function parseStream2(ctx, id, current) {
3231
3366
  });
3232
3367
  return result;
3233
3368
  }
3234
- function handlePromiseSuccess(id, data) {
3369
+ function handlePromiseSuccess(id, depth, data) {
3235
3370
  if (this.state.alive) {
3236
- const parsed = parseWithError(this, data);
3371
+ const parsed = parseWithError(this, depth, data);
3237
3372
  if (parsed) {
3238
3373
  onParse(
3239
3374
  this,
@@ -3245,7 +3380,6 @@ function handlePromiseSuccess(id, data) {
3245
3380
  NIL,
3246
3381
  NIL,
3247
3382
  NIL,
3248
- NIL,
3249
3383
  [
3250
3384
  parseSpecialReference(this.base, 2 /* PromiseSuccess */),
3251
3385
  parsed
@@ -3259,9 +3393,9 @@ function handlePromiseSuccess(id, data) {
3259
3393
  popPendingState(this);
3260
3394
  }
3261
3395
  }
3262
- function handlePromiseFailure(id, data) {
3396
+ function handlePromiseFailure(id, depth, data) {
3263
3397
  if (this.state.alive) {
3264
- const parsed = parseWithError(this, data);
3398
+ const parsed = parseWithError(this, depth, data);
3265
3399
  if (parsed) {
3266
3400
  onParse(
3267
3401
  this,
@@ -3273,7 +3407,6 @@ function handlePromiseFailure(id, data) {
3273
3407
  NIL,
3274
3408
  NIL,
3275
3409
  NIL,
3276
- NIL,
3277
3410
  [
3278
3411
  parseSpecialReference(this.base, 3 /* PromiseFailure */),
3279
3412
  parsed
@@ -3287,80 +3420,74 @@ function handlePromiseFailure(id, data) {
3287
3420
  }
3288
3421
  popPendingState(this);
3289
3422
  }
3290
- function parsePromise2(ctx, id, current) {
3423
+ function parsePromise2(ctx, depth, id, current) {
3291
3424
  const resolver = createIndexForValue(ctx.base, {});
3292
3425
  if (ctx.type === 2 /* Stream */) {
3293
3426
  pushPendingState(ctx);
3294
3427
  current.then(
3295
- handlePromiseSuccess.bind(ctx, resolver),
3296
- handlePromiseFailure.bind(ctx, resolver)
3428
+ handlePromiseSuccess.bind(ctx, resolver, depth),
3429
+ handlePromiseFailure.bind(ctx, resolver, depth)
3297
3430
  );
3298
3431
  }
3299
3432
  return createPromiseConstructorNode(ctx.base, id, resolver);
3300
3433
  }
3301
- function parsePluginSync(ctx, id, current, currentPlugins) {
3434
+ function parsePluginSync(ctx, depth, id, current, currentPlugins) {
3302
3435
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
3303
3436
  const plugin = currentPlugins[i];
3304
3437
  if (plugin.parse.sync && plugin.test(current)) {
3305
- if (ctx.child == null) {
3306
- ctx.child = new SyncParsePluginContext(ctx);
3307
- }
3308
3438
  return createPluginNode(
3309
3439
  id,
3310
3440
  plugin.tag,
3311
- plugin.parse.sync(current, ctx.child, {
3441
+ plugin.parse.sync(current, new SyncParsePluginContext(ctx, depth), {
3312
3442
  id
3313
3443
  })
3314
3444
  );
3315
3445
  }
3316
3446
  }
3317
- return void 0;
3447
+ return NIL;
3318
3448
  }
3319
- function parsePluginStream(ctx, id, current, currentPlugins) {
3449
+ function parsePluginStream(ctx, depth, id, current, currentPlugins) {
3320
3450
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
3321
3451
  const plugin = currentPlugins[i];
3322
3452
  if (plugin.parse.stream && plugin.test(current)) {
3323
- if (ctx.child == null) {
3324
- ctx.child = new StreamParsePluginContext(ctx);
3325
- }
3326
3453
  return createPluginNode(
3327
3454
  id,
3328
3455
  plugin.tag,
3329
- plugin.parse.stream(current, ctx.child, {
3456
+ plugin.parse.stream(current, new StreamParsePluginContext(ctx, depth), {
3330
3457
  id
3331
3458
  })
3332
3459
  );
3333
3460
  }
3334
3461
  }
3335
- return void 0;
3462
+ return NIL;
3336
3463
  }
3337
- function parsePlugin2(ctx, id, current) {
3464
+ function parsePlugin2(ctx, depth, id, current) {
3338
3465
  const currentPlugins = ctx.base.plugins;
3339
3466
  if (currentPlugins) {
3340
- return ctx.type === 1 /* Sync */ ? parsePluginSync(ctx, id, current, currentPlugins) : parsePluginStream(ctx, id, current, currentPlugins);
3467
+ return ctx.type === 1 /* Sync */ ? parsePluginSync(ctx, depth, id, current, currentPlugins) : parsePluginStream(ctx, depth, id, current, currentPlugins);
3341
3468
  }
3342
- return void 0;
3469
+ return NIL;
3343
3470
  }
3344
- function parseObjectPhase2(ctx, id, current, currentClass) {
3471
+ function parseObjectPhase2(ctx, depth, id, current, currentClass) {
3345
3472
  switch (currentClass) {
3346
3473
  case Object:
3347
3474
  return parsePlainObject2(
3348
3475
  ctx,
3476
+ depth,
3349
3477
  id,
3350
3478
  current,
3351
3479
  false
3352
3480
  );
3353
- case void 0:
3481
+ case NIL:
3354
3482
  return parsePlainObject2(
3355
3483
  ctx,
3484
+ depth,
3356
3485
  id,
3357
3486
  current,
3358
3487
  true
3359
3488
  );
3360
3489
  case Date:
3361
3490
  return createDateNode(id, current);
3362
- case RegExp:
3363
- return createRegExpNode(id, current);
3364
3491
  case Error:
3365
3492
  case EvalError:
3366
3493
  case RangeError:
@@ -3368,12 +3495,12 @@ function parseObjectPhase2(ctx, id, current, currentClass) {
3368
3495
  case SyntaxError:
3369
3496
  case TypeError:
3370
3497
  case URIError:
3371
- return parseError2(ctx, id, current);
3498
+ return parseError2(ctx, depth, id, current);
3372
3499
  case Number:
3373
3500
  case Boolean:
3374
3501
  case String:
3375
3502
  case BigInt:
3376
- return parseBoxed2(ctx, id, current);
3503
+ return parseBoxed2(ctx, depth, id, current);
3377
3504
  case ArrayBuffer:
3378
3505
  return createArrayBufferNode(
3379
3506
  ctx.base,
@@ -3389,26 +3516,40 @@ function parseObjectPhase2(ctx, id, current, currentClass) {
3389
3516
  case Uint8ClampedArray:
3390
3517
  case Float32Array:
3391
3518
  case Float64Array:
3392
- return parseTypedArray2(ctx, id, current);
3519
+ return parseTypedArray2(
3520
+ ctx,
3521
+ depth,
3522
+ id,
3523
+ current
3524
+ );
3393
3525
  case DataView:
3394
- return parseDataView2(ctx, id, current);
3526
+ return parseDataView2(ctx, depth, id, current);
3395
3527
  case Map:
3396
- return parseMap2(ctx, id, current);
3528
+ return parseMap2(
3529
+ ctx,
3530
+ depth,
3531
+ id,
3532
+ current
3533
+ );
3397
3534
  case Set:
3398
- return parseSet2(ctx, id, current);
3535
+ return parseSet2(ctx, depth, id, current);
3399
3536
  default:
3400
3537
  break;
3401
3538
  }
3402
3539
  if (currentClass === Promise || current instanceof Promise) {
3403
- return parsePromise2(ctx, id, current);
3540
+ return parsePromise2(ctx, depth, id, current);
3404
3541
  }
3405
3542
  const currentFeatures = ctx.base.features;
3543
+ if (currentFeatures & 32 /* RegExp */ && currentClass === RegExp) {
3544
+ return createRegExpNode(id, current);
3545
+ }
3406
3546
  if (currentFeatures & 16 /* BigIntTypedArray */) {
3407
3547
  switch (currentClass) {
3408
3548
  case BigInt64Array:
3409
3549
  case BigUint64Array:
3410
3550
  return parseBigIntTypedArray2(
3411
3551
  ctx,
3552
+ depth,
3412
3553
  id,
3413
3554
  current
3414
3555
  );
@@ -3417,48 +3558,57 @@ function parseObjectPhase2(ctx, id, current, currentClass) {
3417
3558
  }
3418
3559
  }
3419
3560
  if (currentFeatures & 1 /* AggregateError */ && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) {
3420
- return parseAggregateError2(ctx, id, current);
3561
+ return parseAggregateError2(
3562
+ ctx,
3563
+ depth,
3564
+ id,
3565
+ current
3566
+ );
3421
3567
  }
3422
3568
  if (current instanceof Error) {
3423
- return parseError2(ctx, id, current);
3569
+ return parseError2(ctx, depth, id, current);
3424
3570
  }
3425
3571
  if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) {
3426
- return parsePlainObject2(ctx, id, current, !!currentClass);
3572
+ return parsePlainObject2(ctx, depth, id, current, !!currentClass);
3427
3573
  }
3428
3574
  throw new SerovalUnsupportedTypeError(current);
3429
3575
  }
3430
- function parseObject(ctx, id, current) {
3576
+ function parseObject(ctx, depth, id, current) {
3431
3577
  if (Array.isArray(current)) {
3432
- return parseArray2(ctx, id, current);
3578
+ return parseArray2(ctx, depth, id, current);
3433
3579
  }
3434
3580
  if (isStream(current)) {
3435
- return parseStream2(ctx, id, current);
3581
+ return parseStream2(ctx, depth, id, current);
3436
3582
  }
3437
3583
  const currentClass = current.constructor;
3438
3584
  if (currentClass === OpaqueReference) {
3439
3585
  return parseSOS(
3440
3586
  ctx,
3587
+ depth,
3441
3588
  current.replacement
3442
3589
  );
3443
3590
  }
3444
- const parsed = parsePlugin2(ctx, id, current);
3591
+ const parsed = parsePlugin2(ctx, depth, id, current);
3445
3592
  if (parsed) {
3446
3593
  return parsed;
3447
3594
  }
3448
- return parseObjectPhase2(ctx, id, current, currentClass);
3595
+ return parseObjectPhase2(ctx, depth, id, current, currentClass);
3449
3596
  }
3450
- function parseFunction(ctx, current) {
3597
+ function parseFunction(ctx, depth, current) {
3451
3598
  const ref = getReferenceNode(ctx.base, current);
3452
3599
  if (ref.type !== 0 /* Fresh */) {
3453
3600
  return ref.value;
3454
3601
  }
3455
- const plugin = parsePlugin2(ctx, ref.value, current);
3602
+ const plugin = parsePlugin2(ctx, depth, ref.value, current);
3456
3603
  if (plugin) {
3457
3604
  return plugin;
3458
3605
  }
3459
3606
  throw new SerovalUnsupportedTypeError(current);
3460
3607
  }
3461
- function parseSOS(ctx, current) {
3608
+ function parseSOS(ctx, depth, current) {
3609
+ if (depth >= ctx.base.depthLimit) {
3610
+ throw new SerovalDepthLimitError(ctx.base.depthLimit);
3611
+ }
3462
3612
  switch (typeof current) {
3463
3613
  case "boolean":
3464
3614
  return current ? TRUE_NODE : FALSE_NODE;
@@ -3473,14 +3623,14 @@ function parseSOS(ctx, current) {
3473
3623
  case "object": {
3474
3624
  if (current) {
3475
3625
  const ref = getReferenceNode(ctx.base, current);
3476
- return ref.type === 0 /* Fresh */ ? parseObject(ctx, ref.value, current) : ref.value;
3626
+ return ref.type === 0 /* Fresh */ ? parseObject(ctx, depth + 1, ref.value, current) : ref.value;
3477
3627
  }
3478
3628
  return NULL_NODE;
3479
3629
  }
3480
3630
  case "symbol":
3481
3631
  return parseWellKnownSymbol(ctx.base, current);
3482
3632
  case "function": {
3483
- return parseFunction(ctx, current);
3633
+ return parseFunction(ctx, depth, current);
3484
3634
  }
3485
3635
  default:
3486
3636
  throw new SerovalUnsupportedTypeError(current);
@@ -3488,7 +3638,7 @@ function parseSOS(ctx, current) {
3488
3638
  }
3489
3639
  function parseTop(ctx, current) {
3490
3640
  try {
3491
- return parseSOS(ctx, current);
3641
+ return parseSOS(ctx, 0, current);
3492
3642
  } catch (error) {
3493
3643
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3494
3644
  }
@@ -3527,16 +3677,16 @@ function popPendingState(ctx) {
3527
3677
  onDone(ctx);
3528
3678
  }
3529
3679
  }
3530
- function parseWithError(ctx, current) {
3680
+ function parseWithError(ctx, depth, current) {
3531
3681
  try {
3532
- return parseSOS(ctx, current);
3682
+ return parseSOS(ctx, depth, current);
3533
3683
  } catch (err) {
3534
3684
  onError(ctx, err);
3535
3685
  return NIL;
3536
3686
  }
3537
3687
  }
3538
3688
  function startStreamParse(ctx, current) {
3539
- const parsed = parseWithError(ctx, current);
3689
+ const parsed = parseWithError(ctx, 0, current);
3540
3690
  if (parsed) {
3541
3691
  onParseInternal(ctx, parsed, true);
3542
3692
  ctx.state.initial = false;
@@ -3656,7 +3806,9 @@ function fromCrossJSON(source, options) {
3656
3806
  const plugins = resolvePlugins(options.plugins);
3657
3807
  const ctx = createCrossDeserializerContext({
3658
3808
  plugins,
3659
- refs: options.refs
3809
+ refs: options.refs,
3810
+ features: options.features,
3811
+ disabledFeatures: options.disabledFeatures
3660
3812
  });
3661
3813
  return deserializeTop(ctx, source);
3662
3814
  }
@@ -3806,10 +3958,15 @@ function compileJSON(source, options = {}) {
3806
3958
  return serializeTopVanilla(ctx, source.t);
3807
3959
  }
3808
3960
  function fromJSON(source, options = {}) {
3961
+ var _a;
3809
3962
  const plugins = resolvePlugins(options.plugins);
3963
+ const disabledFeatures = options.disabledFeatures || 0;
3964
+ const sourceFeatures = (_a = source.f) != null ? _a : ALL_ENABLED;
3810
3965
  const ctx = createVanillaDeserializerContext({
3811
3966
  plugins,
3812
- markedRefs: source.m
3967
+ markedRefs: source.m,
3968
+ features: sourceFeatures & ~disabledFeatures,
3969
+ disabledFeatures
3813
3970
  });
3814
3971
  return deserializeTop(ctx, source.t);
3815
3972
  }
@@ -3817,8 +3974,11 @@ export {
3817
3974
  Feature,
3818
3975
  OpaqueReference,
3819
3976
  Serializer,
3977
+ SerovalConflictedNodeIdError,
3978
+ SerovalDepthLimitError,
3820
3979
  SerovalDeserializationError,
3821
3980
  SerovalError,
3981
+ SerovalMalformedNodeError,
3822
3982
  SerovalMissingInstanceError,
3823
3983
  SerovalMissingPluginError,
3824
3984
  SerovalMissingReferenceError,