elysia 1.4.28 → 2.0.0-beta.0
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.
- package/dist/_virtual/_rolldown/runtime.js +29 -0
- package/dist/_virtual/_rolldown/runtime.mjs +38 -0
- package/dist/adapter/bun/index.d.ts +7 -6
- package/dist/adapter/bun/index.js +219 -380
- package/dist/adapter/bun/index.mjs +217 -382
- package/dist/adapter/constants.d.ts +6 -0
- package/dist/adapter/constants.js +10 -0
- package/dist/adapter/constants.mjs +9 -0
- package/dist/adapter/default-headers.d.ts +4 -0
- package/dist/adapter/default-headers.js +7 -0
- package/dist/adapter/default-headers.mjs +5 -0
- package/dist/adapter/index.d.ts +8 -1
- package/dist/adapter/index.js +7 -14
- package/dist/adapter/index.mjs +5 -0
- package/dist/adapter/origin.d.ts +63 -0
- package/dist/adapter/origin.js +64 -0
- package/dist/adapter/origin.mjs +62 -0
- package/dist/adapter/response-ownership.d.ts +5 -0
- package/dist/adapter/response-ownership.js +13 -0
- package/dist/adapter/response-ownership.mjs +10 -0
- package/dist/adapter/skip-clone.d.ts +4 -0
- package/dist/adapter/skip-clone.js +7 -0
- package/dist/adapter/skip-clone.mjs +5 -0
- package/dist/adapter/types.d.ts +50 -125
- package/dist/adapter/types.js +1 -14
- package/dist/adapter/types.mjs +1 -0
- package/dist/adapter/utils.d.ts +47 -19
- package/dist/adapter/utils.js +561 -311
- package/dist/adapter/utils.mjs +551 -283
- package/dist/adapter/web-standard/handler.d.ts +11 -9
- package/dist/adapter/web-standard/handler.js +146 -415
- package/dist/adapter/web-standard/handler.mjs +144 -400
- package/dist/adapter/web-standard/index.d.ts +5 -2
- package/dist/adapter/web-standard/index.js +68 -161
- package/dist/adapter/web-standard/index.mjs +68 -146
- package/dist/adapter/web-standard/utils.d.ts +4 -0
- package/dist/adapter/web-standard/utils.js +153 -0
- package/dist/adapter/web-standard/utils.mjs +152 -0
- package/dist/base.d.ts +898 -0
- package/dist/base.js +1564 -0
- package/dist/base.mjs +1561 -0
- package/dist/compile/analysis-cache.d.ts +6 -0
- package/dist/compile/analysis-cache.js +12 -0
- package/dist/compile/analysis-cache.mjs +11 -0
- package/dist/compile/aot-capture.d.ts +24 -0
- package/dist/compile/aot-capture.js +376 -0
- package/dist/compile/aot-capture.mjs +365 -0
- package/dist/compile/aot-emit.d.ts +29 -0
- package/dist/compile/aot-emit.js +107 -0
- package/dist/compile/aot-emit.mjs +101 -0
- package/dist/compile/aot-reconstruct.d.ts +30 -0
- package/dist/compile/aot-reconstruct.js +156 -0
- package/dist/compile/aot-reconstruct.mjs +147 -0
- package/dist/compile/aot.d.ts +287 -0
- package/dist/compile/aot.js +220 -0
- package/dist/compile/aot.mjs +200 -0
- package/dist/compile/handler/descriptor.d.ts +77 -0
- package/dist/compile/handler/descriptor.js +199 -0
- package/dist/compile/handler/descriptor.mjs +194 -0
- package/dist/compile/handler/frozen-validator.d.ts +39 -0
- package/dist/compile/handler/frozen-validator.js +319 -0
- package/dist/compile/handler/frozen-validator.mjs +313 -0
- package/dist/compile/handler/index.d.ts +19 -0
- package/dist/compile/handler/index.js +379 -0
- package/dist/compile/handler/index.mjs +371 -0
- package/dist/compile/handler/jit.d.ts +43 -0
- package/dist/compile/handler/jit.js +536 -0
- package/dist/compile/handler/jit.mjs +532 -0
- package/dist/compile/handler/params.d.ts +28 -0
- package/dist/compile/handler/params.js +78 -0
- package/dist/compile/handler/params.mjs +76 -0
- package/dist/compile/handler/reconstruct.d.ts +12 -0
- package/dist/compile/handler/reconstruct.js +50 -0
- package/dist/compile/handler/reconstruct.mjs +49 -0
- package/dist/compile/handler/utils.d.ts +45 -0
- package/dist/compile/handler/utils.js +514 -0
- package/dist/compile/handler/utils.mjs +498 -0
- package/dist/compile/jit-probe.d.ts +15 -0
- package/dist/compile/jit-probe.js +28 -0
- package/dist/compile/jit-probe.mjs +26 -0
- package/dist/compile/lexer.d.ts +7 -0
- package/dist/compile/lexer.js +53 -0
- package/dist/compile/lexer.mjs +48 -0
- package/dist/compile/utils.d.ts +8 -0
- package/dist/compile/utils.js +23 -0
- package/dist/compile/utils.mjs +19 -0
- package/dist/compiled.d.ts +17 -0
- package/dist/compiled.js +20 -0
- package/dist/compiled.mjs +16 -0
- package/dist/constants.d.ts +137 -0
- package/dist/constants.js +96 -0
- package/dist/constants.mjs +88 -0
- package/dist/context.d.ts +86 -129
- package/dist/context.js +110 -13
- package/dist/context.mjs +108 -0
- package/dist/cookie/config.d.ts +31 -0
- package/dist/cookie/config.js +98 -0
- package/dist/cookie/config.mjs +95 -0
- package/dist/cookie/cookie.d.ts +26 -0
- package/dist/cookie/cookie.js +105 -0
- package/dist/cookie/cookie.mjs +104 -0
- package/dist/cookie/crypto.d.ts +20 -0
- package/dist/cookie/crypto.js +168 -0
- package/dist/cookie/crypto.mjs +151 -0
- package/dist/cookie/error.d.ts +17 -0
- package/dist/cookie/error.js +25 -0
- package/dist/cookie/error.mjs +24 -0
- package/dist/cookie/index.d.ts +7 -0
- package/dist/cookie/index.js +13 -0
- package/dist/cookie/index.mjs +7 -0
- package/dist/cookie/lib.d.ts +7 -0
- package/dist/cookie/lib.js +76 -0
- package/dist/cookie/lib.mjs +74 -0
- package/dist/cookie/serialize.d.ts +6 -0
- package/dist/cookie/serialize.js +32 -0
- package/dist/cookie/serialize.mjs +31 -0
- package/dist/cookie/types.d.ts +113 -0
- package/dist/cookie/types.js +1 -0
- package/dist/cookie/types.mjs +1 -0
- package/dist/cookie/utils.d.ts +25 -0
- package/dist/cookie/utils.js +182 -0
- package/dist/cookie/utils.mjs +171 -0
- package/dist/error.d.ts +226 -188
- package/dist/error.js +519 -357
- package/dist/error.mjs +503 -327
- package/dist/generation.d.ts +30 -0
- package/dist/generation.js +23 -0
- package/dist/generation.mjs +17 -0
- package/dist/handler/error.d.ts +7 -0
- package/dist/handler/error.js +105 -0
- package/dist/handler/error.mjs +104 -0
- package/dist/handler/fetch.d.ts +8 -0
- package/dist/handler/fetch.js +371 -0
- package/dist/handler/fetch.mjs +367 -0
- package/dist/handler/index.d.ts +2 -0
- package/dist/handler/index.js +5 -0
- package/dist/handler/index.mjs +3 -0
- package/dist/handler/utils.d.ts +15 -0
- package/dist/handler/utils.js +48 -0
- package/dist/handler/utils.mjs +42 -0
- package/dist/index.d.ts +23 -2198
- package/dist/index.js +62 -1948
- package/dist/index.mjs +20 -1986
- package/dist/memory.d.ts +4 -0
- package/dist/memory.js +17 -0
- package/dist/memory.mjs +16 -0
- package/dist/package.js +11 -0
- package/dist/package.mjs +5 -0
- package/dist/parse-query.d.ts +7 -9
- package/dist/parse-query.js +154 -164
- package/dist/parse-query.mjs +150 -133
- package/dist/plugin/aot/bun.d.ts +26 -0
- package/dist/plugin/aot/bun.js +35 -0
- package/dist/plugin/aot/bun.mjs +34 -0
- package/dist/plugin/aot/core.d.ts +291 -0
- package/dist/plugin/aot/core.js +505 -0
- package/dist/plugin/aot/core.mjs +481 -0
- package/dist/plugin/aot/esbuild.d.ts +28 -0
- package/dist/plugin/aot/esbuild.js +48 -0
- package/dist/plugin/aot/esbuild.mjs +46 -0
- package/dist/plugin/aot/hooks.d.ts +28 -0
- package/dist/plugin/aot/hooks.js +114 -0
- package/dist/plugin/aot/hooks.mjs +112 -0
- package/dist/plugin/aot/rspack-loader.d.ts +23 -0
- package/dist/plugin/aot/rspack-loader.js +44 -0
- package/dist/plugin/aot/rspack-loader.mjs +40 -0
- package/dist/plugin/aot/rspack.d.ts +23 -0
- package/dist/plugin/aot/rspack.js +85 -0
- package/dist/plugin/aot/rspack.mjs +82 -0
- package/dist/plugin/aot/source.d.ts +60 -0
- package/dist/plugin/aot/source.js +422 -0
- package/dist/plugin/aot/source.mjs +418 -0
- package/dist/plugin/aot/treeshake.d.ts +4 -0
- package/dist/plugin/aot/treeshake.js +95 -0
- package/dist/plugin/aot/treeshake.mjs +94 -0
- package/dist/plugin/aot/unplugin.d.ts +50 -0
- package/dist/plugin/aot/unplugin.js +64 -0
- package/dist/plugin/aot/unplugin.mjs +62 -0
- package/dist/plugin/aot/vite.d.ts +30 -0
- package/dist/plugin/aot/vite.js +34 -0
- package/dist/plugin/aot/vite.mjs +33 -0
- package/dist/plugin/aot/worker.d.ts +1 -0
- package/dist/plugin/aot/worker.js +28 -0
- package/dist/plugin/aot/worker.mjs +27 -0
- package/dist/plugin/auto-head.d.ts +6 -0
- package/dist/plugin/auto-head.js +38 -0
- package/dist/plugin/auto-head.mjs +36 -0
- package/dist/plugin/trace.d.ts +10 -0
- package/dist/plugin/trace.js +39 -0
- package/dist/plugin/trace.mjs +36 -0
- package/dist/plugin/websocket.d.ts +6 -0
- package/dist/plugin/websocket.js +61 -0
- package/dist/plugin/websocket.mjs +60 -0
- package/dist/route-table.d.ts +28 -0
- package/dist/route-table.js +64 -0
- package/dist/route-table.mjs +61 -0
- package/dist/schema-snapshot.d.ts +9 -0
- package/dist/schema-snapshot.js +271 -0
- package/dist/schema-snapshot.mjs +269 -0
- package/dist/sucrose.d.ts +28 -63
- package/dist/sucrose.js +814 -306
- package/dist/sucrose.mjs +805 -274
- package/dist/trace.d.ts +145 -130
- package/dist/trace.js +387 -135
- package/dist/trace.mjs +385 -113
- package/dist/type/bridge-live.d.ts +15 -0
- package/dist/type/bridge-live.js +39 -0
- package/dist/type/bridge-live.mjs +17 -0
- package/dist/type/bridge.d.ts +56 -0
- package/dist/type/bridge.js +180 -0
- package/dist/type/bridge.mjs +63 -0
- package/dist/type/coerce-plan.d.ts +38 -0
- package/dist/type/coerce-plan.js +107 -0
- package/dist/type/coerce-plan.mjs +98 -0
- package/dist/type/coerce.d.ts +43 -0
- package/dist/type/coerce.js +351 -0
- package/dist/type/coerce.mjs +339 -0
- package/dist/type/compat.d.ts +8 -0
- package/dist/type/compat.js +46 -0
- package/dist/type/compat.mjs +44 -0
- package/dist/type/constants.d.ts +25 -0
- package/dist/type/constants.js +36 -0
- package/dist/type/constants.mjs +32 -0
- package/dist/type/elysia/accelerate.d.ts +6 -0
- package/dist/type/elysia/accelerate.js +12 -0
- package/dist/type/elysia/accelerate.mjs +10 -0
- package/dist/type/elysia/array-buffer.d.ts +6 -0
- package/dist/type/elysia/array-buffer.js +27 -0
- package/dist/type/elysia/array-buffer.mjs +25 -0
- package/dist/type/elysia/array-string.d.ts +7 -0
- package/dist/type/elysia/array-string.js +28 -0
- package/dist/type/elysia/array-string.mjs +26 -0
- package/dist/type/elysia/array.d.ts +6 -0
- package/dist/type/elysia/array.js +32 -0
- package/dist/type/elysia/array.mjs +31 -0
- package/dist/type/elysia/boolean-string.d.ts +6 -0
- package/dist/type/elysia/boolean-string.js +24 -0
- package/dist/type/elysia/boolean-string.mjs +22 -0
- package/dist/type/elysia/boolean.d.ts +6 -0
- package/dist/type/elysia/boolean.js +20 -0
- package/dist/type/elysia/boolean.mjs +18 -0
- package/dist/type/elysia/cookie.d.ts +15 -0
- package/dist/type/elysia/cookie.js +51 -0
- package/dist/type/elysia/cookie.mjs +50 -0
- package/dist/type/elysia/date.d.ts +7 -0
- package/dist/type/elysia/date.js +98 -0
- package/dist/type/elysia/date.mjs +96 -0
- package/dist/type/elysia/file-type.d.ts +25 -0
- package/dist/type/elysia/file-type.js +81 -0
- package/dist/type/elysia/file-type.mjs +71 -0
- package/dist/type/elysia/file.d.ts +9 -0
- package/dist/type/elysia/file.js +64 -0
- package/dist/type/elysia/file.mjs +48 -0
- package/dist/type/elysia/files.d.ts +8 -0
- package/dist/type/elysia/files.js +45 -0
- package/dist/type/elysia/files.mjs +43 -0
- package/dist/type/elysia/form.d.ts +8 -0
- package/dist/type/elysia/form.js +17 -0
- package/dist/type/elysia/form.mjs +15 -0
- package/dist/type/elysia/integer-string.d.ts +7 -0
- package/dist/type/elysia/integer-string.js +40 -0
- package/dist/type/elysia/integer-string.mjs +38 -0
- package/dist/type/elysia/integer.d.ts +6 -0
- package/dist/type/elysia/integer.js +20 -0
- package/dist/type/elysia/integer.mjs +18 -0
- package/dist/type/elysia/intersect.d.ts +6 -0
- package/dist/type/elysia/intersect.js +20 -0
- package/dist/type/elysia/intersect.mjs +19 -0
- package/dist/type/elysia/maybe-empty.d.ts +7 -0
- package/dist/type/elysia/maybe-empty.js +21 -0
- package/dist/type/elysia/maybe-empty.mjs +19 -0
- package/dist/type/elysia/no-validate.d.ts +6 -0
- package/dist/type/elysia/no-validate.js +9 -0
- package/dist/type/elysia/no-validate.mjs +8 -0
- package/dist/type/elysia/nullable.d.ts +7 -0
- package/dist/type/elysia/nullable.js +15 -0
- package/dist/type/elysia/nullable.mjs +13 -0
- package/dist/type/elysia/number.d.ts +6 -0
- package/dist/type/elysia/number.js +20 -0
- package/dist/type/elysia/number.mjs +18 -0
- package/dist/type/elysia/numeric-enum.d.ts +11 -0
- package/dist/type/elysia/numeric-enum.js +26 -0
- package/dist/type/elysia/numeric-enum.mjs +24 -0
- package/dist/type/elysia/numeric.d.ts +7 -0
- package/dist/type/elysia/numeric.js +62 -0
- package/dist/type/elysia/numeric.mjs +60 -0
- package/dist/type/elysia/object-string.d.ts +7 -0
- package/dist/type/elysia/object-string.js +28 -0
- package/dist/type/elysia/object-string.mjs +26 -0
- package/dist/type/elysia/object.d.ts +6 -0
- package/dist/type/elysia/object.js +47 -0
- package/dist/type/elysia/object.mjs +46 -0
- package/dist/type/elysia/optional.d.ts +6 -0
- package/dist/type/elysia/optional.js +22 -0
- package/dist/type/elysia/optional.mjs +21 -0
- package/dist/type/elysia/string.d.ts +6 -0
- package/dist/type/elysia/string.js +43 -0
- package/dist/type/elysia/string.mjs +41 -0
- package/dist/type/elysia/uint8-array.d.ts +6 -0
- package/dist/type/elysia/uint8-array.js +27 -0
- package/dist/type/elysia/uint8-array.mjs +25 -0
- package/dist/type/elysia/union-enum.d.ts +8 -0
- package/dist/type/elysia/union-enum.js +31 -0
- package/dist/type/elysia/union-enum.mjs +29 -0
- package/dist/type/elysia/union.d.ts +6 -0
- package/dist/type/elysia/union.js +20 -0
- package/dist/type/elysia/union.mjs +19 -0
- package/dist/type/elysia/utils.d.ts +53 -0
- package/dist/type/elysia/utils.js +98 -0
- package/dist/type/elysia/utils.mjs +88 -0
- package/dist/type/exports.d.ts +36 -0
- package/dist/type/exports.js +68 -0
- package/dist/type/exports.mjs +36 -0
- package/dist/type/index.d.ts +13 -0
- package/dist/type/index.js +81 -0
- package/dist/type/index.mjs +68 -0
- package/dist/type/shared.d.ts +27 -0
- package/dist/type/shared.js +171 -0
- package/dist/type/shared.mjs +161 -0
- package/dist/type/types.d.ts +206 -0
- package/dist/type/types.js +1 -0
- package/dist/type/types.mjs +1 -0
- package/dist/type/utils.d.ts +7 -0
- package/dist/type/utils.js +43 -0
- package/dist/type/utils.mjs +41 -0
- package/dist/type/validator/clean-safe.d.ts +8 -0
- package/dist/type/validator/clean-safe.js +82 -0
- package/dist/type/validator/clean-safe.mjs +77 -0
- package/dist/type/validator/custom-error.d.ts +10 -0
- package/dist/type/validator/custom-error.js +191 -0
- package/dist/type/validator/custom-error.mjs +188 -0
- package/dist/type/validator/default-precompute.d.ts +26 -0
- package/dist/type/validator/default-precompute.js +432 -0
- package/dist/type/validator/default-precompute.mjs +421 -0
- package/dist/type/validator/exact-mirror.d.ts +7 -0
- package/dist/type/validator/exact-mirror.js +19 -0
- package/dist/type/validator/exact-mirror.mjs +15 -0
- package/dist/type/validator/frozen-check.d.ts +11 -0
- package/dist/type/validator/frozen-check.js +20 -0
- package/dist/type/validator/frozen-check.mjs +19 -0
- package/dist/type/validator/index.d.ts +32 -0
- package/dist/type/validator/index.js +657 -0
- package/dist/type/validator/index.mjs +651 -0
- package/dist/type/validator/string-codec-aot.d.ts +8 -0
- package/dist/type/validator/string-codec-aot.js +68 -0
- package/dist/type/validator/string-codec-aot.mjs +66 -0
- package/dist/type/validator/validator-cache.d.ts +26 -0
- package/dist/type/validator/validator-cache.js +247 -0
- package/dist/type/validator/validator-cache.mjs +242 -0
- package/dist/types.d.ts +938 -1235
- package/dist/types.js +1 -14
- package/dist/types.mjs +1 -0
- package/dist/universal/constants.d.ts +6 -0
- package/dist/universal/constants.js +19 -0
- package/dist/universal/constants.mjs +15 -0
- package/dist/universal/env.d.ts +5 -1
- package/dist/universal/env.js +12 -26
- package/dist/universal/env.mjs +10 -5
- package/dist/universal/file.d.ts +55 -86
- package/dist/universal/file.js +116 -157
- package/dist/universal/file.mjs +111 -132
- package/dist/universal/index.d.ts +5 -3
- package/dist/universal/index.js +8 -27
- package/dist/universal/index.mjs +4 -5
- package/dist/universal/is-production.d.ts +5 -0
- package/dist/universal/is-production.js +10 -0
- package/dist/universal/is-production.mjs +8 -0
- package/dist/universal/server.d.ts +318 -313
- package/dist/universal/server.js +1 -14
- package/dist/universal/server.mjs +1 -0
- package/dist/utils.d.ts +162 -191
- package/dist/utils.js +732 -693
- package/dist/utils.mjs +683 -631
- package/dist/validator/index.d.ts +63 -0
- package/dist/validator/index.js +299 -0
- package/dist/validator/index.mjs +296 -0
- package/dist/validator/route.d.ts +32 -0
- package/dist/validator/route.js +51 -0
- package/dist/validator/route.mjs +50 -0
- package/dist/ws/context.d.ts +49 -0
- package/dist/ws/context.js +152 -0
- package/dist/ws/context.mjs +150 -0
- package/dist/ws/index.d.ts +5 -77
- package/dist/ws/index.js +11 -180
- package/dist/ws/index.mjs +5 -157
- package/dist/ws/parser.d.ts +5 -0
- package/dist/ws/parser.js +69 -0
- package/dist/ws/parser.mjs +66 -0
- package/dist/ws/route.d.ts +16 -0
- package/dist/ws/route.js +560 -0
- package/dist/ws/route.mjs +554 -0
- package/dist/ws/types.d.ts +154 -57
- package/dist/ws/types.js +1 -14
- package/dist/ws/types.mjs +1 -0
- package/package.json +179 -120
- package/bunfig.toml +0 -2
- package/dist/adapter/bun/compose.d.ts +0 -3
- package/dist/adapter/bun/compose.js +0 -90
- package/dist/adapter/bun/compose.mjs +0 -74
- package/dist/adapter/bun/handler-native.d.ts +0 -3
- package/dist/adapter/bun/handler-native.js +0 -42
- package/dist/adapter/bun/handler-native.mjs +0 -22
- package/dist/adapter/bun/handler.d.ts +0 -7
- package/dist/adapter/bun/handler.js +0 -363
- package/dist/adapter/bun/handler.mjs +0 -346
- package/dist/adapter/cloudflare-worker/index.d.ts +0 -20
- package/dist/adapter/cloudflare-worker/index.js +0 -59
- package/dist/adapter/cloudflare-worker/index.mjs +0 -39
- package/dist/compose.d.ts +0 -30
- package/dist/compose.js +0 -1324
- package/dist/compose.mjs +0 -1336
- package/dist/cookies.d.ts +0 -158
- package/dist/cookies.js +0 -250
- package/dist/cookies.mjs +0 -221
- package/dist/dynamic-handle.d.ts +0 -15
- package/dist/dynamic-handle.js +0 -521
- package/dist/dynamic-handle.mjs +0 -509
- package/dist/formats.d.ts +0 -79
- package/dist/formats.js +0 -153
- package/dist/formats.mjs +0 -132
- package/dist/manifest.d.ts +0 -0
- package/dist/manifest.js +0 -1
- package/dist/manifest.mjs +0 -0
- package/dist/replace-schema.d.ts +0 -60
- package/dist/replace-schema.js +0 -140
- package/dist/replace-schema.mjs +0 -115
- package/dist/schema.d.ts +0 -107
- package/dist/schema.js +0 -833
- package/dist/schema.mjs +0 -814
- package/dist/type-system/format.d.ts +0 -55
- package/dist/type-system/format.js +0 -197
- package/dist/type-system/format.mjs +0 -175
- package/dist/type-system/index.d.ts +0 -45
- package/dist/type-system/index.js +0 -406
- package/dist/type-system/index.mjs +0 -394
- package/dist/type-system/types.d.ts +0 -155
- package/dist/type-system/types.js +0 -14
- package/dist/type-system/types.mjs +0 -0
- package/dist/type-system/utils.d.ts +0 -62
- package/dist/type-system/utils.js +0 -141
- package/dist/type-system/utils.mjs +0 -111
- package/dist/universal/request.d.ts +0 -29
- package/dist/universal/request.js +0 -126
- package/dist/universal/request.mjs +0 -105
- package/dist/universal/types.d.ts +0 -124
- package/dist/universal/types.js +0 -41
- package/dist/universal/types.mjs +0 -19
- package/dist/universal/utils.d.ts +0 -3
- package/dist/universal/utils.js +0 -41
- package/dist/universal/utils.mjs +0 -18
- package/dist/ws/bun.d.ts +0 -419
- package/dist/ws/bun.js +0 -14
- package/dist/ws/bun.mjs +0 -0
- package/knip.json +0 -22
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
import { isBun } from "../universal/constants.mjs";
|
|
2
|
+
import { deriveEntryFn, isNotEmpty, nullObject } from "../utils.mjs";
|
|
3
|
+
import { isBridgeLive } from "../type/bridge.mjs";
|
|
4
|
+
import { isProduction } from "../universal/is-production.mjs";
|
|
5
|
+
import { ElysiaError, ElysiaStatus, ValidationError, internalServerErrorBodyString, problemBody, problemResponse } from "../error.mjs";
|
|
6
|
+
import { mapResponse } from "../adapter/web-standard/handler.mjs";
|
|
7
|
+
import { parseQueryFromURL } from "../parse-query.mjs";
|
|
8
|
+
import { frozenRootOf } from "../generation.mjs";
|
|
9
|
+
import { deriveModes, getQueryParseChannels, replaceDeriveContext } from "../compile/handler/utils.mjs";
|
|
10
|
+
import { StandardValidator } from "../validator/index.mjs";
|
|
11
|
+
import { RouteValidator } from "../validator/route.mjs";
|
|
12
|
+
import { buildFrozenRouteValidator, isBridgeNotInitialized } from "../compile/handler/frozen-validator.mjs";
|
|
13
|
+
import { composeRouteHook, localMacroRoot, resolveWSLocalHook } from "../compile/handler/index.mjs";
|
|
14
|
+
import { createErrorHandler } from "../handler/error.mjs";
|
|
15
|
+
import { ElysiaWS, isGeneratorObject } from "./context.mjs";
|
|
16
|
+
import { createMessageParser } from "./parser.mjs";
|
|
17
|
+
|
|
18
|
+
//#region src/ws/route.ts
|
|
19
|
+
const EMPTY_HOOKS = Object.freeze([]);
|
|
20
|
+
/**
|
|
21
|
+
* Build-time (route-registration-time) analysis of a WS `message` handler to
|
|
22
|
+
* decide whether the per-frame `ws.body` view must be assigned.
|
|
23
|
+
*/
|
|
24
|
+
function handlerMayTouchBody(fn) {
|
|
25
|
+
if (!fn) return false;
|
|
26
|
+
let source;
|
|
27
|
+
try {
|
|
28
|
+
source = Function.prototype.toString.call(fn);
|
|
29
|
+
} catch {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
if (source.indexOf("[native code]") !== -1) return true;
|
|
33
|
+
if (/\bbody\b/.test(source)) return true;
|
|
34
|
+
if (/\barguments\b/.test(source)) return true;
|
|
35
|
+
if (source.indexOf("[") !== -1) return true;
|
|
36
|
+
if (source.indexOf("...") !== -1) return true;
|
|
37
|
+
const parsed = firstParamIdentifier(source);
|
|
38
|
+
if (parsed === void 0) return true;
|
|
39
|
+
const { name: wsName, bodyStart, paramsEnd } = parsed;
|
|
40
|
+
if (source.slice(0, paramsEnd).indexOf("(", 1) !== -1) return true;
|
|
41
|
+
const body = source.slice(bodyStart);
|
|
42
|
+
const safeName = wsName.replace(/[$]/g, "\\$&");
|
|
43
|
+
if (new RegExp(`(?<![\\w$.])${safeName}(?![\\w$]|\\s*\\.)`).test(body)) return true;
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
function firstParamIdentifier(source) {
|
|
47
|
+
const open = source.indexOf("(");
|
|
48
|
+
if (open === -1) {
|
|
49
|
+
const m = /^\s*(?:async\s+)?([A-Za-z_$][\w$]*)\s*=>/.exec(source);
|
|
50
|
+
if (!m) return void 0;
|
|
51
|
+
const end = m.index + m[0].length;
|
|
52
|
+
return {
|
|
53
|
+
name: m[1],
|
|
54
|
+
bodyStart: end,
|
|
55
|
+
paramsEnd: end
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
let depth = 0;
|
|
59
|
+
let close = -1;
|
|
60
|
+
for (let i = open; i < source.length; i++) {
|
|
61
|
+
const c = source[i];
|
|
62
|
+
if (c === "(") depth++;
|
|
63
|
+
else if (c === ")") {
|
|
64
|
+
depth--;
|
|
65
|
+
if (depth === 0) {
|
|
66
|
+
close = i;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (close === -1) return void 0;
|
|
72
|
+
const params = source.slice(open + 1, close).trim();
|
|
73
|
+
if (params.length === 0) return void 0;
|
|
74
|
+
let first = params;
|
|
75
|
+
const comma = params.indexOf(",");
|
|
76
|
+
if (comma !== -1) first = params.slice(0, comma).trim();
|
|
77
|
+
if (first[0] === "{" || first[0] === "[") return void 0;
|
|
78
|
+
const m = /^([A-Za-z_$][\w$]*)/.exec(first);
|
|
79
|
+
if (!m) return void 0;
|
|
80
|
+
return {
|
|
81
|
+
name: m[1],
|
|
82
|
+
bodyStart: close + 1,
|
|
83
|
+
paramsEnd: close + 1
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function concatHooks(...sources) {
|
|
87
|
+
let result;
|
|
88
|
+
for (let i = 0; i < sources.length; i++) {
|
|
89
|
+
const s = sources[i];
|
|
90
|
+
if (s == null) continue;
|
|
91
|
+
const arr = Array.isArray(s) ? s : [s];
|
|
92
|
+
if (arr.length === 0) continue;
|
|
93
|
+
result = result ? result.concat(arr) : arr;
|
|
94
|
+
}
|
|
95
|
+
return result ?? EMPTY_HOOKS;
|
|
96
|
+
}
|
|
97
|
+
async function applyMapResponse(ws, value, mapResponses) {
|
|
98
|
+
for (let i = 0; i < mapResponses.length; i++) {
|
|
99
|
+
ws.responseValue = value;
|
|
100
|
+
const r = mapResponses[i](ws);
|
|
101
|
+
const result = r instanceof Promise ? await r : r;
|
|
102
|
+
if (result !== void 0) value = result;
|
|
103
|
+
}
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
const isBackpressured = (status) => typeof status === "number" && status <= 0;
|
|
107
|
+
const isEmptyPayload = (payload) => {
|
|
108
|
+
if (payload === "" || payload == null) return true;
|
|
109
|
+
if (typeof payload === "string") return false;
|
|
110
|
+
if (payload instanceof ArrayBuffer || ArrayBuffer.isView(payload)) return payload.byteLength === 0;
|
|
111
|
+
return false;
|
|
112
|
+
};
|
|
113
|
+
function waitForDrain(ws) {
|
|
114
|
+
const data = ws.raw.data;
|
|
115
|
+
return new Promise((resolve) => {
|
|
116
|
+
(data.resumeWaiters ??= /* @__PURE__ */ new Set()).add(resolve);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function drainWaiters(ws) {
|
|
120
|
+
const waiters = ws.raw.data.resumeWaiters;
|
|
121
|
+
if (!waiters || waiters.size === 0) return;
|
|
122
|
+
ws.raw.data.resumeWaiters = void 0;
|
|
123
|
+
for (const fn of waiters) fn();
|
|
124
|
+
}
|
|
125
|
+
async function handleWSResponse(ws, value, mapResponses) {
|
|
126
|
+
if (value === void 0) return;
|
|
127
|
+
if (isGeneratorObject(value)) {
|
|
128
|
+
const iter = value;
|
|
129
|
+
try {
|
|
130
|
+
while (true) {
|
|
131
|
+
const step = iter.next();
|
|
132
|
+
const { value: yielded, done } = step instanceof Promise ? await step : step;
|
|
133
|
+
if (done) return;
|
|
134
|
+
if (yielded === void 0) continue;
|
|
135
|
+
const mapped = mapResponses.length ? await applyMapResponse(ws, yielded, mapResponses) : yielded;
|
|
136
|
+
let status = ws.send(mapped);
|
|
137
|
+
const canBackpressure = !isEmptyPayload(mapped);
|
|
138
|
+
while (canBackpressure && isBackpressured(status)) {
|
|
139
|
+
if (ws.readyState >= 2) return;
|
|
140
|
+
await waitForDrain(ws);
|
|
141
|
+
if (ws.readyState >= 2) return;
|
|
142
|
+
if (status === -1) break;
|
|
143
|
+
status = ws.send(mapped);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
} finally {
|
|
147
|
+
if (typeof iter.return === "function") try {
|
|
148
|
+
const r = iter.return();
|
|
149
|
+
if (r instanceof Promise) await r;
|
|
150
|
+
} catch {}
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const mapped = mapResponses.length ? await applyMapResponse(ws, value, mapResponses) : value;
|
|
155
|
+
ws.send(mapped);
|
|
156
|
+
}
|
|
157
|
+
function wsErrorFrame(error) {
|
|
158
|
+
if (error instanceof ValidationError) try {
|
|
159
|
+
return JSON.stringify(error.payload);
|
|
160
|
+
} catch {}
|
|
161
|
+
if (error instanceof ElysiaStatus) try {
|
|
162
|
+
return JSON.stringify({
|
|
163
|
+
status: error.status,
|
|
164
|
+
error: error.response
|
|
165
|
+
});
|
|
166
|
+
} catch {}
|
|
167
|
+
if (typeof error?.toResponse === "function") {
|
|
168
|
+
if (error instanceof ElysiaError && error.toResponse === ElysiaError.prototype.toResponse) try {
|
|
169
|
+
return JSON.stringify(problemBody({
|
|
170
|
+
type: error.problemType,
|
|
171
|
+
title: error.problemTitle,
|
|
172
|
+
status: error.status ?? 500,
|
|
173
|
+
detail: error.response !== void 0 && error.response !== error.problemTitle ? error.response : void 0
|
|
174
|
+
}));
|
|
175
|
+
} catch {}
|
|
176
|
+
try {
|
|
177
|
+
const r = error.toResponse();
|
|
178
|
+
return Promise.resolve(r).then((resolved) => resolved instanceof Response ? resolved.text() : wsErrorFrameFallback(error));
|
|
179
|
+
} catch {}
|
|
180
|
+
}
|
|
181
|
+
return wsErrorFrameFallback(error);
|
|
182
|
+
}
|
|
183
|
+
function wsErrorFrameFallback(error) {
|
|
184
|
+
if (error?.status) {
|
|
185
|
+
const body = error.response !== void 0 ? error.response : isProduction() ? error.status >= 500 ? "Internal Server Error" : "" : error.message ?? "";
|
|
186
|
+
return typeof body === "object" ? JSON.stringify(body) : String(body);
|
|
187
|
+
}
|
|
188
|
+
return internalServerErrorBodyString(error);
|
|
189
|
+
}
|
|
190
|
+
function sendErrorFrame(ws, error) {
|
|
191
|
+
const frame = wsErrorFrame(error);
|
|
192
|
+
if (typeof frame === "string") {
|
|
193
|
+
try {
|
|
194
|
+
ws.raw.send(frame);
|
|
195
|
+
} catch {}
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
return frame.then((f) => {
|
|
199
|
+
try {
|
|
200
|
+
ws.raw.send(f);
|
|
201
|
+
} catch {}
|
|
202
|
+
}, () => {});
|
|
203
|
+
}
|
|
204
|
+
function validateUpgradeChannel(validator, value, type) {
|
|
205
|
+
if (validator instanceof StandardValidator) return validator.From(value, type);
|
|
206
|
+
if (validator.hasCodec) return validator.From(value, type);
|
|
207
|
+
return validator.EncodeFrom(value, type);
|
|
208
|
+
}
|
|
209
|
+
const wsOptions = [
|
|
210
|
+
"maxPayloadLength",
|
|
211
|
+
"backpressureLimit",
|
|
212
|
+
"closeOnBackpressureLimit",
|
|
213
|
+
"idleTimeout",
|
|
214
|
+
"publishToSelf",
|
|
215
|
+
"sendPings",
|
|
216
|
+
"perMessageDeflate"
|
|
217
|
+
];
|
|
218
|
+
function buildWSRoute(route, app) {
|
|
219
|
+
const hook = resolveWSLocalHook(localMacroRoot(route[7] ?? route[3] ?? app, app), route[4], app) ?? nullObject();
|
|
220
|
+
let validators;
|
|
221
|
+
const frozenEager = isBridgeLive() ? void 0 : buildFrozenRouteValidator(hook, app, "WS", route[1]);
|
|
222
|
+
if (frozenEager) validators = frozenEager;
|
|
223
|
+
else try {
|
|
224
|
+
validators = new RouteValidator(hook, {
|
|
225
|
+
models: frozenRootOf(app)["~ext"]?.models,
|
|
226
|
+
app,
|
|
227
|
+
aot: {
|
|
228
|
+
method: "WS",
|
|
229
|
+
path: route[1]
|
|
230
|
+
},
|
|
231
|
+
eager: frozenRootOf(app)["~config"]?.precompile
|
|
232
|
+
});
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (!isBridgeNotInitialized(error)) throw error;
|
|
235
|
+
const frozen = buildFrozenRouteValidator(hook, app, "WS", route[1]);
|
|
236
|
+
if (!frozen) throw error;
|
|
237
|
+
validators = frozen;
|
|
238
|
+
}
|
|
239
|
+
const responseValidator = validators.response;
|
|
240
|
+
const defaultResponseValidator = responseValidator ? responseValidator[200] ?? responseValidator[Object.keys(responseValidator)[0]] : void 0;
|
|
241
|
+
const queryChannels = getQueryParseChannels(validators.query?.schema);
|
|
242
|
+
const queryArray = queryChannels?.array;
|
|
243
|
+
const queryObject = queryChannels?.object;
|
|
244
|
+
const instance = route[3] ?? app;
|
|
245
|
+
const appHookChain = route[5];
|
|
246
|
+
const inheritedChain = route[6];
|
|
247
|
+
const flatAppHook = composeRouteHook(instance, void 0, appHookChain, inheritedChain, app, route[7]) ?? {};
|
|
248
|
+
const parseHooks = hook.parse == null ? [] : Array.isArray(hook.parse) ? hook.parse : [hook.parse];
|
|
249
|
+
const transforms = concatHooks(flatAppHook.transform, hook.transform);
|
|
250
|
+
const allBeforeHandles = concatHooks(flatAppHook.beforeHandle, hook.beforeHandle);
|
|
251
|
+
const deriveEntries = [...flatAppHook["~deriveEntries"] ?? [], ...hook["~deriveEntries"] ?? []];
|
|
252
|
+
const deriveSet = deriveEntries.length ? new Set(deriveEntries.map(deriveEntryFn)) : void 0;
|
|
253
|
+
const upgradeDeriveModes = deriveModes(allBeforeHandles, deriveEntries);
|
|
254
|
+
const messageBeforeHandles = allBeforeHandles.filter((fn) => !deriveSet?.has(fn));
|
|
255
|
+
const afterHandles = concatHooks(flatAppHook.afterHandle, hook.afterHandle);
|
|
256
|
+
const mapResponses = concatHooks(flatAppHook.mapResponse, hook.mapResponse);
|
|
257
|
+
const afterResponses = concatHooks(flatAppHook.afterResponse, hook.afterResponse);
|
|
258
|
+
const errorHandlers = concatHooks(hook.error, flatAppHook.error);
|
|
259
|
+
const parseMessage = createMessageParser(parseHooks);
|
|
260
|
+
const handleUpgradeError = createErrorHandler(errorHandlers.length ? errorHandlers : void 0, ((response, set, context) => mapResponse(response, set, context?.request)), void 0, frozenRootOf(app)["~config"]?.allowUnsafeValidationDetails);
|
|
261
|
+
async function handleError(ws, error) {
|
|
262
|
+
const errCtx = Object.create(ws);
|
|
263
|
+
errCtx.error = error;
|
|
264
|
+
if (frozenRootOf(app)["~config"]?.allowUnsafeValidationDetails && error instanceof ValidationError) error.allowUnsafeValidationDetails = true;
|
|
265
|
+
for (let i = 0; i < errorHandlers.length; i++) {
|
|
266
|
+
let r;
|
|
267
|
+
try {
|
|
268
|
+
r = errorHandlers[i](errCtx);
|
|
269
|
+
if (r instanceof Promise) r = await r;
|
|
270
|
+
} catch {
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
if (r !== void 0) {
|
|
274
|
+
try {
|
|
275
|
+
await handleWSResponse(ws, r, mapResponses);
|
|
276
|
+
} catch {}
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
sendErrorFrame(ws, error);
|
|
281
|
+
}
|
|
282
|
+
const bodyValidator = validators.body;
|
|
283
|
+
function validateMessageBody(message) {
|
|
284
|
+
if (!bodyValidator) return message;
|
|
285
|
+
if (bodyValidator.hasCodec) return bodyValidator.From(message, "body");
|
|
286
|
+
if (bodyValidator instanceof StandardValidator) return bodyValidator.From(message, "body");
|
|
287
|
+
return bodyValidator.EncodeFrom(message, "body");
|
|
288
|
+
}
|
|
289
|
+
function onMessageValidationError(ws, error) {
|
|
290
|
+
if (errorHandlers.length === 0) {
|
|
291
|
+
if (frozenRootOf(app)["~config"]?.allowUnsafeValidationDetails && error instanceof ValidationError) error.allowUnsafeValidationDetails = true;
|
|
292
|
+
return sendErrorFrame(ws, error);
|
|
293
|
+
}
|
|
294
|
+
return handleError(ws, error);
|
|
295
|
+
}
|
|
296
|
+
const messageHandlerTouchesBody = !!bodyValidator || handlerMayTouchBody(hook.message) || errorHandlers.some(handlerMayTouchBody);
|
|
297
|
+
const syncDispatchEligible = transforms.length === 0 && messageBeforeHandles.length === 0 && afterHandles.length === 0 && afterResponses.length === 0 && mapResponses.length === 0;
|
|
298
|
+
function finishMessageResult(ws, value) {
|
|
299
|
+
if (value === void 0) return;
|
|
300
|
+
if (isGeneratorObject(value)) return handleWSResponse(ws, value, mapResponses).catch((error) => handleError(ws, error));
|
|
301
|
+
try {
|
|
302
|
+
ws.send(value);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
return handleError(ws, error);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
function dispatchParsedSync(ws, message) {
|
|
308
|
+
if (bodyValidator) {
|
|
309
|
+
let decoded;
|
|
310
|
+
try {
|
|
311
|
+
decoded = validateMessageBody(message);
|
|
312
|
+
} catch (error) {
|
|
313
|
+
return onMessageValidationError(ws, error);
|
|
314
|
+
}
|
|
315
|
+
if (decoded instanceof Promise) return decoded.then((m) => runMessage(ws, m), (error) => onMessageValidationError(ws, error));
|
|
316
|
+
message = decoded;
|
|
317
|
+
}
|
|
318
|
+
return runMessage(ws, message);
|
|
319
|
+
}
|
|
320
|
+
function runMessageSync(ws, message) {
|
|
321
|
+
try {
|
|
322
|
+
if (messageHandlerTouchesBody) ws.body = message;
|
|
323
|
+
const result = hook.message(ws, message);
|
|
324
|
+
if (result instanceof Promise) return result.then((resolved) => finishMessageResult(ws, resolved), (error) => handleError(ws, error));
|
|
325
|
+
return finishMessageResult(ws, result);
|
|
326
|
+
} catch (error) {
|
|
327
|
+
return handleError(ws, error);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
async function runMessageFull(ws, message) {
|
|
331
|
+
try {
|
|
332
|
+
ws.body = message;
|
|
333
|
+
for (let i = 0; i < transforms.length; i++) {
|
|
334
|
+
const r = transforms[i](ws);
|
|
335
|
+
if (r instanceof Promise) await r;
|
|
336
|
+
}
|
|
337
|
+
for (let i = 0; i < messageBeforeHandles.length; i++) {
|
|
338
|
+
let r = messageBeforeHandles[i](ws);
|
|
339
|
+
if (r instanceof Promise) r = await r;
|
|
340
|
+
if (r !== void 0) {
|
|
341
|
+
await handleWSResponse(ws, r, mapResponses);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const result = hook.message(ws, message);
|
|
346
|
+
const resolved = result instanceof Promise ? await result : result;
|
|
347
|
+
if (resolved !== void 0) await handleWSResponse(ws, resolved, mapResponses);
|
|
348
|
+
for (let i = 0; i < afterHandles.length; i++) {
|
|
349
|
+
const r = afterHandles[i](ws);
|
|
350
|
+
if (r instanceof Promise) await r;
|
|
351
|
+
}
|
|
352
|
+
for (let i = 0; i < afterResponses.length; i++) try {
|
|
353
|
+
const r = afterResponses[i](ws);
|
|
354
|
+
if (r instanceof Promise) await r;
|
|
355
|
+
} catch {}
|
|
356
|
+
} catch (error) {
|
|
357
|
+
await handleError(ws, error);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
const runMessage = syncDispatchEligible ? runMessageSync : runMessageFull;
|
|
361
|
+
function dispatchMessageSync(connection, rawMessage) {
|
|
362
|
+
const ws = Object.create(connection);
|
|
363
|
+
try {
|
|
364
|
+
const p = parseMessage(ws, rawMessage);
|
|
365
|
+
if (p instanceof Promise) return p.then((message) => dispatchParsedSync(ws, message), (error) => handleError(ws, error));
|
|
366
|
+
return dispatchParsedSync(ws, p);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
return handleError(ws, error);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function wrapLifecycle(fn, withBody) {
|
|
372
|
+
if (!fn) return;
|
|
373
|
+
return async (connection, bodyArg) => {
|
|
374
|
+
const ws = Object.create(connection);
|
|
375
|
+
try {
|
|
376
|
+
if (withBody) ws.body = bodyArg;
|
|
377
|
+
const result = withBody ? fn(ws, bodyArg) : fn(ws);
|
|
378
|
+
await handleWSResponse(ws, result instanceof Promise ? await result : result, mapResponses);
|
|
379
|
+
} catch (error) {
|
|
380
|
+
await handleError(ws, error);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
const onOpen = wrapLifecycle(hook.open, false);
|
|
385
|
+
const onDrain = wrapLifecycle(hook.drain, false);
|
|
386
|
+
const onPing = wrapLifecycle(hook.ping, true);
|
|
387
|
+
const onPong = wrapLifecycle(hook.pong, true);
|
|
388
|
+
const onClose = hook.close ? async (connection, code, reason) => {
|
|
389
|
+
const ws = Object.create(connection);
|
|
390
|
+
try {
|
|
391
|
+
ws.code = code;
|
|
392
|
+
ws.reason = reason;
|
|
393
|
+
const fn = hook.close;
|
|
394
|
+
const result = fn(ws, code, reason);
|
|
395
|
+
await handleWSResponse(ws, result instanceof Promise ? await result : result, mapResponses);
|
|
396
|
+
} catch (error) {
|
|
397
|
+
await handleError(ws, error);
|
|
398
|
+
}
|
|
399
|
+
} : void 0;
|
|
400
|
+
const fetchHandler = async (context) => {
|
|
401
|
+
const request = context.request;
|
|
402
|
+
try {
|
|
403
|
+
if (validators.params) {
|
|
404
|
+
let r = validateUpgradeChannel(validators.params, context.params ?? nullObject(), "params");
|
|
405
|
+
if (r instanceof Promise) r = await r;
|
|
406
|
+
context.params = r;
|
|
407
|
+
}
|
|
408
|
+
if (validators.query) {
|
|
409
|
+
const url = request.url;
|
|
410
|
+
const query = parseQueryFromURL(url, context.qi ?? url.indexOf("?"), queryArray, queryObject);
|
|
411
|
+
let r = validateUpgradeChannel(validators.query, query, "query");
|
|
412
|
+
if (r instanceof Promise) r = await r;
|
|
413
|
+
context.query = r;
|
|
414
|
+
}
|
|
415
|
+
if (validators.headers) {
|
|
416
|
+
const headers = isBun ? request.headers.toJSON() : Object.fromEntries(request.headers);
|
|
417
|
+
let r = validateUpgradeChannel(validators.headers, headers, "headers");
|
|
418
|
+
if (r instanceof Promise) r = await r;
|
|
419
|
+
context.headers = r;
|
|
420
|
+
}
|
|
421
|
+
for (let i = 0; i < transforms.length; i++) {
|
|
422
|
+
const r = transforms[i](context);
|
|
423
|
+
if (r instanceof Promise) await r;
|
|
424
|
+
}
|
|
425
|
+
for (let i = 0; i < allBeforeHandles.length; i++) {
|
|
426
|
+
const fn = allBeforeHandles[i];
|
|
427
|
+
let r = fn(context);
|
|
428
|
+
if (r instanceof Promise) r = await r;
|
|
429
|
+
const deriveMode = upgradeDeriveModes?.[i];
|
|
430
|
+
if (deriveMode !== void 0 && !(r instanceof ElysiaStatus)) {
|
|
431
|
+
if (r && typeof r === "object") if (deriveMode) context = replaceDeriveContext(context, r);
|
|
432
|
+
else Object.assign(context, r);
|
|
433
|
+
} else if (r !== void 0) {
|
|
434
|
+
if (r instanceof Response) return r;
|
|
435
|
+
return mapResponse(r, context.set, context.request);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
let upgradeHeaders;
|
|
439
|
+
if (hook.upgrade != null) {
|
|
440
|
+
const r = typeof hook.upgrade === "function" ? hook.upgrade(context) : hook.upgrade;
|
|
441
|
+
const resolved = r instanceof Promise ? await r : r;
|
|
442
|
+
if (resolved && typeof resolved === "object") upgradeHeaders = resolved;
|
|
443
|
+
}
|
|
444
|
+
const server = app.server;
|
|
445
|
+
if (!server) return problemResponse({
|
|
446
|
+
status: 500,
|
|
447
|
+
type: "internal-server-error",
|
|
448
|
+
title: "Internal Server Error",
|
|
449
|
+
detail: "WebSocket upgrade requires a running server. Call .listen() first."
|
|
450
|
+
});
|
|
451
|
+
if (!server.upgrade(request, {
|
|
452
|
+
headers: upgradeHeaders,
|
|
453
|
+
data: {
|
|
454
|
+
id: void 0,
|
|
455
|
+
context,
|
|
456
|
+
validator: responseValidator,
|
|
457
|
+
defaultValidator: defaultResponseValidator,
|
|
458
|
+
open: onOpen,
|
|
459
|
+
message: hook.message ? dispatchMessageSync : void 0,
|
|
460
|
+
drain: onDrain,
|
|
461
|
+
close: onClose,
|
|
462
|
+
ping: onPing,
|
|
463
|
+
pong: onPong
|
|
464
|
+
}
|
|
465
|
+
})) return new Response("Expected a websocket connection", { status: 400 });
|
|
466
|
+
} catch (error) {
|
|
467
|
+
return handleUpgradeError(context, error);
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
const options = nullObject();
|
|
471
|
+
for (const k of wsOptions) if (hook[k] !== void 0) options[k] = hook[k];
|
|
472
|
+
return [fetchHandler, options];
|
|
473
|
+
}
|
|
474
|
+
function resolveWSOptions(entries) {
|
|
475
|
+
if (!entries || entries.length === 0) return void 0;
|
|
476
|
+
if (entries.length === 1) return { ...entries[0].value };
|
|
477
|
+
const ordered = entries.map((entry, index) => ({
|
|
478
|
+
entry,
|
|
479
|
+
index
|
|
480
|
+
})).sort((a, b) => b.entry.depth - a.entry.depth || a.index - b.index);
|
|
481
|
+
const result = {};
|
|
482
|
+
let warned;
|
|
483
|
+
for (const { entry } of ordered) for (const key in entry.value) {
|
|
484
|
+
const incoming = entry.value[key];
|
|
485
|
+
if (key in result && result[key] !== incoming && !warned?.has(key)) {
|
|
486
|
+
(warned ??= /* @__PURE__ */ new Set()).add(key);
|
|
487
|
+
console.warn(`[Elysia] Conflicting WebSocket option '${key}' across .use(websocket()) registrations; the shallowest (nearest-root) registration wins.`);
|
|
488
|
+
}
|
|
489
|
+
result[key] = incoming;
|
|
490
|
+
}
|
|
491
|
+
return result;
|
|
492
|
+
}
|
|
493
|
+
function accumulateWSOptions(target, routeOptions, _path) {
|
|
494
|
+
if (!isNotEmpty(routeOptions)) return;
|
|
495
|
+
if (isBun) {
|
|
496
|
+
for (const key in routeOptions) if (key in target && target[key] !== routeOptions[key]) {
|
|
497
|
+
console.warn(`[Elysia] Conflicting per-route WebSocket option '${key}'\nBun uses one global WebSocket config per server, per-route values are not enforced (the last-registered route wins).`);
|
|
498
|
+
console.warn((/* @__PURE__ */ new Error()).stack);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
Object.assign(target, routeOptions);
|
|
502
|
+
}
|
|
503
|
+
function buildGlobalWSHandler() {
|
|
504
|
+
function getElysia(ws) {
|
|
505
|
+
let elysia = ws.data.elysia;
|
|
506
|
+
if (!elysia) {
|
|
507
|
+
elysia = new ElysiaWS(ws, ws.data.context);
|
|
508
|
+
ws.data.elysia = elysia;
|
|
509
|
+
ws.data.context = void 0;
|
|
510
|
+
}
|
|
511
|
+
return elysia;
|
|
512
|
+
}
|
|
513
|
+
const sendErrMsg = (ws, error) => {
|
|
514
|
+
try {
|
|
515
|
+
ws.send(typeof error?.message === "string" ? error.message : "Internal Server Error");
|
|
516
|
+
} catch {}
|
|
517
|
+
};
|
|
518
|
+
return {
|
|
519
|
+
message(ws, message) {
|
|
520
|
+
let result;
|
|
521
|
+
try {
|
|
522
|
+
result = ws.data.message?.(getElysia(ws), message);
|
|
523
|
+
} catch (error) {
|
|
524
|
+
sendErrMsg(ws, error);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (result instanceof Promise) result.catch((error) => sendErrMsg(ws, error));
|
|
528
|
+
},
|
|
529
|
+
open(ws) {
|
|
530
|
+
ws.data.open?.(getElysia(ws));
|
|
531
|
+
},
|
|
532
|
+
drain(ws) {
|
|
533
|
+
const elyWs = getElysia(ws);
|
|
534
|
+
drainWaiters(elyWs);
|
|
535
|
+
ws.data.drain?.(elyWs);
|
|
536
|
+
},
|
|
537
|
+
close(ws, code, reason) {
|
|
538
|
+
const elyWs = getElysia(ws);
|
|
539
|
+
drainWaiters(elyWs);
|
|
540
|
+
if (ws.data.closeHandlerInvoked) return;
|
|
541
|
+
ws.data.closeHandlerInvoked = true;
|
|
542
|
+
ws.data.close?.(elyWs, code, reason);
|
|
543
|
+
},
|
|
544
|
+
ping(ws, data) {
|
|
545
|
+
ws.data.ping?.(getElysia(ws), data);
|
|
546
|
+
},
|
|
547
|
+
pong(ws, data) {
|
|
548
|
+
ws.data.pong?.(getElysia(ws), data);
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
//#endregion
|
|
554
|
+
export { accumulateWSOptions, buildGlobalWSHandler, buildWSRoute, drainWaiters, handleWSResponse, resolveWSOptions };
|