gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf52862a2f52862a2log: HM62b startedmref52862a2/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

  • f52862a2log: HM62b startedmre
  • 88fde2c4mission HM62bmre
  • a722e526log: #62 blocked by hybriel#317mre
  • 074a7418log: #62 assigned to Tracker; pending rulemre
  • 06f6ae8fHM62 report: mpackdb index gapmre
  • 86fe5430log: HM62 startedmre
  • 755307bbmission HM62mre
  • 4b2f2995HM61 report: tracker#61 packed people ordermre
  • 48faa84flog: #60 deploy startedmre
  • 90734c6cMerge branch 'hm60-shortids'mre
  • e0db0d1bHM60 report: tracker#60 packed short-ID ownersmre
  • 3c79b576Pack short-ID ownership into byte buffersmre
  • e822524bstatus wordingmre
  • 1c7f0480log: HM60/HM61 startedmre
  • 2841d215missions HM60 HM61mre
  • 289c6ce4HM1 report: heap object breakdownmre
  • 2e7d6257log: HM1 startedmre
  • 2f790465mission HM1: heap object breakdownmre
  • a18957falog: evening re-measure after CBmre
  • aac9a98dlog: evening re-measure startmre