WASM JavaScript API

Everything the WASM extension adds to pb_hooks and page code, generated from the TypeScript declarations it ships (your app's types.d.ts has the same, so your editor knows them). For how to use it, see its docs; for Go, the Go API.

$wasm

The app's modules, the pb_hooks/.pb.wasm files. A module is a closed box: it gets its input and gives its output, and reaches no database, file, network, environment or process.

Calls are async only: a module runs off the app's JS VM while the hook awaits it, so the app serves other requests meanwhile. $wasm.run(...) without .async throws.

The output is the module's stdout: JSON parsed, a string when it isn't JSON, null when it's empty.

$wasm.run

$wasm.run.async()

$wasm.run.async(name: string, input?: WasmInput): Promise<T>

Calls the module's "run". Rejects with a WasmError when the call fails.

Parameter Type
name string
input WasmInput (optional)

Returns Promise<T>.

onRecordCreate(async (e) => {
  const out = await $wasm.run.async("summary", { title: e.record.get("title"), body: e.record.get("body") })
  e.record.set("summary", out.text)
  return e.next()
}, "posts")

$wasm.call

$wasm.call.async()

$wasm.call.async(name: string, fn: string, input?: WasmInput): Promise<T>

Calls one of the module's functions (its first argument). Rejects with a WasmError when the call fails.

Parameter Type
name string
fn string
input WasmInput (optional)

Returns Promise<T>.

const r = await $wasm.call.async("crypto", "decrypt", { key, data })

$wasm.inspect

$wasm.inspect.async()

$wasm.inspect.async(name: string, fn: string, input?: WasmInput): Promise<WasmInspection<T>>

Calls a function to try a module out: never rejects for the call, and gives its log and timings.

Parameter Type
name string
fn string
input WasmInput (optional)

Returns Promise<WasmInspection<T>>.

$wasm.modules()

$wasm.modules(): Array<WasmModuleInfo>

Every module in pb_hooks/ and how it's doing.

Returns Array<WasmModuleInfo>.

$wasm.limits()

$wasm.limits(): WasmLimits

What every call runs within: memory, time, how many calls and how many MB of modules at once.

Returns WasmLimits.

Types

WasmInput

What a module gets on stdin: a string or bytes (a typed array or an ArrayBuffer) as they are, nothing for undefined or null, anything else as JSON.

type WasmInput = string | ArrayBuffer | Uint8Array | Array<number> | object | number | boolean | null | undefined

WasmExample

One call to try a module with, from its manifest (what it prints for <name> --describe).

Member Type
function string "run" when the manifest leaves it out.
input any A string is the module's stdin as it is (EDIFACT, XML, CSV); anything else is sent as JSON.
output? any What it gives, when the example says.

WasmModuleInfo

How one module in pb_hooks/ is doing, with what it says for --describe.

Member Type
name string
running boolean Its module checks out: it can be called.
description? string
version? string The author's version, as the manifest writes it.
functions? Array<string> The functions it says it has; ["run"] by default.
language? string What it's written in: what the module says, else what its manifest says.
repository? string Where its source is: an http(s) URL.
examples? Array<WasmExample>
size? number Its module's size in bytes: what a call of it takes of the app's maxModulesMB while it runs.
error? string Why it can't run (running false), or what's wrong with what it printed for --describe.

WasmLimits

What every module call in the app runs within.

Member Type
maxCallMemoryMB number Memory each call may allocate (heap, stack, input). Every call has its own, not shared.
timeoutMs number Time limit for a call's run (compiling comes before it).
maxConcurrent number Calls running at once.
maxModulesMB number MB of modules running at once: the sizes of the calls' modules, added up. With 10, two calls of a Go module (~4.4 MB) run at once, and maxConcurrent of a Rust or Zig one (~0.1 MB). Also the largest module that loads.
maxPayload number Largest input or output of a call, in bytes.

WasmErrorCode

The codes ext/wasm gives; a module can give its own (it exits non-zero with {"code", "message"} on stderr).

WasmError

What a failed $wasm.run.async or $wasm.call.async rejects with: catch it to decide what the hook does next.

Member Type
name "WasmError"
module string The module's name.
code WasmErrorCode
retryable boolean Whether the same call may work later (busy, timeout...).

WasmInspection

What $wasm.inspect.async gives: one call, with its log and its timings.

Member Type
output T | null The output; null when the call failed or the module wrote nothing.
stderr string What the module wrote to stderr: its log.
error { code: WasmErrorCode; message: string; retryable: boolean } | null Why the call failed, or null.
compileMs number
runMs number