elysia 2.0.0-beta.3 → 2.0.0-beta.30

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 (369) hide show
  1. package/README.md +1 -1
  2. package/dist/_virtual/_rolldown/runtime.mjs +1 -0
  3. package/dist/adapter/bun/index.d.ts +16 -2
  4. package/dist/adapter/bun/index.js +458 -119
  5. package/dist/adapter/bun/index.mjs +459 -121
  6. package/dist/adapter/constants.mjs +1 -0
  7. package/dist/adapter/default-headers.mjs +1 -0
  8. package/dist/adapter/index.mjs +1 -0
  9. package/dist/adapter/origin.js +0 -57
  10. package/dist/adapter/origin.mjs +1 -57
  11. package/dist/adapter/response-ownership.mjs +1 -0
  12. package/dist/adapter/skip-clone.mjs +1 -0
  13. package/dist/adapter/types.d.ts +2 -21
  14. package/dist/adapter/types.mjs +1 -0
  15. package/dist/adapter/utils.d.ts +11 -3
  16. package/dist/adapter/utils.js +147 -112
  17. package/dist/adapter/utils.mjs +149 -114
  18. package/dist/adapter/web-standard/handler.d.ts +1 -1
  19. package/dist/adapter/web-standard/handler.js +39 -36
  20. package/dist/adapter/web-standard/handler.mjs +41 -37
  21. package/dist/adapter/web-standard/index.js +2 -30
  22. package/dist/adapter/web-standard/index.mjs +3 -30
  23. package/dist/adapter/web-standard/utils.js +5 -18
  24. package/dist/adapter/web-standard/utils.mjs +6 -18
  25. package/dist/base.d.ts +272 -145
  26. package/dist/base.js +740 -675
  27. package/dist/base.mjs +745 -679
  28. package/dist/compile/aot-capture.d.ts +2 -13
  29. package/dist/compile/aot-capture.js +40 -90
  30. package/dist/compile/aot-capture.mjs +44 -89
  31. package/dist/compile/aot-emit.d.ts +9 -8
  32. package/dist/compile/aot-emit.js +36 -38
  33. package/dist/compile/aot-emit.mjs +37 -38
  34. package/dist/compile/aot-reconstruct.js +7 -30
  35. package/dist/compile/aot-reconstruct.mjs +8 -30
  36. package/dist/compile/aot.d.ts +15 -17
  37. package/dist/compile/aot.js +36 -58
  38. package/dist/compile/aot.mjs +34 -57
  39. package/dist/compile/handler/descriptor.d.ts +10 -17
  40. package/dist/compile/handler/descriptor.js +25 -58
  41. package/dist/compile/handler/descriptor.mjs +29 -58
  42. package/dist/compile/handler/frozen-validator.d.ts +7 -12
  43. package/dist/compile/handler/frozen-validator.js +57 -118
  44. package/dist/compile/handler/frozen-validator.mjs +56 -118
  45. package/dist/compile/handler/index.d.ts +8 -4
  46. package/dist/compile/handler/index.js +322 -206
  47. package/dist/compile/handler/index.mjs +324 -211
  48. package/dist/compile/handler/jit.d.ts +28 -21
  49. package/dist/compile/handler/jit.js +232 -159
  50. package/dist/compile/handler/jit.mjs +235 -165
  51. package/dist/compile/handler/params.d.ts +5 -4
  52. package/dist/compile/handler/params.js +20 -9
  53. package/dist/compile/handler/params.mjs +24 -12
  54. package/dist/compile/handler/reconstruct.d.ts +4 -4
  55. package/dist/compile/handler/reconstruct.js +8 -6
  56. package/dist/compile/handler/reconstruct.mjs +9 -6
  57. package/dist/compile/handler/static-clone-resolver.d.ts +6 -0
  58. package/dist/compile/handler/static-clone-resolver.js +8 -0
  59. package/dist/compile/handler/static-clone-resolver.mjs +8 -0
  60. package/dist/compile/handler/utils.d.ts +29 -11
  61. package/dist/compile/handler/utils.js +178 -273
  62. package/dist/compile/handler/utils.mjs +173 -272
  63. package/dist/compile/jit-probe.d.ts +2 -2
  64. package/dist/compile/jit-probe.js +2 -3
  65. package/dist/compile/jit-probe.mjs +3 -3
  66. package/dist/compile/lexer.d.ts +7 -4
  67. package/dist/compile/lexer.js +268 -6
  68. package/dist/compile/lexer.mjs +269 -4
  69. package/dist/compile/utils.d.ts +2 -1
  70. package/dist/compile/utils.js +14 -6
  71. package/dist/compile/utils.mjs +14 -6
  72. package/dist/compiled.d.ts +0 -1
  73. package/dist/compiled.js +0 -10
  74. package/dist/compiled.mjs +1 -10
  75. package/dist/constants.js +19 -4
  76. package/dist/constants.mjs +20 -4
  77. package/dist/context.d.ts +15 -5
  78. package/dist/context.js +17 -36
  79. package/dist/context.mjs +20 -38
  80. package/dist/cookie/config.d.ts +6 -2
  81. package/dist/cookie/config.js +77 -23
  82. package/dist/cookie/config.mjs +79 -25
  83. package/dist/cookie/cookie.d.ts +7 -5
  84. package/dist/cookie/cookie.js +12 -10
  85. package/dist/cookie/cookie.mjs +13 -10
  86. package/dist/cookie/crypto.d.ts +1 -1
  87. package/dist/cookie/crypto.js +19 -14
  88. package/dist/cookie/crypto.mjs +20 -14
  89. package/dist/cookie/error.d.ts +1 -2
  90. package/dist/cookie/error.js +6 -3
  91. package/dist/cookie/error.mjs +7 -3
  92. package/dist/cookie/lib.js +2 -1
  93. package/dist/cookie/lib.mjs +3 -1
  94. package/dist/cookie/serialize.mjs +1 -0
  95. package/dist/cookie/types.d.ts +1 -1
  96. package/dist/cookie/types.mjs +1 -0
  97. package/dist/cookie/utils.d.ts +5 -9
  98. package/dist/cookie/utils.js +109 -83
  99. package/dist/cookie/utils.mjs +112 -84
  100. package/dist/error.d.ts +126 -111
  101. package/dist/error.js +187 -180
  102. package/dist/error.mjs +185 -181
  103. package/dist/generation.d.ts +2 -2
  104. package/dist/generation.mjs +1 -0
  105. package/dist/handler/error.d.ts +15 -18
  106. package/dist/handler/error.js +74 -120
  107. package/dist/handler/error.mjs +77 -122
  108. package/dist/handler/fetch.js +165 -153
  109. package/dist/handler/fetch.mjs +168 -155
  110. package/dist/handler/utils.d.ts +25 -6
  111. package/dist/handler/utils.js +81 -26
  112. package/dist/handler/utils.mjs +76 -25
  113. package/dist/index.d.ts +11 -11
  114. package/dist/index.js +3 -1
  115. package/dist/index.mjs +5 -4
  116. package/dist/memory.js +2 -2
  117. package/dist/memory.mjs +3 -2
  118. package/dist/package.js +1 -1
  119. package/dist/package.mjs +2 -1
  120. package/dist/parse-query.js +76 -80
  121. package/dist/parse-query.mjs +77 -80
  122. package/dist/plugin/aot/bun.d.ts +8 -1
  123. package/dist/plugin/aot/bun.js +9 -28
  124. package/dist/plugin/aot/bun.mjs +11 -29
  125. package/dist/plugin/aot/core.d.ts +37 -66
  126. package/dist/plugin/aot/core.js +197 -153
  127. package/dist/plugin/aot/core.mjs +195 -156
  128. package/dist/plugin/aot/esbuild.js +8 -24
  129. package/dist/plugin/aot/esbuild.mjs +10 -25
  130. package/dist/plugin/aot/hooks.d.ts +6 -17
  131. package/dist/plugin/aot/hooks.js +37 -17
  132. package/dist/plugin/aot/hooks.mjs +38 -19
  133. package/dist/plugin/aot/rspack-loader.d.ts +1 -5
  134. package/dist/plugin/aot/rspack-loader.js +1 -26
  135. package/dist/plugin/aot/rspack-loader.mjs +2 -26
  136. package/dist/plugin/aot/rspack.js +2 -19
  137. package/dist/plugin/aot/rspack.mjs +4 -20
  138. package/dist/plugin/aot/source.d.ts +7 -43
  139. package/dist/plugin/aot/source.js +73 -73
  140. package/dist/plugin/aot/source.mjs +74 -74
  141. package/dist/plugin/aot/treeshake.js +2 -6
  142. package/dist/plugin/aot/treeshake.mjs +3 -6
  143. package/dist/plugin/aot/unplugin.d.ts +1 -4
  144. package/dist/plugin/aot/unplugin.js +4 -32
  145. package/dist/plugin/aot/unplugin.mjs +5 -32
  146. package/dist/plugin/aot/vite.d.ts +2 -6
  147. package/dist/plugin/aot/vite.js +0 -14
  148. package/dist/plugin/aot/vite.mjs +1 -14
  149. package/dist/plugin/aot/worker.js +2 -2
  150. package/dist/plugin/aot/worker.mjs +3 -2
  151. package/dist/plugin/auto-head.js +1 -10
  152. package/dist/plugin/auto-head.mjs +2 -10
  153. package/dist/plugin/trace.d.ts +18 -1
  154. package/dist/plugin/trace.js +0 -16
  155. package/dist/plugin/trace.mjs +1 -16
  156. package/dist/plugin/websocket.d.ts +21 -0
  157. package/dist/plugin/websocket.js +0 -21
  158. package/dist/plugin/websocket.mjs +1 -21
  159. package/dist/route-table.d.ts +3 -0
  160. package/dist/route-table.js +4 -1
  161. package/dist/route-table.mjs +5 -1
  162. package/dist/schema-snapshot.d.ts +0 -4
  163. package/dist/schema-snapshot.js +60 -56
  164. package/dist/schema-snapshot.mjs +61 -56
  165. package/dist/sucrose.d.ts +7 -56
  166. package/dist/sucrose.js +103 -735
  167. package/dist/sucrose.mjs +103 -728
  168. package/dist/trace.d.ts +16 -10
  169. package/dist/trace.js +20 -134
  170. package/dist/trace.mjs +22 -135
  171. package/dist/type/bridge-live.d.ts +5 -3
  172. package/dist/type/bridge-live.js +6 -3
  173. package/dist/type/bridge-live.mjs +6 -3
  174. package/dist/type/bridge.d.ts +7 -5
  175. package/dist/type/bridge.js +38 -28
  176. package/dist/type/bridge.mjs +27 -22
  177. package/dist/type/coerce-plan.d.ts +5 -5
  178. package/dist/type/coerce-plan.js +53 -43
  179. package/dist/type/coerce-plan.mjs +56 -44
  180. package/dist/type/coerce.d.ts +2 -6
  181. package/dist/type/coerce.js +18 -79
  182. package/dist/type/coerce.mjs +20 -80
  183. package/dist/type/compat.js +10 -19
  184. package/dist/type/compat.mjs +12 -20
  185. package/dist/type/constants.d.ts +1 -1
  186. package/dist/type/constants.mjs +1 -0
  187. package/dist/type/elysia/accelerate.mjs +1 -0
  188. package/dist/type/elysia/array-buffer.d.ts +6 -2
  189. package/dist/type/elysia/array-buffer.js +16 -15
  190. package/dist/type/elysia/array-buffer.mjs +17 -16
  191. package/dist/type/elysia/array-string.js +2 -43
  192. package/dist/type/elysia/array-string.mjs +3 -43
  193. package/dist/type/elysia/array.js +5 -9
  194. package/dist/type/elysia/array.mjs +6 -9
  195. package/dist/type/elysia/boolean-string.mjs +1 -0
  196. package/dist/type/elysia/boolean.js +3 -13
  197. package/dist/type/elysia/boolean.mjs +4 -13
  198. package/dist/type/elysia/cookie.d.ts +3 -4
  199. package/dist/type/elysia/cookie.js +3 -14
  200. package/dist/type/elysia/cookie.mjs +4 -14
  201. package/dist/type/elysia/date.d.ts +2 -1
  202. package/dist/type/elysia/date.js +7 -14
  203. package/dist/type/elysia/date.mjs +8 -14
  204. package/dist/type/elysia/file-type.d.ts +2 -2
  205. package/dist/type/elysia/file-type.js +4 -3
  206. package/dist/type/elysia/file-type.mjs +5 -3
  207. package/dist/type/elysia/file.d.ts +1 -3
  208. package/dist/type/elysia/file.js +3 -23
  209. package/dist/type/elysia/file.mjs +5 -10
  210. package/dist/type/elysia/files.js +1 -7
  211. package/dist/type/elysia/files.mjs +3 -8
  212. package/dist/type/elysia/form.mjs +1 -0
  213. package/dist/type/elysia/integer-string.d.ts +1 -1
  214. package/dist/type/elysia/integer-string.js +3 -28
  215. package/dist/type/elysia/integer-string.mjs +5 -29
  216. package/dist/type/elysia/integer.js +3 -13
  217. package/dist/type/elysia/integer.mjs +4 -13
  218. package/dist/type/elysia/intersect.js +0 -5
  219. package/dist/type/elysia/intersect.mjs +1 -5
  220. package/dist/type/elysia/json-string.d.ts +7 -0
  221. package/dist/type/elysia/json-string.js +47 -0
  222. package/dist/type/elysia/json-string.mjs +47 -0
  223. package/dist/type/elysia/keep-config.d.ts +14 -0
  224. package/dist/type/elysia/keep-config.js +62 -0
  225. package/dist/type/elysia/keep-config.mjs +54 -0
  226. package/dist/type/elysia/maybe-empty.mjs +1 -0
  227. package/dist/type/elysia/no-validate.mjs +1 -0
  228. package/dist/type/elysia/nullable.mjs +1 -0
  229. package/dist/type/elysia/number.d.ts +2 -1
  230. package/dist/type/elysia/number.js +19 -12
  231. package/dist/type/elysia/number.mjs +19 -12
  232. package/dist/type/elysia/numeric-enum.d.ts +5 -3
  233. package/dist/type/elysia/numeric-enum.js +1 -5
  234. package/dist/type/elysia/numeric-enum.mjs +2 -5
  235. package/dist/type/elysia/numeric.d.ts +4 -2
  236. package/dist/type/elysia/numeric.js +17 -14
  237. package/dist/type/elysia/numeric.mjs +17 -14
  238. package/dist/type/elysia/object-string.js +2 -40
  239. package/dist/type/elysia/object-string.mjs +3 -40
  240. package/dist/type/elysia/object.js +5 -9
  241. package/dist/type/elysia/object.mjs +6 -9
  242. package/dist/type/elysia/optional.js +8 -8
  243. package/dist/type/elysia/optional.mjs +8 -7
  244. package/dist/type/elysia/problem.d.ts +20 -0
  245. package/dist/type/elysia/problem.js +19 -0
  246. package/dist/type/elysia/problem.mjs +19 -0
  247. package/dist/type/elysia/string.mjs +1 -0
  248. package/dist/type/elysia/uint8-array.d.ts +2 -1
  249. package/dist/type/elysia/uint8-array.js +4 -18
  250. package/dist/type/elysia/uint8-array.mjs +5 -18
  251. package/dist/type/elysia/union-enum.js +5 -6
  252. package/dist/type/elysia/union-enum.mjs +6 -6
  253. package/dist/type/elysia/union.js +0 -5
  254. package/dist/type/elysia/union.mjs +1 -5
  255. package/dist/type/elysia/utils.d.ts +4 -6
  256. package/dist/type/elysia/utils.js +16 -40
  257. package/dist/type/elysia/utils.mjs +15 -36
  258. package/dist/type/exports.d.ts +4 -2
  259. package/dist/type/exports.js +13 -1
  260. package/dist/type/exports.mjs +5 -2
  261. package/dist/type/index.d.ts +1 -1
  262. package/dist/type/index.js +31 -23
  263. package/dist/type/index.mjs +33 -24
  264. package/dist/type/shared.d.ts +12 -10
  265. package/dist/type/shared.js +75 -109
  266. package/dist/type/shared.mjs +72 -108
  267. package/dist/type/sync-require.d.ts +11 -0
  268. package/dist/type/sync-require.js +7 -0
  269. package/dist/type/sync-require.mjs +6 -0
  270. package/dist/type/typebox-system-lite.d.ts +2 -0
  271. package/dist/type/typebox-system-lite.js +34 -0
  272. package/dist/type/typebox-system-lite.mjs +4 -0
  273. package/dist/type/typebox-system-require.d.ts +7 -0
  274. package/dist/type/typebox-system-require.js +24 -0
  275. package/dist/type/typebox-system-require.mjs +24 -0
  276. package/dist/type/typebox-type-live.d.ts +6 -4
  277. package/dist/type/typebox-type-live.js +23 -9
  278. package/dist/type/typebox-type-live.mjs +11 -6
  279. package/dist/type/typebox-type.d.ts +14 -4
  280. package/dist/type/typebox-type.js +85 -22
  281. package/dist/type/typebox-type.mjs +35 -21
  282. package/dist/type/typebox-value-live.d.ts +5 -3
  283. package/dist/type/typebox-value-live.js +7 -3
  284. package/dist/type/typebox-value-live.mjs +6 -3
  285. package/dist/type/typebox-value-ops.d.ts +4 -0
  286. package/dist/type/typebox-value-ops.js +90 -0
  287. package/dist/type/typebox-value-ops.mjs +6 -0
  288. package/dist/type/typebox-value-require.d.ts +8 -0
  289. package/dist/type/typebox-value-require.js +28 -0
  290. package/dist/type/typebox-value-require.mjs +25 -0
  291. package/dist/type/typebox-value.d.ts +15 -10
  292. package/dist/type/typebox-value.js +19 -8
  293. package/dist/type/typebox-value.mjs +19 -9
  294. package/dist/type/types.d.ts +1 -1
  295. package/dist/type/types.mjs +1 -0
  296. package/dist/type/utils.js +1 -10
  297. package/dist/type/utils.mjs +2 -10
  298. package/dist/type/validator/clean-safe.d.ts +2 -4
  299. package/dist/type/validator/clean-safe.js +15 -11
  300. package/dist/type/validator/clean-safe.mjs +17 -10
  301. package/dist/type/validator/custom-error.js +50 -48
  302. package/dist/type/validator/custom-error.mjs +51 -48
  303. package/dist/type/validator/default-precompute.d.ts +10 -2
  304. package/dist/type/validator/default-precompute.js +33 -77
  305. package/dist/type/validator/default-precompute.mjs +33 -77
  306. package/dist/type/validator/exact-mirror-live.d.ts +8 -0
  307. package/dist/type/validator/exact-mirror-live.js +15 -0
  308. package/dist/type/validator/exact-mirror-live.mjs +11 -0
  309. package/dist/type/validator/exact-mirror-require.d.ts +4 -0
  310. package/dist/type/validator/exact-mirror-require.js +11 -0
  311. package/dist/type/validator/exact-mirror-require.mjs +9 -0
  312. package/dist/type/validator/exact-mirror.js +15 -7
  313. package/dist/type/validator/exact-mirror.mjs +17 -7
  314. package/dist/type/validator/frozen-check.js +7 -8
  315. package/dist/type/validator/frozen-check.mjs +9 -9
  316. package/dist/type/validator/index.d.ts +2 -1
  317. package/dist/type/validator/index.js +149 -168
  318. package/dist/type/validator/index.mjs +155 -173
  319. package/dist/type/validator/string-codec-aot.d.ts +3 -2
  320. package/dist/type/validator/string-codec-aot.js +11 -7
  321. package/dist/type/validator/string-codec-aot.mjs +11 -7
  322. package/dist/type/validator/validator-cache.d.ts +5 -4
  323. package/dist/type/validator/validator-cache.js +49 -37
  324. package/dist/type/validator/validator-cache.mjs +49 -36
  325. package/dist/types.d.ts +362 -163
  326. package/dist/types.mjs +1 -0
  327. package/dist/universal/constants.mjs +1 -0
  328. package/dist/universal/env.js +2 -3
  329. package/dist/universal/env.mjs +2 -3
  330. package/dist/universal/file.d.ts +6 -4
  331. package/dist/universal/file.js +7 -19
  332. package/dist/universal/file.mjs +9 -19
  333. package/dist/universal/index.d.ts +2 -2
  334. package/dist/universal/index.mjs +1 -0
  335. package/dist/universal/is-production.d.ts +1 -2
  336. package/dist/universal/is-production.js +1 -3
  337. package/dist/universal/is-production.mjs +3 -3
  338. package/dist/universal/server.d.ts +0 -272
  339. package/dist/universal/server.mjs +1 -0
  340. package/dist/utils.d.ts +62 -22
  341. package/dist/utils.js +342 -277
  342. package/dist/utils.mjs +327 -273
  343. package/dist/validator/index.d.ts +3 -3
  344. package/dist/validator/index.js +20 -34
  345. package/dist/validator/index.mjs +23 -36
  346. package/dist/validator/route.d.ts +3 -10
  347. package/dist/validator/route.js +2 -1
  348. package/dist/validator/route.mjs +3 -1
  349. package/dist/ws/context.d.ts +4 -1
  350. package/dist/ws/context.js +41 -36
  351. package/dist/ws/context.mjs +41 -36
  352. package/dist/ws/index.mjs +1 -0
  353. package/dist/ws/parser.js +12 -22
  354. package/dist/ws/parser.mjs +13 -22
  355. package/dist/ws/route.d.ts +2 -2
  356. package/dist/ws/route.js +292 -222
  357. package/dist/ws/route.mjs +299 -228
  358. package/dist/ws/types.d.ts +4 -5
  359. package/dist/ws/types.mjs +1 -0
  360. package/package.json +22 -55
  361. package/dist/compile/analysis-cache.d.ts +0 -6
  362. package/dist/compile/analysis-cache.js +0 -12
  363. package/dist/compile/analysis-cache.mjs +0 -11
  364. package/dist/cookie/index.d.ts +0 -7
  365. package/dist/cookie/index.js +0 -13
  366. package/dist/cookie/index.mjs +0 -7
  367. package/dist/handler/index.d.ts +0 -2
  368. package/dist/handler/index.js +0 -5
  369. package/dist/handler/index.mjs +0 -3
@@ -1,7 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_adapter_index = require('../index.js');
3
- const require_constants = require('../../constants.js');
4
3
  const require_utils = require('../../utils.js');
4
+ const require_type_bridge = require('../../type/bridge.js');
5
+ const require_universal_is_production = require('../../universal/is-production.js');
5
6
  const require_adapter_web_standard_index = require('../web-standard/index.js');
6
7
  const require_generation = require('../../generation.js');
7
8
  const require_adapter_origin = require('../origin.js');
@@ -9,15 +10,48 @@ const require_compile_handler_index = require('../../compile/handler/index.js');
9
10
  const require_route_table = require('../../route-table.js');
10
11
 
11
12
  //#region src/adapter/bun/index.ts
12
- /**
13
- * ! This may looks like it would cause race condition, but it is not
14
- * Bun is single-threaded and synchronous, so the `finally` block will
15
- * always run before the next request comes in
16
- * @see ../origin.ts
17
- *
18
- * `finally` runs on the synchronous return of `fetch` (the returned promise is
19
- * not awaited), so the slot is live only for the handler's synchronous prologue
20
- */
13
+ function runLifecycle(epoch, callback, ...args) {
14
+ const invoking = epoch.invoking;
15
+ epoch.invoking = true;
16
+ try {
17
+ return callback(...args);
18
+ } finally {
19
+ epoch.invoking = invoking;
20
+ }
21
+ }
22
+ const unavailableFetch = () => new Response(null, {
23
+ headers: { connection: "close" },
24
+ status: 503
25
+ });
26
+ function releaseLifecycle(app, ext, createdExt, stop, registerCleanup) {
27
+ if (ext.stop === stop) delete ext.stop;
28
+ if (ext.cleanupEpoch === registerCleanup) delete ext.cleanupEpoch;
29
+ if (createdExt && app["~ext"] === ext && !Object.keys(ext).length) app["~ext"] = void 0;
30
+ }
31
+ async function waitForServerRequests(server, abandoned) {
32
+ while (server.pendingRequests && !abandoned()) await Bun.sleep(1);
33
+ }
34
+ async function settleServerWebSockets(lifecycle, errors) {
35
+ if (!lifecycle) return true;
36
+ lifecycle.closing = true;
37
+ let failed = false;
38
+ do {
39
+ for (const socket of lifecycle.sockets) if (socket.readyState < 3) try {
40
+ socket.terminate();
41
+ } catch (error) {
42
+ errors.push(error);
43
+ failed = true;
44
+ }
45
+ if (failed) return false;
46
+ await Bun.sleep(1);
47
+ const abandon = lifecycle.abandon === true;
48
+ for (const socket of lifecycle.sockets) if (abandon || require_utils.isSocketQuiet(socket)) {
49
+ delete socket.data["~lifecycleRun"];
50
+ lifecycle.sockets.delete(socket);
51
+ }
52
+ } while (lifecycle.sockets.size);
53
+ return true;
54
+ }
21
55
  const withOrigin = (fetch) => (request, server) => {
22
56
  require_adapter_origin.origin.request = request;
23
57
  try {
@@ -26,102 +60,177 @@ const withOrigin = (fetch) => (request, server) => {
26
60
  require_adapter_origin.origin.request = void 0;
27
61
  }
28
62
  };
29
- const nativeStaticMethods = new Set([
30
- "GET",
31
- "POST",
32
- "PUT",
33
- "DELETE",
34
- "PATCH",
35
- "HEAD",
36
- "OPTIONS"
37
- ]);
63
+ const isNativeStaticMethod = (method) => method === "GET" || method === "POST" || method === "PUT" || method === "DELETE" || method === "PATCH" || method === "HEAD" || method === "OPTIONS";
64
+ function expandOptional(path) {
65
+ const at = path.indexOf("?");
66
+ if (at === -1) return [path];
67
+ const start = path.lastIndexOf("/", at);
68
+ const rest = path.slice(at + 1);
69
+ return [...expandOptional(path.slice(0, start) + rest), ...expandOptional(path.slice(0, at) + rest)].map((variant) => variant || "/");
70
+ }
71
+ function isNativePath(path) {
72
+ const names = /* @__PURE__ */ new Set();
73
+ const segments = path.split("/");
74
+ for (let i = 0; i < segments.length; i++) {
75
+ const segment = segments[i];
76
+ if (segment.indexOf("*") !== -1) {
77
+ if (segment !== "*" || i !== segments.length - 1) return false;
78
+ } else if (segment.indexOf(":") !== -1) {
79
+ if (!/^:[A-Za-z_$][\w$]*$/.test(segment)) return false;
80
+ if (names.has(segment)) return false;
81
+ names.add(segment);
82
+ }
83
+ }
84
+ return true;
85
+ }
86
+ function collectHTMLBundleRoutes(app, handoff, routes) {
87
+ let dynamic;
88
+ let bundles;
89
+ const table = app["~generation"]?.routeTable;
90
+ if (table && !table.handler.some((h, i) => isNativeStaticMethod(table.method[i]) && require_utils.isHTMLBundle(h))) return routes;
91
+ const declared = table ? table.method.map((_, i) => require_route_table.routeRow(table, i)) : app["~routes"];
92
+ for (const route of declared) {
93
+ const [method, path, handler] = route;
94
+ if (!isNativeStaticMethod(method) || !require_utils.isHTMLBundle(handler)) continue;
95
+ if (path.indexOf(":") !== -1 || path.indexOf("*") !== -1) {
96
+ if (!handoff) continue;
97
+ (dynamic ??= /* @__PURE__ */ new Set()).add(method);
98
+ (bundles ??= []).push([
99
+ method,
100
+ path,
101
+ handler
102
+ ]);
103
+ } else ((routes ??= require_utils.nullObject())[path] ??= require_utils.nullObject())[method] = handler;
104
+ if (!require_universal_is_production.isProduction() && (route[4] || route[5] || route[6])) console.warn(`[Elysia] ${method} ${path} is an HTML bundle served natively, hooks do not run for it`);
105
+ }
106
+ if (!dynamic) return routes;
107
+ const strictPath = app["~config"]?.strictPath === true;
108
+ const handoffs = [];
109
+ for (const [method, path, handler] of declared) {
110
+ if (require_utils.isHTMLBundle(handler)) continue;
111
+ const targets = method === "*" ? dynamic : dynamic.has(method) ? [method] : void 0;
112
+ if (!targets) continue;
113
+ for (const variant of expandOptional(path)) {
114
+ const loose = strictPath || variant.endsWith("*") ? variant : require_utils.getLoosePath(variant);
115
+ for (const raw of loose === variant ? [variant] : [variant, loose]) {
116
+ if (!isNativePath(raw)) {
117
+ if (!require_universal_is_production.isProduction()) console.warn(`[Elysia] ${method} ${path} cannot be expressed in Bun's router, so :param / * HTML bundles are not served natively (they would shadow it)`);
118
+ return routes;
119
+ }
120
+ const encoded = encodeURI(raw);
121
+ for (const target of targets) {
122
+ if (!/[^ -~]/.test(raw)) handoffs.push([raw, target]);
123
+ if (encoded !== raw) handoffs.push([encoded, target]);
124
+ }
125
+ }
126
+ }
127
+ }
128
+ for (const [method, path, bundle] of bundles) ((routes ??= require_utils.nullObject())[path] ??= require_utils.nullObject())[method] = bundle;
129
+ for (const [key, method] of handoffs) (routes[key] ??= require_utils.nullObject())[method] ??= handoff;
130
+ return routes;
131
+ }
38
132
  function collectStaticRoutes(app) {
39
- if (app["~config"]?.nativeStaticResponse === false) return;
40
133
  app.fetch;
41
134
  const frozenRoot = require_generation.frozenRootOf(app);
42
135
  const fetchLevelHook = require_utils.flattenChain(frozenRoot["~hookChain"]);
43
- if (fetchLevelHook?.request?.length || fetchLevelHook?.trace?.length || frozenRoot["~ext"]?.hoc?.length) return;
136
+ const promoteResponses = app["~config"]?.nativeStaticResponse !== false && !fetchLevelHook?.request?.length && !fetchLevelHook?.trace?.length && !frozenRoot["~ext"]?.hoc?.length;
44
137
  const table = app["~generation"]?.routeTable ?? app["~routeTable"];
45
138
  const length = table?.length ?? 0;
46
139
  if (!table || !length) return;
47
- const methods = table.method;
48
- const paths = table.path;
49
- const handlers = table.handler;
140
+ const { method: methods, path: paths, handler: handlers, flags } = table;
141
+ const isStatic = (i) => isNativeStaticMethod(methods[i]) && (flags[i] & require_route_table.RouteFlag.Dynamic) === 0;
142
+ const isPromotable = (h) => promoteResponses && typeof h !== "function" && !(h instanceof Error) && !(h instanceof Promise);
50
143
  let hasCandidate = false;
51
144
  for (let i = 0; i < length; i++) {
145
+ if (!isStatic(i)) continue;
52
146
  const h = handlers[i];
53
- if (typeof h === "function" || h instanceof Error || h instanceof Promise) continue;
54
- if (!nativeStaticMethods.has(methods[i])) continue;
55
- hasCandidate = true;
56
- break;
147
+ if (require_utils.isHTMLBundle(h) || isPromotable(h)) {
148
+ hasCandidate = true;
149
+ break;
150
+ }
57
151
  }
58
152
  if (!hasCandidate) return;
59
- const ready = require_utils.nullObject();
60
153
  const strictPath = frozenRoot["~config"]?.strictPath === true;
61
- const seen = /* @__PURE__ */ new Map();
62
- for (let i = 0; i < length; i++) seen.set(methods[i] + " " + paths[i], i);
63
- let explicitPaths;
64
- if (!strictPath) {
65
- explicitPaths = /* @__PURE__ */ new Map();
66
- for (let i = 0; i < length; i++) {
67
- const method = methods[i];
68
- const path = paths[i];
69
- let set = explicitPaths.get(method);
70
- if (!set) explicitPaths.set(method, set = /* @__PURE__ */ new Set());
71
- set.add(path);
72
- if (require_constants.needEncodeRegex.test(path)) {
73
- const encoded = encodeURI(path);
74
- if (encoded !== path) set.add(encoded);
75
- }
154
+ const routeIndex = /* @__PURE__ */ new Map();
155
+ for (let i = 0; i < length; i++) {
156
+ if (!isStatic(i)) continue;
157
+ const method = methods[i];
158
+ const path = paths[i];
159
+ let pathsByMethod = routeIndex.get(method);
160
+ if (!pathsByMethod) routeIndex.set(method, pathsByMethod = /* @__PURE__ */ new Map());
161
+ pathsByMethod.set(path, i);
162
+ if (!strictPath && (flags[i] & require_route_table.RouteFlag.Encode) !== 0) {
163
+ const encoded = encodeURI(path);
164
+ if (encoded !== path && !pathsByMethod.has(encoded)) pathsByMethod.set(encoded, -1);
76
165
  }
77
166
  }
78
- const add = (method, path, value) => {
79
- if (require_constants.needEncodeRegex.test(path)) path = encodeURI(path);
167
+ const ready = require_utils.nullObject();
168
+ let hasReady = false;
169
+ const add = (method, path, value, needsEncode) => {
170
+ if (needsEncode) path = encodeURI(path);
80
171
  (ready[path] ??= require_utils.nullObject())[method] = value;
172
+ hasReady = true;
81
173
  };
82
174
  for (let i = 0; i < length; i++) {
175
+ if (!isStatic(i)) continue;
83
176
  const method = methods[i];
84
- if (method === "WS") continue;
177
+ const pathsByMethod = routeIndex.get(method);
85
178
  const path = paths[i];
86
- if (seen.get(method + " " + path) !== i) continue;
87
- if (!nativeStaticMethods.has(method)) continue;
179
+ if (pathsByMethod.get(path) !== i) continue;
88
180
  const h = handlers[i];
89
- if (typeof h === "function" || h instanceof Error || h instanceof Promise) continue;
90
- const value = require_compile_handler_index.buildNativeStaticResponse(require_route_table.routeRow(table, i), app);
181
+ let value;
182
+ if (require_utils.isHTMLBundle(h)) value = h;
183
+ else if (!isPromotable(h)) continue;
184
+ else value = require_compile_handler_index.buildNativeStaticResponse(require_route_table.routeRow(table, i), app);
91
185
  if (!value) continue;
92
- add(method, path, value);
93
- if (!strictPath && !require_constants.isDynamicRegex.test(path)) {
186
+ const needsEncode = (flags[i] & require_route_table.RouteFlag.Encode) !== 0;
187
+ add(method, path, value, needsEncode);
188
+ if (!strictPath) {
94
189
  const loose = require_utils.getLoosePath(path);
95
- if (loose !== path && !explicitPaths?.get(method)?.has(loose)) add(method, loose, value);
190
+ if (loose !== path && !pathsByMethod.has(loose)) add(method, loose, value, needsEncode);
96
191
  }
97
192
  }
98
- if (!Object.keys(ready).length) return;
99
- return [ready, []];
193
+ if (!hasReady) return;
194
+ return ready;
100
195
  }
101
196
  const BunAdapter = require_adapter_index.createAdapter({
102
- name: "bun",
103
- runtime: "bun",
104
- isWebStandard: true,
105
197
  parse: require_adapter_web_standard_index.WebStandardAdapter.parse,
106
198
  response: require_adapter_web_standard_index.WebStandardAdapter.response,
107
199
  listen(app, options, callback) {
108
- const _config = app["~config"]?.serve;
109
- const optionsIsObject = typeof options === "object";
110
- let live;
111
- const gatedFetch = (request, server) => live ? live(request, server) : Promise.resolve(ready).then(() => {
112
- if (!live) throw new Error("[Elysia] Server was stopped before it was ready");
113
- return live(request, server);
114
- });
115
- const _options = optionsIsObject ? { ...options } : {
200
+ if (app.server || app["~ext"]?.stop) throw new Error("[Elysia] Cannot call listen() while a server or teardown is active");
201
+ const requestReady = Promise.withResolvers();
202
+ function gatedFetch(request, server) {
203
+ return live && !cancelled ? live(request, server) : requestReady.promise.then(() => {
204
+ if (cancelled || !live) return unavailableFetch();
205
+ return live(request, server);
206
+ });
207
+ }
208
+ const _options = typeof options === "object" ? {
209
+ ...options,
210
+ fetch: gatedFetch
211
+ } : {
116
212
  port: +options,
117
213
  fetch: gatedFetch
118
214
  };
119
- if (optionsIsObject) _options.fetch = gatedFetch;
215
+ const _config = app["~config"]?.serve;
120
216
  const serve = _config ? {
121
217
  ..._config,
122
218
  ..._options
123
219
  } : _options;
124
- let ready;
220
+ const handoff = (request, server) => serve.fetch(request, server);
221
+ const htmlRoutes = collectHTMLBundleRoutes(app, app.pending ? void 0 : handoff);
222
+ const server = app.server = Bun.serve(serve.routes || serve.error || htmlRoutes ? {
223
+ ...serve,
224
+ error: unavailableFetch,
225
+ routes: htmlRoutes ?? {}
226
+ } : serve);
227
+ let live;
228
+ let cancelled = false;
229
+ let shutdownAttempt;
230
+ let wsLifecycle;
231
+ let modulesReady;
232
+ let pendingSetups;
233
+ let setupsSettled;
125
234
  const build = () => {
126
235
  const fetch = app.fetch;
127
236
  let routes;
@@ -130,11 +239,22 @@ const BunAdapter = require_adapter_index.createAdapter({
130
239
  } catch (error) {
131
240
  console.warn("[Elysia] Native static promotion was skipped:", error);
132
241
  }
242
+ routes = collectHTMLBundleRoutes(app, handoff, routes);
133
243
  let websocket;
134
244
  if (app["~hasWS"]) {
135
245
  const resolved = require_generation.resolvedWsOf(app);
136
246
  if (!resolved) throw new Error("[Elysia] internal: WebSocket routes are present but no capability provider was resolved");
137
- websocket = resolved.config ? Object.assign(resolved.provider.buildGlobalWSHandler(), resolved.config) : resolved.provider.buildGlobalWSHandler();
247
+ const handler = resolved.provider.buildGlobalWSHandler();
248
+ websocket = resolved.config ? Object.assign(handler, resolved.config) : handler;
249
+ const setter = Object.getOwnPropertyDescriptor(handler, "~lifecycle")?.set;
250
+ if (setter) {
251
+ wsLifecycle = {
252
+ closing: false,
253
+ sockets: /* @__PURE__ */ new Set(),
254
+ run: runLifecycle.bind(null, epoch)
255
+ };
256
+ setter.call(handler, wsLifecycle);
257
+ }
138
258
  }
139
259
  return {
140
260
  fetch,
@@ -142,75 +262,292 @@ const BunAdapter = require_adapter_index.createAdapter({
142
262
  websocket
143
263
  };
144
264
  };
145
- let built;
146
- const server = app.server = Bun.serve(serve);
147
- const reload = () => {
265
+ let startupFailure;
266
+ let forceRequested = false;
267
+ const force = Promise.withResolvers();
268
+ const abandon = Promise.withResolvers();
269
+ let forceDone = false;
270
+ let nativeStopped = false;
271
+ let abandoned = false;
272
+ let published = false;
273
+ let cleanupCompleted = false;
274
+ let idleRequired = false;
275
+ let cleanupFailures;
276
+ let observedOutcome;
277
+ let persistentCleanup;
278
+ let persistentCleanupLength = -1;
279
+ const epochCleanup = [];
280
+ const createdExt = app["~ext"] === void 0;
281
+ const ext = app["~ext"] ??= require_utils.nullObject();
282
+ const epoch = {
283
+ setup: false,
284
+ invoking: false
285
+ };
286
+ const snapshotCleanup = () => {
287
+ if (persistentCleanupLength !== -1) return;
288
+ persistentCleanup = ext.cleanup;
289
+ persistentCleanupLength = persistentCleanup?.length ?? 0;
290
+ };
291
+ const registerCleanup = (handler) => {
292
+ if (ext.cleanupEpoch !== registerCleanup) return false;
293
+ if (!epoch.setup) throw new Error("[Elysia] .onStop() called after its onStart epoch settled");
294
+ if (Array.isArray(handler)) epochCleanup.push(...handler);
295
+ else epochCleanup.push(handler);
296
+ return true;
297
+ };
298
+ const gate = () => {
299
+ if (!published) return;
300
+ live = void 0;
301
+ if (wsLifecycle) wsLifecycle.closing = true;
148
302
  try {
149
- server.reload(serve);
303
+ server.reload({
304
+ ...serve,
305
+ fetch: unavailableFetch,
306
+ error: unavailableFetch,
307
+ routes: {}
308
+ });
150
309
  } catch (error) {
151
- if (!serve.routes) throw error;
152
- delete serve.routes;
153
- console.warn("[Elysia] Native static promotion was skipped:", error);
154
- try {
155
- server.reload(serve);
156
- } catch (fallbackError) {
157
- console.error("[Elysia] Failed to reload Bun server:", fallbackError);
158
- throw fallbackError;
310
+ return { error };
311
+ }
312
+ };
313
+ const nativeStop = async (closeActiveConnections, errors) => {
314
+ try {
315
+ const stopping = server.stop(closeActiveConnections);
316
+ await (closeActiveConnections ? stopping : Promise.race([stopping, force.promise]));
317
+ if (!closeActiveConnections && forceRequested && !forceDone) return false;
318
+ nativeStopped = true;
319
+ if (closeActiveConnections) {
320
+ forceDone = true;
321
+ forceRequested = false;
322
+ }
323
+ return true;
324
+ } catch (error) {
325
+ if (closeActiveConnections) forceRequested = true;
326
+ errors.push(error);
327
+ return false;
328
+ }
329
+ };
330
+ const isAbandoned = () => abandoned;
331
+ const quiesce = async (force) => {
332
+ const errors = [];
333
+ if (!published) return {
334
+ errors,
335
+ releasable: await nativeStop(true, errors),
336
+ safe: true
337
+ };
338
+ if (force && !idleRequired && !wsLifecycle?.closing) {
339
+ const stopped = await nativeStop(true, errors);
340
+ return {
341
+ errors,
342
+ releasable: stopped,
343
+ safe: stopped
344
+ };
345
+ }
346
+ const gateFailure = gate();
347
+ if (gateFailure) errors.push(gateFailure.error);
348
+ let socketsSettled = await settleServerWebSockets(wsLifecycle, errors);
349
+ await Promise.resolve();
350
+ let close = forceRequested || gateFailure !== void 0 || !socketsSettled;
351
+ if (!close && nativeStopped) await waitForServerRequests(server, isAbandoned);
352
+ idleRequired = true;
353
+ let stopped = true;
354
+ while (!nativeStopped || forceRequested && !forceDone) {
355
+ if (nativeStopped) close = true;
356
+ stopped = await nativeStop(close, errors);
357
+ if (stopped) {
358
+ if (socketsSettled) socketsSettled = await settleServerWebSockets(wsLifecycle, errors);
359
+ continue;
360
+ }
361
+ if (close) break;
362
+ await waitForServerRequests(server, isAbandoned);
363
+ close = true;
364
+ }
365
+ let idleClosed = true;
366
+ if (idleRequired) {
367
+ const closeIdle = server.closeIdleConnections;
368
+ if (closeIdle) try {
369
+ closeIdle.call(server);
370
+ } catch (error) {
371
+ errors.push(error);
372
+ idleClosed = false;
159
373
  }
374
+ if (idleClosed && !gateFailure) idleRequired = false;
160
375
  }
376
+ const safe = socketsSettled && stopped && idleClosed && (!gateFailure || forceDone);
377
+ return {
378
+ errors,
379
+ releasable: safe,
380
+ safe
381
+ };
161
382
  };
162
383
  const setup = () => {
163
- const onSetup = app["~ext"]?.setup;
384
+ snapshotCleanup();
385
+ const onSetup = ext.setup;
164
386
  if (!onSetup) return;
165
- let pendingSetups;
387
+ epoch.setup = true;
388
+ ext.cleanupEpoch = registerCleanup;
166
389
  for (let i = 0; i < onSetup.length; i++) {
167
- const result = onSetup[i](app);
168
- if (result && typeof result.then === "function") (pendingSetups ??= []).push(result);
390
+ if (cancelled) break;
391
+ try {
392
+ const result = runLifecycle(epoch, onSetup[i], app);
393
+ if (result && typeof result.then === "function") (pendingSetups ??= []).push(Promise.resolve(result));
394
+ } catch (error) {
395
+ if (!pendingSetups) epoch.setup = false;
396
+ stop(true, { error }).catch(() => {});
397
+ throw error;
398
+ }
399
+ }
400
+ if (pendingSetups) {
401
+ setupsSettled = Promise.allSettled(pendingSetups);
402
+ return Promise.all(pendingSetups).then(() => epoch.setup = false);
169
403
  }
170
- if (pendingSetups) return Promise.all(pendingSetups);
404
+ epoch.setup = false;
171
405
  };
172
- const rollback = (error) => {
173
- if (app.server !== server) return;
174
- try {
175
- server.stop(true);
176
- } catch (stopError) {
177
- console.error(stopError);
178
- } finally {
179
- app.server = void 0;
180
- }
181
- const cleanup = app["~ext"]?.cleanup;
182
- if (cleanup) for (let i = cleanup.length - 1; i >= 0; i--) try {
183
- const result = cleanup[i](app);
184
- if (result && typeof result.then === "function") Promise.resolve(result).catch(console.error);
185
- } catch (cleanupError) {
186
- console.error(cleanupError);
187
- }
188
- return error;
406
+ const stop = (closeActiveConnections, failure) => {
407
+ const reentrant = epoch.invoking;
408
+ if (failure) startupFailure ??= failure;
409
+ if (closeActiveConnections === true) {
410
+ force.resolve();
411
+ if (!failure) abandon.resolve();
412
+ if (!forceDone) forceRequested = true;
413
+ }
414
+ cancelled = true;
415
+ requestReady.resolve();
416
+ if (app.server === server) app.server = void 0;
417
+ let outcome;
418
+ if (shutdownAttempt) {
419
+ if (closeActiveConnections === true && !forceDone) {
420
+ abandoned = true;
421
+ if (wsLifecycle) wsLifecycle.abandon = true;
422
+ }
423
+ outcome = shutdownAttempt;
424
+ } else {
425
+ const { promise, resolve, reject } = Promise.withResolvers();
426
+ outcome = shutdownAttempt = promise;
427
+ const quiescence = quiesce(forceRequested || !published);
428
+ let releasable = false;
429
+ let abandonedSetups;
430
+ (async () => {
431
+ try {
432
+ await Promise.resolve();
433
+ if (modulesReady) try {
434
+ await Promise.race([modulesReady, force.promise]);
435
+ } catch (error) {
436
+ startupFailure ??= { error };
437
+ }
438
+ if (setupsSettled) {
439
+ const setupResults = await Promise.race([setupsSettled, abandon.promise]);
440
+ if (!setupResults) abandonedSetups = setupsSettled;
441
+ else if (!startupFailure) for (let i = 0; i < setupResults.length; i++) {
442
+ const result = setupResults[i];
443
+ if (result.status === "rejected") {
444
+ startupFailure = { error: result.reason };
445
+ break;
446
+ }
447
+ }
448
+ }
449
+ epoch.setup = false;
450
+ const errors = [];
451
+ const quiesced = await quiescence;
452
+ if (startupFailure) errors.push(startupFailure.error);
453
+ errors.push(...quiesced.errors);
454
+ releasable = quiesced.releasable;
455
+ if (!quiesced.safe) {
456
+ if (cleanupFailures) errors.push(...cleanupFailures);
457
+ require_utils.throwLifecycleErrors(errors);
458
+ }
459
+ if (!cleanupCompleted) {
460
+ snapshotCleanup();
461
+ if (ext.stop === stop) {
462
+ const length = persistentCleanupLength + epochCleanup.length;
463
+ for (let n = 0; n < length; n++) {
464
+ if (ext.stop !== stop) break;
465
+ const i = startupFailure ? length - 1 - n : n;
466
+ const handler = i < persistentCleanupLength ? persistentCleanup[i] : epochCleanup[i - persistentCleanupLength];
467
+ try {
468
+ await runLifecycle(epoch, handler, app);
469
+ } catch (error) {
470
+ (cleanupFailures ??= []).push(error);
471
+ }
472
+ }
473
+ }
474
+ cleanupCompleted = true;
475
+ epochCleanup.length = 0;
476
+ }
477
+ if (cleanupFailures) errors.push(...cleanupFailures);
478
+ const release = () => releaseLifecycle(app, ext, createdExt, stop, registerCleanup);
479
+ if (releasable && abandonedSetups) abandonedSetups.then((results) => {
480
+ for (const result of results) if (result.status === "rejected") console.error("[Elysia] onStart abandoned by stop(true) failed:", result.reason);
481
+ release();
482
+ });
483
+ else if (releasable) release();
484
+ require_utils.throwLifecycleErrors(errors);
485
+ } finally {
486
+ epoch.setup = false;
487
+ if (!releasable) shutdownAttempt = void 0;
488
+ }
489
+ })().then(resolve, reject);
490
+ }
491
+ if (!reentrant) return outcome;
492
+ if (observedOutcome !== outcome) {
493
+ observedOutcome = outcome;
494
+ outcome.catch((error) => {
495
+ console.error("[Elysia] stop() failed:", error);
496
+ });
497
+ }
498
+ return Promise.resolve();
189
499
  };
500
+ ext.stop = stop;
190
501
  const publish = () => {
191
- if (app.server !== server) return;
192
- built ??= build();
193
- live = serve.fetch = withOrigin(built.fetch);
194
- if (built.websocket) serve.websocket = built.websocket;
195
- if (built.routes) serve.routes = built.routes[0];
196
- reload();
197
- if (callback) callback(server);
502
+ if (cancelled || app.server !== server) {
503
+ requestReady.resolve();
504
+ return;
505
+ }
506
+ try {
507
+ const built = build();
508
+ live = serve.fetch = withOrigin(built.fetch);
509
+ published = true;
510
+ if (built.websocket) serve.websocket = built.websocket;
511
+ if (built.routes) serve.routes = built.routes;
512
+ try {
513
+ server.reload(serve);
514
+ } catch (error) {
515
+ if (!serve.routes) throw error;
516
+ delete serve.routes;
517
+ console.warn("[Elysia] Native static promotion was skipped:", error);
518
+ try {
519
+ server.reload(serve);
520
+ } catch (fallbackError) {
521
+ console.error("[Elysia] Failed to reload Bun server:", fallbackError);
522
+ throw fallbackError;
523
+ }
524
+ }
525
+ if (callback) callback(server);
526
+ } catch (error) {
527
+ startupFailure ??= { error };
528
+ throw error;
529
+ }
530
+ requestReady.resolve();
198
531
  };
199
532
  const start = () => {
200
- if (app.server !== server) return;
533
+ modulesReady = void 0;
534
+ if (cancelled || app.server !== server) {
535
+ requestReady.resolve();
536
+ return;
537
+ }
201
538
  const setupReady = setup();
202
- if (setupReady && typeof setupReady.then === "function") return Promise.resolve(setupReady).then(publish);
539
+ if (setupReady) return setupReady.then(publish);
203
540
  publish();
204
541
  };
205
542
  try {
206
- if (app.pending) ready = app.modules.then(start);
207
- else ready = Promise.resolve().then(start);
208
- ready.catch((error) => {
543
+ const modules = modulesReady = app.modules;
544
+ const preload = require_type_bridge.preloadTypebox();
545
+ (preload ? Promise.all([modules, preload]) : modules).then(start).catch((error) => stop(true, { error })).catch((error) => {
209
546
  console.error("[Elysia] listen() failed:", error);
210
- return rollback(error);
547
+ if (typeof process !== "undefined") process.exitCode = 1;
211
548
  });
212
549
  } catch (error) {
213
- rollback(error);
550
+ stop(true, { error }).catch(console.error);
214
551
  throw error;
215
552
  }
216
553
  }
@@ -218,4 +555,6 @@ const BunAdapter = require_adapter_index.createAdapter({
218
555
 
219
556
  //#endregion
220
557
  exports.BunAdapter = BunAdapter;
221
- exports.collectStaticRoutes = collectStaticRoutes;
558
+ exports.collectHTMLBundleRoutes = collectHTMLBundleRoutes;
559
+ exports.collectStaticRoutes = collectStaticRoutes;
560
+ exports.isHTMLBundle = require_utils.isHTMLBundle;