| 601 | |
| 602 | /** @internal */ |
| 603 | export const toWeb = (response: ServerResponse.HttpServerResponse, options?: { |
| 604 | readonly withoutBody?: boolean | undefined |
| 605 | readonly runtime?: Runtime.Runtime<never> | undefined |
| 606 | }): Response => { |
| 607 | const headers = new globalThis.Headers(response.headers) |
| 608 | if (!Cookies.isEmpty(response.cookies)) { |
| 609 | const toAdd = Cookies.toSetCookieHeaders(response.cookies) |
| 610 | for (const header of toAdd) { |
| 611 | headers.append("set-cookie", header) |
| 612 | } |
| 613 | } |
| 614 | if (options?.withoutBody) { |
| 615 | return new Response(undefined, { |
| 616 | status: response.status, |
| 617 | statusText: response.statusText as string, |
| 618 | headers |
| 619 | }) |
| 620 | } |
| 621 | const body = response.body |
| 622 | switch (body._tag) { |
| 623 | case "Empty": { |
| 624 | return new Response(undefined, { |
| 625 | status: response.status, |
| 626 | statusText: response.statusText as string, |
| 627 | headers |
| 628 | }) |
| 629 | } |
| 630 | case "Uint8Array": |
| 631 | case "Raw": { |
| 632 | if (body.body instanceof Response) { |
| 633 | for (const [key, value] of headers as any) { |
| 634 | body.body.headers.set(key, value) |
| 635 | } |
| 636 | return body.body |
| 637 | } |
| 638 | return new Response(body.body as any, { |
| 639 | status: response.status, |
| 640 | statusText: response.statusText, |
| 641 | headers |
| 642 | }) |
| 643 | } |
| 644 | case "FormData": { |
| 645 | return new Response(body.formData as any, { |
| 646 | status: response.status, |
| 647 | statusText: response.statusText, |
| 648 | headers |
| 649 | }) |
| 650 | } |
| 651 | case "Stream": { |
| 652 | return new Response(Stream.toReadableStreamRuntime(body.stream, options?.runtime ?? Runtime.defaultRuntime), { |
| 653 | status: response.status, |
| 654 | statusText: response.statusText, |
| 655 | headers |
| 656 | }) |
| 657 | } |
| 658 | } |
| 659 | } |