Node.js HTTP driver for Tupik. It serves any app.fetch over node:http, streams request and response
bodies, enforces a body limit (413), takes a fast path that skips building a native Request
for body-less requests and upgrades WebSocket connections natively (RFC 6455, no dependency).
npm install @tupik/node @tupik/coreNode.js 22.12 or later.
import { once } from 'node:events';
import { createApp, createModule } from '@tupik/core';
import { serve } from '@tupik/node';
const app = createApp().register(
createModule().get('/health', { access: 'public', handler: (ctx) => ctx.json({ ok: true }) }),
);
const server = serve(app, { port: 3000 }); // hostname defaults to 127.0.0.1
await once(server, 'listening');
const response = await fetch('http://127.0.0.1:3000/health');
console.log(response.status, await response.json()); // 200 { ok: true }
server.close();The upgrade request goes through the app like any request (hooks, middleware, access, validation). The route
handler accepts it with upgradeWebSocket; an anonymous client gets its 401 before the handshake.
import { createApp, createModule } from '@tupik/core';
import { serve, upgradeWebSocket } from '@tupik/node';
const live = createModule('', { access: 'authenticated' }).get('/ws', {
handler: (ctx) =>
upgradeWebSocket(
ctx,
{
open: (ws) => ws.send('hello ' + ctx.principal.id),
message: (ws, data) => ws.send(typeof data === 'string' ? data.toUpperCase() : data),
close: (ws, code, reason) => console.log('closed', code, reason),
},
{ maxPayload: 64 * 1024, origin: ['https://app.example.com'] },
),
});
serve(createApp({ authenticate: findUser }).register(live), { port: 3000 });- Text, binary, fragmented messages, ping/pong and the closing handshake; client frames must be masked.
maxPayload(default 1 MiB) is checked on each frame header and across fragments, before buffering: bigger messages close with 1009. Invalid UTF-8 closes with 1007, protocol errors with 1002, a throwing handler with 1011.idleTimeout(default 120 s, 0 disables) closes a silent connection with 1001;closeTimeout(default 5 s) drops a peer that never answers the close frame.origin(list or predicate) answers 403 to other origins;protocolsnegotiatesSec-WebSocket-Protocol.- A plain HTTP request to the route (or any runtime other than
@tupik/node) gets 426 Upgrade Required. server.close()sends 1001 to open sockets;server.closeAllConnections()drops them.- No
permessage-deflate. If you attach your own'upgrade'listener (for example thewslibrary), the driver steps aside;serve(app, { webSocket: false })turns the feature off. With your own server, calldispatchUpgrade(app, req, socket, head)from its'upgrade'event.
| Export | Purpose |
|---|---|
serve(app, { port?, hostname?, bodyLimit?, onError?, webSocket? }) |
Starts a node:http server; returns the Server |
serve(app, { workers: n | 'auto', reusePort?, shutdownTimeout? }) |
node:cluster workers (SO_REUSEPORT on Linux, Node ≥ 22.12); the primary gets a ServeCluster (ready, workers, close()) |
dispatch(app, req, res, options?) |
Handle one request inside your own server |
upgradeWebSocket(ctx, handlers, options?) |
Accept a WebSocket from a route handler (open, message, close, error) |
dispatchUpgrade(app, req, socket, head, options?) |
Run an 'upgrade' event of your own server through the app |
toWebRequest(req, options?) / sendResponse(res, response) |
Lower-level conversions |
PayloadTooLargeError |
Thrown when the body exceeds bodyLimit (default 1 MiB) |
MIT