elysia 2.0.0-beta.3 → 2.0.0-beta.31

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 +387 -207
  26. package/dist/base.js +742 -676
  27. package/dist/base.mjs +747 -680
  28. package/dist/compile/aot-capture.d.ts +2 -13
  29. package/dist/compile/aot-capture.js +39 -89
  30. package/dist/compile/aot-capture.mjs +43 -88
  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 +21 -9
  53. package/dist/compile/handler/params.mjs +25 -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 +184 -276
  62. package/dist/compile/handler/utils.mjs +179 -275
  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 +127 -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 +18 -18
  106. package/dist/handler/error.js +77 -120
  107. package/dist/handler/error.mjs +80 -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 +3 -7
  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 +3 -1
  283. package/dist/type/typebox-value-live.js +5 -1
  284. package/dist/type/typebox-value-live.mjs +4 -1
  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 +9 -4
  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 +596 -187
  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 +295 -221
  357. package/dist/ws/route.mjs +302 -227
  358. package/dist/ws/types.d.ts +6 -7
  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,22 +1,57 @@
1
+ // @bun
1
2
  import { createAdapter } from "../index.mjs";
2
- import { isDynamicRegex, needEncodeRegex } from "../../constants.mjs";
3
- import { flattenChain, getLoosePath, nullObject } from "../../utils.mjs";
3
+ import { flattenChain, getLoosePath, isHTMLBundle, isSocketQuiet, nullObject, throwLifecycleErrors } from "../../utils.mjs";
4
+ import { preloadTypebox } from "../../type/bridge.mjs";
5
+ import { isProduction } from "../../universal/is-production.mjs";
4
6
  import { WebStandardAdapter } from "../web-standard/index.mjs";
5
7
  import { frozenRootOf, resolvedWsOf } from "../../generation.mjs";
6
8
  import { origin } from "../origin.mjs";
7
9
  import { buildNativeStaticResponse } from "../../compile/handler/index.mjs";
8
- import { routeRow } from "../../route-table.mjs";
10
+ import { RouteFlag, routeRow } from "../../route-table.mjs";
9
11
 
10
12
  //#region src/adapter/bun/index.ts
11
- /**
12
- * ! This may looks like it would cause race condition, but it is not
13
- * Bun is single-threaded and synchronous, so the `finally` block will
14
- * always run before the next request comes in
15
- * @see ../origin.ts
16
- *
17
- * `finally` runs on the synchronous return of `fetch` (the returned promise is
18
- * not awaited), so the slot is live only for the handler's synchronous prologue
19
- */
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 || isSocketQuiet(socket)) {
49
+ delete socket.data["~lifecycleRun"];
50
+ lifecycle.sockets.delete(socket);
51
+ }
52
+ } while (lifecycle.sockets.size);
53
+ return true;
54
+ }
20
55
  const withOrigin = (fetch) => (request, server) => {
21
56
  origin.request = request;
22
57
  try {
@@ -25,102 +60,177 @@ const withOrigin = (fetch) => (request, server) => {
25
60
  origin.request = void 0;
26
61
  }
27
62
  };
28
- const nativeStaticMethods = new Set([
29
- "GET",
30
- "POST",
31
- "PUT",
32
- "DELETE",
33
- "PATCH",
34
- "HEAD",
35
- "OPTIONS"
36
- ]);
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]) && isHTMLBundle(h))) return routes;
91
+ const declared = table ? table.method.map((_, i) => routeRow(table, i)) : app["~routes"];
92
+ for (const route of declared) {
93
+ const [method, path, handler] = route;
94
+ if (!isNativeStaticMethod(method) || !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 ??= nullObject())[path] ??= nullObject())[method] = handler;
104
+ if (!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 (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 : getLoosePath(variant);
115
+ for (const raw of loose === variant ? [variant] : [variant, loose]) {
116
+ if (!isNativePath(raw)) {
117
+ if (!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 ??= nullObject())[path] ??= nullObject())[method] = bundle;
129
+ for (const [key, method] of handoffs) (routes[key] ??= nullObject())[method] ??= handoff;
130
+ return routes;
131
+ }
37
132
  function collectStaticRoutes(app) {
38
- if (app["~config"]?.nativeStaticResponse === false) return;
39
133
  app.fetch;
40
134
  const frozenRoot = frozenRootOf(app);
41
135
  const fetchLevelHook = flattenChain(frozenRoot["~hookChain"]);
42
- 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;
43
137
  const table = app["~generation"]?.routeTable ?? app["~routeTable"];
44
138
  const length = table?.length ?? 0;
45
139
  if (!table || !length) return;
46
- const methods = table.method;
47
- const paths = table.path;
48
- const handlers = table.handler;
140
+ const { method: methods, path: paths, handler: handlers, flags } = table;
141
+ const isStatic = (i) => isNativeStaticMethod(methods[i]) && (flags[i] & RouteFlag.Dynamic) === 0;
142
+ const isPromotable = (h) => promoteResponses && typeof h !== "function" && !(h instanceof Error) && !(h instanceof Promise);
49
143
  let hasCandidate = false;
50
144
  for (let i = 0; i < length; i++) {
145
+ if (!isStatic(i)) continue;
51
146
  const h = handlers[i];
52
- if (typeof h === "function" || h instanceof Error || h instanceof Promise) continue;
53
- if (!nativeStaticMethods.has(methods[i])) continue;
54
- hasCandidate = true;
55
- break;
147
+ if (isHTMLBundle(h) || isPromotable(h)) {
148
+ hasCandidate = true;
149
+ break;
150
+ }
56
151
  }
57
152
  if (!hasCandidate) return;
58
- const ready = nullObject();
59
153
  const strictPath = frozenRoot["~config"]?.strictPath === true;
60
- const seen = /* @__PURE__ */ new Map();
61
- for (let i = 0; i < length; i++) seen.set(methods[i] + " " + paths[i], i);
62
- let explicitPaths;
63
- if (!strictPath) {
64
- explicitPaths = /* @__PURE__ */ new Map();
65
- for (let i = 0; i < length; i++) {
66
- const method = methods[i];
67
- const path = paths[i];
68
- let set = explicitPaths.get(method);
69
- if (!set) explicitPaths.set(method, set = /* @__PURE__ */ new Set());
70
- set.add(path);
71
- if (needEncodeRegex.test(path)) {
72
- const encoded = encodeURI(path);
73
- if (encoded !== path) set.add(encoded);
74
- }
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] & RouteFlag.Encode) !== 0) {
163
+ const encoded = encodeURI(path);
164
+ if (encoded !== path && !pathsByMethod.has(encoded)) pathsByMethod.set(encoded, -1);
75
165
  }
76
166
  }
77
- const add = (method, path, value) => {
78
- if (needEncodeRegex.test(path)) path = encodeURI(path);
167
+ const ready = nullObject();
168
+ let hasReady = false;
169
+ const add = (method, path, value, needsEncode) => {
170
+ if (needsEncode) path = encodeURI(path);
79
171
  (ready[path] ??= nullObject())[method] = value;
172
+ hasReady = true;
80
173
  };
81
174
  for (let i = 0; i < length; i++) {
175
+ if (!isStatic(i)) continue;
82
176
  const method = methods[i];
83
- if (method === "WS") continue;
177
+ const pathsByMethod = routeIndex.get(method);
84
178
  const path = paths[i];
85
- if (seen.get(method + " " + path) !== i) continue;
86
- if (!nativeStaticMethods.has(method)) continue;
179
+ if (pathsByMethod.get(path) !== i) continue;
87
180
  const h = handlers[i];
88
- if (typeof h === "function" || h instanceof Error || h instanceof Promise) continue;
89
- const value = buildNativeStaticResponse(routeRow(table, i), app);
181
+ let value;
182
+ if (isHTMLBundle(h)) value = h;
183
+ else if (!isPromotable(h)) continue;
184
+ else value = buildNativeStaticResponse(routeRow(table, i), app);
90
185
  if (!value) continue;
91
- add(method, path, value);
92
- if (!strictPath && !isDynamicRegex.test(path)) {
186
+ const needsEncode = (flags[i] & RouteFlag.Encode) !== 0;
187
+ add(method, path, value, needsEncode);
188
+ if (!strictPath) {
93
189
  const loose = getLoosePath(path);
94
- if (loose !== path && !explicitPaths?.get(method)?.has(loose)) add(method, loose, value);
190
+ if (loose !== path && !pathsByMethod.has(loose)) add(method, loose, value, needsEncode);
95
191
  }
96
192
  }
97
- if (!Object.keys(ready).length) return;
98
- return [ready, []];
193
+ if (!hasReady) return;
194
+ return ready;
99
195
  }
100
196
  const BunAdapter = createAdapter({
101
- name: "bun",
102
- runtime: "bun",
103
- isWebStandard: true,
104
197
  parse: WebStandardAdapter.parse,
105
198
  response: WebStandardAdapter.response,
106
199
  listen(app, options, callback) {
107
- const _config = app["~config"]?.serve;
108
- const optionsIsObject = typeof options === "object";
109
- let live;
110
- const gatedFetch = (request, server) => live ? live(request, server) : Promise.resolve(ready).then(() => {
111
- if (!live) throw new Error("[Elysia] Server was stopped before it was ready");
112
- return live(request, server);
113
- });
114
- 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
+ } : {
115
212
  port: +options,
116
213
  fetch: gatedFetch
117
214
  };
118
- if (optionsIsObject) _options.fetch = gatedFetch;
215
+ const _config = app["~config"]?.serve;
119
216
  const serve = _config ? {
120
217
  ..._config,
121
218
  ..._options
122
219
  } : _options;
123
- 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;
124
234
  const build = () => {
125
235
  const fetch = app.fetch;
126
236
  let routes;
@@ -129,11 +239,22 @@ const BunAdapter = createAdapter({
129
239
  } catch (error) {
130
240
  console.warn("[Elysia] Native static promotion was skipped:", error);
131
241
  }
242
+ routes = collectHTMLBundleRoutes(app, handoff, routes);
132
243
  let websocket;
133
244
  if (app["~hasWS"]) {
134
245
  const resolved = resolvedWsOf(app);
135
246
  if (!resolved) throw new Error("[Elysia] internal: WebSocket routes are present but no capability provider was resolved");
136
- 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
+ }
137
258
  }
138
259
  return {
139
260
  fetch,
@@ -141,79 +262,296 @@ const BunAdapter = createAdapter({
141
262
  websocket
142
263
  };
143
264
  };
144
- let built;
145
- const server = app.server = Bun.serve(serve);
146
- 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"] ??= 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;
147
302
  try {
148
- server.reload(serve);
303
+ server.reload({
304
+ ...serve,
305
+ fetch: unavailableFetch,
306
+ error: unavailableFetch,
307
+ routes: {}
308
+ });
149
309
  } catch (error) {
150
- if (!serve.routes) throw error;
151
- delete serve.routes;
152
- console.warn("[Elysia] Native static promotion was skipped:", error);
153
- try {
154
- server.reload(serve);
155
- } catch (fallbackError) {
156
- console.error("[Elysia] Failed to reload Bun server:", fallbackError);
157
- 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;
158
373
  }
374
+ if (idleClosed && !gateFailure) idleRequired = false;
159
375
  }
376
+ const safe = socketsSettled && stopped && idleClosed && (!gateFailure || forceDone);
377
+ return {
378
+ errors,
379
+ releasable: safe,
380
+ safe
381
+ };
160
382
  };
161
383
  const setup = () => {
162
- const onSetup = app["~ext"]?.setup;
384
+ snapshotCleanup();
385
+ const onSetup = ext.setup;
163
386
  if (!onSetup) return;
164
- let pendingSetups;
387
+ epoch.setup = true;
388
+ ext.cleanupEpoch = registerCleanup;
165
389
  for (let i = 0; i < onSetup.length; i++) {
166
- const result = onSetup[i](app);
167
- 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);
168
403
  }
169
- if (pendingSetups) return Promise.all(pendingSetups);
404
+ epoch.setup = false;
170
405
  };
171
- const rollback = (error) => {
172
- if (app.server !== server) return;
173
- try {
174
- server.stop(true);
175
- } catch (stopError) {
176
- console.error(stopError);
177
- } finally {
178
- app.server = void 0;
179
- }
180
- const cleanup = app["~ext"]?.cleanup;
181
- if (cleanup) for (let i = cleanup.length - 1; i >= 0; i--) try {
182
- const result = cleanup[i](app);
183
- if (result && typeof result.then === "function") Promise.resolve(result).catch(console.error);
184
- } catch (cleanupError) {
185
- console.error(cleanupError);
186
- }
187
- 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
+ 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
+ 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();
188
499
  };
500
+ ext.stop = stop;
189
501
  const publish = () => {
190
- if (app.server !== server) return;
191
- built ??= build();
192
- live = serve.fetch = withOrigin(built.fetch);
193
- if (built.websocket) serve.websocket = built.websocket;
194
- if (built.routes) serve.routes = built.routes[0];
195
- reload();
196
- 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();
197
531
  };
198
532
  const start = () => {
199
- if (app.server !== server) return;
533
+ modulesReady = void 0;
534
+ if (cancelled || app.server !== server) {
535
+ requestReady.resolve();
536
+ return;
537
+ }
200
538
  const setupReady = setup();
201
- if (setupReady && typeof setupReady.then === "function") return Promise.resolve(setupReady).then(publish);
539
+ if (setupReady) return setupReady.then(publish);
202
540
  publish();
203
541
  };
204
542
  try {
205
- if (app.pending) ready = app.modules.then(start);
206
- else ready = Promise.resolve().then(start);
207
- ready.catch((error) => {
543
+ const modules = modulesReady = app.modules;
544
+ const preload = preloadTypebox();
545
+ (preload ? Promise.all([modules, preload]) : modules).then(start).catch((error) => stop(true, { error })).catch((error) => {
208
546
  console.error("[Elysia] listen() failed:", error);
209
- return rollback(error);
547
+ if (typeof process !== "undefined") process.exitCode = 1;
210
548
  });
211
549
  } catch (error) {
212
- rollback(error);
550
+ stop(true, { error }).catch(console.error);
213
551
  throw error;
214
552
  }
215
553
  }
216
554
  });
217
555
 
218
556
  //#endregion
219
- export { BunAdapter, collectStaticRoutes };
557
+ export { BunAdapter, collectHTMLBundleRoutes, collectStaticRoutes, isHTMLBundle };
@@ -1,3 +1,4 @@
1
+ // @bun
1
2
  import { isBun } from "../universal/constants.mjs";
2
3
  import { WebStandardAdapter } from "./web-standard/index.mjs";
3
4
  import { BunAdapter } from "./bun/index.mjs";