| @@ -0,0 +1,256 @@ |
| 1 | +/// Parts of a page rendered by actors of their own, streamed in as they |
| 2 | +/// finish (the markup is `swap`'s). `start` spawns a part's actor at |
| 3 | +/// once and answers its placeholder; `respond` turns a page that started |
| 4 | +/// parts into a streamed response: the page up to its body's end, each |
| 5 | +/// part as it finishes (or `fallback`, if it fails or misses the |
| 6 | +/// deadline), then the end of the document. |
| 7 | +/// |
| 8 | +/// A part's actor gets none of the handlers of the request that started |
| 9 | +/// it (ADR 0004): `render` performs only what an actor can, so whoever |
| 10 | +/// needs the application's effects installs them inside it. |
| 11 | + |
| 12 | +import security |
| 13 | +import std/base64 |
| 14 | +import std/bytes |
| 15 | +import std/html.{type Html} |
| 16 | +import std/http.{type Response, type Writer, Response, Streamed, Whole} |
| 17 | +import std/list |
| 18 | +import std/process. |
| 19 | +{ type Down |
| 20 | +, type ExitReason |
| 21 | +, type Pid |
| 22 | +, type Selector |
| 23 | +, type Subject |
| 24 | +, type proc |
| 25 | +, Normal |
| 26 | +} |
| 27 | +import std/random.{type random} |
| 28 | +import std/string |
| 29 | +import std/time.{type clock} |
| 30 | +import swap |
| 31 | + |
| 32 | +/// How long a page waits for its parts, and what a part that fails or |
| 33 | +/// misses the deadline shows. |
| 34 | +pub type Settings { Settings(timeout_ms: Int, fallback: Html) } |
| 35 | + |
| 36 | +/// The parts one request started. |
| 37 | +pub opaque type Parts |
| 38 | +{ Parts(results: Subject<(String, String)>, started: Subject<Started>) } |
| 39 | + |
| 40 | +type Started { Started(id: String, pid: Pid, down: Subject<Down>) } |
| 41 | + |
| 42 | +type Event |
| 43 | +{ Rendered(id: String, markup: String), Ended(id: String, reason: ExitReason) } |
| 44 | + |
| 45 | +/// No parts yet. The subjects belong to the calling actor: `respond` |
| 46 | +/// must run in it, and so must the response's stream (the connection's |
| 47 | +/// actor does both). |
| 48 | +pub fn new :: -> Parts / <proc> |
| 49 | +{ |
| 50 | + Parts(process.subject (), process.subject ()) |
| 51 | +} |
| 52 | + |
| 53 | +/// Start rendering a part in an actor of its own, and answer `skeleton` |
| 54 | +/// as its placeholder. |
| 55 | +pub fn start :: |
| 56 | + held: Parts |
| 57 | + skeleton: Html |
| 58 | + render: send fn() -> Html / <proc, clock, random, io, ffi> |
| 59 | + -> Html / <proc, random> |
| 60 | +{ |
| 61 | + let id = "swap-{random.int 9007199254740991}" |
| 62 | + let results = held.results |
| 63 | + let (pid, down) = |
| 64 | + process.spawn_monitor |
| 65 | + fn { process.send results (id, html.render (render ())) } |
| 66 | + process.send held.started Started(id, pid, down) |
| 67 | + swap.placeholder id skeleton |
| 68 | +} |
| 69 | + |
| 70 | +/// `resp`, streamed if it is a page and parts were started for it. |
| 71 | +/// Otherwise the parts are stopped and `resp` is answered as it is. |
| 72 | +pub fn respond :: |
| 73 | + held: Parts |
| 74 | + settings: Settings |
| 75 | + resp: Response |
| 76 | + -> Response / <proc, random> |
| 77 | +{ |
| 78 | + match all_started held.started [] |
| 79 | + { [] -> resp |
| 80 | + , started -> match page_of resp |
| 81 | + { Some(page) -> |
| 82 | + { |
| 83 | + let nonce = base64.encode (random.bytes 18) |
| 84 | + match swap.opened page nonce |
| 85 | + { Some((first, last)) -> |
| 86 | + Response( ..security.with_script_nonce resp nonce |
| 87 | + , body: Streamed(fn w { stream w held settings started first last }) |
| 88 | + ) |
| 89 | + , None -> stopped held started resp |
| 90 | + } |
| 91 | + } |
| 92 | + , None -> stopped held started resp |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn all_started :: |
| 98 | + started: Subject<Started> |
| 99 | + held: List<Started> |
| 100 | + -> List<Started> / <proc> |
| 101 | +{ |
| 102 | + match process.receive started 0 |
| 103 | + { Some(s) -> all_started started [s, ..held], None -> list.reverse held } |
| 104 | +} |
| 105 | + |
| 106 | +fn page_of :: resp: Response -> Option<String> |
| 107 | +{ |
| 108 | + let html_type = |
| 109 | + list.any |
| 110 | + resp.headers |
| 111 | + fn h |
| 112 | + { |
| 113 | + string.to_lower h.0 == "content-type" |
| 114 | + && string.starts_with h.1 "text/html" |
| 115 | + } |
| 116 | + match resp.body |
| 117 | + { Whole(body) if resp.status == 200 && html_type -> |
| 118 | + Some(bytes.to_string_lossy body) |
| 119 | + , _ -> None |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +fn stopped :: |
| 124 | + held: Parts |
| 125 | + started: List<Started> |
| 126 | + resp: Response |
| 127 | + -> Response / <proc> |
| 128 | +{ |
| 129 | + stop held started |
| 130 | + resp |
| 131 | +} |
| 132 | + |
| 133 | +fn stop :: held: Parts started: List<Started> -> () / <proc> |
| 134 | +{ |
| 135 | + list.each started fn s { process.kill s.pid } |
| 136 | + list.each started fn s { let _ = process.receive s.down 1000 } |
| 137 | + drain held.results |
| 138 | +} |
| 139 | + |
| 140 | +fn drain :: results: Subject<(String, String)> -> () / <proc> |
| 141 | +{ |
| 142 | + match process.receive results 0 { Some(_) -> drain results, None -> () } |
| 143 | +} |
| 144 | + |
| 145 | +fn stream :: |
| 146 | + w: Writer |
| 147 | + held: Parts |
| 148 | + settings: Settings |
| 149 | + started: List<Started> |
| 150 | + first: String |
| 151 | + last: String |
| 152 | + -> () / <io, proc, clock> |
| 153 | +{ |
| 154 | + match http.write_string w first |
| 155 | + { Ok(()) -> |
| 156 | + { |
| 157 | + let deadline = now_ms () + settings.timeout_ms |
| 158 | + if collect w held settings deadline started [] |
| 159 | + { |
| 160 | + let _ = http.write_string w last |
| 161 | + () |
| 162 | + } |
| 163 | + } |
| 164 | + , Error(_) -> stop held started |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +/// Write each part as its actor ends; `False` when the client is gone. |
| 169 | +fn collect :: |
| 170 | + w: Writer |
| 171 | + held: Parts |
| 172 | + settings: Settings |
| 173 | + deadline: Int |
| 174 | + pending: List<Started> |
| 175 | + rendered: List<(String, String)> |
| 176 | + -> Bool / <io, proc, clock> |
| 177 | +{ |
| 178 | + let left = deadline - now_ms () |
| 179 | + match pending |
| 180 | + { [] -> True |
| 181 | + , _ if left <= 0 -> late w held settings pending |
| 182 | + , _ -> match process.select (events held.results pending) left |
| 183 | + { None -> late w held settings pending |
| 184 | + , Some(Rendered(id, markup)) -> |
| 185 | + collect w held settings deadline pending [(id, markup), ..rendered] |
| 186 | + , Some(Ended(id, reason)) -> |
| 187 | + { |
| 188 | + let markup = match (reason, find rendered id) |
| 189 | + { (Normal, Some(found)) -> found, _ -> html.render settings.fallback } |
| 190 | + let rest = list.filter pending fn s { s.id != id } |
| 191 | + match http.write_string w (swap.part id markup) |
| 192 | + { Ok(()) -> collect w held settings deadline rest rendered |
| 193 | + , Error(_) -> |
| 194 | + { |
| 195 | + stop held rest |
| 196 | + False |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +fn events :: |
| 205 | + results: Subject<(String, String)> |
| 206 | + pending: List<Started> |
| 207 | + -> Selector<Event> |
| 208 | +{ |
| 209 | + list.fold |
| 210 | + pending |
| 211 | + ( process.new_selector () |
| 212 | + |> process.selecting _ results fn r { Rendered(r.0, r.1) } |
| 213 | + ) |
| 214 | + fn selector s |
| 215 | + { |
| 216 | + process.selecting selector s.down fn d { Ended(s.id, d.reason) } |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +/// The deadline passed: the parts still running show the fallback. |
| 221 | +fn late :: |
| 222 | + w: Writer |
| 223 | + held: Parts |
| 224 | + settings: Settings |
| 225 | + pending: List<Started> |
| 226 | + -> Bool / <io, proc> |
| 227 | +{ |
| 228 | + stop held pending |
| 229 | + list.all |
| 230 | + pending |
| 231 | + fn s |
| 232 | + { |
| 233 | + match http.write_string w (swap.part s.id (html.render settings.fallback)) |
| 234 | + { Ok(()) -> True, Error(_) -> False } |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +fn find :: rendered: List<(String, String)> id: String -> Option<String> |
| 239 | +{ |
| 240 | + match rendered |
| 241 | + { [] -> None |
| 242 | + , [(found, markup), ..rest] -> if found == id |
| 243 | + { |
| 244 | + Some(markup) |
| 245 | + } |
| 246 | + else |
| 247 | + { |
| 248 | + find rest id |
| 249 | + } |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +fn now_ms :: -> Int / <clock> |
| 254 | +{ |
| 255 | + time.in_milliseconds (time.monotonic ()) |
| 256 | +} |