gitoria
All repositories: gitoria
2.9 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');}// Generated modules use the runtime's Bytes wrapper; direct JS uses Uint8Array.if (body?._d instanceof Uint8Array) {const { hlIsBytes } = await import('../hl-runtime.js');if (hlIsBytes(body)) body = body._d;}if (body != null && typeof body !== 'string' && !(body instanceof Uint8Array)) {throw new Error('body must be a String or Bytes (use json for 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
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 7b88cfe0Type publicReaderOfRequest parameters (L10)Gitoria
- 2a4b3dc1|||PR Anonymous reads of public repos' pulls and statusesmre
- 21b2c141Merge branch 's25-no-session-on-login-error' into mainGitoria
- 698458e2|||PR No session for cookieless login errors: READMEmre
- cce7b785|||PR No session for cookieless login errorsmre
- 678d6303Merge branch 'v59' into mainGitoria
- 5803cf4f|||PR vendor: Hybriel set-day59 and GC budget 32 MiBmre
- 9eff6627Merge branch 'f2-api-bugs' into mainGitoria
- 26117255|||PR apps API: review comments on files with spaces in their namemre
- b8796cecMerge branch 'f1-broken-json' into mainGitoria
- 2f2959c5|||PR apps API: broken JSON body gives 400 invalid_json, not 500mre
- f7c6ca07Merge branch 'ident-chooser-selector' into mainGitoria
- 691abb8e|||PR tests: find ident identity choice without the removed .choose classmre
- 16a3530fMerge branch 'heap-revert' into mainGitoria
- 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
- 13f0c342Merge branch 'hl25-records' into mainGitoria
- a002ecb7|||PR records: HL25 report, heap helper, logmre
- 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
- fefaedd2log: tracker PR-only, HL25 startedmre
- 21aa57calog: parkmre