| 983 | * @since 4.0.0 |
| 984 | */ |
| 985 | export const toWeb = ( |
| 986 | response: HttpServerResponse, |
| 987 | options?: { |
| 988 | readonly withoutBody?: boolean | undefined |
| 989 | readonly context?: Context.Context<never> | undefined |
| 990 | } |
| 991 | ): Response => { |
| 992 | const headers = new globalThis.Headers(response.headers) |
| 993 | if (!Cookies.isEmpty(response.cookies)) { |
| 994 | const toAdd = Cookies.toSetCookieHeaders(response.cookies) |
| 995 | for (const header of toAdd) { |
| 996 | headers.append("set-cookie", header) |
| 997 | } |
| 998 | } |
| 999 | if (options?.withoutBody) { |
| 1000 | return new Response(undefined, { |
| 1001 | status: response.status, |
| 1002 | statusText: response.statusText as string, |
| 1003 | headers |
| 1004 | }) |
| 1005 | } |
| 1006 | const body = response.body |
| 1007 | switch (body._tag) { |
| 1008 | case "Empty": { |
| 1009 | return new Response(undefined, { |
| 1010 | status: response.status, |
| 1011 | statusText: response.statusText as string, |
| 1012 | headers |
| 1013 | }) |
| 1014 | } |
| 1015 | case "Uint8Array": |
| 1016 | case "Raw": { |
| 1017 | if (body.body instanceof Response) { |
| 1018 | for (const [key, value] of headers as any) { |
| 1019 | body.body.headers.set(key, value) |
| 1020 | } |
| 1021 | return body.body |
| 1022 | } |
| 1023 | return new Response(body.body as any, { |
| 1024 | status: response.status, |
| 1025 | statusText: response.statusText!, |
| 1026 | headers |
| 1027 | }) |
| 1028 | } |
| 1029 | case "FormData": { |
| 1030 | return new Response(body.formData as any, { |
| 1031 | status: response.status, |
| 1032 | statusText: response.statusText!, |
| 1033 | headers |
| 1034 | }) |
| 1035 | } |
| 1036 | case "Stream": { |
| 1037 | return new Response( |
| 1038 | Stream.toReadableStreamWith( |
| 1039 | body.stream, |
| 1040 | options?.context ?? Context.empty() |
| 1041 | ), |
| 1042 | { |