SCUA 0.41.0 lets an http.serve handler send its response
in pieces, so a SCUA server can push server-sent events, serve a
download of any size, or relay text from a model as it arrives. Each
release now also ships SCUA as a single WebAssembly file that runs under
wasmtime or Node on any operating system, and the macOS and Linux
packages include an embedding library with FFI built in.
#http.serve handlers can now stream a response
A handler can now start the response itself and write the body a piece at a time, so the body never has to be in memory whole and the client sees each piece as soon as it is written:
import http
fn handle(req)
if req.path != "/events" then
return { status = 404, body = b"not found" }
end
let s = http.start(req, {
status = 200,
headers = { "content-type" = "text/event-stream", "cache-control" = "no-store" },
idle_ms = 120000,
})?
let n = 0
while true do
select wait(1s) -> nil end
n = n + 1
match s.write(`event: tick\ndata: {n}\n\n`)
Ok(_) -> nil
Error(_) -> return nil -- the client went away
end
end
end
http.serve(handle, { bind = "127.0.0.1:8080" })?
Run it with
scua --allow-serve=127.0.0.1:8080 events.scua and
curl -N http://127.0.0.1:8080/events prints a tick every
second.
http.start sends the status and headers,
s.write sends part of the body, and s.finish()
ends it. The server picks the framing: chunked by default, a
content-length when you give start one (it
then checks the body matches), or a body ended by closing the connection
for an HTTP/1.0 client.
A write the client isn't ready for waits for that client without
holding a worker thread, so one slow reader doesn't slow the others. A
stream that sends nothing for idle_ms is ended. If a
handler faults, or returns, before finish, the connection
is closed, so a client never mistakes a cut-short body for a complete
one. --serve-max-streams caps how many streams are open at
once, and http.stats() counts them.
The handler above mixes its returns: a record on one path,
nil once it has streamed, and an Error passed
on by ?. A function passed to http.serve may
do that with no annotation.
scua serve uses streaming too. It now sends a file
larger than 1 MiB in pieces with its length, so a large download never
sits in memory whole.
The manual's Stream a
response covers relaying an upstream body, sending a file with its
length, and what happens after the head is sent.
examples/serve_stream.scua runs both kinds.
#Handles now have methods
A handle's methods are now its module's functions, with the handle
first. f.read_chunk(n) on an open file is
fs.read_chunk(f, n), c.write(data) on a
connection is net.write(c, data), and
s.write(data) on a response stream is
http.write(s, data). A method can only reach functions the
module has in that run, so it grants nothing new.
http.serve's context can now carry a
module, such as context = { http = http }. A package that
runs your handler can then use http.start without importing
http itself. Each request uses its own partition's module
rather than a copy, so carrying it allocates nothing per request.
#SCUA now ships as one WebAssembly file
Each release now includes scua-0.41.0-wasm32-wasi.wasm
next to the archives, covered by SHA256SUMS. It is the
scua command built for WebAssembly, and it runs wherever a
WASI host does:
wasmtime run --dir=.::. scua-0.41.0-wasm32-wasi.wasm hello.scua
The language, the options and the capability flags are the same as in
the native build. The module can reach only the folders the host gives
it. It suits scripts, tools and sandboxes on machines with no native
build. It has no JIT, no network and no exec, and its
actors take turns on one thread. Run
SCUA under WebAssembly has the full list and shows how to run it
from Node.
#Embedded scripts can now call native libraries
The macOS and Linux packages now include
lib/libscua-ffi.a, the embedding library with FFI built in.
Link it in place of lib/libscua.a, add -lffi,
and the scripts your host runs can call the native libraries it grants
them, with no build from source. See Call native
libraries.
#What changes when you upgrade
as(value, T), renamed parse(value, T) in
0.37.0, no longer compiles. The error shows the parse call
to write, and scua fmt --fix file.scua renames every one in
a file.
Next, SCUA spreads more of its work over your cores. 0.42.0 starts that: when a handler hands work to several partitions, idle cores now join in, and large arrays of numbers move between partitions without extra copies.
The changelog lists everything else in 0.41.0.