kunibert
All repositories: gitoria
2.5 KB
// JS target; redirects are followed explicitly. Network reasons are limited by the host Fetch API.function reason(e, timedOut) {if (timedOut) return 'timed out';const code = e?.cause?.code || e?.code;if (code === 'ECONNREFUSED') return 'connection refused';if (code === 'ENOTFOUND' || code === 'EAI_AGAIN') return 'could not resolve host';if (code === 'ECONNRESET' || code === 'UND_ERR_SOCKET') return 'connection reset by peer';if (code === 'ETIMEDOUT' || code === 'UND_ERR_CONNECT_TIMEOUT') return 'timed out';return e?.cause?.message || e?.message || String(e);}async function exchange(url, options, tolerant) {const o = options || {};const method = (o.method || 'GET').toUpperCase();let requestMethod = method;const controller = new AbortController();let timedOut = false;const timer = setTimeout(() => { timedOut = true; controller.abort(); }, o.timeoutMs ?? 30000);try {const headers = new Headers(o.headers || {});let body = o.body;if (o.json != null) {body = JSON.stringify(o.json);if (!headers.has('content-type')) headers.set('content-type', 'application/json');}let target = url;const limit = o.maxRedirects ?? 5;for (let hop = 0; ; hop++) {const r = await globalThis.fetch(target, { method: requestMethod, headers, body, signal: controller.signal, redirect: 'manual' });const location = r.headers.get('location');if ([301, 302, 303, 307, 308].includes(r.status) && location && limit !== 0) {await r.body?.cancel();if (hop >= limit) throw new Error('too many redirects');const next = new URL(location, target);if (next.origin !== new URL(target).origin) {for (const name of ['authorization', 'proxy-authorization', 'cookie']) headers.delete(name);}if (r.status === 303 || ([301, 302].includes(r.status) && !['GET', 'HEAD'].includes(requestMethod))) {requestMethod = 'GET';body = undefined;headers.delete('content-type');headers.delete('content-length');}target = next.href;continue;}const text = await r.text();return { status: r.status, ok: r.ok, url: r.url, headers: Object.fromEntries(r.headers),body: text, tag: o.tag || '', text() { return text; }, json() { return JSON.parse(text); } };}} catch (e) {const error = `hl:fetch ${method} ${url}: ${reason(e, timedOut)}`;if (tolerant) return { ok: false, status: 0, error };throw new Error(error);} finally { clearTimeout(timer); }}export function fetch(url, options) { return exchange(url, options, false); }export function tryFetch(url, options) { return exchange(url, options, true); }
Branches
- design-guard
- fresh-on-nocmd
- hl-standard
- hl-standard-l10
- hl-standard-l10b
- job-timeout
- mainmain branch
- revendor-day44
Latest commits
- 402bfeb0|||PR runner: the model run uses the job's time limit (jobTimeoutSeconds), not a fixed 900 smre
- 8d34ca33records: repo rename (gitoria#53)mre
- 1e05cb3fMerge branch 'repo-rename' into mainKunibert
- 6ee0d2e9|||PR intake: match a delivery by repo.id and signature, not URLs (gitoria#53 rename)mre
- 900ffcd4status: routgermre
- 59b0b793log: routger merge waitmre
- e52f094alog: routger intake check (hycon#122)mre
- f979027frecords: design rule livemre
- 71efaf84Merge branch 'design-guard' into mainKunibert
- c0656581|||PR Flag removal of a documented design as major unless his recorded yes is linked; rules version 5mre
- 901cdd75records: L10 update 2 livemre
- 4a68016bMerge branch 'hl-standard-l10b' into mainKunibert
- 246d0f30|||PR HL-STANDARD L10 update (hybriel ab293e5a), rules version 4mre
- 1aa4d64crecords: HL-STANDARD L10 update livemre
- 2b04c240Merge branch 'hl-standard-l10' into mainKunibert
- 88c7b09d|||PR HL-STANDARD L10 update (hybriel 5885d688), rules version 3mre
- 00869ecbrecords: gitoria#50 step 3 livemre
- f97a0e5fMerge branch 'hl-standard' into mainKunibert
- 9710e39fType STANDARD as String (L10, Kunibert review)mre
- 4140b664|||PR Review against HL-STANDARD and cite rule ids (gitoria#50 step 3)mre