Q91: sign-in and renewal requests end on cancel or a stalled server

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 15:17:31 -04:00
1 parent bcab38f003
commit 6655982368
10 files changed
+140 -67

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
import { ProviderHttpError } from "../provider-http";
/** How long one sign-in or renewal request may take, body included. */
export const OAUTH_REQUEST_TIMEOUT_MS = 30_000;
/** A sign-in or renewal request that ends on the user's cancel or a stalled server; runs stop only their own wait, since refreshes are shared. */
export async function oauthFetch(url: string, init: RequestInit, cancel?: AbortSignal): Promise<{ response: Response; text: string }> {
const controller = new AbortController();
const abort = () => controller.abort();
if (cancel?.aborted) controller.abort();
else cancel?.addEventListener("abort", abort, { once: true });
const timer = setTimeout(abort, OAUTH_REQUEST_TIMEOUT_MS);
try {
const response = await fetch(url, { ...init, signal: controller.signal });
return { response, text: await response.text() };
} catch (error) {
if (cancel?.aborted) throw new Error("Login cancelled");
if (controller.signal.aborted) throw new ProviderHttpError(`Sign-in request to ${new URL(url).host} timed out.`, null, null, true);
throw error;
} finally {
clearTimeout(timer);
cancel?.removeEventListener("abort", abort);
}
}