The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect" |
| 2 | import * as Socket from "effect/socket/Socket" |
| 3 | import type * as Net from "node:net" |
| 4 | import { type Duplex, finished, Readable } from "node:stream" |
| 5 | import * as NodeSocket from "../NodeSocket.ts" |
| 6 | |
| 7 | /** |
| 8 | * Wraps an upgraded connection. Acquiring the reader runs `open`; releasing |
| 9 | * its scope closes the connection. |
| 10 | * |
| 11 | * No `data` listener is attached before acquisition, so early bytes stay buffered. |
| 12 | * |
| 13 | * @internal |
| 14 | */ |
| 15 | export const toSocket = ( |
| 16 | conn: Duplex, |
| 17 | open: Effect.Effect<void, Socket.SocketError> = Effect.void |
| 18 | ): Effect.Effect<Socket.Socket> => |
| 19 | NodeSocket.fromDuplex( |
| 20 | Effect.acquireRelease( |
| 21 | Effect.suspend(() => |
| 22 | conn.destroyed |
| 23 | ? Effect.fail(openError("The upgraded connection closed before it was read")) |
| 24 | : Effect.as(open, conn) |
| 25 | ), |
| 26 | (conn) => |
| 27 | Effect.sync(() => { |
| 28 | if (conn.destroyed) return |
| 29 | if ("destroySoon" in conn) { |
| 30 | ;(conn as Net.Socket).destroySoon() |
| 31 | } else { |
| 32 | conn.destroy() |
| 33 | } |
| 34 | }) |
| 35 | ) |
| 36 | ) |
| 37 | |
| 38 | const openError = (cause: unknown) => |
| 39 | new Socket.SocketError({ |
| 40 | reason: new Socket.SocketOpenError({ |
| 41 | kind: "Unknown", |
| 42 | cause: typeof cause === "string" ? new Error(cause) : cause |
| 43 | }) |
| 44 | }) |
| 45 | |
| 46 | /** |
| 47 | * Node 25 and older stop parsing at the head of an upgrade request, leaving |
| 48 | * the body in `head` and on the connection. Reads a `Content-Length` body |
| 49 | * from them on demand and returns the bytes after it. |
| 50 | * |
| 51 | * @internal |
| 52 | */ |
| 53 | export const frameBody = (conn: Duplex, head: Buffer, length: number): { |
| 54 | readonly body: Readable |
| 55 | readonly rest: Buffer |
| 56 | } => { |
| 57 | if (head.length >= length) { |
| 58 | return { |
| 59 | body: Readable.from(length === 0 ? [] : [head.subarray(0, length)], { objectMode: false }), |
| 60 | rest: head.subarray(length) |
| 61 | } |
| 62 | } |
| 63 | let remaining = length - head.length |
| 64 | let reading = false |
| 65 | const stop = () => { |
| 66 | conn.off("readable", onReadable) |
| 67 | conn.off("end", onEnd) |
| 68 | } |
| 69 | const onReadable = () => { |
| 70 | while (remaining > 0) { |
| 71 | let chunk = conn.read() as Buffer | null |
| 72 | if (chunk === null) return |
| 73 | if (chunk.length > remaining) { |
| 74 | conn.unshift(chunk.subarray(remaining)) |
| 75 | chunk = chunk.subarray(0, remaining) |
| 76 | } |
| 77 | remaining -= chunk.length |
| 78 | if (!body.push(chunk) && remaining > 0) return |
| 79 | } |
| 80 | stop() |
| 81 | body.push(null) |
| 82 | } |
| 83 | const onEnd = () => { |
| 84 | stop() |
| 85 | body.destroy(new Error("The connection closed before the request body ended")) |
| 86 | } |
| 87 | const body = new Readable({ |
| 88 | read() { |
| 89 | if (reading) return onReadable() |
| 90 | reading = true |
| 91 | // Pushed on first read, so the connection is not read before the body is. |
| 92 | if (head.length > 0) this.push(head) |
| 93 | // A half-closed connection may have ended before the body is read. |
| 94 | if (conn.readableEnded) return onEnd() |
| 95 | conn.on("readable", onReadable) |
| 96 | conn.on("end", onEnd) |
| 97 | }, |
| 98 | destroy(error, callback) { |
| 99 | stop() |
| 100 | callback(error) |
| 101 | } |
| 102 | }) |
| 103 | // Readers get the error through their own listeners; with none, it must not crash the process. |
| 104 | body.on("error", () => {}) |
| 105 | return { body, rest: Buffer.alloc(0) } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Waits for the request body to end before the `101` is written, discarding |
| 110 | * any of it the handler did not read, up to `maxBytes`. |
| 111 | * |
| 112 | * @internal |
| 113 | */ |
| 114 | export const drainBody = (body: Readable, maxBytes: number | undefined): Effect.Effect<void, Socket.SocketError> => |
| 115 | Effect.callback((resume) => { |
| 116 | if (body.readableEnded) return resume(Effect.void) |
| 117 | let bytes = 0 |
| 118 | const cleanup = finished(body, { readable: true, writable: false }, (error) => { |
| 119 | done() |
| 120 | resume(error ? Effect.fail(openError(error)) : Effect.void) |
| 121 | }) |
| 122 | function onData(chunk: Uint8Array) { |
| 123 | bytes += chunk.length |
| 124 | if (maxBytes !== undefined && bytes > maxBytes) { |
| 125 | done() |
| 126 | resume(Effect.fail(openError("The request body exceeds the maximum body size"))) |
| 127 | } |
| 128 | } |
| 129 | function done() { |
| 130 | body.off("data", onData) |
| 131 | cleanup() |
| 132 | } |
| 133 | body.on("data", onData) |
| 134 | // A body paused by an earlier partial read must flow again. |
| 135 | body.resume() |
| 136 | return Effect.sync(done) |
| 137 | }) |