The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics globalTimers:off - vendored upstream protocol, not an Effect service. |
| 2 | import { Buffer } from 'node:buffer'; |
| 3 | import { badRequest, HttpError, tooLarge } from './errors.ts'; |
| 4 | |
| 5 | export class Deadline { |
| 6 | private controller = new AbortController(); |
| 7 | private timer: ReturnType<typeof setTimeout>; |
| 8 | private cancelled: Promise<never>; |
| 9 | private onAbort = () => this.controller.abort(); |
| 10 | constructor( |
| 11 | ms: number, |
| 12 | private signal?: AbortSignal, |
| 13 | ) { |
| 14 | this.timer = setTimeout(this.onAbort, ms); |
| 15 | signal?.addEventListener('abort', this.onAbort, { once: true }); |
| 16 | this.cancelled = new Promise((_, reject) => |
| 17 | this.controller.signal.addEventListener( |
| 18 | 'abort', |
| 19 | () => reject(new HttpError(503, 'deadline')), |
| 20 | { once: true }, |
| 21 | ), |
| 22 | ); |
| 23 | // Cancellation can precede the first awaited operation. |
| 24 | void this.cancelled.catch(() => {}); |
| 25 | if (signal?.aborted) this.onAbort(); |
| 26 | } |
| 27 | check(): void { |
| 28 | if (this.controller.signal.aborted) throw new HttpError(503, 'deadline'); |
| 29 | } |
| 30 | async run<T>(operation: Promise<T>): Promise<T> { |
| 31 | this.check(); |
| 32 | return Promise.race([operation, this.cancelled]); |
| 33 | } |
| 34 | dispose(): void { |
| 35 | clearTimeout(this.timer); |
| 36 | this.signal?.removeEventListener('abort', this.onAbort); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | export async function collect( |
| 41 | source: AsyncIterable<Uint8Array>, |
| 42 | limit: number, |
| 43 | ): Promise<Uint8Array<ArrayBuffer>> { |
| 44 | let size = 0; |
| 45 | // Fixed-capacity accumulation also bounds bookkeeping for one-byte chunks. |
| 46 | const buffer = new Uint8Array(limit); |
| 47 | for await (const chunk of source) { |
| 48 | if (chunk.length > limit - size) tooLarge(); |
| 49 | buffer.set(chunk, size); |
| 50 | size += chunk.length; |
| 51 | } |
| 52 | return buffer.subarray(0, size); |
| 53 | } |
| 54 | |
| 55 | export class Input { |
| 56 | private reader: ReadableStreamDefaultReader<Uint8Array>; |
| 57 | private pending: Uint8Array = new Uint8Array(0); |
| 58 | private offset = 0; |
| 59 | bytes = 0; |
| 60 | ended = false; |
| 61 | buffer: Buffer = Buffer.alloc(0); |
| 62 | constructor( |
| 63 | body: ReadableStream<Uint8Array> | null, |
| 64 | private max: number, |
| 65 | private deadline: Deadline, |
| 66 | ) { |
| 67 | if (!body) badRequest(); |
| 68 | this.reader = body.getReader(); |
| 69 | } |
| 70 | async fill(): Promise<boolean> { |
| 71 | if (this.ended) return false; |
| 72 | if (this.offset === this.pending.length) { |
| 73 | const result = await this.deadline.run(this.reader.read()); |
| 74 | if (result.done) { |
| 75 | this.ended = true; |
| 76 | return false; |
| 77 | } |
| 78 | this.bytes += result.value.length; |
| 79 | if (this.bytes > this.max) tooLarge(); |
| 80 | this.pending = result.value; |
| 81 | this.offset = 0; |
| 82 | } |
| 83 | const next = this.pending.subarray(this.offset, this.offset + 64 * 1024); |
| 84 | this.offset += next.length; |
| 85 | this.buffer = Buffer.concat([this.buffer, next]); |
| 86 | return true; |
| 87 | } |
| 88 | take(size: number): Buffer { |
| 89 | const result = this.buffer.subarray(0, size); |
| 90 | this.buffer = this.buffer.subarray(size); |
| 91 | return result; |
| 92 | } |
| 93 | async until(delimiter: Buffer, limit: number): Promise<Buffer> { |
| 94 | while (true) { |
| 95 | const index = this.buffer.indexOf(delimiter); |
| 96 | if (index >= 0) { |
| 97 | if (index > limit) tooLarge(); |
| 98 | const value = this.take(index); |
| 99 | this.take(delimiter.length); |
| 100 | return value; |
| 101 | } |
| 102 | if (this.buffer.length > limit + delimiter.length) tooLarge(); |
| 103 | if (!(await this.fill())) badRequest(); |
| 104 | } |
| 105 | } |
| 106 | async *rest(): AsyncGenerator<Uint8Array> { |
| 107 | do { |
| 108 | if (this.buffer.length) yield this.take(this.buffer.length); |
| 109 | } while (await this.fill()); |
| 110 | } |
| 111 | close(): void { |
| 112 | void this.reader.cancel().catch(() => {}); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | export async function readBody( |
| 117 | body: ReadableStream<Uint8Array> | null, |
| 118 | limit: number, |
| 119 | deadline: Deadline, |
| 120 | ): Promise<Uint8Array<ArrayBuffer>> { |
| 121 | const input = new Input(body, limit, deadline); |
| 122 | try { |
| 123 | return await collect(input.rest(), limit); |
| 124 | } finally { |
| 125 | input.close(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | export function contentLength(request: Request, maximum: number): number | null { |
| 130 | const raw = request.headers.get('Content-Length'); |
| 131 | if (raw === null) return null; |
| 132 | if (!/^\d+$/.test(raw)) badRequest(); |
| 133 | const length = Number(raw); |
| 134 | if (!Number.isSafeInteger(length) || length > maximum) tooLarge(); |
| 135 | return length; |
| 136 | } |