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 | import { assert, describe, it } from "@effect/vitest" |
| 2 | import * as Effect from "effect/Effect" |
| 3 | import * as Exit from "effect/Exit" |
| 4 | import { constVoid } from "effect/Function" |
| 5 | import * as HttpClient from "effect/http/HttpClient" |
| 6 | import * as HttpClientError from "effect/http/HttpClientError" |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest" |
| 8 | import * as HttpClientResponse from "effect/http/HttpClientResponse" |
| 9 | import * as HttpServer from "effect/http/HttpServer" |
| 10 | import * as HttpServerRequest from "effect/http/HttpServerRequest" |
| 11 | import * as HttpServerResponse from "effect/http/HttpServerResponse" |
| 12 | import * as Latch from "effect/Latch" |
| 13 | import * as Layer from "effect/Layer" |
| 14 | import type * as NetAddress from "effect/net/NetAddress" |
| 15 | import * as Option from "effect/Option" |
| 16 | import * as Stream from "effect/Stream" |
| 17 | import * as Http2 from "node:http2" |
| 18 | import { Readable } from "node:stream" |
| 19 | |
| 20 | /** |
| 21 | * Runs tests of a platform `HttpServer` over one protocol. The layer provides |
| 22 | * the server and an `HttpClient` that reaches it over that protocol. |
| 23 | */ |
| 24 | export const describeHttpServerProtocol = ( |
| 25 | label: string, |
| 26 | layer: Layer.Layer<HttpServer.HttpServer | HttpClient.HttpClient, unknown> |
| 27 | ) => |
| 28 | describe(label, () => { |
| 29 | it.effect("exposes the request URL to web APIs", () => |
| 30 | Effect.gen(function*() { |
| 31 | yield* HttpServer.serveEffect( |
| 32 | HttpServerRequest.HttpServerRequest.use((request) => |
| 33 | Effect.map( |
| 34 | HttpServerRequest.toWeb(request), |
| 35 | (web) => HttpServerResponse.text(`${Option.getOrThrow(HttpServerRequest.toURL(request)).href} ${web.url}`) |
| 36 | ) |
| 37 | ) |
| 38 | ) |
| 39 | const response = yield* HttpClient.get("/path") |
| 40 | assert.strictEqual(yield* response.text, `${response.request.url} ${response.request.url}`) |
| 41 | }).pipe(Effect.provide(layer))) |
| 42 | |
| 43 | it.effect("answers a response that carries connection-specific headers", () => |
| 44 | Effect.gen(function*() { |
| 45 | yield* HttpServer.serveEffect( |
| 46 | Effect.succeed(HttpServerResponse.text("ok", { headers: { "keep-alive": "timeout=5" } })) |
| 47 | ) |
| 48 | const response = yield* HttpClient.get("/") |
| 49 | assert.strictEqual(response.status, 200) |
| 50 | assert.strictEqual(yield* response.text, "ok") |
| 51 | }).pipe(Effect.provide(layer))) |
| 52 | |
| 53 | it.effect("fails the response body when the body stream fails after the first chunk", () => |
| 54 | Effect.gen(function*() { |
| 55 | const headersReceived = Latch.makeUnsafe() |
| 56 | yield* HttpServer.serveEffect( |
| 57 | Effect.succeed(HttpServerResponse.stream(Stream.concat( |
| 58 | Stream.make(new TextEncoder().encode("first")), |
| 59 | Stream.fromEffect(Effect.andThen(headersReceived.await, Effect.fail(new Error("boom")))) |
| 60 | ))) |
| 61 | ) |
| 62 | const response = yield* HttpClient.get("/") |
| 63 | headersReceived.openUnsafe() |
| 64 | assert.isTrue(Exit.isFailure(yield* Effect.exit(response.text))) |
| 65 | }).pipe(Effect.provide(layer))) |
| 66 | }) |
| 67 | |
| 68 | /** |
| 69 | * Prefixes requests with the loopback origin of the current server, using the |
| 70 | * given scheme. |
| 71 | */ |
| 72 | export const layerTestClient = ( |
| 73 | scheme: "http" | "https" |
| 74 | ): Layer.Layer<HttpClient.HttpClient, never, HttpServer.HttpServer | HttpClient.HttpClient> => |
| 75 | Layer.effect(HttpClient.HttpClient)(Effect.gen(function*() { |
| 76 | const server = yield* HttpServer.HttpServer |
| 77 | const client = yield* HttpClient.HttpClient |
| 78 | const { port } = server.address as NetAddress.InetAddress |
| 79 | return HttpClient.mapRequest(client, HttpClientRequest.prependUrl(`${scheme}://127.0.0.1:${port}`)) |
| 80 | })) |
| 81 | |
| 82 | /** |
| 83 | * An `HttpClient` that sends each request over its own `node:http2` session. |
| 84 | * Pass `ca` to trust a test certificate for `https` URLs. |
| 85 | */ |
| 86 | export const layerHttp2Client = (options?: Http2.SecureClientSessionOptions): Layer.Layer<HttpClient.HttpClient> => |
| 87 | Layer.succeed(HttpClient.HttpClient)( |
| 88 | HttpClient.make((request, url) => |
| 89 | Effect.callback<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError>((resume) => { |
| 90 | const fail = (cause: unknown) => |
| 91 | resume(Effect.fail( |
| 92 | new HttpClientError.HttpClientError({ reason: new HttpClientError.TransportError({ request, cause }) }) |
| 93 | )) |
| 94 | const session = Http2.connect(url.origin, options) |
| 95 | session.on("error", constVoid) |
| 96 | const stream = session.request({ |
| 97 | ...request.headers, |
| 98 | ":method": request.method, |
| 99 | ":path": url.pathname + url.search |
| 100 | }) |
| 101 | stream.on("response", (headers) => { |
| 102 | const responseHeaders = new Headers() |
| 103 | for (const [name, value] of Object.entries(headers)) { |
| 104 | if (name[0] !== ":" && value !== undefined) responseHeaders.set(name, String(value)) |
| 105 | } |
| 106 | resume(Effect.succeed(HttpClientResponse.fromWeb( |
| 107 | request, |
| 108 | new Response(Readable.toWeb(stream) as unknown as ReadableStream<Uint8Array>, { |
| 109 | status: headers[":status"]!, |
| 110 | headers: responseHeaders |
| 111 | }) |
| 112 | ))) |
| 113 | }) |
| 114 | stream.on("error", fail) |
| 115 | stream.on("close", () => { |
| 116 | session.close() |
| 117 | fail(new Error(`HTTP/2 stream closed with code ${stream.rstCode}`)) |
| 118 | }) |
| 119 | stream.end(request.body._tag === "Uint8Array" ? request.body.body : undefined) |
| 120 | return Effect.sync(() => session.destroy()) |
| 121 | }) |
| 122 | ) |
| 123 | ) |
| 124 | |
| 125 | /** |
| 126 | * Sends one HTTP/2 request with the given pseudo-headers to a cleartext server |
| 127 | * and returns the response body. |
| 128 | */ |
| 129 | export const http2Get = (port: number, headers: Http2.OutgoingHttpHeaders) => |
| 130 | Effect.acquireUseRelease( |
| 131 | Effect.sync(() => Http2.connect(`http://127.0.0.1:${port}`)), |
| 132 | (session) => |
| 133 | Effect.callback<string, Error>((resume) => { |
| 134 | const stream = session.request(headers) |
| 135 | let body = "" |
| 136 | stream.setEncoding("utf8") |
| 137 | stream.on("data", (chunk: string) => { |
| 138 | body += chunk |
| 139 | }) |
| 140 | stream.on("end", () => resume(Effect.succeed(body))) |
| 141 | stream.on("error", (error) => resume(Effect.fail(error))) |
| 142 | }), |
| 143 | (session) => Effect.callback<void>((resume) => session.close(() => resume(Effect.void))) |
| 144 | ) |