seroval 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/LICENSE +1 -1
  2. package/dist/cjs/development/index.cjs +259 -381
  3. package/dist/cjs/development/index.cjs.map +3 -3
  4. package/dist/cjs/production/index.cjs +6 -6
  5. package/dist/esm/development/index.mjs +259 -381
  6. package/dist/esm/development/index.mjs.map +3 -3
  7. package/dist/esm/production/index.mjs +6 -6
  8. package/dist/types/core/base-primitives.d.ts +1 -2
  9. package/dist/types/core/base-primitives.d.ts.map +1 -1
  10. package/dist/types/core/compat.d.ts +2 -3
  11. package/dist/types/core/compat.d.ts.map +1 -1
  12. package/dist/types/core/constants.d.ts +3 -6
  13. package/dist/types/core/constants.d.ts.map +1 -1
  14. package/dist/types/core/context/deserializer.d.ts +0 -3
  15. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  16. package/dist/types/core/context/parser/async.d.ts +1 -2
  17. package/dist/types/core/context/parser/async.d.ts.map +1 -1
  18. package/dist/types/core/context/parser/stream.d.ts +8 -8
  19. package/dist/types/core/context/parser/stream.d.ts.map +1 -1
  20. package/dist/types/core/context/parser/sync.d.ts +2 -3
  21. package/dist/types/core/context/parser/sync.d.ts.map +1 -1
  22. package/dist/types/core/context/parser.d.ts +3 -3
  23. package/dist/types/core/context/parser.d.ts.map +1 -1
  24. package/dist/types/core/context/serializer.d.ts +1 -5
  25. package/dist/types/core/context/serializer.d.ts.map +1 -1
  26. package/dist/types/core/function-string.d.ts +3 -0
  27. package/dist/types/core/function-string.d.ts.map +1 -0
  28. package/dist/types/core/special-reference.d.ts +4 -5
  29. package/dist/types/core/special-reference.d.ts.map +1 -1
  30. package/dist/types/core/tree/serializer.d.ts +1 -2
  31. package/dist/types/core/tree/serializer.d.ts.map +1 -1
  32. package/dist/types/core/types.d.ts +5 -19
  33. package/dist/types/core/types.d.ts.map +1 -1
  34. package/package.json +8 -8
  35. package/src/core/base-primitives.ts +2 -22
  36. package/src/core/compat.ts +6 -2
  37. package/src/core/constants.ts +2 -5
  38. package/src/core/context/deserializer.ts +2 -35
  39. package/src/core/context/parser/async.ts +54 -83
  40. package/src/core/context/parser/stream.ts +64 -111
  41. package/src/core/context/parser/sync.ts +53 -83
  42. package/src/core/context/parser.ts +9 -24
  43. package/src/core/context/serializer.ts +12 -128
  44. package/src/core/function-string.ts +31 -0
  45. package/src/core/special-reference.ts +96 -8
  46. package/src/core/tree/serializer.ts +0 -7
  47. package/src/core/types.ts +5 -27
  48. package/dist/types/core/abort-signal.d.ts +0 -2
  49. package/dist/types/core/abort-signal.d.ts.map +0 -1
  50. package/src/core/abort-signal.ts +0 -19
@@ -63,10 +63,9 @@ var Feature = /* @__PURE__ */ ((Feature2) => {
63
63
  Feature2[Feature2["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
64
64
  Feature2[Feature2["ObjectAssign"] = 8] = "ObjectAssign";
65
65
  Feature2[Feature2["BigIntTypedArray"] = 16] = "BigIntTypedArray";
66
- Feature2[Feature2["AbortSignal"] = 32] = "AbortSignal";
67
66
  return Feature2;
68
67
  })(Feature || {});
69
- var ALL_ENABLED = 47;
68
+ var ALL_ENABLED = 1 /* AggregateError */ | 2 /* ArrowFunction */ | 4 /* ErrorPrototypeStack */ | 8 /* ObjectAssign */ | 16 /* BigIntTypedArray */;
70
69
 
71
70
  // src/core/string.ts
72
71
  function serializeChar(str) {
@@ -242,19 +241,6 @@ function resolvePlugins(plugins) {
242
241
  return void 0;
243
242
  }
244
243
 
245
- // src/core/abort-signal.ts
246
- function resolveAbortSignalResult(resolve) {
247
- resolve(this.reason);
248
- }
249
- function resolveAbortSignal(resolve) {
250
- this.addEventListener("abort", resolveAbortSignalResult.bind(this, resolve), {
251
- once: true
252
- });
253
- }
254
- function abortSignalToPromise(signal) {
255
- return new Promise(resolveAbortSignal.bind(signal));
256
- }
257
-
258
244
  // src/core/constants.ts
259
245
  var SYMBOL_STRING = {
260
246
  [0 /* AsyncIterator */]: "Symbol.asyncIterator",
@@ -543,10 +529,11 @@ function createIndexedValueNode(id) {
543
529
  );
544
530
  }
545
531
  function createDateNode(id, current) {
532
+ const timestamp = current.valueOf();
546
533
  return createSerovalNode(
547
534
  5 /* Date */,
548
535
  id,
549
- current.toISOString(),
536
+ timestamp !== timestamp ? "" : current.toISOString(),
550
537
  NIL,
551
538
  NIL,
552
539
  NIL,
@@ -962,18 +949,96 @@ var OpaqueReference = class {
962
949
  }
963
950
  };
964
951
 
952
+ // src/core/function-string.ts
953
+ function createFunction(features, parameters, body) {
954
+ if (features & 2 /* ArrowFunction */) {
955
+ const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
956
+ return joined + "=>" + (body.startsWith("{") ? "(" + body + ")" : body);
957
+ }
958
+ return "function(" + parameters.join(",") + "){return " + body + "}";
959
+ }
960
+ function createEffectfulFunction(features, parameters, body) {
961
+ if (features & 2 /* ArrowFunction */) {
962
+ const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
963
+ return joined + "=>{" + body + "}";
964
+ }
965
+ return "function(" + parameters.join(",") + "){" + body + "}";
966
+ }
967
+
965
968
  // src/core/special-reference.ts
966
969
  var ITERATOR = {};
967
970
  var ASYNC_ITERATOR = {};
968
971
  var SPECIAL_REFS = {
969
972
  [0 /* MapSentinel */]: {},
970
973
  [1 /* PromiseConstructor */]: {},
971
- [2 /* PromiseResolve */]: {},
972
- [3 /* PromiseReject */]: {},
973
- [4 /* StreamConstructor */]: {},
974
- [5 /* AbortSignalConstructor */]: {},
975
- [6 /* AbortSignalAbort */]: {}
974
+ [2 /* PromiseSuccess */]: {},
975
+ [3 /* PromiseFailure */]: {},
976
+ [4 /* StreamConstructor */]: {}
976
977
  };
978
+ function serializePromiseConstructor(features) {
979
+ return createFunction(
980
+ features,
981
+ ["r"],
982
+ "(r.p=new Promise(" + createEffectfulFunction(features, ["s", "f"], "r.s=s,r.f=f") + "))"
983
+ );
984
+ }
985
+ function serializePromiseSuccess(features) {
986
+ return createEffectfulFunction(
987
+ features,
988
+ ["r", "d"],
989
+ "r.s(d),r.p.s=1,r.p.v=d"
990
+ );
991
+ }
992
+ function serializePromiseFailure(features) {
993
+ return createEffectfulFunction(
994
+ features,
995
+ ["r", "d"],
996
+ "r.f(d),r.p.s=2,r.p.v=d"
997
+ );
998
+ }
999
+ function serializeStreamConstructor(features) {
1000
+ return createFunction(
1001
+ features,
1002
+ ["b", "a", "s", "l", "p", "f", "e", "n"],
1003
+ "(b=[],a=!0,s=!1,l=[],p=0,f=" + createEffectfulFunction(
1004
+ features,
1005
+ ["v", "m", "x"],
1006
+ "for(x=0;x<p;x++)l[x]&&l[x][m](v)"
1007
+ ) + ",n=" + createEffectfulFunction(
1008
+ features,
1009
+ ["o", "x", "z", "c"],
1010
+ 'for(x=0,z=b.length;x<z;x++)(c=b[x],(!a&&x===z-1)?o[s?"return":"throw"](c):o.next(c))'
1011
+ ) + ",e=" + createFunction(
1012
+ features,
1013
+ ["o", "t"],
1014
+ "(a&&(l[t=p++]=o),n(o)," + createEffectfulFunction(features, [], "a&&(l[t]=void 0)") + ")"
1015
+ ) + ",{__SEROVAL_STREAM__:!0,on:" + createFunction(features, ["o"], "e(o)") + ",next:" + createEffectfulFunction(features, ["v"], 'a&&(b.push(v),f(v,"next"))') + ",throw:" + createEffectfulFunction(
1016
+ features,
1017
+ ["v"],
1018
+ 'a&&(b.push(v),f(v,"throw"),a=s=!1,l.length=0)'
1019
+ ) + ",return:" + createEffectfulFunction(
1020
+ features,
1021
+ ["v"],
1022
+ 'a&&(b.push(v),f(v,"return"),a=!1,s=!0,l.length=0)'
1023
+ ) + "})"
1024
+ );
1025
+ }
1026
+ function serializeSpecialReferenceValue(features, ref) {
1027
+ switch (ref) {
1028
+ case 0 /* MapSentinel */:
1029
+ return "[]";
1030
+ case 1 /* PromiseConstructor */:
1031
+ return serializePromiseConstructor(features);
1032
+ case 2 /* PromiseSuccess */:
1033
+ return serializePromiseSuccess(features);
1034
+ case 3 /* PromiseFailure */:
1035
+ return serializePromiseFailure(features);
1036
+ case 4 /* StreamConstructor */:
1037
+ return serializeStreamConstructor(features);
1038
+ default:
1039
+ return "";
1040
+ }
1041
+ }
977
1042
 
978
1043
  // src/core/utils/deferred.ts
979
1044
  function createDeferred() {
@@ -1237,6 +1302,11 @@ var BaseParserContext = class {
1237
1302
  isMarked(id) {
1238
1303
  return this.marked.has(id);
1239
1304
  }
1305
+ createIndex(current) {
1306
+ const id = this.refs.size;
1307
+ this.refs.set(current, id);
1308
+ return id;
1309
+ }
1240
1310
  getIndexedValue(current) {
1241
1311
  const registeredId = this.refs.get(current);
1242
1312
  if (registeredId != null) {
@@ -1246,11 +1316,9 @@ var BaseParserContext = class {
1246
1316
  value: createIndexedValueNode(registeredId)
1247
1317
  };
1248
1318
  }
1249
- const id = this.refs.size;
1250
- this.refs.set(current, id);
1251
1319
  return {
1252
1320
  type: 0 /* Fresh */,
1253
- value: id
1321
+ value: this.createIndex(current)
1254
1322
  };
1255
1323
  }
1256
1324
  getReference(current) {
@@ -1369,11 +1437,11 @@ var BaseParserContext = class {
1369
1437
  NIL
1370
1438
  );
1371
1439
  }
1372
- createPromiseConstructorNode(id) {
1440
+ createPromiseConstructorNode(id, resolver) {
1373
1441
  return createSerovalNode(
1374
1442
  22 /* PromiseConstructor */,
1375
1443
  id,
1376
- NIL,
1444
+ resolver,
1377
1445
  NIL,
1378
1446
  NIL,
1379
1447
  NIL,
@@ -1385,22 +1453,6 @@ var BaseParserContext = class {
1385
1453
  NIL
1386
1454
  );
1387
1455
  }
1388
- createAbortSignalConstructorNode(id) {
1389
- return createSerovalNode(
1390
- 35 /* AbortSignalConstructor */,
1391
- id,
1392
- NIL,
1393
- NIL,
1394
- NIL,
1395
- NIL,
1396
- NIL,
1397
- NIL,
1398
- NIL,
1399
- this.parseSpecialReference(5 /* AbortSignalConstructor */),
1400
- NIL,
1401
- NIL
1402
- );
1403
- }
1404
1456
  };
1405
1457
 
1406
1458
  // src/core/context/parser/async.ts
@@ -1409,7 +1461,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1409
1461
  const nodes = [];
1410
1462
  for (let i = 0, len = current.length; i < len; i++) {
1411
1463
  if (i in current) {
1412
- nodes[i] = await this.parse(current[i]);
1464
+ nodes[i] = await this.parseTop(current[i]);
1413
1465
  }
1414
1466
  }
1415
1467
  return nodes;
@@ -1423,7 +1475,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1423
1475
  const valueNodes = [];
1424
1476
  for (let i = 0, len = entries.length; i < len; i++) {
1425
1477
  keyNodes.push(serializeString(entries[i][0]));
1426
- valueNodes.push(await this.parse(entries[i][1]));
1478
+ valueNodes.push(await this.parseTop(entries[i][1]));
1427
1479
  }
1428
1480
  let symbol = Symbol.iterator;
1429
1481
  if (symbol in properties) {
@@ -1431,7 +1483,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1431
1483
  valueNodes.push(
1432
1484
  createIteratorFactoryInstanceNode(
1433
1485
  this.parseIteratorFactory(),
1434
- await this.parse(
1486
+ await this.parseTop(
1435
1487
  iteratorToSequence(properties)
1436
1488
  )
1437
1489
  )
@@ -1443,7 +1495,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1443
1495
  valueNodes.push(
1444
1496
  createAsyncIteratorFactoryInstanceNode(
1445
1497
  this.parseAsyncIteratorFactory(),
1446
- await this.parse(
1498
+ await this.parseTop(
1447
1499
  createStreamFromAsyncIterable(
1448
1500
  properties
1449
1501
  )
@@ -1476,20 +1528,24 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1476
1528
  );
1477
1529
  }
1478
1530
  async parseBoxed(id, current) {
1479
- return createBoxedNode(id, await this.parse(current.valueOf()));
1531
+ return createBoxedNode(id, await this.parseTop(current.valueOf()));
1480
1532
  }
1481
1533
  async parseTypedArray(id, current) {
1482
- return createTypedArrayNode(id, current, await this.parse(current.buffer));
1534
+ return createTypedArrayNode(
1535
+ id,
1536
+ current,
1537
+ await this.parseTop(current.buffer)
1538
+ );
1483
1539
  }
1484
1540
  async parseBigIntTypedArray(id, current) {
1485
1541
  return createBigIntTypedArrayNode(
1486
1542
  id,
1487
1543
  current,
1488
- await this.parse(current.buffer)
1544
+ await this.parseTop(current.buffer)
1489
1545
  );
1490
1546
  }
1491
1547
  async parseDataView(id, current) {
1492
- return createDataViewNode(id, current, await this.parse(current.buffer));
1548
+ return createDataViewNode(id, current, await this.parseTop(current.buffer));
1493
1549
  }
1494
1550
  async parseError(id, current) {
1495
1551
  const options = getErrorOptions(current, this.features);
@@ -1511,15 +1567,15 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1511
1567
  const keyNodes = [];
1512
1568
  const valueNodes = [];
1513
1569
  for (const [key, value] of current.entries()) {
1514
- keyNodes.push(await this.parse(key));
1515
- valueNodes.push(await this.parse(value));
1570
+ keyNodes.push(await this.parseTop(key));
1571
+ valueNodes.push(await this.parseTop(value));
1516
1572
  }
1517
1573
  return this.createMapNode(id, keyNodes, valueNodes, current.size);
1518
1574
  }
1519
1575
  async parseSet(id, current) {
1520
1576
  const items = [];
1521
1577
  for (const item of current.keys()) {
1522
- items.push(await this.parse(item));
1578
+ items.push(await this.parseTop(item));
1523
1579
  }
1524
1580
  return createSetNode(id, current.size, items);
1525
1581
  }
@@ -1535,7 +1591,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1535
1591
  NIL,
1536
1592
  NIL,
1537
1593
  NIL,
1538
- await this.parse(result),
1594
+ await this.parseTop(result),
1539
1595
  NIL,
1540
1596
  NIL
1541
1597
  );
@@ -1567,7 +1623,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1567
1623
  const cleanup = current.on({
1568
1624
  next: (value) => {
1569
1625
  this.markRef(id);
1570
- this.parse(value).then(
1626
+ this.parseTop(value).then(
1571
1627
  (data) => {
1572
1628
  sequence.push(createStreamNextNode(id, data));
1573
1629
  },
@@ -1579,7 +1635,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1579
1635
  },
1580
1636
  throw: (value) => {
1581
1637
  this.markRef(id);
1582
- this.parse(value).then(
1638
+ this.parseTop(value).then(
1583
1639
  (data) => {
1584
1640
  sequence.push(createStreamThrowNode(id, data));
1585
1641
  resolve(sequence);
@@ -1593,7 +1649,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1593
1649
  },
1594
1650
  return: (value) => {
1595
1651
  this.markRef(id);
1596
- this.parse(value).then(
1652
+ this.parseTop(value).then(
1597
1653
  (data) => {
1598
1654
  sequence.push(createStreamReturnNode(id, data));
1599
1655
  resolve(sequence);
@@ -1609,28 +1665,6 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1609
1665
  })
1610
1666
  );
1611
1667
  }
1612
- async parseAbortSignalSync(id, current) {
1613
- return createSerovalNode(
1614
- 37 /* AbortSignalSync */,
1615
- id,
1616
- NIL,
1617
- NIL,
1618
- NIL,
1619
- NIL,
1620
- NIL,
1621
- NIL,
1622
- NIL,
1623
- await this.parse(current.reason),
1624
- NIL,
1625
- NIL
1626
- );
1627
- }
1628
- async parseAbortSignal(id, current) {
1629
- if (!current.aborted) {
1630
- await abortSignalToPromise(current);
1631
- }
1632
- return this.parseAbortSignalSync(id, current);
1633
- }
1634
1668
  // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: ehh
1635
1669
  async parseObject(id, current) {
1636
1670
  if (Array.isArray(current)) {
@@ -1641,7 +1675,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1641
1675
  }
1642
1676
  const currentClass = current.constructor;
1643
1677
  if (currentClass === OpaqueReference) {
1644
- return this.parse(
1678
+ return this.parseTop(
1645
1679
  current.replacement
1646
1680
  );
1647
1681
  }
@@ -1704,9 +1738,6 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1704
1738
  return this.parsePromise(id, current);
1705
1739
  }
1706
1740
  const currentFeatures = this.features;
1707
- if (currentFeatures & 32 /* AbortSignal */ && typeof AbortSignal !== "undefined" && currentClass === AbortSignal) {
1708
- return this.parseAbortSignal(id, current);
1709
- }
1710
1741
  if (currentFeatures & 16 /* BigIntTypedArray */) {
1711
1742
  switch (currentClass) {
1712
1743
  case BigInt64Array:
@@ -1741,35 +1772,38 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1741
1772
  }
1742
1773
  throw new SerovalUnsupportedTypeError(current);
1743
1774
  }
1744
- async parse(current) {
1745
- try {
1746
- switch (typeof current) {
1747
- case "boolean":
1748
- return current ? TRUE_NODE : FALSE_NODE;
1749
- case "undefined":
1750
- return UNDEFINED_NODE;
1751
- case "string":
1752
- return createStringNode(current);
1753
- case "number":
1754
- return createNumberNode(current);
1755
- case "bigint":
1756
- return createBigIntNode(current);
1757
- case "object": {
1758
- if (current) {
1759
- const ref = this.getReference(current);
1760
- return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
1761
- }
1762
- return NULL_NODE;
1775
+ async parseTop(current) {
1776
+ switch (typeof current) {
1777
+ case "boolean":
1778
+ return current ? TRUE_NODE : FALSE_NODE;
1779
+ case "undefined":
1780
+ return UNDEFINED_NODE;
1781
+ case "string":
1782
+ return createStringNode(current);
1783
+ case "number":
1784
+ return createNumberNode(current);
1785
+ case "bigint":
1786
+ return createBigIntNode(current);
1787
+ case "object": {
1788
+ if (current) {
1789
+ const ref = this.getReference(current);
1790
+ return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
1763
1791
  }
1764
- case "symbol":
1765
- return this.parseWellKnownSymbol(current);
1766
- case "function":
1767
- return this.parseFunction(current);
1768
- default:
1769
- throw new SerovalUnsupportedTypeError(current);
1792
+ return NULL_NODE;
1770
1793
  }
1794
+ case "symbol":
1795
+ return this.parseWellKnownSymbol(current);
1796
+ case "function":
1797
+ return this.parseFunction(current);
1798
+ default:
1799
+ throw new SerovalUnsupportedTypeError(current);
1800
+ }
1801
+ }
1802
+ async parse(current) {
1803
+ try {
1804
+ return await this.parseTop(current);
1771
1805
  } catch (error) {
1772
- throw new SerovalParserError(error);
1806
+ throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
1773
1807
  }
1774
1808
  }
1775
1809
  };
@@ -2040,21 +2074,6 @@ var BaseDeserializerContext = class {
2040
2074
  this.deserialize(node.a[1]);
2041
2075
  return void 0;
2042
2076
  }
2043
- deserializeAbortSignalConstructor(node) {
2044
- return this.assignIndexedValue(node.i, new AbortController()).signal;
2045
- }
2046
- deserializeAbortSignalAbort(node) {
2047
- const controller = this.refs.get(node.i);
2048
- assert(controller, new SerovalMissingInstanceError("AbortSignal"));
2049
- controller.abort(this.deserialize(node.a[1]));
2050
- return void 0;
2051
- }
2052
- deserializeAbortSignalSync(node) {
2053
- return this.assignIndexedValue(
2054
- node.i,
2055
- AbortSignal.abort(this.deserialize(node.f))
2056
- );
2057
- }
2058
2077
  deserialize(node) {
2059
2078
  try {
2060
2079
  switch (node.t) {
@@ -2104,9 +2123,9 @@ var BaseDeserializerContext = class {
2104
2123
  return this.deserializePlugin(node);
2105
2124
  case 22 /* PromiseConstructor */:
2106
2125
  return this.deserializePromiseConstructor(node);
2107
- case 23 /* PromiseResolve */:
2126
+ case 23 /* PromiseSuccess */:
2108
2127
  return this.deserializePromiseResolve(node);
2109
- case 24 /* PromiseReject */:
2128
+ case 24 /* PromiseFailure */:
2110
2129
  return this.deserializePromiseReject(node);
2111
2130
  case 28 /* IteratorFactoryInstance */:
2112
2131
  return this.deserializeIteratorFactoryInstance(node);
@@ -2124,12 +2143,6 @@ var BaseDeserializerContext = class {
2124
2143
  return this.deserializeIteratorFactory(node);
2125
2144
  case 29 /* AsyncIteratorFactory */:
2126
2145
  return this.deserializeAsyncIteratorFactory(node);
2127
- case 36 /* AbortSignalAbort */:
2128
- return this.deserializeAbortSignalAbort(node);
2129
- case 35 /* AbortSignalConstructor */:
2130
- return this.deserializeAbortSignalConstructor(node);
2131
- case 37 /* AbortSignalSync */:
2132
- return this.deserializeAbortSignalSync(node);
2133
2146
  // case SerovalNodeType.SpecialReference:
2134
2147
  default:
2135
2148
  throw new SerovalUnsupportedNodeError(node);
@@ -2260,18 +2273,10 @@ var BaseSerializerContext = class {
2260
2273
  this.marked = new Set(options.markedRefs);
2261
2274
  }
2262
2275
  createFunction(parameters, body) {
2263
- if (this.features & 2 /* ArrowFunction */) {
2264
- const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
2265
- return joined + "=>" + (body.startsWith("{") ? "(" + body + ")" : body);
2266
- }
2267
- return "function(" + parameters.join(",") + "){return " + body + "}";
2276
+ return createFunction(this.features, parameters, body);
2268
2277
  }
2269
2278
  createEffectfulFunction(parameters, body) {
2270
- if (this.features & 2 /* ArrowFunction */) {
2271
- const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
2272
- return joined + "=>{" + body + "}";
2273
- }
2274
- return "function(" + parameters.join(",") + "){" + body + "}";
2279
+ return createEffectfulFunction(this.features, parameters, body);
2275
2280
  }
2276
2281
  /**
2277
2282
  * A tiny function that tells if a reference
@@ -2740,7 +2745,11 @@ var BaseSerializerContext = class {
2740
2745
  return current === this.getRefParam(node.i) ? current : "(" + current + ")";
2741
2746
  }
2742
2747
  serializePromiseConstructor(node) {
2743
- return this.assignIndexedValue(node.i, this.getConstructor(node.f) + "()");
2748
+ const resolver = this.assignIndexedValue(node.s, "{p:0,s:0,f:0}");
2749
+ return this.assignIndexedValue(
2750
+ node.i,
2751
+ this.getConstructor(node.f) + "(" + resolver + ")"
2752
+ );
2744
2753
  }
2745
2754
  serializePromiseResolve(node) {
2746
2755
  return this.getConstructor(node.a[0]) + "(" + this.getRefParam(node.i) + "," + this.serialize(node.a[1]) + ")";
@@ -2748,63 +2757,10 @@ var BaseSerializerContext = class {
2748
2757
  serializePromiseReject(node) {
2749
2758
  return this.getConstructor(node.a[0]) + "(" + this.getRefParam(node.i) + "," + this.serialize(node.a[1]) + ")";
2750
2759
  }
2751
- serializeSpecialReferenceValue(ref) {
2752
- switch (ref) {
2753
- case 0 /* MapSentinel */:
2754
- return "[]";
2755
- case 1 /* PromiseConstructor */:
2756
- return this.createFunction(
2757
- ["s", "f", "p"],
2758
- "((p=new Promise(" + this.createEffectfulFunction(["a", "b"], "s=a,f=b") + ")).s=s,p.f=f,p)"
2759
- );
2760
- case 2 /* PromiseResolve */:
2761
- return this.createEffectfulFunction(
2762
- ["p", "d"],
2763
- 'p.s(d),p.status="success",p.value=d;delete p.s;delete p.f'
2764
- );
2765
- case 3 /* PromiseReject */:
2766
- return this.createEffectfulFunction(
2767
- ["p", "d"],
2768
- 'p.f(d),p.status="failure",p.value=d;delete p.s;delete p.f'
2769
- );
2770
- case 4 /* StreamConstructor */:
2771
- return this.createFunction(
2772
- ["b", "a", "s", "l", "p", "f", "e", "n"],
2773
- "(b=[],a=!0,s=!1,l=[],p=0,f=" + this.createEffectfulFunction(
2774
- ["v", "m", "x"],
2775
- "for(x=0;x<p;x++)l[x]&&l[x][m](v)"
2776
- ) + ",n=" + this.createEffectfulFunction(
2777
- ["o", "x", "z", "c"],
2778
- 'for(x=0,z=b.length;x<z;x++)(c=b[x],(!a&&x===z-1)?o[s?"return":"throw"](c):o.next(c))'
2779
- ) + ",e=" + this.createFunction(
2780
- ["o", "t"],
2781
- "(a&&(l[t=p++]=o),n(o)," + this.createEffectfulFunction([], "a&&(l[t]=void 0)") + ")"
2782
- ) + ",{__SEROVAL_STREAM__:!0,on:" + this.createFunction(["o"], "e(o)") + ",next:" + this.createEffectfulFunction(["v"], 'a&&(b.push(v),f(v,"next"))') + ",throw:" + this.createEffectfulFunction(
2783
- ["v"],
2784
- 'a&&(b.push(v),f(v,"throw"),a=s=!1,l.length=0)'
2785
- ) + ",return:" + this.createEffectfulFunction(
2786
- ["v"],
2787
- 'a&&(b.push(v),f(v,"return"),a=!1,s=!0,l.length=0)'
2788
- ) + "})"
2789
- );
2790
- case 5 /* AbortSignalConstructor */:
2791
- return this.createFunction(
2792
- ["a", "s"],
2793
- "((s=(a=new AbortController).signal).a=a,s)"
2794
- );
2795
- case 6 /* AbortSignalAbort */:
2796
- return this.createEffectfulFunction(
2797
- ["s", "r"],
2798
- "s.a.abort(r);delete s.a"
2799
- );
2800
- default:
2801
- return "";
2802
- }
2803
- }
2804
2760
  serializeSpecialReference(node) {
2805
2761
  return this.assignIndexedValue(
2806
2762
  node.i,
2807
- this.serializeSpecialReferenceValue(node.s)
2763
+ serializeSpecialReferenceValue(this.features, node.s)
2808
2764
  );
2809
2765
  }
2810
2766
  serializeIteratorFactory(node) {
@@ -2908,18 +2864,6 @@ var BaseSerializerContext = class {
2908
2864
  serializeStreamReturn(node) {
2909
2865
  return this.getRefParam(node.i) + ".return(" + this.serialize(node.f) + ")";
2910
2866
  }
2911
- serializeAbortSignalSync(node) {
2912
- return this.assignIndexedValue(
2913
- node.i,
2914
- "AbortSignal.abort(" + this.serialize(node.f) + ")"
2915
- );
2916
- }
2917
- serializeAbortSignalConstructor(node) {
2918
- return this.assignIndexedValue(node.i, this.getConstructor(node.f) + "()");
2919
- }
2920
- serializeAbortSignalAbort(node) {
2921
- return this.getConstructor(node.a[0]) + "(" + this.getRefParam(node.i) + "," + this.serialize(node.a[1]) + ")";
2922
- }
2923
2867
  serialize(node) {
2924
2868
  try {
2925
2869
  switch (node.t) {
@@ -2968,9 +2912,9 @@ var BaseSerializerContext = class {
2968
2912
  return this.serializeBoxed(node);
2969
2913
  case 22 /* PromiseConstructor */:
2970
2914
  return this.serializePromiseConstructor(node);
2971
- case 23 /* PromiseResolve */:
2915
+ case 23 /* PromiseSuccess */:
2972
2916
  return this.serializePromiseResolve(node);
2973
- case 24 /* PromiseReject */:
2917
+ case 24 /* PromiseFailure */:
2974
2918
  return this.serializePromiseReject(node);
2975
2919
  case 25 /* Plugin */:
2976
2920
  return this.serializePlugin(node);
@@ -2992,12 +2936,6 @@ var BaseSerializerContext = class {
2992
2936
  return this.serializeStreamThrow(node);
2993
2937
  case 34 /* StreamReturn */:
2994
2938
  return this.serializeStreamReturn(node);
2995
- case 36 /* AbortSignalAbort */:
2996
- return this.serializeAbortSignalAbort(node);
2997
- case 35 /* AbortSignalConstructor */:
2998
- return this.serializeAbortSignalConstructor(node);
2999
- case 37 /* AbortSignalSync */:
3000
- return this.serializeAbortSignalSync(node);
3001
2939
  default:
3002
2940
  throw new SerovalUnsupportedNodeError(node);
3003
2941
  }
@@ -3047,7 +2985,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3047
2985
  const nodes = [];
3048
2986
  for (let i = 0, len = current.length; i < len; i++) {
3049
2987
  if (i in current) {
3050
- nodes[i] = this.parse(current[i]);
2988
+ nodes[i] = this.parseTop(current[i]);
3051
2989
  }
3052
2990
  }
3053
2991
  return nodes;
@@ -3061,7 +2999,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3061
2999
  const valueNodes = [];
3062
3000
  for (let i = 0, len = entries.length; i < len; i++) {
3063
3001
  keyNodes.push(serializeString(entries[i][0]));
3064
- valueNodes.push(this.parse(entries[i][1]));
3002
+ valueNodes.push(this.parseTop(entries[i][1]));
3065
3003
  }
3066
3004
  let symbol = Symbol.iterator;
3067
3005
  if (symbol in properties) {
@@ -3069,7 +3007,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3069
3007
  valueNodes.push(
3070
3008
  createIteratorFactoryInstanceNode(
3071
3009
  this.parseIteratorFactory(),
3072
- this.parse(
3010
+ this.parseTop(
3073
3011
  iteratorToSequence(properties)
3074
3012
  )
3075
3013
  )
@@ -3081,7 +3019,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3081
3019
  valueNodes.push(
3082
3020
  createAsyncIteratorFactoryInstanceNode(
3083
3021
  this.parseAsyncIteratorFactory(),
3084
- this.parse(createStream())
3022
+ this.parseTop(createStream())
3085
3023
  )
3086
3024
  );
3087
3025
  }
@@ -3110,16 +3048,20 @@ var BaseSyncParserContext = class extends BaseParserContext {
3110
3048
  );
3111
3049
  }
3112
3050
  parseBoxed(id, current) {
3113
- return createBoxedNode(id, this.parse(current.valueOf()));
3051
+ return createBoxedNode(id, this.parseTop(current.valueOf()));
3114
3052
  }
3115
3053
  parseTypedArray(id, current) {
3116
- return createTypedArrayNode(id, current, this.parse(current.buffer));
3054
+ return createTypedArrayNode(id, current, this.parseTop(current.buffer));
3117
3055
  }
3118
3056
  parseBigIntTypedArray(id, current) {
3119
- return createBigIntTypedArrayNode(id, current, this.parse(current.buffer));
3057
+ return createBigIntTypedArrayNode(
3058
+ id,
3059
+ current,
3060
+ this.parseTop(current.buffer)
3061
+ );
3120
3062
  }
3121
3063
  parseDataView(id, current) {
3122
- return createDataViewNode(id, current, this.parse(current.buffer));
3064
+ return createDataViewNode(id, current, this.parseTop(current.buffer));
3123
3065
  }
3124
3066
  parseError(id, current) {
3125
3067
  const options = getErrorOptions(current, this.features);
@@ -3141,15 +3083,15 @@ var BaseSyncParserContext = class extends BaseParserContext {
3141
3083
  const keyNodes = [];
3142
3084
  const valueNodes = [];
3143
3085
  for (const [key, value] of current.entries()) {
3144
- keyNodes.push(this.parse(key));
3145
- valueNodes.push(this.parse(value));
3086
+ keyNodes.push(this.parseTop(key));
3087
+ valueNodes.push(this.parseTop(value));
3146
3088
  }
3147
3089
  return this.createMapNode(id, keyNodes, valueNodes, current.size);
3148
3090
  }
3149
3091
  parseSet(id, current) {
3150
3092
  const items = [];
3151
3093
  for (const item of current.keys()) {
3152
- items.push(this.parse(item));
3094
+ items.push(this.parseTop(item));
3153
3095
  }
3154
3096
  return createSetNode(id, current.size, items);
3155
3097
  }
@@ -3179,29 +3121,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3179
3121
  );
3180
3122
  }
3181
3123
  parsePromise(id, _current) {
3182
- return this.createPromiseConstructorNode(id);
3183
- }
3184
- parseAbortSignalSync(id, current) {
3185
- return createSerovalNode(
3186
- 37 /* AbortSignalSync */,
3187
- id,
3188
- NIL,
3189
- NIL,
3190
- NIL,
3191
- NIL,
3192
- NIL,
3193
- NIL,
3194
- NIL,
3195
- this.parse(current.reason),
3196
- NIL,
3197
- NIL
3198
- );
3199
- }
3200
- parseAbortSignal(id, current) {
3201
- if (current.aborted) {
3202
- return this.parseAbortSignalSync(id, current);
3203
- }
3204
- return this.createAbortSignalConstructorNode(id);
3124
+ return this.createPromiseConstructorNode(id, this.createIndex({}));
3205
3125
  }
3206
3126
  // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: ehh
3207
3127
  parseObject(id, current) {
@@ -3213,7 +3133,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3213
3133
  }
3214
3134
  const currentClass = current.constructor;
3215
3135
  if (currentClass === OpaqueReference) {
3216
- return this.parse(
3136
+ return this.parseTop(
3217
3137
  current.replacement
3218
3138
  );
3219
3139
  }
@@ -3276,9 +3196,6 @@ var BaseSyncParserContext = class extends BaseParserContext {
3276
3196
  return this.parsePromise(id, current);
3277
3197
  }
3278
3198
  const currentFeatures = this.features;
3279
- if (currentFeatures & 32 /* AbortSignal */ && typeof AbortSignal !== "undefined" && currentClass === AbortSignal) {
3280
- return this.parseAbortSignal(id, current);
3281
- }
3282
3199
  if (currentFeatures & 16 /* BigIntTypedArray */) {
3283
3200
  switch (currentClass) {
3284
3201
  case BigInt64Array:
@@ -3313,36 +3230,39 @@ var BaseSyncParserContext = class extends BaseParserContext {
3313
3230
  }
3314
3231
  throw new SerovalUnsupportedTypeError(current);
3315
3232
  }
3316
- parse(current) {
3317
- try {
3318
- switch (typeof current) {
3319
- case "boolean":
3320
- return current ? TRUE_NODE : FALSE_NODE;
3321
- case "undefined":
3322
- return UNDEFINED_NODE;
3323
- case "string":
3324
- return createStringNode(current);
3325
- case "number":
3326
- return createNumberNode(current);
3327
- case "bigint":
3328
- return createBigIntNode(current);
3329
- case "object": {
3330
- if (current) {
3331
- const ref = this.getReference(current);
3332
- return ref.type === 0 /* Fresh */ ? this.parseObject(ref.value, current) : ref.value;
3333
- }
3334
- return NULL_NODE;
3335
- }
3336
- case "symbol":
3337
- return this.parseWellKnownSymbol(current);
3338
- case "function": {
3339
- return this.parseFunction(current);
3233
+ parseTop(current) {
3234
+ switch (typeof current) {
3235
+ case "boolean":
3236
+ return current ? TRUE_NODE : FALSE_NODE;
3237
+ case "undefined":
3238
+ return UNDEFINED_NODE;
3239
+ case "string":
3240
+ return createStringNode(current);
3241
+ case "number":
3242
+ return createNumberNode(current);
3243
+ case "bigint":
3244
+ return createBigIntNode(current);
3245
+ case "object": {
3246
+ if (current) {
3247
+ const ref = this.getReference(current);
3248
+ return ref.type === 0 /* Fresh */ ? this.parseObject(ref.value, current) : ref.value;
3340
3249
  }
3341
- default:
3342
- throw new SerovalUnsupportedTypeError(current);
3250
+ return NULL_NODE;
3251
+ }
3252
+ case "symbol":
3253
+ return this.parseWellKnownSymbol(current);
3254
+ case "function": {
3255
+ return this.parseFunction(current);
3343
3256
  }
3257
+ default:
3258
+ throw new SerovalUnsupportedTypeError(current);
3259
+ }
3260
+ }
3261
+ parse(current) {
3262
+ try {
3263
+ return this.parseTop(current);
3344
3264
  } catch (error) {
3345
- throw new SerovalParserError(error);
3265
+ throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3346
3266
  }
3347
3267
  }
3348
3268
  };
@@ -3406,7 +3326,7 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3406
3326
  const valueNodes = [];
3407
3327
  for (let i = 0, len = entries.length; i < len; i++) {
3408
3328
  keyNodes.push(serializeString(entries[i][0]));
3409
- valueNodes.push(this.parse(entries[i][1]));
3329
+ valueNodes.push(this.parseTop(entries[i][1]));
3410
3330
  }
3411
3331
  let symbol = Symbol.iterator;
3412
3332
  if (symbol in properties) {
@@ -3414,7 +3334,7 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3414
3334
  valueNodes.push(
3415
3335
  createIteratorFactoryInstanceNode(
3416
3336
  this.parseIteratorFactory(),
3417
- this.parse(
3337
+ this.parseTop(
3418
3338
  iteratorToSequence(properties)
3419
3339
  )
3420
3340
  )
@@ -3426,7 +3346,7 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3426
3346
  valueNodes.push(
3427
3347
  createAsyncIteratorFactoryInstanceNode(
3428
3348
  this.parseAsyncIteratorFactory(),
3429
- this.parse(
3349
+ this.parseTop(
3430
3350
  createStreamFromAsyncIterable(
3431
3351
  properties
3432
3352
  )
@@ -3450,63 +3370,63 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3450
3370
  s: keyNodes.length
3451
3371
  };
3452
3372
  }
3373
+ handlePromiseSuccess(id, data) {
3374
+ const parsed = this.parseWithError(data);
3375
+ if (parsed) {
3376
+ this.onParse(
3377
+ createSerovalNode(
3378
+ 23 /* PromiseSuccess */,
3379
+ id,
3380
+ NIL,
3381
+ NIL,
3382
+ NIL,
3383
+ NIL,
3384
+ NIL,
3385
+ NIL,
3386
+ [this.parseSpecialReference(2 /* PromiseSuccess */), parsed],
3387
+ NIL,
3388
+ NIL,
3389
+ NIL
3390
+ )
3391
+ );
3392
+ }
3393
+ this.popPendingState();
3394
+ }
3395
+ handlePromiseFailure(id, data) {
3396
+ if (this.alive) {
3397
+ const parsed = this.parseWithError(data);
3398
+ if (parsed) {
3399
+ this.onParse(
3400
+ createSerovalNode(
3401
+ 24 /* PromiseFailure */,
3402
+ id,
3403
+ NIL,
3404
+ NIL,
3405
+ NIL,
3406
+ NIL,
3407
+ NIL,
3408
+ NIL,
3409
+ [
3410
+ this.parseSpecialReference(3 /* PromiseFailure */),
3411
+ parsed
3412
+ ],
3413
+ NIL,
3414
+ NIL,
3415
+ NIL
3416
+ )
3417
+ );
3418
+ }
3419
+ }
3420
+ this.popPendingState();
3421
+ }
3453
3422
  parsePromise(id, current) {
3423
+ const resolver = this.createIndex({});
3454
3424
  current.then(
3455
- (data) => {
3456
- const parsed = this.parseWithError(data);
3457
- if (parsed) {
3458
- this.onParse(
3459
- createSerovalNode(
3460
- 23 /* PromiseResolve */,
3461
- id,
3462
- NIL,
3463
- NIL,
3464
- NIL,
3465
- NIL,
3466
- NIL,
3467
- NIL,
3468
- [
3469
- this.parseSpecialReference(2 /* PromiseResolve */),
3470
- parsed
3471
- ],
3472
- NIL,
3473
- NIL,
3474
- NIL
3475
- )
3476
- );
3477
- }
3478
- this.popPendingState();
3479
- },
3480
- (data) => {
3481
- if (this.alive) {
3482
- const parsed = this.parseWithError(data);
3483
- if (parsed) {
3484
- this.onParse(
3485
- createSerovalNode(
3486
- 24 /* PromiseReject */,
3487
- id,
3488
- NIL,
3489
- NIL,
3490
- NIL,
3491
- NIL,
3492
- NIL,
3493
- NIL,
3494
- [
3495
- this.parseSpecialReference(3 /* PromiseReject */),
3496
- parsed
3497
- ],
3498
- NIL,
3499
- NIL,
3500
- NIL
3501
- )
3502
- );
3503
- }
3504
- }
3505
- this.popPendingState();
3506
- }
3425
+ this.handlePromiseSuccess.bind(this, resolver),
3426
+ this.handlePromiseFailure.bind(this, resolver)
3507
3427
  );
3508
3428
  this.pushPendingState();
3509
- return this.createPromiseConstructorNode(id);
3429
+ return this.createPromiseConstructorNode(id, resolver);
3510
3430
  }
3511
3431
  parsePlugin(id, current) {
3512
3432
  const currentPlugins = this.plugins;
@@ -3563,48 +3483,9 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3563
3483
  });
3564
3484
  return result;
3565
3485
  }
3566
- handleAbortSignal(id, current) {
3567
- if (this.alive) {
3568
- const parsed = this.parseWithError(current.reason);
3569
- if (parsed) {
3570
- this.onParse(
3571
- createSerovalNode(
3572
- 36 /* AbortSignalAbort */,
3573
- id,
3574
- NIL,
3575
- NIL,
3576
- NIL,
3577
- NIL,
3578
- NIL,
3579
- NIL,
3580
- [
3581
- this.parseSpecialReference(6 /* AbortSignalAbort */),
3582
- parsed
3583
- ],
3584
- NIL,
3585
- NIL,
3586
- NIL
3587
- )
3588
- );
3589
- }
3590
- }
3591
- this.popPendingState();
3592
- }
3593
- parseAbortSignal(id, current) {
3594
- if (current.aborted) {
3595
- return this.parseAbortSignalSync(id, current);
3596
- }
3597
- this.pushPendingState();
3598
- current.addEventListener(
3599
- "abort",
3600
- this.handleAbortSignal.bind(this, id, current),
3601
- { once: true }
3602
- );
3603
- return this.createAbortSignalConstructorNode(id);
3604
- }
3605
3486
  parseWithError(current) {
3606
3487
  try {
3607
- return this.parse(current);
3488
+ return this.parseTop(current);
3608
3489
  } catch (err) {
3609
3490
  this.onError(err);
3610
3491
  return NIL;
@@ -3845,9 +3726,6 @@ var VanillaSerializerContext = class extends BaseSerializerContext {
3845
3726
  serializePromiseReject(node) {
3846
3727
  throw new SerovalUnsupportedNodeError(node);
3847
3728
  }
3848
- serializeAbortSignalAbort(node) {
3849
- throw new SerovalUnsupportedNodeError(node);
3850
- }
3851
3729
  serializeTop(tree) {
3852
3730
  const result = this.serialize(tree);
3853
3731
  if (tree.i != null && this.vars.length) {