@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,049 lines 32.6 KB
Raw
1 /**
2 * Node.js implementations of the Effect `HttpClient`.
3 *
4 * This module supplies Node runtime backends for the platform-independent
5 * Effect HTTP client API. It re-exports the fetch-based `Fetch`, `RequestInit`,
6 * and `layerFetch` APIs, defines an Undici-backed client with dispatcher
7 * services and request options, and defines a lower-level `node:http` /
8 * `node:https` client with scoped HTTP agent layers.
9 *
10 * @stability unstable
11 * @since 4.0.0
12 */
13 import * as Context from "effect/Context"
14 import * as Effect from "effect/Effect"
15 import * as Fiber from "effect/Fiber"
16 import { constVoid, flow } from "effect/Function"
17 import * as Cookies from "effect/http/Cookies"
18 import * as Headers from "effect/http/Headers"
19 import type * as Body from "effect/http/HttpBody"
20 import * as Client from "effect/http/HttpClient"
21 import * as Error from "effect/http/HttpClientError"
22 import type { HttpClientRequest } from "effect/http/HttpClientRequest"
23 import * as Response from "effect/http/HttpClientResponse"
24 import type { HttpClientResponse } from "effect/http/HttpClientResponse"
25 import * as IncomingMessage from "effect/http/HttpIncomingMessage"
26 import * as UrlParams from "effect/http/UrlParams"
27 import * as Inspectable from "effect/Inspectable"
28 import * as Layer from "effect/Layer"
29 import * as Option from "effect/Option"
30 import { type Pipeable, pipeArguments } from "effect/Pipeable"
31 import type * as Schema from "effect/Schema"
32 import type * as Scope from "effect/Scope"
33 import type * as Socket from "effect/socket/Socket"
34 import * as Stream from "effect/Stream"
35 import type * as Http from "node:http"
36 import type * as Https from "node:https"
37 import { type Duplex, Readable } from "node:stream"
38 import * as Consumers from "node:stream/consumers"
39 import { http, https } from "./internal/http.ts"
40 import { NodeHttpIncomingMessage } from "./NodeHttpIncomingMessage.ts"
41 import * as NodeSink from "./NodeSink.ts"
42 import * as NodeStream from "./NodeStream.ts"
43 import type * as Undici from "./Undici.ts"
44
45 // -----------------------------------------------------------------------------
46 // Fetch
47 // -----------------------------------------------------------------------------
48
49 export {
50 /**
51 * Provides a fetch-based HTTP client implementation for Node.js.
52 *
53 * **When to use**
54 *
55 * Use to access or override the fetch implementation used by the Node
56 * fetch-based HTTP client.
57 *
58 * @stability unstable
59 * @category services
60 * @since 4.0.0
61 */
62 Fetch,
63 /**
64 * Layer that provides the fetch-based HTTP client implementation.
65 *
66 * @stability unstable
67 * @category layers
68 * @since 4.0.0
69 */
70 layer as layerFetch,
71 /**
72 * Provides request initialization options accepted by the fetch-based HTTP client.
73 *
74 * **When to use**
75 *
76 * Use to provide default fetch request options for Node HTTP requests.
77 *
78 * @stability unstable
79 * @category services
80 * @since 4.0.0
81 */
82 RequestInit
83 } from "effect/http/FetchHttpClient"
84
85 // -----------------------------------------------------------------------------
86 // Undici
87 // -----------------------------------------------------------------------------
88
89 /**
90 * Service tag for the Undici `Dispatcher` used by the Undici-backed HTTP
91 * client.
92 *
93 * @stability unstable
94 * @category services
95 * @since 4.0.0
96 */
97 export class Dispatcher extends Context.Service<Dispatcher, Undici.Dispatcher>()(
98 "@effect/platform-node/NodeHttpClient/Dispatcher"
99 ) {}
100
101 const loadUndici = Effect.promise(() => import("./Undici.ts"))
102
103 /**
104 * Acquires a new Undici `Agent` dispatcher configured with the supplied
105 * options and destroys it when the enclosing scope is finalized.
106 *
107 * **When to use**
108 *
109 * Use to control how the Undici client connects, for example to send every
110 * request over a unix domain socket or to present a client certificate.
111 *
112 * **Details**
113 *
114 * The options are passed to `new Agent(options)`. Connection settings live
115 * under `connect`:
116 *
117 * - `connect: { socketPath }` dials a unix domain socket (or a Windows named
118 * pipe) instead of TCP. The request URL still supplies the `Host` header
119 * and path, so any origin such as `http://localhost` works.
120 * - `connect: { ca, cert, key, servername, rejectUnauthorized }` configures
121 * TLS for `https:` origins, including mutual TLS.
122 *
123 * `makeUndici` sets `headersTimeout` and `bodyTimeout` on every request, so
124 * timeouts given here are overridden. Use `Effect.timeout` instead.
125 *
126 * @stability unstable
127 * @category resource management
128 * @since 4.0.5
129 */
130 export const makeDispatcherOptions = (
131 options?: Undici.Agent.Options | undefined
132 ): Effect.Effect<Undici.Dispatcher, never, Scope.Scope> =>
133 Effect.acquireRelease(
134 Effect.map(loadUndici, (_) => new _.Agent(options)),
135 (dispatcher) => Effect.promise(() => dispatcher.destroy())
136 )
137
138 /**
139 * Acquires a new Undici `Agent` dispatcher and destroys it when the enclosing
140 * scope is finalized.
141 *
142 * @stability unstable
143 * @category resource management
144 * @since 4.0.0
145 */
146 export const makeDispatcher: Effect.Effect<Undici.Dispatcher, never, Scope.Scope> = makeDispatcherOptions()
147
148 /**
149 * Provides the `Dispatcher` service using a scoped Undici `Agent` configured
150 * with the supplied options.
151 *
152 * **Details**
153 *
154 * See `makeDispatcherOptions` for the unix socket and TLS settings.
155 *
156 * @stability unstable
157 * @category layers
158 * @since 4.0.5
159 */
160 export const layerDispatcherOptions: (options?: Undici.Agent.Options | undefined) => Layer.Layer<Dispatcher> = flow(
161 makeDispatcherOptions,
162 Layer.effect(Dispatcher)
163 )
164
165 /**
166 * Provides the `Dispatcher` service using a scoped Undici `Agent`.
167 *
168 * @stability unstable
169 * @category layers
170 * @since 4.0.0
171 */
172 export const layerDispatcher: Layer.Layer<Dispatcher> = layerDispatcherOptions()
173
174 /**
175 * Provides the `Dispatcher` service from Undici's process-global dispatcher,
176 * without creating or owning a new agent.
177 *
178 * @stability unstable
179 * @category layers
180 * @since 4.0.0
181 */
182 export const dispatcherLayerGlobal: Layer.Layer<Dispatcher> = Layer.effect(Dispatcher)(
183 Effect.map(loadUndici, (_) => _.getGlobalDispatcher())
184 )
185
186 /**
187 * Fiber reference containing default Undici request options applied to requests
188 * sent by `makeUndici`.
189 *
190 * @stability unstable
191 * @category services
192 * @since 4.0.0
193 */
194 export const UndiciOptions = Context.Reference<Partial<Undici.Dispatcher.RequestOptions>>(
195 "@effect/platform-node/NodeHttpClient/UndiciOptions",
196 { defaultValue: () => ({}) }
197 )
198
199 /**
200 * Creates an `HttpClient` that sends requests through the current Undici
201 * `Dispatcher`, converts Effect HTTP bodies to Undici bodies, and maps
202 * transport and decode failures to `HttpClientError`.
203 *
204 * @stability unstable
205 * @category constructors
206 * @since 4.0.0
207 */
208 export const makeUndici = Effect.gen(function*() {
209 const dispatcher = yield* Dispatcher
210 return Client.make((request, url, signal, fiber) =>
211 convertBody(request).pipe(
212 Effect.flatMap(({ body, headers }): Effect.Effect<UndiciResponseSource, Error.HttpClientError> => {
213 const options = {
214 ...fiber.getRef(UndiciOptions),
215 method: request.method,
216 headers,
217 origin: url.origin,
218 path: url.pathname + url.search,
219 body,
220 // leave timeouts to Effect.timeout etc
221 headersTimeout: 60 * 60 * 1000,
222 bodyTimeout: 0
223 }
224 if (request.headers.upgrade !== undefined) {
225 return dispatchUpgrade(dispatcher, request, signal, {
226 ...options,
227 // undici writes the connection and upgrade headers itself
228 headers: Headers.removeMany(headers, ["connection", "upgrade"]),
229 upgrade: request.headers.upgrade
230 })
231 }
232 return Effect.tryPromise({
233 try: () => dispatcher.request({ ...options, signal }),
234 catch: (cause) =>
235 new Error.HttpClientError({
236 reason: new Error.TransportError({
237 request,
238 cause
239 })
240 })
241 })
242 }),
243 Effect.map((response) => new UndiciResponse(request, response, url.href.split("#")[0]))
244 )
245 )
246 })
247
248 interface UndiciResponseSource {
249 readonly statusCode: number
250 readonly headers: Undici.Dispatcher.ResponseData["headers"]
251 readonly body: Readable & { arrayBuffer(): Promise<ArrayBuffer> }
252 readonly upgraded?: Duplex | undefined
253 }
254
255 // Dispatch directly to support both upgrades and readable refusal bodies.
256 // Dispatcher.request rejects upgrades; Dispatcher.upgrade rejects non-101 responses.
257 const dispatchUpgrade = (
258 dispatcher: Undici.Dispatcher,
259 request: HttpClientRequest,
260 signal: AbortSignal,
261 options: Undici.Dispatcher.DispatchOptions
262 ) =>
263 Effect.callback<UndiciResponseSource, Error.HttpClientError>((resume) => {
264 let controller: Undici.Dispatcher.DispatchController | undefined
265 let body: UpgradeResponseBody | undefined
266 let reason: globalThis.Error | undefined
267 const abort = (error: globalThis.Error) => {
268 reason ??= error
269 if (body !== undefined) body.destroy(error)
270 else controller?.abort(error)
271 }
272 const onAbort = () => abort(signal.reason)
273 const fail = (cause: unknown) =>
274 resume(Effect.fail(
275 new Error.HttpClientError({
276 reason: new Error.TransportError({ request, cause })
277 })
278 ))
279 if (signal.aborted) onAbort()
280 else signal.addEventListener("abort", onAbort, { once: true })
281 const done = () => signal.removeEventListener("abort", onAbort)
282 try {
283 dispatcher.dispatch(options, {
284 onRequestStart(c) {
285 controller = c
286 if (reason !== undefined) c.abort(reason)
287 },
288 onRequestUpgrade(_, statusCode, headers, socket) {
289 done()
290 const body = new UpgradeResponseBody(undefined)
291 body.push(null)
292 // undici has already pushed back the bytes past the response head
293 resume(Effect.succeed({ statusCode, headers, body, upgraded: takeOver(socket, undefined, signal) }))
294 },
295 onResponseStart(c, statusCode, headers) {
296 if (statusCode < 200) return
297 body = new UpgradeResponseBody(c)
298 resume(Effect.succeed({ statusCode, headers, body }))
299 },
300 onResponseData(c, chunk) {
301 if (!body!.push(chunk)) c.pause()
302 },
303 onResponseEnd() {
304 done()
305 body!.push(null)
306 },
307 onResponseError(_, cause) {
308 done()
309 if (body === undefined) fail(cause)
310 else body.destroy(cause)
311 }
312 })
313 } catch (cause) {
314 done()
315 fail(cause)
316 }
317 return Effect.sync(() => {
318 done()
319 abort(new globalThis.Error("The request was interrupted"))
320 })
321 })
322
323 class UpgradeResponseBody extends Readable {
324 readonly controller: Undici.Dispatcher.DispatchController | undefined
325 constructor(controller: Undici.Dispatcher.DispatchController | undefined) {
326 super()
327 this.controller = controller
328 }
329 override _read() {
330 this.controller?.resume()
331 }
332 override _destroy(error: globalThis.Error | null, callback: (error?: globalThis.Error | null) => void) {
333 if (!this.readableEnded) this.controller?.abort(error ?? new globalThis.Error("The response body was destroyed"))
334 callback(error)
335 }
336 arrayBuffer(): Promise<ArrayBuffer> {
337 return Consumers.arrayBuffer(this)
338 }
339 }
340
341 function convertBody(request: HttpClientRequest): Effect.Effect<{
342 readonly body: Exclude<Undici.Dispatcher.DispatchOptions["body"], undefined>
343 readonly headers: Headers.Headers
344 }> {
345 const { body, headers } = request
346 switch (body._tag) {
347 case "Empty": {
348 return Effect.succeed({ body: null, headers })
349 }
350 case "Uint8Array":
351 case "Raw": {
352 return Effect.succeed({ body: body.body as Uint8Array, headers })
353 }
354 case "FormData": {
355 return Effect.map(loadUndici, (undici) => ({
356 body: convertFormData(body.formData, undici),
357 headers: Headers.remove(headers, "content-type")
358 }))
359 }
360 case "Stream": {
361 return Effect.map(NodeStream.toReadable(body.stream), (body) => ({ body, headers }))
362 }
363 }
364 }
365
366 const convertFormData = (formData: FormData, undici: typeof Undici): Undici.FormData => {
367 const converted = new undici.FormData()
368 for (const [key, value] of formData) {
369 converted.append(key, value)
370 }
371 return converted
372 }
373
374 function noopErrorHandler(_: any) {}
375
376 const formDecoder = new TextDecoder("utf-8", { ignoreBOM: true })
377
378 class UndiciResponse extends Inspectable.Class implements HttpClientResponse, Pipeable {
379 readonly [IncomingMessage.TypeId]: typeof IncomingMessage.TypeId
380 readonly [Response.TypeId]: typeof Response.TypeId
381 readonly request: HttpClientRequest
382 readonly source: UndiciResponseSource
383 readonly url: string
384
385 constructor(
386 request: HttpClientRequest,
387 source: UndiciResponseSource,
388 url: string
389 ) {
390 super()
391 this[IncomingMessage.TypeId] = IncomingMessage.TypeId
392 this[Response.TypeId] = Response.TypeId
393 this.request = request
394 this.source = source
395 this.url = url
396 source.body.on("error", noopErrorHandler)
397 }
398
399 get status() {
400 return this.source.statusCode!
401 }
402
403 get statusText() {
404 return undefined
405 }
406
407 get headers(): Headers.Headers {
408 return Headers.fromInput(this.source.headers)
409 }
410
411 cachedCookies?: Cookies.Cookies
412 get cookies(): Cookies.Cookies {
413 if (this.cachedCookies !== undefined) {
414 return this.cachedCookies
415 }
416 const header = this.source.headers["set-cookie"]
417 return this.cachedCookies = header ? Cookies.fromSetCookie(header) : Cookies.empty
418 }
419
420 get remoteAddress(): Option.Option<string> {
421 return Option.none()
422 }
423
424 get stream(): Stream.Stream<Uint8Array, Error.HttpClientError> {
425 return NodeStream.fromReadable({
426 evaluate: () => this.source.body,
427 onError: (cause) =>
428 new Error.HttpClientError({
429 reason: new Error.DecodeError({
430 request: this.request,
431 response: this,
432 cause
433 })
434 })
435 })
436 }
437
438 get json(): Effect.Effect<Schema.Json, Error.HttpClientError> {
439 return Effect.flatMap(this.text, (text) =>
440 Effect.try({
441 try: () => text === "" ? null : JSON.parse(text),
442 catch: (cause) =>
443 new Error.HttpClientError({
444 reason: new Error.DecodeError({
445 request: this.request,
446 response: this,
447 cause
448 })
449 })
450 }))
451 }
452
453 private textBody?: Effect.Effect<string, Error.HttpClientError>
454 get text(): Effect.Effect<string, Error.HttpClientError> {
455 if (this.textBody) {
456 return this.textBody
457 }
458 this.textBody = Effect.map(this.arrayBuffer, (_) => new TextDecoder().decode(_))
459 return this.textBody
460 }
461
462 get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, Error.HttpClientError> {
463 return Effect.flatMap(this.arrayBuffer, (bytes) =>
464 Effect.try({
465 // A leading "?" is form data; URLSearchParams would drop it as a query prefix.
466 try: () => UrlParams.fromInput(new URLSearchParams(formDecoder.decode(bytes).replace(/^\?/, "%3F"))),
467 catch: (cause) =>
468 new Error.HttpClientError({
469 reason: new Error.DecodeError({
470 request: this.request,
471 response: this,
472 cause
473 })
474 })
475 }))
476 }
477
478 private formDataBody?: Effect.Effect<FormData, Error.HttpClientError>
479 get formData(): Effect.Effect<FormData, Error.HttpClientError> {
480 return this.formDataBody ??= Effect.flatMap(this.arrayBuffer, (body) =>
481 Effect.tryPromise({
482 try: () => new globalThis.Response(body, { headers: this.headers }).formData(),
483 catch: (cause) =>
484 new Error.HttpClientError({
485 reason: new Error.DecodeError({
486 request: this.request,
487 response: this,
488 cause
489 })
490 })
491 })).pipe(Effect.cached, Effect.runSync)
492 }
493
494 private arrayBufferBody?: Effect.Effect<ArrayBuffer, Error.HttpClientError>
495 get arrayBuffer(): Effect.Effect<ArrayBuffer, Error.HttpClientError> {
496 if (this.arrayBufferBody) {
497 return this.arrayBufferBody
498 }
499 this.arrayBufferBody = Effect.tryPromise({
500 try: () => this.source.body.arrayBuffer(),
501 catch: (cause) =>
502 new Error.HttpClientError({
503 reason: new Error.DecodeError({
504 request: this.request,
505 response: this,
506 cause
507 })
508 })
509 }).pipe(Effect.cached, Effect.runSync)
510 this.textBody = Effect.map(this.arrayBufferBody, (_) => new TextDecoder().decode(_))
511 return this.arrayBufferBody
512 }
513
514 get upgrade(): Effect.Effect<Socket.Socket, Error.HttpClientError> {
515 return upgradeSocket(this, this.source.upgraded)
516 }
517
518 toJSON(): unknown {
519 return IncomingMessage.inspect(this, {
520 _id: "effect/http/HttpClientResponse",
521 request: this.request.toJSON(),
522 status: this.status
523 })
524 }
525
526 pipe() {
527 return pipeArguments(this, arguments)
528 }
529 }
530
531 /**
532 * Provides an Undici-backed `HttpClient` using the current `Dispatcher`
533 * service.
534 *
535 * @stability unstable
536 * @category layers
537 * @since 4.0.0
538 */
539 export const layerUndiciNoDispatcher: Layer.Layer<
540 Client.HttpClient,
541 never,
542 Dispatcher
543 > = Client.layerMergedContext(makeUndici)
544
545 /**
546 * Provides an Undici-backed `HttpClient` together with a scoped default
547 * Undici `Agent` dispatcher.
548 *
549 * @stability unstable
550 * @category layers
551 * @since 4.0.0
552 */
553 export const layerUndici: Layer.Layer<Client.HttpClient> = Layer.provide(layerUndiciNoDispatcher, layerDispatcher)
554
555 /**
556 * Provides an Undici-backed `HttpClient` together with a scoped Undici
557 * `Agent` dispatcher configured with the supplied options.
558 *
559 * **When to use**
560 *
561 * Use to send every request over a unix domain socket with
562 * `{ connect: { socketPath } }`, or over TLS with a custom CA and client
563 * certificate with `{ connect: { ca, cert, key, servername, rejectUnauthorized } }`.
564 *
565 * **Details**
566 *
567 * See `makeDispatcherOptions` for how the options are applied.
568 *
569 * @stability unstable
570 * @category layers
571 * @since 4.0.5
572 */
573 export const layerUndiciOptions = (options?: Undici.Agent.Options | undefined): Layer.Layer<Client.HttpClient> =>
574 Layer.provide(layerUndiciNoDispatcher, layerDispatcherOptions(options))
575
576 // -----------------------------------------------------------------------------
577 // Upgrades
578 // -----------------------------------------------------------------------------
579
580 // Loaded on demand, as it depends on node:net and ws.
581 const loadUpgrade = Effect.promise(() => import("./internal/upgrade.ts"))
582
583 const takeOver = (conn: Duplex, head: Uint8Array | undefined, signal: AbortSignal): Duplex => {
584 if (head !== undefined && head.length > 0) conn.unshift(head)
585 conn.on("error", constVoid)
586 if (signal.aborted) {
587 conn.destroy()
588 } else {
589 const onAbort = () => conn.destroy()
590 signal.addEventListener("abort", onAbort, { once: true })
591 conn.once("close", () => signal.removeEventListener("abort", onAbort))
592 }
593 return conn
594 }
595
596 const upgradeSocket = (
597 response: HttpClientResponse,
598 upgraded: Duplex | undefined
599 ): Effect.Effect<Socket.Socket, Error.HttpClientError> => {
600 if (upgraded !== undefined) {
601 return Effect.flatMap(loadUpgrade, (_) => _.toSocket(upgraded))
602 }
603 return Effect.fail(
604 new Error.HttpClientError({
605 reason: response.status === 101
606 // Bun before 1.4 reports 101 without handing over the connection.
607 ? new Error.TransportError({
608 request: response.request,
609 description: "the runtime did not hand over the connection of the upgrade"
610 })
611 : new Error.StatusCodeError({
612 request: response.request,
613 response,
614 description: "expected 101 Switching Protocols"
615 })
616 })
617 )
618 }
619
620 // -----------------------------------------------------------------------------
621 // node:http
622 // -----------------------------------------------------------------------------
623
624 /**
625 * Service tag for the paired Node `http` and `https` agents used by the
626 * node:http-backed HTTP client.
627 *
628 * @stability unstable
629 * @category services
630 * @since 4.0.0
631 */
632 export class HttpAgent extends Context.Service<HttpAgent, {
633 readonly http: Http.Agent
634 readonly https: Https.Agent
635 }>()("@effect/platform-node/NodeHttpClient/HttpAgent") {}
636
637 /**
638 * Options for the Node `http` and `https` agents created by `makeAgent`.
639 *
640 * **Details**
641 *
642 * Extends `https.AgentOptions` with `socketPath`. Node agents merge their
643 * options into every request they create, so:
644 *
645 * - `socketPath` dials a unix domain socket (or a Windows named pipe) instead
646 * of TCP. The request URL still supplies the `Host` header and path.
647 * - `ca`, `cert`, `key`, `servername` and `rejectUnauthorized` configure TLS
648 * for `https:` URLs, including mutual TLS.
649 *
650 * Both agents receive the same options.
651 *
652 * @stability unstable
653 * @category models
654 * @since 4.0.5
655 */
656 export interface HttpAgentOptions extends Https.AgentOptions {
657 /**
658 * Path of a unix domain socket or Windows named pipe to connect through
659 * instead of TCP.
660 */
661 readonly socketPath?: string | undefined
662 }
663
664 /**
665 * Acquires Node `http` and `https` agents with the supplied options and
666 * destroys both agents when the enclosing scope is finalized.
667 *
668 * @stability unstable
669 * @category resource management
670 * @since 4.0.0
671 */
672 export const makeAgent = (options?: HttpAgentOptions): Effect.Effect<HttpAgent["Service"], never, Scope.Scope> =>
673 Effect.zipWith(
674 Effect.acquireRelease(
675 Effect.sync(() => new http.Agent(options)),
676 (agent) => Effect.sync(() => agent.destroy())
677 ),
678 Effect.acquireRelease(
679 Effect.sync(() => new https.Agent(options)),
680 (agent) => Effect.sync(() => agent.destroy())
681 ),
682 (http, https) => ({ http, https })
683 )
684
685 /**
686 * Provides the `HttpAgent` service using scoped Node `http` and `https`
687 * agents configured with the supplied options.
688 *
689 * @stability unstable
690 * @category layers
691 * @since 4.0.0
692 */
693 export const layerAgentOptions: (options?: HttpAgentOptions | undefined) => Layer.Layer<
694 HttpAgent
695 > = flow(makeAgent, Layer.effect(HttpAgent))
696
697 /**
698 * Provides the `HttpAgent` service using default scoped Node `http` and
699 * `https` agents.
700 *
701 * @stability unstable
702 * @category layers
703 * @since 4.0.0
704 */
705 export const layerAgent: Layer.Layer<HttpAgent> = layerAgentOptions()
706
707 /**
708 * Creates an `HttpClient` backed by Node `http` and `https`, using the
709 * current `HttpAgent`, streaming request bodies, and wrapping Node responses
710 * as `HttpClientResponse` values.
711 *
712 * @stability unstable
713 * @category constructors
714 * @since 4.0.0
715 */
716 export const makeNodeHttp = Effect.gen(function*() {
717 const agent = yield* HttpAgent
718 return Client.make((request, url, signal) => {
719 const nodeRequest = url.protocol === "https:" ?
720 https.request(url, {
721 agent: agent.https,
722 method: request.method,
723 headers: request.headers,
724 signal
725 }) :
726 http.request(url, {
727 agent: agent.http,
728 method: request.method,
729 headers: request.headers,
730 signal
731 })
732 // Response headers must not interrupt the upload: a bidirectional server
733 // answers before it has read the whole body. The upload runs detached and
734 // is stopped once the request or the response closes.
735 return sendBody(nodeRequest, request, request.body).pipe(
736 Effect.onError(() => Effect.sync(() => nodeRequest.destroy())),
737 Effect.forkDetach,
738 Effect.flatMap((upload) => {
739 const stopUpload = () => upload.interruptUnsafe()
740 nodeRequest.once("close", stopUpload)
741 return Effect.raceFirst(
742 waitForResponse(nodeRequest, request, signal),
743 Fiber.join(upload).pipe(Effect.andThen(Effect.never))
744 ).pipe(
745 Effect.map((result) => {
746 // A keep-alive request stays open while its upload is unfinished,
747 // even after the response has been fully consumed.
748 result[0].once("close", stopUpload)
749 return result
750 })
751 )
752 }),
753 Effect.onError(() => Effect.sync(() => nodeRequest.destroy())),
754 Effect.map(([response, upgraded]) => new NodeHttpResponse(request, response, url.href.split("#")[0], upgraded))
755 )
756 })
757 })
758
759 const sendBody = (
760 nodeRequest: Http.ClientRequest,
761 request: HttpClientRequest,
762 body: Body.HttpBody
763 ): Effect.Effect<void, Error.HttpClientError> =>
764 Effect.suspend((): Effect.Effect<void, Error.HttpClientError> => {
765 switch (body._tag) {
766 case "Empty": {
767 nodeRequest.end()
768 return waitForFinish(nodeRequest, request)
769 }
770 case "Uint8Array":
771 case "Raw": {
772 nodeRequest.end(body.body)
773 return waitForFinish(nodeRequest, request)
774 }
775 case "FormData": {
776 const { contentType, stream } = formDataBody(body.formData, request)
777 nodeRequest.setHeader("content-type", contentType)
778 return sendStream(nodeRequest, request, stream)
779 }
780 case "Stream": {
781 return sendStream(
782 nodeRequest,
783 request,
784 Stream.mapError(body.stream, (cause) =>
785 new Error.HttpClientError({
786 reason: new Error.EncodeError({
787 request,
788 cause
789 })
790 }))
791 )
792 }
793 }
794 })
795
796 const sendStream = (
797 nodeRequest: Http.ClientRequest,
798 request: HttpClientRequest,
799 stream: Stream.Stream<Uint8Array, Error.HttpClientError>
800 ): Effect.Effect<void, Error.HttpClientError> =>
801 Stream.run(
802 stream,
803 NodeSink.fromWritable({
804 evaluate: () => nodeRequest,
805 // The upload fiber owns destruction after observing body failures.
806 endOnDone: false,
807 onError: (cause) =>
808 new Error.HttpClientError({
809 reason: new Error.TransportError({
810 request,
811 cause
812 })
813 })
814 })
815 ).pipe(Effect.andThen(() => {
816 nodeRequest.end()
817 return waitForFinish(nodeRequest, request)
818 }))
819
820 /**
821 * Encodes form data as multipart with the global `Response`, so the encoding
822 * comes from the runtime instead of the external undici package.
823 *
824 * Some Node.js versions read every file eagerly when encoding and never report
825 * read failures. Files are therefore encoded as empty placeholders and their
826 * contents are streamed into the placeholder parts.
827 */
828 const formDataBody = (formData: FormData, request: HttpClientRequest) => {
829 const transportError = (cause: unknown) =>
830 new Error.HttpClientError({ reason: new Error.TransportError({ request, cause }) })
831 const entries = Array.from(formData)
832 const placeholders = new globalThis.FormData()
833 for (const [key, value] of entries) {
834 if (typeof value === "string") placeholders.append(key, value)
835 // A `Blob` with a file name, as the global `File` is missing on Node 18.
836 else placeholders.append(key, new Blob([], { type: value.type }), value.name)
837 }
838 const response = new globalThis.Response(placeholders)
839 const contentType = response.headers.get("content-type")!
840 const delimiter = `--${contentType.slice(contentType.indexOf("boundary=") + 9)}`
841 const stream = Stream.unwrap(Effect.map(Effect.promise(() => response.arrayBuffer()), (buffer) => {
842 const encoded = Buffer.from(buffer)
843 const streams: Array<Stream.Stream<Uint8Array, Error.HttpClientError>> = []
844 let start = 0
845 let delimiterAt = 0
846 for (const [, value] of entries) {
847 delimiterAt = encoded.indexOf(delimiter, delimiterAt + delimiter.length)
848 if (typeof value === "string") continue
849 // The file contents go before the CRLF that ends its empty placeholder part.
850 const end = delimiterAt - 2
851 streams.push(
852 Stream.succeed(encoded.subarray(start, end)),
853 Stream.unwrap(Effect.map(
854 Effect.try({ try: () => value.stream(), catch: transportError }),
855 (readable) => Stream.fromReadableStream({ evaluate: () => readable, onError: transportError })
856 ))
857 )
858 start = end
859 }
860 streams.push(Stream.succeed(encoded.subarray(start)))
861 return Stream.flatten(Stream.fromArray(streams))
862 }))
863 return { contentType, stream } as const
864 }
865
866 const waitForResponse = (nodeRequest: Http.ClientRequest, request: HttpClientRequest, signal: AbortSignal) =>
867 Effect.callback<readonly [Http.IncomingMessage, Duplex?], Error.HttpClientError>((resume) => {
868 function onError(cause: Error) {
869 resume(Effect.fail(
870 new Error.HttpClientError({
871 reason: new Error.TransportError({
872 request,
873 cause
874 })
875 })
876 ))
877 }
878 nodeRequest.on("error", onError)
879
880 function onResponse(response: Http.IncomingMessage) {
881 nodeRequest.off("error", onError)
882 resume(Effect.succeed([response]))
883 }
884 nodeRequest.on("response", onResponse)
885
886 function onUpgrade(response: Http.IncomingMessage, socket: Duplex, head: Buffer) {
887 nodeRequest.off("error", onError)
888 resume(Effect.succeed([response, takeOver(socket, head, signal)]))
889 }
890 nodeRequest.on("upgrade", onUpgrade)
891
892 return Effect.sync(() => {
893 nodeRequest.off("error", onError)
894 nodeRequest.off("upgrade", onUpgrade)
895 nodeRequest.off("response", onResponse)
896 })
897 })
898
899 const waitForFinish = (nodeRequest: Http.ClientRequest, request: HttpClientRequest) =>
900 Effect.callback<void, Error.HttpClientError>((resume) => {
901 function onError(cause: Error) {
902 resume(Effect.fail(
903 new Error.HttpClientError({
904 reason: new Error.TransportError({
905 request,
906 cause
907 })
908 })
909 ))
910 }
911 nodeRequest.once("error", onError)
912
913 function onFinish() {
914 nodeRequest.off("error", onError)
915 resume(Effect.void)
916 }
917 nodeRequest.once("finish", onFinish)
918
919 return Effect.sync(() => {
920 nodeRequest.off("error", onError)
921 nodeRequest.off("finish", onFinish)
922 })
923 })
924
925 class NodeHttpResponse extends NodeHttpIncomingMessage<Error.HttpClientError> implements HttpClientResponse, Pipeable {
926 readonly [Response.TypeId]: typeof Response.TypeId
927 readonly request: HttpClientRequest
928 readonly url: string
929 readonly upgraded: Duplex | undefined
930
931 constructor(
932 request: HttpClientRequest,
933 source: Http.IncomingMessage,
934 url: string,
935 upgraded: Duplex | undefined
936 ) {
937 super(source, (cause) =>
938 new Error.HttpClientError({
939 reason: new Error.DecodeError({
940 request,
941 response: this,
942 cause
943 })
944 }))
945 this[Response.TypeId] = Response.TypeId
946 this.request = request
947 this.url = url
948 this.upgraded = upgraded
949 }
950
951 get status() {
952 return this.source.statusCode!
953 }
954
955 get upgrade(): Effect.Effect<Socket.Socket, Error.HttpClientError> {
956 return upgradeSocket(this, this.upgraded)
957 }
958
959 cachedCookies?: Cookies.Cookies
960 get cookies(): Cookies.Cookies {
961 if (this.cachedCookies !== undefined) {
962 return this.cachedCookies
963 }
964 const header = this.source.headers["set-cookie"]
965 return this.cachedCookies = header ? Cookies.fromSetCookie(header) : Cookies.empty
966 }
967
968 get formData(): Effect.Effect<FormData, Error.HttpClientError> {
969 return Effect.tryPromise({
970 try: () => {
971 const init: {
972 headers: HeadersInit
973 status?: number
974 statusText?: string
975 } = {
976 headers: new globalThis.Headers(this.source.headers as any)
977 }
978
979 if (this.source.statusCode) {
980 init.status = this.source.statusCode
981 }
982
983 if (this.source.statusMessage) {
984 init.statusText = this.source.statusMessage
985 }
986
987 return new globalThis.Response(Readable.toWeb(this.source) as any, init).formData()
988 },
989 catch: this.onError
990 })
991 }
992
993 toJSON(): unknown {
994 return IncomingMessage.inspect(this, {
995 _id: "effect/http/HttpClientResponse",
996 request: this.request.toJSON(),
997 status: this.status
998 })
999 }
1000
1001 pipe() {
1002 return pipeArguments(this, arguments)
1003 }
1004 }
1005
1006 /**
1007 * Provides a node:http-backed `HttpClient` using the current `HttpAgent`
1008 * service.
1009 *
1010 * @stability unstable
1011 * @category layers
1012 * @since 4.0.0
1013 */
1014 export const layerNodeHttpNoAgent: Layer.Layer<
1015 Client.HttpClient,
1016 never,
1017 HttpAgent
1018 > = Client.layerMergedContext(makeNodeHttp)
1019
1020 /**
1021 * Provides a node:http-backed `HttpClient` together with default scoped Node
1022 * `http` and `https` agents.
1023 *
1024 * @stability unstable
1025 * @category layers
1026 * @since 4.0.0
1027 */
1028 export const layerNodeHttp: Layer.Layer<Client.HttpClient> = Layer.provide(layerNodeHttpNoAgent, layerAgent)
1029
1030 /**
1031 * Provides a node:http-backed `HttpClient` together with scoped Node `http`
1032 * and `https` agents configured with the supplied options.
1033 *
1034 * **When to use**
1035 *
1036 * Use to send every request over a unix domain socket with `{ socketPath }`,
1037 * or over TLS with a custom CA and client certificate with
1038 * `{ ca, cert, key, servername, rejectUnauthorized }`.
1039 *
1040 * **Details**
1041 *
1042 * See `HttpAgentOptions` for how the options are applied.
1043 *
1044 * @stability unstable
1045 * @category layers
1046 * @since 4.0.5
1047 */
1048 export const layerNodeHttpOptions = (options?: HttpAgentOptions | undefined): Layer.Layer<Client.HttpClient> =>
1049 Layer.provide(layerNodeHttpNoAgent, layerAgentOptions(options))