/** * Fake only GitHub's network boundary. OAuth state, PKCE, process-local sessions, * channel authorization and WebSocket admission still run in the real server. */ let network = globalThis.fetch; const repositories = [ { node_id: "R_score", owner: { login: "octo-org", avatar_url: "https://example.invalid/octo-org.png" }, name: "score", full_name: "octo-org/score", private: true, html_url: "https://github.com/octo-org/score", default_branch: "main", permissions: { pull: true, push: true, admin: false }, }, { node_id: "R_notes", owner: { login: "octocat", avatar_url: "https://example.invalid/octocat.png" }, name: "notes", full_name: "octocat/notes", private: false, html_url: "https://github.com/octocat/notes", default_branch: "main", permissions: { pull: true, push: false, admin: false }, }, ...Array.from({ length: 12 }, (_, index) => ({ node_id: `R_archive_${index + 1}`, owner: { login: "octo-org", avatar_url: "https://example.invalid/octo-org.png" }, name: `archive-${index + 1}`, full_name: `octo-org/archive-${index + 1}`, private: false, html_url: `https://github.com/octo-org/archive-${index + 1}`, default_branch: "main", permissions: { pull: true, push: false, admin: false }, })), ]; const installations = [ { id: 101, account: { login: "octo-org", avatar_url: "https://example.invalid/octo-org.png", type: "Organization", }, repository_selection: "selected", html_url: "https://github.com/settings/installations/101", suspended_at: null, permissions: { contents: "read", pull_requests: "read", checks: "read", statuses: "read" }, }, { id: 102, account: { login: "octocat", avatar_url: "https://example.invalid/octocat.png", type: "User", }, repository_selection: "selected", html_url: "https://github.com/settings/installations/102", suspended_at: null, permissions: { contents: "read", pull_requests: "read", checks: "read", statuses: "read" }, }, ]; function json(value: unknown, init: ResponseInit = {}): Response { return Response.json(value, init); } let fake = async (input: Parameters[0], init?: Parameters[1]) => { let url = new URL(input instanceof Request ? input.url : input); if (url.href === "https://github.com/login/oauth/access_token") { let body = new URLSearchParams(String(init?.body ?? "")); let refreshing = body.get("grant_type") === "refresh_token"; let refreshed = /^ghr_e2e_(.+)_(\d+)$/.exec(body.get("refresh_token") ?? ""); if (refreshing && !refreshed) return json({ error: "bad_refresh_token" }); let handle = refreshing ? refreshed![1]! : (body.get("code") ?? "e2e-person").replace(/^e2e-/, ""); if (refreshing && handle === "expired") return json({ error: "bad_refresh_token" }); let revision = refreshing ? Number(refreshed![2]) + 1 : 1; return json({ access_token: `ghu_e2e_${handle}_${revision}`, expires_in: 28_800, refresh_token: `ghr_e2e_${handle}_${revision}`, refresh_token_expires_in: 15_897_600, token_type: "bearer", }); } if (url.origin === "https://api.github.com") { let requestHeaders = new Headers(init?.headers); let authorization = requestHeaders.get("authorization") ?? ""; let authorized = /^Bearer ghu_e2e_(.+)_\d+$/.exec(authorization); if (!authorized) return json({ message: "Bad credentials" }, { status: 401 }); let handle = authorized[1]!; let tagged = (value: unknown, etag: string, responseInit: ResponseInit = {}) => { let headers = new Headers(responseInit.headers); headers.set("etag", etag); if (requestHeaders.get("if-none-match") === etag) { return new Response(null, { status: 304, headers }); } return json(value, { ...responseInit, headers }); }; if (url.pathname === "/user") { return json({ node_id: `U_${handle}`, login: handle, avatar_url: `https://example.invalid/${handle}.png`, }); } if (url.pathname === "/user/memberships/orgs/githubnext") { if (handle === "outsider") return json({ message: "Not Found" }, { status: 404 }); return json({ state: handle === "pending" ? "pending" : "active", role: "member", }); } if (handle === "expired") return json({ message: "Bad credentials" }, { status: 401 }); if (url.pathname === "/user/installations") { return tagged({ installations }, `"installations-${handle}"`); } if (url.pathname === "/user/installations/101/repositories") { let available = repositories.filter(value => value.owner.login === "octo-org"); if (handle === "readonly") { available = available.map(value => ({ ...value, permissions: { ...value.permissions, push: false }, })); } if (handle === "paged" && url.searchParams.get("page") !== "2") { return tagged( { repositories: available.slice(0, 1) }, `"repositories-${handle}-101-1"`, { headers: { link: '; rel="next"', }, }, ); } return tagged( { repositories: handle === "paged" ? available.slice(1) : available }, `"repositories-${handle}-101-${url.searchParams.get("page") ?? "1"}"`, ); } if (url.pathname === "/user/installations/102/repositories") { return tagged( { repositories: repositories.filter(value => value.owner.login === "octocat") }, `"repositories-${handle}-102-${url.searchParams.get("page") ?? "1"}"`, ); } let repository = repositories.find(value => url.pathname === `/repos/${value.full_name}`); if (repository) return json(repository); return json({ message: "Not Found" }, { status: 404 }); } return network(input, init); }; globalThis.fetch = Object.assign(fake, { preconnect: network.preconnect });