|
| 1 | +import type { GithubObjectCache, GithubReadFailure, PublicGithubSourceClient } from './github-source' |
| 2 | +import type { ResolutionRow } from './state' |
| 3 | +import { decryptToken } from '#layers/identity/server/utils/crypto' |
| 4 | +import { createPublicGithubSourceClient } from './github-source' |
| 5 | + |
| 6 | +/** |
| 7 | + * Record the signed-in account that asked for a public Resolution. |
| 8 | + * |
| 9 | + * Only that Resolution's build reads it, and only after the shared GitHub |
| 10 | + * credentials are rate limited. The row goes when the build settles. |
| 11 | + */ |
| 12 | +export async function recordResolutionRequester( |
| 13 | + db: D1Database, |
| 14 | + input: { resolutionId: string, accountId: number, now: number }, |
| 15 | +): Promise<void> { |
| 16 | + await db.prepare( |
| 17 | + `INSERT OR IGNORE INTO artifact_resolution_requesters (resolution_id, account_id, created_at) |
| 18 | + VALUES (?1, ?2, ?3)`, |
| 19 | + ).bind(input.resolutionId, input.accountId, input.now).run() |
| 20 | +} |
| 21 | + |
| 22 | +/** Forget who asked for a Resolution. Its build has settled. */ |
| 23 | +export async function forgetResolutionRequester(db: D1Database, resolutionId: string): Promise<void> { |
| 24 | + await db.prepare(`DELETE FROM artifact_resolution_requesters WHERE resolution_id = ?1`).bind(resolutionId).run() |
| 25 | +} |
| 26 | + |
| 27 | +export interface RequesterGithubDependencies { |
| 28 | + db: D1Database |
| 29 | + /** |
| 30 | + * `NUXT_TOKEN_KEY`, which encrypts the stored GitHub OAuth tokens. It is read |
| 31 | + * only when a requester's token is needed. |
| 32 | + */ |
| 33 | + tokenKey: () => string |
| 34 | + fetch: typeof globalThis.fetch |
| 35 | + /** Unix seconds. */ |
| 36 | + now: () => number |
| 37 | + /** Commits and trees by SHA, shared with the build's own client. */ |
| 38 | + cache?: GithubObjectCache |
| 39 | + onReadFailure?: (failure: GithubReadFailure) => void |
| 40 | + /** Called when a stored token cannot be used. It never receives the token. */ |
| 41 | + onUnusableToken?: (reason: string) => void |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * The GitHub client of the account that asked for one public Resolution, or |
| 46 | + * null when nobody signed in asked for it, or the account holds no usable |
| 47 | + * GitHub token. |
| 48 | + * |
| 49 | + * The token is the account's GitHub sign-in token. Its scopes, `read:user` and |
| 50 | + * `user:email`, read public Repositories at that account's own quota, and no |
| 51 | + * private one. The public client also refuses any Repository GitHub reports |
| 52 | + * as private, so a build read with it can only produce public content. |
| 53 | + */ |
| 54 | +export function createRequesterGithubSource( |
| 55 | + dependencies: RequesterGithubDependencies, |
| 56 | +): (row: ResolutionRow) => Promise<PublicGithubSourceClient | null> { |
| 57 | + return async (row) => { |
| 58 | + if (row.visibility !== 'public') |
| 59 | + return null |
| 60 | + const stored = await dependencies.db.prepare( |
| 61 | + `SELECT u.github_token_encrypted, u.github_token_expires_at |
| 62 | + FROM artifact_resolution_requesters r |
| 63 | + JOIN users u ON u.id = r.account_id |
| 64 | + WHERE r.resolution_id = ?1 |
| 65 | + LIMIT 1`, |
| 66 | + ).bind(row.id).first<{ github_token_encrypted: string | null, github_token_expires_at: number | null }>() |
| 67 | + if (!stored?.github_token_encrypted) |
| 68 | + return null |
| 69 | + if (stored.github_token_expires_at !== null && stored.github_token_expires_at <= dependencies.now() + 60) |
| 70 | + return null |
| 71 | + const token = await decryptToken(stored.github_token_encrypted, dependencies.tokenKey()).then( |
| 72 | + value => ({ _tag: 'token' as const, value }), |
| 73 | + (error: unknown) => ({ _tag: 'unreadable' as const, reason: error instanceof Error ? error.message : String(error) }), |
| 74 | + ) |
| 75 | + if (token._tag === 'unreadable') { |
| 76 | + dependencies.onUnusableToken?.(`The stored GitHub token did not decrypt: ${token.reason}`.slice(0, 200)) |
| 77 | + return null |
| 78 | + } |
| 79 | + return createPublicGithubSourceClient({ |
| 80 | + fetch: dependencies.fetch, |
| 81 | + token: token.value, |
| 82 | + cache: dependencies.cache, |
| 83 | + onReadFailure: dependencies.onReadFailure, |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * A client that reads with the shared credential, and repeats a read the |
| 90 | + * shared quota refused with the requester's own client. |
| 91 | + * |
| 92 | + * Every other answer, success or rejection, stays the shared one, so a |
| 93 | + * requester's token is spent only when nothing else could read. |
| 94 | + */ |
| 95 | +export function withRequesterFallback( |
| 96 | + shared: PublicGithubSourceClient, |
| 97 | + requester: () => Promise<PublicGithubSourceClient | null>, |
| 98 | +): PublicGithubSourceClient { |
| 99 | + let own: Promise<PublicGithubSourceClient | null> | null = null |
| 100 | + const ownClient = () => { |
| 101 | + own ??= requester() |
| 102 | + return own |
| 103 | + } |
| 104 | + return { |
| 105 | + async resolve(request) { |
| 106 | + const first = await shared.resolve(request) |
| 107 | + if (first._tag !== 'rejected' || first.code !== 'RATE_LIMITED') |
| 108 | + return first |
| 109 | + const client = await ownClient() |
| 110 | + return client ? await client.resolve(request) : first |
| 111 | + }, |
| 112 | + async load(source) { |
| 113 | + const first = await shared.load(source) |
| 114 | + if (first._tag !== 'rejected' || first.code !== 'RATE_LIMITED') |
| 115 | + return first |
| 116 | + const client = await ownClient() |
| 117 | + return client ? await client.load(source) : first |
| 118 | + }, |
| 119 | + } |
| 120 | +} |
0 commit comments