export async function fetchWithRetry(opts) {
    const f = opts.fetchImpl ?? fetch;
    const sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
    const max = opts.maxRetries ?? 3;
    let attempt = 0;
    while (true) {
        const r = await f(opts.url, opts.init);
        if (r.status !== 429)
            return r;
        if (attempt >= max)
            throw new Error(`rate-limited after ${attempt + 1} attempts: ${opts.url}`);
        const ra = r.headers.get('retry-after');
        const delaySec = ra ? Number(ra) : Math.pow(2, attempt);
        await sleep(Math.max(0, delaySec) * 1000);
        attempt++;
    }
}
//# sourceMappingURL=ratelimit.js.map