Module

whiplash/body

src/whiplash/body.pooka

Bodies 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.

Pooka
body.Stream(200, [#("content-type", "text/plain")], fn(output) {
  let _ = output.send(<<"tick\n":utf8>>)?
  output.send(<<"tock\n":utf8>>)
})

API reference

class

Output

src/whiplash/body.pooka:27
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.

field

post

src/whiplash/body.pooka:27
post: fn(BitArray) -> Result(Nil, String)
INFERREDfn(BitArray) -> Result(Nil, String)
constructor

new

src/whiplash/body.pooka:27
INFERREDfn(fn(BitArray) -> Result(Nil, String)) -> whiplash/body.Output
method

event

src/whiplash/body.pooka:29
event(value: Event) -> Result(Nil, String)
INFERREDfn(whiplash/body.Event) -> Result(Nil, String)

Send one server-sent event.

method

heartbeat

src/whiplash/body.pooka:36
heartbeat(text: String) -> Result(Nil, String)
INFERREDfn(String) -> Result(Nil, String)

Send an event-stream comment, which keeps a quiet connection open through proxies without the client seeing an event.

method

send

src/whiplash/body.pooka:41
send(bytes: BitArray) -> Result(Nil, String)
INFERREDfn(BitArray) -> Result(Nil, String)

Send bytes, waiting while the client catches up.

class

Stream

src/whiplash/body.pooka:52
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.

field

status

src/whiplash/body.pooka:53
status: Int
INFERREDInt
field

headers

src/whiplash/body.pooka:54
headers: List(#(String, String))
INFERREDList(#(String, String))
field

run

src/whiplash/body.pooka:55
run: fn(Output) -> Result(Nil, String)
INFERREDfn(whiplash/body.Output) -> Result(Nil, String)
constructor

new

src/whiplash/body.pooka:53
INFERREDfn(Int, List(#(String, String)), fn(whiplash/body.Output) -> Result(Nil, String)) -> whiplash/body.Stream
method

file

src/whiplash/body.pooka:59
file() -> Option(#(String, Int))
INFERREDfn() -> 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).

function

chunks

src/whiplash/body.pooka:86
chunks(status: Int, headers: List(#(String, String)), chunks: List(BitArray)) -> Stream
INFERREDfn(Int, List(#(String, String)), List(BitArray)) -> whiplash/body.Stream

A finite stream is useful for generated data without concatenating it all.

class

File

src/whiplash/body.pooka:115
class File

An open file handle, used by file streams.

function

read_file

src/whiplash/body.pooka:131
read_file(path: String) -> Result(BitArray, String)
INFERREDfn(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.

function

file

src/whiplash/body.pooka:138
file(path: String, content_type: String) -> Result(Stream, String)
INFERREDfn(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

src/whiplash/body.pooka:171
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.

field

data

src/whiplash/body.pooka:172
data: String
INFERREDString
field

id

src/whiplash/body.pooka:173
id: Option(String)
INFERREDOption(String)
field

event

src/whiplash/body.pooka:174
event: Option(String)
INFERREDOption(String)
field

retry

src/whiplash/body.pooka:175
retry: Option(Int)
INFERREDOption(Int)
constructor

new

src/whiplash/body.pooka:172
INFERREDfn(String, Option(String), Option(String), Option(Int)) -> whiplash/body.Event
function

event

src/whiplash/body.pooka:185
event(data: String) -> Event
INFERREDfn(String) -> whiplash/body.Event

An event carrying only data. Build an Event for an id, a name or a retry delay.

Pooka
let plain = body.event("ping")
let named = body.Event("ping", Some("42"), Some("heartbeat"), None)
function

encode_event

src/whiplash/body.pooka:195
encode_event(event: Event) -> Result(BitArray, String)
INFERREDfn(whiplash/body.Event) -> Result(BitArray, String)

Encode the WHATWG event-stream format, normalising CRLF and CR in data.

Pooka
let assert Ok(bytes) = body.encode_event(body.event("hello"))
assert bytes == <<"data: hello\n\n":utf8>>
function

comment

src/whiplash/body.pooka:243
comment(text: String) -> BitArray
INFERREDfn(String) -> BitArray

A comment keeps an idle event stream alive without dispatching an event.

function

event_headers

src/whiplash/body.pooka:257
event_headers() -> List(#(String, String))
INFERREDfn() -> 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.

function

events

src/whiplash/body.pooka:264
events(events: List(Event)) -> Stream

A 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

src/whiplash/body.pooka:282
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.

field

next

src/whiplash/body.pooka:282
next: fn(Int) -> Result(Option(BitArray), String)
INFERREDfn(Int) -> Result(Option(BitArray), String)
constructor

new

src/whiplash/body.pooka:282
INFERREDfn(fn(Int) -> Result(Option(BitArray), String)) -> whiplash/body.Reader
method

read

src/whiplash/body.pooka:283
read(timeout: Int) -> Result(Option(BitArray), String)
INFERREDfn(Int) -> Result(Option(BitArray), String)
method

all

src/whiplash/body.pooka:288
all(limit: Int, timeout: Int) -> Result(BitArray, String)
INFERREDfn(Int, Int) -> Result(BitArray, String)

The rest of the body as one binary, refused beyond limit bytes.