Open History to see the last commit
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 * @since 4.0.0
13 */
14import * as Effect from "effect/Effect"
15import * as Inspectable from "effect/Inspectable"
16import * as Option from "effect/Option"
17import type * as Schema from "effect/Schema"
18import type * as Stream from "effect/Stream"
19import * as Headers from "effect/unstable/http/Headers"
20import * as IncomingMessage from "effect/unstable/http/HttpIncomingMessage"
21import * as UrlParams from "effect/unstable/http/UrlParams"
22import type * as Http from "node:http"
23import * as NodeStream from "./NodeStream.ts"
24
25/**
26 * Adapts a Node `IncomingMessage` to Effect HTTP incoming messages.
27 *
28 * **When to use**
29 *
30 * Use to implement Node HTTP request or response adapters that expose the
31 * Effect HTTP incoming-message interface.
32 *
33 * **Details**
34 *
35 * The adapter exposes headers, remote address, stream access, and cached body
36 * decoders. Subclasses provide the error mapping for unknown Node errors.
37 *
38 * @category constructors
39 * @since 4.0.0
40 */
41export abstract class NodeHttpIncomingMessage<E> extends Inspectable.Class
42 implements IncomingMessage.HttpIncomingMessage<E>
43{
44 /**
45 * Marks this value as an HTTP incoming message for runtime guards.
46 *
47 * @since 4.0.0
48 */
49 readonly [IncomingMessage.TypeId]: typeof IncomingMessage.TypeId
50 readonly source: Http.IncomingMessage
51 readonly onError: (error: unknown) => E
52 readonly remoteAddressOverride?: Option.Option<string> | undefined
53
54 constructor(
55 source: Http.IncomingMessage,
56 onError: (error: unknown) => E,
57 remoteAddressOverride?: Option.Option<string>
58 ) {
59 super()
60 this[IncomingMessage.TypeId] = IncomingMessage.TypeId
61 this.source = source
62 this.onError = onError
63 this.remoteAddressOverride = remoteAddressOverride
64 }
65
66 get headers() {
67 return Headers.fromInput(this.source.headers as any)
68 }
69
70 get remoteAddress() {
71 return this.remoteAddressOverride ?? Option.fromNullishOr(this.source.socket.remoteAddress)
72 }
73
74 private textEffect: Effect.Effect<string, E> | undefined
75 get text(): Effect.Effect<string, E> {
76 if (this.textEffect) {
77 return this.textEffect
78 }
79 this.textEffect = Effect.runSync(Effect.cached(
80 Effect.flatMap(
81 IncomingMessage.MaxBodySize,
82 (maxBodySize) =>
83 NodeStream.toString(() => this.source, {
84 onError: this.onError,
85 maxBytes: maxBodySize
86 })
87 )
88 ))
89 this.arrayBufferEffect = Effect.map(this.textEffect, (_) => new TextEncoder().encode(_).buffer)
90 return this.textEffect
91 }
92
93 get textUnsafe(): string {
94 return Effect.runSync(this.text)
95 }
96
97 get json(): Effect.Effect<Schema.Json, E> {
98 return Effect.flatMap(this.text, (text) =>
99 Effect.try({
100 try: () => text === "" ? null : JSON.parse(text),
101 catch: this.onError
102 }))
103 }
104
105 get jsonUnsafe(): Schema.Json {
106 return Effect.runSync(this.json)
107 }
108
109 get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, E> {
110 return Effect.flatMap(this.text, (_) =>
111 Effect.try({
112 try: () => UrlParams.fromInput(new URLSearchParams(_)),
113 catch: this.onError
114 }))
115 }
116
117 get stream(): Stream.Stream<Uint8Array, E> {
118 return NodeStream.fromReadable({
119 evaluate: () => this.source,
120 onError: this.onError
121 })
122 }
123
124 private arrayBufferEffect: Effect.Effect<ArrayBuffer, E> | undefined
125 get arrayBuffer(): Effect.Effect<ArrayBuffer, E> {
126 if (this.arrayBufferEffect) {
127 return this.arrayBufferEffect
128 }
129 this.arrayBufferEffect = Effect.withFiber((fiber) =>
130 NodeStream.toArrayBuffer(() => this.source, {
131 onError: this.onError,
132 maxBytes: fiber.getRef(IncomingMessage.MaxBodySize)
133 })
134 ).pipe(
135 Effect.cached,
136 Effect.runSync
137 )
138 this.textEffect = Effect.map(this.arrayBufferEffect, (_) => new TextDecoder().decode(_))
139 return this.arrayBufferEffect
140 }
141}