gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/plugins/fetch/server.js

2.9 KB

  1. // JS target; redirects are followed explicitly. 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').toUpperCase();
  14. let requestMethod = method;
  15. const controller = new AbortController();
  16. let timedOut = false;
  17. const timer = setTimeout(() => { timedOut = true; controller.abort(); }, o.timeoutMs ?? 30000);
  18. try {
  19. const headers = new Headers(o.headers || {});
  20. let body = o.body;
  21. if (o.json != null) {
  22. body = JSON.stringify(o.json);
  23. if (!headers.has('content-type')) headers.set('content-type', 'application/json');
  24. }
  25. // Generated modules use the runtime's Bytes wrapper; direct JS uses Uint8Array.
  26. if (body?._d instanceof Uint8Array) {
  27. const { hlIsBytes } = await import('../hl-runtime.js');
  28. if (hlIsBytes(body)) body = body._d;
  29. }
  30. if (body != null && typeof body !== 'string' && !(body instanceof Uint8Array)) {
  31. throw new Error('body must be a String or Bytes (use json for JSON)');
  32. }
  33. let target = url;
  34. const limit = o.maxRedirects ?? 5;
  35. for (let hop = 0; ; hop++) {
  36. const r = await globalThis.fetch(target, { method: requestMethod, headers, body, signal: controller.signal, redirect: 'manual' });
  37. const location = r.headers.get('location');
  38. if ([301, 302, 303, 307, 308].includes(r.status) && location && limit !== 0) {
  39. await r.body?.cancel();
  40. if (hop >= limit) throw new Error('too many redirects');
  41. const next = new URL(location, target);
  42. if (next.origin !== new URL(target).origin) {
  43. for (const name of ['authorization', 'proxy-authorization', 'cookie']) headers.delete(name);
  44. }
  45. if (r.status === 303 || ([301, 302].includes(r.status) && !['GET', 'HEAD'].includes(requestMethod))) {
  46. requestMethod = 'GET';
  47. body = undefined;
  48. headers.delete('content-type');
  49. headers.delete('content-length');
  50. }
  51. target = next.href;
  52. continue;
  53. }
  54. const text = await r.text();
  55. return { status: r.status, ok: r.ok, url: r.url, headers: Object.fromEntries(r.headers),
  56. body: text, tag: o.tag || '', text() { return text; }, json() { return JSON.parse(text); } };
  57. }
  58. } catch (e) {
  59. const error = `hl:fetch ${method} ${url}: ${reason(e, timedOut)}`;
  60. if (tolerant) return { ok: false, status: 0, error };
  61. throw new Error(error);
  62. } finally { clearTimeout(timer); }
  63. }
  64. export function fetch(url, options) { return exchange(url, options, false); }
  65. export function tryFetch(url, options) { return exchange(url, options, true); }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre