@qredence/fleet 0.10.2 → 0.10.4

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 (95) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/runtime-manifest.json +14 -14
  3. package/dist/web/client/assets/{artifacts-panel-CfWH5cQn.js → artifacts-panel-CDuvwZXW.js} +1 -1
  4. package/dist/web/client/assets/{chat-command-palette-CKGrP-Ke.js → chat-command-palette-DSpwG3Ph.js} +1 -1
  5. package/dist/web/client/assets/{ipython-tool-bieUhw9K.js → cpython-tool-CcGMkNQ4.js} +1 -1
  6. package/dist/web/client/assets/globals-CR3boQb2.css +2 -0
  7. package/dist/web/client/assets/index-BpqhS0Xv.js +12 -0
  8. package/dist/web/client/assets/prime-agent-artifacts-CuR1D_ch.js +5 -0
  9. package/dist/web/client/assets/reasoning-line-B79r9oqP.js +1 -0
  10. package/dist/web/client/assets/{repl-panel-5xgkDXO5.js → repl-panel-Dj0nX5A6.js} +1 -1
  11. package/dist/web/client/assets/right-panel-registry-HNH3niQ8.js +2 -0
  12. package/dist/web/client/assets/routes-CJRPrnVB.js +14 -0
  13. package/dist/web/client/assets/session-activity-panel-Dvy044_T.js +1 -0
  14. package/dist/web/client/assets/{session-insights-panel-BXEmuJKf.js → session-insights-panel-Bltz_o6C.js} +1 -1
  15. package/dist/web/client/assets/{session-tool-renderer-CGMatufW.js → session-tool-renderer-BLFqj28b.js} +7 -7
  16. package/dist/web/client/assets/{subagent-chat-panel-xm-zBsFL.js → subagent-chat-panel-Cr57arv9.js} +1 -1
  17. package/dist/web/client/assets/subagent-transcript-CqAEPEJp.js +1 -0
  18. package/dist/web/client/assets/subagents-panel-DCjSDcM4.js +1 -0
  19. package/dist/web/client/assets/tool-action-icon-vTW_L1Fj.js +1 -0
  20. package/dist/web/client/assets/tool-output-normalizer-DDoLNo4P.js +4 -0
  21. package/dist/web/client/assets/tool-part-normalizer-DdgmYc9z.js +4 -0
  22. package/dist/web/client/assets/{tool-result-C_RIixge.js → tool-result-DxWDQJSF.js} +3 -3
  23. package/dist/web/client/assets/tool-timeline-BoWCZg0K.js +2 -0
  24. package/dist/web/client/bundle-budget.json +5 -5
  25. package/dist/web/server/{_tanstack-start-manifest_v-CGHXgy5L-KSOQEPW7.js → _tanstack-start-manifest_v-BhSwNUm2-HUY424SL.js} +9 -9
  26. package/dist/web/server/{agents-DOgCHixY-KUZP2VDO.js → agents-DOgCHixY-U362NTZW.js} +1 -1
  27. package/dist/web/server/{agents-page-BdiR9H8w-M6X6SXMF.js → agents-page-BdiR9H8w-WK3UCXSA.js} +7 -7
  28. package/dist/web/server/{artifacts-panel-CHPMYc6C-OWIYGSRM.js → artifacts-panel-C9OUKEU0-N74QLS6E.js} +19 -19
  29. package/dist/web/server/{chat-command-palette-VF4C0ttN-GY52QBFP.js → chat-command-palette-CnKy1Q4g-GJJGAVQ4.js} +13 -13
  30. package/dist/web/server/{chunk-GNXIDE26.js → chunk-2R5CY7DW.js} +2 -2
  31. package/dist/web/server/{chunk-W72ZBCJ5.js → chunk-37MAYG3X.js} +15 -15
  32. package/dist/web/server/{chunk-MDGXRDJM.js → chunk-3KHS4HHI.js} +13 -13
  33. package/dist/web/server/{chunk-BD7YKYWL.js → chunk-4ISLMQ2M.js} +3 -3
  34. package/dist/web/server/{chunk-FSQWWR2M.js → chunk-4ZKBYAHH.js} +1 -1
  35. package/dist/web/server/{chunk-BLGKLQJG.js → chunk-63IBRYZL.js} +6 -6
  36. package/dist/web/server/{chunk-TXZDDJCT.js → chunk-6OZY4XH7.js} +3 -3
  37. package/dist/web/server/{chunk-7DUB7HGF.js → chunk-A6LRK2MC.js} +174 -51
  38. package/dist/web/server/{chunk-PBNUO4IN.js → chunk-BG4DVGLL.js} +2 -2
  39. package/dist/web/server/{chunk-KSGGSGOA.js → chunk-BVSDXFQ6.js} +1 -1
  40. package/dist/web/server/{chunk-35RIM6VP.js → chunk-CPB76YQU.js} +1251 -740
  41. package/dist/web/server/{chunk-HAFELESQ.js → chunk-ETTLRHDY.js} +3 -3
  42. package/dist/web/server/{chunk-2ETDIMNM.js → chunk-ETVLY2KE.js} +1 -1
  43. package/dist/web/server/{chunk-BZ2EAUZZ.js → chunk-F6P2FTIT.js} +3 -3
  44. package/dist/web/server/{chunk-CE7U5QB2.js → chunk-FGOT5FGH.js} +2 -2
  45. package/dist/web/server/{chunk-GW7HYECO.js → chunk-FSIRN7F7.js} +105 -95
  46. package/dist/web/server/{chunk-R4BS3KYI.js → chunk-FVTCXLYO.js} +12 -12
  47. package/dist/web/server/{chunk-NDU4LK5M.js → chunk-IJZDCYFG.js} +15 -10
  48. package/dist/web/server/{chunk-FT7HNDR5.js → chunk-JR5TXTGV.js} +5 -5
  49. package/dist/web/server/{chunk-PQVTLG3E.js → chunk-NEMK2RTM.js} +4 -4
  50. package/dist/web/server/{chunk-AXN2OBPF.js → chunk-O5CPI2JH.js} +1 -1
  51. package/dist/web/server/{chunk-Q4ECIATK.js → chunk-ORO7KXHF.js} +4 -4
  52. package/dist/web/server/{chunk-VDEUVEWL.js → chunk-U26ZMWVI.js} +300 -300
  53. package/dist/web/server/{chunk-APOS34PW.js → chunk-U73UDYRF.js} +2 -2
  54. package/dist/web/server/{chunk-P3ZJ6L7V.js → chunk-USP4O3V7.js} +2 -2
  55. package/dist/web/server/{chunk-FNV3WZXL.js → chunk-VFW2KPLD.js} +2 -2
  56. package/dist/web/server/{chunk-5MOXRFX6.js → chunk-Y3YE73VQ.js} +45 -5
  57. package/dist/web/server/{chunk-LYJ3XDEZ.js → chunk-ZRISZYHZ.js} +14 -14
  58. package/dist/web/server/{connectors-Dv_uPdv0-2X5VWQXK.js → connectors-Dv_uPdv0-WIJGW5CQ.js} +1 -1
  59. package/dist/web/server/{connectors-page-DW75T-uH-EKRHUZ6K.js → connectors-page-DW75T-uH-3KBZRVAT.js} +10 -10
  60. package/dist/web/server/{create-project-dialog-CSZHeLNg-EEW3DRTY.js → create-project-dialog-dwXNUwY--APUSOU5G.js} +14 -14
  61. package/dist/web/server/{fork-picker-dialog-DFpKOdAw-JPLJSYYU.js → fork-picker-dialog-DFpKOdAw-WJPMXHSE.js} +6 -6
  62. package/dist/web/server/{markdown-Cc8V9dOF-JTNTJKQU.js → markdown-Cc8V9dOF-MMI3ZBVZ.js} +4 -4
  63. package/dist/web/server/{markdown-code-gaRnbgNM-V5LIRYCD.js → markdown-code-gaRnbgNM-ND6VVJ7D.js} +4 -4
  64. package/dist/web/server/{message-queue-nQ2_kU82-KKSLYR4S.js → message-queue-nQ2_kU82-3PN7CKFG.js} +4 -4
  65. package/dist/web/server/{model-selector-list-q8bMnmhy-SPFJGUBT.js → model-selector-list-q8bMnmhy-W6JM2N7G.js} +8 -8
  66. package/dist/web/server/{openui-renderer-BkDeIpwC-S5BOLH5L.js → openui-renderer-Das3EemD-AODSN4I2.js} +15 -15
  67. package/dist/web/server/{prompt-suggestions-B67Uy5Rk-5SMTSIFQ.js → prompt-suggestions-B67Uy5Rk-6MV56U6N.js} +4 -4
  68. package/dist/web/server/{repl-panel-1kSS1UGd-POXRYDDJ.js → repl-panel-DfC-BCfn-4OZEKSFX.js} +8 -8
  69. package/dist/web/server/{resources-panel-tCQCqEQv-NJO5FRHW.js → resources-panel-BL41dm3H-UWWMTOFL.js} +11 -11
  70. package/dist/web/server/{router-DBc-HYGW-CC6DTCFR.js → router-DJDog9kJ-IHEFPUCH.js} +6 -6
  71. package/dist/web/server/{routes-CcRh_tDy-M7ZRBLVA.js → routes-BWk0YodE-CEPOI27O.js} +30 -32
  72. package/dist/web/server/server.js +1415 -1004
  73. package/dist/web/server/{session-activity-panel-CS56rTyW-J4GAZSTC.js → session-activity-panel-B56ALLHr-6PDGEA2M.js} +84 -19
  74. package/dist/web/server/{session-insights-panel-BiVObm_6-AP4FITT7.js → session-insights-panel-cS12HsWt-YX7QOWIA.js} +2 -2
  75. package/dist/web/server/{session-reasoning-panel-DlqaQNE2-RMOBVYVD.js → session-reasoning-panel-CtQUqSv9-JIXRXBH7.js} +10 -10
  76. package/dist/web/server/{session-tool-renderer-CHYJwJej-Z3K5TRIZ.js → session-tool-renderer-Co466pSk-QUTYNAOE.js} +13 -13
  77. package/dist/web/server/{session-tree-panel-B2GT3Hf_-WGHVVRRR.js → session-tree-panel-B23gApxX-44NFOCFB.js} +10 -10
  78. package/dist/web/server/{settings-dialog-7V9x-CPC-6IEB2DFH.js → settings-dialog-CdRjXyfc-IYMPCNR5.js} +17 -17
  79. package/dist/web/server/{subagent-chat-panel-Cjg1oOFO-PNWSFYNA.js → subagent-chat-panel-DwvOMUnh-EPVZRTRM.js} +16 -16
  80. package/dist/web/server/{subagents-panel-DPfqOHdw-B74PSAMP.js → subagents-panel-DlFBFoX6-SAWYBVZJ.js} +32 -31
  81. package/dist/web/server/{workspace-panel-DRaU8AWD-7AYOEM55.js → workspace-panel-CxUsWmIE-3BMZFN5O.js} +11 -11
  82. package/package.json +1 -1
  83. package/dist/web/client/assets/globals-C6h6fq-s.css +0 -2
  84. package/dist/web/client/assets/index-D3OD2pV7.js +0 -13
  85. package/dist/web/client/assets/prime-agent-artifacts-C-g1VPiW.js +0 -5
  86. package/dist/web/client/assets/reasoning-line-DGdPUMwT.js +0 -1
  87. package/dist/web/client/assets/right-panel-registry-BkBAryPm.js +0 -2
  88. package/dist/web/client/assets/routes-DgbWbQOu.js +0 -14
  89. package/dist/web/client/assets/session-activity-panel-BloVPsLJ.js +0 -1
  90. package/dist/web/client/assets/subagent-transcript-BzToBVWk.js +0 -1
  91. package/dist/web/client/assets/subagents-panel-B5ooG4sS.js +0 -1
  92. package/dist/web/client/assets/tool-output-normalizer-DDU09Zc6.js +0 -4
  93. package/dist/web/client/assets/tool-part-normalizer-CJ2sY87r.js +0 -4
  94. package/dist/web/client/assets/tool-timeline-BVY5VQ3a.js +0 -2
  95. package/dist/web/client/assets/wrench-BXX_Ah2s.js +0 -1
@@ -21,13 +21,12 @@ import {
21
21
  isNotFound,
22
22
  isPromise,
23
23
  isRedirect,
24
- isStream,
25
24
  makeSerovalPlugin,
26
25
  resolveManifestAssetLink,
27
26
  resolveManifestCssLink,
28
27
  rootRouteId,
29
28
  waitForReason
30
- } from "./chunk-35RIM6VP.js";
29
+ } from "./chunk-CPB76YQU.js";
31
30
  import "./chunk-CZ7CSFO4.js";
32
31
 
33
32
  // web/app/dist/server/server.js
@@ -199,7 +198,7 @@ var RawStreamJSONDeserializePlugin = /* @__PURE__ */ createPlugin({
199
198
  }
200
199
  });
201
200
 
202
- // node_modules/.pnpm/seroval-plugins@1.6.7_seroval@1.6.7/node_modules/seroval-plugins/dist/web-CWqRG00W.js
201
+ // node_modules/.pnpm/seroval-plugins@1.7.1_seroval@1.7.1/node_modules/seroval-plugins/dist/web.js
203
202
  var READABLE_STREAM_FACTORY = {};
204
203
  var READABLE_STREAM_FACTORY_CONSTRUCTOR = (stream) => new ReadableStream({ start(controller) {
205
204
  stream.on({
@@ -243,60 +242,85 @@ var ReadableStreamFactoryPlugin = /* @__PURE__ */ createPlugin({
243
242
  return READABLE_STREAM_FACTORY;
244
243
  }
245
244
  });
246
- async function drainStream(stream, reader) {
247
- try {
248
- while (true) {
249
- const result = await reader.read();
250
- if (result.done) {
251
- stream.return(result.value);
245
+ function toAsyncIterable(value) {
246
+ return { [Symbol.asyncIterator]() {
247
+ const reader = value.getReader();
248
+ let active = true;
249
+ let reading = false;
250
+ const release = () => {
251
+ try {
252
252
  reader.releaseLock();
253
- break;
253
+ } catch {
254
254
  }
255
- stream.next(result.value);
256
- }
257
- } catch (error) {
258
- reader.releaseLock();
259
- stream.throw(error);
260
- }
261
- }
262
- function cleanupStream(reader) {
263
- reader.cancel().catch(() => {
264
- });
265
- reader.releaseLock();
266
- }
267
- function toStream(value) {
268
- const stream = createStream();
269
- const reader = value.getReader();
270
- const cleanup = cleanupStream.bind(null, reader);
271
- drainStream(stream, reader).catch(cleanup);
272
- return [stream, cleanup];
255
+ };
256
+ return {
257
+ async next() {
258
+ if (!active) return {
259
+ done: true,
260
+ value: void 0
261
+ };
262
+ reading = true;
263
+ let result;
264
+ try {
265
+ result = await reader.read();
266
+ } catch (error) {
267
+ reading = false;
268
+ release();
269
+ if (!active) return {
270
+ done: true,
271
+ value: void 0
272
+ };
273
+ active = false;
274
+ throw error;
275
+ }
276
+ reading = false;
277
+ if (!active || result.done) {
278
+ active = false;
279
+ release();
280
+ return {
281
+ done: true,
282
+ value: void 0
283
+ };
284
+ }
285
+ return result;
286
+ },
287
+ return(reason) {
288
+ if (active) {
289
+ active = false;
290
+ try {
291
+ reader.cancel(reason).catch(() => {
292
+ });
293
+ } catch {
294
+ }
295
+ if (!reading) release();
296
+ }
297
+ return Promise.resolve({
298
+ done: true,
299
+ value: void 0
300
+ });
301
+ }
302
+ };
303
+ } };
273
304
  }
274
305
  var ReadableStreamPlugin = /* @__PURE__ */ createPlugin({
275
306
  tag: "seroval/plugins/web/ReadableStream",
276
307
  extends: [ReadableStreamFactoryPlugin],
308
+ skipPlainObjects: true,
277
309
  test(value) {
278
310
  if (typeof ReadableStream === "undefined") return false;
279
311
  return value instanceof ReadableStream;
280
312
  },
281
313
  parse: {
282
- sync(_value, ctx) {
283
- return {
284
- factory: ctx.parse(READABLE_STREAM_FACTORY),
285
- stream: ctx.parse(createStream())
286
- };
287
- },
288
314
  async async(value, ctx) {
289
315
  return {
290
316
  factory: await ctx.parse(READABLE_STREAM_FACTORY),
291
- stream: await ctx.parse(toStream(value)[0])
317
+ stream: await ctx.parseStreamSource(toAsyncIterable(value))
292
318
  };
293
319
  },
294
320
  stream(value, ctx) {
295
- const [stream, cleanup] = toStream(value);
296
- ctx.addCleanup(cleanup);
297
321
  return {
298
322
  factory: ctx.parse(READABLE_STREAM_FACTORY),
299
- stream: ctx.parse(stream)
323
+ stream: ctx.parseStreamSource(toAsyncIterable(value))
300
324
  };
301
325
  }
302
326
  },
@@ -305,7 +329,6 @@ var ReadableStreamPlugin = /* @__PURE__ */ createPlugin({
305
329
  },
306
330
  deserialize(node, ctx) {
307
331
  const stream = ctx.deserialize(node.stream);
308
- if (!stream || typeof stream !== "object" || !isStream(stream)) throw new Error("Expected a stream source.");
309
332
  return READABLE_STREAM_FACTORY_CONSTRUCTOR(stream);
310
333
  }
311
334
  });
@@ -1863,7 +1886,7 @@ var renderRouterToStream = async ({ request, router, responseHeaders, children }
1863
1886
  }
1864
1887
  let rendererTeardown = false;
1865
1888
  const bot = isbot(request.headers.get("User-Agent"));
1866
- const onError2 = (renderer) => (error, info) => {
1889
+ const onError = (renderer) => (error, info) => {
1867
1890
  if (!rendererTeardown && !signal.aborted) console.error(`Error in ${renderer}:`, error, info);
1868
1891
  };
1869
1892
  try {
@@ -1872,7 +1895,7 @@ var renderRouterToStream = async ({ request, router, responseHeaders, children }
1872
1895
  signal,
1873
1896
  nonce: router.options.ssr?.nonce,
1874
1897
  progressiveChunkSize: Number.POSITIVE_INFINITY,
1875
- onError: onError2("renderToReadableStream")
1898
+ onError: onError("renderToReadableStream")
1876
1899
  });
1877
1900
  const rendererAbort = bot ? new AbortController() : void 0;
1878
1901
  const responseStream = transformReadableStreamWithRouter(router, stream, {
@@ -1911,7 +1934,7 @@ var renderRouterToStream = async ({ request, router, responseHeaders, children }
1911
1934
  nonce: router.options.ssr?.nonce,
1912
1935
  progressiveChunkSize: Number.POSITIVE_INFINITY,
1913
1936
  ...bot ? { onAllReady: resolveReady } : { onShellReady: resolveReady },
1914
- onError: onError2("renderToPipeableStream"),
1937
+ onError: onError("renderToPipeableStream"),
1915
1938
  onShellError: (error) => rendererAbort.abort(error)
1916
1939
  });
1917
1940
  const responseStream = transformReadableStreamWithRouter(router, Readable.toWeb(reactAppPassthrough), {
@@ -2380,9 +2403,9 @@ function prepareResponse(val, event, config, nested) {
2380
2403
  if (val?.stack) error.stack = val.stack;
2381
2404
  }
2382
2405
  if (error.unhandled && !config.silent) console.error(error);
2383
- const { onError: onError2 } = config;
2406
+ const { onError } = config;
2384
2407
  const errHeaders = event[kEventRes]?.[kEventResErrHeaders];
2385
- return onError2 && !nested ? Promise.resolve(onError2(error, event)).catch((error2) => error2).then((newVal) => prepareResponse(newVal ?? val, event, config, true)) : errorResponse(error, config.debug, errHeaders);
2408
+ return onError && !nested ? Promise.resolve(onError(error, event)).catch((error2) => error2).then((newVal) => prepareResponse(newVal ?? val, event, config, true)) : errorResponse(error, config.debug, errHeaders);
2386
2409
  }
2387
2410
  const preparedRes = event[kEventRes];
2388
2411
  const preparedHeaders = preparedRes?.[kEventResHeaders];
@@ -2548,7 +2571,7 @@ function getResponse() {
2548
2571
  }
2549
2572
  var HEADERS = { TSS_SHELL: "X-TSS_SHELL" };
2550
2573
  async function getStartManifest(matchedRoutes) {
2551
- const { tsrStartManifest } = await import("./_tanstack-start-manifest_v-CGHXgy5L-KSOQEPW7.js");
2574
+ const { tsrStartManifest } = await import("./_tanstack-start-manifest_v-BhSwNUm2-HUY424SL.js");
2552
2575
  const startManifest = tsrStartManifest();
2553
2576
  let routes = startManifest.routes;
2554
2577
  routes[rootRouteId];
@@ -2614,20 +2637,15 @@ var SYM_SPLIT = Symbol.split;
2614
2637
  var SYM_TO_PRIMITIVE = Symbol.toPrimitive;
2615
2638
  var SYM_TO_STRING_TAG = Symbol.toStringTag;
2616
2639
  var SYM_UNSCOPABLES = Symbol.unscopables;
2617
- var INV_SYMBOL_REF = {
2618
- [SYM_ASYNC_ITERATOR]: 0,
2619
- [SYM_HAS_INSTANCE]: 1,
2620
- [SYM_IS_CONCAT_SPREADABLE]: 2,
2621
- [SYM_ITERATOR]: 3,
2622
- [SYM_MATCH]: 4,
2623
- [SYM_MATCH_ALL]: 5,
2624
- [SYM_REPLACE]: 6,
2625
- [SYM_SEARCH]: 7,
2626
- [SYM_SPECIES]: 8,
2627
- [SYM_SPLIT]: 9,
2628
- [SYM_TO_PRIMITIVE]: 10,
2629
- [SYM_TO_STRING_TAG]: 11,
2630
- [SYM_UNSCOPABLES]: 12
2640
+ var TEMPORAL_TYPE_NAME = {
2641
+ [0]: "Instant",
2642
+ [1]: "Duration",
2643
+ [2]: "PlainDate",
2644
+ [3]: "PlainDateTime",
2645
+ [4]: "PlainMonthDay",
2646
+ [5]: "PlainTime",
2647
+ [6]: "PlainYearMonth",
2648
+ [7]: "ZonedDateTime"
2631
2649
  };
2632
2650
  var SYMBOL_REF = {
2633
2651
  [0]: SYM_ASYNC_ITERATOR,
@@ -2644,25 +2662,22 @@ var SYMBOL_REF = {
2644
2662
  [11]: SYM_TO_STRING_TAG,
2645
2663
  [12]: SYM_UNSCOPABLES
2646
2664
  };
2665
+ function createInverseSymbolTable() {
2666
+ const result = {};
2667
+ for (let i = 0; i <= 12; i++) result[SYMBOL_REF[i]] = i;
2668
+ return result;
2669
+ }
2670
+ var INV_SYMBOL_REF = /* @__PURE__ */ createInverseSymbolTable();
2647
2671
  var CONSTANT_VAL = {
2648
2672
  [2]: true,
2649
2673
  [3]: false,
2650
2674
  [1]: void 0,
2651
2675
  [0]: null,
2652
2676
  [4]: -0,
2653
- [5]: Number.POSITIVE_INFINITY,
2654
- [6]: Number.NEGATIVE_INFINITY,
2677
+ [5]: Infinity,
2678
+ [6]: -Infinity,
2655
2679
  [7]: NaN
2656
2680
  };
2657
- var ERROR_CONSTRUCTOR_STRING = {
2658
- [0]: "Error",
2659
- [1]: "EvalError",
2660
- [2]: "RangeError",
2661
- [3]: "ReferenceError",
2662
- [4]: "SyntaxError",
2663
- [5]: "TypeError",
2664
- [6]: "URIError"
2665
- };
2666
2681
  var ERROR_CONSTRUCTOR = {
2667
2682
  [0]: Error,
2668
2683
  [1]: EvalError,
@@ -2672,7 +2687,18 @@ var ERROR_CONSTRUCTOR = {
2672
2687
  [5]: TypeError,
2673
2688
  [6]: URIError
2674
2689
  };
2690
+ function createErrorConstructorStringTable() {
2691
+ const result = {};
2692
+ for (let i = 0; i <= 6; i++) result[i] = ERROR_CONSTRUCTOR[i].name;
2693
+ return result;
2694
+ }
2695
+ var ERROR_CONSTRUCTOR_STRING = /* @__PURE__ */ createErrorConstructorStringTable();
2675
2696
  function createSerovalNode(t, i, s, c, m, p, e, a, f, b, o, l) {
2697
+ if (c === void 0 && m === void 0 && p === void 0 && e === void 0 && a === void 0 && f === void 0 && b === void 0 && o === void 0 && l === void 0) return {
2698
+ t,
2699
+ i,
2700
+ s
2701
+ };
2676
2702
  return {
2677
2703
  t,
2678
2704
  i,
@@ -2689,7 +2715,7 @@ function createSerovalNode(t, i, s, c, m, p, e, a, f, b, o, l) {
2689
2715
  };
2690
2716
  }
2691
2717
  function createConstantNode(value) {
2692
- return createSerovalNode(2, void 0, value, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
2718
+ return createSerovalNode(2, void 0, value);
2693
2719
  }
2694
2720
  var TRUE_NODE = /* @__PURE__ */ createConstantNode(2);
2695
2721
  var FALSE_NODE = /* @__PURE__ */ createConstantNode(3);
@@ -2700,80 +2726,116 @@ var INFINITY_NODE = /* @__PURE__ */ createConstantNode(5);
2700
2726
  var NEG_INFINITY_NODE = /* @__PURE__ */ createConstantNode(6);
2701
2727
  var NAN_NODE = /* @__PURE__ */ createConstantNode(7);
2702
2728
  var MIN_JSON_STRINGIFY_LENGTH = 64;
2703
- var JSON_ESCAPE_DIFFERENCES = /[\x00-\x07\x0b\x0e-\x1f<\u2028\u2029\ud800-\udfff]/;
2704
- function serializeChar(str) {
2705
- switch (str) {
2706
- case '"':
2729
+ var NEEDS_ESCAPE = /["\\\n\r\b\t\f<\u2028\u2029]/;
2730
+ var JSON_ESCAPE_DIFFERENCES = /[\x00-\x07\x0b\x0e-\x1f\ud800-\udfff]/;
2731
+ var LESS_THAN = /</g;
2732
+ var LINE_SEPARATOR = /\u2028/g;
2733
+ var PARAGRAPH_SEPARATOR = /\u2029/g;
2734
+ function escapeCharCode(code) {
2735
+ switch (code) {
2736
+ case 34:
2707
2737
  return '\\"';
2708
- case "\\":
2738
+ case 92:
2709
2739
  return "\\\\";
2710
- case "\n":
2740
+ case 10:
2711
2741
  return "\\n";
2712
- case "\r":
2742
+ case 13:
2713
2743
  return "\\r";
2714
- case "\b":
2744
+ case 8:
2715
2745
  return "\\b";
2716
- case " ":
2746
+ case 9:
2717
2747
  return "\\t";
2718
- case "\f":
2748
+ case 12:
2719
2749
  return "\\f";
2720
- case "<":
2750
+ case 60:
2721
2751
  return "\\x3C";
2722
- case "\u2028":
2752
+ case 8232:
2723
2753
  return "\\u2028";
2724
- case "\u2029":
2754
+ case 8233:
2725
2755
  return "\\u2029";
2726
2756
  default:
2727
2757
  return;
2728
2758
  }
2729
2759
  }
2730
2760
  function serializeString(str) {
2731
- if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) return JSON.stringify(str).slice(1, -1);
2761
+ if (!NEEDS_ESCAPE.test(str)) return str;
2762
+ if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) {
2763
+ let result2 = JSON.stringify(str).slice(1, -1);
2764
+ if (result2.includes("<")) result2 = result2.replace(LESS_THAN, "\\x3C");
2765
+ if (result2.includes("\u2028")) result2 = result2.replace(LINE_SEPARATOR, "\\u2028");
2766
+ if (result2.includes("\u2029")) result2 = result2.replace(PARAGRAPH_SEPARATOR, "\\u2029");
2767
+ return result2;
2768
+ }
2732
2769
  let result = "";
2733
2770
  let lastPos = 0;
2734
2771
  let replacement;
2735
2772
  for (let i = 0, len = str.length; i < len; i++) {
2736
- replacement = serializeChar(str[i]);
2773
+ replacement = escapeCharCode(str.charCodeAt(i));
2737
2774
  if (replacement) {
2738
2775
  result += str.slice(lastPos, i) + replacement;
2739
2776
  lastPos = i + 1;
2740
2777
  }
2741
2778
  }
2742
- if (lastPos === 0) result = str;
2743
- else result += str.slice(lastPos);
2744
- return result;
2745
- }
2746
- function deserializeReplacer(str) {
2747
- switch (str) {
2748
- case "\\\\":
2749
- return "\\";
2750
- case '\\"':
2751
- return '"';
2752
- case "\\n":
2753
- return "\n";
2754
- case "\\r":
2755
- return "\r";
2756
- case "\\b":
2757
- return "\b";
2758
- case "\\t":
2759
- return " ";
2760
- case "\\f":
2761
- return "\f";
2762
- case "\\x3C":
2763
- return "<";
2764
- case "\\u2028":
2765
- return "\u2028";
2766
- case "\\u2029":
2767
- return "\u2029";
2768
- default:
2769
- return str;
2770
- }
2779
+ return result + str.slice(lastPos);
2771
2780
  }
2781
+ var JSON_COMPATIBLE = /^(?:[^"\\\x00-\x1f]|\\["\\nrbtf])*$/;
2782
+ var ESCAPE_SEQUENCE = /(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g;
2772
2783
  function deserializeString(str) {
2773
- if (typeof str === "string" && !str.includes("\\")) return str;
2774
- return str.replace(/(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g, deserializeReplacer);
2784
+ if (typeof str !== "string") return str.replace(ESCAPE_SEQUENCE, deserializeString);
2785
+ let index = str.indexOf("\\");
2786
+ if (index === -1) return str;
2787
+ if (JSON_COMPATIBLE.test(str)) return JSON.parse('"' + str + '"');
2788
+ let result = "";
2789
+ let lastPos = 0;
2790
+ const last = str.length - 1;
2791
+ while (index !== -1 && index < last) {
2792
+ let replacement;
2793
+ let length = 2;
2794
+ switch (str.charCodeAt(index + 1)) {
2795
+ case 92:
2796
+ replacement = "\\";
2797
+ break;
2798
+ case 34:
2799
+ replacement = '"';
2800
+ break;
2801
+ case 110:
2802
+ replacement = "\n";
2803
+ break;
2804
+ case 114:
2805
+ replacement = "\r";
2806
+ break;
2807
+ case 98:
2808
+ replacement = "\b";
2809
+ break;
2810
+ case 116:
2811
+ replacement = " ";
2812
+ break;
2813
+ case 102:
2814
+ replacement = "\f";
2815
+ break;
2816
+ case 120:
2817
+ if (str.startsWith("3C", index + 2)) {
2818
+ replacement = "<";
2819
+ length = 4;
2820
+ }
2821
+ break;
2822
+ case 117:
2823
+ if (str.startsWith("2028", index + 2)) {
2824
+ replacement = "\u2028";
2825
+ length = 6;
2826
+ } else if (str.startsWith("2029", index + 2)) {
2827
+ replacement = "\u2029";
2828
+ length = 6;
2829
+ }
2830
+ }
2831
+ if (replacement) {
2832
+ result += str.slice(lastPos, index) + replacement;
2833
+ lastPos = index + length;
2834
+ index = str.indexOf("\\", lastPos);
2835
+ } else index = str.indexOf("\\", index + 1);
2836
+ }
2837
+ return result + str.slice(lastPos);
2775
2838
  }
2776
- var { toString: objectToString } = Object.prototype;
2777
2839
  var STEP_ERROR_CODES = {
2778
2840
  parsing: 1,
2779
2841
  serialization: 2,
@@ -2782,10 +2844,9 @@ var STEP_ERROR_CODES = {
2782
2844
  function getErrorMessageProd(type) {
2783
2845
  return `Seroval Error (step: ${STEP_ERROR_CODES[type]})`;
2784
2846
  }
2785
- var getErrorMessage = (type, cause) => getErrorMessageProd(type);
2786
2847
  var SerovalError = class extends Error {
2787
2848
  constructor(type, cause) {
2788
- super(getErrorMessage(type, cause));
2849
+ super(getErrorMessageProd(type));
2789
2850
  this.cause = cause;
2790
2851
  }
2791
2852
  };
@@ -2823,12 +2884,6 @@ var SerovalMissingInstanceError = class extends Error {
2823
2884
  super(getSpecificErrorMessage(4));
2824
2885
  }
2825
2886
  };
2826
- var SerovalMissingReferenceError = class extends Error {
2827
- constructor(value) {
2828
- super(getSpecificErrorMessage(5));
2829
- this.value = value;
2830
- }
2831
- };
2832
2887
  var SerovalMissingReferenceForIdError = class extends Error {
2833
2888
  constructor(id) {
2834
2889
  super(getSpecificErrorMessage(6));
@@ -2844,92 +2899,73 @@ var SerovalMalformedNodeError = class extends Error {
2844
2899
  super(getSpecificErrorMessage(8));
2845
2900
  }
2846
2901
  };
2902
+ var SerovalConflictedNodeIdError = class extends Error {
2903
+ constructor(node) {
2904
+ super(getSpecificErrorMessage(9));
2905
+ }
2906
+ };
2847
2907
  var SerovalDepthLimitError = class extends Error {
2848
2908
  constructor(limit) {
2849
- super(getSpecificErrorMessage(9));
2909
+ super(getSpecificErrorMessage(10));
2910
+ }
2911
+ };
2912
+ var SerovalLiveStreamError = class extends Error {
2913
+ constructor(reason) {
2914
+ super(getSpecificErrorMessage(12));
2915
+ this.reason = reason;
2850
2916
  }
2851
2917
  };
2852
- var REFERENCES_KEY = "__SEROVAL_REFS__";
2853
- var REFERENCE = /* @__PURE__ */ new Map();
2854
- var INV_REFERENCE = /* @__PURE__ */ new Map();
2855
- function hasReferenceID(value) {
2856
- return REFERENCE.has(value);
2857
- }
2858
- function hasReference(id) {
2859
- return INV_REFERENCE.has(id);
2860
- }
2861
- function getReferenceID(value) {
2862
- if (hasReferenceID(value)) return REFERENCE.get(value);
2863
- throw new SerovalMissingReferenceError(value);
2864
- }
2865
- function getReference(id) {
2866
- if (hasReference(id)) return INV_REFERENCE.get(id);
2867
- throw new SerovalMissingReferenceForIdError(id);
2868
- }
2869
- if (typeof globalThis !== "undefined") Object.defineProperty(globalThis, REFERENCES_KEY, {
2870
- value: INV_REFERENCE,
2871
- configurable: true,
2872
- writable: false,
2873
- enumerable: false
2874
- });
2875
- else if (typeof window !== "undefined") Object.defineProperty(window, REFERENCES_KEY, {
2876
- value: INV_REFERENCE,
2877
- configurable: true,
2878
- writable: false,
2879
- enumerable: false
2880
- });
2881
- else if (typeof self !== "undefined") Object.defineProperty(self, REFERENCES_KEY, {
2882
- value: INV_REFERENCE,
2883
- configurable: true,
2884
- writable: false,
2885
- enumerable: false
2886
- });
2887
- else if (typeof global !== "undefined") Object.defineProperty(global, REFERENCES_KEY, {
2888
- value: INV_REFERENCE,
2889
- configurable: true,
2890
- writable: false,
2891
- enumerable: false
2892
- });
2893
2918
  function getErrorConstructor(error) {
2894
- if (error instanceof EvalError) return 1;
2895
- if (error instanceof RangeError) return 2;
2896
- if (error instanceof ReferenceError) return 3;
2897
- if (error instanceof SyntaxError) return 4;
2898
- if (error instanceof TypeError) return 5;
2899
- if (error instanceof URIError) return 6;
2919
+ for (let i = 1; i <= 6; i++) if (error instanceof ERROR_CONSTRUCTOR[i]) return i;
2900
2920
  return 0;
2901
2921
  }
2902
- function getInitialErrorOptions(error) {
2903
- const construct = ERROR_CONSTRUCTOR_STRING[getErrorConstructor(error)];
2904
- if (error.name !== construct) return { name: error.name };
2905
- if (error.constructor.name !== construct) return { name: error.constructor.name };
2906
- return {};
2907
- }
2908
2922
  function getErrorOptions(error, features) {
2909
- let options = getInitialErrorOptions(error);
2910
- const names = Object.getOwnPropertyNames(error);
2911
- for (let i = 0, len = names.length, name; i < len; i++) {
2912
- name = names[i];
2913
- if (name !== "name" && name !== "message") {
2914
- if (name === "stack") {
2915
- if (features & 4) {
2916
- options = options || {};
2917
- options[name] = error[name];
2918
- }
2919
- } else {
2920
- options = options || {};
2921
- options[name] = error[name];
2922
- }
2923
- }
2924
- }
2923
+ const options = /* @__PURE__ */ Object.create(null);
2924
+ const construct = ERROR_CONSTRUCTOR_STRING[getErrorConstructor(error)];
2925
+ if (error.name !== construct) options.name = error.name;
2926
+ else if (error.constructor.name !== construct) options.name = error.constructor.name;
2927
+ for (const name of Object.getOwnPropertyNames(error)) if (name !== "name" && name !== "message" && (name !== "stack" || features & 4)) options[name] = error[name];
2925
2928
  return options;
2926
2929
  }
2927
2930
  function getObjectFlag(obj) {
2931
+ if (Object.isExtensible(obj)) return 0;
2928
2932
  if (Object.isFrozen(obj)) return 3;
2929
2933
  if (Object.isSealed(obj)) return 2;
2930
- if (Object.isExtensible(obj)) return 0;
2931
2934
  return 1;
2932
2935
  }
2936
+ function getTypedArrayConstructor(name) {
2937
+ switch (name) {
2938
+ case "Int8Array":
2939
+ return Int8Array;
2940
+ case "Int16Array":
2941
+ return Int16Array;
2942
+ case "Int32Array":
2943
+ return Int32Array;
2944
+ case "Uint8Array":
2945
+ return Uint8Array;
2946
+ case "Uint16Array":
2947
+ return Uint16Array;
2948
+ case "Uint32Array":
2949
+ return Uint32Array;
2950
+ case "Uint8ClampedArray":
2951
+ return Uint8ClampedArray;
2952
+ case "Float32Array":
2953
+ return Float32Array;
2954
+ case "Float64Array":
2955
+ return Float64Array;
2956
+ case "BigInt64Array":
2957
+ return BigInt64Array;
2958
+ case "BigUint64Array":
2959
+ return BigUint64Array;
2960
+ default:
2961
+ throw new SerovalUnknownTypedArrayError(name);
2962
+ }
2963
+ }
2964
+ var typedArrayNameGetter;
2965
+ function getTypedArrayName(value) {
2966
+ if (!typedArrayNameGetter) typedArrayNameGetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
2967
+ return typedArrayNameGetter.call(value);
2968
+ }
2933
2969
  function createNumberNode(value) {
2934
2970
  switch (value) {
2935
2971
  case Number.POSITIVE_INFINITY:
@@ -2939,85 +2975,224 @@ function createNumberNode(value) {
2939
2975
  }
2940
2976
  if (value !== value) return NAN_NODE;
2941
2977
  if (Object.is(value, -0)) return NEG_ZERO_NODE;
2942
- return createSerovalNode(0, void 0, value, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
2978
+ return createSerovalNode(0, void 0, value);
2943
2979
  }
2944
2980
  function createStringNode(value) {
2945
- return createSerovalNode(1, void 0, serializeString(value), void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
2981
+ return createSerovalNode(1, void 0, serializeString(value));
2946
2982
  }
2947
2983
  function createBigIntNode(current) {
2948
- return createSerovalNode(3, void 0, "" + current, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
2984
+ return createSerovalNode(3, void 0, "" + current);
2949
2985
  }
2950
2986
  function createIndexedValueNode(id) {
2951
- return createSerovalNode(4, id, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
2987
+ return createSerovalNode(4, id);
2952
2988
  }
2953
2989
  function createDateNode(id, current) {
2954
2990
  const timestamp = current.valueOf();
2955
- return createSerovalNode(5, id, timestamp !== timestamp ? "" : current.toISOString(), void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
2991
+ return createSerovalNode(5, id, timestamp === timestamp ? current.toISOString() : "");
2956
2992
  }
2957
2993
  function createTemporalNode(id, type, current) {
2958
- return createSerovalNode(36, id, current.toString(), type, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
2994
+ return createSerovalNode(36, id, current.toString(), type);
2959
2995
  }
2960
2996
  function createRegExpNode(id, current) {
2961
- return createSerovalNode(6, id, void 0, serializeString(current.source), current.flags, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
2997
+ return createSerovalNode(6, id, void 0, serializeString(current.source), current.flags);
2962
2998
  }
2963
2999
  function createWKSymbolNode(id, current) {
2964
- return createSerovalNode(17, id, INV_SYMBOL_REF[current], void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
3000
+ return createSerovalNode(17, id, INV_SYMBOL_REF[current]);
2965
3001
  }
2966
- function createReferenceNode(id, ref) {
2967
- return createSerovalNode(18, id, serializeString(getReferenceID(ref)), void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
3002
+ function createReferenceNode(id, referenceId) {
3003
+ return createSerovalNode(18, id, serializeString(referenceId));
2968
3004
  }
2969
3005
  function createPluginNode(id, tag, value) {
2970
- return createSerovalNode(25, id, value, serializeString(tag), void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
3006
+ return createSerovalNode(25, id, value, serializeString(tag));
2971
3007
  }
2972
3008
  function createArrayNode(id, current, parsedItems) {
2973
- return createSerovalNode(9, id, void 0, void 0, void 0, void 0, void 0, parsedItems, void 0, void 0, getObjectFlag(current), void 0);
3009
+ return createSerovalNode(9, id, void 0, void 0, void 0, void 0, void 0, parsedItems, void 0, void 0, getObjectFlag(current));
2974
3010
  }
2975
3011
  function createBoxedNode(id, boxed) {
2976
- return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed, void 0, void 0, void 0);
3012
+ return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
2977
3013
  }
2978
3014
  var MAX_TYPED_ARRAY_LENGTH = 1e6;
2979
- function createTypedArrayNode(id, current, buffer) {
2980
- if (current.length > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
2981
- return createSerovalNode(15, id, void 0, current.constructor.name, void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, current.length);
2982
- }
2983
- function createBigIntTypedArrayNode(id, current, buffer) {
2984
- if (current.length > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
2985
- return createSerovalNode(16, id, void 0, current.constructor.name, void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, current.length);
2986
- }
2987
- function createDataViewNode(id, current, buffer) {
2988
- if (current.byteLength > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
2989
- return createSerovalNode(20, id, void 0, void 0, void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, current.byteLength);
3015
+ function createViewNode(type, id, current, buffer) {
3016
+ const isDataView = type === 20;
3017
+ const size = isDataView ? current.byteLength : current.length;
3018
+ if (size > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
3019
+ return createSerovalNode(type, id, void 0, isDataView ? void 0 : getTypedArrayName(current), void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, size);
2990
3020
  }
2991
- function createErrorNode(id, current, options) {
2992
- return createSerovalNode(13, id, getErrorConstructor(current), void 0, serializeString(current.message), options, void 0, void 0, void 0, void 0, void 0, void 0);
2993
- }
2994
- function createAggregateErrorNode(id, current, options) {
2995
- return createSerovalNode(14, id, getErrorConstructor(current), void 0, serializeString(current.message), options, void 0, void 0, void 0, void 0, void 0, void 0);
3021
+ function createErrorNode(type, id, current, options) {
3022
+ return createSerovalNode(type, id, getErrorConstructor(current), void 0, serializeString(current.message), options);
2996
3023
  }
2997
3024
  function createSetNode(id, items) {
2998
- return createSerovalNode(7, id, void 0, void 0, void 0, void 0, void 0, items, void 0, void 0, void 0, void 0);
2999
- }
3000
- function createIteratorFactoryInstanceNode(factory, items) {
3001
- return createSerovalNode(28, void 0, void 0, void 0, void 0, void 0, void 0, [factory, items], void 0, void 0, void 0, void 0);
3002
- }
3003
- function createAsyncIteratorFactoryInstanceNode(factory, items) {
3004
- return createSerovalNode(30, void 0, void 0, void 0, void 0, void 0, void 0, [factory, items], void 0, void 0, void 0, void 0);
3005
- }
3006
- function createStreamConstructorNode(id, factory, sequence) {
3007
- return createSerovalNode(31, id, void 0, void 0, void 0, void 0, void 0, sequence, factory, void 0, void 0, void 0);
3025
+ return createSerovalNode(7, id, void 0, void 0, void 0, void 0, void 0, items);
3008
3026
  }
3009
- function createStreamNextNode(id, parsed) {
3010
- return createSerovalNode(32, id, void 0, void 0, void 0, void 0, void 0, void 0, parsed, void 0, void 0, void 0);
3027
+ function createIteratorFactoryInstanceNode(type, factory, items) {
3028
+ return createSerovalNode(type, void 0, void 0, void 0, void 0, void 0, void 0, [factory, items]);
3011
3029
  }
3012
- function createStreamThrowNode(id, parsed) {
3013
- return createSerovalNode(33, id, void 0, void 0, void 0, void 0, void 0, void 0, parsed, void 0, void 0, void 0);
3030
+ function createStreamConstructorNode(id, factory, sequence, live) {
3031
+ return createSerovalNode(31, id, void 0, void 0, void 0, void 0, void 0, sequence, factory, void 0, void 0, live);
3014
3032
  }
3015
- function createStreamReturnNode(id, parsed) {
3016
- return createSerovalNode(34, id, void 0, void 0, void 0, void 0, void 0, void 0, parsed, void 0, void 0, void 0);
3033
+ function createStreamEventNode(type, id, parsed) {
3034
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, void 0, parsed);
3017
3035
  }
3018
3036
  function createSequenceNode(id, sequence, throwAt, doneAt) {
3019
3037
  return createSerovalNode(35, id, throwAt, void 0, void 0, void 0, void 0, sequence, void 0, void 0, void 0, doneAt);
3020
3038
  }
3039
+ function isLiveStream(value) {
3040
+ return value instanceof LiveStream;
3041
+ }
3042
+ var LiveStream = class {
3043
+ constructor(options) {
3044
+ this.consumed = false;
3045
+ this.closed = false;
3046
+ this.cancelled = false;
3047
+ this.options = options;
3048
+ }
3049
+ deliver(reader, record) {
3050
+ record.delivered = true;
3051
+ reader.resolve({
3052
+ event: record.event,
3053
+ accept: () => {
3054
+ if (this.pending === record && record.delivered) {
3055
+ const terminal = record.event.type !== "next";
3056
+ record.event = void 0;
3057
+ this.pending = void 0;
3058
+ const resolve = record.resolve;
3059
+ record.resolve = void 0;
3060
+ record.reject = void 0;
3061
+ if (terminal) {
3062
+ this.closed = true;
3063
+ this.options = void 0;
3064
+ const waiter = this.waiting;
3065
+ this.waiting = void 0;
3066
+ waiter?.reject(new SerovalLiveStreamError("closed"));
3067
+ }
3068
+ resolve?.();
3069
+ }
3070
+ }
3071
+ });
3072
+ }
3073
+ /** @internal */
3074
+ push(event) {
3075
+ if (this.cancelled) return Promise.reject(this.cancelReason);
3076
+ if (this.closed) return Promise.reject(new SerovalLiveStreamError("closed"));
3077
+ if (this.pending) return Promise.reject(new SerovalLiveStreamError("pending"));
3078
+ return new Promise((resolve, reject) => {
3079
+ const record = {
3080
+ event,
3081
+ delivered: false,
3082
+ resolve,
3083
+ reject
3084
+ };
3085
+ this.pending = record;
3086
+ if (this.waiting) {
3087
+ const waiter = this.waiting;
3088
+ this.waiting = void 0;
3089
+ this.deliver(waiter, record);
3090
+ }
3091
+ });
3092
+ }
3093
+ cancel(reason) {
3094
+ if (this.cancelled || this.closed) return;
3095
+ this.cancelled = true;
3096
+ this.cancelReason = reason;
3097
+ const record = this.pending;
3098
+ const waiter = this.waiting;
3099
+ this.pending = void 0;
3100
+ this.waiting = void 0;
3101
+ const callbackOwner = this.options;
3102
+ this.options = void 0;
3103
+ if (record) {
3104
+ record.event = void 0;
3105
+ const reject = record.reject;
3106
+ record.resolve = void 0;
3107
+ record.reject = void 0;
3108
+ reject?.(reason);
3109
+ }
3110
+ if (waiter) waiter.reject(reason);
3111
+ callbackOwner?.onCancel?.(reason);
3112
+ }
3113
+ read() {
3114
+ if (this.cancelled) return Promise.reject(this.cancelReason);
3115
+ if (this.closed) return Promise.reject(new SerovalLiveStreamError("closed"));
3116
+ if (this.waiting) return Promise.reject(new SerovalLiveStreamError("reading"));
3117
+ return new Promise((resolve, reject) => {
3118
+ const next = {
3119
+ resolve,
3120
+ reject
3121
+ };
3122
+ if (this.pending && !this.pending.delivered) this.deliver(next, this.pending);
3123
+ else this.waiting = next;
3124
+ });
3125
+ }
3126
+ consume() {
3127
+ if (this.consumed) throw new SerovalLiveStreamError("consumed");
3128
+ this.consumed = true;
3129
+ return {
3130
+ read: () => this.read(),
3131
+ cancel: (reason) => this.cancel(reason)
3132
+ };
3133
+ }
3134
+ /**
3135
+ * Consumes the stream through a replay-style listener, accepting each
3136
+ * event as soon as it has been delivered. Use `consume` or `pump` when
3137
+ * acceptance must wait for processing.
3138
+ */
3139
+ on(listener) {
3140
+ const wrap = (key) => (value, accept) => {
3141
+ listener[key](value);
3142
+ accept();
3143
+ };
3144
+ return this.pump({
3145
+ next: wrap("next"),
3146
+ throw: wrap("throw"),
3147
+ return: wrap("return")
3148
+ });
3149
+ }
3150
+ /**
3151
+ * Consumes the stream into a sink that controls acceptance. Returns the
3152
+ * cancel function.
3153
+ * @internal
3154
+ */
3155
+ pump(sink) {
3156
+ const consumer = this.consume();
3157
+ let current = sink;
3158
+ function next() {
3159
+ consumer.read().then((delivery) => {
3160
+ const target = current;
3161
+ if (!target) return;
3162
+ const event = delivery.event;
3163
+ let failure;
3164
+ let failed = false;
3165
+ try {
3166
+ failure = target[event.type](event.type === "throw" ? event.error : event.value, delivery.accept);
3167
+ } catch (error) {
3168
+ failed = true;
3169
+ failure = error;
3170
+ }
3171
+ if (failed || failure !== void 0) {
3172
+ current = void 0;
3173
+ try {
3174
+ consumer.cancel(failure);
3175
+ } finally {
3176
+ target.done?.();
3177
+ }
3178
+ return;
3179
+ }
3180
+ if (event.type === "next") {
3181
+ next();
3182
+ return;
3183
+ }
3184
+ current = void 0;
3185
+ target.done?.();
3186
+ }, (reason) => {
3187
+ const target = current;
3188
+ current = void 0;
3189
+ target?.error?.(reason);
3190
+ });
3191
+ }
3192
+ next();
3193
+ return consumer.cancel;
3194
+ }
3195
+ };
3021
3196
  var OpaqueReference = class {
3022
3197
  constructor(value, replacement) {
3023
3198
  this.value = value;
@@ -3036,75 +3211,6 @@ var PROMISE_CONSTRUCTOR = () => {
3036
3211
  });
3037
3212
  return resolver;
3038
3213
  };
3039
- var STREAM_CONSTRUCTOR = () => {
3040
- const buffer = [];
3041
- const listeners = [];
3042
- let alive = true;
3043
- let success = false;
3044
- let count = 0;
3045
- const internal = {
3046
- flush(value, mode, x) {
3047
- for (x = 0; x < count; x++) {
3048
- const listener = listeners[x];
3049
- if (listener) listener[mode](value);
3050
- }
3051
- },
3052
- up(listener, x, z, current) {
3053
- for (x = 0, z = buffer.length; x < z; x++) {
3054
- current = buffer[x];
3055
- if (!alive && x === z - 1) listener[success ? "return" : "throw"](current);
3056
- else listener.next(current);
3057
- }
3058
- },
3059
- on(listener, temp = 0) {
3060
- let subscribed = alive;
3061
- if (alive) {
3062
- for (temp = 0; temp < count; temp++) if (!listeners[temp]) break;
3063
- if (temp === count) count++;
3064
- listeners[temp] = listener;
3065
- }
3066
- internal.up(listener);
3067
- return () => {
3068
- if (alive && subscribed) {
3069
- subscribed = false;
3070
- listeners[temp] = void 0;
3071
- while (count > 0 && !listeners[count - 1]) count--;
3072
- listeners.length = count;
3073
- }
3074
- };
3075
- }
3076
- };
3077
- return {
3078
- __SEROVAL_STREAM__: true,
3079
- on(listener) {
3080
- return internal.on(listener);
3081
- },
3082
- next(value) {
3083
- if (alive) {
3084
- buffer.push(value);
3085
- internal.flush(value, "next");
3086
- }
3087
- },
3088
- throw(value) {
3089
- if (alive) {
3090
- buffer.push(value);
3091
- internal.flush(value, "throw");
3092
- alive = false;
3093
- success = false;
3094
- listeners.length = 0;
3095
- }
3096
- },
3097
- return(value) {
3098
- if (alive) {
3099
- buffer.push(value);
3100
- internal.flush(value, "return");
3101
- alive = false;
3102
- success = true;
3103
- listeners.length = 0;
3104
- }
3105
- }
3106
- };
3107
- };
3108
3214
  var ITERATOR_CONSTRUCTOR = (symbol) => (sequence) => () => {
3109
3215
  let index = 0;
3110
3216
  const instance = {
@@ -3210,23 +3316,25 @@ var ARRAY_BUFFER_CONSTRUCTOR = (b64) => {
3210
3316
  for (let i = 0; i < length; i++) arr[i] = decoded.charCodeAt(i);
3211
3317
  return arr.buffer;
3212
3318
  };
3319
+ var Sequence = class {
3320
+ constructor(values, throwAt, doneAt) {
3321
+ this.v = values;
3322
+ this.t = throwAt;
3323
+ this.d = doneAt;
3324
+ }
3325
+ };
3213
3326
  function isSequence(value) {
3214
- return "__SEROVAL_SEQUENCE__" in value;
3327
+ return value instanceof Sequence;
3215
3328
  }
3216
3329
  function createSequence(values, throwAt, doneAt) {
3217
- return {
3218
- __SEROVAL_SEQUENCE__: true,
3219
- v: values,
3220
- t: throwAt,
3221
- d: doneAt
3222
- };
3330
+ return new Sequence(values, throwAt, doneAt);
3223
3331
  }
3224
3332
  function createSequenceFromIterable(source) {
3225
3333
  const values = [];
3226
3334
  let throwsAt = -1;
3227
3335
  let doneAt = -1;
3228
3336
  const iterator = source[SYM_ITERATOR]();
3229
- while (true) try {
3337
+ for (; ; ) try {
3230
3338
  const value = iterator.next();
3231
3339
  values.push(value.value);
3232
3340
  if (value.done) {
@@ -3235,11 +3343,13 @@ function createSequenceFromIterable(source) {
3235
3343
  }
3236
3344
  } catch (error) {
3237
3345
  throwsAt = values.length;
3346
+ doneAt = throwsAt;
3238
3347
  values.push(error);
3348
+ break;
3239
3349
  }
3240
3350
  return createSequence(values, throwsAt, doneAt);
3241
3351
  }
3242
- var createIterator = ITERATOR_CONSTRUCTOR(SYM_ITERATOR);
3352
+ var createIterator = /* @__PURE__ */ ITERATOR_CONSTRUCTOR(SYM_ITERATOR);
3243
3353
  function sequenceToIterator(sequence) {
3244
3354
  return createIterator(sequence);
3245
3355
  }
@@ -3253,49 +3363,87 @@ var SPECIAL_REFS = {
3253
3363
  [4]: {},
3254
3364
  [5]: {}
3255
3365
  };
3256
- function isStream2(value) {
3257
- return "__SEROVAL_STREAM__" in value;
3258
- }
3259
- function createStream2() {
3260
- return STREAM_CONSTRUCTOR();
3261
- }
3262
- function createStreamFromAsyncIterable(iterable, cleanups) {
3263
- const stream = createStream2();
3264
- const iterator = iterable[SYM_ASYNC_ITERATOR]();
3265
- let cancelled = false;
3266
- let done = false;
3267
- cleanups === null || cleanups === void 0 || cleanups.push(() => {
3268
- if (!(done || cancelled)) {
3269
- cancelled = true;
3270
- Promise.resolve().then(() => {
3271
- var _iterator$return;
3272
- return (_iterator$return = iterator.return) === null || _iterator$return === void 0 ? void 0 : _iterator$return.call(iterator);
3273
- }).catch(() => {
3274
- });
3366
+ var Stream = class {
3367
+ constructor(live) {
3368
+ this.buffer = [];
3369
+ this.listeners = [];
3370
+ this.alive = true;
3371
+ this.success = false;
3372
+ this.count = 0;
3373
+ this.live = live;
3374
+ }
3375
+ flush(value, mode) {
3376
+ this.live > 1 || this.buffer.push(value);
3377
+ for (let x = 0; x < this.count; x++) this.listeners[x]?.[mode](value);
3378
+ }
3379
+ replay(listener) {
3380
+ for (let x = 0, z = this.buffer.length; x < z; x++) {
3381
+ const current = this.buffer[x];
3382
+ if (!this.alive && x === z - 1) listener[this.success ? "return" : "throw"](current);
3383
+ else listener.next(current);
3275
3384
  }
3276
- });
3277
- async function push() {
3278
- try {
3279
- while (!cancelled) {
3280
- const value = await iterator.next();
3281
- if (cancelled) return;
3282
- if (value.done) {
3283
- done = true;
3284
- stream.return(value.value);
3285
- break;
3286
- }
3287
- stream.next(value.value);
3385
+ }
3386
+ on(listener) {
3387
+ if (this.live > 1) throw new Error("Stream consumed");
3388
+ let subscribed = this.alive;
3389
+ let temp = 0;
3390
+ if (subscribed) {
3391
+ for (; temp < this.count; temp++) if (!this.listeners[temp]) break;
3392
+ if (temp === this.count) this.count++;
3393
+ this.listeners[temp] = listener;
3394
+ }
3395
+ this.replay(listener);
3396
+ if (this.live) {
3397
+ this.live = 2;
3398
+ this.buffer.length = 0;
3399
+ }
3400
+ return () => {
3401
+ if (this.alive && subscribed) {
3402
+ subscribed = false;
3403
+ this.listeners[temp] = void 0;
3404
+ while (this.count > 0 && !this.listeners[this.count - 1]) this.count--;
3405
+ this.listeners.length = this.count;
3288
3406
  }
3289
- } catch (error) {
3290
- done = true;
3291
- if (!cancelled) stream.throw(error);
3407
+ };
3408
+ }
3409
+ /**
3410
+ * Returns the events of a stream that has ended, without subscribing, so a
3411
+ * live receiver is not consumed. The last value is the `return` or `throw`
3412
+ * value. Returns `undefined` while the stream is open, or when a live
3413
+ * receiver was already consumed and has no events left.
3414
+ * @internal
3415
+ */
3416
+ endedEvents() {
3417
+ if (this.alive || this.live > 1) return void 0;
3418
+ return {
3419
+ values: this.buffer.slice(),
3420
+ success: this.success
3421
+ };
3422
+ }
3423
+ next(value) {
3424
+ if (this.alive) this.flush(value, "next");
3425
+ }
3426
+ throw(value) {
3427
+ if (this.alive) {
3428
+ this.flush(value, "throw");
3429
+ this.alive = false;
3430
+ this.success = false;
3431
+ this.listeners.length = 0;
3292
3432
  }
3293
3433
  }
3294
- push().catch(() => {
3295
- });
3296
- return stream;
3434
+ return(value) {
3435
+ if (this.alive) {
3436
+ this.flush(value, "return");
3437
+ this.alive = false;
3438
+ this.success = true;
3439
+ this.listeners.length = 0;
3440
+ }
3441
+ }
3442
+ };
3443
+ function isStream(value) {
3444
+ return value instanceof Stream || isLiveStream(value);
3297
3445
  }
3298
- var createAsyncIterable = ASYNC_ITERATOR_CONSTRUCTOR(SYM_ASYNC_ITERATOR, PROMISE_CONSTRUCTOR);
3446
+ var createAsyncIterable = /* @__PURE__ */ ASYNC_ITERATOR_CONSTRUCTOR(SYM_ASYNC_ITERATOR, PROMISE_CONSTRUCTOR);
3299
3447
  function streamToAsyncIterable(stream) {
3300
3448
  return createAsyncIterable(stream);
3301
3449
  }
@@ -3306,138 +3454,275 @@ async function promiseToResult(current) {
3306
3454
  return [0, e];
3307
3455
  }
3308
3456
  }
3309
- function createBaseParserContext(mode, options) {
3310
- var _options$compactArray;
3311
- return {
3312
- plugins: options.plugins,
3313
- mode,
3314
- marked: /* @__PURE__ */ new Set(),
3315
- features: 127 ^ (options.disabledFeatures || 0),
3316
- refs: options.refs || /* @__PURE__ */ new Map(),
3317
- depthLimit: options.depthLimit || 1e3,
3318
- compactArrayBufferViews: (_options$compactArray = options.compactArrayBufferViews) !== null && _options$compactArray !== void 0 ? _options$compactArray : false
3319
- };
3457
+ function encodeArrayBuffer$1(current) {
3458
+ const bytes = new Uint8Array(current);
3459
+ if (typeof bytes.toBase64 === "function") return bytes.toBase64();
3460
+ let result = "";
3461
+ for (let i = 0, len = bytes.length; i < len; i++) result += String.fromCharCode(bytes[i]);
3462
+ return btoa(result);
3320
3463
  }
3321
- function markParserRef(ctx, id) {
3322
- ctx.marked.add(id);
3464
+ var MIN_NATIVE_BASE64_LENGTH = 512;
3465
+ function encodeArrayBuffer(current) {
3466
+ if (typeof Buffer !== "undefined") return Buffer.from(current).toString("base64");
3467
+ return encodeArrayBuffer$1(current);
3323
3468
  }
3324
- function createIndexForValue(ctx, current) {
3325
- const id = ctx.refs.size;
3326
- ctx.refs.set(current, id);
3327
- return id;
3469
+ function decodeArrayBuffer(source) {
3470
+ if (source.length < MIN_NATIVE_BASE64_LENGTH || typeof Buffer === "undefined") return ARRAY_BUFFER_CONSTRUCTOR(source);
3471
+ const decoded = atob(source);
3472
+ const buffer = new ArrayBuffer(decoded.length);
3473
+ Buffer.from(buffer).write(decoded, "latin1");
3474
+ return buffer;
3328
3475
  }
3329
- function getNodeForIndexedValue(ctx, current) {
3330
- const registeredId = ctx.refs.get(current);
3331
- if (registeredId != null) {
3332
- markParserRef(ctx, registeredId);
3333
- return {
3334
- type: 1,
3335
- value: createIndexedValueNode(registeredId)
3336
- };
3337
- }
3338
- return {
3339
- type: 0,
3340
- value: createIndexForValue(ctx, current)
3476
+ function dedupePlugins(deduped, plugins) {
3477
+ for (let i = 0, len = plugins.length; i < len; i++) {
3478
+ const current = plugins[i];
3479
+ if (!deduped.has(current)) {
3480
+ deduped.add(current);
3481
+ if (current.extends) dedupePlugins(deduped, current.extends);
3482
+ }
3483
+ }
3484
+ }
3485
+ var RESOLVED_PLUGINS = /* @__PURE__ */ new WeakMap();
3486
+ function matchResolved(resolved, plugins, added) {
3487
+ for (let i = 0, len = plugins.length; i < len; i++) {
3488
+ const current = plugins[i];
3489
+ if (current === resolved[added]) {
3490
+ added++;
3491
+ if (current.extends) {
3492
+ added = matchResolved(resolved, current.extends, added);
3493
+ if (added === -1) return -1;
3494
+ }
3495
+ } else {
3496
+ const index = resolved.indexOf(current);
3497
+ if (index === -1 || index >= added) return -1;
3498
+ }
3499
+ }
3500
+ return added;
3501
+ }
3502
+ function resolvePlugins(plugins) {
3503
+ if (plugins) {
3504
+ const cached = RESOLVED_PLUGINS.get(plugins);
3505
+ if (cached && matchResolved(cached, plugins, 0) === cached.length) return cached;
3506
+ const deduped = /* @__PURE__ */ new Set();
3507
+ dedupePlugins(deduped, plugins);
3508
+ const resolved = [...deduped];
3509
+ RESOLVED_PLUGINS.set(plugins, resolved);
3510
+ return resolved;
3511
+ }
3512
+ }
3513
+ function getPlainObjectPlugins(plugins) {
3514
+ if (plugins) {
3515
+ for (let i = 0, len = plugins.length; i < len; i++) if (plugins[i].skipPlainObjects) {
3516
+ const result = plugins.filter((plugin) => !plugin.skipPlainObjects);
3517
+ return result.length ? result : void 0;
3518
+ }
3519
+ }
3520
+ return plugins;
3521
+ }
3522
+ var REFERENCE = /* @__PURE__ */ new Map();
3523
+ var INV_REFERENCE = /* @__PURE__ */ new Map();
3524
+ function hasReference(id) {
3525
+ return INV_REFERENCE.has(id);
3526
+ }
3527
+ function getReferenceID(value) {
3528
+ return REFERENCE.get(value);
3529
+ }
3530
+ function getReference(id) {
3531
+ if (hasReference(id)) return INV_REFERENCE.get(id);
3532
+ throw new SerovalMissingReferenceForIdError(id);
3533
+ }
3534
+ function createBaseParserContext(options) {
3535
+ return {
3536
+ plugins: options.plugins,
3537
+ plainObjectPlugins: getPlainObjectPlugins(options.plugins),
3538
+ marked: /* @__PURE__ */ new Set(),
3539
+ features: 127 ^ (options.disabledFeatures ?? 0),
3540
+ refs: options.refs ?? /* @__PURE__ */ new Map(),
3541
+ depthLimit: options.depthLimit || 1e3,
3542
+ compactArrayBufferViews: options.compactArrayBufferViews ?? false
3341
3543
  };
3342
3544
  }
3545
+ function getObjectPlugins(ctx, current, currentClass) {
3546
+ return currentClass === Object && Object.getPrototypeOf(current) === Object.prototype ? ctx.plainObjectPlugins : ctx.plugins;
3547
+ }
3548
+ function markParserRef(ctx, id) {
3549
+ ctx.marked.add(id);
3550
+ }
3551
+ function createIndexForValue(ctx, current) {
3552
+ const id = ctx.refs.size;
3553
+ ctx.refs.set(current, id);
3554
+ return id;
3555
+ }
3556
+ function getNodeForIndexedValue(ctx, current) {
3557
+ const registeredId = ctx.refs.get(current);
3558
+ if (registeredId != null) {
3559
+ markParserRef(ctx, registeredId);
3560
+ return createIndexedValueNode(registeredId);
3561
+ }
3562
+ return createIndexForValue(ctx, current);
3563
+ }
3343
3564
  function getReferenceNode(ctx, current) {
3344
3565
  const indexed = getNodeForIndexedValue(ctx, current);
3345
- if (indexed.type === 1) return indexed;
3346
- if (hasReferenceID(current)) return {
3347
- type: 2,
3348
- value: createReferenceNode(indexed.value, current)
3349
- };
3350
- return indexed;
3566
+ if (typeof indexed !== "number") return indexed;
3567
+ const referenceId = getReferenceID(current);
3568
+ return referenceId === void 0 ? indexed : createReferenceNode(indexed, referenceId);
3351
3569
  }
3352
3570
  function parseWellKnownSymbol(ctx, current) {
3353
3571
  const ref = getReferenceNode(ctx, current);
3354
- if (ref.type !== 0) return ref.value;
3355
- if (current in INV_SYMBOL_REF) return createWKSymbolNode(ref.value, current);
3572
+ if (typeof ref !== "number") return ref;
3573
+ if (current in INV_SYMBOL_REF) return createWKSymbolNode(ref, current);
3356
3574
  throw new SerovalUnsupportedTypeError(current);
3357
3575
  }
3358
3576
  function parseSpecialReference(ctx, ref) {
3359
3577
  const result = getNodeForIndexedValue(ctx, SPECIAL_REFS[ref]);
3360
- if (result.type === 1) return result.value;
3361
- return createSerovalNode(26, result.value, ref, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
3578
+ if (typeof result !== "number") return result;
3579
+ return createSerovalNode(26, result, ref);
3362
3580
  }
3363
3581
  function parseIteratorFactory(ctx) {
3364
3582
  const result = getNodeForIndexedValue(ctx, ITERATOR);
3365
- if (result.type === 1) return result.value;
3366
- return createSerovalNode(27, result.value, void 0, void 0, void 0, void 0, void 0, void 0, parseWellKnownSymbol(ctx, SYM_ITERATOR), void 0, void 0, void 0);
3583
+ if (typeof result !== "number") return result;
3584
+ return createSerovalNode(27, result, void 0, void 0, void 0, void 0, void 0, void 0, parseWellKnownSymbol(ctx, SYM_ITERATOR));
3367
3585
  }
3368
3586
  function parseAsyncIteratorFactory(ctx) {
3369
3587
  const result = getNodeForIndexedValue(ctx, ASYNC_ITERATOR);
3370
- if (result.type === 1) return result.value;
3371
- return createSerovalNode(29, result.value, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx, 1), parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR)], void 0, void 0, void 0, void 0);
3588
+ if (typeof result !== "number") return result;
3589
+ return createSerovalNode(29, result, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx, 1), parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR)]);
3590
+ }
3591
+ function getTemporalType(currentClass) {
3592
+ switch (currentClass) {
3593
+ case Temporal.Instant:
3594
+ return 0;
3595
+ case Temporal.Duration:
3596
+ return 1;
3597
+ case Temporal.PlainDate:
3598
+ return 2;
3599
+ case Temporal.PlainDateTime:
3600
+ return 3;
3601
+ case Temporal.PlainMonthDay:
3602
+ return 4;
3603
+ case Temporal.PlainTime:
3604
+ return 5;
3605
+ case Temporal.PlainYearMonth:
3606
+ return 6;
3607
+ case Temporal.ZonedDateTime:
3608
+ return 7;
3609
+ default:
3610
+ return;
3611
+ }
3612
+ }
3613
+ function getObjectClass(current) {
3614
+ const currentClass = current.constructor;
3615
+ if (currentClass !== void 0 && typeof currentClass !== "function") {
3616
+ const proto = Object.getPrototypeOf(current);
3617
+ return proto === null ? void 0 : proto.constructor;
3618
+ }
3619
+ return currentClass;
3620
+ }
3621
+ function getObjectKind(current, currentClass, features) {
3622
+ switch (currentClass) {
3623
+ case Object:
3624
+ return 1;
3625
+ case void 0:
3626
+ return 2;
3627
+ case Date:
3628
+ return 3;
3629
+ case Error:
3630
+ case EvalError:
3631
+ case RangeError:
3632
+ case ReferenceError:
3633
+ case SyntaxError:
3634
+ case TypeError:
3635
+ case URIError:
3636
+ return 4;
3637
+ case Number:
3638
+ case Boolean:
3639
+ case String:
3640
+ case BigInt:
3641
+ return 6;
3642
+ case ArrayBuffer:
3643
+ return 7;
3644
+ case Int8Array:
3645
+ case Int16Array:
3646
+ case Int32Array:
3647
+ case Uint8Array:
3648
+ case Uint16Array:
3649
+ case Uint32Array:
3650
+ case Uint8ClampedArray:
3651
+ case Float32Array:
3652
+ case Float64Array:
3653
+ return 8;
3654
+ case DataView:
3655
+ return 10;
3656
+ case Map:
3657
+ return 11;
3658
+ case Set:
3659
+ return 12;
3660
+ }
3661
+ if (currentClass === Promise || current instanceof Promise) return 13;
3662
+ if (features & 32 && currentClass === RegExp) return 14;
3663
+ if (features & 16 && (currentClass === BigInt64Array || currentClass === BigUint64Array)) return 9;
3664
+ if (features & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return 5;
3665
+ if (features & 64 && typeof Temporal !== "undefined" && getTemporalType(currentClass) != null) return 15;
3666
+ if (current instanceof Error) return 4;
3667
+ if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return 16;
3668
+ return 0;
3372
3669
  }
3373
3670
  function createObjectNode(id, current, empty, record) {
3374
- return createSerovalNode(empty ? 11 : 10, id, void 0, void 0, void 0, record, void 0, void 0, void 0, void 0, getObjectFlag(current), void 0);
3671
+ return createSerovalNode(empty ? 11 : 10, id, void 0, void 0, void 0, record, void 0, void 0, void 0, void 0, getObjectFlag(current));
3672
+ }
3673
+ function parseMapSentinel(ctx) {
3674
+ return parseSpecialReference(ctx, 0);
3375
3675
  }
3376
- function createMapNode(ctx, id, k, v) {
3676
+ function createMapNode(id, sentinel, k, v) {
3377
3677
  return createSerovalNode(8, id, void 0, void 0, void 0, void 0, {
3378
3678
  k,
3379
3679
  v
3380
- }, void 0, parseSpecialReference(ctx, 0), void 0, void 0, void 0);
3680
+ }, void 0, sentinel);
3381
3681
  }
3382
3682
  function createPromiseConstructorNode(ctx, id, resolver) {
3383
- return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1), void 0, void 0, void 0);
3683
+ return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1));
3384
3684
  }
3385
3685
  function getArrayBufferView(ctx, current) {
3386
3686
  if (!ctx.compactArrayBufferViews) return current;
3387
3687
  const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
3388
- const Constructor = current.constructor;
3389
- return new Constructor(buffer);
3390
- }
3391
- function encodeArrayBuffer(current) {
3392
- if (typeof Buffer !== "undefined") return Buffer.from(current).toString("base64");
3393
- const bytes = new Uint8Array(current);
3394
- if (typeof bytes.toBase64 === "function") return bytes.toBase64();
3395
- let result = "";
3396
- for (let i = 0, len = bytes.length; i < len; i++) result += String.fromCharCode(bytes[i]);
3397
- return btoa(result);
3688
+ return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
3398
3689
  }
3399
3690
  function createArrayBufferNode(ctx, id, current) {
3400
- return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5), void 0, void 0, void 0);
3691
+ return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5));
3401
3692
  }
3402
- function createAsyncParserContext(mode, options) {
3403
- return {
3404
- base: createBaseParserContext(mode, options),
3405
- child: void 0
3406
- };
3693
+ function createAsyncParserContext(options) {
3694
+ return { base: createBaseParserContext(options) };
3407
3695
  }
3408
- var AsyncParsePluginContext = class {
3409
- constructor(_p, depth) {
3410
- this._p = _p;
3411
- this.depth = depth;
3412
- }
3413
- parse(current) {
3414
- return parseAsync(this._p, this.depth, current);
3415
- }
3416
- };
3417
3696
  async function parseItems$1(ctx, depth, current) {
3418
- const nodes = [];
3419
- for (let i = 0, len = current.length; i < len; i++) if (i in current) nodes[i] = await parseAsync(ctx, depth, current[i]);
3420
- else nodes[i] = 0;
3697
+ const len = current.length;
3698
+ const nodes = new Array(len);
3699
+ for (let i = 0, node; i < len; i++) if (i in current) {
3700
+ node = parseValueAsync(ctx, depth, current[i]);
3701
+ nodes[i] = node instanceof Promise ? await node : node;
3702
+ } else nodes[i] = 0;
3421
3703
  return nodes;
3422
3704
  }
3423
3705
  async function parseArray$1(ctx, depth, id, current) {
3424
3706
  return createArrayNode(id, current, await parseItems$1(ctx, depth, current));
3425
3707
  }
3426
3708
  async function parseProperties$1(ctx, depth, properties) {
3427
- const entries = Object.entries(properties);
3428
- const keyNodes = [];
3429
- const valueNodes = [];
3430
- for (let i = 0, len = entries.length; i < len; i++) {
3431
- keyNodes.push(serializeString(entries[i][0]));
3432
- valueNodes.push(await parseAsync(ctx, depth, entries[i][1]));
3709
+ const keys = Object.keys(properties);
3710
+ const len = keys.length;
3711
+ const keyNodes = new Array(len);
3712
+ const valueNodes = new Array(len);
3713
+ for (let i = 0, key, node; i < len; i++) {
3714
+ key = keys[i];
3715
+ keyNodes[i] = serializeString(key);
3716
+ node = parseValueAsync(ctx, depth, properties[key]);
3717
+ valueNodes[i] = node instanceof Promise ? await node : node;
3433
3718
  }
3434
3719
  if (SYM_ITERATOR in properties) {
3435
3720
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
3436
- valueNodes.push(createIteratorFactoryInstanceNode(parseIteratorFactory(ctx.base), await parseAsync(ctx, depth, createSequenceFromIterable(properties))));
3721
+ valueNodes.push(createIteratorFactoryInstanceNode(28, parseIteratorFactory(ctx.base), await parseAsync(ctx, depth, createSequenceFromIterable(properties))));
3437
3722
  }
3438
3723
  if (SYM_ASYNC_ITERATOR in properties) {
3439
3724
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ASYNC_ITERATOR));
3440
- valueNodes.push(createAsyncIteratorFactoryInstanceNode(parseAsyncIteratorFactory(ctx.base), await parseAsync(ctx, depth, createStreamFromAsyncIterable(properties))));
3725
+ valueNodes.push(createIteratorFactoryInstanceNode(30, parseAsyncIteratorFactory(ctx.base), await parseAsyncIterable$1(ctx, depth, properties)));
3441
3726
  }
3442
3727
  if (SYM_TO_STRING_TAG in properties) {
3443
3728
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_TO_STRING_TAG));
@@ -3458,185 +3743,193 @@ async function parsePlainObject$1(ctx, depth, id, current, empty) {
3458
3743
  async function parseBoxed$1(ctx, depth, id, current) {
3459
3744
  return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
3460
3745
  }
3461
- async function parseTypedArray$1(ctx, depth, id, current) {
3746
+ async function parseView$1(ctx, depth, type, id, current) {
3462
3747
  current = getArrayBufferView(ctx.base, current);
3463
- return createTypedArrayNode(id, current, await parseAsync(ctx, depth, current.buffer));
3748
+ return createViewNode(type, id, current, await parseAsync(ctx, depth, current.buffer));
3464
3749
  }
3465
- async function parseBigIntTypedArray$1(ctx, depth, id, current) {
3466
- current = getArrayBufferView(ctx.base, current);
3467
- return createBigIntTypedArrayNode(id, current, await parseAsync(ctx, depth, current.buffer));
3468
- }
3469
- async function parseDataView$1(ctx, depth, id, current) {
3470
- current = getArrayBufferView(ctx.base, current);
3471
- return createDataViewNode(id, current, await parseAsync(ctx, depth, current.buffer));
3472
- }
3473
- async function parseError$1(ctx, depth, id, current) {
3474
- const options = getErrorOptions(current, ctx.base.features);
3475
- return createErrorNode(id, current, options ? await parseProperties$1(ctx, depth, options) : void 0);
3476
- }
3477
- async function parseAggregateError$1(ctx, depth, id, current) {
3750
+ async function parseError$1(ctx, depth, type, id, current) {
3478
3751
  const options = getErrorOptions(current, ctx.base.features);
3479
- return createAggregateErrorNode(id, current, options ? await parseProperties$1(ctx, depth, options) : void 0);
3752
+ return createErrorNode(type, id, current, options ? await parseProperties$1(ctx, depth, options) : void 0);
3480
3753
  }
3481
3754
  async function parseMap$1(ctx, depth, id, current) {
3482
- const keyNodes = [];
3483
- const valueNodes = [];
3755
+ const sentinel = parseMapSentinel(ctx.base);
3756
+ const keyNodes = new Array(current.size);
3757
+ const valueNodes = new Array(current.size);
3758
+ let node;
3759
+ let i = 0;
3484
3760
  for (const [key, value] of current.entries()) {
3485
- keyNodes.push(await parseAsync(ctx, depth, key));
3486
- valueNodes.push(await parseAsync(ctx, depth, value));
3761
+ node = parseValueAsync(ctx, depth, key);
3762
+ keyNodes[i] = node instanceof Promise ? await node : node;
3763
+ node = parseValueAsync(ctx, depth, value);
3764
+ valueNodes[i] = node instanceof Promise ? await node : node;
3765
+ i++;
3487
3766
  }
3488
- return createMapNode(ctx.base, id, keyNodes, valueNodes);
3767
+ return createMapNode(id, sentinel, keyNodes, valueNodes);
3489
3768
  }
3490
3769
  async function parseSet$1(ctx, depth, id, current) {
3491
- const items = [];
3492
- for (const item of current.keys()) items.push(await parseAsync(ctx, depth, item));
3770
+ const items = new Array(current.size);
3771
+ let node;
3772
+ let i = 0;
3773
+ for (const item of current.keys()) {
3774
+ node = parseValueAsync(ctx, depth, item);
3775
+ items[i++] = node instanceof Promise ? await node : node;
3776
+ }
3493
3777
  return createSetNode(id, items);
3494
3778
  }
3495
- async function parsePlugin$1(ctx, depth, id, current) {
3496
- const currentPlugins = ctx.base.plugins;
3779
+ async function parsePlugin$1(ctx, depth, id, current, currentPlugins) {
3497
3780
  if (currentPlugins) for (let i = 0, len = currentPlugins.length; i < len; i++) {
3498
3781
  const plugin = currentPlugins[i];
3499
- if (plugin.parse.async && plugin.test(current)) return createPluginNode(id, plugin.tag, await plugin.parse.async(current, new AsyncParsePluginContext(ctx, depth), { id }));
3782
+ if (plugin.parse.async && plugin.test(current)) return createPluginNode(id, plugin.tag, await plugin.parse.async(current, {
3783
+ parse: (value) => parseAsync(ctx, depth, value),
3784
+ parseStreamSource(source) {
3785
+ if (depth >= ctx.base.depthLimit) throw new SerovalDepthLimitError(ctx.base.depthLimit);
3786
+ return parseAsyncIterable$1(ctx, depth + 1, source);
3787
+ }
3788
+ }, { id }));
3500
3789
  }
3501
3790
  }
3502
3791
  async function parsePromise$1(ctx, depth, id, current) {
3503
3792
  const [status, result] = await promiseToResult(current);
3504
- return createSerovalNode(12, id, status, void 0, void 0, void 0, void 0, void 0, await parseAsync(ctx, depth, result), void 0, void 0, void 0);
3793
+ return createSerovalNode(12, id, status, void 0, void 0, void 0, void 0, void 0, await parseAsync(ctx, depth, result));
3505
3794
  }
3506
3795
  function parseStreamHandle(depth, id, current, resolve, reject) {
3507
3796
  const sequence = [];
3508
- const cleanup = current.on({
3509
- next: (value) => {
3510
- markParserRef(this.base, id);
3511
- parseAsync(this, depth, value).then((data) => {
3512
- sequence.push(createStreamNextNode(id, data));
3513
- }, (data) => {
3514
- reject(data);
3515
- cleanup();
3516
- });
3517
- },
3518
- throw: (value) => {
3519
- markParserRef(this.base, id);
3520
- parseAsync(this, depth, value).then((data) => {
3521
- sequence.push(createStreamThrowNode(id, data));
3522
- resolve(sequence);
3523
- cleanup();
3524
- }, (data) => {
3525
- reject(data);
3526
- cleanup();
3527
- });
3528
- },
3529
- return: (value) => {
3530
- markParserRef(this.base, id);
3531
- parseAsync(this, depth, value).then((data) => {
3532
- sequence.push(createStreamReturnNode(id, data));
3533
- resolve(sequence);
3534
- cleanup();
3535
- }, (data) => {
3536
- reject(data);
3537
- cleanup();
3538
- });
3797
+ let unparsed = 0;
3798
+ let ended = false;
3799
+ let settled = false;
3800
+ const fail = (error) => {
3801
+ if (!settled) {
3802
+ settled = true;
3803
+ try {
3804
+ cleanup(error);
3805
+ } catch {
3806
+ }
3807
+ reject(error);
3539
3808
  }
3540
- });
3809
+ };
3810
+ const handle = (type) => (value, accept) => {
3811
+ if (settled) return;
3812
+ markParserRef(this.base, id);
3813
+ const slot = sequence.push(void 0) - 1;
3814
+ unparsed++;
3815
+ if (type !== 32) ended = true;
3816
+ parseAsync(this, depth, value).then((data) => {
3817
+ if (!settled) {
3818
+ sequence[slot] = createStreamEventNode(type, id, data);
3819
+ unparsed--;
3820
+ accept?.();
3821
+ if (ended && unparsed === 0) {
3822
+ settled = true;
3823
+ try {
3824
+ cleanup();
3825
+ } catch (error) {
3826
+ reject(error);
3827
+ return;
3828
+ }
3829
+ resolve(sequence);
3830
+ }
3831
+ }
3832
+ }, fail);
3833
+ };
3834
+ const listener = {
3835
+ next: handle(32),
3836
+ throw: handle(33),
3837
+ return: handle(34)
3838
+ };
3839
+ const cleanup = isLiveStream(current) ? current.pump(listener) : current.on(listener);
3541
3840
  }
3542
3841
  async function parseStream$1(ctx, depth, id, current) {
3543
3842
  return createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), await new Promise(parseStreamHandle.bind(ctx, depth, id, current)));
3544
3843
  }
3844
+ async function parseAsyncIterable$1(ctx, depth, current) {
3845
+ const id = createIndexForValue(ctx.base, {});
3846
+ const reference = parseSpecialReference(ctx.base, 4);
3847
+ const iterator = current[SYM_ASYNC_ITERATOR]();
3848
+ const sequence = [];
3849
+ for (; ; ) {
3850
+ let result;
3851
+ try {
3852
+ result = await iterator.next();
3853
+ } catch (error) {
3854
+ markParserRef(ctx.base, id);
3855
+ sequence.push(createStreamEventNode(33, id, await parseAsync(ctx, depth, error)));
3856
+ break;
3857
+ }
3858
+ markParserRef(ctx.base, id);
3859
+ let parsed;
3860
+ try {
3861
+ parsed = await parseAsync(ctx, depth, result.value);
3862
+ } catch (error) {
3863
+ Promise.resolve().then(() => iterator.return?.()).catch(() => {
3864
+ });
3865
+ throw error;
3866
+ }
3867
+ sequence.push(createStreamEventNode(result.done ? 34 : 32, id, parsed));
3868
+ if (result.done) break;
3869
+ }
3870
+ return createStreamConstructorNode(id, reference, sequence);
3871
+ }
3545
3872
  async function parseSequence$1(ctx, depth, id, current) {
3546
- const nodes = [];
3547
- for (let i = 0, len = current.v.length; i < len; i++) nodes[i] = await parseAsync(ctx, depth, current.v[i]);
3873
+ const len = current.v.length;
3874
+ const nodes = new Array(len);
3875
+ for (let i = 0, node; i < len; i++) {
3876
+ node = parseValueAsync(ctx, depth, current.v[i]);
3877
+ nodes[i] = node instanceof Promise ? await node : node;
3878
+ }
3548
3879
  return createSequenceNode(id, nodes, current.t, current.d);
3549
3880
  }
3550
3881
  async function parseObjectAsync(ctx, depth, id, current) {
3551
3882
  if (Array.isArray(current)) return parseArray$1(ctx, depth, id, current);
3552
- if (isStream2(current)) return parseStream$1(ctx, depth, id, current);
3883
+ if (isStream(current)) return parseStream$1(ctx, depth, id, current);
3553
3884
  if (isSequence(current)) return parseSequence$1(ctx, depth, id, current);
3554
- let currentClass = current.constructor;
3555
- if (currentClass !== void 0 && typeof currentClass !== "function") {
3556
- const proto = Object.getPrototypeOf(current);
3557
- currentClass = proto === null ? void 0 : proto.constructor;
3558
- }
3885
+ const currentClass = getObjectClass(current);
3559
3886
  if (currentClass === OpaqueReference) return parseAsync(ctx, depth, current.replacement);
3560
- const parsed = await parsePlugin$1(ctx, depth, id, current);
3887
+ const parsed = await parsePlugin$1(ctx, depth, id, current, getObjectPlugins(ctx.base, current, currentClass));
3561
3888
  if (parsed) return parsed;
3562
- switch (currentClass) {
3563
- case Object:
3564
- return parsePlainObject$1(ctx, depth, id, current, false);
3565
- case void 0:
3889
+ if (currentClass === Object) return parsePlainObject$1(ctx, depth, id, current, false);
3890
+ switch (getObjectKind(current, currentClass, ctx.base.features)) {
3891
+ case 2:
3566
3892
  return parsePlainObject$1(ctx, depth, id, current, true);
3567
- case Date:
3893
+ case 3:
3568
3894
  return createDateNode(id, current);
3569
- case Error:
3570
- case EvalError:
3571
- case RangeError:
3572
- case ReferenceError:
3573
- case SyntaxError:
3574
- case TypeError:
3575
- case URIError:
3576
- return parseError$1(ctx, depth, id, current);
3577
- case Number:
3578
- case Boolean:
3579
- case String:
3580
- case BigInt:
3895
+ case 4:
3896
+ return parseError$1(ctx, depth, 13, id, current);
3897
+ case 5:
3898
+ return parseError$1(ctx, depth, 14, id, current);
3899
+ case 6:
3581
3900
  return parseBoxed$1(ctx, depth, id, current);
3582
- case ArrayBuffer:
3901
+ case 7:
3583
3902
  return createArrayBufferNode(ctx.base, id, current);
3584
- case Int8Array:
3585
- case Int16Array:
3586
- case Int32Array:
3587
- case Uint8Array:
3588
- case Uint16Array:
3589
- case Uint32Array:
3590
- case Uint8ClampedArray:
3591
- case Float32Array:
3592
- case Float64Array:
3593
- return parseTypedArray$1(ctx, depth, id, current);
3594
- case DataView:
3595
- return parseDataView$1(ctx, depth, id, current);
3596
- case Map:
3903
+ case 8:
3904
+ return parseView$1(ctx, depth, 15, id, current);
3905
+ case 9:
3906
+ return parseView$1(ctx, depth, 16, id, current);
3907
+ case 10:
3908
+ return parseView$1(ctx, depth, 20, id, current);
3909
+ case 11:
3597
3910
  return parseMap$1(ctx, depth, id, current);
3598
- case Set:
3911
+ case 12:
3599
3912
  return parseSet$1(ctx, depth, id, current);
3913
+ case 13:
3914
+ return parsePromise$1(ctx, depth, id, current);
3915
+ case 14:
3916
+ return createRegExpNode(id, current);
3917
+ case 15:
3918
+ return createTemporalNode(id, getTemporalType(currentClass), current);
3919
+ case 16:
3920
+ return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
3921
+ default:
3922
+ throw new SerovalUnsupportedTypeError(current);
3600
3923
  }
3601
- if (currentClass === Promise || current instanceof Promise) return parsePromise$1(ctx, depth, id, current);
3602
- const currentFeatures = ctx.base.features;
3603
- if (currentFeatures & 32 && currentClass === RegExp) return createRegExpNode(id, current);
3604
- if (currentFeatures & 16) switch (currentClass) {
3605
- case BigInt64Array:
3606
- case BigUint64Array:
3607
- return parseBigIntTypedArray$1(ctx, depth, id, current);
3608
- }
3609
- if (currentFeatures & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return parseAggregateError$1(ctx, depth, id, current);
3610
- if (currentFeatures & 64 && typeof Temporal !== "undefined") switch (currentClass) {
3611
- case Temporal.Instant:
3612
- return createTemporalNode(id, 0, current);
3613
- case Temporal.Duration:
3614
- return createTemporalNode(id, 1, current);
3615
- case Temporal.PlainDate:
3616
- return createTemporalNode(id, 2, current);
3617
- case Temporal.PlainDateTime:
3618
- return createTemporalNode(id, 3, current);
3619
- case Temporal.PlainMonthDay:
3620
- return createTemporalNode(id, 4, current);
3621
- case Temporal.PlainTime:
3622
- return createTemporalNode(id, 5, current);
3623
- case Temporal.PlainYearMonth:
3624
- return createTemporalNode(id, 6, current);
3625
- case Temporal.ZonedDateTime:
3626
- return createTemporalNode(id, 7, current);
3627
- }
3628
- if (current instanceof Error) return parseError$1(ctx, depth, id, current);
3629
- if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
3630
- throw new SerovalUnsupportedTypeError(current);
3631
3924
  }
3632
3925
  async function parseFunctionAsync(ctx, depth, current) {
3633
3926
  const ref = getReferenceNode(ctx.base, current);
3634
- if (ref.type !== 0) return ref.value;
3635
- const plugin = await parsePlugin$1(ctx, depth, ref.value, current);
3927
+ if (typeof ref !== "number") return ref;
3928
+ const plugin = await parsePlugin$1(ctx, depth, ref, current, ctx.base.plugins);
3636
3929
  if (plugin) return plugin;
3637
3930
  throw new SerovalUnsupportedTypeError(current);
3638
3931
  }
3639
- async function parseAsync(ctx, depth, current) {
3932
+ function parseValueAsync(ctx, depth, current) {
3640
3933
  if (depth >= ctx.base.depthLimit) throw new SerovalDepthLimitError(ctx.base.depthLimit);
3641
3934
  switch (typeof current) {
3642
3935
  case "boolean":
@@ -3652,7 +3945,7 @@ async function parseAsync(ctx, depth, current) {
3652
3945
  case "object":
3653
3946
  if (current) {
3654
3947
  const ref = getReferenceNode(ctx.base, current);
3655
- return ref.type === 0 ? await parseObjectAsync(ctx, depth + 1, ref.value, current) : ref.value;
3948
+ return typeof ref === "number" ? parseObjectAsync(ctx, depth + 1, ref, current) : ref;
3656
3949
  }
3657
3950
  return NULL_NODE;
3658
3951
  case "symbol":
@@ -3663,6 +3956,9 @@ async function parseAsync(ctx, depth, current) {
3663
3956
  throw new SerovalUnsupportedTypeError(current);
3664
3957
  }
3665
3958
  }
3959
+ async function parseAsync(ctx, depth, current) {
3960
+ return await parseValueAsync(ctx, depth, current);
3961
+ }
3666
3962
  async function parseTopAsync(ctx, current) {
3667
3963
  try {
3668
3964
  return await parseAsync(ctx, 0, current);
@@ -3670,50 +3966,6 @@ async function parseTopAsync(ctx, current) {
3670
3966
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3671
3967
  }
3672
3968
  }
3673
- function dedupePlugins(deduped, plugins) {
3674
- for (let i = 0, len = plugins.length; i < len; i++) {
3675
- const current = plugins[i];
3676
- if (!deduped.has(current)) {
3677
- deduped.add(current);
3678
- if (current.extends) dedupePlugins(deduped, current.extends);
3679
- }
3680
- }
3681
- }
3682
- function resolvePlugins(plugins) {
3683
- if (plugins) {
3684
- const deduped = /* @__PURE__ */ new Set();
3685
- dedupePlugins(deduped, plugins);
3686
- return [...deduped];
3687
- }
3688
- }
3689
- function getTypedArrayConstructor(name) {
3690
- switch (name) {
3691
- case "Int8Array":
3692
- return Int8Array;
3693
- case "Int16Array":
3694
- return Int16Array;
3695
- case "Int32Array":
3696
- return Int32Array;
3697
- case "Uint8Array":
3698
- return Uint8Array;
3699
- case "Uint16Array":
3700
- return Uint16Array;
3701
- case "Uint32Array":
3702
- return Uint32Array;
3703
- case "Uint8ClampedArray":
3704
- return Uint8ClampedArray;
3705
- case "Float32Array":
3706
- return Float32Array;
3707
- case "Float64Array":
3708
- return Float64Array;
3709
- case "BigInt64Array":
3710
- return BigInt64Array;
3711
- case "BigUint64Array":
3712
- return BigUint64Array;
3713
- default:
3714
- throw new SerovalUnknownTypedArrayError(name);
3715
- }
3716
- }
3717
3969
  function isValidKey(key) {
3718
3970
  switch (key) {
3719
3971
  case "constructor":
@@ -3740,7 +3992,6 @@ function isValidSymbol(symbol) {
3740
3992
  }
3741
3993
  }
3742
3994
  var DEFAULT_MAX_BASE64_LENGTH = 1e6;
3743
- var MIN_NATIVE_BASE64_LENGTH = 512;
3744
3995
  var MAX_BIGINT_LENGTH = 1e4;
3745
3996
  var MAX_REGEXP_SOURCE_LENGTH = 2e4;
3746
3997
  function applyObjectFlag(obj, flag) {
@@ -3756,61 +4007,45 @@ function applyObjectFlag(obj, flag) {
3756
4007
  }
3757
4008
  }
3758
4009
  var DEFAULT_DEPTH_LIMIT = 1e3;
3759
- function createBaseDeserializerContext(mode, options) {
3760
- var _options$maxBase64Len, _options$features;
3761
- const maxBase64Length = (_options$maxBase64Len = options.maxBase64Length) !== null && _options$maxBase64Len !== void 0 ? _options$maxBase64Len : DEFAULT_MAX_BASE64_LENGTH;
4010
+ function createBaseDeserializerContext(options) {
4011
+ const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
3762
4012
  if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
3763
- const refs = options.refs || /* @__PURE__ */ new Map();
4013
+ const refs = options.refs ?? /* @__PURE__ */ new Map();
3764
4014
  if (!("types" in refs)) Object.assign(refs, { types: /* @__PURE__ */ new Map() });
3765
4015
  return {
3766
- mode,
3767
4016
  plugins: options.plugins,
3768
4017
  refs,
3769
- features: (_options$features = options.features) !== null && _options$features !== void 0 ? _options$features : 127 ^ (options.disabledFeatures || 0),
4018
+ features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
3770
4019
  depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
3771
4020
  maxBase64Length
3772
4021
  };
3773
4022
  }
3774
4023
  function createVanillaDeserializerContext(options) {
3775
4024
  return {
3776
- mode: 1,
3777
- base: createBaseDeserializerContext(1, options),
3778
- child: void 0,
3779
- state: { marked: new Set(options.markedRefs) }
4025
+ base: createBaseDeserializerContext(options),
4026
+ marked: new Set(options.markedRefs)
3780
4027
  };
3781
4028
  }
3782
- var DeserializePluginContext = class {
3783
- constructor(_p, depth) {
3784
- this._p = _p;
3785
- this.depth = depth;
3786
- }
3787
- deserialize(node) {
3788
- return deserialize$1(this._p, this.depth, node);
3789
- }
3790
- };
3791
4029
  function guardIndexedValue(ctx, id) {
3792
- if (id < 0 || !Number.isFinite(id) || !Number.isInteger(id)) throw new SerovalMalformedNodeError({
4030
+ const node = {
3793
4031
  t: 4,
3794
4032
  i: id
3795
- });
3796
- if (ctx.refs.has(id)) throw new Error("Conflicted ref id: " + id);
4033
+ };
4034
+ if (id < 0 || !Number.isInteger(id)) throw new SerovalMalformedNodeError(node);
4035
+ if (ctx.refs.has(id)) throw new SerovalConflictedNodeIdError(node);
3797
4036
  }
3798
4037
  function isThennable(value) {
3799
- return !!value && typeof value === "object" && "then" in value && typeof value.then === "function";
4038
+ return !!value && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
3800
4039
  }
3801
- function assignIndexedValueVanilla(ctx, id, value) {
3802
- guardIndexedValue(ctx.base, id);
3803
- if (ctx.state.marked.has(id)) ctx.base.refs.set(id, value);
3804
- return value;
3805
- }
3806
- function assignIndexedValueCross(ctx, id, value) {
4040
+ var ignoreRejection = () => {
4041
+ };
4042
+ function assignIndexedValue$1(ctx, id, value) {
3807
4043
  guardIndexedValue(ctx.base, id);
3808
- ctx.base.refs.set(id, value);
4044
+ if (ctx.base.pending && value instanceof Promise) value.catch(ignoreRejection);
4045
+ const marked = ctx.marked;
4046
+ if (!marked || marked.has(id)) ctx.base.refs.set(id, value);
3809
4047
  return value;
3810
4048
  }
3811
- function assignIndexedValue$1(ctx, id, value) {
3812
- return ctx.mode === 1 ? assignIndexedValueVanilla(ctx, id, value) : assignIndexedValueCross(ctx, id, value);
3813
- }
3814
4049
  function deserializeKnownValue(node, record, key) {
3815
4050
  if (Object.hasOwn(record, key)) return record[key];
3816
4051
  throw new SerovalMalformedNodeError(node);
@@ -3818,8 +4053,12 @@ function deserializeKnownValue(node, record, key) {
3818
4053
  function deserializeReference(ctx, node) {
3819
4054
  return assignIndexedValue$1(ctx, node.i, getReference(deserializeString(node.s)));
3820
4055
  }
4056
+ function validateNodeList(node, list) {
4057
+ if (!Array.isArray(list)) throw new SerovalMalformedNodeError(node);
4058
+ }
3821
4059
  function deserializeArray(ctx, depth, node) {
3822
4060
  const items = node.a;
4061
+ validateNodeList(node, items);
3823
4062
  const len = items.length;
3824
4063
  const result = assignIndexedValue$1(ctx, node.i, new Array(len));
3825
4064
  for (let i = 0, item; i < len; i++) {
@@ -3862,7 +4101,10 @@ function validateNodeType(ctx, node, id, type) {
3862
4101
  }
3863
4102
  function deserializeProperties(ctx, depth, node, result) {
3864
4103
  const keys = node.k;
3865
- if (keys.length > 0) for (let i = 0, vals = node.v, len = keys.length; i < len; i++) assignProperty(ctx, depth, result, keys[i], vals[i]);
4104
+ validateNodeList(node, keys);
4105
+ validateNodeList(node, node.v);
4106
+ const len = keys.length;
4107
+ if (len > 0) for (let i = 0, vals = node.v; i < len; i++) assignProperty(ctx, depth, result, keys[i], vals[i]);
3866
4108
  return result;
3867
4109
  }
3868
4110
  function deserializeObject(ctx, depth, node) {
@@ -3876,36 +4118,8 @@ function deserializeDate(ctx, node) {
3876
4118
  }
3877
4119
  function deserializeTemporal(ctx, node) {
3878
4120
  if (!(ctx.base.features & 64)) throw new SerovalUnsupportedNodeError(node);
3879
- let value;
3880
- switch (node.c) {
3881
- case 0:
3882
- value = Temporal.Instant.from(node.s);
3883
- break;
3884
- case 1:
3885
- value = Temporal.Duration.from(node.s);
3886
- break;
3887
- case 2:
3888
- value = Temporal.PlainDate.from(node.s);
3889
- break;
3890
- case 3:
3891
- value = Temporal.PlainDateTime.from(node.s);
3892
- break;
3893
- case 4:
3894
- value = Temporal.PlainMonthDay.from(node.s);
3895
- break;
3896
- case 5:
3897
- value = Temporal.PlainTime.from(node.s);
3898
- break;
3899
- case 6:
3900
- value = Temporal.PlainYearMonth.from(node.s);
3901
- break;
3902
- case 7:
3903
- value = Temporal.ZonedDateTime.from(node.s);
3904
- break;
3905
- default:
3906
- throw new SerovalMalformedNodeError(node);
3907
- }
3908
- return assignIndexedValue$1(ctx, node.i, value);
4121
+ const construct = Temporal[deserializeKnownValue(node, TEMPORAL_TYPE_NAME, node.c)];
4122
+ return assignIndexedValue$1(ctx, node.i, construct.from(node.s));
3909
4123
  }
3910
4124
  function deserializeRegExp(ctx, node) {
3911
4125
  if (ctx.base.features & 32) {
@@ -3917,43 +4131,29 @@ function deserializeRegExp(ctx, node) {
3917
4131
  }
3918
4132
  function deserializeSet(ctx, depth, node) {
3919
4133
  const result = assignIndexedValue$1(ctx, node.i, /* @__PURE__ */ new Set());
4134
+ validateNodeList(node, node.a);
3920
4135
  for (let i = 0, items = node.a, len = items.length; i < len; i++) result.add(deserialize$1(ctx, depth, items[i]));
3921
4136
  return result;
3922
4137
  }
3923
4138
  function deserializeMap(ctx, depth, node) {
3924
4139
  const result = assignIndexedValue$1(ctx, node.i, /* @__PURE__ */ new Map());
4140
+ validateNodeList(node, node.e.k);
4141
+ validateNodeList(node, node.e.v);
3925
4142
  for (let i = 0, keys = node.e.k, vals = node.e.v, len = keys.length; i < len; i++) result.set(deserialize$1(ctx, depth, keys[i]), deserialize$1(ctx, depth, vals[i]));
3926
4143
  return result;
3927
4144
  }
3928
4145
  function deserializeArrayBuffer(ctx, node) {
3929
4146
  if (typeof node.s !== "string") throw new SerovalMalformedNodeError(node);
3930
4147
  if (node.s.length > ctx.base.maxBase64Length) throw new RangeError("ArrayBuffer exceeds maxBase64Length (" + ctx.base.maxBase64Length + ")");
3931
- const source = deserializeString(node.s);
3932
- let buffer;
3933
- if (source.length < MIN_NATIVE_BASE64_LENGTH || typeof Buffer === "undefined") buffer = ARRAY_BUFFER_CONSTRUCTOR(source);
3934
- else {
3935
- const decoded = atob(source);
3936
- buffer = new ArrayBuffer(decoded.length);
3937
- Buffer.from(buffer).write(decoded, "latin1");
3938
- }
4148
+ const buffer = decodeArrayBuffer(deserializeString(node.s));
3939
4149
  return assignIndexedValue$1(ctx, node.i, buffer);
3940
4150
  }
3941
- function deserializeTypedArray(ctx, depth, node) {
3942
- var _node$b;
3943
- const construct = getTypedArrayConstructor(node.c);
4151
+ function deserializeView(ctx, depth, node, construct) {
3944
4152
  const source = deserialize$1(ctx, depth, node.f);
3945
4153
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
3946
- const offset = (_node$b = node.b) !== null && _node$b !== void 0 ? _node$b : 0;
4154
+ const offset = node.b ?? 0;
3947
4155
  if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
3948
- return assignIndexedValue$1(ctx, node.i, new construct(source, offset, node.l));
3949
- }
3950
- function deserializeDataView(ctx, depth, node) {
3951
- var _node$b2;
3952
- const source = deserialize$1(ctx, depth, node.f);
3953
- if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
3954
- const offset = (_node$b2 = node.b) !== null && _node$b2 !== void 0 ? _node$b2 : 0;
3955
- if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
3956
- return assignIndexedValue$1(ctx, node.i, new DataView(source, offset, node.l));
4156
+ return assignIndexedValue$1(ctx, node.i, construct ? new construct(source, offset, node.l) : new DataView(source, offset, node.l));
3957
4157
  }
3958
4158
  function deserializeDictionary(ctx, depth, node, result) {
3959
4159
  if (node.p) {
@@ -3987,7 +4187,7 @@ function deserializePlugin(ctx, depth, node) {
3987
4187
  const tag = deserializeString(node.c);
3988
4188
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
3989
4189
  const plugin = currentPlugins[i];
3990
- if (plugin.tag === tag) return assignIndexedValue$1(ctx, node.i, plugin.deserialize(node.s, new DeserializePluginContext(ctx, depth), { id: node.i }));
4190
+ if (plugin.tag === tag) return assignIndexedValue$1(ctx, node.i, plugin.deserialize(node.s, { deserialize: (current) => deserialize$1(ctx, depth, current) }, { id: node.i }));
3991
4191
  }
3992
4192
  }
3993
4193
  throw new SerovalMissingPluginError(node.c);
@@ -3995,6 +4195,7 @@ function deserializePlugin(ctx, depth, node) {
3995
4195
  function deserializePromiseConstructor(ctx, node) {
3996
4196
  const value = assignIndexedValue$1(ctx, node.i, assignIndexedValue$1(ctx, node.s, PROMISE_CONSTRUCTOR()).p);
3997
4197
  assignNodeType(ctx, node.s, 22);
4198
+ ctx.base.pending?.add(node.s);
3998
4199
  return value;
3999
4200
  }
4000
4201
  function deserializePromiseFulfill(ctx, depth, node) {
@@ -4005,6 +4206,7 @@ function deserializePromiseFulfill(ctx, depth, node) {
4005
4206
  if (isThennable(deserialized)) throw new SerovalMalformedNodeError(node.a[1]);
4006
4207
  if (node.t === 23) deferred.s(deserialized);
4007
4208
  else deferred.f(deserialized);
4209
+ ctx.base.pending?.delete(node.i);
4008
4210
  return;
4009
4211
  }
4010
4212
  throw new SerovalMissingInstanceError("Promise");
@@ -4012,48 +4214,31 @@ function deserializePromiseFulfill(ctx, depth, node) {
4012
4214
  function deserializeIteratorFactoryInstance(ctx, depth, node) {
4013
4215
  deserialize$1(ctx, depth, node.a[0]);
4014
4216
  const source = deserialize$1(ctx, depth, node.a[1]);
4015
- validateNodeType(ctx, node, node.a[1].i, 35);
4016
- if (!source) throw new SerovalMalformedNodeError(node.a[1]);
4217
+ if (!isSequence(source)) throw new SerovalMalformedNodeError(node.a[1]);
4017
4218
  return sequenceToIterator(source);
4018
4219
  }
4019
4220
  function deserializeAsyncIteratorFactoryInstance(ctx, depth, node) {
4020
4221
  deserialize$1(ctx, depth, node.a[0]);
4021
4222
  const source = deserialize$1(ctx, depth, node.a[1]);
4022
- validateNodeType(ctx, node, node.a[1].i, 31);
4023
- if (!source) throw new SerovalMalformedNodeError(node.a[1]);
4223
+ if (!isStream(source)) throw new SerovalMalformedNodeError(node.a[1]);
4024
4224
  return streamToAsyncIterable(source);
4025
4225
  }
4026
4226
  function deserializeStreamConstructor(ctx, depth, node) {
4027
- const result = assignIndexedValue$1(ctx, node.i, createStream2());
4227
+ const result = assignIndexedValue$1(ctx, node.i, new Stream(node.l === 1 ? 1 : void 0));
4028
4228
  assignNodeType(ctx, node.i, 31);
4229
+ ctx.base.pending?.add(node.i);
4029
4230
  const items = node.a;
4231
+ validateNodeList(node, items);
4030
4232
  const len = items.length;
4031
4233
  if (len) for (let i = 0; i < len; i++) deserialize$1(ctx, depth, items[i]);
4032
4234
  return result;
4033
4235
  }
4034
- function deserializeStreamNext(ctx, depth, node) {
4035
- const deferred = ctx.base.refs.get(node.i);
4036
- if (deferred) {
4037
- validateNodeType(ctx, node, node.i, 31);
4038
- deferred.next(deserialize$1(ctx, depth, node.f));
4039
- return;
4040
- }
4041
- throw new SerovalMissingInstanceError("Stream");
4042
- }
4043
- function deserializeStreamThrow(ctx, depth, node) {
4044
- const deferred = ctx.base.refs.get(node.i);
4045
- if (deferred) {
4046
- validateNodeType(ctx, node, node.i, 31);
4047
- deferred.throw(deserialize$1(ctx, depth, node.f));
4048
- return;
4049
- }
4050
- throw new SerovalMissingInstanceError("Stream");
4051
- }
4052
- function deserializeStreamReturn(ctx, depth, node) {
4236
+ function deserializeStreamCall(ctx, depth, node, method) {
4053
4237
  const deferred = ctx.base.refs.get(node.i);
4054
4238
  if (deferred) {
4055
4239
  validateNodeType(ctx, node, node.i, 31);
4056
- deferred.return(deserialize$1(ctx, depth, node.f));
4240
+ deferred[method](deserialize$1(ctx, depth, node.f));
4241
+ if (method !== "next") ctx.base.pending?.delete(node.i);
4057
4242
  return;
4058
4243
  }
4059
4244
  throw new SerovalMissingInstanceError("Stream");
@@ -4064,10 +4249,16 @@ function deserializeIteratorFactory(ctx, depth, node) {
4064
4249
  function deserializeAsyncIteratorFactory(ctx, depth, node) {
4065
4250
  deserialize$1(ctx, depth, node.a[1]);
4066
4251
  }
4252
+ function isSequenceIndex(value, size) {
4253
+ return Number.isInteger(value) && value >= -1 && value < size;
4254
+ }
4067
4255
  function deserializeSequence(ctx, depth, node) {
4256
+ validateNodeList(node, node.a);
4257
+ const size = node.a.length;
4258
+ if (!(isSequenceIndex(node.s, size) && isSequenceIndex(node.l, size))) throw new SerovalMalformedNodeError(node);
4068
4259
  const result = assignIndexedValue$1(ctx, node.i, createSequence([], node.s, node.l));
4069
4260
  assignNodeType(ctx, node.i, 35);
4070
- for (let i = 0, len = node.a.length; i < len; i++) result.v[i] = deserialize$1(ctx, depth, node.a[i]);
4261
+ for (let i = 0; i < size; i++) result.v[i] = deserialize$1(ctx, depth, node.a[i]);
4071
4262
  return result;
4072
4263
  }
4073
4264
  function deserialize$1(ctx, depth, node) {
@@ -4104,9 +4295,9 @@ function deserialize$1(ctx, depth, node) {
4104
4295
  return deserializeArrayBuffer(ctx, node);
4105
4296
  case 16:
4106
4297
  case 15:
4107
- return deserializeTypedArray(ctx, depth, node);
4298
+ return deserializeView(ctx, depth, node, getTypedArrayConstructor(node.c));
4108
4299
  case 20:
4109
- return deserializeDataView(ctx, depth, node);
4300
+ return deserializeView(ctx, depth, node);
4110
4301
  case 14:
4111
4302
  return deserializeAggregateError(ctx, depth, node);
4112
4303
  case 13:
@@ -4131,11 +4322,11 @@ function deserialize$1(ctx, depth, node) {
4131
4322
  case 31:
4132
4323
  return deserializeStreamConstructor(ctx, depth, node);
4133
4324
  case 32:
4134
- return deserializeStreamNext(ctx, depth, node);
4325
+ return deserializeStreamCall(ctx, depth, node, "next");
4135
4326
  case 33:
4136
- return deserializeStreamThrow(ctx, depth, node);
4327
+ return deserializeStreamCall(ctx, depth, node, "throw");
4137
4328
  case 34:
4138
- return deserializeStreamReturn(ctx, depth, node);
4329
+ return deserializeStreamCall(ctx, depth, node, "return");
4139
4330
  case 27:
4140
4331
  return deserializeIteratorFactory(ctx, depth, node);
4141
4332
  case 29:
@@ -4155,67 +4346,10 @@ function deserializeTop(ctx, node) {
4155
4346
  throw new SerovalDeserializationError(error);
4156
4347
  }
4157
4348
  }
4158
- var SyncParsePluginContext = class {
4159
- constructor(_p, depth) {
4160
- this._p = _p;
4161
- this.depth = depth;
4162
- }
4163
- parse(current) {
4164
- return parseSOS(this._p, this.depth, current);
4165
- }
4166
- };
4167
- var StreamParsePluginContext = class {
4168
- constructor(_p, depth) {
4169
- this._p = _p;
4170
- this.depth = depth;
4171
- }
4172
- parse(current) {
4173
- return parseSOS(this._p, this.depth, current);
4174
- }
4175
- parseWithError(current) {
4176
- return parseWithError(this._p, this.depth, current);
4177
- }
4178
- isAlive() {
4179
- return this._p.state.alive;
4180
- }
4181
- pushPendingState() {
4182
- pushPendingState(this._p);
4183
- }
4184
- popPendingState() {
4185
- popPendingState(this._p);
4186
- }
4187
- onParse(node) {
4188
- onParse(this._p, node);
4189
- }
4190
- onError(error) {
4191
- onError(this._p, error);
4192
- }
4193
- addCleanup(callback) {
4194
- this._p.state.cleanups.push(callback);
4195
- }
4196
- };
4197
- function createStreamParserState(options) {
4198
- return {
4199
- alive: true,
4200
- pending: 0,
4201
- initial: true,
4202
- buffer: [],
4203
- onParse: options.onParse,
4204
- onError: options.onError,
4205
- onDone: options.onDone,
4206
- cleanups: []
4207
- };
4208
- }
4209
- function createStreamParserContext(options) {
4210
- return {
4211
- type: 2,
4212
- base: createBaseParserContext(2, options),
4213
- state: createStreamParserState(options)
4214
- };
4215
- }
4216
4349
  function parseItems(ctx, depth, current) {
4217
- const nodes = [];
4218
- for (let i = 0, len = current.length; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
4350
+ const len = current.length;
4351
+ const nodes = new Array(len);
4352
+ for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
4219
4353
  else nodes[i] = 0;
4220
4354
  return nodes;
4221
4355
  }
@@ -4223,20 +4357,22 @@ function parseArray(ctx, depth, id, current) {
4223
4357
  return createArrayNode(id, current, parseItems(ctx, depth, current));
4224
4358
  }
4225
4359
  function parseProperties(ctx, depth, properties) {
4226
- const entries = Object.entries(properties);
4227
- const keyNodes = [];
4228
- const valueNodes = [];
4229
- for (let i = 0, len = entries.length; i < len; i++) {
4230
- keyNodes.push(serializeString(entries[i][0]));
4231
- valueNodes.push(parseSOS(ctx, depth, entries[i][1]));
4360
+ const keys = Object.keys(properties);
4361
+ const len = keys.length;
4362
+ const keyNodes = new Array(len);
4363
+ const valueNodes = new Array(len);
4364
+ for (let i = 0, key; i < len; i++) {
4365
+ key = keys[i];
4366
+ keyNodes[i] = serializeString(key);
4367
+ valueNodes[i] = parseSOS(ctx, depth, properties[key]);
4232
4368
  }
4233
4369
  if (SYM_ITERATOR in properties) {
4234
4370
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
4235
- valueNodes.push(createIteratorFactoryInstanceNode(parseIteratorFactory(ctx.base), parseSOS(ctx, depth, createSequenceFromIterable(properties))));
4371
+ valueNodes.push(createIteratorFactoryInstanceNode(28, parseIteratorFactory(ctx.base), parseSOS(ctx, depth, createSequenceFromIterable(properties))));
4236
4372
  }
4237
4373
  if (SYM_ASYNC_ITERATOR in properties) {
4238
4374
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ASYNC_ITERATOR));
4239
- valueNodes.push(createAsyncIteratorFactoryInstanceNode(parseAsyncIteratorFactory(ctx.base), parseSOS(ctx, depth, ctx.type === 1 ? createStream2() : createStreamFromAsyncIterable(properties, ctx.state.cleanups))));
4375
+ valueNodes.push(createIteratorFactoryInstanceNode(30, parseAsyncIteratorFactory(ctx.base), parseAsyncIterable(ctx, depth, properties)));
4240
4376
  }
4241
4377
  if (SYM_TO_STRING_TAG in properties) {
4242
4378
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_TO_STRING_TAG));
@@ -4251,206 +4387,142 @@ function parseProperties(ctx, depth, properties) {
4251
4387
  v: valueNodes
4252
4388
  };
4253
4389
  }
4390
+ function parseAsyncIterable(ctx, depth, current) {
4391
+ if (ctx.type !== 2) throw new SerovalUnsupportedTypeError(current);
4392
+ const id = createIndexForValue(ctx.base, {});
4393
+ const result = createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), []);
4394
+ ctx.state.iterable(ctx, depth, id, current);
4395
+ return result;
4396
+ }
4254
4397
  function parsePlainObject(ctx, depth, id, current, empty) {
4255
4398
  return createObjectNode(id, current, empty, parseProperties(ctx, depth, current));
4256
4399
  }
4257
4400
  function parseBoxed(ctx, depth, id, current) {
4258
4401
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
4259
4402
  }
4260
- function parseTypedArray(ctx, depth, id, current) {
4261
- current = getArrayBufferView(ctx.base, current);
4262
- return createTypedArrayNode(id, current, parseSOS(ctx, depth, current.buffer));
4263
- }
4264
- function parseBigIntTypedArray(ctx, depth, id, current) {
4403
+ function parseView(ctx, depth, type, id, current) {
4265
4404
  current = getArrayBufferView(ctx.base, current);
4266
- return createBigIntTypedArrayNode(id, current, parseSOS(ctx, depth, current.buffer));
4267
- }
4268
- function parseDataView(ctx, depth, id, current) {
4269
- current = getArrayBufferView(ctx.base, current);
4270
- return createDataViewNode(id, current, parseSOS(ctx, depth, current.buffer));
4271
- }
4272
- function parseError(ctx, depth, id, current) {
4273
- const options = getErrorOptions(current, ctx.base.features);
4274
- return createErrorNode(id, current, options ? parseProperties(ctx, depth, options) : void 0);
4405
+ return createViewNode(type, id, current, parseSOS(ctx, depth, current.buffer));
4275
4406
  }
4276
- function parseAggregateError(ctx, depth, id, current) {
4407
+ function parseError(ctx, depth, type, id, current) {
4277
4408
  const options = getErrorOptions(current, ctx.base.features);
4278
- return createAggregateErrorNode(id, current, options ? parseProperties(ctx, depth, options) : void 0);
4409
+ return createErrorNode(type, id, current, options ? parseProperties(ctx, depth, options) : void 0);
4279
4410
  }
4280
4411
  function parseMap(ctx, depth, id, current) {
4281
- const keyNodes = [];
4282
- const valueNodes = [];
4412
+ const sentinel = parseMapSentinel(ctx.base);
4413
+ const keyNodes = new Array(current.size);
4414
+ const valueNodes = new Array(current.size);
4415
+ let i = 0;
4283
4416
  for (const [key, value] of current.entries()) {
4284
- keyNodes.push(parseSOS(ctx, depth, key));
4285
- valueNodes.push(parseSOS(ctx, depth, value));
4417
+ keyNodes[i] = parseSOS(ctx, depth, key);
4418
+ valueNodes[i] = parseSOS(ctx, depth, value);
4419
+ i++;
4286
4420
  }
4287
- return createMapNode(ctx.base, id, keyNodes, valueNodes);
4421
+ return createMapNode(id, sentinel, keyNodes, valueNodes);
4288
4422
  }
4289
4423
  function parseSet(ctx, depth, id, current) {
4290
- const items = [];
4291
- for (const item of current.keys()) items.push(parseSOS(ctx, depth, item));
4424
+ const items = new Array(current.size);
4425
+ let i = 0;
4426
+ for (const item of current.keys()) items[i++] = parseSOS(ctx, depth, item);
4292
4427
  return createSetNode(id, items);
4293
4428
  }
4294
- function parseStream(ctx, depth, id, current) {
4295
- const result = createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), []);
4296
- if (ctx.type === 1) return result;
4297
- pushPendingState(ctx);
4298
- current.on({
4299
- next: (value) => {
4300
- if (ctx.state.alive) {
4301
- const parsed = parseWithError(ctx, depth, value);
4302
- if (parsed) onParse(ctx, createStreamNextNode(id, parsed));
4303
- }
4304
- },
4305
- throw: (value) => {
4306
- if (ctx.state.alive) {
4307
- const parsed = parseWithError(ctx, depth, value);
4308
- if (parsed) onParse(ctx, createStreamThrowNode(id, parsed));
4309
- }
4310
- popPendingState(ctx);
4311
- },
4312
- return: (value) => {
4313
- if (ctx.state.alive) {
4314
- const parsed = parseWithError(ctx, depth, value);
4315
- if (parsed) onParse(ctx, createStreamReturnNode(id, parsed));
4316
- }
4317
- popPendingState(ctx);
4318
- }
4319
- });
4320
- return result;
4429
+ function parseEndedStream(ctx, depth, id, current) {
4430
+ const events = current.endedEvents();
4431
+ if (!events) throw new SerovalUnsupportedTypeError(current);
4432
+ const values = events.values;
4433
+ const last = values.length - 1;
4434
+ const sequence = new Array(values.length);
4435
+ if (last >= 0) markParserRef(ctx.base, id);
4436
+ for (let i = 0; i <= last; i++) sequence[i] = createStreamEventNode(i < last ? 32 : events.success ? 34 : 33, id, parseSOS(ctx, depth, values[i]));
4437
+ return sequence;
4321
4438
  }
4322
- function handlePromiseSuccess(id, depth, data) {
4323
- if (this.state.alive) {
4324
- const parsed = parseWithError(this, depth, data);
4325
- if (parsed) onParse(this, createSerovalNode(23, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(this.base, 2), parsed], void 0, void 0, void 0, void 0));
4326
- popPendingState(this);
4439
+ function parseStream(ctx, depth, id, current) {
4440
+ const reference = parseSpecialReference(ctx.base, 4);
4441
+ if (ctx.type === 2) {
4442
+ const result = createStreamConstructorNode(id, reference, []);
4443
+ ctx.state.stream(ctx, depth, id, current);
4444
+ return result;
4327
4445
  }
4446
+ return createStreamConstructorNode(id, reference, parseEndedStream(ctx, depth, id, current));
4328
4447
  }
4329
- function handlePromiseFailure(id, depth, data) {
4330
- if (this.state.alive) {
4331
- const parsed = parseWithError(this, depth, data);
4332
- if (parsed) onParse(this, createSerovalNode(24, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(this.base, 3), parsed], void 0, void 0, void 0, void 0));
4333
- }
4334
- popPendingState(this);
4448
+ function parseLiveStream(ctx, depth, id, current) {
4449
+ if (ctx.type !== 2) throw new SerovalUnsupportedTypeError(current);
4450
+ const result = createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), [], 1);
4451
+ ctx.state.live(ctx, depth, id, current);
4452
+ return result;
4335
4453
  }
4336
4454
  function parsePromise(ctx, depth, id, current) {
4455
+ if (ctx.type !== 2) throw new SerovalUnsupportedTypeError(current);
4337
4456
  const resolver = createIndexForValue(ctx.base, {});
4338
- if (ctx.type === 2) {
4339
- pushPendingState(ctx);
4340
- current.then(handlePromiseSuccess.bind(ctx, resolver, depth), handlePromiseFailure.bind(ctx, resolver, depth));
4341
- }
4457
+ ctx.state.promise(ctx, resolver, depth, current);
4342
4458
  return createPromiseConstructorNode(ctx.base, id, resolver);
4343
4459
  }
4344
4460
  function parsePluginSync(ctx, depth, id, current, currentPlugins) {
4345
4461
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
4346
4462
  const plugin = currentPlugins[i];
4347
- if (plugin.parse.sync && plugin.test(current)) return createPluginNode(id, plugin.tag, plugin.parse.sync(current, new SyncParsePluginContext(ctx, depth), { id }));
4348
- }
4349
- }
4350
- function parsePluginStream(ctx, depth, id, current, currentPlugins) {
4351
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
4352
- const plugin = currentPlugins[i];
4353
- if (plugin.parse.stream && plugin.test(current)) return createPluginNode(id, plugin.tag, plugin.parse.stream(current, new StreamParsePluginContext(ctx, depth), { id }));
4463
+ if (plugin.parse.sync && plugin.test(current)) return createPluginNode(id, plugin.tag, plugin.parse.sync(current, { parse: (value) => parseSOS(ctx, depth, value) }, { id }));
4354
4464
  }
4355
4465
  }
4356
- function parsePlugin(ctx, depth, id, current) {
4357
- const currentPlugins = ctx.base.plugins;
4358
- if (currentPlugins) return ctx.type === 1 ? parsePluginSync(ctx, depth, id, current, currentPlugins) : parsePluginStream(ctx, depth, id, current, currentPlugins);
4466
+ function parsePlugin(ctx, depth, id, current, currentPlugins) {
4467
+ if (currentPlugins) return ctx.type === 1 ? parsePluginSync(ctx, depth, id, current, currentPlugins) : ctx.state.plugin(ctx, depth, id, current, currentPlugins);
4359
4468
  }
4360
4469
  function parseSequence(ctx, depth, id, current) {
4361
- const nodes = [];
4362
- for (let i = 0, len = current.v.length; i < len; i++) nodes[i] = parseSOS(ctx, depth, current.v[i]);
4470
+ const len = current.v.length;
4471
+ const nodes = new Array(len);
4472
+ for (let i = 0; i < len; i++) nodes[i] = parseSOS(ctx, depth, current.v[i]);
4363
4473
  return createSequenceNode(id, nodes, current.t, current.d);
4364
4474
  }
4365
4475
  function parseObjectPhase2(ctx, depth, id, current, currentClass) {
4366
- switch (currentClass) {
4367
- case Object:
4368
- return parsePlainObject(ctx, depth, id, current, false);
4369
- case void 0:
4476
+ if (currentClass === Object) return parsePlainObject(ctx, depth, id, current, false);
4477
+ switch (getObjectKind(current, currentClass, ctx.base.features)) {
4478
+ case 2:
4370
4479
  return parsePlainObject(ctx, depth, id, current, true);
4371
- case Date:
4480
+ case 3:
4372
4481
  return createDateNode(id, current);
4373
- case Error:
4374
- case EvalError:
4375
- case RangeError:
4376
- case ReferenceError:
4377
- case SyntaxError:
4378
- case TypeError:
4379
- case URIError:
4380
- return parseError(ctx, depth, id, current);
4381
- case Number:
4382
- case Boolean:
4383
- case String:
4384
- case BigInt:
4482
+ case 4:
4483
+ return parseError(ctx, depth, 13, id, current);
4484
+ case 5:
4485
+ return parseError(ctx, depth, 14, id, current);
4486
+ case 6:
4385
4487
  return parseBoxed(ctx, depth, id, current);
4386
- case ArrayBuffer:
4488
+ case 7:
4387
4489
  return createArrayBufferNode(ctx.base, id, current);
4388
- case Int8Array:
4389
- case Int16Array:
4390
- case Int32Array:
4391
- case Uint8Array:
4392
- case Uint16Array:
4393
- case Uint32Array:
4394
- case Uint8ClampedArray:
4395
- case Float32Array:
4396
- case Float64Array:
4397
- return parseTypedArray(ctx, depth, id, current);
4398
- case DataView:
4399
- return parseDataView(ctx, depth, id, current);
4400
- case Map:
4490
+ case 8:
4491
+ return parseView(ctx, depth, 15, id, current);
4492
+ case 9:
4493
+ return parseView(ctx, depth, 16, id, current);
4494
+ case 10:
4495
+ return parseView(ctx, depth, 20, id, current);
4496
+ case 11:
4401
4497
  return parseMap(ctx, depth, id, current);
4402
- case Set:
4498
+ case 12:
4403
4499
  return parseSet(ctx, depth, id, current);
4500
+ case 13:
4501
+ return parsePromise(ctx, depth, id, current);
4502
+ case 14:
4503
+ return createRegExpNode(id, current);
4504
+ case 15:
4505
+ return createTemporalNode(id, getTemporalType(currentClass), current);
4506
+ case 16:
4507
+ return parsePlainObject(ctx, depth, id, current, !!currentClass);
4508
+ default:
4509
+ throw new SerovalUnsupportedTypeError(current);
4404
4510
  }
4405
- if (currentClass === Promise || current instanceof Promise) return parsePromise(ctx, depth, id, current);
4406
- const currentFeatures = ctx.base.features;
4407
- if (currentFeatures & 32 && currentClass === RegExp) return createRegExpNode(id, current);
4408
- if (currentFeatures & 16) switch (currentClass) {
4409
- case BigInt64Array:
4410
- case BigUint64Array:
4411
- return parseBigIntTypedArray(ctx, depth, id, current);
4412
- }
4413
- if (currentFeatures & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return parseAggregateError(ctx, depth, id, current);
4414
- if (currentFeatures & 64 && typeof Temporal !== "undefined") switch (currentClass) {
4415
- case Temporal.Instant:
4416
- return createTemporalNode(id, 0, current);
4417
- case Temporal.Duration:
4418
- return createTemporalNode(id, 1, current);
4419
- case Temporal.PlainDate:
4420
- return createTemporalNode(id, 2, current);
4421
- case Temporal.PlainDateTime:
4422
- return createTemporalNode(id, 3, current);
4423
- case Temporal.PlainMonthDay:
4424
- return createTemporalNode(id, 4, current);
4425
- case Temporal.PlainTime:
4426
- return createTemporalNode(id, 5, current);
4427
- case Temporal.PlainYearMonth:
4428
- return createTemporalNode(id, 6, current);
4429
- case Temporal.ZonedDateTime:
4430
- return createTemporalNode(id, 7, current);
4431
- }
4432
- if (current instanceof Error) return parseError(ctx, depth, id, current);
4433
- if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return parsePlainObject(ctx, depth, id, current, !!currentClass);
4434
- throw new SerovalUnsupportedTypeError(current);
4435
4511
  }
4436
4512
  function parseObject(ctx, depth, id, current) {
4437
4513
  if (Array.isArray(current)) return parseArray(ctx, depth, id, current);
4438
- if (isStream2(current)) return parseStream(ctx, depth, id, current);
4514
+ if (isStream(current)) return isLiveStream(current) ? parseLiveStream(ctx, depth, id, current) : parseStream(ctx, depth, id, current);
4439
4515
  if (isSequence(current)) return parseSequence(ctx, depth, id, current);
4440
- let currentClass = current.constructor;
4441
- if (currentClass !== void 0 && typeof currentClass !== "function") {
4442
- const proto = Object.getPrototypeOf(current);
4443
- currentClass = proto === null ? void 0 : proto.constructor;
4444
- }
4516
+ const currentClass = getObjectClass(current);
4445
4517
  if (currentClass === OpaqueReference) return parseSOS(ctx, depth, current.replacement);
4446
- const parsed = parsePlugin(ctx, depth, id, current);
4518
+ const parsed = parsePlugin(ctx, depth, id, current, getObjectPlugins(ctx.base, current, currentClass));
4447
4519
  if (parsed) return parsed;
4448
4520
  return parseObjectPhase2(ctx, depth, id, current, currentClass);
4449
4521
  }
4450
4522
  function parseFunction(ctx, depth, current) {
4451
4523
  const ref = getReferenceNode(ctx.base, current);
4452
- if (ref.type !== 0) return ref.value;
4453
- const plugin = parsePlugin(ctx, depth, ref.value, current);
4524
+ if (typeof ref !== "number") return ref;
4525
+ const plugin = parsePlugin(ctx, depth, ref, current, ctx.base.plugins);
4454
4526
  if (plugin) return plugin;
4455
4527
  throw new SerovalUnsupportedTypeError(current);
4456
4528
  }
@@ -4470,7 +4542,7 @@ function parseSOS(ctx, depth, current) {
4470
4542
  case "object":
4471
4543
  if (current) {
4472
4544
  const ref = getReferenceNode(ctx.base, current);
4473
- return ref.type === 0 ? parseObject(ctx, depth + 1, ref.value, current) : ref.value;
4545
+ return typeof ref === "number" ? parseObject(ctx, depth + 1, ref, current) : ref;
4474
4546
  }
4475
4547
  return NULL_NODE;
4476
4548
  case "symbol":
@@ -4481,61 +4553,400 @@ function parseSOS(ctx, depth, current) {
4481
4553
  throw new SerovalUnsupportedTypeError(current);
4482
4554
  }
4483
4555
  }
4484
- function onParse(ctx, node) {
4485
- if (ctx.state.initial) ctx.state.buffer.push(node);
4486
- else onParseInternal(ctx, node, false);
4556
+ function createStreamParsePluginContext(ctx, depth) {
4557
+ const state = ctx.state;
4558
+ function parse(current) {
4559
+ state.parsing++;
4560
+ try {
4561
+ return parseSOS(ctx, depth, current);
4562
+ } finally {
4563
+ state.parsing--;
4564
+ if (!state.alive) releaseParserValues(ctx);
4565
+ }
4566
+ }
4567
+ return {
4568
+ parse,
4569
+ parseStreamSource(current) {
4570
+ state.parsing++;
4571
+ try {
4572
+ if (depth >= ctx.base.depthLimit) throw new SerovalDepthLimitError(ctx.base.depthLimit);
4573
+ const id = createIndexForValue(ctx.base, {});
4574
+ const node = createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), [], 1);
4575
+ iterateAsync(ctx, depth + 1, id, current, true);
4576
+ return node;
4577
+ } finally {
4578
+ state.parsing--;
4579
+ if (!state.alive) releaseParserValues(ctx);
4580
+ }
4581
+ },
4582
+ parseWithError(current) {
4583
+ try {
4584
+ return parse(current);
4585
+ } catch (error) {
4586
+ stopStreamParse(ctx, 1, error);
4587
+ return;
4588
+ }
4589
+ },
4590
+ isAlive: () => state.alive,
4591
+ pushPendingState: () => {
4592
+ pushPendingState(ctx);
4593
+ },
4594
+ popPendingState: () => {
4595
+ popPendingState(ctx);
4596
+ },
4597
+ onParse(node) {
4598
+ if (state.alive) {
4599
+ state.queue.push({
4600
+ node,
4601
+ initial: false,
4602
+ accept: void 0
4603
+ });
4604
+ drainOutput(ctx);
4605
+ }
4606
+ },
4607
+ onError(error) {
4608
+ stopStreamParse(ctx, 1, error);
4609
+ },
4610
+ addCleanup(callback) {
4611
+ addCleanup(ctx, callback);
4612
+ }
4613
+ };
4614
+ }
4615
+ function getStreamParsePluginContext(ctx, depth) {
4616
+ const contexts = ctx.state.pluginContexts;
4617
+ return contexts[depth] ?? (contexts[depth] = createStreamParsePluginContext(ctx, depth));
4618
+ }
4619
+ function addCleanup(ctx, callback) {
4620
+ if (ctx.state.alive) ctx.state.cleanups.push(callback);
4621
+ else callback();
4487
4622
  }
4488
- function onError(ctx, error) {
4489
- if (ctx.state.onError) ctx.state.onError(error);
4490
- else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
4623
+ function parsePluginStream(ctx, depth, id, current, currentPlugins) {
4624
+ for (let i = 0, len = currentPlugins.length; i < len; i++) {
4625
+ const plugin = currentPlugins[i];
4626
+ if (plugin.parse.stream && plugin.test(current)) return createPluginNode(id, plugin.tag, plugin.parse.stream(current, getStreamParsePluginContext(ctx, depth), { id }));
4627
+ }
4491
4628
  }
4492
- function onDone(ctx) {
4493
- if (ctx.state.onDone) ctx.state.onDone();
4494
- for (let i = 0, len = ctx.state.cleanups.length; i < len; i++) ctx.state.cleanups[i]();
4629
+ function streamListener(ctx, depth, id, subscription) {
4630
+ const handle = (type, terminal) => (value, accept) => {
4631
+ let failure = void 0;
4632
+ if (ctx.state.alive) try {
4633
+ failure = parseEvent(ctx, depth, value, createStreamEventNode.bind(null, type, id), accept);
4634
+ } catch (error) {
4635
+ if (!subscription?.active) throw error;
4636
+ subscription.failure ?? (subscription.failure = { value: error });
4637
+ failure = error;
4638
+ }
4639
+ if (terminal && !accept) popPendingState(ctx);
4640
+ return failure;
4641
+ };
4642
+ return {
4643
+ next: handle(32, false),
4644
+ throw: handle(33, true),
4645
+ return: handle(34, true),
4646
+ done() {
4647
+ popPendingState(ctx);
4648
+ },
4649
+ error(reason) {
4650
+ if (ctx.state.alive) stopStreamParse(ctx, 1, reason);
4651
+ }
4652
+ };
4495
4653
  }
4496
- function onParseInternal(ctx, node, initial) {
4654
+ function subscribeStream(ctx, depth, id, current) {
4655
+ pushPendingState(ctx);
4656
+ const subscription = {
4657
+ active: true,
4658
+ failure: void 0
4659
+ };
4660
+ let cleanup;
4497
4661
  try {
4498
- ctx.state.onParse(node, initial);
4662
+ cleanup = current.on(streamListener(ctx, depth, id, subscription));
4663
+ } finally {
4664
+ subscription.active = false;
4665
+ }
4666
+ try {
4667
+ addCleanup(ctx, cleanup);
4499
4668
  } catch (error) {
4500
- onError(ctx, error);
4669
+ subscription.failure ?? (subscription.failure = { value: error });
4670
+ }
4671
+ const failure = subscription.failure;
4672
+ subscription.failure = void 0;
4673
+ if (failure) throw failure.value;
4674
+ }
4675
+ function consumeLiveStream(ctx, depth, id, current) {
4676
+ const cancel = current.pump(streamListener(ctx, depth, id));
4677
+ pushPendingState(ctx);
4678
+ addCleanup(ctx, () => {
4679
+ cancel(ctx.state.reason);
4680
+ });
4681
+ }
4682
+ function wrapPromiseResult(ctx, type, id, node) {
4683
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node], void 0, void 0, void 0, void 0);
4684
+ }
4685
+ function handlePromise(type, id, depth, data) {
4686
+ if (this.state.alive) parseEvent(this, depth, data, wrapPromiseResult.bind(null, this, type, id), void 0);
4687
+ popPendingState(this);
4688
+ }
4689
+ function watchPromise(ctx, resolver, depth, current) {
4690
+ pushPendingState(ctx);
4691
+ current.then(handlePromise.bind(ctx, 23, resolver, depth), handlePromise.bind(ctx, 24, resolver, depth));
4692
+ }
4693
+ function returnIterator(iterator) {
4694
+ Promise.resolve().then(() => iterator.return?.()).catch(() => {
4695
+ });
4696
+ }
4697
+ function iterateAsync(ctx, depth, id, current, forwardReason) {
4698
+ const iterator = current[SYM_ASYNC_ITERATOR]();
4699
+ const listener = streamListener(ctx, depth, id);
4700
+ let active = true;
4701
+ function stop() {
4702
+ if (active) {
4703
+ active = false;
4704
+ if (forwardReason) try {
4705
+ Promise.resolve(iterator.return?.(ctx.state.reason)).catch(() => {
4706
+ });
4707
+ } catch {
4708
+ }
4709
+ else returnIterator(iterator);
4710
+ }
4711
+ }
4712
+ function pull() {
4713
+ if (active) iterator.next().then((result) => {
4714
+ if (active) {
4715
+ if (result.done) {
4716
+ active = false;
4717
+ listener.return(result.value, void 0);
4718
+ } else if (listener.next(result.value, pull) !== void 0) {
4719
+ stop();
4720
+ listener.done();
4721
+ }
4722
+ }
4723
+ }, (error) => {
4724
+ if (active) {
4725
+ active = false;
4726
+ listener.throw(error, void 0);
4727
+ }
4728
+ });
4501
4729
  }
4730
+ pushPendingState(ctx);
4731
+ addCleanup(ctx, stop);
4732
+ pull();
4733
+ }
4734
+ function createStreamParserState(options) {
4735
+ return {
4736
+ alive: true,
4737
+ pending: 0,
4738
+ parsing: 0,
4739
+ queue: [],
4740
+ writing: false,
4741
+ inFlight: void 0,
4742
+ reason: void 0,
4743
+ onParse: options.onParse,
4744
+ onError: options.onError,
4745
+ onDone: options.onDone,
4746
+ cleanups: [],
4747
+ pluginContexts: [],
4748
+ stream: subscribeStream,
4749
+ live: consumeLiveStream,
4750
+ promise: watchPromise,
4751
+ iterable: iterateAsync,
4752
+ plugin: parsePluginStream
4753
+ };
4754
+ }
4755
+ function createStreamParserContext(options) {
4756
+ return {
4757
+ type: 2,
4758
+ base: createBaseParserContext(options),
4759
+ state: createStreamParserState(options)
4760
+ };
4502
4761
  }
4503
4762
  function pushPendingState(ctx) {
4504
- ctx.state.pending++;
4763
+ if (ctx.state.alive) ctx.state.pending++;
4505
4764
  }
4506
4765
  function popPendingState(ctx) {
4507
- if (--ctx.state.pending <= 0) onDone(ctx);
4766
+ if (ctx.state.alive) {
4767
+ ctx.state.pending--;
4768
+ checkStreamParse(ctx);
4769
+ }
4508
4770
  }
4509
- function parseWithError(ctx, depth, current) {
4771
+ function parseEvent(ctx, depth, value, wrap, accept) {
4772
+ const state = ctx.state;
4773
+ const record = {
4774
+ node: void 0,
4775
+ initial: false,
4776
+ accept
4777
+ };
4778
+ let failure;
4779
+ state.queue.push(record);
4780
+ state.parsing++;
4510
4781
  try {
4511
- return parseSOS(ctx, depth, current);
4512
- } catch (err) {
4513
- onError(ctx, err);
4514
- return;
4782
+ record.node = wrap(parseSOS(ctx, depth, value));
4783
+ } catch (error) {
4784
+ failure = { value: error };
4785
+ } finally {
4786
+ state.parsing--;
4787
+ if (!state.alive) releaseParserValues(ctx);
4788
+ }
4789
+ if (failure) stopStreamParse(ctx, 1, failure.value);
4790
+ else drainOutput(ctx);
4791
+ return failure?.value;
4792
+ }
4793
+ function drainOutput(ctx) {
4794
+ const state = ctx.state;
4795
+ if (state.writing || state.parsing > 0) return;
4796
+ while (state.alive && state.queue.length > 0) {
4797
+ const record = state.queue.shift();
4798
+ if (!record.node) continue;
4799
+ let pending;
4800
+ state.writing = true;
4801
+ state.inFlight = record;
4802
+ state.parsing++;
4803
+ try {
4804
+ if (!state.onParse) throw new Error("Stream output callback is unavailable");
4805
+ const result = state.onParse(record.node, record.initial);
4806
+ if (result !== null && (typeof result === "object" || typeof result === "function")) {
4807
+ const then = Reflect.get(result, "then");
4808
+ if (typeof then === "function") pending = new Promise((resolve, reject) => {
4809
+ Reflect.apply(then, result, [resolve, reject]);
4810
+ });
4811
+ }
4812
+ } catch (error) {
4813
+ releaseRecord(record);
4814
+ stopStreamParse(ctx, 1, error);
4815
+ return;
4816
+ } finally {
4817
+ state.parsing--;
4818
+ if (!state.alive) releaseParserValues(ctx);
4819
+ }
4820
+ record.node = void 0;
4821
+ if (pending) {
4822
+ if (!state.alive) releaseRecord(record);
4823
+ pending.then(() => {
4824
+ state.writing = false;
4825
+ state.inFlight = void 0;
4826
+ if (state.alive) {
4827
+ const accept2 = record.accept;
4828
+ releaseRecord(record);
4829
+ accept2?.();
4830
+ drainOutput(ctx);
4831
+ }
4832
+ }, (error) => {
4833
+ state.writing = false;
4834
+ state.inFlight = void 0;
4835
+ releaseRecord(record);
4836
+ stopStreamParse(ctx, 1, error);
4837
+ });
4838
+ return;
4839
+ }
4840
+ if (!state.alive) {
4841
+ releaseRecord(record);
4842
+ return;
4843
+ }
4844
+ state.writing = false;
4845
+ state.inFlight = void 0;
4846
+ const accept = record.accept;
4847
+ releaseRecord(record);
4848
+ accept?.();
4515
4849
  }
4850
+ checkStreamParse(ctx);
4851
+ }
4852
+ function checkStreamParse(ctx) {
4853
+ const state = ctx.state;
4854
+ if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length === 0 && !state.writing) stopStreamParse(ctx, 0);
4516
4855
  }
4517
4856
  function startStreamParse(ctx, current) {
4518
- const parsed = parseWithError(ctx, 0, current);
4519
- if (parsed) {
4520
- onParseInternal(ctx, parsed, true);
4521
- ctx.state.initial = false;
4522
- flushStreamParse(ctx, ctx.state);
4523
- if (ctx.state.pending <= 0) destroyStreamParse(ctx);
4857
+ const state = ctx.state;
4858
+ let parsed;
4859
+ let failure;
4860
+ state.parsing++;
4861
+ try {
4862
+ parsed = parseSOS(ctx, 0, current);
4863
+ } catch (error) {
4864
+ failure = { value: error };
4865
+ } finally {
4866
+ state.parsing--;
4867
+ if (!state.alive) releaseParserValues(ctx);
4868
+ }
4869
+ if (failure) {
4870
+ if (state.alive) stopStreamParse(ctx, 1, failure.value);
4871
+ else throw failure.value;
4872
+ } else if (parsed && state.alive) {
4873
+ state.queue.unshift({
4874
+ node: parsed,
4875
+ initial: true,
4876
+ accept: void 0
4877
+ });
4878
+ drainOutput(ctx);
4879
+ }
4880
+ }
4881
+ function runCleanups(state) {
4882
+ const cleanups = state.cleanups;
4883
+ let failure;
4884
+ state.cleanups = [];
4885
+ for (const record of state.queue) releaseRecord(record);
4886
+ state.queue.length = 0;
4887
+ if (state.inFlight) releaseRecord(state.inFlight);
4888
+ state.inFlight = void 0;
4889
+ for (let i = 0, len = cleanups.length; i < len; i++) try {
4890
+ cleanups[i]();
4891
+ } catch (error) {
4892
+ failure ?? (failure = { value: error });
4893
+ }
4894
+ if (failure) throw failure.value;
4895
+ }
4896
+ function releaseRecord(record) {
4897
+ record.node = void 0;
4898
+ record.accept = void 0;
4899
+ }
4900
+ function releaseParserValues(ctx) {
4901
+ ctx.base.refs = /* @__PURE__ */ new Map();
4902
+ ctx.base.marked.clear();
4903
+ ctx.base.plugins = void 0;
4904
+ ctx.base.plainObjectPlugins = void 0;
4905
+ ctx.state.pluginContexts = [];
4906
+ if (ctx.state.parsing === 0) ctx.state.reason = void 0;
4907
+ }
4908
+ function stopStreamParse(ctx, mode, reason) {
4909
+ const state = ctx.state;
4910
+ if (state.alive) {
4911
+ state.alive = false;
4912
+ const onError = state.onError;
4913
+ const onDone = state.onDone;
4914
+ if (mode === 1 && !onError && !(reason instanceof SerovalParserError)) reason = new SerovalParserError(reason);
4915
+ state.reason = reason;
4916
+ state.onParse = void 0;
4917
+ state.onError = void 0;
4918
+ state.onDone = void 0;
4919
+ state.writing = false;
4920
+ state.parsing++;
4921
+ let failure;
4922
+ try {
4923
+ if (mode === 1) {
4924
+ if (onError) onError(reason);
4925
+ else throw reason;
4926
+ } else if (mode === 0) onDone?.();
4927
+ } catch (error) {
4928
+ failure = { value: error };
4929
+ }
4930
+ try {
4931
+ runCleanups(state);
4932
+ } catch (error) {
4933
+ failure ?? (failure = { value: error });
4934
+ } finally {
4935
+ state.parsing--;
4936
+ releaseParserValues(ctx);
4937
+ state.pending = 0;
4938
+ }
4939
+ if (failure) throw failure.value;
4524
4940
  }
4525
4941
  }
4526
- function flushStreamParse(ctx, state) {
4527
- for (let i = 0, len = state.buffer.length; i < len; i++) onParseInternal(ctx, state.buffer[i], false);
4528
- }
4529
- function destroyStreamParse(ctx) {
4530
- if (ctx.state.alive) {
4531
- onDone(ctx);
4532
- ctx.state.alive = false;
4533
- }
4942
+ function destroyStreamParse(ctx, reason) {
4943
+ stopStreamParse(ctx, 2, reason);
4534
4944
  }
4535
4945
  async function toCrossJSONAsync(source, options = {}) {
4536
4946
  const plugins = resolvePlugins(options.plugins);
4537
- return await parseTopAsync(createAsyncParserContext(2, {
4947
+ return await parseTopAsync(createAsyncParserContext({
4538
4948
  compactArrayBufferViews: options.compactArrayBufferViews,
4949
+ depthLimit: options.depthLimit,
4539
4950
  plugins,
4540
4951
  disabledFeatures: options.disabledFeatures,
4541
4952
  refs: options.refs
@@ -4557,11 +4968,11 @@ function toCrossJSONStream(source, options) {
4557
4968
  return destroyStreamParse.bind(null, ctx);
4558
4969
  }
4559
4970
  function fromJSON(source, options = {}) {
4560
- var _source$f;
4561
4971
  const plugins = resolvePlugins(options.plugins);
4562
- const disabledFeatures = options.disabledFeatures || 0;
4563
- const sourceFeatures = (_source$f = source.f) !== null && _source$f !== void 0 ? _source$f : 127;
4972
+ const disabledFeatures = options.disabledFeatures ?? 0;
4973
+ const sourceFeatures = source.f ?? 127;
4564
4974
  return deserializeTop(createVanillaDeserializerContext({
4975
+ depthLimit: options.depthLimit,
4565
4976
  maxBase64Length: options.maxBase64Length,
4566
4977
  plugins,
4567
4978
  markedRefs: source.m,
@@ -4976,7 +5387,7 @@ function serializeResult(res, signal, plugins) {
4976
5387
  let initialParsed = false;
4977
5388
  let serializationFailure;
4978
5389
  let disposeSerialization;
4979
- let onParse2 = (value, initial) => {
5390
+ let onParse = (value, initial) => {
4980
5391
  if (serializationFailure) return;
4981
5392
  initialParsed ||= initial;
4982
5393
  const record = encodeSerializationRecord(value);
@@ -4987,10 +5398,10 @@ function serializeResult(res, signal, plugins) {
4987
5398
  initialRecords.push(record);
4988
5399
  initialBytes += record.byteLength;
4989
5400
  };
4990
- let onDone2 = () => {
5401
+ let onDone = () => {
4991
5402
  if (initialParsed) done = true;
4992
5403
  };
4993
- let onError2 = (error) => {
5404
+ let onError = (error) => {
4994
5405
  serializationFailure ??= [error];
4995
5406
  };
4996
5407
  const rawStreamPlugin = createRawStreamRPCPlugin((id, stream) => {
@@ -5001,7 +5412,7 @@ function serializeResult(res, signal, plugins) {
5001
5412
  if (id > 1024) {
5002
5413
  const error = /* @__PURE__ */ new Error(`Too many raw streams in framed response (max ${MAX_FRAMED_STREAMS})`);
5003
5414
  cancelRawStream(stream, error);
5004
- onError2(error);
5415
+ onError(error);
5005
5416
  return;
5006
5417
  }
5007
5418
  pendingRawStreams.push({
@@ -5013,13 +5424,13 @@ function serializeResult(res, signal, plugins) {
5013
5424
  refs: /* @__PURE__ */ new Map(),
5014
5425
  plugins: [rawStreamPlugin, ...plugins],
5015
5426
  onParse(value, initial) {
5016
- onParse2(value, initial);
5427
+ onParse(value, initial);
5017
5428
  },
5018
5429
  onDone() {
5019
- onDone2();
5430
+ onDone();
5020
5431
  },
5021
5432
  onError: (error) => {
5022
- onError2(error);
5433
+ onError(error);
5023
5434
  }
5024
5435
  });
5025
5436
  if (serializationFailure) {
@@ -5084,7 +5495,7 @@ function serializeResult(res, signal, plugins) {
5084
5495
  if (json.byteLength > 16777216 || exceedsPendingSerializationLimit(json, pendingRecords.size, pendingBytes)) {
5085
5496
  const error = /* @__PURE__ */ new Error("Server function serialization exceeded its pending output limit");
5086
5497
  for (const registration of rawStreams) cancelRawStream(registration.stream, error);
5087
- onError2(error);
5498
+ onError(error);
5088
5499
  return false;
5089
5500
  }
5090
5501
  pendingBytes += json.byteLength;
@@ -5103,17 +5514,17 @@ function serializeResult(res, signal, plugins) {
5103
5514
  });
5104
5515
  return true;
5105
5516
  };
5106
- onParse2 = (value) => {
5517
+ onParse = (value) => {
5107
5518
  if (serializationFailure) return;
5108
5519
  writeRecord(encodeSerializationRecord(value), pendingRawStreams.splice(0));
5109
5520
  };
5110
- onDone2 = () => {
5521
+ onDone = () => {
5111
5522
  if (serializationFailure) return;
5112
5523
  disposeSerialization = void 0;
5113
5524
  writer.close().catch(() => {
5114
5525
  });
5115
5526
  };
5116
- onError2 = (error) => {
5527
+ onError = (error) => {
5117
5528
  abortRecordStream(error);
5118
5529
  };
5119
5530
  const initialRawStreams = pendingRawStreams.splice(0);
@@ -5126,7 +5537,7 @@ function serializeResult(res, signal, plugins) {
5126
5537
  }
5127
5538
  }
5128
5539
  initialRecords.length = 0;
5129
- if (done) onDone2();
5540
+ if (done) onDone();
5130
5541
  writer.closed.catch((error) => {
5131
5542
  abortRecordStream(error);
5132
5543
  });
@@ -5722,7 +6133,7 @@ var getBaseManifest = getProdBaseManifest;
5722
6133
  var createEarlyHintsForRequest = createEarlyHintsCollector;
5723
6134
  async function loadEntries() {
5724
6135
  const [routerEntry, startEntry, pluginAdapters] = await Promise.all([
5725
- import("./router-DBc-HYGW-CC6DTCFR.js").then((n) => n.t),
6136
+ import("./router-DJDog9kJ-IHEFPUCH.js").then((n) => n.t),
5726
6137
  import("./start-2iP5edI6-7IIO7TA6.js"),
5727
6138
  import("./empty-plugin-adapters-CVykojMw-NXXTUYZA.js")
5728
6139
  ]);