HTTP
HTTP handlers are declared in a service inside a context. See the
grammar for HTTP handlers for the production
and the diagnostics that govern it.
Handler form
Section titled “Handler form”service <Name> from http { on <METHOD>("<route>") (<params>) -> Effect[HttpResult[T]] { … }}- Methods:
GET,POST,PUT,PATCH,DELETE. - Route: must start with
/; a:namesegment is a path parameter. - Reserved characters:
*,{and}are reserved in a route, in any segment and inside a:name, for a future form that matches the rest of a path (bynk.http.reserved_route_syntax). To match one literally, take the segment as a:nameparameter and check it in the handler. - Parameters: each parameter is a path parameter (matching a
:namesegment), the specialbodyparameter, or the specialheaders,query,cookiesorcfparameter. A path parameter’s type must be constructible from a string (bynk.http.path_param_not_stringy);GETandDELETEmay not take abody(bynk.http.body_on_get_or_delete). - Return type: must be
Effect[HttpResult[T]](bynk.http.return_not_effect_http_result).
A body parameter is parsed from the request JSON and validated before the
handler runs; an invalid body is rejected with 400 at the boundary — unless the
handler is @raw, which takes the request’s octets as
Bytes instead, or @form, which decodes an HTML form post into
the same kind of record.
Request headers
Section titled “Request headers”A handler that needs the request’s headers declares a headers: RequestHeaders
parameter. It may sit anywhere among the parameters; the router passes the
request’s own headers. RequestHeaders is a read-only view with three methods:
| Method | Returns | Reads |
|---|---|---|
get(name) | Option[String] | any header, by case-insensitive name |
bearer() | Option[String] | the token of an Authorization: Bearer <token> header |
clientIp() | Option[String] | the platform’s CF-Connecting-IP (see below) |
on GET("/whoami") (headers: RequestHeaders) -> Effect[HttpResult[String]] by v: Visitor { match headers.bearer() { Some(token) => Ok("token \(token)") None => Unauthorized }}clientIp() reads CF-Connecting-IP, which Cloudflare’s edge sets on every
request, overwriting any value the client sent. It is the client’s address
only behind that edge: under bynk dev, in tests, or on any other host, the
caller sets the header, so don’t rate-limit or audit by it there.
This is how a service implements an existing API’s authentication scheme, one an actor does not describe. Where an actor fits, prefer it: the seam verifies the credential before the handler runs.
A route whose by actor’s seam consumed a credential does not show it in this view. On a Bearer or Oidc
route the Authorization header is omitted, so get("authorization") and bearer() are None; on a
Signature route the signature header is omitted (the body stays readable). A SignedCookie route omits its
session cookie, and on an actor sum every member’s credential is omitted. A handler that must forward a token
downstream can’t take it from here. A by v: Visitor route consumed nothing, so the example above works.
The view is read while the request is handled, never kept: RequestHeaders can’t
be a field of a type or cross a boundary (a call, a message, an agent), though a
plain function may take it to share a check between handlers
(bynk.types.headers_at_boundary). A parameter named headers must have this
type (bynk.http.headers_wrong_type). A test builds one with
RequestHeaders.of.
Cloudflare request metadata
Section titled “Cloudflare request metadata”A handler that wants what Cloudflare derived about the connection (the caller’s
country, the data centre that served the request, the ASN) declares a
cf: RequestCf parameter. It may sit anywhere among the parameters. RequestCf is
a record in bynk.cloudflare, so naming it needs consumes bynk.cloudflare { RequestCf }
and locks the deployment unit to Cloudflare (a platform lock, as for any
bynk.cloudflare name). It is experimental: using it needs bynk.cloudflare.RequestCf
in [unstable] allow.
consumes bynk.cloudflare { RequestCf }
service api from http { on GET("/hello") (cf: RequestCf) -> Effect[HttpResult[String]] by v: Visitor { match cf.country { Some("GB") => Ok("Hello") _ => Ok("Hi") } }}| Field | Type | request.cf property |
|---|---|---|
country, continent, city, region, regionCode, postalCode, timezone | Option[String] | the property of the same name |
latitude, longitude | Option[Float] | the property of the same name, which Cloudflare sends as a numeric string |
colo | Option[String] | colo, the data centre’s airport code |
asn | Option[Int] | asn |
asOrganization, httpProtocol, tlsVersion | Option[String] | the property of the same name |
isEuCountry | Option[Bool] | isEUCountry, which Cloudflare sends as "1" for a country in the EU |
Every field is optional, and the parameter is not: a request with no
request.cf (a test, the dashboard’s preview) gives a RequestCf whose fields are
all None, never a fault, so the handler reads cf.country without first matching
on whether cf is there. A property of an unexpected type is None too. The set is
the initial one; a field is added later as a defaulted field, which does not break a
handler. The plan-dependent properties (bot management, client certificates) are not
part of it.
The facts come from the client’s IP address, so they can be personal data, and they are best-effort: a VPN or a proxy makes them wrong. Cloudflare documents the properties on its Request page.
cf is not part of a route’s wire contract: bynk client and
bynk openapi leave it out, so a handler that declares it has
the same client and the same OpenAPI document as without it. A parameter named cf
must be typed RequestCf (bynk.http.cf_wrong_type), and a :cf path segment
stays a path parameter.
A test builds one with the record literal: RequestCf { country: Some("GB") }, and
RequestCf {} for a request with none. At the system tier the request is real, so
cf carries whatever the platform supplies there, and the argument a case passes is
not sent.
Query strings
Section titled “Query strings”A handler reads the request’s query string through a query parameter, in one
of two forms. It may sit anywhere among the parameters, and a handler has at most
one. A :query path segment keeps its meaning as a path parameter.
A typed query
Section titled “A typed query”query: T, where T is a record, decodes the query string into T and
validates it before the handler runs, as a body is:
type Limit = Int where InRange(1, 500)type Day = String where Matches("^\\d{4}-\\d{2}-\\d{2}$")
type ActivityFilter = { repo: Option[String], since: Option[Day], tag: List[String] = [], limit: Limit = 50,}on GET("/activity") (query: ActivityFilter) -> Effect[HttpResult[List[Activity]]] by r: Reader { Ok([]) }GET /activity?repo=bynk-lang/bynk&tag=a&tag=b&limit=100 decodes to a filter;
?limit=0 or ?since=yesterday is a 400 before the handler runs, with the same
RefinementViolation or StructuralMismatch body a bad body gets (the path
names the field, query.limit).
A field of T is a value a query string can carry:
- a
String,Int,BoolorDecimal, a refined or opaque type over one of those, or an enum (?kind=Push); - an
Optionof one, which isNonewhen its key is absent; - a
Listof one, filled by a repeated key (?tag=a&tag=b). It is empty only when the field has a default, astagdoes above; with none, an absent key is a400(give it a default of[]to make the key optional).
Anything else is refused (bynk.http.query_field_not_stringy), and so is a
query that is not a record (bynk.http.query_wrong_type). A field’s default
applies when its key is absent. Names and values are percent-decoded, and + is
a space, as bynk.url.decodeQuery reads them.
Two decisions keep the boundary strict where it matters and tolerant where it doesn’t:
- Unknown keys are ignored, as unknown fields of a
bodyare: a cache-buster or a tracking parameter (?utm_source=…) is not an error. - A repeated key on a field that is not a
Listis a400(StructuralMismatch), rather than the first value silently winning:?limit=1&limit=2is ambiguous, and the boundary says so.
The query never affects which handler runs. Routes are still told apart by method and path alone.
The raw view — query: RequestQuery
Section titled “The raw view — query: RequestQuery”For a proxy, or keys only known at run time, a handler declares
query: RequestQuery, a read-only view of the query string. It mirrors
RequestHeaders, and nothing is validated:
| Method | Returns | Reads |
|---|---|---|
get(name) | Option[String] | the first value of a key |
all(name) | List[String] | every value of a key, in order ([] when absent) |
params() | Map[String, List[String]] | every key with its values |
on GET("/echo") (query: RequestQuery) -> Effect[HttpResult[String]] by v: Visitor { Ok(query.get("q").getOrElse("-"))}The view is read while the request is handled, never kept: RequestQuery can’t be
a field of a type or cross a boundary (bynk.types.request_query_at_boundary), and only an
HTTP handler’s query can have it (bynk.http.query_not_http). A test builds one
with RequestQuery.of.
HttpResult variants
Section titled “HttpResult variants”The vocabulary tracks the common, modern HTTP status codes (RFC 9110). A
variant’s payload is one of seven shapes: the value T as JSON (Value), a
target URL emitted as a Location header (Location), an explanatory
message as an { "error": … } JSON body (Message), a Stream[String]
emitted as an SSE (text/event-stream) body (Streamed), a raw
Bytes body under an author-declared content-type (Raw, and RawCreated at
201), the same with the entity tag its ETag carries (RawTagged), a
Stream[Bytes] body streamed chunk by chunk (RawStreamed), or no body at all
(None).
2xx success
Section titled “2xx success”| Variant | Status | Payload |
|---|---|---|
Ok(value) | 200 | the value, as JSON |
Streaming(stream) | 200 | a Stream[String], SSE-framed (see Streamed responses) |
Raw(body, contentType) | 200 | a Bytes body under the given content-type (see Raw responses) |
RawTagged(body, contentType, etag) | 200 | as Raw, sent with etag as its ETag (see Supplying the validator) |
RawStreamed(body, contentType) | 200 | a Stream[Bytes] body, streamed chunk by chunk (see Streamed raw responses) |
RawCreated(body, contentType) | 201 | as Raw (see Raw responses) |
Created(value) | 201 | the value, as JSON |
Accepted(value) | 202 | the value, as JSON |
NoContent | 204 | none |
3xx redirection
Section titled “3xx redirection”A redirect carries the target URL, emitted as a Location header with an empty
body.
| Variant | Status | Payload |
|---|---|---|
MovedPermanently(url) | 301 | Location header |
Found(url) | 302 | Location header |
SeeOther(url) | 303 | Location header |
TemporaryRedirect(url) | 307 | Location header |
PermanentRedirect(url) | 308 | Location header |
4xx client error
Section titled “4xx client error”| Variant | Status | Payload |
|---|---|---|
BadRequest(message) | 400 | message |
Unauthorized | 401 | none |
Forbidden | 403 | none |
NotFound | 404 | none |
MethodNotAllowed | 405 | none |
NotAcceptable | 406 | none |
RequestTimeout | 408 | none |
Conflict(message) | 409 | message |
Gone | 410 | none |
LengthRequired | 411 | none |
PayloadTooLarge(message) | 413 | message |
UnsupportedMediaType(message) | 415 | message |
UnprocessableEntity(message) | 422 | message |
TooManyRequests(message) | 429 | message |
UnavailableForLegalReasons(message) | 451 | message |
5xx server error
Section titled “5xx server error”| Variant | Status | Payload |
|---|---|---|
ServerError(message) | 500 | message |
NotImplemented(message) | 501 | message |
BadGateway(message) | 502 | message |
ServiceUnavailable(message) | 503 | message |
GatewayTimeout(message) | 504 | message |
Error bodies
Section titled “Error bodies”The error variants answer with Bynk’s own body: { "error": message }, or no body
at all. A service that implements an existing API’s contract needs that API’s error
shape instead. RawError(status, body, contentType) answers an error status with a
Bytes body you choose, under the content type you
give, and runs no codec, just as Raw does for a 200:
fn problem(code: String, message: String) -> Bytes { Bytes.fromUtf8("{\"error\":\"\(code)\",\"message\":\"\(message)\"}")}on PUT("/packages/:name") (name: String, body: String) -> Effect[HttpResult[()]] by v: Visitor { RawError(Conflict, problem("conflict", "\(name) already exists"), "application/json")}The status is an ErrorStatus: the name of one of the 4xx or 5xx variants in the
tables above (Forbidden, Conflict, ServiceUnavailable, …), bare or qualified
(ErrorStatus.Forbidden). Only those names are accepted, so a RawError can’t
answer a 2xx or 3xx (bynk.http.raw_error_status). The status is fixed where the
response is built: ErrorStatus is not a type, so a helper takes the parts of the
body and the handler names the status (bynk.http.error_status_position).
A test matches a RawError by its number: expect r is RawError(409, _, _). A
system-tier test decodes the response by its status, as it does for every
variant, so there the same response reads as Conflict(_).
Rendering rejections
Section titled “Rendering rejections”The router refuses some requests before any handler runs: a 400 for a body or
path parameter that fails to parse or validate, a 405 for a method the path
doesn’t declare, a 413 for a body over its cap, a 415 for a @raw, @form or @multipart route’s
other content type, and a 401 from a Signature seam. Each answers in Bynk’s own
shape, { "kind": …, "details": … } or no body. A service whose contract says
otherwise renders them itself with on rejected:
on rejected(why: BoundaryRejection) -> Effect[HttpResult[()]] { match why { PayloadTooLarge(_) => RawError(PayloadTooLarge, problem("too_large", "the body is too large"), "application/json") MethodNotAllowed => RawError(MethodNotAllowed, problem("method", "no such method"), "application/json") _ => RawError(BadRequest, problem("bad_request", "the request is malformed"), "application/json") }}why is the rejection: MalformedJson(detail), StructuralMismatch(detail),
RefinementViolation(detail), PayloadTooLarge(detail),
UnsupportedMediaType(detail), MalformedForm(detail), MethodNotAllowed or Unauthorized.
The status is the rejection’s own. The handler supplies the body, so it answers a
RawError of that status; the router keeps its other headers (the Allow of a
405) and stamps the service’s CORS and security headers as usual. Any other
answer, another status or a fault in the handler sends the router’s default
response instead, and logs why. A service without on rejected is unchanged.
The hook renders the rejections of its own service’s routes. A 405 belongs to
a path rather than a route, so when two from http services in one context share
a path, the path’s 405 uses the hook of the service declared first, just as its
CORS and security headers do. A service with on rejected and no routes
compiles; its hook is never called.
A service has at most one (bynk.http.duplicate_rejected). It takes exactly the
BoundaryRejection and returns HttpResult[()], and it has no by (it runs before
any actor is known) and no given (bynk.http.rejected_signature). The 401s an
actor’s Bearer or OIDC seam returns, and the body refusals of a route whose by
is a sum of actors, are not rendered through it yet.
Lifting an Option with ? (v0.153)
Section titled “Lifting an Option with ? (v0.153)”Inside a handler returning HttpResult[T] (or Effect[HttpResult[T]]), the ?
operator lifts an Option: Some(v) yields v, and None early-returns
NotFound (404). This collapses the outer half of the ubiquitous read →
respond pyramid — a lookup that misses becomes a 404 without a match:
on GET("/links/:code") (code: Slug) -> Effect[HttpResult[String]] by v: Visitor given Kv { let stored <- Kv.get(code.value) // stored : Option[String] let raw = stored? // None → 404 NotFound; Some(s) → s Ok(raw)}? on an Option is accepted only where the enclosing return is an
HttpResult — elsewhere an Option has no error channel, and the checker
rejects it with bynk.types.question_option_outside_http (use .okOr(err) to
turn an Option into a Result in a Result-returning function). A Result
under ? is unchanged: its Err propagates as before. Mapping a domain
error into an HttpResult — the Result → HttpResult direction — is a
separate, later capability, tracked with the declared-embedding design
(#543); see
Answering another status for what to write today.
? on an Option always means 404. That is right for “the thing you asked
for is not there” (a lookup that misses), and wrong for a missing or malformed
request input, which is a 400. Hovering the ? in an editor says so
(None → 404 NotFound), and RequestHeaders.get(…)? is flagged by
bynk.http.header_question_not_found (below).
Answering another status
Section titled “Answering another status”let … else names the status. At the top of a handler body the else block’s
value is the handler’s result, so a bare BadRequest(…) is accepted there:
on POST("/github") (headers: RequestHeaders, body: Bytes) -> Effect[HttpResult[String]] by GitHub { let Some(event) = headers.get("X-GitHub-Event") else { BadRequest("missing X-GitHub-Event") } let Some(row) = stored else { NotFound } // the 404 case, spelled out Ok(event)}Where the None arm needs a computed answer, use else match { None => … }. A
wrongly typed else is rejected with bynk.types.match_arm_mismatch.
The header lint
Section titled “The header lint”headers.get("X")? in an HttpResult handler is the shortest spelling of the
wrong status: a delivery missing the header would answer 404, telling the
sender the URL does not exist. The checker warns
(bynk.http.header_question_not_found, a non-failing warning) on exactly that
shape — get on a RequestHeaders value, under ?, in an HttpResult
handler. Query parameters, parsed bodies and every other Option are not
flagged: ? keeps its 404 there. When the ? is the whole value of a plain
let x = headers.get("X")?, the warning carries a machine-applicable fix
(bynk check --fix, or the editor’s quick fix) that rewrites it to the
let Some(x) = headers.get("X") else { BadRequest("missing X") } above, keeping
the binding name and leaving the message for you to edit.
Cookies
Section titled “Cookies”A handler reads the request’s cookies through a cookies: T parameter, where T is a
record. The router parses the Cookie header, decodes it into T and validates it before
the handler runs, as it does a typed query: refinements, defaults,
Option and List fields, and a 400 with the same RefinementViolation or
StructuralMismatch body a bad body gets (the path names the cookie,
cookies.sid). The parameter may sit anywhere among the parameters.
type Token = String where NonEmpty && MaxLength(128)type Theme = enum { Light, Dark }
type Session = { sid: Option[Token], theme: Theme = Light,}on GET("/me") (cookies: Session) -> Effect[HttpResult[String]] by v: Visitor { match cookies.sid { Some(_) => Ok("signed in") None => Unauthorized }}A field of T is a value a cookie can carry, the set a typed query takes: a String,
Int, Bool or Decimal, a refined or opaque type over one of those, or an enum;
an Option of one, which is None when the cookie is absent; or a List of one, filled
by a name sent twice. Anything else is refused (bynk.http.cookies_field_not_stringy), and
so is a cookies that is not a record (bynk.http.cookies_wrong_type).
- Parsing. The header is read as RFC 6265 §5.4 describes: pairs split on
;, each on its first=, names and values trimmed, one pair of double quotes dropped from a value. A pair with no=, or an empty name, is ignored. Cookies the record does not name are ignored, so an analytics cookie is not an error. - No decoding. A cookie value is opaque. It is not percent-decoded, so
a%41is the five characters it is, and the writer emits what it is given. A service that stores structured data in a cookie encodes it itself (toBase64Url,Json.encode). - A name sent twice. A browser sends the longer-
Pathcookie first when two share a name. On a field that is not aListthat is a400(StructuralMismatch), rather than the first one silently winning, as for a repeated query key. - An empty cookie (
a=) is a value, not an absent one.
A cookie is not authentication: it reaches the router from the client, so treat every field as input to check. A cookie-backed actor is separate. See #3181.
Reading a prefixed cookie
Section titled “Reading a prefixed cookie”A cookie named __Host-sid or __Secure-id is not a legal record field name. There are two
ways to read it. The typed way is a @name on the field,
which gives it the key the browser sends, so the value is decoded and validated before the
handler runs:
type SessionCookies = { @name("__Host-sid") sid: Option[Token],}on GET("/me") (cookies: SessionCookies) -> Effect[HttpResult[String]] by v: Visitor { match cookies.sid { Some(t) => Ok("\(t)") None => Unauthorized }}The other is cookies: RequestCookies: the raw, read-only view of the request’s cookies. It
is not decoded or validated at the boundary, so the handler builds the values it needs. Use it
when the names are not known until the request arrives.
on GET("/me") (cookies: RequestCookies) -> Effect[HttpResult[String]] by v: Visitor { match cookies.get("__Host-sid") { Some(raw) => Ok(raw) None => Unauthorized }}| Method | Type | Meaning |
|---|---|---|
get(name) | Option[String] | the first cookie of that name |
all(name) | List[String] | every cookie of that name, in header order |
names() | List[String] | each name once, in order of first appearance |
Parsing is the typed form’s: RFC 6265 §5.4, no decoding. Pass the value through a refined
type’s .of (Token.of(raw)) to check it. A RequestCookies may only be a handler’s
cookies parameter (bynk.http.cookies_not_http; it cannot be stored or sent,
bynk.types.request_cookies_at_boundary). Reading __Host- cookies this way is the
defence against cookie tossing, since a sibling subdomain cannot set one.
Setting cookies
Section titled “Setting cookies”bynk exports a Cookie record and a SameSite enum (Strict, Lax, Unrestricted;
the last is SameSite=None). withCookie attaches a cookie to any HttpResult, and the
router sends one Set-Cookie header for each.
on POST("/login") () -> Effect[HttpResult[String]] by v: Visitor { Created("ok").withCookie(Cookie { name: "sid", value: "abc", maxAge: Some(30.minutes) })}The defaults are the safe ones: path: "/", secure: true, httpOnly: true,
sameSite: Lax, partitioned: false, and no domain, maxAge or expires, so the
cookie ends with the browser session. A literal that sets one of these unsafe is refused
or warned about:
| Diagnostic | When |
|---|---|
bynk.http.cookie_samesite_none_insecure | sameSite: SameSite.Unrestricted with secure: false |
bynk.http.cookie_partitioned_insecure | partitioned: true with secure: false |
bynk.http.cookie_host_prefix | a __Host- name that is not secure, has a domain, or a path other than / |
bynk.http.cookie_secure_prefix | a __Secure- name that is not secure |
bynk.http.cookie_script_readable | httpOnly: false (a warning) |
bynk.http.cookie_domain_widens | a domain on a cookie whose name is not __Host- (a warning): every subdomain receives it |
bynk.http.cookie_cross_site_without_credentials | a service attaches a SameSite.Unrestricted cookie but has no cors { credentials: true } (a warning): a cross-origin fetch can never send it back |
bynk.http.cookie_untyped_has_field | an untyped Cookie literal named like a field of a cookies record of the unit (a warning): write it with Record.field.cookie(v), which the quick-fix does where it can |
bynk.http.cookie_value_undecodable | the same literal, with a value that field’s decoder rejects ("dark" for an enum read as Dark) |
bynk.actor.signed_cookie_write_literal | a literal named like a SignedCookie actor’s session cookie: only Actor.issue and Actor.expire write it |
bynk.http.cache_public_reads_cookies | @cache(scope: public) on a handler that takes cookies |
Only a literal is judged. A computed cookie is checked when it is sent: a name that is not
a token, a value with whitespace, a quote, ,, ;, \ or a control character, an
unsafe Domain or Path, an Expires outside the range of dates, or a header over
4096 bytes faults the request (500) rather than sending a malformed or unsafe header.
clearCookie(c) removes a cookie: it sends c again, empty, with Max-Age=0 and an
epoch Expires, under the same name, Domain and Path, which is what a browser matches
on. It keeps the cookie’s own secure and sameSite, so build it as the cookie was set.
on POST("/logout") () -> Effect[HttpResult[()]] by v: Visitor { NoContent.clearCookie(Cookie { name: "sid", value: "" })}Writing a cookie from its record
Section titled “Writing a cookie from its record”A name and a value written by hand are not tied to the record that reads the cookie. When
the cookie is a field of a cookies record, write it from the field instead:
type Theme = enum { Light, Dark }type Prefs = { theme: Theme = Light, @name("__Host-visits") visits: Int = 0,}on POST("/theme/dark") () -> Effect[HttpResult[String]] by v: Visitor { Ok("dark").withCookie(Cookie { ...Prefs.theme.cookie(Dark), maxAge: Some(365.days) })}
on POST("/theme/reset") () -> Effect[HttpResult[()]] by v: Visitor { NoContent.withCookie(Prefs.theme.expire())}Prefs.theme.cookie(Dark) is a Cookie named by the field’s wire name (its @name, here
__Host-visits for visits) and valued as the cookies decoder reads it: an Int is its
digits, a Bool is true or false, an enum is its variant’s name, and a String, a
refined or opaque type over one, and a Decimal are their text. An Option[T] field takes a
T. Every attribute is the Cookie default, so give others by spreading it into a Cookie
literal. Prefs.theme.expire() is the deletion Cookie (empty, Max-Age=0, epoch
Expires, default Path and no Domain; a cookie written with another Path or Domain
needs the same on its deletion). Renaming the field renames the write, and a value of the
wrong type is a type error at the write, not a 400 on the next request.
The record must be the cookies parameter of an on http handler in this unit
(bynk.http.cookie_field_not_cookie_record), the field must exist
(bynk.http.cookie_field_unknown), cookie takes the one value and expire none
(bynk.http.cookie_field_arity), and a List field is refused, since a repeated name is
several Set-Cookie lines (bynk.http.cookie_field_list). Prefs.theme alone is not a value.
The prefix and attribute rules above apply to a typed cookie too, judged against the field’s
wire name: Cookie { ...Prefs.visits.cookie(n), domain: Some("example.com") } for a
__Host-visits field is bynk.http.cookie_host_prefix. The session cookie of a SignedCookie
actor cannot be written through a record (bynk.actor.signed_cookie_write_directly) or a
literal (bynk.actor.signed_cookie_write_literal); only Actor.issue and Actor.expire write it.
A cookie in no record is still written as a plain Cookie { name, value }; where a record reads
the name, the literal draws bynk.http.cookie_untyped_has_field.
A response that sets a cookie is one user’s, so a public cache may not keep it: at
runtime a response carrying Set-Cookie is always sent Cache-Control: private, and a
conditional-GET 304 keeps the Set-Cookie of the response it replaces. In a test,
result.cookies() reads the List[Cookie] a result carries.
Method semantics
Section titled “Method semantics”The methods a path answers are derived from the routes you declare — there is
nothing to write. For a service declaring POST /links and GET /links/:code,
the router synthesises, from that table alone:
| Request | Answer |
|---|---|
GET /links (a live path, wrong method) | 405 with Allow: OPTIONS, POST |
OPTIONS /links (a plain, non-preflight OPTIONS) | 204 with Allow: OPTIONS, POST |
HEAD /links/:code | the GET status and headers, with an empty body |
OPTIONS /links/:code | 204 with Allow: GET, HEAD, OPTIONS |
DELETE /links/:code (wrong method) | 405 with Allow: GET, HEAD, OPTIONS |
GET /nope (no such path) | 404 — unchanged |
The allowed-method set for a path is the union of the methods declared on it,
plus OPTIONS always, plus HEAD whenever GET is present. So:
405 + Allow— a request to a live path with an undeclared method is a405(not a404), carrying the derivedAllowheader (RFC 9110 §15.5.6). A request to a path that does not exist is still a404.OPTIONS— a plain (non-preflight)OPTIONSto a known path is a204carryingAllow. (A CORS preflight — anOPTIONSbearingAccess-Control-Request-Method— is answered by the CORS machinery instead, with theAccess-Control-*grant.)HEAD— aHEADto aGETroute runs theGEThandler and returns its status and headers with an empty body (RFC 9110 §9.3.2). Because the handler runs, the headers are exactly aGET’s; aStreamingGETanswered asHEADreturns the stream’s headers without draining it.content-lengthis omitted (the body is never materialised — permitted).
HEAD and OPTIONS are not declarable methods — there is no on HEAD/on OPTIONS to write; they are synthesised. This is a router correctness property
with no configuration and no “off”: every from http service answers its method
contract. The 405/OPTIONS answers are produced before the by/Bearer
auth seam (method discovery and method rejection are credential-less); a HEAD
runs the GET handler and so runs GET’s auth seam unchanged.
Caching
Section titled “Caching”A GET response splits into two halves — a validator (has this changed?) and
a freshness window (is stale data acceptable, and for how long?). The validator
is derivable from the bytes the handler already produces, so the compiler
synthesises it; the freshness window is a judgement only you can make, so you
declare it. Nothing else.
Automatic revalidation (ETag / 304)
Section titled “Automatic revalidation (ETag / 304)”Every eligible GET — one returning the JSON Ok variant, or a Raw body of
at most 1 MiB — carries a synthesised weak
ETag over its
body (the serialised JSON, or the raw bytes). When a client re-requests with a matching
If-None-Match, the router answers 304 Not Modified with an empty body,
saving the transfer. This is on by default, with nothing to write:
| Request | Answer |
|---|---|
GET /links/:code (first call) | 200 + body + ETag: W/"…" |
GET /links/:code, If-None-Match: W/"…" (unchanged) | 304, empty body, same ETag |
GET /links/:code, a stale If-None-Match | 200 + body + the new ETag |
Only Ok and a small Raw are eligible. Streaming, redirects, and error
variants carry no ETag and are never answered 304 (a stream has no hashable
body; the rest have no representation to validate). A Raw body over 1 MiB
carries none either: hashing runs on every request, in proportion to the body,
and above that size it would spend the Worker’s CPU budget — return
RawTagged to give a large body a validator.
The validator is weak
(W/"…"): it asserts semantic equivalence of the representation, which is what
revalidation needs. Because it is a content hash, a 304 still runs the handler
and serialises the body — it saves bandwidth, not server work. A cheaper
validator that short-circuits before the handler (e.g. a Last-Modified from a
store timestamp) is a planned follow-on.
A query string is part of what a GET answers, so the ETag is a hash of the body
that that query produced: GET /activity?limit=10 and GET /activity?limit=20
carry different validators, and an If-None-Match from one is never answered 304
for the other. The Workers cache that an EdgeCache
instance turns on keys a response by its full URL, query included, so
@cached answers for two queries on one path are stored apart. EdgeCache.purge
takes a path prefix, so it clears every query variant under it.
Supplying the validator (RawTagged)
Section titled “Supplying the validator (RawTagged)”When the handler already has a validator — a content hash in the key, an object
store’s etag, a version number — return RawTagged(body, contentType, etag)
instead of Raw. The etag is sent as the response’s ETag exactly as given,
with no hashing whatever the body’s size, and a matching If-None-Match is
answered 304 through the same router step:
@cache(maxAge: 365.days, scope: public, immutable: true)on GET("/dl/sha256/:hex") (hex: String) -> Effect[HttpResult[()]] by v: Visitor given Store { match Store.get("dl/sha256/\(hex)") { Some(body) => RawTagged(body, "application/vnd.bynk.tree-v1", "\"sha256-\(hex)\"") None => NotFound }}The etag is an entity tag as RFC 9110 §8.8.3
writes it: double-quoted, optionally W/-prefixed ("v1", W/"v1"), with
printable ASCII inside and no ". Leaving the W/ off makes it a strong
validator. It is checked when the response is built: a malformed tag (v1,
unquoted) is never sent as a broken header — the handler’s request faults, the
client gets a 500, and the Worker logs the route (files GET /dl/sha256/:hex faulted) with the reason. RawTagged sends its ETag on any method; only a
GET is answered 304.
The 304 is a router response synthesised from the request, not an
HttpResult variant — there is no NotModified to return. Like the CORS
preflight, it is stamped with Access-Control-Allow-Origin for a CORS service, so
a cross-origin revalidation stays readable by the browser.
Declared freshness (@cache)
Section titled “Declared freshness (@cache)”To let a client or CDN serve a response without revalidating for a window,
annotate the handler with @cache, written immediately before on GET:
service links from http { @cache(maxAge: 5.minutes) on GET("/links/:code") (code: Slug) -> Effect[HttpResult[Url]] by v: Visitor given Kv { … }}This emits Cache-Control: private, max-age=300 alongside the automatic ETag.
@cache is the first handler-position annotation; it is legal only on a GET
handler, and applies to a response that is Ok, Raw or RawTagged. It is
ignored on Streaming and on errors.
| Field | Meaning | Default |
|---|---|---|
maxAge | The freshness window, as a whole-second Duration — emitted as Cache-Control: max-age in whole seconds. Required. | — |
scope | public or private. private lets only a client’s own cache store the response; public also lets a shared cache / CDN store it. | private |
immutable | true appends the immutable directive (Cache-Control: public, max-age=31536000, immutable): browsers and CDNs do not revalidate within maxAge. | false |
immutable: true is your promise that the representation at this URL never
changes — true of a content-addressed path, whose URL changes whenever its
content does. A wrong promise means clients and CDNs serve the stale response
until maxAge runs out, with no revalidation to correct them. A value that is
not true or false is bynk.http.cache_bad_immutable.
The private default is the safe one: a shared cache never stores a response
unless you opt into public. A GET with no @cache still carries its ETag
(so it is revalidatable) but emits no Cache-Control. A duration literal’s unit agrees with its number —
5.minutes, 1.hour, 30.seconds; a value with a fractional-second
remainder (500.milliseconds, or 1500.milliseconds, which is not a whole
number of seconds even though it is over one) is
bynk.http.cache_max_age_fractional_seconds — Cache-Control: max-age has no
finer granularity, so the remainder would otherwise be silently dropped rather
than honoured; @cache on a non-GET (or streaming) handler is
bynk.http.cache_on_non_get.
@cache only sets the header. Cloudflare does not store a response a Worker
generates because of its Cache-Control, so on its own @cache is honoured by
browsers and other caches downstream, not by Cloudflare’s edge. To cache at the
edge, declare an EdgeCache
instance in the context. Its Worker then keeps a @cache route’s 2xx at the
edge, sends no-store on every other GET or HEAD answer (a 404 included),
and can purge what it cached. That is the purge path an immutable response
needs if it can ever change, as a takedown changes it.
Streamed responses
Section titled “Streamed responses”Streaming(stream) returns a 200 whose body is a Stream[String],
emitted as Server-Sent Events (content-type: text/event-stream). Each stream
element becomes one SSE event — data: <element>\n\n — so a handler can send an
incremental feed without buffering the whole response:
on GET("/ticks") () -> Effect[HttpResult[()]] by v: Visitor { Streaming(Stream.of(["tick-1", "tick-2", "tick-3"]).take(3))}A streamed handler returns Effect[HttpResult[()]] — the JSON body parameter
T is unused, since the body is the stream. A response commits its status and
headers before the first chunk, so streaming is 200-only: handle a
pre-stream failure by returning an ordinary variant instead of Streaming
(NotFound, Unauthorized(…), …), which share HttpResult[()] and so sit in
the same handler with no type conflict:
on GET("/feed/:mode") (mode: String) -> Effect[HttpResult[()]] by v: Visitor { if mode == "live" { Streaming(Stream.of(events).take(100)) } else { NotFound }}Raw responses
Section titled “Raw responses”Raw(body, contentType) returns a 200 whose body is a raw
Bytes, written straight into the response under
the content-type you give — no codec runs. This is the service-tier escape
hatch for non-JSON bodies: robots.txt, sitemap.xml, .well-known documents,
RSS/Atom feeds, a CSV download, a QR-code PNG. Bynk serves bytes with a
content-type; it does not template HTML (that is the frontend tier).
Bytes is binary-first, so a PNG flows in directly and text goes through
Bytes.fromUtf8 — which makes the UTF-8 charset an explicit author decision
rather than a runtime guess:
on GET("/sitemap.xml") () -> Effect[HttpResult[()]] by v: Visitor { let xml = "<?xml version=\"1.0\"?><urlset></urlset>" Raw(Bytes.fromUtf8(xml), "application/xml")}Like Streaming, Raw returns Effect[HttpResult[()]] — the JSON body
parameter T is unused — and is 200-only: it serves service-tier bodies,
which are overwhelmingly 200. A custom-status raw body (a 404 with an HTML
error page) is a presentation concern held out of scope; a Raw branch and an
ordinary pre-body variant share HttpResult[()], so they coexist in one handler.
The content-type is an opaque String, unvalidated — you own it. If you write
Bytes.fromUtf8(s), the body is UTF-8, so a content-type claiming a different
charset would mislead; keep them in step.
A Raw GET is cacheable: a body of at most 1 MiB carries an
automatic weak ETag, and @cache adds a freshness window. For a larger body, or
when the handler already knows a validator, return
RawTagged(body, contentType, etag), which sends etag as the ETag (see
Supplying the validator).
RawCreated(body, contentType) is the same for a 201: a service that
implements an external contract answers a creation with the bytes the contract
fixes. Use Created(value) unless the contract fixes the bytes, since the codec
keeps the typed-wire guarantee and RawCreated gives it up. A 201 answers a write,
so RawCreated carries no ETag. RawCreated shares HttpResult[T] with the
handler’s other branches, so one handler can answer a new resource
RawCreated(…), a retry Ok(value), and a clash RawError(…):
on PUT("/packages/:name") (name: String, body: String) -> Effect[HttpResult[String]] by v: Visitor { if body == "same" { Ok("\(name) unchanged") } else { RawCreated(Bytes.fromUtf8("{\"name\":\"\(name)\"}\n"), "application/json") }}A test matches it at the unit tier as expect r is RawCreated(_, "application/json"),
binding body and contentType. A system-tier test decodes the response by its
status, so there it reads as Created(_): a JSON body decodes to the value, and
any other body is read as its text.
A file that never changes per request — a stylesheet, a script or an icon
beside a page a handler builds — is better shipped as a
static asset: the platform serves it without running
the Worker, at a content-hashed URL cached for a year, and the handler links it
through the asset’s .url. Markup that is mostly static but filled in per
request is better kept in its own file and read with an
embed; see
Serve a page from an HTML file.
A producer that can fail mid-stream carries its outcome in-band — build a
Stream[Result[String, E]] and .map it to Stream[String], encoding an Err
as an error event — because the HTTP status is already sent once streaming
begins. A bounded take is the language-level guard against an unbounded
response. A structured event type (named event/id/retry fields) is a
planned follow-on; v1 streams plain String events.
Streamed raw responses
Section titled “Streamed raw responses”RawStreamed(body, contentType) returns a 200 whose body is a
Stream[Bytes], written into the response one
chunk at a time as the stream yields them, so a body too large to buffer is never
held whole. The stream usually comes from a capability, inside a record with its
metadata:
on GET("/files/:key") (key: String) -> Effect[HttpResult[()]] by v: Visitor given Blobs { let d <- Blobs.read(key) RawStreamed(d.body, d.contentType)}It’s Raw without the buffer, so it differs from Raw in three ways:
- No
ETag. The bytes can’t be hashed without buffering them.@cachestill adds its freshness headers. ReturnRawTaggedwhen you have the whole body and a validator. - No
content-length. The size isn’t known until the stream ends. - A fault mid-body aborts the response. The status and headers are sent
before the first chunk, as for
Streaming. Answer a failure you can detect first (a missing object) with an ordinary variant instead, which sharesHttpResult[()].
The body is lazy: nothing is read from the stream until the client reads the
response, and a HEAD answered from the GET never reads it at all. Handing a
stream to a response consumes it, since a stream is read once.
A unit-tier test matches RawStreamed(body, contentType) and drains body with
.collect(); at the system tier it reads as Ok(_), with the body’s text as the
value.
Raw request bodies
Section titled “Raw request bodies”@raw(contentType: "…") marks a handler’s body: Bytes as the raw request
body: the router checks the request’s Content-Type, counts the bytes it reads
against the route’s cap, and hands the handler the octets untouched — no JSON,
no base64. It is the request-side counterpart of Raw responses,
and what an upload endpoint needs: an image, a CSV import, an archive, a webhook
payload that is not JSON.
service registry from http { limits { maxBody: 64_000_000 }
@raw(contentType: "application/vnd.bynk.tree-v1") on PUT("/api/:org/:package/:version") (org: String, package: String, version: String, body: Bytes) -> Effect[HttpResult[Json]] given Kv { … }
@raw(contentType: "image/png") @limit(maxBody: 5_000_000) on POST("/avatars/:user") (user: String, body: Bytes) -> Effect[HttpResult[String]] { … }}- The annotation.
@rawis legal only on aPOST/PUT/PATCHhandler that has abody, at most once, andbodymust be typedBytes.contentTypeis required: a media type (type/subtype), or"*/*"to accept any. Parameters such ascharsetare not part of the match. - Without it, a
body: Byteskeeps its base64-in-JSON wire form.@rawis opt-in because making everyBytesbody raw would silently break every caller that sends base64 JSON. 415 UnsupportedMediaType. The request’sContent-Type, with parameters stripped and compared case-insensitively, must equal the declared media type (unless it is"*/*"). A missing or different one is a synthesised415,{ kind: "UnsupportedMediaType", details: "…" }. Like the413, it is a router decision, not anHttpResultthe handler returns, and it comes before the auth seam and the body read.- A cap is required. A
@rawroute must have an effectivemaxBody— from@limitor the service’slimits { }— or it isbynk.http.raw_body_unbounded. An unbounded binary upload is the denial of service body limits exist to stop. - Buffered, or streamed. A
body: Bytesis read whole, up to the cap, before the handler runs. Abody: Stream[Bytes]is handed over as it arrives (see Streamed request bodies). A form post has its own@formdecoding, and a browser upload its own@multipart, whose parts are buffered. A@rawhandler can still takeheaders: RequestHeadersalongside its body, say to read a webhook’s event-name header. - Signatures. A
Signatureactor on a@rawroute computes its HMAC over the octets as received, and the handler gets those same bytes. - The content type is a contract, not a security boundary. The handler still validates what it receives.
Streamed request bodies
Section titled “Streamed request bodies”A @raw handler whose body is a Stream[Bytes]
receives the request body as it arrives, instead of after it has been read
whole. A large upload can then go on to a sink without being held in memory.
service archives from http { limits { maxBody: 5_000_000_000 }
@raw(contentType: "application/zip") on PUT("/archives/:name") (name: ArchiveName, headers: RequestHeaders, body: Stream[Bytes]) -> Effect[HttpResult[()]] by u: User given Archives { // `size` is the Int parsed from headers.get("content-length"); // R2.putStream needs the length up front. let _ <- Archives.putStream(name.value, body, size) NoContent }}- The type chooses it.
body: Stream[Bytes]under@rawstreams;body: Bytesbuffers, as above. There is no new annotation. Any other element type isbynk.http.raw_body_not_bytes, and aStream[Bytes]bodywithout@rawisbynk.http.stream_body_not_raw. A stream is still refused everywhere else a request or a result crosses a boundary (bynk.types.stream_at_boundary): as a result, an agent oron callparameter, a stored field or an event, so a streamed body cannot reach an agent. - Lazy, and read once. Nothing is read from the request until the handler (or a
sink) pulls, and each pull reads one chunk, so a slow sink slows the read. Draining
the stream, or passing it to a sink or a response, consumes it; a second read faults
with
bynk: a stream can be read only once. The platform buffers a window of its own ahead of the pull, of the order of megabytes. - The cap is still required, and counted as the handler reads. A declared
Content-Lengthover the cap is a413before the handler runs, as for a buffered body. A body that crosses the cap while the handler is reading it cancels the read and faults it, and the router answers the same413 PayloadTooLarge(asystemtest seesRejected(PayloadTooLarge(_))), not a500. The count is of the bytes actually read, so a chunked body, or one whoseContent-Lengthunderstates it, is cut off all the same. A client that is still sending when the413is answered may see its connection reset instead of the response; that is the platform’s and the client’s, and the cap guarantees only that the Worker stops reading. - Do the reading before other effects. The
413can follow work the handler has already done. A sink that fails whole is the safe pattern:R2.putStreamstores nothing if its stream faults. - Sinks.
R2.putStream(key, body, size)needs the length up front, so read it fromheaders.get("content-length"); a stream with more or fewer bytes thansizefaults the write and stores nothing. A chunked upload has noContent-Length, so it cannot go toputStream: pass it toFetch.sendStream, which needs no length, or answer411. A proxy reads the body first, then answers: it does not re-serve the request stream as its response (see below). - Not with a signature or a sum of actors. A
Signatureactor verifies an HMAC over the whole body before the handler runs (bynk.http.stream_body_signature), and a sum of actors reads the body itself (bynk.http.stream_body_on_sum_route). Use abody: Bytesroute for a signed webhook. A singlebyactor is checked on the request’s head, before any of the body is pulled. - Answering without reading is allowed: the unread body is discarded when the request ends. A client that is still sending a large body may see a reset instead of the response. A handler cannot read the request after it has returned, so read first, then answer.
- Do not build a response from the request stream. A handler that returns
RawStreamed(body, …), orRawStreamed(body.map(f), …), with the request’s ownbody(a streamed echo or transform) passes itsbynk testcases at every tier and then fails underwrangler dev. Nothing warns you: the compiler does not follow the stream into the response, and the test runner has no proxy in front of the handler.- What happens. With the pinned Wrangler (4.162.0 at the time of writing), a client that
sends a 256 KiB chunked body and then reads the response gets a
500. The dev server logsUncaught TypeError: This ReadableStream is currently locked to a readerandError inside ProxyWorker … Network connection lost. The same route, reading the body to the end withcollect()and then answering, returns200. - What is not known. Whether the fault is
wrangler dev’s proxy or workerd itself, and whether it also happens on Cloudflare’s edge. Driving Miniflare directly (no dev proxy) streamed 8 MiB back intact, and workerd separately refuses a read of the request body after the response has been sent (Can't read from request stream after response has been sent). Neither has been checked on a deployed Worker. - What to do. Read the body (
collect(), or hand it to a capability such asR2.putStream) and then answer, as the/archives/:nameexample above does. To echo bytes back, answer from what you collected. If a streamed answer is the point, take the stream from a capability, not from the request. - Why there is no warning. It is unconfirmed on the edge, and a diagnostic would be a new one; see #3396 for the status.
- What happens. With the pinned Wrangler (4.162.0 at the time of writing), a client that
sends a 256 KiB chunked body and then reads the response gets a
- Retries and idempotency. A request body cannot be replayed, so a client that retries
sends it again. Take an
Idempotencykey from the request’s head (a path segment or a header), not the body. - What stays buffered. A
@multipartfile part is still held whole. A large browser upload canPUTthe file itself to a streamed@rawroute. - On the bundle target the router is not part of the output: the host calls the
handler and supplies the
Stream[Bytes], and applies any cap itself. The in-process test tiers pass aStreamvalue directly.
The checks are bynk.http.raw_on_bodyless (a method with no body, or a handler
without body), raw_duplicate, raw_unknown_arg, raw_bad_content_type,
raw_body_not_bytes (neither Bytes nor Stream[Bytes]) and raw_body_unbounded. In a system test, a typed call
sends the Bytes raw under the declared type (application/octet-stream for
"*/*"), and a Wire body is sent as text/plain, so it probes the 415.
Form bodies
Section titled “Form bodies”@form marks a handler’s body as an HTML form post
(application/x-www-form-urlencoded) and decodes it into a record at the boundary,
with the same field types, defaults, Options and refinements as a typed
query or a JSON body. It is the request-side counterpart of a
<form method="post"> in a page that serves its own HTML.
type Email = String where Matches("^[^@]+@[^@]+$")type Age = Int where InRange(0, 150)type Plan = enum { Free, Pro }
type SignupForm = { email: Email, age: Option[Age], // absent or empty: None plan: Plan = Free, // absent: the default tags: List[String] = [], // a repeated key remember: Bool = false, // an unchecked checkbox sends nothing}@formon POST("/signup") (body: SignupForm) -> Effect[HttpResult[()]] by v: Visitor { SeeOther("/welcome")}email=a%40b.c&age=41&plan=Pro&tags=x&tags=y&remember=on decodes to a SignupForm;
email=nope is a 400 before the handler runs, with the RefinementViolation
body a bad JSON body gets, its path naming the field (body.email).
- The annotation.
@formis legal only on aPOST/PUT/PATCHhandler with abody(bynk.http.form_on_bodyless), at most once (bynk.http.form_duplicate), and takes no arguments (bynk.http.form_unknown_arg). It excludes@raw(bynk.http.body_kind_conflict): each fixes the wire format of the onebody. Thebodymust be a record (bynk.http.form_body_not_record), and a route whosebyis a sum of actors is refused (bynk.http.form_on_sum_route), since its wrapper reads the body as JSON. 415. A request whoseContent-Typeis notapplication/x-www-form-urlencoded(parameters stripped, case-insensitive) is a synthesised415, as for@raw, ahead of the auth seam and the body read. That includes JSON: a@formroute answers one media type. Acharsetparameter other than UTF-8 is a415too, because the decoder reads UTF-8 and nothing else.- Fields. A field is a
String,Int,BoolorDecimal, a refined or opaque type over one of those, or an enum (a unit variant, by name), optionally in anOptionor aList. Anything else, a nested record included, is refused (bynk.http.form_field_not_stringy): form keys are flat, with noa[b]nesting. A key matches the field’s name literally, so aListfieldtagsreadstags, nottags[]. Unknown keys are ignored. - Repeated keys. A key that repeats fills a
Listfield in order. On any other field it is a400(StructuralMismatch), never first-wins. - Absent and empty. An absent field takes its default; an
OptionisNone; otherwise the field is missing (400). A browser sends an unfilled input asname=, so for a form an empty value is absent for anOptionand for every field except a plainString, which stays""so aNonEmptyrefinement still fires. (A typedquerykeeps?limit=as a value, and so a400; the browser’s empty inputs are why a form differs.) - Checkboxes. An unchecked checkbox sends nothing, and a checked one sends
onunless its markup sets avalue. ABoolreadstrue/onastrueandfalse/offasfalse; anything else is a400. ABoolfield with no default and noOptionis a warning (bynk.http.form_bool_without_default), since an unchecked box would be a400; writeremember: Bool = false. - Numbers. An
Intreads-?[0-9]+within the safe-integer range; aDecimalreads its own text form. There is no locale (1,5), no leading+and no whitespace trimming: a refinement such asMatchesis how a field is tightened. - Charset and decoding. The body is decoded as UTF-8, as the URL standard does:
+is a space, and a percent-sequence that is not valid UTF-8 reads as U+FFFD rather than failing. - As a codec.
Form.decode[T](s)andForm.encode(v)expose this mapping to a@rawhandler or a test, withDecodeErrors rooted at$. - Caps. The body is buffered like a JSON one. A
@limitorlimits { maxBody }applies, with the same413; unlike a@rawroute, a@formroute needs no cap, but a public form should have one. - CSRF. A form post is a simple request in CORS terms, so a
cors { }policy does not stop a page on another origin submitting to the route. Nothing here authenticates the submitter; use an actor, and do not rely on a cookie alone. A route guarded by aSignedCookieactor is checked against cross-site posts at the seam (fetch metadata and an exactOriginmatch against the service’s credentialedcorsorigins).
In a system test a typed body is sent as the form its record encodes to, and a
Wire(…) body is sent as a form too, so a case can probe the decoding rules with
the text a browser would send (Wire("email=nope")). At the unit and
integration tiers the record reaches the handler directly. See
Sending a form.
Multipart bodies
Section titled “Multipart bodies”@multipart marks a handler’s body as a browser upload (multipart/form-data)
and decodes it into a record at the boundary: text parts as a @form
body’s fields, and file parts as File values. The route is
buffered whole under a body cap, and the parts are parsed from that buffer.
consumes bynk { File }
type Upload = { caption: Option[String], photo: File, extras: List[File] = [],}
service site from http { limits { maxBody: 20_000_000, maxFile: 5_000_000 }
@multipart on POST("/avatar") (body: Upload) -> Effect[HttpResult[()]] by u: User given Photos { let _ <- Photos.put(u.identity, body.photo.bytes) NoContent }}A File is the record bynk exports: filename: Option[String] (None for a part
with no filename), contentType: String (what the client claimed) and bytes: Bytes
(the whole part; its size is bytes.length()). Both the name and the type are
client-controlled: never use filename as a storage key or a path, and do not
trust contentType as proof of what the bytes are. A part with no Content-Type reads
as application/octet-stream on Workers; the local Node runner reports text/plain
for it, so a handler should not branch on that case.
- The annotation.
@multipartfollows@form’s placement rules (bynk.http.form_on_bodyless,form_duplicate,form_unknown_arg,form_on_sum_route) and excludes@rawand@form(bynk.http.body_kind_conflict). Thebodyis a record whose fields are the form leaves, or aFile, optionally in anOptionor aList(a repeated part); keys are flat (bynk.http.form_field_not_stringy). - A cap is required. The body is held in memory with a parsed copy beside it, so a
@multipartroute needs an effectivemaxBody, from@limitor the service’slimits { }(bynk.http.multipart_body_unbounded). A cap over 32 MiB is a warning (bynk.http.multipart_large_cap): a Worker has 128 MB for everything it does, and the platform’s own request-body ceiling depends on the zone’s plan. - Caps on the parts.
maxFile(bytes in one file part) andmaxParts(parts in the request, 64 unless set) joinmaxBodyinlimits { }and@limit(…); the route’s@limitwins.maxFileandmaxPartsin@limitare only valid on a@multipartroute (bynk.http.limit_on_non_multipart), and must be positiveInts (bynk.http.limit_bad_max_file). Past either cap is a413, asmaxBodyis. 415,400and413. AnotherContent-Typeis a415. A body that is not multipart (noboundary, or one that never closes) is a400MalformedForm(aBoundaryRejection). A text part for aFilefield, a file for a text field, or two files for a singleFileis a400StructuralMismatchnamingbody.<field>; the text parts’ refinements are checked as for a form.- Unchosen file inputs. A browser sends an unchosen
<input type="file">as a part withfilename=""and no bytes. It is no file: anOption[File]isNone, aListgets no element, and a requiredFileis missing. - A
Filestays in the handler. It is refused anywhere it would be persisted or sent: an agent’s store, a handler’s parameter or result, an event, a call argument (bynk.types.file_at_boundary). A record field or the@multipartbody itself may name one, which is how a body names its parts. Take thebytesand store those. maxPartsbounds what the handler sees, not the cost of parsing: the whole capped body is parsed first, somaxBodyis the guard against an oversized upload.- Where the bytes go. The handler holds the file as
Bytes, soR2.put(key, f.bytes)stores it. A body over the cap is refused, so a large upload does not fit here. A multipart part is not streamed; a large upload is aPUTof the file itself to a streamed@rawroute. - Signatures and sums. A
Signatureactor signs the octets as received; abythat is a sum of actors is refused.
In a system test a typed body is sent as the FormData its record encodes to, with a
Blob per File; a Wire(…) body is the multipart text itself, framed with the boundary
bynk-boundary (--bynk-boundary\r\n…).
A from http service is same-origin by default: a browser page served from a
different origin cannot read its responses. A cross-origin preflight is answered
(a plain OPTIONS gets 204 + Allow — see Method semantics),
but without the Access-Control-* grant, so the browser still blocks the read.
To make a service cross-origin callable, declare a cors { } policy in header
position — before the routes:
service api from http { cors { origins: ["https://app.example.com"], credentials: false, maxAge: 1.hour, }
on GET("/items/:id") (id: Slug) -> Effect[HttpResult[Item]] by v: Visitor given Kv { … }}From this the compiler synthesises, for that service:
- an
OPTIONSpreflight against any of its route paths — answered with204and theAccess-Control-*headers, before theby/Bearer auth seam (a preflight is credential-less by spec, so it must not be rejected by an actor check); and Access-Control-Allow-Origin(plusVary: Origin) stamped onto every response of the service — uniformly across theOk/Raw/redirect/error/stream variants.
Fields
Section titled “Fields”| Field | Meaning | Default |
|---|---|---|
origins | The allowed origins, as string literals — an exact allowlist (["https://a.com", "https://b.com"]), or the wildcard ["*"]. Required. | — |
headers | The Access-Control-Allow-Headers a preflight advertises. | content-type (plus authorization when the service has a Bearer route) |
credentials | Whether credentialed requests (cookies / Authorization) are allowed — sends Access-Control-Allow-Credentials: true. | false |
maxAge | How long a browser may cache the preflight, as a Duration — sent as Access-Control-Max-Age seconds. | omitted (browser default) |
Access-Control-Allow-Methods is not a field: it is derived from the service’s
routes (their methods, plus HEAD where GET is present, plus OPTIONS) — the same
derivation that drives the Allow header — so it can never drift
from what the service actually serves.
Origin matching
Section titled “Origin matching”An exact allowlist compares the request’s Origin and reflects the matched value
(it never echoes an unvalidated origin), adding Vary: Origin so a shared cache does
not serve one origin’s grant to another. A request from an origin not on the list gets
no Access-Control-Allow-Origin — the browser blocks the read (fail closed). The
wildcard ["*"] answers every origin with a literal * (and needs no Vary).
The credentials + wildcard rule
Section titled “The credentials + wildcard rule”The Fetch spec forbids Access-Control-Allow-Credentials: true alongside a wildcard
origin — a browser rejects that combination at runtime. Bynk catches it at compile
time (bynk.http.cors_wildcard_credentials): with credentials: true, list the exact
origins instead of ["*"].
A cors { } block is only valid on a from http service
(bynk.http.cors_not_http), and a service declares at most one.
Security headers
Section titled “Security headers”A from http service is secure by default: X-Content-Type-Options: nosniff
is stamped on every response, with no opt-in required. That header stops a
browser MIME-sniffing a JSON or text body into HTML and executing it — a real
attack on an API that serves data, and one with no downside, so it is on by
default. The one header with a genuine footgun — HSTS — is a deliberate opt-in.
Declare a security { } policy in header position, beside cors { }:
service api from http { security { hsts: 180.days, // opt-in; omit to send no HSTS nosniff: true, // the default — set false only to opt out }
on GET("/orders/:id") (id: Slug) -> Effect[HttpResult[Order]] by v: Visitor given Kv { … }}The wire behaviour, for any response of the service:
security block | Response security headers |
|---|---|
| none (no block) | X-Content-Type-Options: nosniff |
security { hsts: 180.days } | nosniff + Strict-Transport-Security: max-age=15552000 |
security { nosniff: false } | (none — explicitly opted out) |
Fields
Section titled “Fields”| Field | Meaning | Default |
|---|---|---|
nosniff | Stamp X-Content-Type-Options: nosniff. Set false to opt out entirely. | true (on) |
hsts | Opt in to Strict-Transport-Security, as a Duration — sent as max-age seconds. Omit to send no HSTS. | omitted |
The headers are stamped uniformly across every response family — the Ok/Raw/
redirect/error/stream variants, and the synthesised preflight, 405/OPTIONS,
and 304 — composing with the CORS headers (the two sets are disjoint). A header
already set at the edge (e.g. by Cloudflare) is overwritten with the policy’s
value.
HSTS is opt-in for a reason
Section titled “HSTS is opt-in for a reason”Strict-Transport-Security pins a browser to HTTPS for its whole max-age,
cached and hard to undo. That breaks a custom domain served over plain HTTP in
dev or staging, and on a platform like Cloudflare TLS/HSTS is frequently owned at
the edge — so Bynk never enables it for you. hsts emits max-age only;
includeSubDomains and preload are each their own pinning footgun and are not
offered. A team terminating HSTS at the edge simply omits hsts and keeps the
default nosniff.
What is deliberately excluded
Section titled “What is deliberately excluded”Content-Security-Policy and X-Frame-Options constrain the script and framing
of markup. Bynk serves bytes and a content-type; it does not template HTML, so
those headers would have nothing here to govern — shipping them would be
cargo-cult. If Bynk ever serves HTML, the security { } section is where they
would land.
A security { } block is only valid on a from http service
(bynk.http.security_not_http), and a service declares at most one.
Request body limits
Section titled “Request body limits”A body-taking route (POST/PUT/PATCH) with an unbounded body is a
denial-of-service surface: a client can stream an arbitrarily large payload the
service reads into memory before it can reject it. Declare a byte ceiling and
Bynk rejects an oversized request with a synthesised 413 PayloadTooLarge
before the body is read — the same “reject at the boundary” posture as the
method-semantics 405.
Set a service-wide default with a limits { } section in header position,
beside cors { } and security { }; override it per route with a @limit
handler annotation, written immediately before on (the @cache placement):
service uploads from http { limits { maxBody: 1_048_576, // 1 MiB — the default for every body-taking route }
@limit(maxBody: 26_214_400) // 25 MiB — this one route accepts a larger payload on POST("/files") (body: FileMeta) -> Effect[HttpResult[Slug]] by u: Uploader given Kv { … }
on PATCH("/files/:code") (code: Slug, body: FilePatch) -> Effect[HttpResult[()]] by u: Uploader given Kv { … }}Here POST /files caps at 25 MiB (its @limit wins), and PATCH /files/:code
falls back to the service default of 1 MiB.
The wire behaviour, for a body-taking route with an effective cap of N bytes:
| Request | Answer |
|---|---|
Content-Length ≤ N | the body is read and validated, then the handler runs |
Content-Length > N | 413 PayloadTooLarge — { kind: "PayloadTooLarge", details: "…" }, the body never read |
no Content-Length, or one that understates the body, and more than N bytes arrive | 413 PayloadTooLarge — the read is cut off at N bytes |
| a route with no effective cap | the body is read as before — byte-for-byte unchanged output |
The limits { } section
Section titled “The limits { } section”| Field | Meaning | Default |
|---|---|---|
maxBody | The byte ceiling for the service’s body-taking routes, as a positive Int byte count. | — (no cap) |
maxFile | The byte ceiling for one file part of a @multipart body. | the body cap |
maxParts | The most parts one @multipart request may carry. | 64 |
The @limit override
Section titled “The @limit override”@limit(maxBody: <Int>) overrides the service default for one route. It is legal
only on a POST/PUT/PATCH handler — a body-taking method — and at most
once per handler:
| Field | Meaning | Default |
|---|---|---|
maxBody | The byte ceiling for this route, as a positive Int byte count, or an Int constant bound to one. Overrides limits { maxBody }. | — |
maxFile, maxParts | A @multipart route’s own file and part-count caps; override limits { maxFile } and limits { maxParts }. | — |
Precedence and the opt-in posture
Section titled “Precedence and the opt-in posture”The effective cap is route @limit first, then service limits, then none:
- a route with a
@limituses it (its cap wins over the service default); - otherwise a body-taking route uses the service
limits { maxBody }; - with neither, the route has no cap — it reads the body as before and emits byte-for-byte unchanged output.
Body limits are therefore opt-in (the CORS posture, not the security
default-on posture): a service that declares no limits { } and no @limit is
unaffected. The synthesised 413 is stamped with the CORS and security headers
(via the same applyCors/applySecurityHeaders pass as every other response), so
a cross-origin caller can read it.
maxBody is a byte count
Section titled “maxBody is a byte count”maxBody is a positive Int byte count — 1_048_576 for 1 MiB,
26_214_400 for 25 MiB (the digit separators
are just visual grouping). There is no byte Size literal (1.mb) yet — a
Size literal, mirroring the Duration playbook, is a named follow-on.
The bytes read are counted
Section titled “The bytes read are counted”Enforcement has two parts. The request’s Content-Length header is checked
first, and an oversized declared length is rejected before any body read. Because
Content-Length can be absent (a chunked transfer) or wrong, a capped
body is also read through a counting reader: the bytes actually arriving are
counted against the cap, and the read is cancelled with the same 413 as soon as
it crosses it. So a chunked upload, or one whose Content-Length understates it,
cannot get past the cap. The counted read covers a JSON body as well as a
@raw one, and a multi-actor route too (there the 413
comes from the route’s wrapper, as a returned PayloadTooLarge). In a system
test the 413 is observable as Rejected(PayloadTooLarge(_)).
A limits { } block is only valid on a from http service
(bynk.http.limits_not_http), and a service declares at most one
(bynk.parse.duplicate_limits). The section’s fields are maxBody, maxFile and maxParts
(bynk.http.limits_unknown_field), each of which MUST be a positive Int
(bynk.http.limits_invalid_field). A @limit on a GET/DELETE (a bodyless
method) is bynk.http.limit_on_bodyless; a duplicate @limit on one handler is
bynk.http.limit_duplicate; any argument other than maxBody is
bynk.http.limit_unknown_arg; and a non-positive-Int maxBody is
bynk.http.limit_bad_max_body.
JSON for non-Bynk clients
Section titled “JSON for non-Bynk clients”An Ok(value) body is the value as JSON, with an Option[U] written in Bynk’s
tagged form, {"kind": "Some", "value": v} or {"kind": "None"}. A Bynk
client reads that, but curl | jq, a dashboard and a script expect "action": "opened" and "deleted": null. A service (or one route) can opt its response
bodies into plain JSON:
context api
type Activity = { id: String, action: Option[String], deleted: Option[Bool],}
service api from http { json { options: plain, }
on GET("/activity") () -> Effect[HttpResult[List[Activity]]] by r: Visitor { Ok([Activity { id: "a1", action: Some("opened"), deleted: None }]) }}[{"id":"a1","action":"opened","deleted":null}]In plain mode Some(v) is written v and None is written null (the key is
never dropped), at any depth: in a record, a List, a Map value, a sum
variant’s field. Everything else about the encoding is unchanged. Bynk’s own
decoder reads the plain shape too (ADR 0450),
so a Bynk client still reads the response.
The json { } section and @json
Section titled “The json { } section and @json”| Form | Notes | |
|---|---|---|
| Service default | json { options: plain } | header position, after cors, security and limits; at most once (bynk.parse.duplicate_json); from http only (bynk.http.json_not_http) |
| Per-route override | @json(options: tagged) or @json(options: plain) | on an on http handler; at most once (bynk.http.json_duplicate) |
options is the only field, and its value is tagged (the default) or plain
(bynk.http.json_unknown_field, bynk.http.json_invalid_field,
bynk.http.json_unknown_arg, bynk.http.json_invalid_arg). The effective mode is
the route’s @json, then the service’s json { }, then tagged. A service
with neither is byte-for-byte unchanged.
An Option whose payload can be null is refused
Section titled “An Option whose payload can be null is refused”Plain mode must read back what it wrote. If an Option[U]’s payload U can
itself be null, Some(U) and None would both be written null. So an
Option[Option[T]] or an Option[()] anywhere in a plain route’s response
type, through records, sums, containers and generic arguments, is
bynk.json.nested_option_plain, and the message names the payload. Model the
distinction as a sum type, or keep that route tagged.
What plain mode does not change
Section titled “What plain mode does not change”Only Ok, Created and Accepted response bodies of from http routes.
Request bodies already decode plain JSON. Cross-context calls, queue messages,
agent state, Json.encode, Streaming (SSE) events and the contract hashes keep
the tagged form (Json.encodePlain is the plain form outside a route), so decode(encode(x)) == x still holds everywhere else.
bynk openapi describes a plain route’s Option as nullable, and bynk client
generates it as T | null (see the HTTP client).
Request lifecycle
Section titled “Request lifecycle”Validation happens once, at the edge; the handler only ever sees valid input.
Text equivalent: the Worker’s fetch entry point (index.ts) routes the request
on method and path. On a match, path parameters are bound and a typed
query is decoded and validated (400 on a violation); a @raw, @form or @multipart route’s
Content-Type is checked (415) and a capped route’s Content-Length (413);
then any body is read under its cap, parsed and validated against its refined type — an invalid body is rejected with
400 at the boundary, before the handler runs. The handler then runs as an
Effect and returns an HttpResult[T], which is mapped to an HTTP status and
JSON body per the table above. When no route matches, a request to a live path
under an unhandled method is a 405 + Allow (or 204 + Allow for a plain
OPTIONS); only a request to a path that exists under no method is a 404
(see Method semantics). A service with
on rejected renders the router’s 400, 405, 413 and
415 bodies itself, keeping their status.
Example
Section titled “Example”context notes
service api from http { on GET("/ping") () -> Effect[HttpResult[String]] by Visitor { Ok("pong") }
on GET("/notes/:id") (id: String) -> Effect[HttpResult[String]] by Visitor { NotFound }}Emission
Section titled “Emission”from http services compile to a runnable Cloudflare Worker on the --target workers target (index.ts router, handlers.ts, compose.ts,
wrangler.toml). See emission and
Target Cloudflare Workers.