Module
whiplash/body
src/whiplash/body.pookaBodies that are not all there at once: streamed responses, server-sent events, files, and request bodies read as they arrive.
A Stream is a status, fields and a producer. The producer runs in its
own process and writes through an Output, which waits whenever the
client is slower than the producer, so a fast source never piles up in
memory. Chunks larger than 64 KiB are split on the way. The Output
belongs to that process; do not hand it to another one.
body.Stream(200, [#("content-type", "text/plain")], fn(output) {
let _ = output.send(<<"tick\n":utf8>>)?
output.send(<<"tock\n":utf8>>)
})
API reference
class Output(post: fn(BitArray) -> Result(Nil, String))
Where a producer writes. Each call returns an error once the client has gone; a producer should stop then.
post: fn(BitArray) -> Result(Nil, String)
fn(BitArray) -> Result(Nil, String)
fn(fn(BitArray) -> Result(Nil, String)) -> whiplash/body.Outputevent(value: Event) -> Result(Nil, String)
fn(whiplash/body.Event) -> Result(Nil, String)Send one server-sent event.
heartbeat(text: String) -> Result(Nil, String)
fn(String) -> Result(Nil, String)Send an event-stream comment, which keeps a quiet connection open through proxies without the client seeing an event.
send(bytes: BitArray) -> Result(Nil, String)
fn(BitArray) -> Result(Nil, String)Send bytes, waiting while the client catches up.
class Stream(
status: Int,
headers: List(#(String, String)),
run: fn(Output) -> Result(Nil, String)
)
A response produced a piece at a time: its status, its fields, and the producer that writes the body. Whiplash adds the framing; a producer that returns an error ends the response early.
status: Int
Intheaders: List(#(String, String))
List(#(String, String))run: fn(Output) -> Result(Nil, String)
fn(whiplash/body.Output) -> Result(Nil, String)
fn(Int, List(#(String, String)), fn(whiplash/body.Output) -> Result(Nil, String)) -> whiplash/body.Streamfile() -> Option(#(String, Int))
fn() -> Option(#(String, Int))A regular file's path and size when this stream sends one unchanged, so the connection may read it whole or hand it to the kernel (sendfile).
chunks(status: Int, headers: List(#(String, String)), chunks: List(BitArray)) -> Stream
fn(Int, List(#(String, String)), List(BitArray)) -> whiplash/body.StreamA finite stream is useful for generated data without concatenating it all.
class File
An open file handle, used by file streams.
read_file(path: String) -> Result(BitArray, String)
fn(String) -> Result(BitArray, String)A whole small file, as one binary. The connection checks its length against the size announced when the stream was made.
file(path: String, content_type: String) -> Result(Stream, String)
fn(String, String) -> Result(whiplash/body.Stream, String)A regular file, read in bounded chunks by the supervised producer. A file changed after inspection must still satisfy the declared response length; the connection closes/resets on a mismatch rather than corrupting a pipeline.
class Event(
data: String,
id: Option(String),
event: Option(String),
retry: Option(Int)
)
One server-sent event. The wire fields remain explicit; data may contain lines, while id and event name may not inject a new field or event.
data: String
Stringid: Option(String)
Option(String)event: Option(String)
Option(String)retry: Option(Int)
Option(Int)
fn(String, Option(String), Option(String), Option(Int)) -> whiplash/body.Eventevent(data: String) -> Event
fn(String) -> whiplash/body.EventAn event carrying only data. Build an Event for an id, a name or a
retry delay.
let plain = body.event("ping")
let named = body.Event("ping", Some("42"), Some("heartbeat"), None)
encode_event(event: Event) -> Result(BitArray, String)
fn(whiplash/body.Event) -> Result(BitArray, String)Encode the WHATWG event-stream format, normalising CRLF and CR in data.
let assert Ok(bytes) = body.encode_event(body.event("hello"))
assert bytes == <<"data: hello\n\n":utf8>>
comment(text: String) -> BitArray
fn(String) -> BitArrayA comment keeps an idle event stream alive without dispatching an event.
event_headers() -> List(#(String, String))
fn() -> List(#(String, String))Ordinary HTTP streaming carries SSE over HTTP/1 or HTTP/2. For a live source, construct Stream with these fields and call output.send/event.
events(events: List(Event)) -> Stream
fn(List(whiplash/body.Event)) -> whiplash/body.StreamA finished list of events as an event stream, for when they are all known
up front. For a live source, build a Stream with event_headers() and
write with output.event.
class Reader(next: fn(Int) -> Result(Option(BitArray), String))
A request body read as it arrives, for routes added with
Application.with_uploads. read waits up to timeout milliseconds for
the next piece and returns None once the body has ended.
next: fn(Int) -> Result(Option(BitArray), String)
fn(Int) -> Result(Option(BitArray), String)
fn(fn(Int) -> Result(Option(BitArray), String)) -> whiplash/body.Readerread(timeout: Int) -> Result(Option(BitArray), String)
fn(Int) -> Result(Option(BitArray), String)all(limit: Int, timeout: Int) -> Result(BitArray, String)
fn(Int, Int) -> Result(BitArray, String)The rest of the body as one binary, refused beyond limit bytes.