The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Data from "effect/Data"; |
| 2 | import type * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as HttpClient from "effect/http/HttpClient"; |
| 5 | import type * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | |
| 8 | export class WorkerNotPropagated extends Data.TaggedError("WorkerNotPropagated")<{ |
| 9 | readonly url: string; |
| 10 | }> {} |
| 11 | |
| 12 | /** The Worker never answered ready `consecutive` times in a row before the timeout. */ |
| 13 | export class WorkerNotStable extends Data.TaggedError("WorkerNotStable")<{ |
| 14 | readonly label: string; |
| 15 | readonly consecutive: number; |
| 16 | }> {} |
| 17 | |
| 18 | const isWorkerPlaceholder = (status: number, body: string) => |
| 19 | (status === 404 && |
| 20 | body.includes("<title>Page not found</title>") && |
| 21 | body.includes("There is nothing here yet")) || |
| 22 | (status === 500 && |
| 23 | body.includes("<title>Script not found |") && |
| 24 | body.includes(" | Cloudflare</title>") && |
| 25 | body.includes("/cdn-cgi/styles/cf.errors.css")); |
| 26 | |
| 27 | /** Retry only Cloudflare's pre-invocation placeholder, never application errors. */ |
| 28 | export const requestWorker = ( |
| 29 | request: HttpClientRequest.HttpClientRequest, |
| 30 | options: { retryDelay?: Duration.Input } = {}, |
| 31 | ) => |
| 32 | HttpClient.execute(request).pipe( |
| 33 | Effect.flatMap((response) => |
| 34 | response.status !== 404 && response.status !== 500 |
| 35 | ? Effect.succeed(response) |
| 36 | : response.text.pipe( |
| 37 | Effect.flatMap((body) => |
| 38 | isWorkerPlaceholder(response.status, body) |
| 39 | ? Effect.fail(new WorkerNotPropagated({ url: request.url })) |
| 40 | : Effect.succeed(response), |
| 41 | ), |
| 42 | ), |
| 43 | ), |
| 44 | Effect.retry({ |
| 45 | while: (error) => error._tag === "WorkerNotPropagated", |
| 46 | schedule: Schedule.spaced(options.retryDelay ?? "1 second"), |
| 47 | times: 8, |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | /** |
| 52 | * Wait until a freshly deployed Worker answers `check` as ready `consecutive` |
| 53 | * times in a row. |
| 54 | * |
| 55 | * Cloudflare rolls a new script version out host by host and exposes no |
| 56 | * propagation API, so one ready response only proves that one host has it; |
| 57 | * the next request can still land on a host serving the placeholder or the |
| 58 | * previous version. `check` returns `false` while the Worker is still |
| 59 | * propagating (any not-ready answer resets the streak) and fails on a real |
| 60 | * error, which is never retried. |
| 61 | */ |
| 62 | export const waitUntilStable = <E, R>( |
| 63 | label: string, |
| 64 | check: Effect.Effect<boolean, E, R>, |
| 65 | options: { |
| 66 | readonly consecutive?: number; |
| 67 | readonly spacing?: Duration.Input; |
| 68 | readonly timeout?: Duration.Input; |
| 69 | } = {}, |
| 70 | ): Effect.Effect<void, E | WorkerNotStable, R> => { |
| 71 | const consecutive = options.consecutive ?? 3; |
| 72 | return Effect.suspend(() => { |
| 73 | let streak = 0; |
| 74 | return check.pipe( |
| 75 | Effect.map((ready) => { |
| 76 | streak = ready ? streak + 1 : 0; |
| 77 | return streak >= consecutive; |
| 78 | }), |
| 79 | Effect.repeat({ |
| 80 | schedule: Schedule.spaced(options.spacing ?? "1 second"), |
| 81 | until: (stable) => stable, |
| 82 | }), |
| 83 | Effect.asVoid, |
| 84 | Effect.timeoutOrElse({ |
| 85 | duration: options.timeout ?? "90 seconds", |
| 86 | orElse: () => Effect.fail(new WorkerNotStable({ label, consecutive })), |
| 87 | }), |
| 88 | ); |
| 89 | }); |
| 90 | }; |