gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchw52b-approval-followupc01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mrew52b-approval-followup/plugins/fetch/README.md

5.3 KB

  1. # hl:fetch
  2. `fetch(url, options)` makes an HTTP or HTTPS request and waits for the answer.
  3. ## Request body
  4. `body` accepts a String (UTF-8) or Bytes (raw octets, including zero and invalid
  5. UTF-8). Empty bodies are sent with Content-Length 0; null or an omitted body
  6. means no body. Native fetch computes Content-Length from the transmitted bytes.
  7. Other body types raise an error. Use `json = value` to serialize JSON; when
  8. present, `json` takes precedence over `body` and supplies the default
  9. application/json Content-Type. JS targets unwrap runtime Bytes and also accept Uint8Array; the host
  10. Fetch API controls Content-Length and rejects bodies for GET/HEAD.
  11. ## Waiting and the event loop
  12. `fetch()` suspends only the delivery fiber that called it; other requests and
  13. concurrent arms can run. At the process root there is no delivery fiber, so the
  14. wait can block. `fetchStart()` returns immediately. With `completions()` and
  15. `on fetched(ev)`, the caller never waits for the response. Register the source
  16. before starting requests, and keep the pending ticket reachable until completion
  17. (closing a ticket joins its worker).
  18. ```hybriel
  19. // /slow queues a fetch; /fast answers while it runs.
  20. import { NativeHttpServer, Response } from 'hl:http1'
  21. import { fetchStart, completions } from 'hl:fetch'
  22. inherit NativeHttpServer
  23. port = 27570
  24. pending = null
  25. on request(req) { if (req.path == '/slow') { pending = fetchStart('http://127.0.0.1:27571/slow', { tag = 'job' }) } return new Response('accepted') }
  26. on fetched(ev) { console.log(ev.tag + ': ' + ev.status) }
  27. listen()
  28. __native('eventloop.register', completions(), 'fetched')
  29. ```
  30. Native fetch starts resolved addresses in order, 300 ms apart while a connection
  31. is pending; refused or failed connections move on immediately. Earlier attempts
  32. stay pending while later addresses are tried. The first connection to complete
  33. wins and the other sockets close. All attempts share the remaining whole-exchange
  34. `timeoutMs` budget, including time spent in DNS and earlier redirect hops. DNS itself uses a blocking resolver and cannot be
  35. interrupted at the deadline; after it returns, an expired budget prevents connect.
  36. ## When the connection fails
  37. A refused connection, an unknown host, a connect timeout, a reset by the peer and a TLS
  38. verification failure are all raised as an ordinary located error. The message names the method,
  39. the URL and the reason, for example
  40. `hl:fetch POST http://127.0.0.1:9/x: connection refused`,
  41. `hl:fetch GET http://nonexistent.invalid/x: could not resolve host` or `hl:fetch GET http://10.255.255.1/x: timed out`.
  42. The error can be caught with `on Error(e)`, in a plain program and inside a request handler alike.
  43. When the handler has absorbed the error, `fetch` gives back `null`, so the code can answer the failure itself:
  44. ```hybriel
  45. import { NativeHttpServer, Response } from 'hl:http1'
  46. import { fetch } from 'hl:fetch'
  47. inherit NativeHttpServer
  48. port = 8080
  49. on Error(e) { console.log('fetch failed: ' + e.message) }
  50. on request(req) {
  51. let res = fetch('http://127.0.0.1:9/x', { method = 'POST' json = { a = 1 } })
  52. if (res == null) { return new Response('upstream is down', { status = 502 }) }
  53. return new Response(res.text())
  54. }
  55. listen()
  56. ```
  57. Without an `on Error` handler the error is not caught. That is the language rule: the request then ends
  58. with the framework's `500` answer carrying the located message, and the server keeps serving.
  59. ## A failure as a value
  60. Import `tryFetch` from `hl:fetch` to handle a failed exchange without `on Error`.
  61. It accepts fetch's options and waits for completion. Every HTTP reply, including
  62. 404 or 500, has the same response and text()/json() readers as fetch. Connection,
  63. DNS, TLS and timeout failures return exactly `{ ok = false, status = 0, error }`.
  64. The error is fetch's message without its call site, for example
  65. `hl:fetch GET http://127.0.0.1:9/x: connection refused`. Native fetch is unchanged.
  66. The JS target and browser use the host Fetch API with the same response shape
  67. and whole-exchange `timeoutMs`. Browsers enforce CORS and expose only a generic
  68. network reason for many failures. `caFile` is supported only by the native engine.
  69. `tryFetch` waits even with `stream = true`; use fetchStart for native chunk events.
  70. ## Redirects
  71. Native and server JS follow 301, 302, 303, 307 and 308 with a Location, up to
  72. `maxRedirects` (default 5). Zero returns the redirect response. Every origin change
  73. (scheme, case-insensitive host, or effective port), including HTTPS to HTTP, removes
  74. Authorization, Proxy-Authorization and Cookie. Same-origin hops keep them; once
  75. removed they stay removed throughout the chain. There is no sensitive-header option.
  76. 303 changes the method to GET; 301/302 do so for methods other than GET/HEAD.
  77. Changing to GET removes the body, Content-Type and Content-Length. 307/308 retain
  78. method and body.
  79. The browser uses Fetch's `follow` mode and returns the final `response.url`.
  80. Its host applies redirect method and credential rules, CORS, mixed-content checks
  81. and cookie policy; caller-supplied Cookie and Proxy-Authorization are forbidden.
  82. Positive custom `maxRedirects` values cannot be enforced: the browser's own hop
  83. limit applies (normally 20). Zero selects `manual`: a redirect is an opaque answer
  84. with status 0, no readable headers or body, and the URL exposed by the browser.
  85. Browser Fetch preserves HEAD on 303 and rewrites only POST on 301/302; these host
  86. rules cannot be overridden while following redirects safely in the browser.

Branches

Latest commits

  • c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
  • 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
  • 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
  • 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
  • 155861f6Merge branch 'w52-person-approval' into mainGitoria
  • b5435de8|||PR person approvals: second Kunibert round (#52)mre
  • 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
  • 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
  • 83f0c953Report person approval verificationmre
  • 3ae94b86Record browser failures and verify owned tab cleanupmre
  • 54eec687Gate person approvals and merge alternativesmre
  • 12b08829Add person approvals for pull request headsmre
  • 55c84779Merge branch 'gp-gate-ports' into mainGitoria
  • 57af42bbREADME: point to deploy.sh for the port listmre
  • d63375d2README: gate commands with explicit portsmre
  • feeaa701|||PR gates: explicit ports only, deploy.sh passes themmre
  • 6cc1f943Merge branch 'records-1112' into mainGitoria
  • 9448c438|||PR records: LOG, missions and reports since 11-09mre
  • 6dc08a8eMerge branch 'p51-public-pr-reads' into mainGitoria
  • 7b88cfe0Type publicReaderOfRequest parameters (L10)Gitoria