gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitda60547fda60547fstatus: GC1 done, backup bugmreda60547f/plugins/fetch/server.js

2.0 KB

  1. // JS target and browser twin. Network reasons are limited by the host Fetch API.
  2. function reason(e, timedOut) {
  3. if (timedOut) return 'timed out';
  4. const code = e?.cause?.code || e?.code;
  5. if (code === 'ECONNREFUSED') return 'connection refused';
  6. if (code === 'ENOTFOUND' || code === 'EAI_AGAIN') return 'could not resolve host';
  7. if (code === 'ECONNRESET' || code === 'UND_ERR_SOCKET') return 'connection reset by peer';
  8. if (code === 'ETIMEDOUT' || code === 'UND_ERR_CONNECT_TIMEOUT') return 'timed out';
  9. return e?.cause?.message || e?.message || String(e);
  10. }
  11. async function exchange(url, options, tolerant) {
  12. const o = options || {};
  13. const method = o.method || 'GET';
  14. const controller = new AbortController();
  15. let timedOut = false;
  16. const timer = setTimeout(() => { timedOut = true; controller.abort(); }, o.timeoutMs ?? 30000);
  17. try {
  18. const headers = new Headers(o.headers || {});
  19. let body = o.body;
  20. if (o.json != null) {
  21. body = JSON.stringify(o.json);
  22. if (!headers.has('content-type')) headers.set('content-type', 'application/json');
  23. }
  24. let target = url;
  25. const limit = o.maxRedirects ?? 5;
  26. for (let hop = 0; ; hop++) {
  27. const r = await globalThis.fetch(target, { method, headers, body, signal: controller.signal, redirect: 'manual' });
  28. const location = r.headers.get('location');
  29. if ([301, 302, 303, 307, 308].includes(r.status) && location && limit !== 0) {
  30. await r.body?.cancel();
  31. if (hop >= limit) throw new Error('too many redirects');
  32. target = new URL(location, target).href;
  33. continue;
  34. }
  35. const text = await r.text();
  36. return { status: r.status, ok: r.ok, url: r.url, headers: Object.fromEntries(r.headers),
  37. body: text, tag: o.tag || '', text() { return text; }, json() { return JSON.parse(text); } };
  38. }
  39. } catch (e) {
  40. const error = `hl:fetch ${method} ${url}: ${reason(e, timedOut)}`;
  41. if (tolerant) return { ok: false, status: 0, error };
  42. throw new Error(error);
  43. } finally { clearTimeout(timer); }
  44. }
  45. export function fetch(url, options) { return exchange(url, options, false); }
  46. export function tryFetch(url, options) { return exchange(url, options, true); }

Branches

Latest commits

  • da60547fstatus: GC1 done, backup bugmre
  • e4171021log: GC1 result, backup hard-link bugmre
  • f1597214deploy.sh backup: tar --hard-dereference (hot tar stored mpackdb sidecars as hard links to .sessions files)mre
  • 6b58571elog: GC windowmre
  • 5c8baa1fdeploy lock: hold only around deploy.shmre
  • b8dac822log: hybriel#309mre
  • bf676d9elog: GC1 startedmre
  • c7828541mission GC1mre
  • ef949994conductor: neutral ticket rulemre
  • 89e91ef3log: RV39 livemre
  • ee2b677alog: RV39 deployingmre
  • c1c4178cMerge branch 'rv39'mre
  • a016e9abReapply "Merge branch 'rv39'"mre
  • f29e2502log: RV39 gate fixmre
  • d9fce111browser gate #26: exact frame count incl. SL1 people step (hybriel#308)mre
  • ad529ac1log: m572 verdictmre
  • 1b37d404RV39 on hold: hybriel#308mre
  • f9127c68Revert "Merge branch 'rv39'"mre
  • cd39f3d3log: hybriel#308mre
  • 51341d6elog: RV39 deploy startedmre