elysia 2.0.0-beta.2 → 2.0.0-beta.21

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 (339) hide show
  1. package/dist/adapter/bun/index.d.ts +18 -2
  2. package/dist/adapter/bun/index.js +467 -110
  3. package/dist/adapter/bun/index.mjs +467 -112
  4. package/dist/adapter/types.d.ts +2 -21
  5. package/dist/adapter/utils.d.ts +14 -3
  6. package/dist/adapter/utils.js +146 -87
  7. package/dist/adapter/utils.mjs +148 -90
  8. package/dist/adapter/web-standard/handler.d.ts +1 -1
  9. package/dist/adapter/web-standard/handler.js +47 -36
  10. package/dist/adapter/web-standard/handler.mjs +48 -37
  11. package/dist/adapter/web-standard/index.js +2 -30
  12. package/dist/adapter/web-standard/index.mjs +2 -30
  13. package/dist/adapter/web-standard/utils.js +5 -18
  14. package/dist/adapter/web-standard/utils.mjs +5 -18
  15. package/dist/base.d.ts +99 -67
  16. package/dist/base.js +678 -510
  17. package/dist/base.mjs +682 -514
  18. package/dist/compile/aot-capture.d.ts +2 -13
  19. package/dist/compile/aot-capture.js +26 -69
  20. package/dist/compile/aot-capture.mjs +28 -67
  21. package/dist/compile/aot-emit.d.ts +9 -7
  22. package/dist/compile/aot-emit.js +42 -19
  23. package/dist/compile/aot-emit.mjs +42 -20
  24. package/dist/compile/aot.d.ts +17 -19
  25. package/dist/compile/aot.js +41 -51
  26. package/dist/compile/aot.mjs +39 -49
  27. package/dist/compile/handler/descriptor.d.ts +9 -12
  28. package/dist/compile/handler/descriptor.js +19 -44
  29. package/dist/compile/handler/descriptor.mjs +20 -42
  30. package/dist/compile/handler/frozen-validator.d.ts +5 -10
  31. package/dist/compile/handler/frozen-validator.js +55 -105
  32. package/dist/compile/handler/frozen-validator.mjs +53 -105
  33. package/dist/compile/handler/index.d.ts +7 -5
  34. package/dist/compile/handler/index.js +270 -111
  35. package/dist/compile/handler/index.mjs +270 -113
  36. package/dist/compile/handler/jit.d.ts +19 -17
  37. package/dist/compile/handler/jit.js +222 -149
  38. package/dist/compile/handler/jit.mjs +224 -152
  39. package/dist/compile/handler/params.d.ts +1 -4
  40. package/dist/compile/handler/params.js +10 -5
  41. package/dist/compile/handler/params.mjs +14 -8
  42. package/dist/compile/handler/reconstruct.d.ts +2 -2
  43. package/dist/compile/handler/reconstruct.js +6 -4
  44. package/dist/compile/handler/reconstruct.mjs +6 -4
  45. package/dist/compile/handler/static-clone-resolver.d.ts +6 -0
  46. package/dist/compile/handler/static-clone-resolver.js +8 -0
  47. package/dist/compile/handler/static-clone-resolver.mjs +7 -0
  48. package/dist/compile/handler/utils.d.ts +22 -11
  49. package/dist/compile/handler/utils.js +159 -253
  50. package/dist/compile/handler/utils.mjs +155 -252
  51. package/dist/compile/jit-probe.d.ts +2 -2
  52. package/dist/compile/jit-probe.js +2 -3
  53. package/dist/compile/jit-probe.mjs +2 -3
  54. package/dist/compile/lexer.d.ts +7 -4
  55. package/dist/compile/lexer.js +268 -6
  56. package/dist/compile/lexer.mjs +268 -4
  57. package/dist/compile/utils.js +7 -4
  58. package/dist/compile/utils.mjs +7 -4
  59. package/dist/compiled.d.ts +0 -1
  60. package/dist/compiled.js +0 -1
  61. package/dist/compiled.mjs +0 -1
  62. package/dist/context.d.ts +14 -4
  63. package/dist/context.js +17 -32
  64. package/dist/context.mjs +19 -34
  65. package/dist/cookie/config.d.ts +7 -2
  66. package/dist/cookie/config.js +76 -16
  67. package/dist/cookie/config.mjs +77 -18
  68. package/dist/cookie/cookie.d.ts +1 -1
  69. package/dist/cookie/cookie.js +8 -10
  70. package/dist/cookie/cookie.mjs +8 -10
  71. package/dist/cookie/crypto.d.ts +8 -7
  72. package/dist/cookie/crypto.js +51 -40
  73. package/dist/cookie/crypto.mjs +51 -41
  74. package/dist/cookie/error.d.ts +1 -2
  75. package/dist/cookie/error.js +6 -4
  76. package/dist/cookie/error.mjs +6 -4
  77. package/dist/cookie/lib.js +7 -2
  78. package/dist/cookie/lib.mjs +7 -2
  79. package/dist/cookie/serialize.js +1 -1
  80. package/dist/cookie/serialize.mjs +1 -1
  81. package/dist/cookie/types.d.ts +10 -0
  82. package/dist/cookie/utils.d.ts +4 -8
  83. package/dist/cookie/utils.js +86 -52
  84. package/dist/cookie/utils.mjs +89 -54
  85. package/dist/error.d.ts +113 -103
  86. package/dist/error.js +181 -117
  87. package/dist/error.mjs +179 -118
  88. package/dist/generation.d.ts +2 -2
  89. package/dist/handler/error.d.ts +26 -15
  90. package/dist/handler/error.js +77 -96
  91. package/dist/handler/error.mjs +74 -97
  92. package/dist/handler/fetch.js +87 -108
  93. package/dist/handler/fetch.mjs +89 -110
  94. package/dist/handler/utils.d.ts +12 -6
  95. package/dist/handler/utils.js +84 -28
  96. package/dist/handler/utils.mjs +80 -28
  97. package/dist/index.d.ts +12 -12
  98. package/dist/index.js +2 -9
  99. package/dist/index.mjs +1 -1
  100. package/dist/memory.js +1 -1
  101. package/dist/memory.mjs +1 -1
  102. package/dist/package.js +1 -1
  103. package/dist/package.mjs +1 -1
  104. package/dist/plugin/aot/bun.d.ts +8 -1
  105. package/dist/plugin/aot/bun.js +16 -9
  106. package/dist/plugin/aot/bun.mjs +17 -10
  107. package/dist/plugin/aot/core.d.ts +52 -60
  108. package/dist/plugin/aot/core.js +208 -109
  109. package/dist/plugin/aot/core.mjs +205 -111
  110. package/dist/plugin/aot/esbuild.js +8 -5
  111. package/dist/plugin/aot/esbuild.mjs +9 -6
  112. package/dist/plugin/aot/hooks.d.ts +6 -17
  113. package/dist/plugin/aot/hooks.js +36 -14
  114. package/dist/plugin/aot/hooks.mjs +37 -16
  115. package/dist/plugin/aot/rspack-loader.d.ts +1 -5
  116. package/dist/plugin/aot/rspack-loader.js +1 -10
  117. package/dist/plugin/aot/rspack-loader.mjs +1 -10
  118. package/dist/plugin/aot/rspack.js +2 -5
  119. package/dist/plugin/aot/rspack.mjs +3 -6
  120. package/dist/plugin/aot/source.d.ts +6 -42
  121. package/dist/plugin/aot/source.js +71 -71
  122. package/dist/plugin/aot/source.mjs +72 -72
  123. package/dist/plugin/aot/treeshake.js +2 -6
  124. package/dist/plugin/aot/treeshake.mjs +2 -6
  125. package/dist/plugin/aot/unplugin.d.ts +1 -4
  126. package/dist/plugin/aot/unplugin.js +4 -13
  127. package/dist/plugin/aot/unplugin.mjs +4 -13
  128. package/dist/plugin/aot/vite.d.ts +2 -6
  129. package/dist/plugin/aot/worker.js +2 -2
  130. package/dist/plugin/aot/worker.mjs +2 -2
  131. package/dist/plugin/auto-head.js +1 -10
  132. package/dist/plugin/auto-head.mjs +1 -10
  133. package/dist/plugin/trace.d.ts +2 -1
  134. package/dist/route-table.d.ts +3 -0
  135. package/dist/route-table.js +16 -11
  136. package/dist/route-table.mjs +16 -11
  137. package/dist/schema-snapshot.d.ts +0 -4
  138. package/dist/schema-snapshot.js +32 -49
  139. package/dist/schema-snapshot.mjs +32 -49
  140. package/dist/sucrose.d.ts +6 -55
  141. package/dist/sucrose.js +103 -731
  142. package/dist/sucrose.mjs +102 -724
  143. package/dist/trace.d.ts +5 -8
  144. package/dist/trace.js +23 -134
  145. package/dist/trace.mjs +24 -135
  146. package/dist/type/bridge-live.d.ts +4 -2
  147. package/dist/type/bridge-live.js +6 -3
  148. package/dist/type/bridge-live.mjs +4 -2
  149. package/dist/type/bridge.d.ts +6 -4
  150. package/dist/type/bridge.js +17 -9
  151. package/dist/type/bridge.mjs +5 -3
  152. package/dist/type/coerce-plan.d.ts +5 -5
  153. package/dist/type/coerce-plan.js +41 -39
  154. package/dist/type/coerce-plan.mjs +43 -39
  155. package/dist/type/coerce.d.ts +3 -7
  156. package/dist/type/coerce.js +18 -75
  157. package/dist/type/coerce.mjs +19 -76
  158. package/dist/type/compat.d.ts +3 -0
  159. package/dist/type/compat.js +23 -14
  160. package/dist/type/compat.mjs +20 -10
  161. package/dist/type/elysia/array-buffer.d.ts +4 -1
  162. package/dist/type/elysia/array-buffer.js +17 -17
  163. package/dist/type/elysia/array-buffer.mjs +17 -17
  164. package/dist/type/elysia/array-string.js +2 -40
  165. package/dist/type/elysia/array-string.mjs +2 -39
  166. package/dist/type/elysia/array.js +5 -9
  167. package/dist/type/elysia/array.mjs +5 -9
  168. package/dist/type/elysia/boolean-string.js +2 -3
  169. package/dist/type/elysia/boolean-string.mjs +1 -1
  170. package/dist/type/elysia/boolean.js +3 -12
  171. package/dist/type/elysia/boolean.mjs +3 -11
  172. package/dist/type/elysia/cookie.d.ts +3 -4
  173. package/dist/type/elysia/cookie.js +5 -15
  174. package/dist/type/elysia/cookie.mjs +5 -15
  175. package/dist/type/elysia/date.js +13 -20
  176. package/dist/type/elysia/date.mjs +10 -16
  177. package/dist/type/elysia/file-type.d.ts +11 -4
  178. package/dist/type/elysia/file-type.js +17 -2
  179. package/dist/type/elysia/file-type.mjs +17 -3
  180. package/dist/type/elysia/file.d.ts +1 -3
  181. package/dist/type/elysia/file.js +11 -27
  182. package/dist/type/elysia/file.mjs +11 -12
  183. package/dist/type/elysia/files.js +7 -11
  184. package/dist/type/elysia/files.mjs +7 -10
  185. package/dist/type/elysia/form.js +2 -3
  186. package/dist/type/elysia/form.mjs +3 -3
  187. package/dist/type/elysia/integer-string.d.ts +1 -1
  188. package/dist/type/elysia/integer-string.js +4 -30
  189. package/dist/type/elysia/integer-string.mjs +5 -30
  190. package/dist/type/elysia/integer.js +3 -12
  191. package/dist/type/elysia/integer.mjs +3 -11
  192. package/dist/type/elysia/intersect.js +0 -5
  193. package/dist/type/elysia/intersect.mjs +0 -5
  194. package/dist/type/elysia/json-string.d.ts +7 -0
  195. package/dist/type/elysia/json-string.js +47 -0
  196. package/dist/type/elysia/json-string.mjs +46 -0
  197. package/dist/type/elysia/keep-config.d.ts +14 -0
  198. package/dist/type/elysia/keep-config.js +62 -0
  199. package/dist/type/elysia/keep-config.mjs +53 -0
  200. package/dist/type/elysia/maybe-empty.js +3 -4
  201. package/dist/type/elysia/maybe-empty.mjs +1 -1
  202. package/dist/type/elysia/nullable.js +2 -3
  203. package/dist/type/elysia/nullable.mjs +1 -1
  204. package/dist/type/elysia/number.d.ts +2 -1
  205. package/dist/type/elysia/number.js +19 -11
  206. package/dist/type/elysia/number.mjs +18 -10
  207. package/dist/type/elysia/numeric-enum.d.ts +1 -1
  208. package/dist/type/elysia/numeric-enum.js +4 -5
  209. package/dist/type/elysia/numeric-enum.mjs +3 -3
  210. package/dist/type/elysia/numeric.d.ts +4 -2
  211. package/dist/type/elysia/numeric.js +20 -17
  212. package/dist/type/elysia/numeric.mjs +19 -16
  213. package/dist/type/elysia/object-string.js +2 -37
  214. package/dist/type/elysia/object-string.mjs +2 -36
  215. package/dist/type/elysia/object.js +5 -9
  216. package/dist/type/elysia/object.mjs +5 -9
  217. package/dist/type/elysia/optional.js +8 -8
  218. package/dist/type/elysia/optional.mjs +7 -7
  219. package/dist/type/elysia/problem.d.ts +20 -0
  220. package/dist/type/elysia/problem.js +25 -0
  221. package/dist/type/elysia/problem.mjs +24 -0
  222. package/dist/type/elysia/string.js +4 -3
  223. package/dist/type/elysia/string.mjs +4 -2
  224. package/dist/type/elysia/uint8-array.d.ts +1 -1
  225. package/dist/type/elysia/uint8-array.js +5 -20
  226. package/dist/type/elysia/uint8-array.mjs +5 -19
  227. package/dist/type/elysia/union-enum.js +7 -8
  228. package/dist/type/elysia/union-enum.mjs +6 -6
  229. package/dist/type/elysia/union.js +0 -5
  230. package/dist/type/elysia/union.mjs +0 -5
  231. package/dist/type/elysia/utils.d.ts +2 -4
  232. package/dist/type/elysia/utils.js +10 -21
  233. package/dist/type/elysia/utils.mjs +7 -15
  234. package/dist/type/exports.d.ts +4 -2
  235. package/dist/type/exports.js +23 -2
  236. package/dist/type/exports.mjs +11 -2
  237. package/dist/type/index.d.ts +10 -2
  238. package/dist/type/index.js +62 -15
  239. package/dist/type/index.mjs +65 -11
  240. package/dist/type/shared.d.ts +3 -9
  241. package/dist/type/shared.js +44 -97
  242. package/dist/type/shared.mjs +43 -97
  243. package/dist/type/sync-require.d.ts +11 -0
  244. package/dist/type/sync-require.js +14 -0
  245. package/dist/type/sync-require.mjs +12 -0
  246. package/dist/type/typebox-system-lite.d.ts +2 -0
  247. package/dist/type/typebox-system-lite.js +34 -0
  248. package/dist/type/typebox-system-lite.mjs +3 -0
  249. package/dist/type/typebox-system-require.d.ts +7 -0
  250. package/dist/type/typebox-system-require.js +24 -0
  251. package/dist/type/typebox-system-require.mjs +23 -0
  252. package/dist/type/typebox-type-live.d.ts +11 -0
  253. package/dist/type/typebox-type-live.js +45 -0
  254. package/dist/type/typebox-type-live.mjs +20 -0
  255. package/dist/type/typebox-type.d.ts +34 -0
  256. package/dist/type/typebox-type.js +199 -0
  257. package/dist/type/typebox-type.mjs +86 -0
  258. package/dist/type/typebox-value-live.d.ts +11 -0
  259. package/dist/type/typebox-value-live.js +31 -0
  260. package/dist/type/typebox-value-live.mjs +13 -0
  261. package/dist/type/typebox-value-ops.d.ts +4 -0
  262. package/dist/type/typebox-value-ops.js +90 -0
  263. package/dist/type/typebox-value-ops.mjs +5 -0
  264. package/dist/type/typebox-value-require.d.ts +8 -0
  265. package/dist/type/typebox-value-require.js +28 -0
  266. package/dist/type/typebox-value-require.mjs +25 -0
  267. package/dist/type/typebox-value.d.ts +35 -0
  268. package/dist/type/typebox-value.js +161 -0
  269. package/dist/type/typebox-value.mjs +74 -0
  270. package/dist/type/utils.js +1 -10
  271. package/dist/type/utils.mjs +1 -10
  272. package/dist/type/validator/clean-safe.d.ts +2 -4
  273. package/dist/type/validator/clean-safe.js +15 -11
  274. package/dist/type/validator/clean-safe.mjs +16 -10
  275. package/dist/type/validator/custom-error.js +12 -16
  276. package/dist/type/validator/custom-error.mjs +11 -14
  277. package/dist/type/validator/default-precompute.d.ts +9 -1
  278. package/dist/type/validator/default-precompute.js +40 -77
  279. package/dist/type/validator/default-precompute.mjs +37 -74
  280. package/dist/type/validator/exact-mirror-live.d.ts +8 -0
  281. package/dist/type/validator/exact-mirror-live.js +22 -0
  282. package/dist/type/validator/exact-mirror-live.mjs +17 -0
  283. package/dist/type/validator/exact-mirror-require.d.ts +4 -0
  284. package/dist/type/validator/exact-mirror-require.js +11 -0
  285. package/dist/type/validator/exact-mirror-require.mjs +9 -0
  286. package/dist/type/validator/exact-mirror.js +8 -8
  287. package/dist/type/validator/exact-mirror.mjs +9 -8
  288. package/dist/type/validator/frozen-check.js +5 -6
  289. package/dist/type/validator/frozen-check.mjs +6 -7
  290. package/dist/type/validator/index.d.ts +1 -0
  291. package/dist/type/validator/index.js +169 -166
  292. package/dist/type/validator/index.mjs +162 -158
  293. package/dist/type/validator/string-codec-aot.d.ts +2 -1
  294. package/dist/type/validator/string-codec-aot.js +8 -2
  295. package/dist/type/validator/string-codec-aot.mjs +7 -2
  296. package/dist/type/validator/validator-cache.d.ts +5 -4
  297. package/dist/type/validator/validator-cache.js +41 -24
  298. package/dist/type/validator/validator-cache.mjs +40 -22
  299. package/dist/types.d.ts +263 -95
  300. package/dist/universal/constants.d.ts +2 -1
  301. package/dist/universal/constants.js +3 -1
  302. package/dist/universal/constants.mjs +2 -1
  303. package/dist/universal/env.js +2 -3
  304. package/dist/universal/env.mjs +2 -3
  305. package/dist/universal/file.d.ts +6 -4
  306. package/dist/universal/file.js +11 -16
  307. package/dist/universal/file.mjs +12 -16
  308. package/dist/universal/index.d.ts +2 -2
  309. package/dist/universal/is-production.d.ts +1 -2
  310. package/dist/universal/is-production.js +1 -3
  311. package/dist/universal/is-production.mjs +2 -3
  312. package/dist/universal/server.d.ts +0 -272
  313. package/dist/utils.d.ts +43 -7
  314. package/dist/utils.js +181 -126
  315. package/dist/utils.mjs +168 -126
  316. package/dist/validator/index.d.ts +1 -1
  317. package/dist/validator/index.js +17 -30
  318. package/dist/validator/index.mjs +18 -31
  319. package/dist/validator/route.js +2 -1
  320. package/dist/validator/route.mjs +2 -1
  321. package/dist/ws/context.d.ts +8 -1
  322. package/dist/ws/context.js +68 -44
  323. package/dist/ws/context.mjs +67 -44
  324. package/dist/ws/parser.js +10 -20
  325. package/dist/ws/parser.mjs +10 -20
  326. package/dist/ws/route.d.ts +2 -2
  327. package/dist/ws/route.js +249 -141
  328. package/dist/ws/route.mjs +254 -146
  329. package/dist/ws/types.d.ts +2 -2
  330. package/package.json +23 -49
  331. package/dist/compile/analysis-cache.d.ts +0 -6
  332. package/dist/compile/analysis-cache.js +0 -12
  333. package/dist/compile/analysis-cache.mjs +0 -11
  334. package/dist/cookie/index.d.ts +0 -7
  335. package/dist/cookie/index.js +0 -13
  336. package/dist/cookie/index.mjs +0 -7
  337. package/dist/handler/index.d.ts +0 -2
  338. package/dist/handler/index.js +0 -5
  339. package/dist/handler/index.mjs +0 -3
package/dist/ws/route.js CHANGED
@@ -2,13 +2,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_universal_constants = require('../universal/constants.js');
3
3
  const require_utils = require('../utils.js');
4
4
  const require_type_bridge = require('../type/bridge.js');
5
- const require_universal_is_production = require('../universal/is-production.js');
6
5
  const require_error = require('../error.js');
6
+ const require_cookie_crypto = require('../cookie/crypto.js');
7
7
  const require_adapter_web_standard_handler = require('../adapter/web-standard/handler.js');
8
8
  const require_parse_query = require('../parse-query.js');
9
+ const require_compile_aot = require('../compile/aot.js');
9
10
  const require_generation = require('../generation.js');
11
+ const require_cookie_config = require('../cookie/config.js');
12
+ const require_cookie_utils = require('../cookie/utils.js');
13
+ const require_handler_utils = require('../handler/utils.js');
10
14
  const require_handler_error = require('../handler/error.js');
11
15
  const require_compile_handler_utils = require('../compile/handler/utils.js');
16
+ const require_sucrose = require('../sucrose.js');
12
17
  const require_validator_index = require('../validator/index.js');
13
18
  const require_validator_route = require('../validator/route.js');
14
19
  const require_compile_handler_frozen_validator = require('../compile/handler/frozen-validator.js');
@@ -18,72 +23,6 @@ const require_ws_parser = require('./parser.js');
18
23
 
19
24
  //#region src/ws/route.ts
20
25
  const EMPTY_HOOKS = Object.freeze([]);
21
- /**
22
- * Build-time (route-registration-time) analysis of a WS `message` handler to
23
- * decide whether the per-frame `ws.body` view must be assigned.
24
- */
25
- function handlerMayTouchBody(fn) {
26
- if (!fn) return false;
27
- let source;
28
- try {
29
- source = Function.prototype.toString.call(fn);
30
- } catch {
31
- return true;
32
- }
33
- if (source.indexOf("[native code]") !== -1) return true;
34
- if (/\bbody\b/.test(source)) return true;
35
- if (/\barguments\b/.test(source)) return true;
36
- if (source.indexOf("[") !== -1) return true;
37
- if (source.indexOf("...") !== -1) return true;
38
- const parsed = firstParamIdentifier(source);
39
- if (parsed === void 0) return true;
40
- const { name: wsName, bodyStart, paramsEnd } = parsed;
41
- if (source.slice(0, paramsEnd).indexOf("(", 1) !== -1) return true;
42
- const body = source.slice(bodyStart);
43
- const safeName = wsName.replace(/[$]/g, "\\$&");
44
- if (new RegExp(`(?<![\\w$.])${safeName}(?![\\w$]|\\s*\\.)`).test(body)) return true;
45
- return false;
46
- }
47
- function firstParamIdentifier(source) {
48
- const open = source.indexOf("(");
49
- if (open === -1) {
50
- const m = /^\s*(?:async\s+)?([A-Za-z_$][\w$]*)\s*=>/.exec(source);
51
- if (!m) return void 0;
52
- const end = m.index + m[0].length;
53
- return {
54
- name: m[1],
55
- bodyStart: end,
56
- paramsEnd: end
57
- };
58
- }
59
- let depth = 0;
60
- let close = -1;
61
- for (let i = open; i < source.length; i++) {
62
- const c = source[i];
63
- if (c === "(") depth++;
64
- else if (c === ")") {
65
- depth--;
66
- if (depth === 0) {
67
- close = i;
68
- break;
69
- }
70
- }
71
- }
72
- if (close === -1) return void 0;
73
- const params = source.slice(open + 1, close).trim();
74
- if (params.length === 0) return void 0;
75
- let first = params;
76
- const comma = params.indexOf(",");
77
- if (comma !== -1) first = params.slice(0, comma).trim();
78
- if (first[0] === "{" || first[0] === "[") return void 0;
79
- const m = /^([A-Za-z_$][\w$]*)/.exec(first);
80
- if (!m) return void 0;
81
- return {
82
- name: m[1],
83
- bodyStart: close + 1,
84
- paramsEnd: close + 1
85
- };
86
- }
87
26
  function concatHooks(...sources) {
88
27
  let result;
89
28
  for (let i = 0; i < sources.length; i++) {
@@ -104,7 +43,6 @@ async function applyMapResponse(ws, value, mapResponses) {
104
43
  }
105
44
  return value;
106
45
  }
107
- const isBackpressured = (status) => typeof status === "number" && status <= 0;
108
46
  const isEmptyPayload = (payload) => {
109
47
  if (payload === "" || payload == null) return true;
110
48
  if (typeof payload === "string") return false;
@@ -136,7 +74,7 @@ async function handleWSResponse(ws, value, mapResponses) {
136
74
  const mapped = mapResponses.length ? await applyMapResponse(ws, yielded, mapResponses) : yielded;
137
75
  let status = ws.send(mapped);
138
76
  const canBackpressure = !isEmptyPayload(mapped);
139
- while (canBackpressure && isBackpressured(status)) {
77
+ while (canBackpressure && typeof status === "number" && status <= 0) {
140
78
  if (ws.readyState >= 2) return;
141
79
  await waitForDrain(ws);
142
80
  if (ws.readyState >= 2) return;
@@ -155,6 +93,38 @@ async function handleWSResponse(ws, value, mapResponses) {
155
93
  const mapped = mapResponses.length ? await applyMapResponse(ws, value, mapResponses) : value;
156
94
  ws.send(mapped);
157
95
  }
96
+ function wsErrorFrameFallback(error) {
97
+ if (require_handler_error.claimsProblemType(error)) try {
98
+ const status = require_handler_error.resolveStatus(error.status);
99
+ const served = typeof status === "number" && status >= 100 ? status : 500;
100
+ const stringify = (value) => typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
101
+ const problemFrame = (detail) => JSON.stringify(require_error.problemBody({
102
+ type: error.type ?? "about:blank",
103
+ ...typeof error.code === "string" ? { code: error.code } : {},
104
+ detail,
105
+ status: served
106
+ }));
107
+ const serveMessage = () => problemFrame(require_handler_error.statusFallbackBody(error, served));
108
+ const serveDetail = () => {
109
+ const detail = require_handler_error.readAnnotation(error, "detail", true);
110
+ if (detail === void 0) return serveMessage();
111
+ if (detail instanceof Promise) return detail.then((resolved) => resolved === void 0 ? serveMessage() : problemFrame(resolved)).catch(() => wsLegacyFrame(error));
112
+ return problemFrame(detail);
113
+ };
114
+ const value = require_handler_error.readAnnotation(error, "value", true);
115
+ if (value === void 0) return serveDetail();
116
+ if (value instanceof Promise) return value.then((resolved) => resolved === void 0 ? serveDetail() : stringify(resolved)).catch(() => wsLegacyFrame(error));
117
+ return stringify(value);
118
+ } catch {}
119
+ return wsLegacyFrame(error);
120
+ }
121
+ function wsLegacyFrame(error) {
122
+ if (error?.status) {
123
+ const body = require_handler_error.statusFallbackBody(error, error.status);
124
+ return typeof body === "object" ? JSON.stringify(body) : String(body);
125
+ }
126
+ return require_error.internalServerErrorBodyString(error);
127
+ }
158
128
  function wsErrorFrame(error) {
159
129
  if (error instanceof require_error.ValidationError) try {
160
130
  return JSON.stringify(error.payload);
@@ -167,12 +137,7 @@ function wsErrorFrame(error) {
167
137
  } catch {}
168
138
  if (typeof error?.toResponse === "function") {
169
139
  if (error instanceof require_error.ElysiaError && error.toResponse === require_error.ElysiaError.prototype.toResponse) try {
170
- return JSON.stringify(require_error.problemBody({
171
- type: error.problemType,
172
- title: error.problemTitle,
173
- status: error.status ?? 500,
174
- detail: error.response !== void 0 && error.response !== error.problemTitle ? error.response : void 0
175
- }));
140
+ return JSON.stringify(require_error.problemBody(require_error.elysiaErrorProblem(error)));
176
141
  } catch {}
177
142
  try {
178
143
  const r = error.toResponse();
@@ -181,13 +146,6 @@ function wsErrorFrame(error) {
181
146
  }
182
147
  return wsErrorFrameFallback(error);
183
148
  }
184
- function wsErrorFrameFallback(error) {
185
- if (error?.status) {
186
- const body = error.response !== void 0 ? error.response : require_universal_is_production.isProduction() ? error.status >= 500 ? "Internal Server Error" : "" : error.message ?? "";
187
- return typeof body === "object" ? JSON.stringify(body) : String(body);
188
- }
189
- return require_error.internalServerErrorBodyString(error);
190
- }
191
149
  function sendErrorFrame(ws, error) {
192
150
  const frame = wsErrorFrame(error);
193
151
  if (typeof frame === "string") {
@@ -203,9 +161,7 @@ function sendErrorFrame(ws, error) {
203
161
  }, () => {});
204
162
  }
205
163
  function validateUpgradeChannel(validator, value, type) {
206
- if (validator instanceof require_validator_index.StandardValidator) return validator.From(value, type);
207
- if (validator.hasCodec) return validator.From(value, type);
208
- return validator.EncodeFrom(value, type);
164
+ return validator.hasCodec || validator instanceof require_validator_index.StandardValidator ? validator.From(value, type) : validator.EncodeFrom(value, type);
209
165
  }
210
166
  const wsOptions = [
211
167
  "maxPayloadLength",
@@ -218,14 +174,35 @@ const wsOptions = [
218
174
  ];
219
175
  function buildWSRoute(route, app) {
220
176
  const hook = require_compile_handler_index.resolveWSLocalHook(require_compile_handler_index.localMacroRoot(route[7] ?? route[3] ?? app, app), route[4], app) ?? require_utils.nullObject();
177
+ const instance = route[3] ?? app;
178
+ const appHookChain = route[5];
179
+ const inheritedChain = route[6];
180
+ const composed = require_compile_handler_index.composeRouteHook(instance, route[4], appHookChain, inheritedChain, app, route[7]) ?? hook;
221
181
  let validators;
222
- const frozenEager = require_type_bridge.isBridgeLive() ? void 0 : require_compile_handler_frozen_validator.buildFrozenRouteValidator(hook, app, "WS", route[1]);
182
+ const programId = require_generation.frozenRootOf(app)["~programId"];
183
+ const captured = require_compile_aot.Compiled.getHandler(programId, "WS", route[1]);
184
+ let live = false;
185
+ if (require_compile_aot.Capture.isCapturing()) require_compile_aot.Capture.handler({
186
+ method: "WS",
187
+ path: route[1],
188
+ alias: "",
189
+ code: "",
190
+ k: require_compile_handler_index.routeShape(composed, void 0, app)
191
+ });
192
+ else if (require_compile_aot.Compiled.hasProgram(programId)) {
193
+ const shape = require_compile_handler_index.routeShape(composed, void 0, app);
194
+ live = shape === void 0 || captured?.k !== shape;
195
+ }
196
+ const frozenEager = require_type_bridge.isBridgeLive() || live ? void 0 : require_compile_handler_frozen_validator.buildFrozenRouteValidator(composed, app, "WS", route[1]);
223
197
  if (frozenEager) validators = frozenEager;
224
198
  else try {
225
- validators = new require_validator_route.RouteValidator(hook, {
199
+ validators = new require_validator_route.RouteValidator(composed, {
226
200
  models: require_generation.frozenRootOf(app)["~ext"]?.models,
227
201
  app,
228
- aot: {
202
+ normalize: require_generation.frozenRootOf(app)["~config"]?.normalize,
203
+ sanitize: require_generation.frozenRootOf(app)["~config"]?.sanitize,
204
+ schemas: composed.schemas,
205
+ aot: live ? void 0 : {
229
206
  method: "WS",
230
207
  path: route[1]
231
208
  },
@@ -233,23 +210,23 @@ function buildWSRoute(route, app) {
233
210
  });
234
211
  } catch (error) {
235
212
  if (!require_compile_handler_frozen_validator.isBridgeNotInitialized(error)) throw error;
236
- const frozen = require_compile_handler_frozen_validator.buildFrozenRouteValidator(hook, app, "WS", route[1]);
213
+ if (live) throw new Error(require_compile_aot.aotDrift("WS", route[1]), { cause: error });
214
+ const frozen = require_compile_handler_frozen_validator.buildFrozenRouteValidator(composed, app, "WS", route[1]);
237
215
  if (!frozen) throw error;
238
216
  validators = frozen;
239
217
  }
218
+ if (live && captured) require_compile_aot.warnAotDrift("WS", route[1]);
240
219
  const responseValidator = validators.response;
241
220
  const defaultResponseValidator = responseValidator ? responseValidator[200] ?? responseValidator[Object.keys(responseValidator)[0]] : void 0;
221
+ const cookieIsOptional = !!composed.cookie?.["~optional"];
242
222
  const queryChannels = require_compile_handler_utils.getQueryParseChannels(validators.query?.schema);
243
223
  const queryArray = queryChannels?.array;
244
224
  const queryObject = queryChannels?.object;
245
- const instance = route[3] ?? app;
246
- const appHookChain = route[5];
247
- const inheritedChain = route[6];
248
- const flatAppHook = require_compile_handler_index.composeRouteHook(instance, void 0, appHookChain, inheritedChain, app, route[7]) ?? {};
249
- const parseHooks = hook.parse == null ? [] : Array.isArray(hook.parse) ? hook.parse : [hook.parse];
225
+ const flatAppHook = require_compile_handler_index.composeRouteHook(instance, void 0, appHookChain, inheritedChain, app, route[7], false) ?? {};
226
+ const parseHooks = concatHooks(hook.parse);
250
227
  const transforms = concatHooks(flatAppHook.transform, hook.transform);
251
228
  const allBeforeHandles = concatHooks(flatAppHook.beforeHandle, hook.beforeHandle);
252
- const deriveEntries = [...flatAppHook["~deriveEntries"] ?? [], ...hook["~deriveEntries"] ?? []];
229
+ const deriveEntries = concatHooks(flatAppHook["~deriveEntries"], hook["~deriveEntries"]);
253
230
  const deriveSet = deriveEntries.length ? new Set(deriveEntries.map(require_utils.deriveEntryFn)) : void 0;
254
231
  const upgradeDeriveModes = require_compile_handler_utils.deriveModes(allBeforeHandles, deriveEntries);
255
232
  const messageBeforeHandles = allBeforeHandles.filter((fn) => !deriveSet?.has(fn));
@@ -257,8 +234,31 @@ function buildWSRoute(route, app) {
257
234
  const mapResponses = concatHooks(flatAppHook.mapResponse, hook.mapResponse);
258
235
  const afterResponses = concatHooks(flatAppHook.afterResponse, hook.afterResponse);
259
236
  const errorHandlers = concatHooks(hook.error, flatAppHook.error);
237
+ const inference = require_sucrose.sucrose(hook.message, {
238
+ beforeHandle: [
239
+ ...allBeforeHandles,
240
+ hook.open,
241
+ hook.close,
242
+ hook.drain,
243
+ hook.ping,
244
+ hook.pong,
245
+ typeof hook.upgrade === "function" ? hook.upgrade : void 0
246
+ ].filter(Boolean),
247
+ parse: parseHooks,
248
+ transform: transforms,
249
+ error: errorHandlers,
250
+ afterHandle: afterHandles,
251
+ mapResponse: mapResponses,
252
+ afterResponse: afterResponses
253
+ });
254
+ const cookieConfig = validators.cookie || inference.cookie ? require_cookie_config.compileCookieConfig(composed.cookie, require_generation.frozenRootOf(app)["~config"]?.cookie, require_generation.frozenRootOf(app)["~ext"]?.models, composed.schemas) : void 0;
255
+ const signCookies = cookieConfig?.hasSign ? (set) => require_cookie_utils.signCookieValues(set.cookie, cookieConfig) : void 0;
260
256
  const parseMessage = require_ws_parser.createMessageParser(parseHooks);
261
- const handleUpgradeError = require_handler_error.createErrorHandler(errorHandlers.length ? errorHandlers : void 0, ((response, set, context) => require_adapter_web_standard_handler.mapResponse(response, set, context?.request)), void 0, require_generation.frozenRootOf(app)["~config"]?.allowUnsafeValidationDetails);
257
+ const handleUpgradeError = require_handler_error.createErrorHandler(errorHandlers.length ? errorHandlers : void 0, ((response, set, context) => {
258
+ const map = () => require_adapter_web_standard_handler.mapResponse(response, set, context?.request);
259
+ const pending = signCookies?.(set);
260
+ return pending ? pending.then(map) : map();
261
+ }), require_generation.frozenRootOf(app)["~config"]?.allowUnsafeValidationDetails);
262
262
  async function handleError(ws, error) {
263
263
  const errCtx = Object.create(ws);
264
264
  errCtx.error = error;
@@ -272,6 +272,7 @@ function buildWSRoute(route, app) {
272
272
  break;
273
273
  }
274
274
  if (r !== void 0) {
275
+ r = require_handler_error.adoptErrorType(r, error);
275
276
  try {
276
277
  await handleWSResponse(ws, r, mapResponses);
277
278
  } catch {}
@@ -281,12 +282,6 @@ function buildWSRoute(route, app) {
281
282
  sendErrorFrame(ws, error);
282
283
  }
283
284
  const bodyValidator = validators.body;
284
- function validateMessageBody(message) {
285
- if (!bodyValidator) return message;
286
- if (bodyValidator.hasCodec) return bodyValidator.From(message, "body");
287
- if (bodyValidator instanceof require_validator_index.StandardValidator) return bodyValidator.From(message, "body");
288
- return bodyValidator.EncodeFrom(message, "body");
289
- }
290
285
  function onMessageValidationError(ws, error) {
291
286
  if (errorHandlers.length === 0) {
292
287
  if (require_generation.frozenRootOf(app)["~config"]?.allowUnsafeValidationDetails && error instanceof require_error.ValidationError) error.allowUnsafeValidationDetails = true;
@@ -294,7 +289,6 @@ function buildWSRoute(route, app) {
294
289
  }
295
290
  return handleError(ws, error);
296
291
  }
297
- const messageHandlerTouchesBody = !!bodyValidator || handlerMayTouchBody(hook.message) || errorHandlers.some(handlerMayTouchBody);
298
292
  const syncDispatchEligible = transforms.length === 0 && messageBeforeHandles.length === 0 && afterHandles.length === 0 && afterResponses.length === 0 && mapResponses.length === 0;
299
293
  function finishMessageResult(ws, value) {
300
294
  if (value === void 0) return;
@@ -309,7 +303,7 @@ function buildWSRoute(route, app) {
309
303
  if (bodyValidator) {
310
304
  let decoded;
311
305
  try {
312
- decoded = validateMessageBody(message);
306
+ decoded = validateUpgradeChannel(bodyValidator, message, "body");
313
307
  } catch (error) {
314
308
  return onMessageValidationError(ws, error);
315
309
  }
@@ -320,7 +314,7 @@ function buildWSRoute(route, app) {
320
314
  }
321
315
  function runMessageSync(ws, message) {
322
316
  try {
323
- if (messageHandlerTouchesBody) ws.body = message;
317
+ ws.body = message;
324
318
  const result = hook.message(ws, message);
325
319
  if (result instanceof Promise) return result.then((resolved) => finishMessageResult(ws, resolved), (error) => handleError(ws, error));
326
320
  return finishMessageResult(ws, result);
@@ -406,19 +400,37 @@ function buildWSRoute(route, app) {
406
400
  if (r instanceof Promise) r = await r;
407
401
  context.params = r;
408
402
  }
409
- if (validators.query) {
403
+ if (validators.query || inference.query) {
410
404
  const url = request.url;
411
- const query = require_parse_query.parseQueryFromURL(url, context.qi ?? url.indexOf("?"), queryArray, queryObject);
412
- let r = validateUpgradeChannel(validators.query, query, "query");
413
- if (r instanceof Promise) r = await r;
405
+ let r = require_parse_query.parseQueryFromURL(url, context.qi ?? url.indexOf("?"), queryArray, queryObject);
406
+ if (validators.query) {
407
+ r = validateUpgradeChannel(validators.query, r, "query");
408
+ if (r instanceof Promise) r = await r;
409
+ }
414
410
  context.query = r;
415
411
  }
416
- if (validators.headers) {
417
- const headers = require_universal_constants.isBun ? request.headers.toJSON() : Object.fromEntries(request.headers);
418
- let r = validateUpgradeChannel(validators.headers, headers, "headers");
419
- if (r instanceof Promise) r = await r;
412
+ if (validators.headers || inference.headers) {
413
+ let r = require_universal_constants.isBun ? request.headers.toJSON() : Object.fromEntries(request.headers);
414
+ if (validators.headers) {
415
+ r = validateUpgradeChannel(validators.headers, r, "headers");
416
+ if (r instanceof Promise) r = await r;
417
+ }
420
418
  context.headers = r;
421
419
  }
420
+ if (cookieConfig) {
421
+ const cookieHeader = request.headers.get("cookie");
422
+ if (validators.cookie) {
423
+ const raw = await require_cookie_utils.parseCookieRaw(cookieHeader, cookieConfig);
424
+ let r = raw;
425
+ if (!cookieIsOptional || Object.keys(raw).length) {
426
+ r = validateUpgradeChannel(validators.cookie, raw, "cookie");
427
+ if (r instanceof Promise) r = await r;
428
+ }
429
+ context.cookie = require_cookie_utils.buildCookieJar(context.set, r, cookieConfig);
430
+ } else if (!cookieConfig.hasSign) context.cookie = require_cookie_utils.buildCookieJar(context.set, require_cookie_utils.parseCookieRawDeferred(cookieHeader, cookieConfig), cookieConfig, void 0, 1);
431
+ else if (require_cookie_crypto.hasSyncHmac && cookieConfig.verify === "lazy") context.cookie = require_cookie_utils.buildCookieJar(context.set, require_cookie_utils.parseCookieRawLazy(cookieHeader, cookieConfig), cookieConfig, 1);
432
+ else context.cookie = require_cookie_utils.buildCookieJar(context.set, await require_cookie_utils.parseCookieRaw(cookieHeader, cookieConfig, cookieConfig.verify === "lazy" ? 1 : void 0), cookieConfig);
433
+ }
422
434
  for (let i = 0; i < transforms.length; i++) {
423
435
  const r = transforms[i](context);
424
436
  if (r instanceof Promise) await r;
@@ -430,9 +442,10 @@ function buildWSRoute(route, app) {
430
442
  const deriveMode = upgradeDeriveModes?.[i];
431
443
  if (deriveMode !== void 0 && !(r instanceof require_error.ElysiaStatus)) {
432
444
  if (r && typeof r === "object") if (deriveMode) context = require_compile_handler_utils.replaceDeriveContext(context, r);
433
- else Object.assign(context, r);
445
+ else require_utils.assignOwn(context, r);
434
446
  } else if (r !== void 0) {
435
447
  if (r instanceof Response) return r;
448
+ if (signCookies) await signCookies(context.set);
436
449
  return require_adapter_web_standard_handler.mapResponse(r, context.set, context.request);
437
450
  }
438
451
  }
@@ -465,7 +478,11 @@ function buildWSRoute(route, app) {
465
478
  }
466
479
  })) return new Response("Expected a websocket connection", { status: 400 });
467
480
  } catch (error) {
468
- return handleUpgradeError(context, error);
481
+ try {
482
+ return await handleUpgradeError(context, error);
483
+ } catch (thrown) {
484
+ return require_handler_utils.finalizeRouteError(app, context, thrown, signCookies);
485
+ }
469
486
  }
470
487
  };
471
488
  const options = require_utils.nullObject();
@@ -475,33 +492,46 @@ function buildWSRoute(route, app) {
475
492
  function resolveWSOptions(entries) {
476
493
  if (!entries || entries.length === 0) return void 0;
477
494
  if (entries.length === 1) return { ...entries[0].value };
478
- const ordered = entries.map((entry, index) => ({
479
- entry,
480
- index
481
- })).sort((a, b) => b.entry.depth - a.entry.depth || a.index - b.index);
495
+ const ordered = entries.toSorted((a, b) => b.depth - a.depth);
482
496
  const result = {};
483
497
  let warned;
484
- for (const { entry } of ordered) for (const key in entry.value) {
498
+ for (const entry of ordered) for (const key in entry.value) {
485
499
  const incoming = entry.value[key];
486
500
  if (key in result && result[key] !== incoming && !warned?.has(key)) {
487
501
  (warned ??= /* @__PURE__ */ new Set()).add(key);
488
- console.warn(`[Elysia] Conflicting WebSocket option '${key}' across .use(websocket()) registrations; the shallowest (nearest-root) registration wins.`);
502
+ console.warn(`[Elysia] Conflicting WebSocket option '${key}' across .use(websocket()). Using nearest-root registration.`);
489
503
  }
490
504
  result[key] = incoming;
491
505
  }
492
506
  return result;
493
507
  }
494
- function accumulateWSOptions(target, routeOptions, _path) {
495
- if (!require_utils.isNotEmpty(routeOptions)) return;
496
- if (require_universal_constants.isBun) {
497
- for (const key in routeOptions) if (key in target && target[key] !== routeOptions[key]) {
498
- console.warn(`[Elysia] Conflicting per-route WebSocket option '${key}'\nBun uses one global WebSocket config per server, per-route values are not enforced (the last-registered route wins).`);
499
- console.warn((/* @__PURE__ */ new Error()).stack);
508
+ const wsLimits = [
509
+ "maxPayloadLength",
510
+ "backpressureLimit",
511
+ "idleTimeout"
512
+ ];
513
+ const routeOwnedKeys = /* @__PURE__ */ new WeakMap();
514
+ function accumulateWSOptions(target, routeOptions) {
515
+ let owned = routeOwnedKeys.get(target);
516
+ if (!owned) routeOwnedKeys.set(target, owned = /* @__PURE__ */ new Set());
517
+ for (const key in routeOptions) {
518
+ const incoming = routeOptions[key];
519
+ const current = target[key];
520
+ if (key in target && current !== incoming) {
521
+ if (require_universal_constants.isBun) {
522
+ console.warn(`[Elysia] Conflicting per-route WebSocket option '${key}'\nBun uses one global WebSocket config per server, per-route values are not enforced (for limits the strictest route wins, otherwise the last-registered route).`);
523
+ console.warn((/* @__PURE__ */ new Error()).stack);
524
+ }
525
+ if (owned.has(key) && typeof current === "number" && typeof incoming === "number" && current < incoming && wsLimits.includes(key)) continue;
500
526
  }
527
+ owned.add(key);
528
+ target[key] = incoming;
501
529
  }
502
- Object.assign(target, routeOptions);
503
530
  }
531
+ const MAX_INFLIGHT_MESSAGES = 256;
504
532
  function buildGlobalWSHandler() {
533
+ let lifecycle;
534
+ const runLifecycle = (callback, ...args) => lifecycle ? lifecycle.run(callback, ...args) : callback(...args);
505
535
  function getElysia(ws) {
506
536
  let elysia = ws.data.elysia;
507
537
  if (!elysia) {
@@ -516,39 +546,117 @@ function buildGlobalWSHandler() {
516
546
  ws.send(typeof error?.message === "string" ? error.message : "Internal Server Error");
517
547
  } catch {}
518
548
  };
519
- return {
549
+ const releaseLifecycle = (ws) => {
550
+ if (!lifecycle?.closing && require_utils.isSocketQuiet(ws)) {
551
+ delete ws.data["~lifecycleRun"];
552
+ lifecycle?.sockets.delete(ws);
553
+ }
554
+ };
555
+ const release = (ws) => {
556
+ const data = ws.data;
557
+ const inflight = data.inflight;
558
+ if (inflight) {
559
+ data.inflight = inflight - 1;
560
+ if (inflight === 1 && data.closeHandlerInvoked) releaseLifecycle(ws);
561
+ }
562
+ };
563
+ function dispatch(ws, message) {
564
+ let result;
565
+ try {
566
+ result = ws.data.message(getElysia(ws), message);
567
+ } catch (error) {
568
+ release(ws);
569
+ sendErrMsg(ws, error);
570
+ return;
571
+ }
572
+ if (result instanceof Promise) result.then(() => release(ws), (error) => {
573
+ release(ws);
574
+ sendErrMsg(ws, error);
575
+ });
576
+ else release(ws);
577
+ }
578
+ const handler = {
520
579
  message(ws, message) {
521
- let result;
522
- try {
523
- result = ws.data.message?.(getElysia(ws), message);
524
- } catch (error) {
525
- sendErrMsg(ws, error);
580
+ if (lifecycle?.closing) return;
581
+ const data = ws.data;
582
+ if (!data.message) return;
583
+ const inflight = (data.inflight ?? 0) + 1;
584
+ if (inflight > MAX_INFLIGHT_MESSAGES) {
585
+ try {
586
+ ws.close(1013, "Too many in-flight messages");
587
+ } catch {}
526
588
  return;
527
589
  }
528
- if (result instanceof Promise) result.catch((error) => sendErrMsg(ws, error));
590
+ data.inflight = inflight;
591
+ const opening = data.opening;
592
+ if (opening) return void opening.then(() => {
593
+ if (lifecycle?.closing || ws.readyState > 1) return release(ws);
594
+ dispatch(ws, message);
595
+ });
596
+ dispatch(ws, message);
529
597
  },
530
598
  open(ws) {
531
- ws.data.open?.(getElysia(ws));
599
+ if (lifecycle) Object.defineProperty(ws.data, "~lifecycleRun", {
600
+ configurable: true,
601
+ value: lifecycle.run
602
+ });
603
+ lifecycle?.sockets.add(ws);
604
+ if (lifecycle?.closing) {
605
+ ws.data.closeHandlerInvoked = true;
606
+ try {
607
+ ws.terminate();
608
+ } catch {}
609
+ return;
610
+ }
611
+ const open = ws.data.open;
612
+ const result = open && runLifecycle(open, getElysia(ws));
613
+ if (result instanceof Promise) {
614
+ const clear = () => {
615
+ ws.data.opening = void 0;
616
+ releaseLifecycle(ws);
617
+ };
618
+ ws.data.opening = result.then(clear, clear);
619
+ }
532
620
  },
533
621
  drain(ws) {
622
+ if (lifecycle?.closing) return;
534
623
  const elyWs = getElysia(ws);
535
624
  drainWaiters(elyWs);
536
- ws.data.drain?.(elyWs);
625
+ const drain = ws.data.drain;
626
+ require_ws_context.trackWSSettling(ws.data, drain && runLifecycle(drain, elyWs), () => releaseLifecycle(ws));
537
627
  },
538
628
  close(ws, code, reason) {
539
629
  const elyWs = getElysia(ws);
540
630
  drainWaiters(elyWs);
541
- if (ws.data.closeHandlerInvoked) return;
631
+ if (ws.data.closeHandlerInvoked) return releaseLifecycle(ws);
542
632
  ws.data.closeHandlerInvoked = true;
543
- ws.data.close?.(elyWs, code, reason);
633
+ let result;
634
+ try {
635
+ const close = ws.data.close;
636
+ result = close && runLifecycle(close, elyWs, code, reason);
637
+ } finally {
638
+ if (!(result instanceof Promise)) releaseLifecycle(ws);
639
+ }
640
+ if (result instanceof Promise) {
641
+ const closing = result.catch(() => {});
642
+ require_ws_context.trackWSSettling(ws.data, closing, () => releaseLifecycle(ws));
643
+ }
544
644
  },
545
645
  ping(ws, data) {
546
- ws.data.ping?.(getElysia(ws), data);
646
+ if (lifecycle?.closing) return;
647
+ const ping = ws.data.ping;
648
+ require_ws_context.trackWSSettling(ws.data, ping && runLifecycle(ping, getElysia(ws), data), () => releaseLifecycle(ws));
547
649
  },
548
650
  pong(ws, data) {
549
- ws.data.pong?.(getElysia(ws), data);
651
+ if (lifecycle?.closing) return;
652
+ const pong = ws.data.pong;
653
+ require_ws_context.trackWSSettling(ws.data, pong && runLifecycle(pong, getElysia(ws), data), () => releaseLifecycle(ws));
550
654
  }
551
655
  };
656
+ Object.defineProperty(handler, "~lifecycle", { set(value) {
657
+ lifecycle = value;
658
+ } });
659
+ return handler;
552
660
  }
553
661
 
554
662
  //#endregion