| 1556 | * @since 4.0.0 |
| 1557 | */ |
| 1558 | export const fromWeb = ( |
| 1559 | response: Response, |
| 1560 | options?: { |
| 1561 | readonly raw?: boolean | undefined |
| 1562 | } | undefined |
| 1563 | ): HttpServerResponse => { |
| 1564 | const headers = new globalThis.Headers(response.headers) |
| 1565 | const setCookieHeaders = headers.getSetCookie() |
| 1566 | headers.delete("set-cookie") |
| 1567 | let self = empty({ |
| 1568 | status: response.status, |
| 1569 | statusText: response.statusText, |
| 1570 | headers: headers as any, |
| 1571 | cookies: Cookies.fromSetCookie(setCookieHeaders) |
| 1572 | }) |
| 1573 | const body = response.body |
| 1574 | if (body) { |
| 1575 | const contentType = response.headers.get("content-type") ?? undefined |
| 1576 | const contentLength = bodyInternal.parseContentLength(response.headers.get("content-length")) |
| 1577 | self = setBody( |
| 1578 | self, |
| 1579 | options?.raw ? |
| 1580 | Body.raw(body, { contentType, contentLength }) : |
| 1581 | Body.stream( |
| 1582 | Stream.fromReadableStream({ |
| 1583 | // Read lazily: `Response.clone` replaces `response.body`. |
| 1584 | evaluate: () => response.body!, |
| 1585 | onError: (e) => e |
| 1586 | }), |
| 1587 | contentType, |
| 1588 | contentLength |
| 1589 | ) |
| 1590 | ) |
| 1591 | } |
| 1592 | return self |
| 1593 | } |