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 { NodeHttpServer } from "@effect/platform-node" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import { ByteSize, Effect, Layer, Stream } from "effect" |
| 4 | import { |
| 5 | HttpBody, |
| 6 | HttpClient, |
| 7 | type HttpClientRequest, |
| 8 | HttpClientResponse, |
| 9 | HttpIncomingMessage, |
| 10 | HttpRouter, |
| 11 | HttpServer |
| 12 | } from "effect/http" |
| 13 | import type * as NetAddress from "effect/net/NetAddress" |
| 14 | import { OtlpProxy } from "effect/observability" |
| 15 | import * as Http from "node:http" |
| 16 | |
| 17 | describe("OtlpProxy on NodeHttpServer", () => { |
| 18 | it.effect("applies an explicit maxBodySize above the ambient MaxBodySize to declared and chunked bodies", () => |
| 19 | Effect.gen(function*() { |
| 20 | const forwarded: Array<HttpClientRequest.HttpClientRequest> = [] |
| 21 | const upstream = HttpClient.make((request) => { |
| 22 | forwarded.push(request) |
| 23 | return Effect.succeed(HttpClientResponse.fromWeb(request, new Response())) |
| 24 | }) |
| 25 | yield* OtlpProxy.layer({ baseUrl: "http://upstream.example", maxBodySize: 8 }).pipe( |
| 26 | Layer.provide(Layer.succeed(HttpClient.HttpClient, upstream)), |
| 27 | (proxy) => |
| 28 | HttpRouter.serve(proxy, { |
| 29 | disableLogger: true, |
| 30 | middleware: Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(4)) |
| 31 | }), |
| 32 | Layer.build |
| 33 | ) |
| 34 | const client = yield* HttpClient.HttpClient |
| 35 | const bytes = new Uint8Array([1, 2, 3, 4, 5, 6]) |
| 36 | |
| 37 | const declared = yield* client.post("/v1/traces", { |
| 38 | body: HttpBody.uint8Array(bytes, "application/x-protobuf") |
| 39 | }) |
| 40 | const chunked = yield* client.post("/v1/traces", { |
| 41 | body: HttpBody.stream(Stream.make(bytes), "application/x-protobuf") |
| 42 | }) |
| 43 | |
| 44 | assert.deepStrictEqual([declared.status, chunked.status], [200, 200]) |
| 45 | assert.strictEqual(forwarded.length, 2) |
| 46 | }).pipe(Effect.provide(NodeHttpServer.layerTest))) |
| 47 | |
| 48 | it.effect.each([ |
| 49 | { name: "", headers: {} }, |
| 50 | { name: " with Expect: 100-continue", headers: { expect: "100-continue" } } |
| 51 | ])( |
| 52 | "rejects an over-limit content-length before the body arrives$name", |
| 53 | ({ headers }) => |
| 54 | Effect.gen(function*() { |
| 55 | const forwarded: Array<HttpClientRequest.HttpClientRequest> = [] |
| 56 | const upstream = HttpClient.make((request) => { |
| 57 | forwarded.push(request) |
| 58 | return Effect.succeed(HttpClientResponse.fromWeb(request, new Response())) |
| 59 | }) |
| 60 | yield* OtlpProxy.layer({ baseUrl: "http://upstream.example", maxBodySize: 4 }).pipe( |
| 61 | Layer.provide(Layer.succeed(HttpClient.HttpClient, upstream)), |
| 62 | (proxy) => HttpRouter.serve(proxy, { disableLogger: true }), |
| 63 | Layer.build |
| 64 | ) |
| 65 | const server = yield* HttpServer.HttpServer |
| 66 | const port = (server.address as NetAddress.InetAddress).port |
| 67 | |
| 68 | const response = yield* headersOnlyRequest(port, headers) |
| 69 | assert.deepStrictEqual(response, { status: 413, continues: 0 }) |
| 70 | assert.strictEqual(forwarded.length, 0) |
| 71 | }).pipe(Effect.provide(NodeHttpServer.layerTest)) |
| 72 | ) |
| 73 | }) |
| 74 | |
| 75 | // Declares a 100-byte body but never sends it, so only the headers can be rejected. |
| 76 | const headersOnlyRequest = (port: number, headers: Http.OutgoingHttpHeaders) => |
| 77 | Effect.callback<{ status: number | undefined; continues: number }, Error>((resume) => { |
| 78 | let continues = 0 |
| 79 | const request = Http.request({ |
| 80 | host: "127.0.0.1", |
| 81 | port, |
| 82 | path: "/v1/traces", |
| 83 | method: "POST", |
| 84 | headers: { |
| 85 | "content-type": "application/x-protobuf", |
| 86 | "content-length": 100, |
| 87 | ...headers |
| 88 | }, |
| 89 | agent: false |
| 90 | }, (response) => { |
| 91 | response.on("error", (error) => resume(Effect.fail(error))) |
| 92 | response.on("end", () => resume(Effect.succeed({ status: response.statusCode, continues }))) |
| 93 | response.resume() |
| 94 | }) |
| 95 | request.on("continue", () => { |
| 96 | continues++ |
| 97 | }) |
| 98 | request.on("error", (error) => resume(Effect.fail(error))) |
| 99 | request.setTimeout(3000, () => request.destroy(new Error("no response before the body was sent"))) |
| 100 | request.flushHeaders() |
| 101 | return Effect.sync(() => request.destroy()) |
| 102 | }) |