@tinkered/bun
The parts of a Tinker app that run on Bun
(ADR 0130).
@tinkered/bun: serve, Bun.serve as a
capsule; run, the host; and
ws.server, rpc over one WebSocket.
@tinkered/bun/client: ws.client, the
browser half of that socket.
@tinkered/bun/sql: bunSql, Postgres on
Bun.SQL, with a pool.
@tinkered/bun/sqlite: bunSqlite, SQLite
on bun:sqlite.
Install
bun add @tinkered/core @tinkered/bun
Each release also attaches tarballs to its
GitHub release; the
changelog lists them.
Serve and run
serve is a capsule: Bun.serve for the
life of the scope (ADR 0132). The cmd gives
it its values; it reads no process.env.
const socket = ws.server({
path: "/rpc",
services: [api],
});
export default defineMain(
({ env, signal, stdout }) => {
// `routes` is the template's request table.
const table = routes({
stop: signal,
table: { "GET /api/health": health },
});
return createScope({
signal,
capsules: [
serve({
port: Number(env.PORT ?? 4318),
pages: { "/*": page },
websocket: socket,
handlers: [table],
stdout,
}),
api({ implement }),
socket,
table,
],
});
},
);
serve goes first: its start wraps the
rest, so it listens once all started.
- On close it stops the port first.
pages: static files, Bun's own routes.
Every path no handler takes gets them.
websocket: the one handler with Bun's
websocket (Bun takes one per server).
handlers: asked in order; the first
Response wins.
/api/*, /rpc and /rpc/* go to the
handlers; paths adds more.
- Each handler reads a body up to its own
maxBody, 1 MiB when unset. Past it,
serve answers 413.
- A handler's reply that leaves the body
unread closes the connection: the client
may still be sending it. So do
serve's
own 404 and 413. A body read to its end
keeps the connection open.
- A handler's
request.signal aborts when
the client leaves, body or not.
server.requestIP(request) and
server.timeout(request, s) work in a
handler, body or not.
A handler is a capsule whose start returns
Bun's own shape, { fetch, websocket? }.
fetch gives a Response, null (it
upgraded the request), or undefined (not
its own: the next handler gets it).
run runs one cmd. It owns the signals and
the exit code.
import { run } from "@tinkered/bun";
import main from "./server";
await run(main);
- A config error at start exits 78.
- Any other throw at start exits 1.
- A failed root exits 1.
- A signal exits 128 plus its number.
The socket
The server is a handler: give it to serve
as websocket.
const socket = ws.server({
path: "/rpc",
services: [api],
});
Its fetch upgrades the socket's path, then
takes a bytes POST. Each answer:
undefined: not this path;
null: upgraded, Bun owns the request;
- a
Response: refused (a foreign origin
gives 403), or the POST's reply.
A bytes POST is for a key with a
bytes field. A POST it refuses before it
reads the body closes the connection, on
serve or a bare Bun.serve.
Two services with one address fail the start
with DuplicateAddress. The fault names both:
two boards, each with a cell named "flows":
DuplicateAddress
address: "flows"
services: ["approvals.flows", "admin.flows"]
- The fix is a rename of one cell.
strict: false lets the first in the list
win, with a warning at start.
The client:
import { ws } from "@tinkered/bun/client";
const tab = await createScope({
capsules: [
ws.client({ url: "wss://host/rpc" }),
api({ remote: ws.client }),
],
}).ready;
Databases
Each gives a Drizzle connection.
drizzle-orm is an optional peer.
import { bunSql } from "@tinkered/bun/sql";
import { bunSqlite } from "@tinkered/bun/sqlite";
capsules: [
bunSql({ url: new URL(env.DATABASE_URL) }),
drizzlePg({ driver: bunSql.connection }),
];
bunSql: url (secret), max (10).
bunSqlite: file (:memory:).
- The file is in WAL mode, with foreign
keys on.
begin takes the write lock at once.
- Each closes with the scope.
Guards
- The Origin check at upgrade.
- A frame over
maxPayload, or of more than 32
messages, closes the socket.
- More than
maxWatches live watches answers 429.
- More than
maxUploads bytes POSTs at once
answers 503 with Retry-After.
- A socket quiet for 60 s closes.