Something went wrong. Try again.
move github sites
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103// Minimal GitHub REST client. Auth comes from `gh auth token` so we inherit the// gh CLI's keyring rather than asking for a PAT.
import { execFile } from 'node:child_process'import { promisify } from 'node:util'
const exec = promisify(execFile)
const API = 'https://api.github.com'
let token = null
export async function ghToken() { if (token) return token const { stdout } = await exec('gh', ['auth', 'token']) token = stdout.trim() if (!token) throw new Error('no token from `gh auth token` — run `gh auth login`') return token}
export async function ghUser() { const { stdout } = await exec('gh', ['api', 'user', '--jq', '.login']) return stdout.trim()}
// Returns { ok, status, body }. 404 is a normal answer here (no Pages, no file),// not an exception, so callers branch on status instead of catching.export async function api(path, { method = 'GET', raw = false } = {}) { const t = await ghToken() const url = path.startsWith('http') ? path : API + path for (let attempt = 0; ; attempt++) { const res = await fetch(url, { method, headers: { authorization: `Bearer ${t}`, accept: raw ? 'application/vnd.github.raw' : 'application/vnd.github+json', 'x-github-api-version': '2022-11-28', 'user-agent': 'entangle', }, })
// GitHub's rate limits are a remote server's clock; it states how long to // wait, and waiting is the only way through. The primary limit sends // x-ratelimit-remaining: 0 and a reset time. The secondary limit sends // retry-after, or neither header, in which case GitHub's guidance is to // wait at least a minute and back off further on repeats. if ((res.status === 403 || res.status === 429) && attempt < 4) { const reset = Number(res.headers.get('x-ratelimit-reset')) const remaining = res.headers.get('x-ratelimit-remaining') const retryAfter = Number(res.headers.get('retry-after')) let waitMs = null if (retryAfter > 0) waitMs = retryAfter * 1000 else if (remaining === '0' && reset) waitMs = Math.max(1000, reset * 1000 - Date.now() + 1000) else if (res.status === 429 || /secondary rate limit/i.test(await res.clone().text())) waitMs = 60_000 * 2 ** attempt if (waitMs !== null) { process.stderr.write(`rate limited, sleeping ${Math.round(waitMs / 1000)}s\n`) await new Promise((r) => setTimeout(r, waitMs)) continue } } if (res.status >= 500 && attempt < 3) { await new Promise((r) => setTimeout(r, 500 * 2 ** attempt)) continue }
const body = raw ? await res.text() : res.status === 204 ? null : await res.json().catch(() => null) return { ok: res.ok, status: res.status, body, link: res.headers.get('link') } }}
export async function paginate(path) { const out = [] let url = path + (path.includes('?') ? '&' : '?') + 'per_page=100' while (url) { const res = await api(url) if (!res.ok) throw new Error(`${url} → ${res.status}`) out.push(...res.body) const next = /<([^>]+)>;\s*rel="next"/.exec(res.link || '') url = next ? next[1] : null } return out}
// Bounded-concurrency map. GitHub tolerates ~8 in flight comfortably.export async function pmap(items, limit, fn) { const results = new Array(items.length) let i = 0 const workers = Array.from({ length: Math.min(limit, items.length) }, async () => { while (true) { const idx = i++ if (idx >= items.length) return results[idx] = await fn(items[idx], idx) } }) await Promise.all(workers) return results}