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
| 1 | /** |
| 2 | * Node.js implementation of the Effect HTTP platform service. |
| 3 | * |
| 4 | * This module connects the portable `HttpPlatform` file response helpers to |
| 5 | * Node runtime primitives. It serves local files through Node readable streams, |
| 6 | * supports byte ranges, converts Web `File` values to readable streams, and |
| 7 | * fills in content type and content length headers when needed. |
| 8 | * |
| 9 | * @stability unstable |
| 10 | * @since 4.0.0 |
| 11 | */ |
| 12 | import * as NodeHttpCompression from "@effect/platform-node-shared/NodeHttpCompression" |
| 13 | import * as Effect from "effect/Effect" |
| 14 | import { pipe } from "effect/Function" |
| 15 | import * as EtagImpl from "effect/http/Etag" |
| 16 | import * as Headers from "effect/http/Headers" |
| 17 | import * as HttpBody from "effect/http/HttpBody" |
| 18 | import * as Platform from "effect/http/HttpPlatform" |
| 19 | import * as ServerResponse from "effect/http/HttpServerResponse" |
| 20 | import * as Mime from "effect/http/Mime" |
| 21 | import * as Layer from "effect/Layer" |
| 22 | import * as Option from "effect/Option" |
| 23 | import * as Fs from "node:fs" |
| 24 | import { pipeline, Readable } from "node:stream" |
| 25 | import * as NodeFileSystem from "./NodeFileSystem.ts" |
| 26 | import * as NodeStream from "./NodeStream.ts" |
| 27 | |
| 28 | // Keep the native Raw/pipeline path without opening discarded response bodies. |
| 29 | const lazyReadable = (evaluate: () => Readable, onDestroy?: () => void): Readable => { |
| 30 | let source: Readable | undefined |
| 31 | return new Readable({ |
| 32 | read() { |
| 33 | if (source === undefined) { |
| 34 | try { |
| 35 | source = evaluate() |
| 36 | } catch (cause) { |
| 37 | this.destroy(cause instanceof Error ? cause : new Error(String(cause))) |
| 38 | return |
| 39 | } |
| 40 | source.on("data", (chunk) => { |
| 41 | if (!this.push(chunk)) source!.pause() |
| 42 | }) |
| 43 | source.once("end", () => this.push(null)) |
| 44 | source.once("error", (cause) => this.destroy(cause)) |
| 45 | source.once("close", () => { |
| 46 | if (!source!.readableEnded) this.destroy(new Error("Response stream closed prematurely")) |
| 47 | }) |
| 48 | } |
| 49 | source.resume() |
| 50 | }, |
| 51 | destroy(error, callback) { |
| 52 | source?.destroy(error ?? undefined) |
| 53 | onDestroy?.() |
| 54 | callback(error) |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | // replaces the response body while keeping every other field, dropping the |
| 60 | // now-stale Content-Length header |
| 61 | const compressedBody = ( |
| 62 | response: ServerResponse.HttpServerResponse, |
| 63 | body: HttpBody.HttpBody |
| 64 | ): ServerResponse.HttpServerResponse => |
| 65 | ServerResponse.removeHeader(ServerResponse.setBody(response, body), "content-length") |
| 66 | |
| 67 | const compression = NodeHttpCompression.make({ |
| 68 | algorithms: NodeHttpCompression.algorithms, |
| 69 | compressResponse(response, algorithm, options) { |
| 70 | const body = response.body |
| 71 | switch (body._tag) { |
| 72 | case "Stream": { |
| 73 | return Effect.succeed(compressedBody( |
| 74 | response, |
| 75 | HttpBody.stream( |
| 76 | NodeStream.pipeThroughDuplex(body.stream, { |
| 77 | evaluate: () => NodeHttpCompression.compressTransform(algorithm, options) |
| 78 | }), |
| 79 | response.headers["content-type"] ?? body.contentType |
| 80 | ) |
| 81 | )) |
| 82 | } |
| 83 | case "Raw": { |
| 84 | let readable = body.body instanceof Readable ? body.body : undefined |
| 85 | const compressed = lazyReadable(() => { |
| 86 | const transform = NodeHttpCompression.compressTransform(algorithm, options) |
| 87 | readable ??= Readable.fromWeb(new Response(body.body as BodyInit).body as any) |
| 88 | return pipeline(readable, transform, (cause) => { |
| 89 | if (cause) compressed.destroy(cause) |
| 90 | }) |
| 91 | }, () => readable?.destroy()) |
| 92 | return Effect.succeed( |
| 93 | compressedBody( |
| 94 | response, |
| 95 | HttpBody.raw(compressed, { |
| 96 | contentType: response.headers["content-type"] ?? body.contentType |
| 97 | }) |
| 98 | ) |
| 99 | ) |
| 100 | } |
| 101 | default: { |
| 102 | return Effect.succeed(response) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | }) |
| 107 | |
| 108 | /** |
| 109 | * Creates the Node `HttpPlatform`, serving file responses from Node readable |
| 110 | * streams and adding MIME type and content-length headers when needed. |
| 111 | * |
| 112 | * @stability unstable |
| 113 | * @category constructors |
| 114 | * @since 4.0.0 |
| 115 | */ |
| 116 | export const make = Platform.make({ |
| 117 | platform: "node", |
| 118 | compression, |
| 119 | fileResponse(path, status, statusText, headers, start, end, contentLength) { |
| 120 | const stream = lazyReadable(() => |
| 121 | contentLength === BigInt(0) |
| 122 | ? Readable.from([]) |
| 123 | : Fs.createReadStream(path, { start, end: end === undefined ? undefined : end - 1 }) |
| 124 | ) |
| 125 | return ServerResponse.raw(stream, { |
| 126 | headers: { |
| 127 | ...headers, |
| 128 | "content-type": headers["content-type"] ?? |
| 129 | Option.getOrElse(Mime.getType(path), () => "application/octet-stream"), |
| 130 | "content-length": contentLength.toString() |
| 131 | }, |
| 132 | status, |
| 133 | statusText |
| 134 | }) |
| 135 | }, |
| 136 | fileWebResponse(file, status, statusText, headers, _options) { |
| 137 | return ServerResponse.raw( |
| 138 | lazyReadable(() => Readable.fromWeb(file.stream() as any)), |
| 139 | { |
| 140 | headers: Headers.merge( |
| 141 | headers, |
| 142 | Headers.fromRecordUnsafe({ |
| 143 | "content-type": headers["content-type"] ?? |
| 144 | (file.type === "" |
| 145 | ? Option.getOrElse(Mime.getType(file.name), () => "application/octet-stream") |
| 146 | : file.type), |
| 147 | "content-length": file.size.toString() |
| 148 | }) |
| 149 | ), |
| 150 | status, |
| 151 | statusText |
| 152 | } |
| 153 | ) |
| 154 | } |
| 155 | }) |
| 156 | |
| 157 | /** |
| 158 | * Provides the Node `HttpPlatform` together with the filesystem and ETag |
| 159 | * services it needs for file responses. |
| 160 | * |
| 161 | * @stability unstable |
| 162 | * @category layers |
| 163 | * @since 4.0.0 |
| 164 | */ |
| 165 | export const layer: Layer.Layer<Platform.HttpPlatform> = pipe( |
| 166 | Layer.effect(Platform.HttpPlatform)(make), |
| 167 | Layer.provide(NodeFileSystem.layer), |
| 168 | Layer.provide(EtagImpl.layer) |
| 169 | ) |