fix(github): bound and reuse app token fetches

Repeated token exchanges waste requests and JWT work for every event.
Reuse valid installation tokens, share refreshes and back off briefly
after failures while keeping every shared wait bounded.
This commit is contained in:
翅膀 committed 2026-10-23 23:21:38 +08:00
1 parent 2d87367b48
commit d5e86bb4d9
2 files changed
+73 -3

No files matched your search

+72 -2
View File
@@ -1,25 +1,89 @@
import { createAppJwt } from "./oauth";
import { sharedFlight, type Flight } from "../lib/shared-flight";
export const GITHUB_API_VERSION = "2022-11-28";
/**
* Longest an optional enrichment call (installation token, jobs, check runs)
* may take, its body included: a hanging one must not hold up the delivery.
*/
export const ENRICH_TIMEOUT_MS = 10_000;
export const GITHUB_USER_AGENT = "WebHooker (https://github.com/Elysola/WebHooker)";
/**
* A payload-supplied API URL (`jobs_url`, `check_runs_url`) parsed only when
* it points at the GitHub REST API's repository endpoints. Anything else — a
* forged payload aiming a token at its own server — yields null.
*/
export function githubApiUrl(value: string | undefined): URL | null {
if (!value) return null;
try {
const url = new URL(value);
if (url.protocol !== "https:" || url.host !== "api.github.com") return null;
if (!url.pathname.startsWith("/repos/") || url.username || url.password) return null;
return url;
} catch {
return null;
}
}
/**
* Mint a short-lived installation access token for a GitHub App installation.
* Returns undefined when the App credentials are missing or the exchange
* fails. Best-effort: callers must not depend on it.
*/
/**
* Installation tokens this isolate holds (until shortly before they expire),
* and exchanges in flight: one per installation, not one per event — each is
* a subrequest plus a JWT signature.
*/
const installationTokens = new Map<string, { token: string; until: number }>();
const exchanging = new Map<string, Flight<string | undefined>>();
/**
* Installations whose exchange just failed, until when: enrichment is
* optional, so the events right after go without it instead of each signing
* a JWT and asking GitHub again.
*/
const failedExchanges = new Map<string, number>();
const FAILED_EXCHANGE_MS = 60_000;
export async function getInstallationToken(
appId: string | undefined,
privateKey: string | undefined,
installationId: number | undefined,
): Promise<string | undefined> {
if (!appId || !privateKey || !installationId) return undefined;
const key = `${appId}:${installationId}`;
const held = installationTokens.get(key);
if (held && Date.now() < held.until) return held.token;
if ((failedExchanges.get(key) ?? 0) > Date.now()) return undefined;
return sharedFlight(
exchanging,
key,
async () => {
const token = await exchangeInstallationToken(appId, privateKey, installationId);
if (token) failedExchanges.delete(key);
else {
if (failedExchanges.size >= 1000) failedExchanges.clear();
failedExchanges.set(key, Date.now() + FAILED_EXCHANGE_MS);
}
return token;
},
ENRICH_TIMEOUT_MS + 5_000,
);
}
async function exchangeInstallationToken(
appId: string,
privateKey: string,
installationId: number,
): Promise<string | undefined> {
try {
const jwt = await createAppJwt(appId, privateKey);
const res = await fetch(
`https://api.github.com/app/installations/${installationId}/access_tokens`,
{
method: "POST",
signal: AbortSignal.timeout(ENRICH_TIMEOUT_MS),
headers: {
Authorization: `Bearer ${jwt}`,
Accept: "application/vnd.github+json",
@@ -29,8 +93,14 @@ export async function getInstallationToken(
},
);
if (!res.ok) return undefined;
const { token } = (await res.json()) as { token?: string };
return token ?? undefined;
const { token, expires_at } = (await res.json()) as { token?: string; expires_at?: string };
if (!token) return undefined;
const expires = Date.parse(expires_at ?? "");
// Tokens last an hour; given back 5 minutes early (a request in flight
// never carries one that just expired).
const until = (Number.isFinite(expires) ? expires : Date.now() + 3600_000) - 5 * 60_000;
if (until > Date.now()) installationTokens.set(`${appId}:${installationId}`, { token, until });
return token;
} catch {
return undefined;
}
+1 -1
View File
@@ -116,7 +116,7 @@ describe("getWorkflowRunJobs", () => {
);
});
expect(await getWorkflowRunJobs(JOBS_URL, appId, pem, 555)).toBeDefined();
expect(await getWorkflowRunJobs(JOBS_URL, appId, pem, 5551)).toBeDefined();
expect(tokenUA).toBeTruthy();
expect(sawUA).toBeTruthy();
});