The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Adapter base for exposing Node `http.IncomingMessage` values as Effect HTTP |
| 3 | * incoming messages. |
| 4 | * |
| 5 | * Server requests and Node client responses both arrive as Node readable |
| 6 | * streams with raw header objects, socket metadata, and one-shot body |
| 7 | * consumption. This module's `NodeHttpIncomingMessage` class keeps the original |
| 8 | * Node message available while presenting Effect's `HttpIncomingMessage` shape: |
| 9 | * typed headers, remote address lookup, stream access, and text, JSON, |
| 10 | * URL-encoded, and array-buffer body readers. |
| 11 | * |
| 12 | * @stability unstable |
| 13 | * @since 4.0.0 |
| 14 | */ |
| 15 | import * as Effect from "effect/Effect" |
| 16 | import * as Headers from "effect/http/Headers" |
| 17 | import * as IncomingMessage from "effect/http/HttpIncomingMessage" |
| 18 | import * as UrlParams from "effect/http/UrlParams" |
| 19 | import * as Inspectable from "effect/Inspectable" |
| 20 | import * as Option from "effect/Option" |
| 21 | import type * as Schema from "effect/Schema" |
| 22 | import type * as Stream from "effect/Stream" |
| 23 | import type * as Http from "node:http" |
| 24 | import type { Readable } from "node:stream" |
| 25 | import * as NodeStream from "./NodeStream.ts" |
| 26 | |
| 27 | const textDecoder = new TextDecoder() |
| 28 | |
| 29 | const formDecoder = new TextDecoder("utf-8", { ignoreBOM: true }) |
| 30 | |
| 31 | /** |
| 32 | * Adapts a Node `IncomingMessage` to Effect HTTP incoming messages. |
| 33 | * |
| 34 | * **When to use** |
| 35 | * |
| 36 | * Use to implement Node HTTP request or response adapters that expose the |
| 37 | * Effect HTTP incoming-message interface. |
| 38 | * |
| 39 | * **Details** |
| 40 | * |
| 41 | * The adapter exposes headers, remote address, stream access, and cached body |
| 42 | * decoders. Subclasses provide the error mapping for unknown Node errors. |
| 43 | * |
| 44 | * @stability unstable |
| 45 | * @category constructors |
| 46 | * @since 4.0.0 |
| 47 | */ |
| 48 | export abstract class NodeHttpIncomingMessage<E> extends Inspectable.Class |
| 49 | implements IncomingMessage.HttpIncomingMessage<E> |
| 50 | { |
| 51 | /** |
| 52 | * Marks this value as an HTTP incoming message for runtime guards. |
| 53 | * |
| 54 | * @since 4.0.0 |
| 55 | */ |
| 56 | readonly [IncomingMessage.TypeId]: typeof IncomingMessage.TypeId |
| 57 | readonly source: Http.IncomingMessage |
| 58 | readonly onError: (error: unknown) => E |
| 59 | readonly remoteAddressOverride?: Option.Option<string> | undefined |
| 60 | protected readonly destroyOnOverflow: boolean = true |
| 61 | protected readonly closeOnDone: boolean = true |
| 62 | |
| 63 | constructor( |
| 64 | source: Http.IncomingMessage, |
| 65 | onError: (error: unknown) => E, |
| 66 | remoteAddressOverride?: Option.Option<string> |
| 67 | ) { |
| 68 | super() |
| 69 | this[IncomingMessage.TypeId] = IncomingMessage.TypeId |
| 70 | this.source = source |
| 71 | this.onError = onError |
| 72 | this.remoteAddressOverride = remoteAddressOverride |
| 73 | } |
| 74 | |
| 75 | get headers() { |
| 76 | return Headers.fromInput(this.source.headers as any) |
| 77 | } |
| 78 | |
| 79 | get remoteAddress() { |
| 80 | return this.remoteAddressOverride ?? Option.fromNullishOr(this.source.socket?.remoteAddress) |
| 81 | } |
| 82 | |
| 83 | private textEffect: Effect.Effect<string, E> | undefined |
| 84 | get text(): Effect.Effect<string, E> { |
| 85 | if (this.textEffect) { |
| 86 | return this.textEffect |
| 87 | } |
| 88 | this.textEffect = Effect.map(this.arrayBuffer, (_) => textDecoder.decode(_)) |
| 89 | return this.textEffect |
| 90 | } |
| 91 | |
| 92 | get textUnsafe(): string { |
| 93 | return Effect.runSync(this.text) |
| 94 | } |
| 95 | |
| 96 | get json(): Effect.Effect<Schema.Json, E> { |
| 97 | return Effect.flatMap(this.text, (text) => |
| 98 | Effect.try({ |
| 99 | try: () => text === "" ? null : JSON.parse(text), |
| 100 | catch: this.onError |
| 101 | })) |
| 102 | } |
| 103 | |
| 104 | get jsonUnsafe(): Schema.Json { |
| 105 | return Effect.runSync(this.json) |
| 106 | } |
| 107 | |
| 108 | get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, E> { |
| 109 | return Effect.flatMap(this.arrayBuffer, (bytes) => |
| 110 | Effect.try({ |
| 111 | // A leading "?" is form data; URLSearchParams would drop it as a query prefix. |
| 112 | try: () => UrlParams.fromInput(new URLSearchParams(formDecoder.decode(bytes).replace(/^\?/, "%3F"))), |
| 113 | catch: this.onError |
| 114 | })) |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * The readable the body is read from, `source` unless a subclass reads the |
| 119 | * body from elsewhere. |
| 120 | */ |
| 121 | protected get bodySource(): Readable { |
| 122 | return this.source |
| 123 | } |
| 124 | |
| 125 | get stream(): Stream.Stream<Uint8Array, E> { |
| 126 | return NodeStream.fromReadable({ |
| 127 | evaluate: () => this.bodySource, |
| 128 | onError: this.onError, |
| 129 | closeOnDone: this.closeOnDone |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | private arrayBufferEffect: Effect.Effect<ArrayBuffer, E> | undefined |
| 134 | get arrayBuffer(): Effect.Effect<ArrayBuffer, E> { |
| 135 | if (this.arrayBufferEffect) { |
| 136 | return this.arrayBufferEffect |
| 137 | } |
| 138 | this.arrayBufferEffect = Effect.withFiber((fiber) => |
| 139 | NodeStream.toArrayBuffer(() => this.bodySource, { |
| 140 | onError: this.onError, |
| 141 | destroyOnOverflow: this.destroyOnOverflow, |
| 142 | maxBytes: fiber.getRef(IncomingMessage.MaxBodySize) |
| 143 | }) |
| 144 | ).pipe( |
| 145 | Effect.cached, |
| 146 | Effect.runSync |
| 147 | ) |
| 148 | this.textEffect = Effect.map(this.arrayBufferEffect, (_) => textDecoder.decode(_)) |
| 149 | return this.arrayBufferEffect |
| 150 | } |
| 151 | } |