@nifrajs/node-uws
v5.17.0
Published
Serve a nifra app on uWebSockets.js from Node - the node-direct lane over uWS's native HTTP server.
Maintainers
Readme
@nifrajs/node-uws
Serve a nifra app on uWebSockets.js
from Node. It is @nifrajs/node with uWS's native HTTP server in place of node:http: requests reach
the app through the same node-direct lane, and answers are written with uWS's corked, copy-once API.
import { server } from "@nifrajs/core/server"
import { serve } from "@nifrajs/node-uws"
const app = server().get("/users/:id", (c) => ({ id: c.params.id }))
const handle = await serve(app, { port: 3000 })
// handle.port the bound port (useful with `port: 0`)
// await handle.stop() stop accepting, drain in-flight requests, close the restInstall
Install this package with its core and Node adapter dependencies:
npm install @nifrajs/node-uws @nifrajs/node @nifrajs/coreuWebSockets.js is not published to npm. Install it from GitHub, next to this package:
npm install uNetworking/uWebSockets.js#v20.71.0Its releases ship prebuilt binaries for Node 22, 24 and 26 on macOS and Linux (x64, arm64) and Windows
(x64). There is none for Alpine (musl) or Windows on arm64, and a new Node major has none until upstream
publishes one. The Node 24 binary for Linux arm64 needs glibc 2.38 or newer, so Debian 12 and Ubuntu 22.04
cannot load it. Importing this package succeeds either way; serve() loads the addon and throws an error
that says so when it cannot load. Use @nifrajs/node where that matters.
Options
| Option | |
|---|---|
| port, hostname | As in @nifrajs/node. |
| protocol | "http" or "https": the scheme Request.url carries. "https" by default when tls is set. |
| tls | { keyFileName, certFileName, passphrase?, caFileName?, dhParamsFileName?, sslCiphers?, sslPreferLowMemoryUsage? }. uWS reads the files itself, so these are paths, not PEM text. |
| allowedHosts, canonicalHost, normalizeTarget | Same rules and defaults as @nifrajs/node. |
| gracefulSignals | Call stop() on SIGTERM/SIGINT. Off by default. |
static and fastResponse are refused with a TypeError: serve files from a route or a static host in
front.
stop({ drainMs, delayMs }), await using, and the app's onStopping / onStop hooks behave as they
do on @nifrajs/node.
How it differs from @nifrajs/node
- Fixed replies skip the app. A
staticResponse()GET route (no path parameter, no schema, auth or other route-level stage) is compiled into uWS and answered without calling into JavaScript, and a fixed POST is answered from uWS's callback without building a request, as long as the app has no request or response hook, deadline, capacity gate, response headers or Host policy (allowedHosts,canonicalHost,normalizeTarget). A POST carrying anOriginheader goes to the ordinary path, which runs the cross-site form check; one without cannot fail it. AnechoParam()route (whose only parameter is its last segment, and which no other route can overlap) is answered the same way, from the path as sent. A malformedHostis screened on every other route and not on these; a malformed percent-escape in an echoed segment is echoed as sent, where the other runtimes answer 400. Serving seals the app, aslisten()does: a route or hook added afterwards throwsSERVER_SEALED. - No WebSockets. A handshake is refused with
501. Use@nifrajs/nodefor apps withwsroutes. - Streaming responses (SSE, streamed SSR) are written chunked with back-pressure; a body whose
Content-Lengthis declared is sent with that length and cut off, not finished, if the stream ends short or runs long. - Headers uWS frames itself -
Content-Length,Transfer-EncodingandDate- are dropped from the app's response. TheuWebSockets: 20banner uWS would add is switched off for the thread that loads the adapter; a worker thread switches off its own by loading it. - Header values are checked for CR, LF and other control characters before anything is written, and
a status outside 100-999 is refused: either is a
500, reported through the app's logger. - uWS's parser, not Node's. It rejects a repeated
Host, a request carrying bothContent-LengthandTransfer-Encoding, and an absolute-form target with400, before the app sees them. Its idle timeout is fixed at 10 seconds, and there is no equivalent of Node'srequestTimeout. - Request bodies are pushed by uWS, not pulled. The socket is throttled once 1 MiB is buffered
unread; a body larger than the route's cap is refused with
413as on@nifrajs/node. A handler that answers without reading the body releases the throttle first and the rest of the upload is dropped, so the next request on that connection is read; reading the body afterwards rejects. - An empty header value reads as absent through
c.header().
Notes for contributors
The shared pieces - the outcome runtime, Host and target policy, lifecycle and error hooks - come from
@nifrajs/node/transport. uWS objects are valid only while their callback runs, so the request copies
its headers out the moment an app goes asynchronous, and a response is never touched after it ended or
aborted: uWS throws on that, and an uncaught throw there ends the process.
Tests run on Node (bun run test:node), because uWS does not load under Bun.
For AI agents
Start with LLM.md - this package's contract card (the exports you call + its footguns),
one cheap read instead of the whole corpus. For the wider framework: the repo's
AGENTS.md is the copy-paste quick reference, and
llms-full.txt is the full machine-readable corpus. Run nifra check as the
done-gate, or nifra mcp to give the agent live project tools.
