gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchv61-day6183faeb15|||PR ident: vendor Hybriel day61mrev61-day61/plugins/fetch/pending_fetch.hl

4.3 KB

  1. \* A request in flight. Handed back by fetch()/fetchStart() — never `new`ed.
  2. The request is already on the wire when you hold one: it has a thread of
  3. its own, holding one costs a socket, not a fiber. With stream = true the
  4. events below arrive AT this object. *\
  5. \* A decoded piece of the body, token by token (stream mode). *\
  6. emit chunk(
  7. String chunk \\ the decoded bytes as they arrived
  8. )
  9. \* The stream's final frame — body is EMPTY, the chunks were the body. *\
  10. emit done(
  11. Number status \\ the HTTP status
  12. Boolean ok \\ true for 2xx
  13. Hybrid headers \\ response headers
  14. String error \\ '' or the failure text
  15. Boolean aborted \\ true when abort() cut it
  16. )
  17. Hybrid #handle
  18. Number #srcId = 0
  19. // Has the answer arrived? Never waits — a poll for code that wants to do
  20. // something else in the meantime.
  21. done() {
  22. return #handle.done()
  23. }
  24. // THE WAIT. Blocks the calling fiber until the answer is in, then hands back a
  25. // FetchResponse. A failed fetch (refused connection, timeout, TLS verification,
  26. // too many redirects) surfaces here as a located error routed to `on Error`, so
  27. // a caller that wants to survive one writes an `on Error(e)` handler.
  28. response() {
  29. // Not in yet: let the sibling fibers (a `concurrent` group, the loop's other
  30. // handlers) run instead of blocking the thread on this socket. On the process
  31. // stack there is no one to yield to and result() is the wait.
  32. // The yield names the fetch's bell (an eventfd rung when it is done): a
  33. // `concurrent` group whose arms ALL wait sleeps on those, not on a timer.
  34. let bell = #handle.doneFd()
  35. while (!#handle.done()) {
  36. if (!__native("fiber.yield", bell)) { break }
  37. }
  38. let r = #handle.result()
  39. // An `on Error` handler that ABSORBED the failure leaves the call yielding
  40. // null (the plugin-error contract every `hl:` module follows). Hydrating null
  41. // would be a second, misleading error, so a caught failure answers null —
  42. // exactly what `hl:fs`'s file() does.
  43. if (r == null) {
  44. return null
  45. }
  46. return r > new FetchResponse()
  47. }
  48. // STREAM MODE (the LLM-token flow). `fetch(url, { stream = true })` calls this
  49. // before handing the instance out: the ticket's own event source is registered
  50. // AT this instance and the fan-out below re-emits every frame at itself, so app
  51. // code holds nothing but the object and its events — `on p.chunk`, `on p.done`.
  52. arm() {
  53. #srcId = __native("eventloop.register", #handle.events(), "__fetchEvent")
  54. }
  55. // THE FAN-OUT: a native frame in, a DECLARED event out. The frames are Hybrids
  56. // ({ tag, url, chunk } or { tag, url, status, ok, body, error, headers, aborted });
  57. // `chunk` and `done` above declare TYPED parameters, so each field is handed over
  58. // BY NAME — `emit chunk(ev)` would bind a Hybrid to `String chunk` and fail every
  59. // frame (mission 257: it did).
  60. //
  61. // NAMED, NOT POSITIONAL, and that distinction cost the creator a night (2026-08-30).
  62. // 257 fixed the Hybrid-into-String bind by emitting POSITIONALLY — `emit
  63. // chunk(ev.chunk)` — which type-checks and is still wrong: every handler in this
  64. // language receives a PAYLOAD OBJECT and destructures it. His own working code is
  65. // `on stream.chunk({ chunk })`, exactly as `on server.line({ line, stream })` reads
  66. // for hl:proc and as his `emit ready(port = …)` writes it. Positional emission
  67. // hands `{ chunk }` a String, and the app dies with "Cannot destructure non-object
  68. // value" — a DIFFERENT error from the one 257 fixed, on the same line of his app.
  69. // `name = value` is the form that satisfies the typed declaration AND keeps
  70. // destructuring working. Do not "simplify" it back.
  71. on __fetchEvent(ev) {
  72. if (ev.chunk != null) {
  73. emit chunk(chunk = ev.chunk)
  74. return null
  75. }
  76. // the final frame — retire the source so the loop can end by itself
  77. __native("eventloop.unregister", #srcId)
  78. emit done(status = ev.status, ok = ev.ok, headers = ev.headers, error = ev.error, aborted = ev.aborted)
  79. return null
  80. }
  81. \* THE STOP BUTTON: abort this fetch — the socket is shut down, the peer
  82. sees the disconnect and stops generating, and `done` arrives with
  83. aborted = true. (fetchAbort(tag) cuts a whole tag at once.) *\
  84. abort() {
  85. return #handle.abort()
  86. }
  87. // Same wait as response(), with the native failure kept as an ordinary Hybrid.
  88. tryResponse() {
  89. bell = #handle.doneFd()
  90. while (!#handle.done()) {
  91. if (!__native("fiber.yield", bell)) { break }
  92. }
  93. r = #handle.tryResult()
  94. if (r.status == 0) { return r }
  95. return r > new FetchResponse()
  96. }

Branches

Latest commits

  • 83faeb15|||PR ident: vendor Hybriel day61mre
  • b2572b34Merge branch 'v59-day59' into mainIdent
  • 67cf016d|||PR ident: vendor Hybriel day59mre
  • 600c043dMerge branch 'v58-day58' into mainIdent
  • 365f8c80|||PR ident: vendor Hybriel day58mre
  • 585126e5Merge branch 'f16-setf' into mainIdent
  • 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
  • a1419d9cMerge branch 'f15-navigate' into mainIdent
  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent