MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / openOAuthSystemBrowser

Function openOAuthSystemBrowser

packages/react/src/api/oauth-popup.ts:251–311  ·  view source on GitHub ↗
(
  input: OpenOAuthSystemBrowserInput<TAuth>,
)

Source from the content-addressed store, hash-verified

249const OAUTH_AWAIT_DEFAULT_TIMEOUT_MS = 10 * 60 * 1000;
250
251export const openOAuthSystemBrowser = <TAuth>(
252 input: OpenOAuthSystemBrowserInput<TAuth>,
253): (() => void) => {
254 let settled = false;
255 let pollHandle: ReturnType<typeof setInterval> | null = null;
256 let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
257 const controller = new AbortController();
258
259 const settle = () => {
260 if (settled) return;
261 settled = true;
262 if (pollHandle !== null) clearInterval(pollHandle);
263 if (timeoutHandle !== null) clearTimeout(timeoutHandle);
264 controller.abort();
265 };
266
267 const poll = async () => {
268 if (settled) return;
269 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: fetch can reject for transient network errors during polling
270 try {
271 // The await poll is now gated like the rest of /api — carry the bearer
272 // (standalone web). On desktop the connection has no client-side auth and
273 // the main process injects the header instead.
274 const authorization = getExecutorServerAuthorizationHeader();
275 const response = await fetch(`/api/oauth/await/${encodeURIComponent(input.sessionId)}`, {
276 signal: controller.signal,
277 cache: "no-store",
278 ...(authorization ? { headers: { authorization } } : {}),
279 });
280 if (!response.ok) return;
281 const body = (await response.json()) as unknown;
282 if (body === null || settled) return;
283 if (!isOAuthPopupResult<TAuth>(body)) return;
284 settle();
285 input.onResult(body);
286 } catch {
287 // Transient — next tick will retry. AbortError after settle is also caught here.
288 }
289 };
290
291 void (async () => {
292 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: openExternal is host-provided IPC, no Effect runtime in this browser-only helper
293 try {
294 await input.openExternal(input.url);
295 } catch (cause: unknown) {
296 if (settled) return;
297 settle();
298 input.onOpenFailed?.(cause);
299 }
300 })();
301
302 pollHandle = setInterval(() => void poll(), input.pollMs ?? OAUTH_AWAIT_DEFAULT_POLL_MS);
303 void poll();
304 timeoutHandle = setTimeout(() => {
305 if (settled) return;
306 settle();
307 input.onTimeout?.();
308 }, input.timeoutMs ?? OAUTH_AWAIT_DEFAULT_TIMEOUT_MS);

Callers 1

useOAuthPopupFlowFunction · 0.90

Calls 2

pollFunction · 0.85
settleFunction · 0.70

Tested by

no test coverage detected