JSR publishes `src/` and runs no bundler, so the `createRequire` banner tsup gives
the ESM build (tsup.config.ts) never exists there. Every lazy `require(...)` in the
source had nothing to resolve, and one of them was unguarded: `@Html('file')` and
template mode died at boot on Deno with `ReferenceError: require is not defined`.
The two guarded sites were worse than the crash, because they answered confidently
and wrongly. `static` reported *"needs a filesystem and is unavailable on this
runtime (edge)"* while running on Deno, which has one; multipart reported busboy as
not installed while it sat in node_modules. Both blamed the runtime for a packaging
problem, and both named a runtime the reader was not on.
Route all ten call sites through one `nodeRequire`, which prefers the ambient
`require` — so the CJS build and the banner-carrying ESM build behave exactly as
before — and otherwise rebuilds one from `process.getBuiltinModule('node:module')`.
Node 22.3+, Deno and Bun all expose that synchronously, which is the requirement:
every caller sits on a sync path a dynamic `import()` could not serve. It resolves
from `process.cwd()`, since a peer dependency like busboy belongs to the application,
not to a module served over https from a registry.
Where neither exists — workerd, where esbuild rewrites the call into its own throwing
stub — the behaviour is unchanged: the guarded sites still catch, and their "edge has
no filesystem" story is finally the true one.
Covered by a Deno suite that imports `src/` exactly as JSR serves it. The other Deno
tests already did that; none of them touched a path that loads a Node builtin lazily,
which is how a package that died at boot kept a green suite.
Closes #69.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
126 lines
4.0 KiB
TypeScript
126 lines
4.0 KiB
TypeScript
import type http from 'http';
|
|
import { channel } from '../channel';
|
|
import type { Bus } from '../bus';
|
|
import { runWsConnection, matchWsRoute, trackUntil, type WsSocket, type WsRequest } from './ws-core';
|
|
import type { WsRouteDef, MeshControl } from './types';
|
|
import { nodeRequire } from '../node-require';
|
|
|
|
/** Lazily loads the optional `ws` peer dependency's `WebSocketServer`, or null when it is not installed. */
|
|
function loadWss(): any | null {
|
|
try {
|
|
// `ws` is an OPTIONAL peer dependency: lazy-required so installs without WebSocket
|
|
// support don't fail. When absent this throws and we fall back to null (501 on upgrade).
|
|
return nodeRequire<{ WebSocketServer: unknown }>('ws').WebSocketServer;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Writes a 501 Not Implemented status to the raw socket and destroys it. */
|
|
function reject501(socket: any): void {
|
|
socket.write('HTTP/1.1 501 Not Implemented\r\n\r\n');
|
|
socket.destroy();
|
|
}
|
|
|
|
/** Handles the mesh-control upgrade path; returns true when the request was for the mesh control endpoint. */
|
|
function handleMeshUpgrade(
|
|
wss: any,
|
|
meshControl: MeshControl,
|
|
path: string,
|
|
req: http.IncomingMessage,
|
|
socket: any,
|
|
head: Buffer,
|
|
streams?: Set<() => void>,
|
|
): boolean {
|
|
if (path !== meshControl.path) return false;
|
|
|
|
if (!wss) {
|
|
reject501(socket);
|
|
return true;
|
|
}
|
|
|
|
// nodeSocket, not the raw ws: the control channel is the last consumer that spoke the
|
|
// `ws` EventEmitter API directly. It now takes the same neutral socket every route does.
|
|
// Tracked like any other long-lived socket: a connected teacup has no reason to hang up, so
|
|
// an untracked control channel leaves the teapot's server.close() waiting on it forever.
|
|
// serveFrames swallows its own failures, so the floating promise cannot reject.
|
|
wss.handleUpgrade(req, socket, head, (ws: any) => {
|
|
const neutral = nodeSocket(ws);
|
|
|
|
void trackUntil(neutral, meshControl.handle(neutral), streams);
|
|
});
|
|
return true;
|
|
}
|
|
|
|
/** Builds the neutral WsRequest from a Node upgrade request. Same values the old open() read from req.socket. */
|
|
function nodeWsRequest(req: http.IncomingMessage): WsRequest {
|
|
return {
|
|
url: req.url ?? '/',
|
|
headers: req.headers,
|
|
protocol: (req.socket as any).encrypted ? 'https' : 'http',
|
|
ip: req.socket.remoteAddress ?? '',
|
|
};
|
|
}
|
|
|
|
/** Wraps a `ws` socket as a neutral WsSocket: inbound channel + abort on close + send/close/terminate. */
|
|
function nodeSocket(ws: any): WsSocket {
|
|
const inbound = channel<unknown>();
|
|
const ac = new AbortController();
|
|
ws.on('message', (data: Buffer) => inbound.push(data.toString()));
|
|
ws.on('close', () => {
|
|
inbound.close();
|
|
ac.abort();
|
|
});
|
|
ws.on('error', (err: unknown) => inbound.fail(err));
|
|
return {
|
|
inbound,
|
|
abort: ac.signal,
|
|
get isOpen() {
|
|
return ws.readyState === 1; // 1 = OPEN
|
|
},
|
|
send: (data) => ws.send(data),
|
|
close: (code, reason) => {
|
|
try {
|
|
ws.close(code, reason);
|
|
} catch {
|
|
/* already closed */
|
|
}
|
|
},
|
|
terminate: () => (ws.terminate ?? ws.close).call(ws),
|
|
};
|
|
}
|
|
|
|
/** Attaches WebSocket upgrade handling to the server: routes to `wsRoutes` or a mesh control path. */
|
|
export function attachWs(
|
|
server: http.Server,
|
|
wsRoutes: WsRouteDef[],
|
|
bus?: Bus,
|
|
meshControl?: MeshControl,
|
|
streams?: Set<() => void>,
|
|
): void {
|
|
if (wsRoutes.length === 0 && !meshControl) return;
|
|
const WebSocketServer = loadWss();
|
|
const wss = WebSocketServer ? new WebSocketServer({ noServer: true }) : null;
|
|
server.on('upgrade', (req, socket, head) => {
|
|
const path = (req.url ?? '/').split('?')[0];
|
|
|
|
if (meshControl && handleMeshUpgrade(wss, meshControl, path, req, socket, head, streams)) return;
|
|
|
|
const route = matchWsRoute(wsRoutes, path);
|
|
|
|
if (!route) {
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
|
|
if (!wss) {
|
|
reject501(socket);
|
|
return;
|
|
}
|
|
|
|
wss.handleUpgrade(req, socket, head, (ws: any) => {
|
|
void runWsConnection(nodeSocket(ws), nodeWsRequest(req), route, bus, streams);
|
|
});
|
|
});
|
|
}
|