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
@@ -5,10 +5,9 @@ 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["AbortSignal"] = 32] = "AbortSignal";
9
8
  return Feature2;
10
9
  })(Feature || {});
11
- var ALL_ENABLED = 47;
10
+ var ALL_ENABLED = 1 /* AggregateError */ | 2 /* ArrowFunction */ | 4 /* ErrorPrototypeStack */ | 8 /* ObjectAssign */ | 16 /* BigIntTypedArray */;
12
11
 
13
12
  // src/core/string.ts
14
13
  function serializeChar(str) {
@@ -184,19 +183,6 @@ function resolvePlugins(plugins) {
184
183
  return void 0;
185
184
  }
186
185
 
187
- // src/core/abort-signal.ts
188
- function resolveAbortSignalResult(resolve) {
189
- resolve(this.reason);
190
- }
191
- function resolveAbortSignal(resolve) {
192
- this.addEventListener("abort", resolveAbortSignalResult.bind(this, resolve), {
193
- once: true
194
- });
195
- }
196
- function abortSignalToPromise(signal) {
197
- return new Promise(resolveAbortSignal.bind(signal));
198
- }
199
-
200
186
  // src/core/constants.ts
201
187
  var SYMBOL_STRING = {
202
188
  [0 /* AsyncIterator */]: "Symbol.asyncIterator",
@@ -485,10 +471,11 @@ function createIndexedValueNode(id) {
485
471
  );
486
472
  }
487
473
  function createDateNode(id, current) {
474
+ const timestamp = current.valueOf();
488
475
  return createSerovalNode(
489
476
  5 /* Date */,
490
477
  id,
491
- current.toISOString(),
478
+ timestamp !== timestamp ? "" : current.toISOString(),
492
479
  NIL,
493
480
  NIL,
494
481
  NIL,
@@ -904,18 +891,96 @@ var OpaqueReference = class {
904
891
  }
905
892
  };
906
893
 
894
+ // src/core/function-string.ts
895
+ function createFunction(features, parameters, body) {
896
+ if (features & 2 /* ArrowFunction */) {
897
+ const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
898
+ return joined + "=>" + (body.startsWith("{") ? "(" + body + ")" : body);
899
+ }
900
+ return "function(" + parameters.join(",") + "){return " + body + "}";
901
+ }
902
+ function createEffectfulFunction(features, parameters, body) {
903
+ if (features & 2 /* ArrowFunction */) {
904
+ const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
905
+ return joined + "=>{" + body + "}";
906
+ }
907
+ return "function(" + parameters.join(",") + "){" + body + "}";
908
+ }
909
+
907
910
  // src/core/special-reference.ts
908
911
  var ITERATOR = {};
909
912
  var ASYNC_ITERATOR = {};
910
913
  var SPECIAL_REFS = {
911
914
  [0 /* MapSentinel */]: {},
912
915
  [1 /* PromiseConstructor */]: {},
913
- [2 /* PromiseResolve */]: {},
914
- [3 /* PromiseReject */]: {},
915
- [4 /* StreamConstructor */]: {},
916
- [5 /* AbortSignalConstructor */]: {},
917
- [6 /* AbortSignalAbort */]: {}
916
+ [2 /* PromiseSuccess */]: {},
917
+ [3 /* PromiseFailure */]: {},
918
+ [4 /* StreamConstructor */]: {}
918
919
  };
920
+ function serializePromiseConstructor(features) {
921
+ return createFunction(
922
+ features,
923
+ ["r"],
924
+ "(r.p=new Promise(" + createEffectfulFunction(features, ["s", "f"], "r.s=s,r.f=f") + "))"
925
+ );
926
+ }
927
+ function serializePromiseSuccess(features) {
928
+ return createEffectfulFunction(
929
+ features,
930
+ ["r", "d"],
931
+ "r.s(d),r.p.s=1,r.p.v=d"
932
+ );
933
+ }
934
+ function serializePromiseFailure(features) {
935
+ return createEffectfulFunction(
936
+ features,
937
+ ["r", "d"],
938
+ "r.f(d),r.p.s=2,r.p.v=d"
939
+ );
940
+ }
941
+ function serializeStreamConstructor(features) {
942
+ return createFunction(
943
+ features,
944
+ ["b", "a", "s", "l", "p", "f", "e", "n"],
945
+ "(b=[],a=!0,s=!1,l=[],p=0,f=" + createEffectfulFunction(
946
+ features,
947
+ ["v", "m", "x"],
948
+ "for(x=0;x<p;x++)l[x]&&l[x][m](v)"
949
+ ) + ",n=" + createEffectfulFunction(
950
+ features,
951
+ ["o", "x", "z", "c"],
952
+ 'for(x=0,z=b.length;x<z;x++)(c=b[x],(!a&&x===z-1)?o[s?"return":"throw"](c):o.next(c))'
953
+ ) + ",e=" + createFunction(
954
+ features,
955
+ ["o", "t"],
956
+ "(a&&(l[t=p++]=o),n(o)," + createEffectfulFunction(features, [], "a&&(l[t]=void 0)") + ")"
957
+ ) + ",{__SEROVAL_STREAM__:!0,on:" + createFunction(features, ["o"], "e(o)") + ",next:" + createEffectfulFunction(features, ["v"], 'a&&(b.push(v),f(v,"next"))') + ",throw:" + createEffectfulFunction(
958
+ features,
959
+ ["v"],
960
+ 'a&&(b.push(v),f(v,"throw"),a=s=!1,l.length=0)'
961
+ ) + ",return:" + createEffectfulFunction(
962
+ features,
963
+ ["v"],
964
+ 'a&&(b.push(v),f(v,"return"),a=!1,s=!0,l.length=0)'
965
+ ) + "})"
966
+ );
967
+ }
968
+ function serializeSpecialReferenceValue(features, ref) {
969
+ switch (ref) {
970
+ case 0 /* MapSentinel */:
971
+ return "[]";
972
+ case 1 /* PromiseConstructor */:
973
+ return serializePromiseConstructor(features);
974
+ case 2 /* PromiseSuccess */:
975
+ return serializePromiseSuccess(features);
976
+ case 3 /* PromiseFailure */:
977
+ return serializePromiseFailure(features);
978
+ case 4 /* StreamConstructor */:
979
+ return serializeStreamConstructor(features);
980
+ default:
981
+ return "";
982
+ }
983
+ }
919
984
 
920
985
  // src/core/utils/deferred.ts
921
986
  function createDeferred() {
@@ -1179,6 +1244,11 @@ var BaseParserContext = class {
1179
1244
  isMarked(id) {
1180
1245
  return this.marked.has(id);
1181
1246
  }
1247
+ createIndex(current) {
1248
+ const id = this.refs.size;
1249
+ this.refs.set(current, id);
1250
+ return id;
1251
+ }
1182
1252
  getIndexedValue(current) {
1183
1253
  const registeredId = this.refs.get(current);
1184
1254
  if (registeredId != null) {
@@ -1188,11 +1258,9 @@ var BaseParserContext = class {
1188
1258
  value: createIndexedValueNode(registeredId)
1189
1259
  };
1190
1260
  }
1191
- const id = this.refs.size;
1192
- this.refs.set(current, id);
1193
1261
  return {
1194
1262
  type: 0 /* Fresh */,
1195
- value: id
1263
+ value: this.createIndex(current)
1196
1264
  };
1197
1265
  }
1198
1266
  getReference(current) {
@@ -1311,11 +1379,11 @@ var BaseParserContext = class {
1311
1379
  NIL
1312
1380
  );
1313
1381
  }
1314
- createPromiseConstructorNode(id) {
1382
+ createPromiseConstructorNode(id, resolver) {
1315
1383
  return createSerovalNode(
1316
1384
  22 /* PromiseConstructor */,
1317
1385
  id,
1318
- NIL,
1386
+ resolver,
1319
1387
  NIL,
1320
1388
  NIL,
1321
1389
  NIL,
@@ -1327,22 +1395,6 @@ var BaseParserContext = class {
1327
1395
  NIL
1328
1396
  );
1329
1397
  }
1330
- createAbortSignalConstructorNode(id) {
1331
- return createSerovalNode(
1332
- 35 /* AbortSignalConstructor */,
1333
- id,
1334
- NIL,
1335
- NIL,
1336
- NIL,
1337
- NIL,
1338
- NIL,
1339
- NIL,
1340
- NIL,
1341
- this.parseSpecialReference(5 /* AbortSignalConstructor */),
1342
- NIL,
1343
- NIL
1344
- );
1345
- }
1346
1398
  };
1347
1399
 
1348
1400
  // src/core/context/parser/async.ts
@@ -1351,7 +1403,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1351
1403
  const nodes = [];
1352
1404
  for (let i = 0, len = current.length; i < len; i++) {
1353
1405
  if (i in current) {
1354
- nodes[i] = await this.parse(current[i]);
1406
+ nodes[i] = await this.parseTop(current[i]);
1355
1407
  }
1356
1408
  }
1357
1409
  return nodes;
@@ -1365,7 +1417,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1365
1417
  const valueNodes = [];
1366
1418
  for (let i = 0, len = entries.length; i < len; i++) {
1367
1419
  keyNodes.push(serializeString(entries[i][0]));
1368
- valueNodes.push(await this.parse(entries[i][1]));
1420
+ valueNodes.push(await this.parseTop(entries[i][1]));
1369
1421
  }
1370
1422
  let symbol = Symbol.iterator;
1371
1423
  if (symbol in properties) {
@@ -1373,7 +1425,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1373
1425
  valueNodes.push(
1374
1426
  createIteratorFactoryInstanceNode(
1375
1427
  this.parseIteratorFactory(),
1376
- await this.parse(
1428
+ await this.parseTop(
1377
1429
  iteratorToSequence(properties)
1378
1430
  )
1379
1431
  )
@@ -1385,7 +1437,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1385
1437
  valueNodes.push(
1386
1438
  createAsyncIteratorFactoryInstanceNode(
1387
1439
  this.parseAsyncIteratorFactory(),
1388
- await this.parse(
1440
+ await this.parseTop(
1389
1441
  createStreamFromAsyncIterable(
1390
1442
  properties
1391
1443
  )
@@ -1418,20 +1470,24 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1418
1470
  );
1419
1471
  }
1420
1472
  async parseBoxed(id, current) {
1421
- return createBoxedNode(id, await this.parse(current.valueOf()));
1473
+ return createBoxedNode(id, await this.parseTop(current.valueOf()));
1422
1474
  }
1423
1475
  async parseTypedArray(id, current) {
1424
- return createTypedArrayNode(id, current, await this.parse(current.buffer));
1476
+ return createTypedArrayNode(
1477
+ id,
1478
+ current,
1479
+ await this.parseTop(current.buffer)
1480
+ );
1425
1481
  }
1426
1482
  async parseBigIntTypedArray(id, current) {
1427
1483
  return createBigIntTypedArrayNode(
1428
1484
  id,
1429
1485
  current,
1430
- await this.parse(current.buffer)
1486
+ await this.parseTop(current.buffer)
1431
1487
  );
1432
1488
  }
1433
1489
  async parseDataView(id, current) {
1434
- return createDataViewNode(id, current, await this.parse(current.buffer));
1490
+ return createDataViewNode(id, current, await this.parseTop(current.buffer));
1435
1491
  }
1436
1492
  async parseError(id, current) {
1437
1493
  const options = getErrorOptions(current, this.features);
@@ -1453,15 +1509,15 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1453
1509
  const keyNodes = [];
1454
1510
  const valueNodes = [];
1455
1511
  for (const [key, value] of current.entries()) {
1456
- keyNodes.push(await this.parse(key));
1457
- valueNodes.push(await this.parse(value));
1512
+ keyNodes.push(await this.parseTop(key));
1513
+ valueNodes.push(await this.parseTop(value));
1458
1514
  }
1459
1515
  return this.createMapNode(id, keyNodes, valueNodes, current.size);
1460
1516
  }
1461
1517
  async parseSet(id, current) {
1462
1518
  const items = [];
1463
1519
  for (const item of current.keys()) {
1464
- items.push(await this.parse(item));
1520
+ items.push(await this.parseTop(item));
1465
1521
  }
1466
1522
  return createSetNode(id, current.size, items);
1467
1523
  }
@@ -1477,7 +1533,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1477
1533
  NIL,
1478
1534
  NIL,
1479
1535
  NIL,
1480
- await this.parse(result),
1536
+ await this.parseTop(result),
1481
1537
  NIL,
1482
1538
  NIL
1483
1539
  );
@@ -1509,7 +1565,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1509
1565
  const cleanup = current.on({
1510
1566
  next: (value) => {
1511
1567
  this.markRef(id);
1512
- this.parse(value).then(
1568
+ this.parseTop(value).then(
1513
1569
  (data) => {
1514
1570
  sequence.push(createStreamNextNode(id, data));
1515
1571
  },
@@ -1521,7 +1577,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1521
1577
  },
1522
1578
  throw: (value) => {
1523
1579
  this.markRef(id);
1524
- this.parse(value).then(
1580
+ this.parseTop(value).then(
1525
1581
  (data) => {
1526
1582
  sequence.push(createStreamThrowNode(id, data));
1527
1583
  resolve(sequence);
@@ -1535,7 +1591,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1535
1591
  },
1536
1592
  return: (value) => {
1537
1593
  this.markRef(id);
1538
- this.parse(value).then(
1594
+ this.parseTop(value).then(
1539
1595
  (data) => {
1540
1596
  sequence.push(createStreamReturnNode(id, data));
1541
1597
  resolve(sequence);
@@ -1551,28 +1607,6 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1551
1607
  })
1552
1608
  );
1553
1609
  }
1554
- async parseAbortSignalSync(id, current) {
1555
- return createSerovalNode(
1556
- 37 /* AbortSignalSync */,
1557
- id,
1558
- NIL,
1559
- NIL,
1560
- NIL,
1561
- NIL,
1562
- NIL,
1563
- NIL,
1564
- NIL,
1565
- await this.parse(current.reason),
1566
- NIL,
1567
- NIL
1568
- );
1569
- }
1570
- async parseAbortSignal(id, current) {
1571
- if (!current.aborted) {
1572
- await abortSignalToPromise(current);
1573
- }
1574
- return this.parseAbortSignalSync(id, current);
1575
- }
1576
1610
  // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: ehh
1577
1611
  async parseObject(id, current) {
1578
1612
  if (Array.isArray(current)) {
@@ -1583,7 +1617,7 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1583
1617
  }
1584
1618
  const currentClass = current.constructor;
1585
1619
  if (currentClass === OpaqueReference) {
1586
- return this.parse(
1620
+ return this.parseTop(
1587
1621
  current.replacement
1588
1622
  );
1589
1623
  }
@@ -1646,9 +1680,6 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1646
1680
  return this.parsePromise(id, current);
1647
1681
  }
1648
1682
  const currentFeatures = this.features;
1649
- if (currentFeatures & 32 /* AbortSignal */ && typeof AbortSignal !== "undefined" && currentClass === AbortSignal) {
1650
- return this.parseAbortSignal(id, current);
1651
- }
1652
1683
  if (currentFeatures & 16 /* BigIntTypedArray */) {
1653
1684
  switch (currentClass) {
1654
1685
  case BigInt64Array:
@@ -1683,35 +1714,38 @@ var BaseAsyncParserContext = class extends BaseParserContext {
1683
1714
  }
1684
1715
  throw new SerovalUnsupportedTypeError(current);
1685
1716
  }
1686
- async parse(current) {
1687
- try {
1688
- switch (typeof current) {
1689
- case "boolean":
1690
- return current ? TRUE_NODE : FALSE_NODE;
1691
- case "undefined":
1692
- return UNDEFINED_NODE;
1693
- case "string":
1694
- return createStringNode(current);
1695
- case "number":
1696
- return createNumberNode(current);
1697
- case "bigint":
1698
- return createBigIntNode(current);
1699
- case "object": {
1700
- if (current) {
1701
- const ref = this.getReference(current);
1702
- return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
1703
- }
1704
- return NULL_NODE;
1717
+ async parseTop(current) {
1718
+ switch (typeof current) {
1719
+ case "boolean":
1720
+ return current ? TRUE_NODE : FALSE_NODE;
1721
+ case "undefined":
1722
+ return UNDEFINED_NODE;
1723
+ case "string":
1724
+ return createStringNode(current);
1725
+ case "number":
1726
+ return createNumberNode(current);
1727
+ case "bigint":
1728
+ return createBigIntNode(current);
1729
+ case "object": {
1730
+ if (current) {
1731
+ const ref = this.getReference(current);
1732
+ return ref.type === 0 ? await this.parseObject(ref.value, current) : ref.value;
1705
1733
  }
1706
- case "symbol":
1707
- return this.parseWellKnownSymbol(current);
1708
- case "function":
1709
- return this.parseFunction(current);
1710
- default:
1711
- throw new SerovalUnsupportedTypeError(current);
1734
+ return NULL_NODE;
1712
1735
  }
1736
+ case "symbol":
1737
+ return this.parseWellKnownSymbol(current);
1738
+ case "function":
1739
+ return this.parseFunction(current);
1740
+ default:
1741
+ throw new SerovalUnsupportedTypeError(current);
1742
+ }
1743
+ }
1744
+ async parse(current) {
1745
+ try {
1746
+ return await this.parseTop(current);
1713
1747
  } catch (error) {
1714
- throw new SerovalParserError(error);
1748
+ throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
1715
1749
  }
1716
1750
  }
1717
1751
  };
@@ -1982,21 +2016,6 @@ var BaseDeserializerContext = class {
1982
2016
  this.deserialize(node.a[1]);
1983
2017
  return void 0;
1984
2018
  }
1985
- deserializeAbortSignalConstructor(node) {
1986
- return this.assignIndexedValue(node.i, new AbortController()).signal;
1987
- }
1988
- deserializeAbortSignalAbort(node) {
1989
- const controller = this.refs.get(node.i);
1990
- assert(controller, new SerovalMissingInstanceError("AbortSignal"));
1991
- controller.abort(this.deserialize(node.a[1]));
1992
- return void 0;
1993
- }
1994
- deserializeAbortSignalSync(node) {
1995
- return this.assignIndexedValue(
1996
- node.i,
1997
- AbortSignal.abort(this.deserialize(node.f))
1998
- );
1999
- }
2000
2019
  deserialize(node) {
2001
2020
  try {
2002
2021
  switch (node.t) {
@@ -2046,9 +2065,9 @@ var BaseDeserializerContext = class {
2046
2065
  return this.deserializePlugin(node);
2047
2066
  case 22 /* PromiseConstructor */:
2048
2067
  return this.deserializePromiseConstructor(node);
2049
- case 23 /* PromiseResolve */:
2068
+ case 23 /* PromiseSuccess */:
2050
2069
  return this.deserializePromiseResolve(node);
2051
- case 24 /* PromiseReject */:
2070
+ case 24 /* PromiseFailure */:
2052
2071
  return this.deserializePromiseReject(node);
2053
2072
  case 28 /* IteratorFactoryInstance */:
2054
2073
  return this.deserializeIteratorFactoryInstance(node);
@@ -2066,12 +2085,6 @@ var BaseDeserializerContext = class {
2066
2085
  return this.deserializeIteratorFactory(node);
2067
2086
  case 29 /* AsyncIteratorFactory */:
2068
2087
  return this.deserializeAsyncIteratorFactory(node);
2069
- case 36 /* AbortSignalAbort */:
2070
- return this.deserializeAbortSignalAbort(node);
2071
- case 35 /* AbortSignalConstructor */:
2072
- return this.deserializeAbortSignalConstructor(node);
2073
- case 37 /* AbortSignalSync */:
2074
- return this.deserializeAbortSignalSync(node);
2075
2088
  // case SerovalNodeType.SpecialReference:
2076
2089
  default:
2077
2090
  throw new SerovalUnsupportedNodeError(node);
@@ -2202,18 +2215,10 @@ var BaseSerializerContext = class {
2202
2215
  this.marked = new Set(options.markedRefs);
2203
2216
  }
2204
2217
  createFunction(parameters, body) {
2205
- if (this.features & 2 /* ArrowFunction */) {
2206
- const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
2207
- return joined + "=>" + (body.startsWith("{") ? "(" + body + ")" : body);
2208
- }
2209
- return "function(" + parameters.join(",") + "){return " + body + "}";
2218
+ return createFunction(this.features, parameters, body);
2210
2219
  }
2211
2220
  createEffectfulFunction(parameters, body) {
2212
- if (this.features & 2 /* ArrowFunction */) {
2213
- const joined = parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")";
2214
- return joined + "=>{" + body + "}";
2215
- }
2216
- return "function(" + parameters.join(",") + "){" + body + "}";
2221
+ return createEffectfulFunction(this.features, parameters, body);
2217
2222
  }
2218
2223
  /**
2219
2224
  * A tiny function that tells if a reference
@@ -2682,7 +2687,11 @@ var BaseSerializerContext = class {
2682
2687
  return current === this.getRefParam(node.i) ? current : "(" + current + ")";
2683
2688
  }
2684
2689
  serializePromiseConstructor(node) {
2685
- return this.assignIndexedValue(node.i, this.getConstructor(node.f) + "()");
2690
+ const resolver = this.assignIndexedValue(node.s, "{p:0,s:0,f:0}");
2691
+ return this.assignIndexedValue(
2692
+ node.i,
2693
+ this.getConstructor(node.f) + "(" + resolver + ")"
2694
+ );
2686
2695
  }
2687
2696
  serializePromiseResolve(node) {
2688
2697
  return this.getConstructor(node.a[0]) + "(" + this.getRefParam(node.i) + "," + this.serialize(node.a[1]) + ")";
@@ -2690,63 +2699,10 @@ var BaseSerializerContext = class {
2690
2699
  serializePromiseReject(node) {
2691
2700
  return this.getConstructor(node.a[0]) + "(" + this.getRefParam(node.i) + "," + this.serialize(node.a[1]) + ")";
2692
2701
  }
2693
- serializeSpecialReferenceValue(ref) {
2694
- switch (ref) {
2695
- case 0 /* MapSentinel */:
2696
- return "[]";
2697
- case 1 /* PromiseConstructor */:
2698
- return this.createFunction(
2699
- ["s", "f", "p"],
2700
- "((p=new Promise(" + this.createEffectfulFunction(["a", "b"], "s=a,f=b") + ")).s=s,p.f=f,p)"
2701
- );
2702
- case 2 /* PromiseResolve */:
2703
- return this.createEffectfulFunction(
2704
- ["p", "d"],
2705
- 'p.s(d),p.status="success",p.value=d;delete p.s;delete p.f'
2706
- );
2707
- case 3 /* PromiseReject */:
2708
- return this.createEffectfulFunction(
2709
- ["p", "d"],
2710
- 'p.f(d),p.status="failure",p.value=d;delete p.s;delete p.f'
2711
- );
2712
- case 4 /* StreamConstructor */:
2713
- return this.createFunction(
2714
- ["b", "a", "s", "l", "p", "f", "e", "n"],
2715
- "(b=[],a=!0,s=!1,l=[],p=0,f=" + this.createEffectfulFunction(
2716
- ["v", "m", "x"],
2717
- "for(x=0;x<p;x++)l[x]&&l[x][m](v)"
2718
- ) + ",n=" + this.createEffectfulFunction(
2719
- ["o", "x", "z", "c"],
2720
- 'for(x=0,z=b.length;x<z;x++)(c=b[x],(!a&&x===z-1)?o[s?"return":"throw"](c):o.next(c))'
2721
- ) + ",e=" + this.createFunction(
2722
- ["o", "t"],
2723
- "(a&&(l[t=p++]=o),n(o)," + this.createEffectfulFunction([], "a&&(l[t]=void 0)") + ")"
2724
- ) + ",{__SEROVAL_STREAM__:!0,on:" + this.createFunction(["o"], "e(o)") + ",next:" + this.createEffectfulFunction(["v"], 'a&&(b.push(v),f(v,"next"))') + ",throw:" + this.createEffectfulFunction(
2725
- ["v"],
2726
- 'a&&(b.push(v),f(v,"throw"),a=s=!1,l.length=0)'
2727
- ) + ",return:" + this.createEffectfulFunction(
2728
- ["v"],
2729
- 'a&&(b.push(v),f(v,"return"),a=!1,s=!0,l.length=0)'
2730
- ) + "})"
2731
- );
2732
- case 5 /* AbortSignalConstructor */:
2733
- return this.createFunction(
2734
- ["a", "s"],
2735
- "((s=(a=new AbortController).signal).a=a,s)"
2736
- );
2737
- case 6 /* AbortSignalAbort */:
2738
- return this.createEffectfulFunction(
2739
- ["s", "r"],
2740
- "s.a.abort(r);delete s.a"
2741
- );
2742
- default:
2743
- return "";
2744
- }
2745
- }
2746
2702
  serializeSpecialReference(node) {
2747
2703
  return this.assignIndexedValue(
2748
2704
  node.i,
2749
- this.serializeSpecialReferenceValue(node.s)
2705
+ serializeSpecialReferenceValue(this.features, node.s)
2750
2706
  );
2751
2707
  }
2752
2708
  serializeIteratorFactory(node) {
@@ -2850,18 +2806,6 @@ var BaseSerializerContext = class {
2850
2806
  serializeStreamReturn(node) {
2851
2807
  return this.getRefParam(node.i) + ".return(" + this.serialize(node.f) + ")";
2852
2808
  }
2853
- serializeAbortSignalSync(node) {
2854
- return this.assignIndexedValue(
2855
- node.i,
2856
- "AbortSignal.abort(" + this.serialize(node.f) + ")"
2857
- );
2858
- }
2859
- serializeAbortSignalConstructor(node) {
2860
- return this.assignIndexedValue(node.i, this.getConstructor(node.f) + "()");
2861
- }
2862
- serializeAbortSignalAbort(node) {
2863
- return this.getConstructor(node.a[0]) + "(" + this.getRefParam(node.i) + "," + this.serialize(node.a[1]) + ")";
2864
- }
2865
2809
  serialize(node) {
2866
2810
  try {
2867
2811
  switch (node.t) {
@@ -2910,9 +2854,9 @@ var BaseSerializerContext = class {
2910
2854
  return this.serializeBoxed(node);
2911
2855
  case 22 /* PromiseConstructor */:
2912
2856
  return this.serializePromiseConstructor(node);
2913
- case 23 /* PromiseResolve */:
2857
+ case 23 /* PromiseSuccess */:
2914
2858
  return this.serializePromiseResolve(node);
2915
- case 24 /* PromiseReject */:
2859
+ case 24 /* PromiseFailure */:
2916
2860
  return this.serializePromiseReject(node);
2917
2861
  case 25 /* Plugin */:
2918
2862
  return this.serializePlugin(node);
@@ -2934,12 +2878,6 @@ var BaseSerializerContext = class {
2934
2878
  return this.serializeStreamThrow(node);
2935
2879
  case 34 /* StreamReturn */:
2936
2880
  return this.serializeStreamReturn(node);
2937
- case 36 /* AbortSignalAbort */:
2938
- return this.serializeAbortSignalAbort(node);
2939
- case 35 /* AbortSignalConstructor */:
2940
- return this.serializeAbortSignalConstructor(node);
2941
- case 37 /* AbortSignalSync */:
2942
- return this.serializeAbortSignalSync(node);
2943
2881
  default:
2944
2882
  throw new SerovalUnsupportedNodeError(node);
2945
2883
  }
@@ -2989,7 +2927,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
2989
2927
  const nodes = [];
2990
2928
  for (let i = 0, len = current.length; i < len; i++) {
2991
2929
  if (i in current) {
2992
- nodes[i] = this.parse(current[i]);
2930
+ nodes[i] = this.parseTop(current[i]);
2993
2931
  }
2994
2932
  }
2995
2933
  return nodes;
@@ -3003,7 +2941,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3003
2941
  const valueNodes = [];
3004
2942
  for (let i = 0, len = entries.length; i < len; i++) {
3005
2943
  keyNodes.push(serializeString(entries[i][0]));
3006
- valueNodes.push(this.parse(entries[i][1]));
2944
+ valueNodes.push(this.parseTop(entries[i][1]));
3007
2945
  }
3008
2946
  let symbol = Symbol.iterator;
3009
2947
  if (symbol in properties) {
@@ -3011,7 +2949,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3011
2949
  valueNodes.push(
3012
2950
  createIteratorFactoryInstanceNode(
3013
2951
  this.parseIteratorFactory(),
3014
- this.parse(
2952
+ this.parseTop(
3015
2953
  iteratorToSequence(properties)
3016
2954
  )
3017
2955
  )
@@ -3023,7 +2961,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3023
2961
  valueNodes.push(
3024
2962
  createAsyncIteratorFactoryInstanceNode(
3025
2963
  this.parseAsyncIteratorFactory(),
3026
- this.parse(createStream())
2964
+ this.parseTop(createStream())
3027
2965
  )
3028
2966
  );
3029
2967
  }
@@ -3052,16 +2990,20 @@ var BaseSyncParserContext = class extends BaseParserContext {
3052
2990
  );
3053
2991
  }
3054
2992
  parseBoxed(id, current) {
3055
- return createBoxedNode(id, this.parse(current.valueOf()));
2993
+ return createBoxedNode(id, this.parseTop(current.valueOf()));
3056
2994
  }
3057
2995
  parseTypedArray(id, current) {
3058
- return createTypedArrayNode(id, current, this.parse(current.buffer));
2996
+ return createTypedArrayNode(id, current, this.parseTop(current.buffer));
3059
2997
  }
3060
2998
  parseBigIntTypedArray(id, current) {
3061
- return createBigIntTypedArrayNode(id, current, this.parse(current.buffer));
2999
+ return createBigIntTypedArrayNode(
3000
+ id,
3001
+ current,
3002
+ this.parseTop(current.buffer)
3003
+ );
3062
3004
  }
3063
3005
  parseDataView(id, current) {
3064
- return createDataViewNode(id, current, this.parse(current.buffer));
3006
+ return createDataViewNode(id, current, this.parseTop(current.buffer));
3065
3007
  }
3066
3008
  parseError(id, current) {
3067
3009
  const options = getErrorOptions(current, this.features);
@@ -3083,15 +3025,15 @@ var BaseSyncParserContext = class extends BaseParserContext {
3083
3025
  const keyNodes = [];
3084
3026
  const valueNodes = [];
3085
3027
  for (const [key, value] of current.entries()) {
3086
- keyNodes.push(this.parse(key));
3087
- valueNodes.push(this.parse(value));
3028
+ keyNodes.push(this.parseTop(key));
3029
+ valueNodes.push(this.parseTop(value));
3088
3030
  }
3089
3031
  return this.createMapNode(id, keyNodes, valueNodes, current.size);
3090
3032
  }
3091
3033
  parseSet(id, current) {
3092
3034
  const items = [];
3093
3035
  for (const item of current.keys()) {
3094
- items.push(this.parse(item));
3036
+ items.push(this.parseTop(item));
3095
3037
  }
3096
3038
  return createSetNode(id, current.size, items);
3097
3039
  }
@@ -3121,29 +3063,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3121
3063
  );
3122
3064
  }
3123
3065
  parsePromise(id, _current) {
3124
- return this.createPromiseConstructorNode(id);
3125
- }
3126
- parseAbortSignalSync(id, current) {
3127
- return createSerovalNode(
3128
- 37 /* AbortSignalSync */,
3129
- id,
3130
- NIL,
3131
- NIL,
3132
- NIL,
3133
- NIL,
3134
- NIL,
3135
- NIL,
3136
- NIL,
3137
- this.parse(current.reason),
3138
- NIL,
3139
- NIL
3140
- );
3141
- }
3142
- parseAbortSignal(id, current) {
3143
- if (current.aborted) {
3144
- return this.parseAbortSignalSync(id, current);
3145
- }
3146
- return this.createAbortSignalConstructorNode(id);
3066
+ return this.createPromiseConstructorNode(id, this.createIndex({}));
3147
3067
  }
3148
3068
  // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: ehh
3149
3069
  parseObject(id, current) {
@@ -3155,7 +3075,7 @@ var BaseSyncParserContext = class extends BaseParserContext {
3155
3075
  }
3156
3076
  const currentClass = current.constructor;
3157
3077
  if (currentClass === OpaqueReference) {
3158
- return this.parse(
3078
+ return this.parseTop(
3159
3079
  current.replacement
3160
3080
  );
3161
3081
  }
@@ -3218,9 +3138,6 @@ var BaseSyncParserContext = class extends BaseParserContext {
3218
3138
  return this.parsePromise(id, current);
3219
3139
  }
3220
3140
  const currentFeatures = this.features;
3221
- if (currentFeatures & 32 /* AbortSignal */ && typeof AbortSignal !== "undefined" && currentClass === AbortSignal) {
3222
- return this.parseAbortSignal(id, current);
3223
- }
3224
3141
  if (currentFeatures & 16 /* BigIntTypedArray */) {
3225
3142
  switch (currentClass) {
3226
3143
  case BigInt64Array:
@@ -3255,36 +3172,39 @@ var BaseSyncParserContext = class extends BaseParserContext {
3255
3172
  }
3256
3173
  throw new SerovalUnsupportedTypeError(current);
3257
3174
  }
3258
- parse(current) {
3259
- try {
3260
- switch (typeof current) {
3261
- case "boolean":
3262
- return current ? TRUE_NODE : FALSE_NODE;
3263
- case "undefined":
3264
- return UNDEFINED_NODE;
3265
- case "string":
3266
- return createStringNode(current);
3267
- case "number":
3268
- return createNumberNode(current);
3269
- case "bigint":
3270
- return createBigIntNode(current);
3271
- case "object": {
3272
- if (current) {
3273
- const ref = this.getReference(current);
3274
- return ref.type === 0 /* Fresh */ ? this.parseObject(ref.value, current) : ref.value;
3275
- }
3276
- return NULL_NODE;
3277
- }
3278
- case "symbol":
3279
- return this.parseWellKnownSymbol(current);
3280
- case "function": {
3281
- return this.parseFunction(current);
3175
+ parseTop(current) {
3176
+ switch (typeof current) {
3177
+ case "boolean":
3178
+ return current ? TRUE_NODE : FALSE_NODE;
3179
+ case "undefined":
3180
+ return UNDEFINED_NODE;
3181
+ case "string":
3182
+ return createStringNode(current);
3183
+ case "number":
3184
+ return createNumberNode(current);
3185
+ case "bigint":
3186
+ return createBigIntNode(current);
3187
+ case "object": {
3188
+ if (current) {
3189
+ const ref = this.getReference(current);
3190
+ return ref.type === 0 /* Fresh */ ? this.parseObject(ref.value, current) : ref.value;
3282
3191
  }
3283
- default:
3284
- throw new SerovalUnsupportedTypeError(current);
3192
+ return NULL_NODE;
3193
+ }
3194
+ case "symbol":
3195
+ return this.parseWellKnownSymbol(current);
3196
+ case "function": {
3197
+ return this.parseFunction(current);
3285
3198
  }
3199
+ default:
3200
+ throw new SerovalUnsupportedTypeError(current);
3201
+ }
3202
+ }
3203
+ parse(current) {
3204
+ try {
3205
+ return this.parseTop(current);
3286
3206
  } catch (error) {
3287
- throw new SerovalParserError(error);
3207
+ throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3288
3208
  }
3289
3209
  }
3290
3210
  };
@@ -3348,7 +3268,7 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3348
3268
  const valueNodes = [];
3349
3269
  for (let i = 0, len = entries.length; i < len; i++) {
3350
3270
  keyNodes.push(serializeString(entries[i][0]));
3351
- valueNodes.push(this.parse(entries[i][1]));
3271
+ valueNodes.push(this.parseTop(entries[i][1]));
3352
3272
  }
3353
3273
  let symbol = Symbol.iterator;
3354
3274
  if (symbol in properties) {
@@ -3356,7 +3276,7 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3356
3276
  valueNodes.push(
3357
3277
  createIteratorFactoryInstanceNode(
3358
3278
  this.parseIteratorFactory(),
3359
- this.parse(
3279
+ this.parseTop(
3360
3280
  iteratorToSequence(properties)
3361
3281
  )
3362
3282
  )
@@ -3368,7 +3288,7 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3368
3288
  valueNodes.push(
3369
3289
  createAsyncIteratorFactoryInstanceNode(
3370
3290
  this.parseAsyncIteratorFactory(),
3371
- this.parse(
3291
+ this.parseTop(
3372
3292
  createStreamFromAsyncIterable(
3373
3293
  properties
3374
3294
  )
@@ -3392,63 +3312,63 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3392
3312
  s: keyNodes.length
3393
3313
  };
3394
3314
  }
3315
+ handlePromiseSuccess(id, data) {
3316
+ const parsed = this.parseWithError(data);
3317
+ if (parsed) {
3318
+ this.onParse(
3319
+ createSerovalNode(
3320
+ 23 /* PromiseSuccess */,
3321
+ id,
3322
+ NIL,
3323
+ NIL,
3324
+ NIL,
3325
+ NIL,
3326
+ NIL,
3327
+ NIL,
3328
+ [this.parseSpecialReference(2 /* PromiseSuccess */), parsed],
3329
+ NIL,
3330
+ NIL,
3331
+ NIL
3332
+ )
3333
+ );
3334
+ }
3335
+ this.popPendingState();
3336
+ }
3337
+ handlePromiseFailure(id, data) {
3338
+ if (this.alive) {
3339
+ const parsed = this.parseWithError(data);
3340
+ if (parsed) {
3341
+ this.onParse(
3342
+ createSerovalNode(
3343
+ 24 /* PromiseFailure */,
3344
+ id,
3345
+ NIL,
3346
+ NIL,
3347
+ NIL,
3348
+ NIL,
3349
+ NIL,
3350
+ NIL,
3351
+ [
3352
+ this.parseSpecialReference(3 /* PromiseFailure */),
3353
+ parsed
3354
+ ],
3355
+ NIL,
3356
+ NIL,
3357
+ NIL
3358
+ )
3359
+ );
3360
+ }
3361
+ }
3362
+ this.popPendingState();
3363
+ }
3395
3364
  parsePromise(id, current) {
3365
+ const resolver = this.createIndex({});
3396
3366
  current.then(
3397
- (data) => {
3398
- const parsed = this.parseWithError(data);
3399
- if (parsed) {
3400
- this.onParse(
3401
- createSerovalNode(
3402
- 23 /* PromiseResolve */,
3403
- id,
3404
- NIL,
3405
- NIL,
3406
- NIL,
3407
- NIL,
3408
- NIL,
3409
- NIL,
3410
- [
3411
- this.parseSpecialReference(2 /* PromiseResolve */),
3412
- parsed
3413
- ],
3414
- NIL,
3415
- NIL,
3416
- NIL
3417
- )
3418
- );
3419
- }
3420
- this.popPendingState();
3421
- },
3422
- (data) => {
3423
- if (this.alive) {
3424
- const parsed = this.parseWithError(data);
3425
- if (parsed) {
3426
- this.onParse(
3427
- createSerovalNode(
3428
- 24 /* PromiseReject */,
3429
- id,
3430
- NIL,
3431
- NIL,
3432
- NIL,
3433
- NIL,
3434
- NIL,
3435
- NIL,
3436
- [
3437
- this.parseSpecialReference(3 /* PromiseReject */),
3438
- parsed
3439
- ],
3440
- NIL,
3441
- NIL,
3442
- NIL
3443
- )
3444
- );
3445
- }
3446
- }
3447
- this.popPendingState();
3448
- }
3367
+ this.handlePromiseSuccess.bind(this, resolver),
3368
+ this.handlePromiseFailure.bind(this, resolver)
3449
3369
  );
3450
3370
  this.pushPendingState();
3451
- return this.createPromiseConstructorNode(id);
3371
+ return this.createPromiseConstructorNode(id, resolver);
3452
3372
  }
3453
3373
  parsePlugin(id, current) {
3454
3374
  const currentPlugins = this.plugins;
@@ -3505,48 +3425,9 @@ var BaseStreamParserContext = class extends BaseSyncParserContext {
3505
3425
  });
3506
3426
  return result;
3507
3427
  }
3508
- handleAbortSignal(id, current) {
3509
- if (this.alive) {
3510
- const parsed = this.parseWithError(current.reason);
3511
- if (parsed) {
3512
- this.onParse(
3513
- createSerovalNode(
3514
- 36 /* AbortSignalAbort */,
3515
- id,
3516
- NIL,
3517
- NIL,
3518
- NIL,
3519
- NIL,
3520
- NIL,
3521
- NIL,
3522
- [
3523
- this.parseSpecialReference(6 /* AbortSignalAbort */),
3524
- parsed
3525
- ],
3526
- NIL,
3527
- NIL,
3528
- NIL
3529
- )
3530
- );
3531
- }
3532
- }
3533
- this.popPendingState();
3534
- }
3535
- parseAbortSignal(id, current) {
3536
- if (current.aborted) {
3537
- return this.parseAbortSignalSync(id, current);
3538
- }
3539
- this.pushPendingState();
3540
- current.addEventListener(
3541
- "abort",
3542
- this.handleAbortSignal.bind(this, id, current),
3543
- { once: true }
3544
- );
3545
- return this.createAbortSignalConstructorNode(id);
3546
- }
3547
3428
  parseWithError(current) {
3548
3429
  try {
3549
- return this.parse(current);
3430
+ return this.parseTop(current);
3550
3431
  } catch (err) {
3551
3432
  this.onError(err);
3552
3433
  return NIL;
@@ -3787,9 +3668,6 @@ var VanillaSerializerContext = class extends BaseSerializerContext {
3787
3668
  serializePromiseReject(node) {
3788
3669
  throw new SerovalUnsupportedNodeError(node);
3789
3670
  }
3790
- serializeAbortSignalAbort(node) {
3791
- throw new SerovalUnsupportedNodeError(node);
3792
- }
3793
3671
  serializeTop(tree) {
3794
3672
  const result = this.serialize(tree);
3795
3673
  if (tree.i != null && this.vars.length) {