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
@@ -1,6 +1,6 @@
1
1
  import { ListenCallback, Serve } from "../universal/server.js";
2
- import { MaybePromise } from "../types.js";
3
2
  import { Context } from "../context.js";
3
+ import { MaybePromise } from "../types.js";
4
4
  import { AnyElysia } from "../base.js";
5
5
 
6
6
  //#region src/adapter/types.d.ts
@@ -10,25 +10,6 @@ import { AnyElysia } from "../base.js";
10
10
  * @since 2.0.0
11
11
  */
12
12
  interface ElysiaAdapterOptions<App extends AnyElysia | void = void> {
13
- /**
14
- * Name of the adapter, preferably runtime
15
- */
16
- name: string;
17
- /**
18
- *
19
- * @param app
20
- */
21
- runtime: 'node' | 'deno' | 'bun' | 'cloudflare-worker' | 'browser' | 'vercel' | 'netlify' | 'lambda' | 'fastly' | 'edge' | 'unknown' | (string & {});
22
- /**
23
- * Whether this adapter is web standard
24
- */
25
- isWebStandard: boolean;
26
- /**
27
- * Whether this adapter supports WebSocket.
28
- *
29
- * `.ws()` throws on non-Bun runtimes unless this is set.
30
- */
31
- websocket?: boolean;
32
13
  listen?(app: AnyElysia, options: string | number | Partial<Serve>, callback?: ListenCallback): void;
33
14
  parse: {
34
15
  json: (context: Context) => MaybePromise<Record<keyof any, undefined> | unknown[]>;
@@ -36,7 +17,7 @@ interface ElysiaAdapterOptions<App extends AnyElysia | void = void> {
36
17
  urlencoded: (context: Context) => MaybePromise<Record<string, string | string[]>>;
37
18
  arrayBuffer: (context: Context) => MaybePromise<ArrayBuffer>;
38
19
  formData: (context: Context) => MaybePromise<Record<string, unknown>>;
39
- default: (context: Context, contentType: string, normalized?: boolean) => MaybePromise<any>;
20
+ default: (context: Context, contentType: string) => MaybePromise<any>;
40
21
  };
41
22
  response: {
42
23
  /**
@@ -1,8 +1,8 @@
1
1
  import { BaseCookie } from "../cookie/types.js";
2
- import { ElysiaFile } from "../universal/file.js";
3
2
  import { StatusMap } from "../constants.js";
4
- import { HTTPHeaders } from "../types.js";
5
3
  import { Context } from "../context.js";
4
+ import { ElysiaFile } from "../universal/file.js";
5
+ import { HTTPHeaders } from "../types.js";
6
6
  //#region src/adapter/utils.d.ts
7
7
  declare function materializeSetHeaders(set: Context['set']): any;
8
8
  declare function normalizeContentType(contentType: string): string;
@@ -12,6 +12,7 @@ declare function responseToSetHeaders(response: Response, set?: Context['set']):
12
12
  headers: HTTPHeaders;
13
13
  status?: number | keyof StatusMap;
14
14
  cookie?: Record<string, BaseCookie>;
15
+ redirect?: never;
15
16
  } | {
16
17
  headers: any;
17
18
  status: number | "Continue" | "Switching Protocols" | "Processing" | "Early Hints" | "OK" | "Created" | "Accepted" | "Non-Authoritative Information" | "No Content" | "Reset Content" | "Partial Content" | "Multi-Status" | "Already Reported" | "Multiple Choices" | "Moved Permanently" | "Found" | "See Other" | "Not Modified" | "Temporary Redirect" | "Permanent Redirect" | "Bad Request" | "Unauthorized" | "Payment Required" | "Forbidden" | "Not Found" | "Method Not Allowed" | "Not Acceptable" | "Proxy Authentication Required" | "Request Timeout" | "Conflict" | "Gone" | "Length Required" | "Precondition Failed" | "Payload Too Large" | "URI Too Long" | "Unsupported Media Type" | "Range Not Satisfiable" | "Expectation Failed" | "I'm a teapot" | "Enhance Your Calm" | "Misdirected Request" | "Unprocessable Content" | "Locked" | "Failed Dependency" | "Too Early" | "Upgrade Required" | "Precondition Required" | "Too Many Requests" | "Request Header Fields Too Large" | "Unavailable For Legal Reasons" | "Internal Server Error" | "Not Implemented" | "Bad Gateway" | "Service Unavailable" | "Gateway Timeout" | "HTTP Version Not Supported" | "Variant Also Negotiates" | "Insufficient Storage" | "Loop Detected" | "Not Extended" | "Network Authentication Required";
@@ -27,6 +28,16 @@ declare function createStreamHandler({
27
28
  declare function streamResponse(response: Response): AsyncGenerator<any, void, any>;
28
29
  declare function handleSet(set: Context['set']): void;
29
30
  declare function createResponseHandler(handler: CreateHandlerParameter): (response: Response, set?: Context["set"], request?: Request, owned?: boolean) => any;
31
+ /**
32
+ * A returned `ReadableStream` is consumed after the handler returns, so
33
+ * afterResponse, `defer()` and derive dispose must wait for it the way a tee'd
34
+ * generator is waited on
35
+ *
36
+ * A returned `Response` is not observed, reading `.body` would move
37
+ * every Response on these routes off Bun's native send path; a `bytes()`
38
+ * stream keeps its exact-stream contract and releases early as before
39
+ */
40
+ declare function observeStream(source: ReadableStream): [ReadableStream, AsyncIterable<unknown>, AsyncIterableIterator<unknown>] | undefined;
30
41
  /**
31
42
  * Split async source into `branches` independent iterators
32
43
  *
@@ -47,4 +58,4 @@ declare function createResponseHandler(handler: CreateHandlerParameter): (respon
47
58
  */
48
59
  declare function tee<T>(source: AsyncIterable<T>, branches?: number, cap?: number, capBytes?: number): AsyncIterableIterator<T>[];
49
60
  //#endregion
50
- export { createResponseHandler, createStreamHandler, handleFile, handleSet, materializeSetHeaders, normalizeContentType, parseSetCookies, responseToSetHeaders, streamResponse, tee };
61
+ export { createResponseHandler, createStreamHandler, handleFile, handleSet, materializeSetHeaders, normalizeContentType, observeStream, parseSetCookies, responseToSetHeaders, streamResponse, tee };
@@ -14,25 +14,19 @@ function materializeSetHeaders(set) {
14
14
  if (headers["\0"] !== headers) return headers;
15
15
  return set.headers = Object.assign(require_utils.nullObject(), headers);
16
16
  }
17
- const sseFormat = (data) => `data: ${data}\n\n`;
17
+ const sseFormat = (data) => require_utils.sseData(data) + "\n";
18
18
  const identityFormat = (data) => data;
19
19
  const textEncoder = new TextEncoder();
20
- const encodeChunk = (s) => textEncoder.encode(s);
21
20
  function normalizeContentType(contentType) {
22
21
  if (contentType === "application/json") return contentType;
23
22
  const end = contentType.indexOf(";");
24
23
  return (end === -1 ? contentType : contentType.slice(0, end)).trim().toLowerCase();
25
24
  }
26
25
  function handleFile(response, set, request, size = response.size) {
27
- if (!require_universal_constants.isBun && response instanceof Promise) return response.then((res) => handleFile(res, set, request, size));
28
26
  const rangeHeader = request?.headers.get("range");
29
27
  if (rangeHeader) {
30
28
  const match = /bytes=(\d*)-(\d*)/.exec(rangeHeader);
31
29
  if (match) {
32
- if (!match[1] && !match[2]) return new Response(null, {
33
- status: 416,
34
- headers: mergeHeaders(new Headers({ "content-range": `bytes */${size}` }), set?.headers ?? require_utils.nullObject())
35
- });
36
30
  let start;
37
31
  let end;
38
32
  if (!match[1] && match[2]) {
@@ -43,7 +37,7 @@ function handleFile(response, set, request, size = response.size) {
43
37
  start = match[1] ? parseInt(match[1]) : 0;
44
38
  end = match[2] ? Math.min(parseInt(match[2]), size - 1) : size - 1;
45
39
  }
46
- if (start >= size || start > end) return new Response(null, {
40
+ if (!match[1] && !match[2] || start >= size || start > end) return new Response(null, {
47
41
  status: 416,
48
42
  headers: mergeHeaders(new Headers({ "content-range": `bytes */${size}` }), set?.headers ?? require_utils.nullObject())
49
43
  });
@@ -75,13 +69,9 @@ function handleFile(response, set, request, size = response.size) {
75
69
  }
76
70
  return new Response(body, set);
77
71
  }
78
- if (require_utils.isNotEmpty(set.headers)) return new Response(body, {
79
- status: set.status,
80
- headers: Object.assign(defaultHeader, set.headers)
81
- });
82
72
  return new Response(body, {
83
73
  status: set.status,
84
- headers: defaultHeader
74
+ headers: Object.assign(defaultHeader, set.headers)
85
75
  });
86
76
  }
87
77
  function normalizeHeaders(set) {
@@ -124,27 +114,9 @@ function responseToSetHeaders(response, set) {
124
114
  if (set.headers["content-encoding"]) delete set.headers["content-encoding"];
125
115
  return set;
126
116
  }
127
- function enqueueBinaryChunk(controller, chunk) {
128
- if (chunk instanceof Blob) return chunk.arrayBuffer().then((buffer) => {
129
- controller.enqueue(new Uint8Array(buffer));
130
- return true;
131
- });
132
- if (chunk instanceof Uint8Array) {
133
- controller.enqueue(chunk);
134
- return true;
135
- }
136
- if (chunk instanceof ArrayBuffer) {
137
- controller.enqueue(new Uint8Array(chunk));
138
- return true;
139
- }
140
- if (ArrayBuffer.isView(chunk)) {
141
- controller.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
142
- return true;
143
- }
144
- return false;
145
- }
146
117
  function createStreamHandler({ mapResponse, mapCompactResponse }) {
147
118
  return async (generator, set, request, skipFormat, owned = false) => {
119
+ if (request === null) return void 0;
148
120
  if (require_utils.isByteStream(generator)) {
149
121
  if (generator.locked) throw new TypeError("Cannot transfer a locked or consumed byte stream");
150
122
  if (set) {
@@ -158,10 +130,12 @@ function createStreamHandler({ mapResponse, mapCompactResponse }) {
158
130
  return new Response(generator, { headers: { "content-type": "application/octet-stream" } });
159
131
  }
160
132
  const typedSSE = !skipFormat && "sse" in generator && generator.sse === true;
133
+ const sourceIterator = typeof generator.next === "function" ? generator : void 0;
161
134
  let init = typedSSE ? void 0 : generator.next?.();
162
135
  if (set) handleSet(set);
163
136
  if (init instanceof Promise) init = await init;
164
- if (init?.value instanceof ReadableStream) generator = init.value;
137
+ const yieldedStream = init?.value instanceof ReadableStream;
138
+ if (yieldedStream) generator = init.value;
165
139
  else if (init && (typeof init?.done === "undefined" || init?.done)) {
166
140
  if (set) return mapResponse(init.value, set, request, owned);
167
141
  return mapCompactResponse(init.value, request, owned);
@@ -195,8 +169,32 @@ function createStreamHandler({ mapResponse, mapCompactResponse }) {
195
169
  connection: "keep-alive"
196
170
  }
197
171
  };
198
- const iterator = typeof generator.next === "function" ? generator : generator[Symbol.asyncIterator]();
172
+ const sourceStream = generator instanceof ReadableStream ? generator : void 0;
173
+ let streamReader;
174
+ const iterator = sourceStream ? {
175
+ next: () => (streamReader ??= sourceStream.getReader()).read(),
176
+ async return() {
177
+ if (!streamReader) {
178
+ await sourceStream.cancel();
179
+ return {
180
+ done: true,
181
+ value: void 0
182
+ };
183
+ }
184
+ try {
185
+ await streamReader.cancel();
186
+ } finally {
187
+ streamReader.releaseLock();
188
+ }
189
+ return {
190
+ done: true,
191
+ value: void 0
192
+ };
193
+ }
194
+ } : sourceIterator ?? generator[Symbol.asyncIterator]();
195
+ const outerIterator = yieldedStream && !init.done ? sourceIterator : void 0;
199
196
  let end = false;
197
+ let finalizing;
200
198
  const signal = request?.signal;
201
199
  let onAbort;
202
200
  const cleanupAbort = () => {
@@ -205,71 +203,93 @@ function createStreamHandler({ mapResponse, mapCompactResponse }) {
205
203
  onAbort = void 0;
206
204
  }
207
205
  };
208
- const safeReturn = () => {
206
+ const safeReturn = async (target) => {
209
207
  try {
210
- const r = iterator.return?.();
211
- if (r && typeof r.then === "function") r.catch(() => {});
208
+ await target?.return?.();
212
209
  } catch {}
213
210
  };
214
- const closeSafely = (controller) => {
211
+ const finalize = (mode, controller, error) => {
212
+ if (end) return finalizing;
213
+ end = true;
214
+ cleanupAbort();
215
+ finalizing = safeReturn(iterator).then(() => safeReturn(outerIterator));
216
+ if (!controller) return finalizing;
215
217
  try {
216
- controller.close();
218
+ if (mode === "error") controller.error(error);
219
+ else controller.close();
217
220
  } catch {}
218
- cleanupAbort();
219
221
  };
220
- const enqueueValue = async (controller, value) => {
222
+ const enqueueValue = (controller, value) => {
221
223
  if (value.toSSE) {
222
- controller.enqueue(encodeChunk(value.toSSE()));
224
+ controller.enqueue(textEncoder.encode(value.toSSE()));
223
225
  return;
224
226
  }
225
- const p = enqueueBinaryChunk(controller, value);
226
- if (p !== false) return void await p;
227
+ let p;
228
+ if (value instanceof Blob) p = value.arrayBuffer().then((buffer) => {
229
+ controller.enqueue(new Uint8Array(buffer));
230
+ });
231
+ else if (value instanceof Uint8Array) {
232
+ controller.enqueue(value);
233
+ p = true;
234
+ } else if (value instanceof ArrayBuffer) {
235
+ controller.enqueue(new Uint8Array(value));
236
+ p = true;
237
+ } else if (ArrayBuffer.isView(value)) {
238
+ controller.enqueue(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
239
+ p = true;
240
+ } else p = false;
241
+ if (p !== false) return p === true ? void 0 : p;
227
242
  if (typeof value === "object") try {
228
- controller.enqueue(encodeChunk(format(JSON.stringify(value))));
243
+ controller.enqueue(textEncoder.encode(format(JSON.stringify(value))));
229
244
  } catch {
230
- controller.enqueue(encodeChunk(format(value.toString())));
245
+ controller.enqueue(textEncoder.encode(format(value.toString())));
231
246
  }
232
- else controller.enqueue(encodeChunk(format(value.toString())));
247
+ else controller.enqueue(textEncoder.encode(format(value.toString())));
233
248
  };
234
249
  return new Response(new ReadableStream({
235
- async start(controller) {
250
+ start(controller) {
236
251
  if (signal) {
237
- onAbort = () => {
238
- cleanupAbort();
239
- end = true;
240
- safeReturn();
241
- try {
242
- controller.close();
243
- } catch {}
244
- };
252
+ onAbort = () => finalize("close", controller);
245
253
  if (signal.aborted) onAbort();
246
254
  else signal.addEventListener("abort", onAbort, { once: true });
247
255
  }
248
256
  if (!init || init.value instanceof ReadableStream || init.value === void 0 || init.value === null) return;
249
- await enqueueValue(controller, init.value);
250
- },
251
- async pull(controller) {
252
- if (end) {
253
- closeSafely(controller);
254
- return;
255
- }
257
+ const fail = (error) => {
258
+ finalize("error", controller, error);
259
+ };
256
260
  try {
257
- const { value: chunk, done } = await iterator.next();
258
- if (done || end) {
259
- closeSafely(controller);
260
- return;
261
+ return enqueueValue(controller, init.value)?.catch(fail);
262
+ } catch (error) {
263
+ fail(error);
264
+ }
265
+ },
266
+ pull(controller) {
267
+ if (end) return;
268
+ const fail = (error) => {
269
+ finalize("error", controller, error);
270
+ };
271
+ const step = (result) => {
272
+ while (true) {
273
+ if (result.done || end) {
274
+ finalize("close", controller);
275
+ return;
276
+ }
277
+ const chunk = result.value;
278
+ if (chunk !== void 0 && chunk !== null) return enqueueValue(controller, chunk);
279
+ const next = iterator.next();
280
+ if (typeof next.then === "function") return Promise.resolve(next).then(step);
281
+ result = next;
261
282
  }
262
- if (chunk === void 0 || chunk === null) return;
263
- await enqueueValue(controller, chunk);
283
+ };
284
+ try {
285
+ const next = iterator.next();
286
+ return (typeof next.then === "function" ? Promise.resolve(next).then(step) : step(next))?.catch(fail);
264
287
  } catch (error) {
265
- cleanupAbort();
266
- controller.error(error);
288
+ fail(error);
267
289
  }
268
290
  },
269
291
  cancel() {
270
- end = true;
271
- cleanupAbort();
272
- safeReturn();
292
+ finalize("close");
273
293
  }
274
294
  }, typedSSE ? { highWaterMark: 0 } : void 0), set);
275
295
  };
@@ -284,7 +304,7 @@ function handleSet(set) {
284
304
  const proto = Object.getPrototypeOf(set.headers);
285
305
  if (proto !== null && proto !== Object.prototype) {
286
306
  const flat = Object.create(null);
287
- for (const key in set.headers) flat[key] = set.headers[key];
307
+ for (const key of Object.keys(set.headers)) flat[key] = set.headers[key];
288
308
  set.headers = flat;
289
309
  }
290
310
  if (set.cookie && require_utils.isNotEmpty(set.cookie)) {
@@ -292,8 +312,11 @@ function handleSet(set) {
292
312
  const cookie = require_cookie_serialize.serializeCookie(set.cookie);
293
313
  if (cookie) {
294
314
  const existing = set.headers[setCookie];
295
- if (Array.isArray(existing)) set.headers[setCookie] = existing.concat(cookie);
296
- else set.headers[setCookie] = cookie;
315
+ if (existing) {
316
+ const kept = typeof existing === "string" ? [existing] : existing;
317
+ const added = (typeof cookie === "string" ? [cookie] : cookie).filter((value) => !kept.includes(value));
318
+ set.headers[setCookie] = added.length ? kept.concat(added) : existing;
319
+ } else set.headers[setCookie] = cookie;
297
320
  }
298
321
  }
299
322
  if (set.headers[setCookie] && Array.isArray(set.headers[setCookie])) set.headers = parseSetCookies(new Headers(set.headers), set.headers[setCookie]);
@@ -311,7 +334,7 @@ function applySetHeaders(target, setHeaders, present) {
311
334
  for (const key of setHeaders.keys()) if (key !== setCookie && !present.has(key)) target.set(key, setHeaders.get(key) ?? "");
312
335
  } else {
313
336
  let cookies;
314
- for (const key in setHeaders) if (key === setCookie) {
337
+ for (const key of Object.keys(setHeaders)) if (key === setCookie) {
315
338
  const cookie = setHeaders[key];
316
339
  cookies ??= target.getSetCookie();
317
340
  if (!cookies.includes(cookie)) {
@@ -349,7 +372,6 @@ function cancelPropagatingBody(clonedBody, orphanedBranch, preserveOrphan = fals
349
372
  }
350
373
  });
351
374
  }
352
- const warnedStreamBorrows = /* @__PURE__ */ new WeakSet();
353
375
  function createResponseHandler(handler) {
354
376
  const handleStream = createStreamHandler(handler);
355
377
  return (response, set, request, owned = false) => {
@@ -361,25 +383,21 @@ function createResponseHandler(handler) {
361
383
  const borrowed = !owned || explicitlyBorrowed;
362
384
  const mustClone = owned && explicitlyBorrowed;
363
385
  if (!borrowed && response.bodyUsed) throw new TypeError("Cannot reuse a consumed Response across requests");
386
+ let status;
364
387
  if (set) {
365
- const status = mergeStatus(response.status, set.status);
388
+ status = mergeStatus(response.status, set.status);
366
389
  const statusUnchanged = status === void 0 || status === response.status;
367
390
  if (!mustClone && statusUnchanged && !set.cookie && !require_utils.isNotEmpty(set.headers)) return response;
368
391
  } else if (!mustClone) return response;
369
392
  if (!borrowed && response.body?.locked) throw new TypeError("Cannot patch a Response whose body is locked");
370
393
  let body = response.body;
371
394
  if (borrowed) {
372
- const bodyBefore = body;
373
395
  const cloned = response.clone();
374
- if (explicitlyBorrowed && bodyBefore && response.body !== bodyBefore && !warnedStreamBorrows.has(response)) {
375
- warnedStreamBorrows.add(response);
376
- console.warn("[elysia] borrow() was called on a stream-backed Response");
377
- }
378
396
  body = cloned.body && response.body ? cancelPropagatingBody(cloned.body, response.body, explicitlyBorrowed) : cloned.body;
379
397
  }
380
398
  const newResponse = new Response(body, set ? {
381
399
  headers: mergeHeaders(response.headers, set.headers),
382
- status: mergeStatus(response.status, set.status),
400
+ status,
383
401
  statusText: response.statusText
384
402
  } : {
385
403
  headers: response.headers,
@@ -404,6 +422,46 @@ const doneResult = {
404
422
  value: void 0
405
423
  };
406
424
  /**
425
+ * A returned `ReadableStream` is consumed after the handler returns, so
426
+ * afterResponse, `defer()` and derive dispose must wait for it the way a tee'd
427
+ * generator is waited on
428
+ *
429
+ * A returned `Response` is not observed, reading `.body` would move
430
+ * every Response on these routes off Bun's native send path; a `bytes()`
431
+ * stream keeps its exact-stream contract and releases early as before
432
+ */
433
+ function observeStream(source) {
434
+ if (source.locked || require_utils.isByteStream(source)) return;
435
+ const reader = source.getReader();
436
+ const [value, observer] = tee({ [Symbol.asyncIterator]: () => ({
437
+ next: () => reader.read(),
438
+ return: (reason) => reader.cancel(reason).then(() => ({
439
+ done: true,
440
+ value: void 0
441
+ }))
442
+ }) });
443
+ const body = new ReadableStream({
444
+ async pull(controller) {
445
+ try {
446
+ const result = await value.next();
447
+ if (result.done) controller.close();
448
+ else controller.enqueue(result.value);
449
+ } catch (error) {
450
+ controller.error(error);
451
+ }
452
+ },
453
+ cancel(reason) {
454
+ return value.return?.(reason);
455
+ }
456
+ }, { highWaterMark: 0 });
457
+ if (source.sse === true) body.sse = true;
458
+ return [
459
+ body,
460
+ observer,
461
+ value
462
+ ];
463
+ }
464
+ /**
407
465
  * Split async source into `branches` independent iterators
408
466
  *
409
467
  * A producer drains the source ahead of consumers
@@ -524,7 +582,7 @@ function tee(source, branches = 2, cap = 64, capBytes = 1 << 22) {
524
582
  wakeAll();
525
583
  }
526
584
  })();
527
- const makeBranch = (me) => ({
585
+ const makeBranch = (me) => Object.assign(Object.create(null), {
528
586
  [Symbol.asyncIterator]() {
529
587
  return this;
530
588
  },
@@ -564,6 +622,7 @@ exports.handleFile = handleFile;
564
622
  exports.handleSet = handleSet;
565
623
  exports.materializeSetHeaders = materializeSetHeaders;
566
624
  exports.normalizeContentType = normalizeContentType;
625
+ exports.observeStream = observeStream;
567
626
  exports.parseSetCookies = parseSetCookies;
568
627
  exports.responseToSetHeaders = responseToSetHeaders;
569
628
  exports.streamResponse = streamResponse;