kunibert
All repositories: gitoria
4.7 KB
# hl:fetch`fetch(url, options)` makes an HTTP or HTTPS request and waits for the answer.## Waiting and the event loop`fetch()` suspends only the delivery fiber that called it; other requests andconcurrent arms can run. At the process root there is no delivery fiber, so thewait can block. `fetchStart()` returns immediately. With `completions()` and`on fetched(ev)`, the caller never waits for the response. Register the sourcebefore starting requests, and keep the pending ticket reachable until completion(closing a ticket joins its worker).```hybriel// /slow queues a fetch; /fast answers while it runs.import { NativeHttpServer, Response } from 'hl:http1'import { fetchStart, completions } from 'hl:fetch'inherit NativeHttpServerport = 27570pending = nullon request(req) { if (req.path == '/slow') { pending = fetchStart('http://127.0.0.1:27571/slow', { tag = 'job' }) } return new Response('accepted') }on fetched(ev) { console.log(ev.tag + ': ' + ev.status) }listen()__native('eventloop.register', completions(), 'fetched')```Native fetch tries resolved addresses in order. While another address remains,a connection attempt waits at most 300 ms before closing its socket and tryingthe next; refused or failed connections move on immediately. The last addressgets the remaining whole-exchange `timeoutMs` budget, including time spent in DNSand earlier redirect hops. DNS itself uses a blocking resolver and cannot beinterrupted at the deadline; after it returns, an expired budget prevents connect.## When the connection failsA refused connection, an unknown host, a connect timeout, a reset by the peer and a TLSverification failure are all raised as an ordinary located error. The message names the method,the URL and the reason, for example`hl:fetch POST http://127.0.0.1:9/x: connection refused`,`hl:fetch GET http://nonexistent.invalid/x: could not resolve host` or `hl:fetch GET http://10.255.255.1/x: timed out`.The error can be caught with `on Error(e)`, in a plain program and inside a request handler alike.When the handler has absorbed the error, `fetch` gives back `null`, so the code can answer the failure itself:```hybrielimport { NativeHttpServer, Response } from 'hl:http1'import { fetch } from 'hl:fetch'inherit NativeHttpServerport = 8080on Error(e) { console.log('fetch failed: ' + e.message) }on request(req) {let res = fetch('http://127.0.0.1:9/x', { method = 'POST' json = { a = 1 } })if (res == null) { return new Response('upstream is down', { status = 502 }) }return new Response(res.text())}listen()```Without an `on Error` handler the error is not caught. That is the language rule: the request then endswith the framework's `500` answer carrying the located message, and the server keeps serving.## A failure as a valueImport `tryFetch` from `hl:fetch` to handle a failed exchange without `on Error`.It accepts fetch's options and waits for completion. Every HTTP reply, including404 or 500, has the same response and text()/json() readers as fetch. Connection,DNS, TLS and timeout failures return exactly `{ ok = false, status = 0, error }`.The error is fetch's message without its call site, for example`hl:fetch GET http://127.0.0.1:9/x: connection refused`. Native fetch is unchanged.The JS target and browser use the host Fetch API with the same response shapeand whole-exchange `timeoutMs`. Browsers enforce CORS and expose only a genericnetwork reason for many failures. `caFile` is supported only by the native engine.`tryFetch` waits even with `stream = true`; use fetchStart for native chunk events.## RedirectsNative and server JS follow 301, 302, 303, 307 and 308 with a Location, up to`maxRedirects` (default 5). Zero returns the redirect response. Every origin change(scheme, case-insensitive host, or effective port), including HTTPS to HTTP, removesAuthorization, Proxy-Authorization and Cookie. Same-origin hops keep them; onceremoved they stay removed throughout the chain. There is no sensitive-header option.303 changes the method to GET; 301/302 do so for methods other than GET/HEAD.Changing to GET removes the body, Content-Type and Content-Length. 307/308 retainmethod and body.The browser uses Fetch's `follow` mode and returns the final `response.url`.Its host applies redirect method and credential rules, CORS, mixed-content checksand cookie policy; caller-supplied Cookie and Proxy-Authorization are forbidden.Positive custom `maxRedirects` values cannot be enforced: the browser's own hoplimit applies (normally 20). Zero selects `manual`: a redirect is an opaque answerwith status 0, no readable headers or body, and the URL exposed by the browser.Browser Fetch preserves HEAD on 303 and rewrites only POST on 301/302; these hostrules cannot be overridden while following redirects safely in the browser.
Branches
Latest commits
- 6981d96fMerge branch 'fresh-on-nocmd' into mainKunibert
- 37c47840runner: one shared prompt for both fresh-session fallbacks (L16)mre
- f945d2e1|||PR runner: a resumed run with no command on a new head gets one fresh session instead of a tool errormre
- 10b2a49estatus: PR 314, guard fix proposedmre
- e73995a3log: hycon PR 314 approved after fresh reviewmre
- f60427falog: hycon PR 314 false tool errormre
- 481974f7log: Hermes accepts PR 259 findingmre
- bb0976acrecords: hycon PR 259 design findingmre
- 5c637c24records: hycon PR 259 false tool errormre
- a344c406log: routger 132 reviewedmre
- 56e70dcbrecords: review time limit 1800 s (routger 132)mre
- c3a304c5Merge branch 'job-timeout' into mainKunibert
- 402bfeb0|||PR runner: the model run uses the job's time limit (jobTimeoutSeconds), not a fixed 900 smre
- 8d34ca33records: repo rename (gitoria#53)mre
- 1e05cb3fMerge branch 'repo-rename' into mainKunibert
- 6ee0d2e9|||PR intake: match a delivery by repo.id and signature, not URLs (gitoria#53 rename)mre
- 900ffcd4status: routgermre
- 59b0b793log: routger merge waitmre
- e52f094alog: routger intake check (hycon#122)mre
- f979027frecords: design rule livemre