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.
- package/README.md +1 -1
- package/dist/_virtual/_rolldown/runtime.mjs +1 -0
- package/dist/adapter/bun/index.d.ts +16 -2
- package/dist/adapter/bun/index.js +458 -119
- package/dist/adapter/bun/index.mjs +459 -121
- package/dist/adapter/constants.mjs +1 -0
- package/dist/adapter/default-headers.mjs +1 -0
- package/dist/adapter/index.mjs +1 -0
- package/dist/adapter/origin.js +0 -57
- package/dist/adapter/origin.mjs +1 -57
- package/dist/adapter/response-ownership.mjs +1 -0
- package/dist/adapter/skip-clone.mjs +1 -0
- package/dist/adapter/types.d.ts +2 -21
- package/dist/adapter/types.mjs +1 -0
- package/dist/adapter/utils.d.ts +11 -3
- package/dist/adapter/utils.js +147 -112
- package/dist/adapter/utils.mjs +149 -114
- package/dist/adapter/web-standard/handler.d.ts +1 -1
- package/dist/adapter/web-standard/handler.js +39 -36
- package/dist/adapter/web-standard/handler.mjs +41 -37
- package/dist/adapter/web-standard/index.js +2 -30
- package/dist/adapter/web-standard/index.mjs +3 -30
- package/dist/adapter/web-standard/utils.js +5 -18
- package/dist/adapter/web-standard/utils.mjs +6 -18
- package/dist/base.d.ts +387 -207
- package/dist/base.js +742 -676
- package/dist/base.mjs +747 -680
- package/dist/compile/aot-capture.d.ts +2 -13
- package/dist/compile/aot-capture.js +39 -89
- package/dist/compile/aot-capture.mjs +43 -88
- package/dist/compile/aot-emit.d.ts +9 -8
- package/dist/compile/aot-emit.js +36 -38
- package/dist/compile/aot-emit.mjs +37 -38
- package/dist/compile/aot-reconstruct.js +7 -30
- package/dist/compile/aot-reconstruct.mjs +8 -30
- package/dist/compile/aot.d.ts +15 -17
- package/dist/compile/aot.js +36 -58
- package/dist/compile/aot.mjs +34 -57
- package/dist/compile/handler/descriptor.d.ts +10 -17
- package/dist/compile/handler/descriptor.js +25 -58
- package/dist/compile/handler/descriptor.mjs +29 -58
- package/dist/compile/handler/frozen-validator.d.ts +7 -12
- package/dist/compile/handler/frozen-validator.js +57 -118
- package/dist/compile/handler/frozen-validator.mjs +56 -118
- package/dist/compile/handler/index.d.ts +8 -4
- package/dist/compile/handler/index.js +322 -206
- package/dist/compile/handler/index.mjs +324 -211
- package/dist/compile/handler/jit.d.ts +28 -21
- package/dist/compile/handler/jit.js +232 -159
- package/dist/compile/handler/jit.mjs +235 -165
- package/dist/compile/handler/params.d.ts +5 -4
- package/dist/compile/handler/params.js +21 -9
- package/dist/compile/handler/params.mjs +25 -12
- package/dist/compile/handler/reconstruct.d.ts +4 -4
- package/dist/compile/handler/reconstruct.js +8 -6
- package/dist/compile/handler/reconstruct.mjs +9 -6
- package/dist/compile/handler/static-clone-resolver.d.ts +6 -0
- package/dist/compile/handler/static-clone-resolver.js +8 -0
- package/dist/compile/handler/static-clone-resolver.mjs +8 -0
- package/dist/compile/handler/utils.d.ts +29 -11
- package/dist/compile/handler/utils.js +184 -276
- package/dist/compile/handler/utils.mjs +179 -275
- package/dist/compile/jit-probe.d.ts +2 -2
- package/dist/compile/jit-probe.js +2 -3
- package/dist/compile/jit-probe.mjs +3 -3
- package/dist/compile/lexer.d.ts +7 -4
- package/dist/compile/lexer.js +268 -6
- package/dist/compile/lexer.mjs +269 -4
- package/dist/compile/utils.d.ts +2 -1
- package/dist/compile/utils.js +14 -6
- package/dist/compile/utils.mjs +14 -6
- package/dist/compiled.d.ts +0 -1
- package/dist/compiled.js +0 -10
- package/dist/compiled.mjs +1 -10
- package/dist/constants.js +19 -4
- package/dist/constants.mjs +20 -4
- package/dist/context.d.ts +15 -5
- package/dist/context.js +17 -36
- package/dist/context.mjs +20 -38
- package/dist/cookie/config.d.ts +6 -2
- package/dist/cookie/config.js +77 -23
- package/dist/cookie/config.mjs +79 -25
- package/dist/cookie/cookie.d.ts +7 -5
- package/dist/cookie/cookie.js +12 -10
- package/dist/cookie/cookie.mjs +13 -10
- package/dist/cookie/crypto.d.ts +1 -1
- package/dist/cookie/crypto.js +19 -14
- package/dist/cookie/crypto.mjs +20 -14
- package/dist/cookie/error.d.ts +1 -2
- package/dist/cookie/error.js +6 -3
- package/dist/cookie/error.mjs +7 -3
- package/dist/cookie/lib.js +2 -1
- package/dist/cookie/lib.mjs +3 -1
- package/dist/cookie/serialize.mjs +1 -0
- package/dist/cookie/types.d.ts +1 -1
- package/dist/cookie/types.mjs +1 -0
- package/dist/cookie/utils.d.ts +5 -9
- package/dist/cookie/utils.js +109 -83
- package/dist/cookie/utils.mjs +112 -84
- package/dist/error.d.ts +127 -111
- package/dist/error.js +187 -180
- package/dist/error.mjs +185 -181
- package/dist/generation.d.ts +2 -2
- package/dist/generation.mjs +1 -0
- package/dist/handler/error.d.ts +18 -18
- package/dist/handler/error.js +77 -120
- package/dist/handler/error.mjs +80 -122
- package/dist/handler/fetch.js +165 -153
- package/dist/handler/fetch.mjs +168 -155
- package/dist/handler/utils.d.ts +25 -6
- package/dist/handler/utils.js +81 -26
- package/dist/handler/utils.mjs +76 -25
- package/dist/index.d.ts +11 -11
- package/dist/index.js +3 -1
- package/dist/index.mjs +5 -4
- package/dist/memory.js +2 -2
- package/dist/memory.mjs +3 -2
- package/dist/package.js +1 -1
- package/dist/package.mjs +2 -1
- package/dist/parse-query.js +76 -80
- package/dist/parse-query.mjs +77 -80
- package/dist/plugin/aot/bun.d.ts +8 -1
- package/dist/plugin/aot/bun.js +9 -28
- package/dist/plugin/aot/bun.mjs +11 -29
- package/dist/plugin/aot/core.d.ts +37 -66
- package/dist/plugin/aot/core.js +197 -153
- package/dist/plugin/aot/core.mjs +195 -156
- package/dist/plugin/aot/esbuild.js +8 -24
- package/dist/plugin/aot/esbuild.mjs +10 -25
- package/dist/plugin/aot/hooks.d.ts +6 -17
- package/dist/plugin/aot/hooks.js +37 -17
- package/dist/plugin/aot/hooks.mjs +38 -19
- package/dist/plugin/aot/rspack-loader.d.ts +1 -5
- package/dist/plugin/aot/rspack-loader.js +1 -26
- package/dist/plugin/aot/rspack-loader.mjs +2 -26
- package/dist/plugin/aot/rspack.js +2 -19
- package/dist/plugin/aot/rspack.mjs +4 -20
- package/dist/plugin/aot/source.d.ts +7 -43
- package/dist/plugin/aot/source.js +73 -73
- package/dist/plugin/aot/source.mjs +74 -74
- package/dist/plugin/aot/treeshake.js +2 -6
- package/dist/plugin/aot/treeshake.mjs +3 -6
- package/dist/plugin/aot/unplugin.d.ts +1 -4
- package/dist/plugin/aot/unplugin.js +4 -32
- package/dist/plugin/aot/unplugin.mjs +5 -32
- package/dist/plugin/aot/vite.d.ts +2 -6
- package/dist/plugin/aot/vite.js +0 -14
- package/dist/plugin/aot/vite.mjs +1 -14
- package/dist/plugin/aot/worker.js +2 -2
- package/dist/plugin/aot/worker.mjs +3 -2
- package/dist/plugin/auto-head.js +1 -10
- package/dist/plugin/auto-head.mjs +2 -10
- package/dist/plugin/trace.d.ts +18 -1
- package/dist/plugin/trace.js +0 -16
- package/dist/plugin/trace.mjs +1 -16
- package/dist/plugin/websocket.d.ts +21 -0
- package/dist/plugin/websocket.js +0 -21
- package/dist/plugin/websocket.mjs +1 -21
- package/dist/route-table.d.ts +3 -0
- package/dist/route-table.js +4 -1
- package/dist/route-table.mjs +5 -1
- package/dist/schema-snapshot.d.ts +0 -4
- package/dist/schema-snapshot.js +60 -56
- package/dist/schema-snapshot.mjs +61 -56
- package/dist/sucrose.d.ts +7 -56
- package/dist/sucrose.js +103 -735
- package/dist/sucrose.mjs +103 -728
- package/dist/trace.d.ts +16 -10
- package/dist/trace.js +20 -134
- package/dist/trace.mjs +22 -135
- package/dist/type/bridge-live.d.ts +5 -3
- package/dist/type/bridge-live.js +6 -3
- package/dist/type/bridge-live.mjs +6 -3
- package/dist/type/bridge.d.ts +7 -5
- package/dist/type/bridge.js +38 -28
- package/dist/type/bridge.mjs +27 -22
- package/dist/type/coerce-plan.d.ts +5 -5
- package/dist/type/coerce-plan.js +53 -43
- package/dist/type/coerce-plan.mjs +56 -44
- package/dist/type/coerce.d.ts +3 -7
- package/dist/type/coerce.js +18 -79
- package/dist/type/coerce.mjs +20 -80
- package/dist/type/compat.js +10 -19
- package/dist/type/compat.mjs +12 -20
- package/dist/type/constants.d.ts +1 -1
- package/dist/type/constants.mjs +1 -0
- package/dist/type/elysia/accelerate.mjs +1 -0
- package/dist/type/elysia/array-buffer.d.ts +6 -2
- package/dist/type/elysia/array-buffer.js +16 -15
- package/dist/type/elysia/array-buffer.mjs +17 -16
- package/dist/type/elysia/array-string.js +2 -43
- package/dist/type/elysia/array-string.mjs +3 -43
- package/dist/type/elysia/array.js +5 -9
- package/dist/type/elysia/array.mjs +6 -9
- package/dist/type/elysia/boolean-string.mjs +1 -0
- package/dist/type/elysia/boolean.js +3 -13
- package/dist/type/elysia/boolean.mjs +4 -13
- package/dist/type/elysia/cookie.d.ts +3 -4
- package/dist/type/elysia/cookie.js +3 -14
- package/dist/type/elysia/cookie.mjs +4 -14
- package/dist/type/elysia/date.d.ts +2 -1
- package/dist/type/elysia/date.js +7 -14
- package/dist/type/elysia/date.mjs +8 -14
- package/dist/type/elysia/file-type.d.ts +2 -2
- package/dist/type/elysia/file-type.js +4 -3
- package/dist/type/elysia/file-type.mjs +5 -3
- package/dist/type/elysia/file.d.ts +1 -3
- package/dist/type/elysia/file.js +3 -23
- package/dist/type/elysia/file.mjs +5 -10
- package/dist/type/elysia/files.js +1 -7
- package/dist/type/elysia/files.mjs +3 -8
- package/dist/type/elysia/form.mjs +1 -0
- package/dist/type/elysia/integer-string.d.ts +1 -1
- package/dist/type/elysia/integer-string.js +3 -28
- package/dist/type/elysia/integer-string.mjs +5 -29
- package/dist/type/elysia/integer.js +3 -13
- package/dist/type/elysia/integer.mjs +4 -13
- package/dist/type/elysia/intersect.js +0 -5
- package/dist/type/elysia/intersect.mjs +1 -5
- package/dist/type/elysia/json-string.d.ts +7 -0
- package/dist/type/elysia/json-string.js +47 -0
- package/dist/type/elysia/json-string.mjs +47 -0
- package/dist/type/elysia/keep-config.d.ts +14 -0
- package/dist/type/elysia/keep-config.js +62 -0
- package/dist/type/elysia/keep-config.mjs +54 -0
- package/dist/type/elysia/maybe-empty.mjs +1 -0
- package/dist/type/elysia/no-validate.mjs +1 -0
- package/dist/type/elysia/nullable.mjs +1 -0
- package/dist/type/elysia/number.d.ts +2 -1
- package/dist/type/elysia/number.js +19 -12
- package/dist/type/elysia/number.mjs +19 -12
- package/dist/type/elysia/numeric-enum.d.ts +5 -3
- package/dist/type/elysia/numeric-enum.js +1 -5
- package/dist/type/elysia/numeric-enum.mjs +2 -5
- package/dist/type/elysia/numeric.d.ts +4 -2
- package/dist/type/elysia/numeric.js +17 -14
- package/dist/type/elysia/numeric.mjs +17 -14
- package/dist/type/elysia/object-string.js +2 -40
- package/dist/type/elysia/object-string.mjs +3 -40
- package/dist/type/elysia/object.js +5 -9
- package/dist/type/elysia/object.mjs +6 -9
- package/dist/type/elysia/optional.js +8 -8
- package/dist/type/elysia/optional.mjs +8 -7
- package/dist/type/elysia/problem.d.ts +20 -0
- package/dist/type/elysia/problem.js +19 -0
- package/dist/type/elysia/problem.mjs +19 -0
- package/dist/type/elysia/string.mjs +1 -0
- package/dist/type/elysia/uint8-array.d.ts +2 -1
- package/dist/type/elysia/uint8-array.js +4 -18
- package/dist/type/elysia/uint8-array.mjs +5 -18
- package/dist/type/elysia/union-enum.js +5 -6
- package/dist/type/elysia/union-enum.mjs +6 -6
- package/dist/type/elysia/union.js +0 -5
- package/dist/type/elysia/union.mjs +1 -5
- package/dist/type/elysia/utils.d.ts +4 -6
- package/dist/type/elysia/utils.js +16 -40
- package/dist/type/elysia/utils.mjs +15 -36
- package/dist/type/exports.d.ts +4 -2
- package/dist/type/exports.js +13 -1
- package/dist/type/exports.mjs +5 -2
- package/dist/type/index.d.ts +1 -1
- package/dist/type/index.js +31 -23
- package/dist/type/index.mjs +33 -24
- package/dist/type/shared.d.ts +12 -10
- package/dist/type/shared.js +75 -109
- package/dist/type/shared.mjs +72 -108
- package/dist/type/sync-require.d.ts +11 -0
- package/dist/type/sync-require.js +7 -0
- package/dist/type/sync-require.mjs +6 -0
- package/dist/type/typebox-system-lite.d.ts +2 -0
- package/dist/type/typebox-system-lite.js +34 -0
- package/dist/type/typebox-system-lite.mjs +4 -0
- package/dist/type/typebox-system-require.d.ts +7 -0
- package/dist/type/typebox-system-require.js +24 -0
- package/dist/type/typebox-system-require.mjs +24 -0
- package/dist/type/typebox-type-live.d.ts +6 -4
- package/dist/type/typebox-type-live.js +23 -9
- package/dist/type/typebox-type-live.mjs +11 -6
- package/dist/type/typebox-type.d.ts +14 -4
- package/dist/type/typebox-type.js +85 -22
- package/dist/type/typebox-type.mjs +35 -21
- package/dist/type/typebox-value-live.d.ts +3 -1
- package/dist/type/typebox-value-live.js +5 -1
- package/dist/type/typebox-value-live.mjs +4 -1
- package/dist/type/typebox-value-ops.d.ts +4 -0
- package/dist/type/typebox-value-ops.js +90 -0
- package/dist/type/typebox-value-ops.mjs +6 -0
- package/dist/type/typebox-value-require.d.ts +8 -0
- package/dist/type/typebox-value-require.js +28 -0
- package/dist/type/typebox-value-require.mjs +25 -0
- package/dist/type/typebox-value.d.ts +9 -4
- package/dist/type/typebox-value.js +19 -8
- package/dist/type/typebox-value.mjs +19 -9
- package/dist/type/types.d.ts +1 -1
- package/dist/type/types.mjs +1 -0
- package/dist/type/utils.js +1 -10
- package/dist/type/utils.mjs +2 -10
- package/dist/type/validator/clean-safe.d.ts +2 -4
- package/dist/type/validator/clean-safe.js +15 -11
- package/dist/type/validator/clean-safe.mjs +17 -10
- package/dist/type/validator/custom-error.js +50 -48
- package/dist/type/validator/custom-error.mjs +51 -48
- package/dist/type/validator/default-precompute.d.ts +10 -2
- package/dist/type/validator/default-precompute.js +33 -77
- package/dist/type/validator/default-precompute.mjs +33 -77
- package/dist/type/validator/exact-mirror-live.d.ts +8 -0
- package/dist/type/validator/exact-mirror-live.js +15 -0
- package/dist/type/validator/exact-mirror-live.mjs +11 -0
- package/dist/type/validator/exact-mirror-require.d.ts +4 -0
- package/dist/type/validator/exact-mirror-require.js +11 -0
- package/dist/type/validator/exact-mirror-require.mjs +9 -0
- package/dist/type/validator/exact-mirror.js +15 -7
- package/dist/type/validator/exact-mirror.mjs +17 -7
- package/dist/type/validator/frozen-check.js +7 -8
- package/dist/type/validator/frozen-check.mjs +9 -9
- package/dist/type/validator/index.d.ts +2 -1
- package/dist/type/validator/index.js +149 -168
- package/dist/type/validator/index.mjs +155 -173
- package/dist/type/validator/string-codec-aot.d.ts +3 -2
- package/dist/type/validator/string-codec-aot.js +11 -7
- package/dist/type/validator/string-codec-aot.mjs +11 -7
- package/dist/type/validator/validator-cache.d.ts +5 -4
- package/dist/type/validator/validator-cache.js +49 -37
- package/dist/type/validator/validator-cache.mjs +49 -36
- package/dist/types.d.ts +596 -187
- package/dist/types.mjs +1 -0
- package/dist/universal/constants.mjs +1 -0
- package/dist/universal/env.js +2 -3
- package/dist/universal/env.mjs +2 -3
- package/dist/universal/file.d.ts +6 -4
- package/dist/universal/file.js +7 -19
- package/dist/universal/file.mjs +9 -19
- package/dist/universal/index.d.ts +2 -2
- package/dist/universal/index.mjs +1 -0
- package/dist/universal/is-production.d.ts +1 -2
- package/dist/universal/is-production.js +1 -3
- package/dist/universal/is-production.mjs +3 -3
- package/dist/universal/server.d.ts +0 -272
- package/dist/universal/server.mjs +1 -0
- package/dist/utils.d.ts +62 -22
- package/dist/utils.js +342 -277
- package/dist/utils.mjs +327 -273
- package/dist/validator/index.d.ts +3 -3
- package/dist/validator/index.js +20 -34
- package/dist/validator/index.mjs +23 -36
- package/dist/validator/route.d.ts +3 -10
- package/dist/validator/route.js +2 -1
- package/dist/validator/route.mjs +3 -1
- package/dist/ws/context.d.ts +4 -1
- package/dist/ws/context.js +41 -36
- package/dist/ws/context.mjs +41 -36
- package/dist/ws/index.mjs +1 -0
- package/dist/ws/parser.js +12 -22
- package/dist/ws/parser.mjs +13 -22
- package/dist/ws/route.d.ts +2 -2
- package/dist/ws/route.js +295 -221
- package/dist/ws/route.mjs +302 -227
- package/dist/ws/types.d.ts +6 -7
- package/dist/ws/types.mjs +1 -0
- package/package.json +22 -55
- package/dist/compile/analysis-cache.d.ts +0 -6
- package/dist/compile/analysis-cache.js +0 -12
- package/dist/compile/analysis-cache.mjs +0 -11
- package/dist/cookie/index.d.ts +0 -7
- package/dist/cookie/index.js +0 -13
- package/dist/cookie/index.mjs +0 -7
- package/dist/handler/index.d.ts +0 -2
- package/dist/handler/index.js +0 -5
- package/dist/handler/index.mjs +0 -3
package/dist/adapter/utils.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
import { hasHeaderShorthand } from "../universal/constants.mjs";
|
|
2
3
|
import { StatusMap } from "../constants.mjs";
|
|
3
4
|
import { isByteStream, isNotEmpty, nullObject, sseData } from "../utils.mjs";
|
|
4
5
|
import { skipClone } from "./skip-clone.mjs";
|
|
@@ -16,22 +17,16 @@ function materializeSetHeaders(set) {
|
|
|
16
17
|
const sseFormat = (data) => sseData(data) + "\n";
|
|
17
18
|
const identityFormat = (data) => data;
|
|
18
19
|
const textEncoder = new TextEncoder();
|
|
19
|
-
const encodeChunk = (s) => textEncoder.encode(s);
|
|
20
20
|
function normalizeContentType(contentType) {
|
|
21
21
|
if (contentType === "application/json") return contentType;
|
|
22
22
|
const end = contentType.indexOf(";");
|
|
23
23
|
return (end === -1 ? contentType : contentType.slice(0, end)).trim().toLowerCase();
|
|
24
24
|
}
|
|
25
25
|
function handleFile(response, set, request, size = response.size) {
|
|
26
|
-
if (!isBun && response instanceof Promise) return response.then((res) => handleFile(res, set, request, size));
|
|
27
26
|
const rangeHeader = request?.headers.get("range");
|
|
28
27
|
if (rangeHeader) {
|
|
29
28
|
const match = /bytes=(\d*)-(\d*)/.exec(rangeHeader);
|
|
30
29
|
if (match) {
|
|
31
|
-
if (!match[1] && !match[2]) return new Response(null, {
|
|
32
|
-
status: 416,
|
|
33
|
-
headers: mergeHeaders(new Headers({ "content-range": `bytes */${size}` }), set?.headers ?? nullObject())
|
|
34
|
-
});
|
|
35
30
|
let start;
|
|
36
31
|
let end;
|
|
37
32
|
if (!match[1] && match[2]) {
|
|
@@ -42,7 +37,7 @@ function handleFile(response, set, request, size = response.size) {
|
|
|
42
37
|
start = match[1] ? parseInt(match[1]) : 0;
|
|
43
38
|
end = match[2] ? Math.min(parseInt(match[2]), size - 1) : size - 1;
|
|
44
39
|
}
|
|
45
|
-
if (start >= size || start > end) return new Response(null, {
|
|
40
|
+
if (!match[1] && !match[2] || start >= size || start > end) return new Response(null, {
|
|
46
41
|
status: 416,
|
|
47
42
|
headers: mergeHeaders(new Headers({ "content-range": `bytes */${size}` }), set?.headers ?? nullObject())
|
|
48
43
|
});
|
|
@@ -60,10 +55,8 @@ function handleFile(response, set, request, size = response.size) {
|
|
|
60
55
|
}
|
|
61
56
|
const body = "value" in response ? response.value : response;
|
|
62
57
|
const immutable = set && (set.status === 206 || set.status === 304 || set.status === 412 || set.status === 416);
|
|
63
|
-
const defaultHeader = immutable ? nullObject() : {
|
|
64
|
-
|
|
65
|
-
"content-range": size ? `bytes 0-${size - 1}/${size}` : void 0
|
|
66
|
-
};
|
|
58
|
+
const defaultHeader = immutable ? nullObject() : { "accept-ranges": "bytes" };
|
|
59
|
+
if (!immutable && size) defaultHeader["content-range"] = `bytes 0-${size - 1}/${size}`;
|
|
67
60
|
if (!set && !size) return new Response(body);
|
|
68
61
|
if (!set) return new Response(body, { headers: defaultHeader });
|
|
69
62
|
if (set.headers instanceof Headers) {
|
|
@@ -74,13 +67,9 @@ function handleFile(response, set, request, size = response.size) {
|
|
|
74
67
|
}
|
|
75
68
|
return new Response(body, set);
|
|
76
69
|
}
|
|
77
|
-
if (isNotEmpty(set.headers)) return new Response(body, {
|
|
78
|
-
status: set.status,
|
|
79
|
-
headers: Object.assign(defaultHeader, set.headers)
|
|
80
|
-
});
|
|
81
70
|
return new Response(body, {
|
|
82
71
|
status: set.status,
|
|
83
|
-
headers: defaultHeader
|
|
72
|
+
headers: Object.assign(defaultHeader, set.headers)
|
|
84
73
|
});
|
|
85
74
|
}
|
|
86
75
|
function normalizeHeaders(set) {
|
|
@@ -123,27 +112,9 @@ function responseToSetHeaders(response, set) {
|
|
|
123
112
|
if (set.headers["content-encoding"]) delete set.headers["content-encoding"];
|
|
124
113
|
return set;
|
|
125
114
|
}
|
|
126
|
-
function enqueueBinaryChunk(controller, chunk) {
|
|
127
|
-
if (chunk instanceof Blob) return chunk.arrayBuffer().then((buffer) => {
|
|
128
|
-
controller.enqueue(new Uint8Array(buffer));
|
|
129
|
-
return true;
|
|
130
|
-
});
|
|
131
|
-
if (chunk instanceof Uint8Array) {
|
|
132
|
-
controller.enqueue(chunk);
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
if (chunk instanceof ArrayBuffer) {
|
|
136
|
-
controller.enqueue(new Uint8Array(chunk));
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
if (ArrayBuffer.isView(chunk)) {
|
|
140
|
-
controller.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
141
|
-
return true;
|
|
142
|
-
}
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
115
|
function createStreamHandler({ mapResponse, mapCompactResponse }) {
|
|
146
116
|
return async (generator, set, request, skipFormat, owned = false) => {
|
|
117
|
+
if (request === null) return void 0;
|
|
147
118
|
if (isByteStream(generator)) {
|
|
148
119
|
if (generator.locked) throw new TypeError("Cannot transfer a locked or consumed byte stream");
|
|
149
120
|
if (set) {
|
|
@@ -157,10 +128,21 @@ function createStreamHandler({ mapResponse, mapCompactResponse }) {
|
|
|
157
128
|
return new Response(generator, { headers: { "content-type": "application/octet-stream" } });
|
|
158
129
|
}
|
|
159
130
|
const typedSSE = !skipFormat && "sse" in generator && generator.sse === true;
|
|
131
|
+
const sourceIterator = typeof generator.next === "function" ? generator : void 0;
|
|
160
132
|
let init = typedSSE ? void 0 : generator.next?.();
|
|
133
|
+
if (init instanceof Promise || typeof init?.then === "function") init = await init;
|
|
134
|
+
const sign = set?.["~sign"];
|
|
135
|
+
if (sign) try {
|
|
136
|
+
await sign(set);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
try {
|
|
139
|
+
await generator.return?.(void 0);
|
|
140
|
+
} catch {}
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
161
143
|
if (set) handleSet(set);
|
|
162
|
-
|
|
163
|
-
if (
|
|
144
|
+
const yieldedStream = init?.value instanceof ReadableStream;
|
|
145
|
+
if (yieldedStream) generator = init.value;
|
|
164
146
|
else if (init && (typeof init?.done === "undefined" || init?.done)) {
|
|
165
147
|
if (set) return mapResponse(init.value, set, request, owned);
|
|
166
148
|
return mapCompactResponse(init.value, request, owned);
|
|
@@ -194,8 +176,32 @@ function createStreamHandler({ mapResponse, mapCompactResponse }) {
|
|
|
194
176
|
connection: "keep-alive"
|
|
195
177
|
}
|
|
196
178
|
};
|
|
197
|
-
const
|
|
179
|
+
const sourceStream = generator instanceof ReadableStream ? generator : void 0;
|
|
180
|
+
let streamReader;
|
|
181
|
+
const iterator = sourceStream ? {
|
|
182
|
+
next: () => (streamReader ??= sourceStream.getReader()).read(),
|
|
183
|
+
async return() {
|
|
184
|
+
if (!streamReader) {
|
|
185
|
+
await sourceStream.cancel();
|
|
186
|
+
return {
|
|
187
|
+
done: true,
|
|
188
|
+
value: void 0
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
await streamReader.cancel();
|
|
193
|
+
} finally {
|
|
194
|
+
streamReader.releaseLock();
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
done: true,
|
|
198
|
+
value: void 0
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
} : sourceIterator ?? generator[Symbol.asyncIterator]();
|
|
202
|
+
const outerIterator = yieldedStream && !init.done ? sourceIterator : void 0;
|
|
198
203
|
let end = false;
|
|
204
|
+
let finalizing;
|
|
199
205
|
const signal = request?.signal;
|
|
200
206
|
let onAbort;
|
|
201
207
|
const cleanupAbort = () => {
|
|
@@ -204,71 +210,93 @@ function createStreamHandler({ mapResponse, mapCompactResponse }) {
|
|
|
204
210
|
onAbort = void 0;
|
|
205
211
|
}
|
|
206
212
|
};
|
|
207
|
-
const safeReturn = () => {
|
|
213
|
+
const safeReturn = async (target) => {
|
|
208
214
|
try {
|
|
209
|
-
|
|
210
|
-
if (r && typeof r.then === "function") r.catch(() => {});
|
|
215
|
+
await target?.return?.();
|
|
211
216
|
} catch {}
|
|
212
217
|
};
|
|
213
|
-
const
|
|
218
|
+
const finalize = (mode, controller, error) => {
|
|
219
|
+
if (end) return finalizing;
|
|
220
|
+
end = true;
|
|
221
|
+
cleanupAbort();
|
|
222
|
+
finalizing = safeReturn(iterator).then(() => safeReturn(outerIterator));
|
|
223
|
+
if (!controller) return finalizing;
|
|
214
224
|
try {
|
|
215
|
-
controller.
|
|
225
|
+
if (mode === "error") controller.error(error);
|
|
226
|
+
else controller.close();
|
|
216
227
|
} catch {}
|
|
217
|
-
cleanupAbort();
|
|
218
228
|
};
|
|
219
|
-
const enqueueValue =
|
|
229
|
+
const enqueueValue = (controller, value) => {
|
|
220
230
|
if (value.toSSE) {
|
|
221
|
-
controller.enqueue(
|
|
231
|
+
controller.enqueue(textEncoder.encode(value.toSSE()));
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (value instanceof Blob) return value.arrayBuffer().then((buffer) => {
|
|
235
|
+
controller.enqueue(new Uint8Array(buffer));
|
|
236
|
+
});
|
|
237
|
+
if (value instanceof Uint8Array) {
|
|
238
|
+
controller.enqueue(value);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (value instanceof ArrayBuffer) {
|
|
242
|
+
controller.enqueue(new Uint8Array(value));
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (ArrayBuffer.isView(value)) {
|
|
246
|
+
controller.enqueue(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
|
|
222
247
|
return;
|
|
223
248
|
}
|
|
224
|
-
const p = enqueueBinaryChunk(controller, value);
|
|
225
|
-
if (p !== false) return void await p;
|
|
226
249
|
if (typeof value === "object") try {
|
|
227
|
-
controller.enqueue(
|
|
250
|
+
controller.enqueue(textEncoder.encode(format(JSON.stringify(value))));
|
|
228
251
|
} catch {
|
|
229
|
-
controller.enqueue(
|
|
252
|
+
controller.enqueue(textEncoder.encode(format(value.toString())));
|
|
230
253
|
}
|
|
231
|
-
else controller.enqueue(
|
|
254
|
+
else controller.enqueue(textEncoder.encode(format(value.toString())));
|
|
232
255
|
};
|
|
233
256
|
return new Response(new ReadableStream({
|
|
234
|
-
|
|
257
|
+
start(controller) {
|
|
235
258
|
if (signal) {
|
|
236
|
-
onAbort = () =>
|
|
237
|
-
cleanupAbort();
|
|
238
|
-
end = true;
|
|
239
|
-
safeReturn();
|
|
240
|
-
try {
|
|
241
|
-
controller.close();
|
|
242
|
-
} catch {}
|
|
243
|
-
};
|
|
259
|
+
onAbort = () => finalize("close", controller);
|
|
244
260
|
if (signal.aborted) onAbort();
|
|
245
261
|
else signal.addEventListener("abort", onAbort, { once: true });
|
|
246
262
|
}
|
|
247
263
|
if (!init || init.value instanceof ReadableStream || init.value === void 0 || init.value === null) return;
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if (end) {
|
|
252
|
-
closeSafely(controller);
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
264
|
+
const fail = (error) => {
|
|
265
|
+
finalize("error", controller, error);
|
|
266
|
+
};
|
|
255
267
|
try {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
268
|
+
return enqueueValue(controller, init.value)?.catch(fail);
|
|
269
|
+
} catch (error) {
|
|
270
|
+
fail(error);
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
pull(controller) {
|
|
274
|
+
if (end) return;
|
|
275
|
+
const fail = (error) => {
|
|
276
|
+
finalize("error", controller, error);
|
|
277
|
+
};
|
|
278
|
+
const step = (result) => {
|
|
279
|
+
while (true) {
|
|
280
|
+
if (result.done || end) {
|
|
281
|
+
finalize("close", controller);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const chunk = result.value;
|
|
285
|
+
if (chunk !== void 0 && chunk !== null) return enqueueValue(controller, chunk);
|
|
286
|
+
const next = iterator.next();
|
|
287
|
+
if (typeof next.then === "function") return Promise.resolve(next).then(step);
|
|
288
|
+
result = next;
|
|
260
289
|
}
|
|
261
|
-
|
|
262
|
-
|
|
290
|
+
};
|
|
291
|
+
try {
|
|
292
|
+
const next = iterator.next();
|
|
293
|
+
return (typeof next.then === "function" ? Promise.resolve(next).then(step) : step(next))?.catch(fail);
|
|
263
294
|
} catch (error) {
|
|
264
|
-
|
|
265
|
-
controller.error(error);
|
|
295
|
+
fail(error);
|
|
266
296
|
}
|
|
267
297
|
},
|
|
268
298
|
cancel() {
|
|
269
|
-
|
|
270
|
-
cleanupAbort();
|
|
271
|
-
safeReturn();
|
|
299
|
+
finalize("close");
|
|
272
300
|
}
|
|
273
301
|
}, typedSSE ? { highWaterMark: 0 } : void 0), set);
|
|
274
302
|
};
|
|
@@ -291,8 +319,11 @@ function handleSet(set) {
|
|
|
291
319
|
const cookie = serializeCookie(set.cookie);
|
|
292
320
|
if (cookie) {
|
|
293
321
|
const existing = set.headers[setCookie];
|
|
294
|
-
if (
|
|
295
|
-
|
|
322
|
+
if (existing) {
|
|
323
|
+
const kept = typeof existing === "string" ? [existing] : existing;
|
|
324
|
+
const added = (typeof cookie === "string" ? [cookie] : cookie).filter((value) => !kept.includes(value));
|
|
325
|
+
set.headers[setCookie] = added.length ? kept.concat(added) : existing;
|
|
326
|
+
} else set.headers[setCookie] = cookie;
|
|
296
327
|
}
|
|
297
328
|
}
|
|
298
329
|
if (set.headers[setCookie] && Array.isArray(set.headers[setCookie])) set.headers = parseSetCookies(new Headers(set.headers), set.headers[setCookie]);
|
|
@@ -325,11 +356,6 @@ function mergeHeaders(responseHeaders, setHeaders) {
|
|
|
325
356
|
applySetHeaders(headers, setHeaders, responseHeaders);
|
|
326
357
|
return headers;
|
|
327
358
|
}
|
|
328
|
-
function mergeStatus(responseStatus, setStatus) {
|
|
329
|
-
if (typeof setStatus === "string") setStatus = StatusMap[setStatus];
|
|
330
|
-
if (responseStatus === 200) return setStatus;
|
|
331
|
-
return responseStatus;
|
|
332
|
-
}
|
|
333
359
|
function cancelPropagatingBody(clonedBody, orphanedBranch, preserveOrphan = false) {
|
|
334
360
|
const reader = clonedBody.getReader();
|
|
335
361
|
return new ReadableStream({
|
|
@@ -348,7 +374,6 @@ function cancelPropagatingBody(clonedBody, orphanedBranch, preserveOrphan = fals
|
|
|
348
374
|
}
|
|
349
375
|
});
|
|
350
376
|
}
|
|
351
|
-
const warnedStreamBorrows = /* @__PURE__ */ new WeakSet();
|
|
352
377
|
function createResponseHandler(handler) {
|
|
353
378
|
const handleStream = createStreamHandler(handler);
|
|
354
379
|
return (response, set, request, owned = false) => {
|
|
@@ -360,25 +385,22 @@ function createResponseHandler(handler) {
|
|
|
360
385
|
const borrowed = !owned || explicitlyBorrowed;
|
|
361
386
|
const mustClone = owned && explicitlyBorrowed;
|
|
362
387
|
if (!borrowed && response.bodyUsed) throw new TypeError("Cannot reuse a consumed Response across requests");
|
|
388
|
+
let status;
|
|
363
389
|
if (set) {
|
|
364
|
-
|
|
390
|
+
status = response.status === 200 ? set.status : response.status;
|
|
391
|
+
if (typeof status === "string") status = StatusMap[status];
|
|
365
392
|
const statusUnchanged = status === void 0 || status === response.status;
|
|
366
393
|
if (!mustClone && statusUnchanged && !set.cookie && !isNotEmpty(set.headers)) return response;
|
|
367
394
|
} else if (!mustClone) return response;
|
|
368
395
|
if (!borrowed && response.body?.locked) throw new TypeError("Cannot patch a Response whose body is locked");
|
|
369
396
|
let body = response.body;
|
|
370
397
|
if (borrowed) {
|
|
371
|
-
const bodyBefore = body;
|
|
372
398
|
const cloned = response.clone();
|
|
373
|
-
if (explicitlyBorrowed && bodyBefore && response.body !== bodyBefore && !warnedStreamBorrows.has(response)) {
|
|
374
|
-
warnedStreamBorrows.add(response);
|
|
375
|
-
console.warn("[elysia] borrow() was called on a stream-backed Response");
|
|
376
|
-
}
|
|
377
399
|
body = cloned.body && response.body ? cancelPropagatingBody(cloned.body, response.body, explicitlyBorrowed) : cloned.body;
|
|
378
400
|
}
|
|
379
401
|
const newResponse = new Response(body, set ? {
|
|
380
402
|
headers: mergeHeaders(response.headers, set.headers),
|
|
381
|
-
status
|
|
403
|
+
status,
|
|
382
404
|
statusText: response.statusText
|
|
383
405
|
} : {
|
|
384
406
|
headers: response.headers,
|
|
@@ -402,24 +424,37 @@ const doneResult = {
|
|
|
402
424
|
done: true,
|
|
403
425
|
value: void 0
|
|
404
426
|
};
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
427
|
+
function observeStream(source) {
|
|
428
|
+
if (source.locked || isByteStream(source)) return;
|
|
429
|
+
const reader = source.getReader();
|
|
430
|
+
const [value, observer] = tee({ [Symbol.asyncIterator]: () => ({
|
|
431
|
+
next: () => reader.read(),
|
|
432
|
+
return: (reason) => reader.cancel(reason).then(() => ({
|
|
433
|
+
done: true,
|
|
434
|
+
value: void 0
|
|
435
|
+
}))
|
|
436
|
+
}) });
|
|
437
|
+
const body = new ReadableStream({
|
|
438
|
+
async pull(controller) {
|
|
439
|
+
try {
|
|
440
|
+
const result = await value.next();
|
|
441
|
+
if (result.done) controller.close();
|
|
442
|
+
else controller.enqueue(result.value);
|
|
443
|
+
} catch (error) {
|
|
444
|
+
controller.error(error);
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
cancel(reason) {
|
|
448
|
+
return value.return?.(reason);
|
|
449
|
+
}
|
|
450
|
+
}, { highWaterMark: 0 });
|
|
451
|
+
if (source.sse === true) body.sse = true;
|
|
452
|
+
return [
|
|
453
|
+
body,
|
|
454
|
+
observer,
|
|
455
|
+
value
|
|
456
|
+
];
|
|
457
|
+
}
|
|
423
458
|
function tee(source, branches = 2, cap = 64, capBytes = 1 << 22) {
|
|
424
459
|
const iterator = source[Symbol.asyncIterator]?.() ?? source[Symbol.iterator]();
|
|
425
460
|
const buffer = [];
|
|
@@ -523,7 +558,7 @@ function tee(source, branches = 2, cap = 64, capBytes = 1 << 22) {
|
|
|
523
558
|
wakeAll();
|
|
524
559
|
}
|
|
525
560
|
})();
|
|
526
|
-
const makeBranch = (me) => ({
|
|
561
|
+
const makeBranch = (me) => Object.assign(Object.create(null), {
|
|
527
562
|
[Symbol.asyncIterator]() {
|
|
528
563
|
return this;
|
|
529
564
|
},
|
|
@@ -557,4 +592,4 @@ function tee(source, branches = 2, cap = 64, capBytes = 1 << 22) {
|
|
|
557
592
|
}
|
|
558
593
|
|
|
559
594
|
//#endregion
|
|
560
|
-
export { createResponseHandler, createStreamHandler, handleFile, handleSet, materializeSetHeaders, normalizeContentType, parseSetCookies, responseToSetHeaders, streamResponse, tee };
|
|
595
|
+
export { createResponseHandler, createStreamHandler, handleFile, handleSet, materializeSetHeaders, normalizeContentType, observeStream, parseSetCookies, responseToSetHeaders, streamResponse, tee };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MaybePromise } from "../../types.js";
|
|
2
1
|
import { Context } from "../../context.js";
|
|
2
|
+
import { MaybePromise } from "../../types.js";
|
|
3
3
|
|
|
4
4
|
//#region src/adapter/web-standard/handler.d.ts
|
|
5
5
|
declare function mapResponse(response: unknown, set: Context['set'], request?: Request, owned?: boolean): Response;
|
|
@@ -2,6 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2
2
|
const require_universal_constants = require('../../universal/constants.js');
|
|
3
3
|
const require_universal_file = require('../../universal/file.js');
|
|
4
4
|
const require_utils = require('../../utils.js');
|
|
5
|
+
const require_universal_is_production = require('../../universal/is-production.js');
|
|
5
6
|
const require_error = require('../../error.js');
|
|
6
7
|
const require_adapter_default_headers = require('../default-headers.js');
|
|
7
8
|
const require_adapter_utils = require('../utils.js');
|
|
@@ -10,10 +11,10 @@ const require_cookie_cookie = require('../../cookie/cookie.js');
|
|
|
10
11
|
//#region src/adapter/web-standard/handler.ts
|
|
11
12
|
const textPlainInit = { headers: { "content-type": "text/plain" } };
|
|
12
13
|
function handleElysiaFile(file, set = { headers: require_utils.nullObject() }, request) {
|
|
13
|
-
const
|
|
14
|
-
const contentType = require_universal_file.mime[path.slice(path.lastIndexOf(".") + 1).toLowerCase()];
|
|
14
|
+
const contentType = file.type;
|
|
15
15
|
const headers = require_adapter_utils.materializeSetHeaders(set);
|
|
16
|
-
if (contentType) headers
|
|
16
|
+
if (contentType !== "application/octet-stream") if (headers instanceof Headers) headers.set("content-type", contentType);
|
|
17
|
+
else headers["content-type"] = contentType;
|
|
17
18
|
const stats = file.stats;
|
|
18
19
|
if (stats) return stats.then((stat) => {
|
|
19
20
|
const size = stat.size;
|
|
@@ -23,6 +24,7 @@ function handleElysiaFile(file, set = { headers: require_utils.nullObject() }, r
|
|
|
23
24
|
return require_adapter_utils.handleFile(file.value, set, request);
|
|
24
25
|
}
|
|
25
26
|
function responseTag(response) {
|
|
27
|
+
if (typeof response === "string") return "String";
|
|
26
28
|
if (response == null) return;
|
|
27
29
|
const constructor = Object.getPrototypeOf(response)?.constructor;
|
|
28
30
|
if (typeof constructor !== "function") return typeof response.next === "function" ? "" : void 0;
|
|
@@ -31,6 +33,7 @@ function responseTag(response) {
|
|
|
31
33
|
function mapResponseWithSet(response, set, request, owned) {
|
|
32
34
|
require_adapter_utils.handleSet(set);
|
|
33
35
|
const headers = set.headers;
|
|
36
|
+
if (!require_universal_constants.isBun && (set.status === 204 || set.status === 304 || set.status === 205) && !(response instanceof Response) && !(response instanceof require_error.ElysiaStatus)) return new Response(null, set);
|
|
34
37
|
switch (responseTag(response)) {
|
|
35
38
|
case "String":
|
|
36
39
|
if (!require_universal_constants.isBun && !headers["content-type"]) require_adapter_utils.materializeSetHeaders(set)["content-type"] = "text/plain";
|
|
@@ -41,14 +44,11 @@ function mapResponseWithSet(response, set, request, owned) {
|
|
|
41
44
|
if (typeof response?.next === "function") return handleStream(response, set, request, void 0, owned);
|
|
42
45
|
return Response.json(response, set);
|
|
43
46
|
case "Number":
|
|
44
|
-
case "Boolean": return new Response(
|
|
47
|
+
case "Boolean": return new Response(String(response), set);
|
|
45
48
|
case "ElysiaFile": return handleElysiaFile(response, set, request);
|
|
46
49
|
case "File":
|
|
47
50
|
case "Blob": return require_adapter_utils.handleFile(response, set, request);
|
|
48
|
-
case "ElysiaStatus":
|
|
49
|
-
set.status = response.code;
|
|
50
|
-
if (response.headers) Object.assign(require_adapter_utils.materializeSetHeaders(set), response.headers);
|
|
51
|
-
return mapResponseWithSet(response.response, set, request, owned);
|
|
51
|
+
case "ElysiaStatus": return withStatus(response, set, request, owned);
|
|
52
52
|
case void 0: return response ? Response.json(response, set) : new Response(null, set);
|
|
53
53
|
case "Response": return handleResponse(response, set, request, owned);
|
|
54
54
|
case "Error": return errorToResponse(response, set, request, owned);
|
|
@@ -58,14 +58,26 @@ function mapResponseWithSet(response, set, request, owned) {
|
|
|
58
58
|
default: return mapFallback(response, set, request, owned);
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
+
function withStatus(response, set, request, owned) {
|
|
62
|
+
if (set) set.status = response.status;
|
|
63
|
+
else set = {
|
|
64
|
+
status: response.status,
|
|
65
|
+
headers: require_utils.nullObject()
|
|
66
|
+
};
|
|
67
|
+
if (response.headers) if (set.headers instanceof Headers) for (const [key, value] of Object.entries(response.headers)) if (key.toLowerCase() === "set-cookie") for (const cookie of Array.isArray(value) ? value : [value]) set.headers.append(key, cookie);
|
|
68
|
+
else set.headers.set(key, value);
|
|
69
|
+
else Object.assign(require_adapter_utils.materializeSetHeaders(set), response.headers);
|
|
70
|
+
return mapResponseWithSet(response.response, set, request, owned);
|
|
71
|
+
}
|
|
72
|
+
const checkRemovedSetRedirect = !require_universal_is_production.isProduction();
|
|
61
73
|
function mapResponse(response, set, request, owned) {
|
|
74
|
+
if (checkRemovedSetRedirect && Object.hasOwn(set, "redirect") && !require_universal_is_production.isProduction()) {
|
|
75
|
+
delete set.redirect;
|
|
76
|
+
throw new Error("[Elysia] set.redirect was removed in 2.0 — return redirect(url) instead");
|
|
77
|
+
}
|
|
62
78
|
const headers = set.headers;
|
|
63
79
|
if (set.status !== void 0 || set.cookie || headers["\0"] === headers || require_utils.isNotEmpty(headers)) return mapResponseWithSet(response, set, request, owned);
|
|
64
|
-
if (response instanceof require_error.ElysiaStatus)
|
|
65
|
-
set.status = response.code;
|
|
66
|
-
if (response.headers) Object.assign(set.headers, response.headers);
|
|
67
|
-
return mapResponse(response.response, set, request, owned);
|
|
68
|
-
}
|
|
80
|
+
if (response instanceof require_error.ElysiaStatus) return withStatus(response, set, request, owned);
|
|
69
81
|
if (response instanceof Response) return owned ? handleResponse(response, void 0, request, true) : response;
|
|
70
82
|
if (response instanceof Promise) return response.then((x) => mapResponse(x, set, request, owned));
|
|
71
83
|
if (typeof response?.next === "function" || response instanceof ReadableStream) return handleStream(response, set, request, void 0, owned);
|
|
@@ -77,16 +89,15 @@ function mapCompactResponse(response, request, owned) {
|
|
|
77
89
|
case "String": return new Response(response, textHeaders);
|
|
78
90
|
case "Array": return Response.json(response);
|
|
79
91
|
case "ElysiaForm": return new Response(require_utils.formToFormData(response));
|
|
80
|
-
case "Object":
|
|
92
|
+
case "Object":
|
|
93
|
+
if (typeof response?.next === "function") return handleStream(response, void 0, request, void 0, owned);
|
|
94
|
+
return Response.json(response);
|
|
81
95
|
case "Number":
|
|
82
|
-
case "Boolean": return new Response(
|
|
96
|
+
case "Boolean": return new Response(String(response));
|
|
83
97
|
case "ElysiaFile": return handleElysiaFile(response, void 0, request);
|
|
84
98
|
case "File":
|
|
85
99
|
case "Blob": return require_adapter_utils.handleFile(response, void 0, request);
|
|
86
|
-
case "ElysiaStatus": return
|
|
87
|
-
status: response.code,
|
|
88
|
-
headers: response.headers ? Object.assign(require_utils.nullObject(), response.headers) : require_utils.nullObject()
|
|
89
|
-
}, request, owned);
|
|
100
|
+
case "ElysiaStatus": return withStatus(response, void 0, request, owned);
|
|
90
101
|
case void 0: return response ? Response.json(response) : new Response("");
|
|
91
102
|
case "Response": return owned ? handleResponse(response, void 0, request, true) : response;
|
|
92
103
|
case "Error": return errorToResponse(response, void 0, request, owned);
|
|
@@ -118,28 +129,20 @@ function errorToResponse(error, set, request, owned = false) {
|
|
|
118
129
|
}
|
|
119
130
|
function mapFallback(response, set, request, owned = false) {
|
|
120
131
|
if (response instanceof Response) return handleResponse(response, set, request, owned);
|
|
121
|
-
if (response instanceof Promise) return response.then((x) => set ? mapResponse(x, set, request, owned) : mapCompactResponse(x, request, owned));
|
|
122
132
|
if (response instanceof Error) return errorToResponse(response, set, request, owned);
|
|
123
|
-
if (response instanceof require_error.ElysiaStatus)
|
|
124
|
-
|
|
125
|
-
if (response.headers) set.headers = {
|
|
126
|
-
...set.headers,
|
|
127
|
-
...response.headers
|
|
128
|
-
};
|
|
129
|
-
return mapResponse(response.response, set, request, owned);
|
|
130
|
-
} else return mapResponse(response.response, {
|
|
131
|
-
status: response.code,
|
|
132
|
-
headers: response.headers ? { ...response.headers } : {}
|
|
133
|
-
}, request, owned);
|
|
134
|
-
if (response instanceof require_universal_file.ElysiaFile) return handleElysiaFile(response, void 0, request);
|
|
133
|
+
if (response instanceof require_error.ElysiaStatus) return withStatus(response, set, request, owned);
|
|
134
|
+
if (response instanceof require_universal_file.ElysiaFile) return handleElysiaFile(response, set, request);
|
|
135
135
|
if (require_utils.isElysiaForm(response)) return new Response(require_utils.formToFormData(response), set);
|
|
136
136
|
if (typeof response?.next === "function" || response instanceof ReadableStream) return handleStream(response, set, request, void 0, owned);
|
|
137
|
-
if (typeof response?.then === "function") return response.then((x) =>
|
|
138
|
-
if (Array.isArray(response)) return Response.json(response);
|
|
139
|
-
if (typeof response?.toResponse === "function") return
|
|
140
|
-
if (responseTag(response) === "Cookie" && require_cookie_cookie.Cookie.isCookie(response)) return
|
|
137
|
+
if (typeof response?.then === "function") return response.then((x) => remap(x, set, request, owned));
|
|
138
|
+
if (Array.isArray(response)) return Response.json(response, set);
|
|
139
|
+
if (typeof response?.toResponse === "function") return remap(response.toResponse(), set, request, owned);
|
|
140
|
+
if (responseTag(response) === "Cookie" && require_cookie_cookie.Cookie.isCookie(response)) return remap(response.value, set, request, owned);
|
|
141
141
|
return new Response(response, set);
|
|
142
142
|
}
|
|
143
|
+
function remap(response, set, request, owned) {
|
|
144
|
+
return set ? mapResponse(response, set, request, owned) : mapCompactResponse(response, request, owned);
|
|
145
|
+
}
|
|
143
146
|
const handleResponse = require_adapter_utils.createResponseHandler({
|
|
144
147
|
mapResponse,
|
|
145
148
|
mapCompactResponse
|