elysia 2.0.0-beta.3 → 2.0.0-beta.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 (69) hide show
  1. package/dist/adapter/bun/index.d.ts +1 -1
  2. package/dist/adapter/bun/index.js +320 -56
  3. package/dist/adapter/bun/index.mjs +321 -57
  4. package/dist/adapter/utils.js +48 -27
  5. package/dist/adapter/utils.mjs +48 -27
  6. package/dist/base.d.ts +16 -5
  7. package/dist/base.js +171 -74
  8. package/dist/base.mjs +172 -75
  9. package/dist/compile/aot-capture.js +1 -1
  10. package/dist/compile/aot-capture.mjs +1 -1
  11. package/dist/compile/aot.d.ts +1 -0
  12. package/dist/compile/aot.js +3 -0
  13. package/dist/compile/aot.mjs +3 -0
  14. package/dist/compile/handler/frozen-validator.d.ts +4 -1
  15. package/dist/compile/handler/frozen-validator.js +8 -3
  16. package/dist/compile/handler/frozen-validator.mjs +5 -3
  17. package/dist/compile/handler/index.d.ts +1 -1
  18. package/dist/compile/handler/index.js +3 -3
  19. package/dist/compile/handler/index.mjs +3 -3
  20. package/dist/compile/handler/reconstruct.d.ts +1 -1
  21. package/dist/compile/handler/reconstruct.js +4 -3
  22. package/dist/compile/handler/reconstruct.mjs +4 -3
  23. package/dist/compile/jit-probe.d.ts +2 -2
  24. package/dist/context.d.ts +1 -1
  25. package/dist/context.js +5 -1
  26. package/dist/context.mjs +5 -1
  27. package/dist/package.js +1 -1
  28. package/dist/package.mjs +1 -1
  29. package/dist/plugin/aot/core.d.ts +11 -5
  30. package/dist/plugin/aot/core.js +86 -38
  31. package/dist/plugin/aot/core.mjs +87 -41
  32. package/dist/plugin/aot/esbuild.js +8 -2
  33. package/dist/plugin/aot/esbuild.mjs +9 -3
  34. package/dist/plugin/aot/hooks.d.ts +2 -1
  35. package/dist/plugin/aot/hooks.js +3 -3
  36. package/dist/plugin/aot/hooks.mjs +4 -4
  37. package/dist/plugin/aot/source.d.ts +4 -1
  38. package/dist/plugin/aot/source.js +21 -14
  39. package/dist/plugin/aot/source.mjs +21 -14
  40. package/dist/plugin/aot/worker.js +2 -2
  41. package/dist/plugin/aot/worker.mjs +2 -2
  42. package/dist/route-table.d.ts +3 -0
  43. package/dist/route-table.js +4 -1
  44. package/dist/route-table.mjs +4 -1
  45. package/dist/schema-snapshot.js +17 -22
  46. package/dist/schema-snapshot.mjs +17 -22
  47. package/dist/type/bridge-live.js +1 -1
  48. package/dist/type/bridge-live.mjs +1 -1
  49. package/dist/type/coerce.d.ts +1 -1
  50. package/dist/type/constants.d.ts +1 -1
  51. package/dist/type/typebox-value-live.d.ts +2 -2
  52. package/dist/type/typebox-value-live.js +2 -2
  53. package/dist/type/typebox-value-live.mjs +2 -2
  54. package/dist/type/typebox-value.d.ts +6 -6
  55. package/dist/type/validator/index.d.ts +1 -1
  56. package/dist/type/validator/index.js +10 -7
  57. package/dist/type/validator/index.mjs +10 -7
  58. package/dist/type/validator/validator-cache.d.ts +1 -1
  59. package/dist/type/validator/validator-cache.js +17 -9
  60. package/dist/type/validator/validator-cache.mjs +17 -9
  61. package/dist/utils.d.ts +10 -1
  62. package/dist/utils.js +8 -1
  63. package/dist/utils.mjs +6 -1
  64. package/dist/ws/context.d.ts +4 -1
  65. package/dist/ws/context.js +21 -3
  66. package/dist/ws/context.mjs +20 -3
  67. package/dist/ws/route.js +57 -12
  68. package/dist/ws/route.mjs +59 -14
  69. package/package.json +2 -2
package/dist/ws/route.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isBun } from "../universal/constants.mjs";
2
- import { assignOwn, deriveEntryFn, isNotEmpty, nullObject } from "../utils.mjs";
2
+ import { assignOwn, deriveEntryFn, isNotEmpty, isSocketQuiet, nullObject } from "../utils.mjs";
3
3
  import { isBridgeLive } from "../type/bridge.mjs";
4
4
  import { ElysiaError, ElysiaStatus, ValidationError, internalServerErrorBodyString, problemBody, problemResponse } from "../error.mjs";
5
5
  import { mapResponse } from "../adapter/web-standard/handler.mjs";
@@ -13,7 +13,7 @@ import { StandardValidator } from "../validator/index.mjs";
13
13
  import { RouteValidator } from "../validator/route.mjs";
14
14
  import { buildFrozenRouteValidator, isBridgeNotInitialized } from "../compile/handler/frozen-validator.mjs";
15
15
  import { composeRouteHook, localMacroRoot, resolveWSLocalHook } from "../compile/handler/index.mjs";
16
- import { ElysiaWS, isGeneratorObject } from "./context.mjs";
16
+ import { ElysiaWS, isGeneratorObject, trackWSSettling } from "./context.mjs";
17
17
  import { createMessageParser } from "./parser.mjs";
18
18
 
19
19
  //#region src/ws/route.ts
@@ -569,6 +569,8 @@ function accumulateWSOptions(target, routeOptions, _path) {
569
569
  }
570
570
  const MAX_INFLIGHT_MESSAGES = 256;
571
571
  function buildGlobalWSHandler() {
572
+ let lifecycle;
573
+ const runLifecycle = (callback, ...args) => lifecycle ? lifecycle.run(callback, ...args) : callback(...args);
572
574
  function getElysia(ws) {
573
575
  let elysia = ws.data.elysia;
574
576
  if (!elysia) {
@@ -583,9 +585,19 @@ function buildGlobalWSHandler() {
583
585
  ws.send(typeof error?.message === "string" ? error.message : "Internal Server Error");
584
586
  } catch {}
585
587
  };
588
+ const releaseLifecycle = (ws) => {
589
+ if (!lifecycle?.closing && isSocketQuiet(ws)) {
590
+ delete ws.data["~lifecycleRun"];
591
+ lifecycle?.sockets.delete(ws);
592
+ }
593
+ };
586
594
  const release = (ws) => {
587
- const inflight = ws.data.inflight;
588
- if (inflight) ws.data.inflight = inflight - 1;
595
+ const data = ws.data;
596
+ const inflight = data.inflight;
597
+ if (inflight) {
598
+ data.inflight = inflight - 1;
599
+ if (inflight === 1 && data.closeHandlerInvoked) releaseLifecycle(ws);
600
+ }
589
601
  };
590
602
  function dispatch(ws, message) {
591
603
  let result;
@@ -602,8 +614,9 @@ function buildGlobalWSHandler() {
602
614
  });
603
615
  else release(ws);
604
616
  }
605
- return {
617
+ const handler = {
606
618
  message(ws, message) {
619
+ if (lifecycle?.closing) return;
607
620
  const data = ws.data;
608
621
  if (!data.message) return;
609
622
  const inflight = (data.inflight ?? 0) + 1;
@@ -616,41 +629,73 @@ function buildGlobalWSHandler() {
616
629
  data.inflight = inflight;
617
630
  const opening = data.opening;
618
631
  if (opening) return void opening.then(() => {
619
- if (ws.readyState > 1) return release(ws);
632
+ if (lifecycle?.closing || ws.readyState > 1) return release(ws);
620
633
  dispatch(ws, message);
621
634
  });
622
635
  dispatch(ws, message);
623
636
  },
624
637
  open(ws) {
625
- const result = ws.data.open?.(getElysia(ws));
638
+ if (lifecycle) Object.defineProperty(ws.data, "~lifecycleRun", {
639
+ configurable: true,
640
+ value: lifecycle.run
641
+ });
642
+ lifecycle?.sockets.add(ws);
643
+ if (lifecycle?.closing) {
644
+ ws.data.closeHandlerInvoked = true;
645
+ try {
646
+ ws.terminate();
647
+ } catch {}
648
+ return;
649
+ }
650
+ const open = ws.data.open;
651
+ const result = open && runLifecycle(open, getElysia(ws));
626
652
  if (result instanceof Promise) {
627
653
  const clear = () => {
628
654
  ws.data.opening = void 0;
655
+ releaseLifecycle(ws);
629
656
  };
630
657
  ws.data.opening = result.then(clear, clear);
631
658
  }
632
659
  },
633
660
  drain(ws) {
661
+ if (lifecycle?.closing) return;
634
662
  const elyWs = getElysia(ws);
635
663
  drainWaiters(elyWs);
636
- ws.data.drain?.(elyWs);
664
+ const drain = ws.data.drain;
665
+ trackWSSettling(ws.data, drain && runLifecycle(drain, elyWs), () => releaseLifecycle(ws));
637
666
  },
638
667
  close(ws, code, reason) {
639
668
  const elyWs = getElysia(ws);
640
669
  drainWaiters(elyWs);
641
- ws.data.opening = void 0;
642
- if (ws.data.closeHandlerInvoked) return;
670
+ if (ws.data.closeHandlerInvoked) return releaseLifecycle(ws);
643
671
  ws.data.closeHandlerInvoked = true;
644
- const result = ws.data.close?.(elyWs, code, reason);
645
- if (result instanceof Promise) result.catch(() => {});
672
+ let result;
673
+ try {
674
+ const close = ws.data.close;
675
+ result = close && runLifecycle(close, elyWs, code, reason);
676
+ } finally {
677
+ if (!(result instanceof Promise)) releaseLifecycle(ws);
678
+ }
679
+ if (result instanceof Promise) {
680
+ const closing = result.catch(() => {});
681
+ trackWSSettling(ws.data, closing, () => releaseLifecycle(ws));
682
+ }
646
683
  },
647
684
  ping(ws, data) {
648
- ws.data.ping?.(getElysia(ws), data);
685
+ if (lifecycle?.closing) return;
686
+ const ping = ws.data.ping;
687
+ trackWSSettling(ws.data, ping && runLifecycle(ping, getElysia(ws), data), () => releaseLifecycle(ws));
649
688
  },
650
689
  pong(ws, data) {
651
- ws.data.pong?.(getElysia(ws), data);
690
+ if (lifecycle?.closing) return;
691
+ const pong = ws.data.pong;
692
+ trackWSSettling(ws.data, pong && runLifecycle(pong, getElysia(ws), data), () => releaseLifecycle(ws));
652
693
  }
653
694
  };
695
+ Object.defineProperty(handler, "~lifecycle", { set(value) {
696
+ lifecycle = value;
697
+ } });
698
+ return handler;
654
699
  }
655
700
 
656
701
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "elysia",
3
3
  "description": "Ergonomic Framework for Human",
4
- "version": "2.0.0-beta.3",
4
+ "version": "2.0.0-beta.4",
5
5
  "author": {
6
6
  "name": "saltyAom",
7
7
  "url": "https://github.com/SaltyAom",
@@ -244,7 +244,7 @@
244
244
  ],
245
245
  "dependencies": {
246
246
  "deuri": "^3.0.0",
247
- "memoirist": "1.2.1"
247
+ "memoirist": "1.2.2"
248
248
  },
249
249
  "devDependencies": {
250
250
  "@eslint/eslintrc": "^3.3.5",