Open History to see the last commit
1/**
2 * Accessors for the Node.js objects backing a platform Node
3 * `HttpServerRequest`.
4 *
5 * Use this module at interop boundaries when an Effect HTTP handler needs the
6 * original `http.IncomingMessage` or `http.ServerResponse` for APIs that are
7 * specific to Node, such as existing middleware, socket inspection, raw stream
8 * piping, or response customization that cannot be expressed with the portable
9 * `HttpServerRequest` and `HttpServerResponse` interfaces.
10 *
11 * The returned request is the original Node request supplied to the server. It
12 * does not reflect Effect request overrides made by middleware, such as a
13 * rewritten URL, adjusted headers, or a substituted remote address. Its body is
14 * also Node's one-shot readable stream, so avoid mixing raw stream consumption
15 * with Effect body, multipart, or stream helpers unless ownership of the body
16 * is clear. The returned response is the Node response owned by the platform
17 * server; writing to it directly bypasses the usual Effect response writer and
18 * must be coordinated carefully to avoid duplicate writes. Upgrade requests may
19 * create that response lazily when it is first requested.
20 *
21 * @since 4.0.0
22 */
23import type { HttpServerRequest } from "effect/unstable/http/HttpServerRequest"
24import type * as Http from "node:http"
25
26/**
27 * Returns the underlying Node `IncomingMessage` for a platform Node
28 * `HttpServerRequest`.
29 *
30 * @category accessors
31 * @since 4.0.0
32 */
33export const toIncomingMessage = (self: HttpServerRequest): Http.IncomingMessage => self.source as any
34
35/**
36 * Returns the underlying Node `ServerResponse` for a platform Node
37 * `HttpServerRequest`, evaluating the stored response thunk when the response
38 * was created lazily.
39 *
40 * @category accessors
41 * @since 4.0.0
42 */
43export const toServerResponse = (self: HttpServerRequest): Http.ServerResponse => {
44 const res = (self as any).response
45 return typeof res === "function" ? res() : res
46}