@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 1,232 lines 43.6 KB
Raw
1 /**
2 * Node.js implementation of the Effect `HttpServer`.
3 *
4 * This module adapts a supplied Node `http.Server` or `node:http2` server into
5 * Effect's platform-independent HTTP server service. It starts the server with
6 * Node `listen` options, converts `request` events into `HttpServerRequest`
7 * values, writes `HttpServerResponse` bodies through Node's `ServerResponse`,
8 * and handles `upgrade` events by exposing the upgraded socket through
9 * `HttpServerRequest.upgrade`. It also exports request and upgrade handler
10 * constructors plus layers for the server alone, HTTP support services, the
11 * combined server, configurable options, and tests.
12 *
13 * **HTTP/2**
14 *
15 * Pass `Http2.createServer()` for cleartext HTTP/2 (h2c), or
16 * `Http2.createSecureServer({ key, cert, allowHTTP1: true })` to negotiate
17 * HTTP/2 over TLS and fall back to HTTP/1.1 for clients that do not support
18 * it. The protocol is detected per request, so the application sees the same
19 * `HttpServerRequest` either way:
20 *
21 * - pseudo-headers are removed from `headers`, and `host` is taken from
22 * `:authority`
23 * - `originalUrl` and `HttpServerRequest.toURL` use the `:scheme` the client
24 * sent, which a client can set as freely as `x-forwarded-proto`
25 * - connection-specific response headers that HTTP/2 forbids, such as
26 * `connection`, `keep-alive` and `transfer-encoding`, are dropped silently
27 * - a response that fails after its headers were sent resets the stream with
28 * `INTERNAL_ERROR`, so clients do not mistake a truncated body for a
29 * complete one
30 *
31 * WebSockets and other upgrades only work over HTTP/1.1, which needs
32 * `allowHTTP1` on a TLS server. Trailers are only available through the Node
33 * objects from `NodeHttpServerRequest`. Tune HTTP/2 limits, such as
34 * `maxSessionMemory` and `settings.maxConcurrentStreams`, through the Node
35 * server options.
36 *
37 * @stability unstable
38 * @since 4.0.0
39 */
40 import * as Cause from "effect/Cause"
41 import * as Config from "effect/Config"
42 import * as Context from "effect/Context"
43 import * as Duration from "effect/Duration"
44 import * as Effect from "effect/Effect"
45 import * as Exit from "effect/Exit"
46 import * as Fiber from "effect/Fiber"
47 import type * as FileSystem from "effect/FileSystem"
48 import { flow, type LazyArg } from "effect/Function"
49 import * as Cookies from "effect/http/Cookies"
50 import * as Etag from "effect/http/Etag"
51 import * as FetchHttpClient from "effect/http/FetchHttpClient"
52 import * as Headers from "effect/http/Headers"
53 import type { HttpClient } from "effect/http/HttpClient"
54 import * as HttpEffect from "effect/http/HttpEffect"
55 import * as HttpIncomingMessage from "effect/http/HttpIncomingMessage"
56 import type { HttpMethod } from "effect/http/HttpMethod"
57 import type * as Middleware from "effect/http/HttpMiddleware"
58 import type * as HttpPlatform from "effect/http/HttpPlatform"
59 import * as HttpServer from "effect/http/HttpServer"
60 import {
61 causeResponse,
62 ClientAbort,
63 HttpServerError,
64 InternalError,
65 RequestParseError,
66 ResponseError,
67 ServeError
68 } from "effect/http/HttpServerError"
69 import * as Request from "effect/http/HttpServerRequest"
70 import { HttpServerRequest } from "effect/http/HttpServerRequest"
71 import type { HttpServerResponse } from "effect/http/HttpServerResponse"
72 import * as Response from "effect/http/HttpServerResponse"
73 import type * as Multipart from "effect/http/Multipart"
74 import * as UrlParams from "effect/http/UrlParams"
75 import * as Latch from "effect/Latch"
76 import * as Layer from "effect/Layer"
77 import * as NetAddress from "effect/net/NetAddress"
78 import type * as Option from "effect/Option"
79 import type * as Path from "effect/Path"
80 import type * as Record from "effect/Record"
81 import * as Scope from "effect/Scope"
82 import * as Socket from "effect/socket/Socket"
83 import * as Stream from "effect/Stream"
84 import type * as Http from "node:http"
85 import type * as Http2 from "node:http2"
86 import type * as Net from "node:net"
87 import type { Duplex } from "node:stream"
88 import { Readable, Transform } from "node:stream"
89 import { pipeline } from "node:stream/promises"
90 import type * as Tls from "node:tls"
91 import { http } from "./internal/http.ts"
92 import * as Upgrade from "./internal/upgrade.ts"
93 import { NodeHttpIncomingMessage } from "./NodeHttpIncomingMessage.ts"
94 import * as NodeHttpPlatform from "./NodeHttpPlatform.ts"
95 import * as NodeMultipart from "./NodeMultipart.ts"
96 import * as NodeServices from "./NodeServices.ts"
97 import * as NodeStream from "./NodeStream.ts"
98 import * as NodeWS from "./NodeWS.ts"
99
100 /**
101 * Options accepted by the Node `HttpServer` constructors and layers.
102 *
103 * @stability unstable
104 * @category options
105 * @since 4.0.0
106 */
107 export interface Options extends Net.ListenOptions {
108 readonly disablePreemptiveShutdown?: boolean | undefined
109 readonly gracefulShutdownTimeout?: Duration.Input | undefined
110 /**
111 * Options forwarded to the underlying `ws` `WebSocketServer`, minus the
112 * wiring options the server manages itself. Use this to enable
113 * `permessage-deflate` compression or tune payload limits, e.g.
114 * `websocket: { perMessageDeflate: true }`.
115 *
116 * @stability unstable
117 */
118 readonly websocket?:
119 | Omit<NodeWS.ServerOptions, "noServer" | "server" | "host" | "port" | "path">
120 | undefined
121 }
122
123 /**
124 * Creates a scoped `HttpServer` from a Node `http.Server` or `node:http2`
125 * server, starts listening with the supplied options, registers request and
126 * upgrade handling, and closes the server during scope finalization with
127 * optional graceful-shutdown control.
128 *
129 * **Details**
130 *
131 * Node's own timeouts are set on the server you create, for example
132 * `createServer({ requestTimeout: 0, headersTimeout: 60_000 })`:
133 *
134 * - `requestTimeout` (default 300 seconds) bounds receiving the whole request,
135 * headers and body. On expiry Node answers `408`, or closes the connection
136 * if the response has started. It is checked every
137 * `connectionsCheckingInterval` (default 30 seconds). `0` disables it.
138 * - `headersTimeout` (default 60 seconds) bounds receiving the headers. Left
139 * unset, it is lowered to a smaller `requestTimeout`, so `requestTimeout: 0`
140 * alone disables both. When both are nonzero it may not exceed
141 * `requestTimeout`.
142 * - `keepAliveTimeout` (default 5 seconds) closes connections idle between
143 * requests.
144 * - `server.timeout`, socket inactivity, is off by default, so a quiet
145 * response stream is never cut once the request is received.
146 *
147 * Use `Stream` and `Effect` timeouts in handlers or middleware for per-route
148 * limits on reading the body or producing the response.
149 *
150 * **Gotchas**
151 *
152 * A request counts as received once Node has parsed the whole body, so a
153 * handler that streams a response without reading a large body is still under
154 * `requestTimeout` and is cut when it expires.
155 *
156 * @stability unstable
157 * @category constructors
158 * @since 4.0.0
159 */
160 export const make = Effect.fnUntraced(function*(
161 evaluate: LazyArg<Http.Server | Http2.Http2Server | Http2.Http2SecureServer>,
162 options: Options
163 ) {
164 const scope = yield* Effect.scope
165 // The members used here exist on every server kind, with listener types that differ per kind.
166 const server = evaluate() as Http.Server
167
168 // Node 22 `close` waits for idle HTTP/2 sessions, and no version closes a session whose TLS
169 // handshake finishes after `close`. Closing them still lets open streams finish.
170 let closing = false
171 const sessions = new Set<Http2.ServerHttp2Session>()
172 server.on("session", (session: Http2.ServerHttp2Session) => {
173 if (closing) {
174 return session.close()
175 }
176 sessions.add(session)
177 session.once("close", () => sessions.delete(session))
178 })
179
180 const shutdown = yield* Effect.callback<void>((resume) => {
181 if (!server.listening) {
182 return resume(Effect.void)
183 }
184 server.close((error) => {
185 if (error) {
186 resume(Effect.die(error))
187 } else {
188 resume(Effect.void)
189 }
190 })
191 closing = true
192 for (const session of sessions) {
193 session.close()
194 }
195 }).pipe(Effect.cached)
196
197 const preemptiveShutdown = options.disablePreemptiveShutdown ?
198 Effect.void :
199 Effect.timeoutOrElse(shutdown, {
200 duration: options.gracefulShutdownTimeout ?? Duration.seconds(20),
201 orElse: () => Effect.void
202 })
203
204 yield* Scope.addFinalizer(scope, shutdown)
205
206 yield* Effect.callback<void, ServeError>((resume) => {
207 function onError(cause: Error) {
208 resume(Effect.fail(new ServeError({ cause })))
209 }
210 server.on("error", onError)
211 server.listen(options, () => {
212 server.off("error", onError)
213 resume(Effect.void)
214 })
215 })
216
217 const address = server.address()!
218 const effectAddress = typeof address === "string"
219 ? Effect.succeed(NetAddress.unixPathAddress(address))
220 : Effect.fromResult(NetAddress.inetAddressFromIpString(address.address, address.port)).pipe(
221 Effect.mapError((cause) => new ServeError({ cause }))
222 )
223 const boundAddress = yield* effectAddress
224
225 const wss = yield* Effect.acquireRelease(
226 Effect.sync(() => new NodeWS.WebSocketServer({ ...options.websocket, noServer: true })),
227 (wss) =>
228 Effect.callback<void>((resume) => {
229 wss.close(() => resume(Effect.void))
230 })
231 ).pipe(
232 Scope.provide(scope),
233 Effect.cached
234 )
235
236 return HttpServer.make({
237 address: boundAddress,
238 serve: Effect.fnUntraced(function*(httpApp, middleware) {
239 const serveScope = yield* Effect.scope
240 const scope = Scope.forkUnsafe(serveScope, "parallel")
241 const handler = yield* (makeHandler(httpApp, {
242 middleware: middleware as any,
243 scope
244 }) as Effect.Effect<(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void>)
245 const upgradeHandler = yield* makeUpgradeHandler(wss, httpApp, {
246 middleware: middleware as any,
247 scope
248 })
249 const checkContinueHandler = (request: Http.IncomingMessage, response: Http.ServerResponse) => {
250 pendingContinue.add(request)
251 handler(request, response)
252 }
253 yield* Scope.addFinalizerExit(serveScope, () =>
254 Effect.ensuring(
255 preemptiveShutdown,
256 Effect.sync(() => {
257 server.off("request", handler)
258 server.off("checkContinue", checkContinueHandler)
259 server.off("upgrade", upgradeHandler)
260 })
261 ))
262 server.on("request", handler)
263 server.on("checkContinue", checkContinueHandler)
264 server.on("upgrade", upgradeHandler)
265 })
266 })
267 })
268
269 /**
270 * Creates a Node `request` event handler for an Effect HTTP application,
271 * injecting a `HttpServerRequest` and interrupting the request fiber if the
272 * client closes the response before it finishes. It accepts both HTTP/1.1 and
273 * HTTP/2 compatibility requests.
274 *
275 * @stability unstable
276 * @category handlers
277 * @since 4.0.0
278 */
279 export const makeHandler = <
280 R,
281 E,
282 App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<HttpServerResponse, E, R>
283 >(
284 httpEffect: Effect.Effect<HttpServerResponse, E, R>,
285 options: {
286 readonly scope: Scope.Scope
287 readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
288 }
289 ): Effect.Effect<
290 (
291 nodeRequest: Http.IncomingMessage | Http2.Http2ServerRequest,
292 nodeResponse: Http.ServerResponse | Http2.Http2ServerResponse
293 ) => void,
294 never,
295 Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
296 > => {
297 const handled = HttpEffect.toHandled(httpEffect, handleResponse, options.middleware as any)
298 return Effect.withFiber((parent) => {
299 const services = parent.context
300 return Effect.succeed(function handler(
301 request: Http.IncomingMessage | Http2.Http2ServerRequest,
302 response: Http.ServerResponse | Http2.Http2ServerResponse
303 ) {
304 // The HTTP/2 compatibility API mirrors the members the adapter uses.
305 const nodeRequest = request as Http.IncomingMessage
306 const nodeResponse = response as Http.ServerResponse
307 if (nodeRequest.httpVersionMajor === 1) {
308 // Prepended so it runs before Node's own `finish` listener drains or destroys.
309 nodeResponse.prependListener("finish", () => teardownBody(nodeRequest, nodeResponse))
310 }
311 const context = Context.add(services, HttpServerRequest, new ServerRequestImpl(nodeRequest, nodeResponse))
312 const fiber = Fiber.runIn(Effect.runForkWith(context as Context.Context<any>)(handled), options.scope)
313 if (fiber.pollUnsafe() === undefined) {
314 nodeResponse.on("close", () => {
315 if (!nodeResponse.writableEnded) {
316 fiber.interruptUnsafe(parent.id, ClientAbort.annotation)
317 }
318 })
319 }
320 })
321 })
322 }
323
324 /**
325 * Creates a Node `upgrade` event handler for an Effect HTTP application,
326 * exposing the upgraded connection as the request's `upgrade` effect and
327 * interrupting the request fiber when the socket closes early.
328 *
329 * **Details**
330 *
331 * `Upgrade: websocket` requests complete the WebSocket handshake. Any other
332 * protocol answers `101 Switching Protocols` with the requested protocol and
333 * the headers given to `upgradeWith`, and exposes the raw connection, with
334 * bytes the client sent after the request body first on the reader. Either
335 * response is sent when the socket's reader is acquired. A handler that never
336 * asks for `upgrade` answers with its response as usual.
337 *
338 * The request body can be read before the upgrade. The `101` waits for the
339 * body to end, discarding any unread rest up to the maximum body size. Node 25
340 * and older do not parse the body of an upgrade request, so it is read from
341 * the connection with `Content-Length`; chunked bodies get
342 * `411 Length Required` there. `Expect: 100-continue` is not answered on
343 * upgrade requests.
344 *
345 * @stability unstable
346 * @category handlers
347 * @since 4.0.0
348 */
349 export const makeUpgradeHandler = <
350 R,
351 E,
352 App extends Effect.Effect<HttpServerResponse, any, any> = Effect.Effect<HttpServerResponse, E, R>
353 >(
354 lazyWss: Effect.Effect<NodeWS.WebSocketServer>,
355 httpEffect: Effect.Effect<HttpServerResponse, E, R>,
356 options: {
357 readonly scope: Scope.Scope
358 readonly middleware?: Middleware.HttpMiddleware.Applied<App, E, R> | undefined
359 }
360 ): Effect.Effect<
361 (nodeRequest: Http.IncomingMessage, socket: Duplex, head: Buffer) => void,
362 never,
363 Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
364 > => {
365 const handledApp = HttpEffect.toHandled(httpEffect, handleResponse, options.middleware as any)
366 return Effect.withFiber((parent) => {
367 const services = parent.context
368 return Effect.succeed(function handler(
369 nodeRequest: Http.IncomingMessage,
370 socket: Duplex,
371 head: Buffer
372 ) {
373 const protocol = nodeRequest.headers.upgrade
374 const isWebSocket = protocol === undefined || protocol.toLowerCase() === "websocket"
375 let body: Readable | undefined
376 if (!isWebSocket && legacyUpgradeBody) {
377 if (nodeRequest.headers["transfer-encoding"] !== undefined) {
378 socket.on("error", () => {})
379 socket.end("HTTP/1.1 411 Length Required\r\nConnection: close\r\nContent-Length: 0\r\n\r\n")
380 return
381 }
382 const framed = Upgrade.frameBody(socket, head, Number(nodeRequest.headers["content-length"] ?? 0))
383 body = framed.body
384 head = framed.rest
385 }
386 let nodeResponse_: Http.ServerResponse | undefined = undefined
387 const nodeResponse = () => {
388 if (nodeResponse_ === undefined) {
389 nodeResponse_ = new http.ServerResponse(nodeRequest)
390 if (state.response !== undefined || socket.destroyed) {
391 // End without assigning the socket so handleResponse skips HTTP writes.
392 nodeResponse_.end()
393 } else {
394 nodeResponse_.assignSocket(socket as any)
395 nodeResponse_.on("finish", () => {
396 socket.end()
397 })
398 }
399 }
400 return nodeResponse_
401 }
402 const webSocketUpgrade = Socket.fromWebSocket(Effect.flatMap(
403 lazyWss,
404 (wss) =>
405 Effect.acquireRelease(
406 Effect.callback<NodeWS.WebSocket, Socket.SocketError>((resume) => {
407 // A refused handshake never invokes the callback, so fail on close instead.
408 const onClose = () =>
409 resume(Effect.fail(
410 new Socket.SocketError({
411 reason: new Socket.SocketOpenError({
412 kind: "Unknown",
413 cause: new Error("The socket closed before the upgrade")
414 })
415 })
416 ))
417 if (socket.destroyed) return onClose()
418 socket.once("close", onClose)
419 wss.handleUpgrade(nodeRequest, socket, head, (ws) => {
420 socket.off("close", onClose)
421 state.response = upgradedResponse
422 resume(Effect.succeed(ws))
423 })
424 }),
425 (ws, exit) => Effect.sync(() => ws.close(closeCode(exit)))
426 )
427 ))
428 const state: UpgradeState = {
429 body,
430 response: undefined,
431 upgradeWith(request, headers) {
432 if (isWebSocket) {
433 return Object.keys(headers).length === 0
434 ? webSocketUpgrade
435 : Effect.fail(upgradeError(request, "WebSocket upgrades do not support custom response headers"))
436 }
437 const invalid = validateUpgradeHeaders(request, headers)
438 if (invalid !== undefined) return Effect.fail(invalid)
439 return Upgrade.toSocket(
440 socket,
441 Effect.withFiber((fiber) => {
442 const maxBytes = fiber.getRef(HttpIncomingMessage.MaxBodySize)
443 return Upgrade.drainBody(body ?? nodeRequest, maxBytes === undefined ? undefined : Number(maxBytes))
444 }).pipe(Effect.andThen(Effect.sync(() => {
445 if (state.response !== undefined) return
446 state.response = Response.empty({
447 status: 101,
448 headers: { ...headers, connection: "Upgrade", upgrade: protocol! }
449 })
450 if (head.length > 0) socket.unshift(head)
451 // Node rejects CR/LF in header values, so protocol cannot inject response headers.
452 let responseHead = `HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: ${protocol}\r\n`
453 for (const name in headers) responseHead += `${name}: ${headers[name]}\r\n`
454 // Node writes header values as Latin-1, which is what peers decode them as.
455 socket.write(responseHead + "\r\n", "latin1")
456 })))
457 )
458 }
459 }
460 const request = new ServerRequestImpl(nodeRequest, nodeResponse, state)
461 const context = Context.add(services, HttpServerRequest, request)
462 const fiber = Fiber.runIn(Effect.runForkWith(context as Context.Context<any>)(handledApp), options.scope)
463 socket.on("error", () => {})
464 socket.on("close", () => {
465 if (!socket.writableEnded) {
466 fiber.interruptUnsafe(parent.id, ClientAbort.annotation)
467 }
468 })
469 })
470 })
471 }
472
473 // Node 26 parses the body of an upgrade request (nodejs/node#60016).
474 const legacyUpgradeBody = Number(process.versions.node.split(".")[0]) < 26
475
476 // Shared by an upgrade request and the views `modify` creates.
477 interface UpgradeState {
478 readonly upgradeWith: (
479 request: HttpServerRequest,
480 headers: Headers.Headers
481 ) => Effect.Effect<Socket.Socket, HttpServerError>
482 // The body read from the connection on Node 25 and older.
483 readonly body: Readable | undefined
484 // The response reported to middleware once the connection is upgraded.
485 response: HttpServerResponse | undefined
486 }
487
488 // The transport sets these, and a body cannot follow the `101`.
489 const reservedUpgradeHeaders = new Set(["connection", "upgrade", "content-length", "transfer-encoding"])
490
491 const validateUpgradeHeaders = (
492 request: HttpServerRequest,
493 headers: Headers.Headers
494 ): HttpServerError | undefined => {
495 for (const name in headers) {
496 if (reservedUpgradeHeaders.has(name)) {
497 return upgradeError(request, `Upgrade responses cannot set the ${name} header`)
498 }
499 try {
500 http.validateHeaderName(name)
501 http.validateHeaderValue(name, headers[name])
502 } catch (cause) {
503 return upgradeError(request, `Invalid upgrade response header: ${name}`, cause)
504 }
505 }
506 }
507
508 const upgradeError = (request: HttpServerRequest, description: string, cause?: unknown) =>
509 new HttpServerError({ reason: new InternalError({ request, description, cause }) })
510
511 const isHttp2 = (request: Http.IncomingMessage | undefined): boolean => request?.httpVersionMajor === 2
512
513 // HTTP/2 sends the authority as `:authority`, which takes precedence over a Host header.
514 // Pseudo-headers are not valid header names elsewhere, such as in a web `Request`.
515 const requestHeaders = (source: Http.IncomingMessage): Http.IncomingHttpHeaders => {
516 if (!isHttp2(source)) {
517 return source.headers
518 }
519 const headers: Http.IncomingHttpHeaders = {}
520 for (const name in source.headers) {
521 if (name[0] !== ":") headers[name] = source.headers[name]
522 }
523 const authority = source.headers[":authority"]
524 if (typeof authority === "string") headers.host = authority
525 return headers
526 }
527
528 const absoluteTarget = (url: string): URL | undefined => {
529 if (!/^https?:\/\//i.test(url)) {
530 return undefined
531 }
532 try {
533 return new URL(url)
534 } catch {
535 return undefined
536 }
537 }
538
539 // Requests dispatched by our checkContinue listener that have not been
540 // acknowledged yet. Standalone makeHandler users leave acknowledgement to Node
541 // or their own listener. Keyed by the Node request so modified requests share
542 // the entry and body readers acknowledge only once.
543 const pendingContinue = new WeakSet<Http.IncomingMessage>()
544
545 // Body bytes pulled through `stream`, shared by modified requests.
546 const bodyBytesRead = new WeakMap<Http.IncomingMessage, number>()
547
548 class ServerRequestImpl extends NodeHttpIncomingMessage<HttpServerError> implements HttpServerRequest {
549 protected override readonly destroyOnOverflow = false
550 // The adapter tears the body down after the response instead.
551 protected override readonly closeOnDone = false
552 readonly [Request.TypeId]: typeof Request.TypeId
553 readonly response: Http.ServerResponse | LazyArg<Http.ServerResponse>
554 private upgradeState?: UpgradeState | undefined
555 readonly url: string
556 private headersOverride?: Headers.Headers | undefined
557 private peerAddressOverride?: Option.Option<string> | undefined
558 readonly protocol: "http" | "https" | undefined
559
560 constructor(
561 source: Http.IncomingMessage,
562 response: Http.ServerResponse | LazyArg<Http.ServerResponse>,
563 upgradeState?: UpgradeState,
564 url?: string,
565 headersOverride?: Headers.Headers,
566 remoteAddressOverride?: Option.Option<string>,
567 peerAddressOverride?: Option.Option<string>,
568 protocol?: "http" | "https"
569 ) {
570 super(source, (cause) =>
571 new HttpServerError({
572 reason: new RequestParseError({
573 request: this,
574 reason: cause instanceof NodeStream.MaxBytesError ? "BodyTooLarge" : "Parse",
575 cause
576 })
577 }), remoteAddressOverride)
578 this[Request.TypeId] = Request.TypeId
579 this.response = response
580 this.upgradeState = upgradeState
581 if (url === undefined) {
582 const target = absoluteTarget(source.url!)
583 if (target === undefined) {
584 url = source.url!
585 } else {
586 // RFC 9112 §3.2.2: the request-target authority takes precedence over Host.
587 url = target.pathname + target.search
588 const headers: Http.IncomingHttpHeaders = { ...requestHeaders(source), host: target.host }
589 headersOverride = Headers.fromInput(headers)
590 }
591 }
592 this.url = url
593 this.headersOverride = headersOverride
594 this.peerAddressOverride = peerAddressOverride
595 this.protocol = protocol
596 }
597
598 get peerAddress(): Option.Option<string> {
599 return this.peerAddressOverride ?? this.remoteAddress
600 }
601
602 private cachedCookies: Record.ReadonlyRecord<string, string> | undefined
603 get cookies() {
604 if (this.cachedCookies) {
605 return this.cachedCookies
606 }
607 return this.cachedCookies = Cookies.parseHeader(this.headers.cookie ?? "")
608 }
609
610 get resolvedResponse(): Http.ServerResponse {
611 return typeof this.response === "function" ? this.response() : this.response
612 }
613
614 private writeContinue(): void {
615 if (pendingContinue.delete(this.source) && !this.resolvedResponse.headersSent) {
616 this.resolvedResponse.writeContinue()
617 }
618 }
619
620 override get stream(): Stream.Stream<Uint8Array, HttpServerError> {
621 return Stream.suspend(() => {
622 this.writeContinue()
623 const source = this.source
624 return Stream.mapArray(super.stream, (chunk) => {
625 let bytes = bodyBytesRead.get(source) ?? 0
626 for (const item of chunk) bytes += item.length
627 bodyBytesRead.set(source, bytes)
628 return chunk
629 })
630 })
631 }
632
633 override get arrayBuffer(): Effect.Effect<ArrayBuffer, HttpServerError> {
634 return Effect.suspend(() => {
635 this.writeContinue()
636 return super.arrayBuffer
637 })
638 }
639
640 modify(
641 options: {
642 readonly url?: string | undefined
643 readonly headers?: Headers.Headers | undefined
644 readonly remoteAddress?: Option.Option<string> | undefined
645 readonly peerAddress?: Option.Option<string> | undefined
646 readonly protocol?: "http" | "https" | undefined
647 }
648 ) {
649 return new ServerRequestImpl(
650 this.source,
651 this.response,
652 this.upgradeState,
653 options.url ?? this.url,
654 options.headers ?? this.headersOverride,
655 "remoteAddress" in options ? options.remoteAddress : this.remoteAddressOverride,
656 options.peerAddress ?? this.peerAddressOverride,
657 options.protocol ?? this.protocol
658 )
659 }
660
661 get originalUrl(): string {
662 const url = this.source.url!
663 const headers = this.source.headers as Http2.IncomingHttpHeaders
664 const http2 = isHttp2(this.source)
665 const host = (http2 ? headers[":authority"] : undefined) ?? headers.host
666 if (url[0] !== "/" || host === undefined) {
667 return url
668 }
669 // `:scheme` is the target URI scheme, which also covers h2c behind a TLS terminator
670 const scheme = http2 ? headers[":scheme"] : undefined
671 const encrypted = (this.source.socket as Tls.TLSSocket | undefined)?.encrypted
672 return `${scheme ?? (encrypted ? "https" : "http")}://${host}${url}`
673 }
674
675 private cachedMethod: HttpMethod | undefined
676 get method(): HttpMethod {
677 return this.cachedMethod ??= this.source.method!.toUpperCase() as HttpMethod
678 }
679
680 override get headers(): Headers.Headers {
681 this.headersOverride ??= Headers.fromInput(requestHeaders(this.source))
682 return this.headersOverride
683 }
684
685 private multipartEffect:
686 | Effect.Effect<
687 Multipart.Persisted,
688 Multipart.MultipartError,
689 Scope.Scope | FileSystem.FileSystem | Path.Path
690 >
691 | undefined
692 get multipart(): Effect.Effect<
693 Multipart.Persisted,
694 Multipart.MultipartError,
695 Scope.Scope | FileSystem.FileSystem | Path.Path
696 > {
697 if (this.multipartEffect) {
698 return this.multipartEffect
699 }
700 this.multipartEffect = Effect.runSync(Effect.cached(
701 Effect.suspend(() => {
702 this.writeContinue()
703 return NodeMultipart.persisted(this.bodySource, this.source.headers)
704 })
705 ))
706 return this.multipartEffect
707 }
708
709 get multipartStream(): Stream.Stream<Multipart.Part, Multipart.MultipartError> {
710 return Stream.suspend(() => {
711 this.writeContinue()
712 return NodeMultipart.stream(this.bodySource, this.source.headers)
713 })
714 }
715
716 get upgrade(): Effect.Effect<Socket.Socket, HttpServerError> {
717 return this.upgradeWith({})
718 }
719
720 upgradeWith(
721 options: { readonly headers?: Headers.Input | undefined }
722 ): Effect.Effect<Socket.Socket, HttpServerError> {
723 const state = this.upgradeState
724 if (state === undefined) {
725 return Effect.fail(
726 new HttpServerError({
727 reason: new RequestParseError({
728 request: this,
729 description: "not an upgradeable ServerRequest"
730 })
731 })
732 )
733 }
734 return state.upgradeWith(this, Headers.fromInput(options.headers))
735 }
736
737 // The response reported to middleware once the connection is upgraded.
738 get upgradedResponse(): HttpServerResponse | undefined {
739 return this.upgradeState?.response
740 }
741
742 protected override get bodySource(): Readable {
743 return this.upgradeState?.body ?? this.source
744 }
745
746 override toString(): string {
747 return `ServerRequest(${this.method} ${UrlParams.redactUrl(this.url)})`
748 }
749
750 toJSON(): unknown {
751 return HttpIncomingMessage.inspect(this, {
752 _id: "HttpServerRequest",
753 method: this.method,
754 url: UrlParams.redactUrl(this.originalUrl)
755 })
756 }
757 }
758
759 /**
760 * Provides an `HttpServer` by creating and managing a scoped Node
761 * `http.Server` or `node:http2` server with the supplied listen and shutdown
762 * options.
763 *
764 * @stability unstable
765 * @category layers
766 * @since 4.0.0
767 */
768 export const layerServer: (
769 evaluate: LazyArg<Http.Server | Http2.Http2Server | Http2.Http2SecureServer>,
770 options: Options
771 ) => Layer.Layer<HttpServer.HttpServer, ServeError> = flow(make, Layer.effect(HttpServer.HttpServer))
772
773 /**
774 * Provides the Node HTTP support services used by `NodeHttpServer`, including
775 * the HTTP platform, ETag generator, and core Node platform services.
776 *
777 * @stability unstable
778 * @category layers
779 * @since 4.0.0
780 */
781 export const layerHttpServices: Layer.Layer<
782 NodeServices.NodeServices | HttpPlatform.HttpPlatform | Etag.Generator
783 > = Layer.mergeAll(
784 NodeHttpPlatform.layer,
785 Etag.layerWeak,
786 NodeServices.layer
787 )
788
789 /**
790 * Provides a Node `HttpServer` together with the Node HTTP platform, ETag, and
791 * core platform services required to serve requests.
792 *
793 * **Details**
794 *
795 * Node's timeouts are set on the server returned by `evaluate`; see `make`.
796 *
797 * @stability unstable
798 * @category layers
799 * @since 4.0.0
800 */
801 export const layer = (
802 evaluate: LazyArg<Http.Server | Http2.Http2Server | Http2.Http2SecureServer>,
803 options: Options
804 ): Layer.Layer<
805 HttpServer.HttpServer | NodeServices.NodeServices | HttpPlatform.HttpPlatform | Etag.Generator,
806 ServeError
807 > =>
808 Layer.mergeAll(
809 layerServer(evaluate, options),
810 layerHttpServices
811 )
812
813 /**
814 * Provides a Node `HttpServer` together with the Node HTTP platform, ETag,
815 * and core Node platform services, reading the listen and shutdown options from
816 * a `Config` value.
817 *
818 * @stability unstable
819 * @category layers
820 * @since 4.0.0
821 */
822 export const layerConfig = (
823 evaluate: LazyArg<Http.Server | Http2.Http2Server | Http2.Http2SecureServer>,
824 options: Config.Wrap<Options>
825 ): Layer.Layer<
826 HttpServer.HttpServer | NodeServices.NodeServices | HttpPlatform.HttpPlatform | Etag.Generator,
827 ServeError | Config.ConfigError
828 > =>
829 Layer.mergeAll(
830 Layer.effect(HttpServer.HttpServer)(
831 Effect.flatMap(Config.unwrap(options), (options) => make(evaluate, options))
832 ),
833 layerHttpServices
834 )
835
836 /**
837 * Provides a test HTTP server listening on an ephemeral port together with a
838 * Fetch-backed `HttpClient` configured for server integration tests.
839 *
840 * @stability unstable
841 * @category testing
842 * @since 4.0.0
843 */
844 export const layerTest: Layer.Layer<
845 | HttpServer.HttpServer
846 | FileSystem.FileSystem
847 | Path.Path
848 | HttpPlatform.HttpPlatform
849 | Etag.Generator
850 | HttpClient,
851 ServeError,
852 never
853 > = HttpServer.layerTestClient.pipe(
854 Layer.provide(
855 Layer.fresh(FetchHttpClient.layer).pipe(
856 Layer.provide(Layer.succeed(FetchHttpClient.RequestInit)({ keepalive: false }))
857 )
858 ),
859 Layer.provideMerge(layer(http.createServer, { port: 0 }))
860 )
861
862 // -----------------------------------------------------------------------------
863 // Internal
864 // -----------------------------------------------------------------------------
865
866 const closeCode = (exit: Exit.Exit<unknown, unknown>): number =>
867 Exit.isSuccess(exit) ? 1000 : Cause.hasInterruptsOnly(exit.cause) ? 1001 : 1011
868
869 // Reported to middleware in place of the handler's discarded response.
870 const upgradedResponse = Response.empty({ status: 101 })
871
872 const handleResponse = (
873 request: HttpServerRequest,
874 response: HttpServerResponse
875 ): Effect.Effect<unknown, HttpServerError> => {
876 const upgraded = (request as ServerRequestImpl).upgradedResponse
877 if (upgraded !== undefined) {
878 return Effect.succeed(upgraded)
879 }
880 const nodeResponse = (request as ServerRequestImpl).resolvedResponse
881 if (nodeResponse.writableEnded) {
882 return Effect.void
883 }
884
885 let headers: Record<string, string | Array<string>> = response.headers
886 if (response.status === 204 || response.status === 205) {
887 const { "content-length": _, ...rest } = headers
888 headers = rest
889 }
890 if (!Cookies.isEmpty(response.cookies)) {
891 headers = { ...headers }
892 const toSet = Cookies.toSetCookieHeaders(response.cookies)
893 if (headers["set-cookie"] !== undefined) {
894 toSet.push(headers["set-cookie"] as string)
895 }
896 headers["set-cookie"] = toSet
897 }
898
899 try {
900 return writeResponse(request, response, nodeResponse, headers)
901 } catch (defect) {
902 // `writeHead` throws synchronously on headers Node rejects and on statuses outside 200-599
903 return Effect.andThen(
904 cancelResponseBody(response.body),
905 handleCause(nodeResponse, response)(Cause.die(defect))
906 )
907 }
908 }
909
910 const writeResponse = (
911 request: HttpServerRequest,
912 response: HttpServerResponse,
913 nodeResponse: Http.ServerResponse,
914 headers: Record<string, string | Array<string>>
915 ): Effect.Effect<unknown, HttpServerError> => {
916 // Nothing can reach a disconnected client, and writing would throw or never finish
917 if (nodeResponse.destroyed) {
918 return cancelResponseBody(response.body)
919 }
920 if (request.method === "HEAD") {
921 writeHead(nodeResponse, response, headers)
922 return Effect.andThen(cancelResponseBody(response.body), endAndAwait(nodeResponse))
923 }
924 if (Response.omitsBody(response)) {
925 writeHead(nodeResponse, response, headers)
926 nodeResponse.end()
927 return cancelResponseBody(response.body)
928 }
929 nodeResponse.strictContentLength = true
930 const body = response.body
931 switch (body._tag) {
932 case "Empty": {
933 writeHead(nodeResponse, response, headers)
934 return end(nodeResponse, response)
935 }
936 case "Raw": {
937 const piped = typeof body.body === "object" && body.body !== null && "pipe" in body.body &&
938 typeof body.body.pipe === "function"
939 writeHead(nodeResponse, response, headers, piped)
940 if (piped) {
941 return Effect.tryPromise({
942 try: (signal) => pipeTo(body.body as Readable, nodeResponse, headers, signal),
943 catch: (cause) =>
944 new HttpServerError({
945 reason: new ResponseError({
946 request,
947 response,
948 description: "Error writing raw response",
949 cause
950 })
951 })
952 }).pipe(
953 Effect.interruptible,
954 Effect.tapCause(handleCause(nodeResponse, response))
955 )
956 }
957 return endAndAwait(nodeResponse, body.body).pipe(Effect.tapCause(handleCause(nodeResponse, response)))
958 }
959 case "Uint8Array": {
960 writeHead(nodeResponse, response, headers)
961 // If the body is less than 1MB, we skip the callback
962 if (body.contentLength < 1024 * 1024) {
963 // Writing text directly lets Node flush headers and body together.
964 return end(nodeResponse, response, body.text ?? body.body)
965 }
966 return endAndAwait(nodeResponse, body.text ?? body.body).pipe(
967 Effect.tapCause(handleCause(nodeResponse, response))
968 )
969 }
970 case "FormData": {
971 const r = new globalThis.Response(body.formData)
972 const formHeaders = { ...headers, ...Object.fromEntries(r.headers) }
973 writeHead(nodeResponse, response, formHeaders)
974 return Effect.tryPromise({
975 try: (signal) => pipeTo(Readable.fromWeb(r.body as any), nodeResponse, formHeaders, signal),
976 catch: (cause) =>
977 new HttpServerError({
978 reason: new ResponseError({
979 request,
980 response,
981 description: "Error writing FormData response",
982 cause
983 })
984 })
985 }).pipe(
986 Effect.interruptible,
987 Effect.tapCause(handleCause(nodeResponse, response))
988 )
989 }
990 case "Stream": {
991 writeHead(nodeResponse, response, headers, true)
992 const drainLatch = Latch.makeUnsafe()
993 nodeResponse.on("drain", () => drainLatch.openUnsafe())
994 return body.stream.pipe(
995 Stream.orDie,
996 Stream.runForEachArray((array) => {
997 const chunk = array.length > 1 ? Buffer.concat(array) : array[0]
998 if (nodeResponse.write(chunk)) return Effect.void
999 drainLatch.closeUnsafe()
1000 return drainLatch.await
1001 }),
1002 Effect.interruptible,
1003 Effect.matchCauseEffect({
1004 onSuccess: () => end(nodeResponse, response),
1005 onFailure: handleCause(nodeResponse, response)
1006 })
1007 )
1008 }
1009 }
1010 }
1011
1012 // On HTTP/1.1 Node sends a 1xx as an interim response, so the client never gets a final one
1013 const isFinalStatus = (status: number): boolean => status >= 200 && status <= 599
1014
1015 const internalServerError = Response.empty({ status: 500 })
1016
1017 const invalidStatusError = (status: number) => new RangeError(`Invalid final response status: ${status}`)
1018
1019 const writeHead = (
1020 nodeResponse: Http.ServerResponse,
1021 response: HttpServerResponse,
1022 headers: Record<string, string | Array<string>>,
1023 streaming = false
1024 ): void => {
1025 if (!isFinalStatus(response.status)) {
1026 throw invalidStatusError(response.status)
1027 }
1028 if (isHttp2(nodeResponse.req)) {
1029 // HTTP/2 has no status message
1030 nodeResponse.writeHead(response.status, withoutConnectionHeaders(headers))
1031 return
1032 }
1033 nodeResponse.writeHead(
1034 response.status,
1035 response.statusText,
1036 closesConnection(nodeResponse.req, streaming) ? { ...headers, connection: "close" } : headers
1037 )
1038 }
1039
1040 // RFC 9113 §8.2.2: HTTP/2 forbids connection-specific headers, and Node rejects them. They
1041 // mostly come from proxied HTTP/1.1 responses, so they are dropped rather than failing the response.
1042 const connectionHeaders = new Set([
1043 "connection",
1044 "keep-alive",
1045 "proxy-connection",
1046 "transfer-encoding",
1047 "upgrade",
1048 "http2-settings"
1049 ])
1050
1051 const withoutConnectionHeaders = (
1052 headers: Record<string, string | Array<string>>
1053 ): Record<string, string | Array<string>> => {
1054 let result: Record<string, string | Array<string>> | undefined
1055 for (const name in headers) {
1056 if (connectionHeaders.has(name) || (name === "te" && headers[name] !== "trailers")) {
1057 result ??= { ...headers }
1058 delete result[name]
1059 }
1060 }
1061 return result ?? headers
1062 }
1063
1064 // Unread request body discarded after the response to keep the connection alive. Past this it closes.
1065 const maxDrainBytes = 256 * 1024
1066
1067 // Time the client gets to read the response after the FIN, before a destroy can reset the connection.
1068 const closeGracePeriod = 500
1069
1070 // Decided when the headers are written, while `Connection: close` can still be sent. A streaming
1071 // response may still read the body, so only a pending 100 Continue closes it here.
1072 const closesConnection = (request: Http.IncomingMessage | undefined, streaming: boolean): boolean => {
1073 if (request === undefined || request.httpVersionMajor !== 1 || request.complete) {
1074 return false
1075 }
1076 if (pendingContinue.has(request)) {
1077 return true
1078 }
1079 const contentLength = request.headers["content-length"]
1080 return !streaming && contentLength !== undefined &&
1081 Number(contentLength) - (bodyBytesRead.get(request) ?? 0) > maxDrainBytes
1082 }
1083
1084 // Node internals read by its own `finish` listener, which runs right after `teardownBody`.
1085 interface IncomingMessageInternals {
1086 _consuming: boolean
1087 }
1088 interface ServerResponseInternals {
1089 _last: boolean
1090 }
1091
1092 // Node drains an untouched body in full, leaves a partly read one unread, and
1093 // destroys a closing socket as soon as the response is flushed, which resets the
1094 // connection while body bytes are queued and can lose the response.
1095 const teardownBody = (request: Http.IncomingMessage, response: Http.ServerResponse): void => {
1096 if (request.complete || request.destroyed) {
1097 return
1098 }
1099 const socket = request.socket
1100 if ((response as unknown as ServerResponseInternals)._last) {
1101 // Skips Node's `_dump`. Unlike `_dumped`, it keeps the parser applying backpressure.
1102 ;(request as unknown as IncomingMessageInternals)._consuming = true
1103 ;(response as unknown as ServerResponseInternals)._last = false
1104 return closeSocket(socket)
1105 }
1106 let bytes = 0
1107 request.on("data", (chunk: Buffer) => {
1108 bytes += chunk.length
1109 if (bytes > maxDrainBytes) {
1110 // Pausing stops further `data` events, so this runs once.
1111 request.pause()
1112 closeSocket(socket)
1113 }
1114 })
1115 request.resume()
1116 }
1117
1118 const closeSocket = (socket: Net.Socket): void => {
1119 if (socket.destroyed) {
1120 return
1121 }
1122 socket.end()
1123 const timer = setTimeout(() => socket.destroy(), closeGracePeriod)
1124 socket.once("close", () => clearTimeout(timer))
1125 }
1126
1127 const cancelResponseBody = (body: HttpServerResponse["body"]): Effect.Effect<void> => {
1128 const stream = body._tag === "Raw" ? body.body : undefined
1129 if (stream instanceof Readable) {
1130 return Effect.sync(() => stream.destroy())
1131 }
1132 return Effect.void
1133 }
1134
1135 // `end`'s callback only runs on `finish`, which a disconnect replaces with `close`.
1136 // A response that is already destroyed emits neither.
1137 const endAndAwait = (nodeResponse: Http.ServerResponse, chunk?: unknown): Effect.Effect<void> =>
1138 Effect.callback<void>((resume) => {
1139 if (nodeResponse.destroyed) return resume(Effect.void)
1140 let completed = false
1141 const done = () => {
1142 if (completed) return
1143 completed = true
1144 nodeResponse.off("close", done)
1145 resume(Effect.void)
1146 }
1147 nodeResponse.once("close", done)
1148 if (chunk === undefined) {
1149 nodeResponse.end(done)
1150 } else {
1151 nodeResponse.end(chunk, done)
1152 }
1153 })
1154
1155 const end = (
1156 nodeResponse: Http.ServerResponse,
1157 response: HttpServerResponse,
1158 chunk?: string | Uint8Array
1159 ): Effect.Effect<void> => {
1160 try {
1161 nodeResponse.end(chunk)
1162 return Effect.void
1163 } catch (defect) {
1164 return handleCause(nodeResponse, response)(Cause.die(defect))
1165 }
1166 }
1167
1168 // Check lengths before writing: Node's strictContentLength throws uncaught inside pipeline.
1169 const pipeTo = (
1170 readable: Readable,
1171 nodeResponse: Http.ServerResponse,
1172 headers: Record<string, string | Array<string>>,
1173 signal: AbortSignal
1174 ): Promise<void> => {
1175 const contentLength = headers["content-length"]
1176 if (typeof contentLength !== "string") {
1177 return pipeline(readable, nodeResponse, { signal })
1178 }
1179 let remaining = Number(contentLength)
1180 const mismatch = () => new Error(`Response body doesn't match content-length of ${contentLength}`)
1181 return pipeline(
1182 readable,
1183 new Transform({
1184 transform(chunk: Buffer, _encoding, callback) {
1185 remaining -= chunk.length
1186 callback(remaining < 0 ? mismatch() : null, chunk)
1187 },
1188 flush(callback) {
1189 callback(remaining > 0 ? mismatch() : null)
1190 }
1191 }),
1192 nodeResponse,
1193 { signal }
1194 )
1195 }
1196
1197 const handleCause = (
1198 nodeResponse: Http.ServerResponse,
1199 originalResponse: HttpServerResponse
1200 ) =>
1201 <E>(originalCause: Cause.Cause<E>) =>
1202 Effect.flatMap(causeResponse(originalCause), ([response, cause]) => {
1203 const headersSent = nodeResponse.headersSent
1204 let abort = headersSent
1205 if (!headersSent) {
1206 if (!isFinalStatus(response.status)) {
1207 cause = Cause.combine(cause, Cause.die(invalidStatusError(response.status)))
1208 response = internalServerError
1209 }
1210 try {
1211 writeHead(nodeResponse, response, {})
1212 } catch {
1213 // HTTP/2 keeps the headers of a rejected `writeHead`, so the fallback can throw too
1214 abort = true
1215 }
1216 }
1217 if (!nodeResponse.writableEnded) {
1218 // Ending a started response normally would make a partial body look complete
1219 if (abort) {
1220 // An error resets an HTTP/2 stream with INTERNAL_ERROR rather than NO_ERROR. On
1221 // HTTP/1.1 it could reach `clientError` listeners, and the socket closes either way.
1222 nodeResponse.destroy(isHttp2(nodeResponse.req) ? new Error("The response was aborted") : undefined)
1223 } else {
1224 nodeResponse.end()
1225 }
1226 }
1227 return Effect.failCause(
1228 headersSent
1229 ? Cause.combine(originalCause, Cause.die(originalResponse))
1230 : cause
1231 )
1232 })