@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 630 lines 20.9 KB
Raw
1 import * as dns from "node:dns/promises";
2 import * as Data from "effect/Data";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Path from "effect/Path";
7 import * as Schedule from "effect/Schedule";
8
9 /**
10 * HTTP assertion helpers tuned for Cloudflare's "deploy → propagate →
11 * serve" pipeline.
12 *
13 * Newly-deployed workers.dev URLs commonly:
14 * - Return Cloudflare's "There is nothing here yet" placeholder page
15 * while the workers.dev subdomain is enabling for the first time.
16 * - Serve a stale cached body for a few seconds after the version flip.
17 * - Briefly 522 / 530 while the edge fetches the new version.
18 *
19 * The helpers here retry through all of those failure modes with an
20 * exponential backoff capped by an explicit total `timeout`, while
21 * still surfacing a precise, actionable error if the budget is
22 * exhausted.
23 */
24
25 export class HttpAssertionFailed extends Data.TaggedError("HttpAssertionFailed")<{
26 url: string;
27 marker: string;
28 status: number;
29 bodyExcerpt: string;
30 }> {}
31
32 export class HttpFetchFailed extends Data.TaggedError("HttpFetchFailed")<{
33 url: string;
34 message: string;
35 }> {}
36
37 export class HttpMarkerPresent extends Data.TaggedError("HttpMarkerPresent")<{
38 url: string;
39 marker: string;
40 bodyExcerpt: string;
41 }> {}
42
43 export class DnsNotReady extends Data.TaggedError("DnsNotReady")<{
44 hostname: string;
45 type: string;
46 detail: string;
47 }> {
48 override get message() {
49 return `${this.type} lookup for ${this.hostname} failed: ${this.detail}`;
50 }
51 }
52
53 /**
54 * Wait until `hostname` resolves for every record type in `types` on its
55 * zone's authoritative nameservers. Call it before the first fetch of a
56 * hostname the test just created.
57 *
58 * No caching resolver may be asked about the hostname before the record is
59 * live: an early lookup caches the negative answer for the zone's SOA
60 * minimum (30 minutes on Cloudflare), and that poisons every later fetch.
61 * This applies to public resolvers too: polling 1.1.1.1 over DoH caches the
62 * NXDOMAIN at 1.1.1.1, which is often the machine's own upstream.
63 * Authoritative nameservers keep no cache, so they answer as soon as the
64 * record exists. The zone is found by asking for NS records on the
65 * hostname's parents, never on the hostname itself.
66 *
67 * Each attempt queries through a fresh `Resolver`: c-ares (behind
68 * `dns.Resolver` in Node and Bun) caches negative answers per resolver for
69 * the SOA minimum, so reusing one would replay the first NXDOMAIN on every
70 * retry.
71 */
72 export const waitForDns = Effect.fn(function* (
73 hostname: string,
74 types: ReadonlyArray<"A" | "AAAA"> = ["A", "AAAA"],
75 ) {
76 const nameservers = yield* Effect.tryPromise({
77 try: async () => {
78 const labels = hostname.split(".");
79 for (let i = 1; i < labels.length - 1; i++) {
80 const zone = labels.slice(i).join(".");
81 const ns = await dns.resolveNs(zone).catch(() => []);
82 if (ns.length > 0) {
83 return (await Promise.all(ns.map((host) => dns.resolve4(host)))).flat();
84 }
85 }
86 throw new Error(`no zone found for ${hostname}`);
87 },
88 catch: (cause) => new DnsNotReady({ hostname, type: "NS", detail: String(cause) }),
89 });
90 yield* Effect.forEach(
91 types,
92 (type) =>
93 Effect.tryPromise({
94 try: async () => {
95 const authoritative = new dns.Resolver();
96 authoritative.setServers(nameservers);
97 const answers =
98 type === "A"
99 ? await authoritative.resolve4(hostname)
100 : await authoritative.resolve6(hostname);
101 if (answers.length === 0) throw new Error("no answer");
102 },
103 catch: (cause) => new DnsNotReady({ hostname, type, detail: String(cause) }),
104 }).pipe(
105 Effect.retry({
106 while: (e) => e._tag === "DnsNotReady",
107 schedule: Schedule.spaced("2 seconds"),
108 times: 45,
109 }),
110 ),
111 { discard: true },
112 );
113 });
114
115 export interface ExpectUrlContainsOptions {
116 /** Request headers, e.g. an explicit HTML Accept header for content negotiation. */
117 headers?: Record<string, string>;
118 /** Maximum total time to retry before failing. Default 90s. */
119 timeout?: Duration.Input;
120 /** Initial backoff between attempts. Default 750ms. */
121 initialBackoff?: Duration.Input;
122 /** Description used in error messages. */
123 label?: string;
124 }
125
126 const looksLikeCloudflarePlaceholder = (body: string) =>
127 // Cloudflare's "no worker / not propagated" landing page.
128 body.includes("There is nothing here yet") ||
129 // The blue 522 / 1xxx error page family.
130 /Error\s+\d{3,4}/i.test(body);
131
132 /**
133 * Hard settlement bound for a single attempt, applied at the PROMISE level
134 * (`Promise.race`), not via fiber interruption: bun's fetch can wedge on a
135 * pooled connection the server closed after an error response — the
136 * request's promise neither settles nor honors its abort signal, and any
137 * Effect timeout that interrupts `tryPromise` then awaits that settlement
138 * forever. The race settles regardless and simply abandons the wedged
139 * promise. Generous on purpose: a cold dev server's first SSR/MDX compile
140 * can legitimately take tens of seconds.
141 */
142 const ATTEMPT_SETTLEMENT_CAP_MS = 60_000;
143
144 const fetchOnce = (url: string, marker: string, headers?: Record<string, string>) =>
145 Effect.tryPromise({
146 try: async (signal) => {
147 // Cache-busting query string defeats both edge caches and any
148 // intermediate proxy that ignores `cache-control: no-cache`.
149 const u = new URL(url);
150 u.searchParams.set("__alchemy_cb", String(Date.now()));
151 const attempt = async () => {
152 const res = await fetch(u, {
153 signal,
154 cache: "no-store",
155 headers: {
156 "cache-control": "no-cache",
157 pragma: "no-cache",
158 accept: "*/*",
159 // No keep-alive reuse: the wedge above starts with a reused
160 // connection the server already closed.
161 connection: "close",
162 ...headers,
163 },
164 });
165 return { res, body: await res.text() };
166 };
167 let capTimer: ReturnType<typeof setTimeout> | undefined;
168 const cap = new Promise<never>((_, reject) => {
169 capTimer = setTimeout(
170 () =>
171 reject(
172 new Error(`attempt did not settle within ${ATTEMPT_SETTLEMENT_CAP_MS}ms — abandoned`),
173 ),
174 ATTEMPT_SETTLEMENT_CAP_MS,
175 );
176 });
177 const { res, body } = await Promise.race([attempt(), cap]).finally(() =>
178 clearTimeout(capTimer),
179 );
180 if (!res.ok || looksLikeCloudflarePlaceholder(body) || !body.includes(marker)) {
181 if (process.env.DEBUG_HTTP_ASSERT) {
182 const flat = body.replace(/\s+/g, " ");
183 const interesting = flat.match(/(Error|Worker threw|exception)[^<]{0,140}/gi) ?? [];
184 console.error(
185 `[http-assert] ${res.status} ${url} :: ${interesting.length ? interesting.slice(0, 3).join(" | ") : flat.slice(0, 160)}`,
186 );
187 }
188 throw new HttpAssertionFailed({
189 url,
190 marker,
191 status: res.status,
192 bodyExcerpt: body.slice(0, 240),
193 });
194 }
195 return body;
196 },
197 catch: (e) =>
198 e instanceof HttpAssertionFailed
199 ? e
200 : new HttpFetchFailed({
201 url,
202 message: e instanceof Error ? e.message : String(e),
203 }),
204 });
205
206 /**
207 * Fetch `url` and assert the response body contains `marker`. Retries
208 * the fetch on transient failures (network errors, 5xx, Cloudflare
209 * placeholders, missing marker) until the marker appears or the
210 * `timeout` elapses. Each attempt uses a fresh cache-busting query
211 * param and `cache: "no-store"` so we don't sit on a CDN-cached body.
212 *
213 * The hash assertions in the deploy tests prove the *intent* to
214 * publish; this helper proves the assets actually serve over HTTP.
215 */
216 export const expectUrlContains = (
217 url: string,
218 marker: string,
219 options: ExpectUrlContainsOptions = {},
220 ) => {
221 const totalTimeout = Duration.fromInputUnsafe(options.timeout ?? "90 seconds");
222 const initial = options.initialBackoff ?? "750 millis";
223 const label = options.label ?? "url";
224
225 return fetchOnce(url, marker, options.headers).pipe(
226 Effect.retry({
227 // Cap individual sleeps at 8s so very long timeouts still
228 // sample at a reasonable rate near the end of the budget.
229 schedule: Schedule.min([Schedule.exponential(initial, 1.5), Schedule.spaced("8 seconds")]),
230 }),
231 // Bound the *total* retry budget. `Effect.retry` on its own would
232 // back off forever; the timeout guarantees the test fails loudly
233 // instead of stalling out the suite. We swap the default
234 // `TimeoutException` for a typed assertion error so the failure
235 // message says *what* we were waiting for, not just "timed out".
236 Effect.timeoutOrElse({
237 duration: totalTimeout,
238 orElse: () =>
239 Effect.fail(
240 new HttpAssertionFailed({
241 url,
242 marker,
243 status: 0,
244 bodyExcerpt: `[timed out after ${Duration.toMillis(totalTimeout)}ms waiting for marker "${marker}"]`,
245 }),
246 ),
247 }),
248 Effect.tapError((error) => Effect.logError(`expectUrlContains(${label}) failed`, error)),
249 );
250 };
251
252 /**
253 * Bounded readiness probe: the server behind `url` answers HTTP at all
254 * (any 2xx). Used by the local/dev suites, where there is no CDN to
255 * propagate through — only a dev server that needs a moment to listen.
256 */
257 export const expectUrlOk = (url: string) =>
258 Effect.tryPromise({
259 try: async (signal) => {
260 const response = await fetch(url, { signal, cache: "no-store" });
261 await response.arrayBuffer().catch(() => {});
262 if (!response.ok) {
263 throw new Error(`${url} responded ${response.status}`);
264 }
265 return response.status;
266 },
267 catch: (error) => new Error(String(error)),
268 }).pipe(
269 Effect.retry({ schedule: Schedule.spaced("1 second"), times: 30 }),
270 Effect.timeout("60 seconds"),
271 );
272
273 export class HttpResponseMismatch extends Data.TaggedError("HttpResponseMismatch")<{
274 url: string;
275 expected: string;
276 actual: string;
277 }> {}
278
279 const fetchOnceResponse = (
280 url: string,
281 check: (res: Response) => HttpResponseMismatch | undefined,
282 ) =>
283 Effect.tryPromise({
284 try: async (signal) => {
285 const u = new URL(url);
286 u.searchParams.set("__alchemy_cb", String(Date.now()));
287 const res = await fetch(u, {
288 signal,
289 cache: "no-store",
290 redirect: "manual",
291 headers: {
292 "cache-control": "no-cache",
293 pragma: "no-cache",
294 accept: "*/*",
295 // No keep-alive reuse: bun's fetch can wedge on a pooled
296 // connection the server closed after an error response — the
297 // reused request's promise neither settles nor honors its abort
298 // signal, pinning any timeout that awaits its interruption.
299 connection: "close",
300 },
301 });
302 const mismatch = check(res);
303 if (mismatch) {
304 throw mismatch;
305 }
306 return res;
307 },
308 catch: (e) =>
309 e instanceof HttpResponseMismatch
310 ? e
311 : new HttpFetchFailed({
312 url,
313 message: e instanceof Error ? e.message : String(e),
314 }),
315 });
316
317 const retryResponse = (
318 url: string,
319 expected: string,
320 effect: Effect.Effect<Response, HttpResponseMismatch | HttpFetchFailed>,
321 options: ExpectUrlContainsOptions,
322 ) => {
323 const totalTimeout = Duration.fromInputUnsafe(options.timeout ?? "90 seconds");
324 const initial = options.initialBackoff ?? "750 millis";
325 const label = options.label ?? "url";
326 return effect.pipe(
327 Effect.retry({
328 schedule: Schedule.min([Schedule.exponential(initial, 1.5), Schedule.spaced("8 seconds")]),
329 }),
330 Effect.timeoutOrElse({
331 duration: totalTimeout,
332 orElse: () =>
333 Effect.fail(
334 new HttpResponseMismatch({
335 url,
336 expected,
337 actual: `[timed out after ${Duration.toMillis(totalTimeout)}ms waiting for ${expected}]`,
338 }),
339 ),
340 }),
341 Effect.tapError((error) => Effect.logError(`expect response (${label}) failed`, error)),
342 );
343 };
344
345 /**
346 * Fetch `url` without following redirects and assert the response is a
347 * redirect to `location` (with `status`, default 301). Retries through
348 * deploy propagation like {@link expectUrlContains}.
349 */
350 export const expectUrlRedirect = (
351 url: string,
352 location: string,
353 options: ExpectUrlContainsOptions & { status?: number } = {},
354 ) => {
355 const status = options.status ?? 301;
356 const expected = `${status} -> ${location}`;
357 return retryResponse(
358 url,
359 expected,
360 fetchOnceResponse(url, (res) => {
361 // Cloudflare appends the request's query string to the Location
362 // header, so compare pathnames rather than the raw value.
363 const actual = res.headers.get("location");
364 const actualPath = actual === null ? null : new URL(actual, url).pathname;
365 return res.status === status && actualPath === location
366 ? undefined
367 : new HttpResponseMismatch({
368 url,
369 expected,
370 actual: `${res.status} -> ${actual}`,
371 });
372 }),
373 options,
374 );
375 };
376
377 /**
378 * Fetch `url` and assert response header `header` equals `value`.
379 * Retries through deploy propagation like {@link expectUrlContains}.
380 */
381 export const expectUrlHeader = (
382 url: string,
383 header: string,
384 value: string,
385 options: ExpectUrlContainsOptions = {},
386 ) => {
387 const expected = `${header}: ${value}`;
388 return retryResponse(
389 url,
390 expected,
391 fetchOnceResponse(url, (res) =>
392 res.headers.get(header) === value
393 ? undefined
394 : new HttpResponseMismatch({
395 url,
396 expected,
397 actual: `${res.status} ${header}: ${res.headers.get(header)}`,
398 }),
399 ),
400 options,
401 );
402 };
403
404 /**
405 * Fetch `url` WITHOUT following redirects and assert the immediate
406 * response status equals `expected` (and the body is not a Cloudflare
407 * placeholder page). Retries through deploy propagation like
408 * {@link expectUrlContains}. Use it to prove a URL serves (or redirects)
409 * *directly* rather than bouncing through a 3xx first.
410 */
411 export const expectDirectStatus = (
412 url: string,
413 expected: number,
414 options: ExpectUrlContainsOptions = {},
415 ) =>
416 retryResponse(
417 url,
418 `direct ${expected}`,
419 Effect.tryPromise({
420 try: async (signal) => {
421 const u = new URL(url);
422 u.searchParams.set("__alchemy_cb", String(Date.now()));
423 const res = await fetch(u, {
424 signal,
425 cache: "no-store",
426 redirect: "manual",
427 headers: {
428 "cache-control": "no-cache",
429 pragma: "no-cache",
430 accept: "*/*",
431 },
432 });
433 const body = await res.text();
434 if (res.status !== expected || looksLikeCloudflarePlaceholder(body)) {
435 throw new HttpResponseMismatch({
436 url,
437 expected: `direct ${expected}`,
438 actual: `${res.status} ${body.slice(0, 240)}`,
439 });
440 }
441 return res;
442 },
443 catch: (e) =>
444 e instanceof HttpResponseMismatch
445 ? e
446 : new HttpFetchFailed({
447 url,
448 message: e instanceof Error ? e.message : String(e),
449 }),
450 }),
451 options,
452 );
453
454 const fetchOnceAbsent = (url: string, marker: string) =>
455 Effect.tryPromise({
456 try: async (signal) => {
457 const u = new URL(url);
458 u.searchParams.set("__alchemy_cb", String(Date.now()));
459 const res = await fetch(u, {
460 signal,
461 cache: "no-store",
462 headers: {
463 "cache-control": "no-cache",
464 pragma: "no-cache",
465 accept: "*/*",
466 // No keep-alive reuse: bun's fetch can wedge on a pooled
467 // connection the server closed after an error response — the
468 // reused request's promise neither settles nor honors its abort
469 // signal, pinning any timeout that awaits its interruption.
470 connection: "close",
471 },
472 });
473 const body = await res.text();
474 if (body.includes(marker)) {
475 throw new HttpMarkerPresent({
476 url,
477 marker,
478 bodyExcerpt: body.slice(0, 240),
479 });
480 }
481 return body;
482 },
483 catch: (e) =>
484 e instanceof HttpMarkerPresent
485 ? e
486 : new HttpFetchFailed({
487 url,
488 message: e instanceof Error ? e.message : String(e),
489 }),
490 });
491
492 /**
493 * Fetch `url` and assert the response body does *not* contain `marker`.
494 * Retries while the marker is still present so a briefly-overbroad route
495 * fails loudly instead of slipping through on the first fetch.
496 */
497 export const expectUrlAbsent = (
498 url: string,
499 marker: string,
500 options: ExpectUrlContainsOptions = {},
501 ) => {
502 const totalTimeout = Duration.fromInputUnsafe(options.timeout ?? "90 seconds");
503 const initial = options.initialBackoff ?? "750 millis";
504 const label = options.label ?? "url";
505
506 return fetchOnceAbsent(url, marker).pipe(
507 Effect.retry({
508 schedule: Schedule.min([Schedule.exponential(initial, 1.5), Schedule.spaced("8 seconds")]),
509 }),
510 Effect.timeoutOrElse({
511 duration: totalTimeout,
512 orElse: () =>
513 Effect.fail(
514 new HttpMarkerPresent({
515 url,
516 marker,
517 bodyExcerpt: `[timed out after ${Duration.toMillis(totalTimeout)}ms waiting for marker "${marker}" to disappear]`,
518 }),
519 ),
520 }),
521 Effect.tapError((error) => Effect.logError(`expectUrlAbsent(${label}) failed`, error)),
522 );
523 };
524
525 /**
526 * Assert a build-time public env value was inlined into the client
527 * bundle a site deployed: find the client chunk under `clientDir` (the
528 * build's assets directory) that contains `marker`, then fetch that chunk
529 * from the deployed site.
530 */
531 export const expectClientChunkContains = (
532 url: string,
533 clientDir: string,
534 marker: string,
535 options: ExpectUrlContainsOptions = {},
536 ) =>
537 Effect.gen(function* () {
538 const fs = yield* FileSystem.FileSystem;
539 const path = yield* Path.Path;
540 const chunks = (yield* fs.readDirectory(clientDir, { recursive: true })).filter((file) =>
541 /\.m?js$/.test(file),
542 );
543 const inlined = yield* Effect.filter(chunks, (file) =>
544 fs
545 .readFileString(path.join(clientDir, file))
546 .pipe(Effect.map((code) => code.includes(marker))),
547 );
548 if (inlined.length === 0) {
549 return yield* new HttpAssertionFailed({
550 url,
551 marker,
552 status: 0,
553 bodyExcerpt: `[no client chunk under ${clientDir} contains "${marker}"]`,
554 });
555 }
556 yield* expectUrlContains(`${url}/${inlined[0]!.replaceAll("\\", "/")}`, marker, options);
557 });
558
559 export interface ExpectedResponse {
560 readonly status: number;
561 readonly headers: Headers;
562 readonly body: string;
563 }
564
565 /**
566 * Fetch `url` without following redirects and retry until `check` accepts
567 * the response (returns `undefined`); `check` returns a description of what
568 * is wrong otherwise. For assertions on status codes, headers and redirect
569 * responses the body-marker helpers can't express (404 pages, `Location`).
570 */
571 export const expectResponse = (
572 url: string,
573 check: (response: ExpectedResponse) => string | undefined,
574 options: ExpectUrlContainsOptions = {},
575 ) => {
576 const expected = options.label ?? "response check";
577 const totalTimeout = Duration.fromInputUnsafe(options.timeout ?? "90 seconds");
578 // Reported on timeout, so a failure shows what the URL actually returned.
579 let lastActual = "no response";
580 return Effect.tryPromise({
581 try: async (signal) => {
582 const u = new URL(url);
583 u.searchParams.set("__alchemy_cb", String(Date.now()));
584 const res = await fetch(u, {
585 signal,
586 cache: "no-store",
587 redirect: "manual",
588 headers: {
589 "cache-control": "no-cache",
590 pragma: "no-cache",
591 accept: "*/*",
592 connection: "close",
593 ...options.headers,
594 },
595 });
596 const body = await res.text();
597 const mismatch = looksLikeCloudflarePlaceholder(body)
598 ? "Cloudflare placeholder page"
599 : check({ status: res.status, headers: res.headers, body });
600 if (mismatch !== undefined) {
601 lastActual = `${res.status} ${mismatch} :: ${body.replace(/\s+/g, " ").slice(0, 160)}`;
602 throw new HttpResponseMismatch({ url, expected, actual: lastActual });
603 }
604 return res;
605 },
606 catch: (e) =>
607 e instanceof HttpResponseMismatch
608 ? e
609 : new HttpFetchFailed({ url, message: e instanceof Error ? e.message : String(e) }),
610 }).pipe(
611 Effect.retry({
612 schedule: Schedule.min([
613 Schedule.exponential(options.initialBackoff ?? "750 millis", 1.5),
614 Schedule.spaced("8 seconds"),
615 ]),
616 }),
617 Effect.timeoutOrElse({
618 duration: totalTimeout,
619 orElse: () =>
620 Effect.fail(
621 new HttpResponseMismatch({
622 url,
623 expected,
624 actual: `[timed out after ${Duration.toMillis(totalTimeout)}ms; last: ${lastActual}]`,
625 }),
626 ),
627 }),
628 Effect.tapError((error) => Effect.logError(`expectResponse(${expected}) failed`, error)),
629 );
630 };