The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as kv from "@distilled.cloud/cloudflare/kv"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Path from "effect/Path"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import * as Stream from "effect/Stream"; |
| 15 | import * as pathe from "pathe"; |
| 16 | import { cloneFixture } from "../Utils/Fixture.ts"; |
| 17 | import { expectClientChunkContains, expectUrlContains } from "../Utils/Http.ts"; |
| 18 | import { expectWorkerExists, waitForWorkerToBeDeleted } from "../Utils/Worker.ts"; |
| 19 | |
| 20 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 21 | |
| 22 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 23 | |
| 24 | const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "nuxt-app"); |
| 25 | |
| 26 | // Keep the temp clone under the alchemy package (same convention as the |
| 27 | // Vite tests) so the project root stays representable relative to cwd — |
| 28 | // and so `nuxt`/`nitropack` resolve from the workspace's hoisted |
| 29 | // node_modules when the fixture's own tree has none. |
| 30 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 31 | |
| 32 | const fixtureEntries = [ |
| 33 | ".gitignore", |
| 34 | "package.json", |
| 35 | "nuxt.config.ts", |
| 36 | "worker-entry.ts", |
| 37 | "app", |
| 38 | "server", |
| 39 | "public", |
| 40 | ]; |
| 41 | |
| 42 | const memoInclude = [ |
| 43 | "app/**", |
| 44 | "server/**", |
| 45 | "public/**", |
| 46 | "nuxt.config.ts", |
| 47 | "worker-entry.ts", |
| 48 | "package.json", |
| 49 | ]; |
| 50 | |
| 51 | const nuxtProps = (rootDir: string) => ({ |
| 52 | rootDir, |
| 53 | workersDev: { enabled: true, previewsEnabled: true }, |
| 54 | memo: { include: memoInclude }, |
| 55 | }); |
| 56 | |
| 57 | class NamespaceStillExists extends Data.TaggedError("NamespaceStillExists") {} |
| 58 | |
| 59 | const waitForNamespaceToBeDeleted = Effect.fn(function* (namespaceId: string, accountId: string) { |
| 60 | yield* kv.getNamespace({ accountId, namespaceId }).pipe( |
| 61 | Effect.flatMap(() => Effect.fail(new NamespaceStillExists())), |
| 62 | Effect.retry({ |
| 63 | while: (e): e is NamespaceStillExists => e instanceof NamespaceStillExists, |
| 64 | schedule: Schedule.min([Schedule.exponential(250), Schedule.spaced("2 seconds")]), |
| 65 | times: 10, |
| 66 | }), |
| 67 | Effect.catchTag("NamespaceNotFound", () => Effect.void), |
| 68 | ); |
| 69 | }); |
| 70 | |
| 71 | /** |
| 72 | * Read `keyName` from the real cloud namespace via distilled's KV API, |
| 73 | * retrying through KV's brief read-after-write lag while the key is not |
| 74 | * yet visible. |
| 75 | */ |
| 76 | const readNamespaceValue = Effect.fn(function* (options: { |
| 77 | accountId: string; |
| 78 | namespaceId: string; |
| 79 | keyName: string; |
| 80 | }) { |
| 81 | const res = yield* kv.getNamespaceValue(options).pipe( |
| 82 | Effect.retry({ |
| 83 | while: (e): boolean => e._tag === "KeyNotFound", |
| 84 | schedule: Schedule.min([Schedule.exponential("1 second"), Schedule.spaced("3 seconds")]), |
| 85 | times: 8, |
| 86 | }), |
| 87 | ); |
| 88 | return yield* Effect.tryPromise(() => |
| 89 | new Response(Stream.toReadableStream(res.body) as BodyInit).text(), |
| 90 | ); |
| 91 | }); |
| 92 | |
| 93 | // Tests are independent (per-test scratch stacks, private fixture clones), |
| 94 | // so run them concurrently; suites are sequential by default. |
| 95 | describe.concurrent( |
| 96 | "Nuxt", |
| 97 | { |
| 98 | tags: [ |
| 99 | "provider:cloudflare", |
| 100 | "provider:cloudflare:website", |
| 101 | "provider:cloudflare:worker", |
| 102 | "live", |
| 103 | ], |
| 104 | }, |
| 105 | () => { |
| 106 | test.provider( |
| 107 | "Nuxt: deploys SSR + bindings + static assets and memoizes unchanged rebuilds", |
| 108 | (stack) => |
| 109 | Effect.gen(function* () { |
| 110 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 111 | const fs = yield* FileSystem.FileSystem; |
| 112 | const path = yield* Path.Path; |
| 113 | |
| 114 | yield* stack.destroy(); |
| 115 | |
| 116 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 117 | prefix: "alchemy-nuxt-", |
| 118 | tempRoot, |
| 119 | entries: fixtureEntries, |
| 120 | }); |
| 121 | |
| 122 | const bindingMarker = "nuxt-binding-marker"; |
| 123 | |
| 124 | const deploy = () => |
| 125 | stack.deploy( |
| 126 | Effect.gen(function* () { |
| 127 | const siteKv = yield* Cloudflare.KV.Namespace("SiteKV"); |
| 128 | const site = yield* Cloudflare.Website.Nuxt("NuxtSite", { |
| 129 | ...nuxtProps(rootDir), |
| 130 | env: { |
| 131 | TEST_BINDING: bindingMarker, |
| 132 | SITE_KV: siteKv, |
| 133 | VITE_SITE_MARKER: "NUXT_PUBLIC_ENV_MARKER", |
| 134 | }, |
| 135 | }); |
| 136 | return { site, siteKv }; |
| 137 | }), |
| 138 | ); |
| 139 | |
| 140 | const { site: site1, siteKv } = yield* deploy(); |
| 141 | |
| 142 | expect(site1.url).toBeDefined(); |
| 143 | expect(site1.hash?.input).toBeDefined(); |
| 144 | yield* expectWorkerExists(site1.workerName, accountId); |
| 145 | |
| 146 | // SSR page: rendered by the Worker at request time. |
| 147 | yield* expectUrlContains(`${site1.url!}/`, "NUXT_PAGE_MARKER", { |
| 148 | timeout: "120 seconds", |
| 149 | label: "SSR home page", |
| 150 | }); |
| 151 | |
| 152 | // A declared public env value is inlined into the deployed client |
| 153 | // bundle at build time. |
| 154 | yield* expectClientChunkContains( |
| 155 | site1.url!, |
| 156 | pathe.join(rootDir, ".output", "public"), |
| 157 | "NUXT_PUBLIC_ENV_MARKER", |
| 158 | { |
| 159 | timeout: "60 seconds", |
| 160 | label: "nuxt public env", |
| 161 | }, |
| 162 | ); |
| 163 | |
| 164 | // The SSR page reads `event.context.cloudflare.env.TEST_BINDING` — |
| 165 | // proves bindings reach nitro's cloudflare_module runtime contract. |
| 166 | yield* expectUrlContains(`${site1.url!}/`, `binding:${bindingMarker}`, { |
| 167 | timeout: "60 seconds", |
| 168 | label: "SSR page with event.context.cloudflare.env binding", |
| 169 | }); |
| 170 | |
| 171 | // The fixture's own nuxt.config.ts loaded natively — its |
| 172 | // `runtimeConfig.public.fixtureMarker` renders on the page. |
| 173 | yield* expectUrlContains(`${site1.url!}/`, "config:nuxt-user-config-loaded", { |
| 174 | timeout: "60 seconds", |
| 175 | label: "user nuxt.config.ts applied", |
| 176 | }); |
| 177 | |
| 178 | // API route: reads the binding + waitUntil from the runtime contract. |
| 179 | const hello = yield* fetchJsonReady<{ |
| 180 | marker: string; |
| 181 | binding: string | null; |
| 182 | hasWaitUntil: boolean; |
| 183 | }>(`${site1.url!}/api/hello`); |
| 184 | expect(hello.marker).toBe("api-route-ok"); |
| 185 | expect(hello.binding).toBe(bindingMarker); |
| 186 | expect(hello.hasWaitUntil).toBe(true); |
| 187 | |
| 188 | // POST route: the JSON body flows through h3's readBody and comes |
| 189 | // back verbatim, with the binding visible on a non-GET route. |
| 190 | const echoPayload = { message: "hello-from-post", n: 42 }; |
| 191 | const echo = yield* postJsonReady<{ |
| 192 | method: string; |
| 193 | echoed: { message: string; n: number }; |
| 194 | binding: string | null; |
| 195 | }>(`${site1.url!}/api/echo`, echoPayload); |
| 196 | expect(echo.method).toBe("POST"); |
| 197 | expect(echo.echoed).toEqual(echoPayload); |
| 198 | expect(echo.binding).toBe(bindingMarker); |
| 199 | |
| 200 | // KV binding: the worker's PUT lands in the real namespace and the |
| 201 | // worker's GET reads it back through the native binding. |
| 202 | const kvKey = "nuxt-live-key"; |
| 203 | const kvValue = "nuxt-live-value"; |
| 204 | expect(siteKv.namespaceId).toBeDefined(); |
| 205 | const put = yield* putJsonReady<{ put: boolean; key: string }>( |
| 206 | `${site1.url!}/api/kv?key=${kvKey}&value=${kvValue}`, |
| 207 | ); |
| 208 | expect(put.put).toBe(true); |
| 209 | const got = yield* fetchJsonReady<{ key: string; value: string | null }>( |
| 210 | `${site1.url!}/api/kv?key=${kvKey}`, |
| 211 | ); |
| 212 | expect(got.value).toBe(kvValue); |
| 213 | |
| 214 | // Out-of-band: the write is visible through the cloud KV API — the |
| 215 | // binding really targeted the namespace this stack provisioned. |
| 216 | const observed = yield* readNamespaceValue({ |
| 217 | accountId, |
| 218 | namespaceId: siteKv.namespaceId, |
| 219 | keyName: kvKey, |
| 220 | }); |
| 221 | expect(observed).toBe(kvValue); |
| 222 | |
| 223 | // Static asset from `public/`. |
| 224 | yield* expectUrlContains(`${site1.url!}/robots.txt`, "User-agent", { |
| 225 | timeout: "60 seconds", |
| 226 | label: "static asset", |
| 227 | }); |
| 228 | // The same file read by a server route through the `ASSETS` binding. |
| 229 | yield* expectUrlContains(`${site1.url!}/api/asset`, "User-agent", { |
| 230 | timeout: "60 seconds", |
| 231 | label: "nuxt ASSETS binding", |
| 232 | }); |
| 233 | |
| 234 | // Route-rule prerendered page, served from assets. |
| 235 | yield* expectUrlContains(`${site1.url!}/prerendered`, "this-page-is-prerendered", { |
| 236 | timeout: "60 seconds", |
| 237 | label: "prerendered page", |
| 238 | }); |
| 239 | |
| 240 | // ── deploy 2: no changes ⇒ the rebuild-free input hash matches and |
| 241 | // the deploy short-circuits without building ───────────────────────── |
| 242 | const { site: site2 } = yield* deploy(); |
| 243 | |
| 244 | expect(site2.hash?.input).toBeDefined(); |
| 245 | expect(site2.hash?.input).toEqual(site1.hash?.input); |
| 246 | expect(site2.url).toBe(site1.url); |
| 247 | |
| 248 | // ── deploy 3: edit a page ⇒ the input hash changes and the new |
| 249 | // content deploys. The edited marker is asserted on the *dynamic* |
| 250 | // page (worker-rendered per request) — a changed static asset at the |
| 251 | // same URL can stay stale at a PoP far longer than a worker-version |
| 252 | // flip ───────────────────────────────────────────────────────────── |
| 253 | const indexPath = path.join(rootDir, "app/pages/index.vue"); |
| 254 | const index = yield* fs.readFileString(indexPath); |
| 255 | yield* fs.writeFileString( |
| 256 | indexPath, |
| 257 | index.replace("NUXT_PAGE_MARKER", "NUXT_PAGE_MARKER_V2"), |
| 258 | ); |
| 259 | |
| 260 | const { site: site3 } = yield* deploy(); |
| 261 | |
| 262 | expect(site3.hash?.input).toBeDefined(); |
| 263 | expect(site3.hash?.input).not.toEqual(site1.hash?.input); |
| 264 | yield* expectUrlContains(`${site3.url!}/`, "NUXT_PAGE_MARKER_V2", { |
| 265 | timeout: "180 seconds", |
| 266 | label: "SSR page after edit", |
| 267 | }); |
| 268 | |
| 269 | yield* stack.destroy(); |
| 270 | yield* waitForWorkerToBeDeleted(site1.workerName, accountId); |
| 271 | |
| 272 | // Destroy must also delete the stack-provisioned KV namespace. |
| 273 | yield* waitForNamespaceToBeDeleted(siteKv.namespaceId, accountId); |
| 274 | }).pipe(logLevel), |
| 275 | { tags: ["provider:cloudflare:kv"], timeout: 600_000 }, |
| 276 | ); |
| 277 | |
| 278 | // ───────────────────────────────────────────────────────────────────── |
| 279 | // Custom worker entry (seam: NuxtProps.main → nitro.options.entry) |
| 280 | // |
| 281 | // `main` points the deploy at the user's own module, which wraps nitro's |
| 282 | // emitted cloudflare-module handler (imported from |
| 283 | // `nitropack/presets/cloudflare/runtime/cloudflare-module`) and re-exports |
| 284 | // the `Counter` Durable Object class — DO classes must live on the |
| 285 | // deployed worker for their namespace bindings to resolve. Mirrors |
| 286 | // Waku.test.ts's "main deploys a custom worker entry" test. |
| 287 | // ───────────────────────────────────────────────────────────────────── |
| 288 | |
| 289 | test.provider( |
| 290 | "Nuxt: main deploys a custom worker entry hosting a Durable Object", |
| 291 | (stack) => |
| 292 | Effect.gen(function* () { |
| 293 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 294 | |
| 295 | yield* stack.destroy(); |
| 296 | |
| 297 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 298 | prefix: "alchemy-nuxt-main-", |
| 299 | tempRoot, |
| 300 | entries: fixtureEntries, |
| 301 | }); |
| 302 | |
| 303 | const bindingMarker = "nuxt-main-marker"; |
| 304 | |
| 305 | const site = yield* stack.deploy( |
| 306 | Effect.gen(function* () { |
| 307 | return yield* Cloudflare.Website.Nuxt("NuxtMainSite", { |
| 308 | ...nuxtProps(rootDir), |
| 309 | // The user's own worker entry: wraps nitro's handler and |
| 310 | // exports the Counter DO. |
| 311 | main: "worker-entry.ts", |
| 312 | env: { |
| 313 | TEST_BINDING: bindingMarker, |
| 314 | COUNTER: Cloudflare.DurableObject("Counter", { className: "Counter" }), |
| 315 | }, |
| 316 | }); |
| 317 | }), |
| 318 | ); |
| 319 | |
| 320 | expect(site.url).toBeDefined(); |
| 321 | yield* expectWorkerExists(site.workerName, accountId); |
| 322 | |
| 323 | // The DO namespace is bound and state increments ACROSS requests — |
| 324 | // instance identity on the deployed worker, through the custom entry. |
| 325 | // (POST increments, GET reads — see server/api/counter.ts.) |
| 326 | const first = yield* postJsonReady<{ count: number }>(`${site.url!}/api/counter`, {}); |
| 327 | const second = yield* postJsonReady<{ count: number }>(`${site.url!}/api/counter`, {}); |
| 328 | expect(second.count).toBe(first.count + 1); |
| 329 | |
| 330 | const read = yield* fetchJsonReady<{ count: number }>(`${site.url!}/api/counter`); |
| 331 | expect(read.count).toBe(second.count); |
| 332 | |
| 333 | // Wrapping nitro's handler keeps every framework route working: |
| 334 | // the SSR page still renders (and still reads bindings). |
| 335 | yield* expectUrlContains(`${site.url!}/`, "NUXT_PAGE_MARKER", { |
| 336 | timeout: "120 seconds", |
| 337 | label: "SSR page through the custom entry", |
| 338 | }); |
| 339 | yield* expectUrlContains(`${site.url!}/`, `binding:${bindingMarker}`, { |
| 340 | timeout: "60 seconds", |
| 341 | label: "env binding through the custom entry", |
| 342 | }); |
| 343 | |
| 344 | // API route through the wrapped handler. |
| 345 | const hello = yield* fetchJsonReady<{ marker: string; binding: string | null }>( |
| 346 | `${site.url!}/api/hello`, |
| 347 | ); |
| 348 | expect(hello.marker).toBe("api-route-ok"); |
| 349 | expect(hello.binding).toBe(bindingMarker); |
| 350 | |
| 351 | // Static asset + prerendered page still serve from assets alongside |
| 352 | // the custom entry. |
| 353 | yield* expectUrlContains(`${site.url!}/robots.txt`, "User-agent", { |
| 354 | timeout: "60 seconds", |
| 355 | label: "static asset with custom entry", |
| 356 | }); |
| 357 | yield* expectUrlContains(`${site.url!}/prerendered`, "this-page-is-prerendered", { |
| 358 | timeout: "60 seconds", |
| 359 | label: "prerendered page with custom entry", |
| 360 | }); |
| 361 | |
| 362 | yield* stack.destroy(); |
| 363 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 364 | }).pipe(logLevel), |
| 365 | { timeout: 600_000 }, |
| 366 | ); |
| 367 | |
| 368 | // ───────────────────────────────────────────────────────────────────── |
| 369 | // SPA mode: `ssr: false` |
| 370 | // |
| 371 | // Pages render exclusively in the browser — the raw HTML the deploy |
| 372 | // serves is the app shell (identified by the fixture's `app.head` meta |
| 373 | // marker), for `/` and for deep links alike. Nitro keeps `server/` |
| 374 | // routes executing in the worker with the full cloudflare runtime |
| 375 | // contract; only page rendering moves to the client. |
| 376 | // ───────────────────────────────────────────────────────────────────── |
| 377 | |
| 378 | const spaFixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "nuxt-spa-app"); |
| 379 | |
| 380 | const SPA_SHELL_MARKER = "nuxt-spa-shell"; |
| 381 | |
| 382 | test.provider( |
| 383 | "Nuxt: ssr:false serves the SPA shell while server routes still run in the worker", |
| 384 | (stack) => |
| 385 | Effect.gen(function* () { |
| 386 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 387 | |
| 388 | yield* stack.destroy(); |
| 389 | |
| 390 | const rootDir = yield* cloneFixture(spaFixtureDir, { |
| 391 | prefix: "alchemy-nuxt-spa-", |
| 392 | tempRoot, |
| 393 | entries: [".gitignore", "package.json", "nuxt.config.ts", "app", "server", "public"], |
| 394 | }); |
| 395 | |
| 396 | const bindingMarker = "nuxt-spa-binding-marker"; |
| 397 | |
| 398 | const site = yield* stack.deploy( |
| 399 | Effect.gen(function* () { |
| 400 | return yield* Cloudflare.Website.Nuxt("NuxtSpaSite", { |
| 401 | rootDir, |
| 402 | workersDev: { enabled: true, previewsEnabled: true }, |
| 403 | memo: { |
| 404 | include: ["app/**", "server/**", "public/**", "nuxt.config.ts", "package.json"], |
| 405 | }, |
| 406 | env: { TEST_BINDING: bindingMarker }, |
| 407 | }); |
| 408 | }), |
| 409 | ); |
| 410 | |
| 411 | expect(site.url).toBeDefined(); |
| 412 | yield* expectWorkerExists(site.workerName, accountId); |
| 413 | |
| 414 | // (a) `/` serves the app shell: the `app.head` meta marker is in |
| 415 | // the raw HTML, while the page markup (which only ever renders in |
| 416 | // the browser under `ssr: false`) is absent. |
| 417 | const homeBody = yield* expectUrlContains(`${site.url!}/`, SPA_SHELL_MARKER, { |
| 418 | timeout: "120 seconds", |
| 419 | label: "SPA shell at /", |
| 420 | }); |
| 421 | expect(homeBody).not.toContain("NUXT_SPA_PAGE_MARKER"); |
| 422 | |
| 423 | // (b) A hard GET to a client route serves the shell too — the |
| 424 | // client router owns the route; its markup never appears in HTML. |
| 425 | const deepBody = yield* expectUrlContains(`${site.url!}/deep`, SPA_SHELL_MARKER, { |
| 426 | timeout: "60 seconds", |
| 427 | label: "deep link serves SPA shell", |
| 428 | }); |
| 429 | expect(deepBody).not.toContain("NUXT_SPA_DEEP_MARKER"); |
| 430 | |
| 431 | // (c) Nitro keeps `server/api` routes executing in the worker, |
| 432 | // `ssr: false` notwithstanding — with the cloudflare_module |
| 433 | // runtime contract (env binding + waitUntil) intact. |
| 434 | const hello = yield* fetchJsonReady<{ |
| 435 | marker: string; |
| 436 | binding: string | null; |
| 437 | hasWaitUntil: boolean; |
| 438 | }>(`${site.url!}/api/hello`); |
| 439 | expect(hello.marker).toBe("spa-api-route-ok"); |
| 440 | expect(hello.binding).toBe(bindingMarker); |
| 441 | expect(hello.hasWaitUntil).toBe(true); |
| 442 | |
| 443 | // (d) A `public/` asset serves from the asset layer. |
| 444 | yield* expectUrlContains(`${site.url!}/robots.txt`, "User-agent", { |
| 445 | timeout: "60 seconds", |
| 446 | label: "static asset in SPA mode", |
| 447 | }); |
| 448 | |
| 449 | yield* stack.destroy(); |
| 450 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 451 | }).pipe(logLevel), |
| 452 | { timeout: 600_000 }, |
| 453 | ); |
| 454 | }, |
| 455 | ); |
| 456 | |
| 457 | /** |
| 458 | * GET `url` until it answers 200 with a JSON body (fresh workers.dev URLs |
| 459 | * take a few seconds to start serving). Mirrors SvelteKit.test.ts's |
| 460 | * helper of the same name. |
| 461 | */ |
| 462 | const fetchJsonReady = <T>(url: string) => |
| 463 | Effect.gen(function* () { |
| 464 | const client = yield* HttpClient.HttpClient; |
| 465 | return yield* client.get(url).pipe( |
| 466 | Effect.flatMap((res) => |
| 467 | res.status === 200 |
| 468 | ? Effect.flatMap(res.text, (body) => |
| 469 | Effect.try({ |
| 470 | try: () => JSON.parse(body) as T, |
| 471 | catch: () => new Error(`non-json body: ${body}`), |
| 472 | }), |
| 473 | ) |
| 474 | : Effect.fail(new Error(`Worker not ready: ${res.status}`)), |
| 475 | ), |
| 476 | Effect.retry({ |
| 477 | // Capped interval, ~90s total budget: fresh workers.dev subdomains and |
| 478 | // DO namespaces can take over a minute to start serving under |
| 479 | // concurrent deploys. |
| 480 | schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 481 | times: 45, |
| 482 | }), |
| 483 | ); |
| 484 | }); |
| 485 | |
| 486 | /** POST a JSON body to `url` until it answers 200 with a JSON body. */ |
| 487 | const postJsonReady = <T>(url: string, body: unknown) => |
| 488 | HttpClient.execute(HttpClientRequest.post(url).pipe(HttpClientRequest.bodyJsonUnsafe(body))).pipe( |
| 489 | Effect.flatMap((res) => |
| 490 | res.status === 200 |
| 491 | ? Effect.flatMap(res.text, (responseBody) => |
| 492 | Effect.try({ |
| 493 | try: () => JSON.parse(responseBody) as T, |
| 494 | catch: () => new Error(`non-json body: ${responseBody}`), |
| 495 | }), |
| 496 | ) |
| 497 | : Effect.fail(new Error(`Worker not ready: ${res.status}`)), |
| 498 | ), |
| 499 | Effect.retry({ |
| 500 | // Capped interval, ~90s total budget (workers.dev / DO propagation). |
| 501 | schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 502 | times: 45, |
| 503 | }), |
| 504 | ); |
| 505 | |
| 506 | /** PUT (empty body) to `url` until it answers 200 with a JSON body. */ |
| 507 | const putJsonReady = <T>(url: string) => |
| 508 | HttpClient.execute(HttpClientRequest.put(url)).pipe( |
| 509 | Effect.flatMap((res) => |
| 510 | res.status === 200 |
| 511 | ? Effect.flatMap(res.text, (responseBody) => |
| 512 | Effect.try({ |
| 513 | try: () => JSON.parse(responseBody) as T, |
| 514 | catch: () => new Error(`non-json body: ${responseBody}`), |
| 515 | }), |
| 516 | ) |
| 517 | : Effect.fail(new Error(`Worker not ready: ${res.status}`)), |
| 518 | ), |
| 519 | Effect.retry({ |
| 520 | // Capped interval, ~90s total budget (workers.dev / DO propagation). |
| 521 | schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 522 | times: 45, |
| 523 | }), |
| 524 | ); |