The wire format
A call is POST {base}/_dusk/fn/<id> with headers content-type: application/json and x-dusk-fn: 1.
A call is POST {base}/_dusk/fn/<id> with headers content-type: application/json and x-dusk-fn: 1. The
body is serialize([input]) from @shadow-js/web/server: an array of exactly one element (more is a 400). The
answer is always content-type: application/json; charset=utf-8, cache-control: no-store and
x-content-type-options: nosniff, and its body is serialize(envelope) with one of three shapes:
{ ok: true, value }
{ ok: false, error: { name, message, status, requestId?, issues?, data? } }
{ redirect: { to, status } }
serialize and deserialize are also the codec of the hydration data a page sends. They carry undefined, NaN, infinities,
-0, bigint, Date, URL, Map, Set, ArrayBuffer, typed arrays and DataView, plain objects and arrays,
and shared references and cycles. Functions, symbols and class instances throw a TypeError: in the browser before
the request is sent, on the server while the answer is built (which answers 500). A server function returns data,
not objects with behavior.
import { serverFn } from "@shadow-js/dusk/server";
export const echo = serverFn({ method: "GET" }, async (input) => input);
class Money {
cents = 500;
}
export const instance = serverFn({ method: "GET" }, async () => new Money());
export const huge = serverFn({ method: "GET" }, async () => "x".repeat(9 * 1024 * 1024));