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 type * as cf from "@cloudflare/workers-types"; |
| 2 | import * as Http from "alchemy/Http"; |
| 3 | import * as Deferred from "effect/Deferred"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as EffectHttp from "effect/http/HttpEffect"; |
| 6 | import { ClientAbort } from "effect/http/HttpServerError"; |
| 7 | import * as HttpServerRequest from "effect/http/HttpServerRequest"; |
| 8 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Option from "effect/Option"; |
| 11 | import { Request } from "./Request.ts"; |
| 12 | import type { WorkerServices } from "./Worker.ts"; |
| 13 | import { isWorkerEvent } from "./WorkerRuntime.ts"; |
| 14 | |
| 15 | export type HttpEffect = Http.HttpEffect<WorkerServices>; |
| 16 | |
| 17 | export const makeRequestHandler = |
| 18 | <Req = never>(handler: Http.HttpEffect<Req> | Effect.Effect<Http.HttpEffect<Req>>) => |
| 19 | (event: any) => |
| 20 | isWorkerEvent(event) && event.type === "fetch" |
| 21 | ? makeRequestEffect(event.input, handler, event.context) |
| 22 | : undefined; |
| 23 | |
| 24 | export const makeRequestEffect = <Req = never>( |
| 25 | webRequest: cf.Request, |
| 26 | handler: Http.HttpEffect<Req> | Effect.Effect<Http.HttpEffect<Req>>, |
| 27 | /** A Worker fetch's context. With it, a client abort interrupts the handler. */ |
| 28 | ctx?: cf.ExecutionContext, |
| 29 | ) => { |
| 30 | const signal = ctx && (webRequest as any as globalThis.Request).signal; |
| 31 | const safeHandler = Http.safeHttpEffect(handler); |
| 32 | return Effect.gen(function* () { |
| 33 | const request = HttpServerRequest.fromWeb(webRequest as any as globalThis.Request).modify({ |
| 34 | remoteAddress: Option.fromUndefinedOr( |
| 35 | webRequest.headers.get("cf-connecting-ip") ?? undefined, |
| 36 | ), |
| 37 | }); |
| 38 | |
| 39 | Object.defineProperty(request, "raw", { |
| 40 | get: () => |
| 41 | Object.assign(request.stream, { |
| 42 | raw: webRequest.body, |
| 43 | }), |
| 44 | }); |
| 45 | |
| 46 | // `toHandled` runs the request uninterruptibly; the handler opts back in, |
| 47 | // as `HttpRouter` routes do, so a client abort reaches it. |
| 48 | return yield* toHandledWebResponse( |
| 49 | signal ? Effect.interruptible(safeHandler) : safeHandler, |
| 50 | ).pipe( |
| 51 | Effect.provide([ |
| 52 | Layer.succeed(HttpServerRequest.HttpServerRequest, request), |
| 53 | Layer.succeed(Request, webRequest as any), |
| 54 | ]), |
| 55 | ); |
| 56 | }).pipe(interruptOnClientAbort(signal, ctx)) as any; |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Interrupts the request fiber when the client disconnects, as Effect's |
| 61 | * `HttpEffect.toWebHandler` does, annotated with `ClientAbort` so |
| 62 | * `causeResponse` answers 499. The signal only fires under the |
| 63 | * `enable_request_signal` compatibility flag. |
| 64 | * |
| 65 | * workerd cancels the event's pending I/O once the client is gone, so the |
| 66 | * abort also registers the fiber with `waitUntil`, letting the handler's |
| 67 | * finalizers finish. |
| 68 | */ |
| 69 | const interruptOnClientAbort = |
| 70 | (signal: AbortSignal | undefined, ctx: cf.ExecutionContext | undefined) => |
| 71 | <A, E, R>(self: Effect.Effect<A, E, R>) => |
| 72 | signal === undefined || ctx === undefined |
| 73 | ? self |
| 74 | : Effect.withFiber((fiber) => { |
| 75 | const onAbort = () => { |
| 76 | ctx.waitUntil(new Promise<void>((resolve) => fiber.addObserver(() => resolve()))); |
| 77 | fiber.interruptUnsafe(undefined, ClientAbort.annotation); |
| 78 | }; |
| 79 | if (signal.aborted) { |
| 80 | onAbort(); |
| 81 | } else { |
| 82 | signal.addEventListener("abort", onAbort, { once: true }); |
| 83 | fiber.addObserver(() => signal.removeEventListener("abort", onAbort)); |
| 84 | } |
| 85 | return self; |
| 86 | }); |
| 87 | |
| 88 | const toHandledWebResponse = <Req>( |
| 89 | handler: Effect.Effect<HttpServerResponse.HttpServerResponse, never, Req>, |
| 90 | ) => |
| 91 | Effect.gen(function* () { |
| 92 | // `toHandled` exposes the final response through this callback, not its |
| 93 | // return value. Keep the assignment isolated here so callers get Response. |
| 94 | const context = yield* Effect.context(); |
| 95 | const webResponse = yield* Deferred.make<Response>(); |
| 96 | |
| 97 | yield* EffectHttp.toHandled(handler, (request, response) => { |
| 98 | const withoutBody = request.method === "HEAD"; |
| 99 | if (!HttpServerResponse.omitsBody(response, withoutBody)) { |
| 100 | response = EffectHttp.scopeTransferToStream(response); |
| 101 | } |
| 102 | return Deferred.succeed( |
| 103 | webResponse, |
| 104 | HttpServerResponse.toWeb(response, { withoutBody, context }), |
| 105 | ); |
| 106 | }); |
| 107 | return yield* Deferred.await(webResponse); |
| 108 | }); |
| 109 | |
| 110 | export { isScopeEjected } from "alchemy/Http"; |