gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb78fb85ab78fb85a|||PR README matches the codemreb78fb85a/plugins/fetch/README.md

4.7 KB

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

Branches

Latest commits

  • b78fb85a|||PR README matches the codemre
  • 9f351699Preserve comments and nest supported selectors without changing rendered outputmre
  • 97110ad9WIP: nest accepted layout selectors; stopped at startup parse failuremre
  • ce4a0baeKeep caller slot styles whole so minimal shell apps startmre
  • 12cd3992Re-vendor complete Hybriel plugins and runtime to day44mre
  • b05c20feBackup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 8d9ed0b8Re-vendor complete Hybriel plugins and runtime to day39mre
  • ebd00b6cNest caller slot styles in three-panemre
  • 009991efNest caller slot styles in list-firstmre
  • 494d75d4Nest caller slot styles in app-sidebarmre
  • e5571680Align all plugin sources with Hybriel day38bmre
  • 5d204c46Re-vendor layouts to Hybriel set day38bmre
  • 17e0f03eNest html-prefixed theme component overridesmre
  • e54e735eFormat plain theme elements with nested rules and bare mediamre
  • 547f834eRecord LY7 deployment and live verificationmre
  • fa4600b1Link caller-slot style exceptions to hybriel#270mre
  • 413b11e1Record corrected LY7 nesting, exact CSS and Chrome proofmre
  • b810453etracker-demo: nest child and compound selectors in Stylemre
  • 0b5b1d61notes-demo: nest child and compound selectors in Stylemre
  • ab22bdc2mail-demo: nest child and compound selectors in Stylemre