Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

backendReadiness.ts

107 lines3.0 KB

1export interface WaitForHttpReadyOptions {
2 readonly timeoutMs?: number;
3 readonly intervalMs?: number;
4 readonly requestTimeoutMs?: number;
5 readonly fetchImpl?: typeof fetch;
6 readonly signal?: AbortSignal;
7 readonly path?: string;
8 readonly isReady?: (response: Response) => boolean;
9}
10​
11const DEFAULT_TIMEOUT_MS = 30_000;
12const DEFAULT_INTERVAL_MS = 100;
13const DEFAULT_REQUEST_TIMEOUT_MS = 1_000;
14​
15export class BackendReadinessAbortedError extends Error {
16 constructor() {
17 super("Backend readiness wait was aborted.");
18 this.name = "BackendReadinessAbortedError";
19 }
21​
22function delay(ms: number, signal: AbortSignal | undefined): Promise<void> {
23 return new Promise<void>((resolve, reject) => {
24 const timer = setTimeout(() => {
25 cleanup();
26 resolve();
27 }, ms);
28​
29 const onAbort = () => {
30 cleanup();
31 reject(new BackendReadinessAbortedError());
32 };
33​
34 const cleanup = () => {
35 clearTimeout(timer);
36 signal?.removeEventListener("abort", onAbort);
37 };
38​
39 if (signal?.aborted) {
40 cleanup();
41 reject(new BackendReadinessAbortedError());
42 return;
43 }
44​
45 signal?.addEventListener("abort", onAbort, { once: true });
46 });
48​
49export function isBackendReadinessAborted(error: unknown): error is BackendReadinessAbortedError {
50 return error instanceof BackendReadinessAbortedError;
52​
53export async function waitForHttpReady(
54 baseUrl: string,
55 options?: WaitForHttpReadyOptions,
56): Promise<void> {
57 const fetchImpl = options?.fetchImpl ?? fetch;
58 const signal = options?.signal;
59 const timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
60 const intervalMs = options?.intervalMs ?? DEFAULT_INTERVAL_MS;
61 const requestTimeoutMs = options?.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
62 const readinessPath = options?.path ?? "/";
63 const isReady = options?.isReady ?? ((response: Response) => response.ok);
64 const deadline = Date.now() + timeoutMs;
65​
66 for (;;) {
67 if (signal?.aborted) {
68 throw new BackendReadinessAbortedError();
69 }
70​
71 const requestController = new AbortController();
72 const requestTimeout = setTimeout(() => {
73 requestController.abort();
74 }, requestTimeoutMs);
75 const abortRequest = () => {
76 requestController.abort();
77 };
78 signal?.addEventListener("abort", abortRequest, { once: true });
79​
80 try {
81 const response = await fetchImpl(new URL(readinessPath, baseUrl).toString(), {
82 redirect: "manual",
83 signal: requestController.signal,
84 });
85 if (isReady(response)) {
86 return;
87 }
88 } catch (error) {
89 if (isBackendReadinessAborted(error)) {
90 throw error;
91 }
92 if (signal?.aborted) {
93 throw new BackendReadinessAbortedError();
94 }
95 // Retry until the backend becomes reachable or the deadline expires.
96 } finally {
97 clearTimeout(requestTimeout);
98 signal?.removeEventListener("abort", abortRequest);
99 }
100​
101 if (Date.now() >= deadline) {
102 throw new Error(`Timed out waiting for backend readiness at ${baseUrl}.`);
103 }
104​
105 await delay(intervalMs, signal);
106 }