Files
unkernel/js/node/net.ts
Pooya Parsa d80c033106 net: drop the default Wisp server from the SDK
The SDK named the project's Wisp server (DEFAULT_WISP_SERVERS), and the
Node entry sent Origin: http://localhost to pass that server's allow
list (NODE_WISP_HEADERS). The server accepts only the page's origins,
so both are gone: wispBackend() takes the caller's server, and Node
sends handshake headers only when `headers` asks for them. The page
keeps wss://wisp.unkernel.dev:6680/ as its own default (web/src/net.ts).

The live Wisp test takes its server from UNKERNEL_TEST_WISP.
2026-11-27 22:24:02 +00:00

58 lines
1.8 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// NetStack backend for Node: real TCP sockets and the host resolver.
//
// The guest reaches whatever this machine can reach, as this process. Use
// it with `new NetStack({ backend: directBackend(), hostLoopback: true })`
// to also let TCP to the gateway (10.0.2.2) reach 127.0.0.1.
import net from "node:net";
import dns from "node:dns";
import type { NetBackend } from "../core/net.ts";
const CONNECT_TIMEOUT_MS = 20000;
export function directBackend(): NetBackend {
const sockets = new Set<net.Socket>();
return {
connect({ host, port }, ev) {
const s = net.connect({ host, port, allowHalfOpen: true, noDelay: true });
sockets.add(s);
let closed = false;
const timer = setTimeout(() => s.destroy(new Error("connect timeout")), CONNECT_TIMEOUT_MS);
s.on("connect", () => { clearTimeout(timer); ev.onConnect(); });
s.on("data", (b: Buffer) => ev.onData(new Uint8Array(b.buffer, b.byteOffset, b.byteLength)));
s.on("end", () => ev.onEnd());
s.on("drain", () => ev.onDrain());
s.on("error", () => {});
s.on("close", () => {
clearTimeout(timer);
sockets.delete(s);
if (closed) return;
closed = true;
ev.onClose(s.errored ?? null);
});
return {
write: (u8) => { s.write(u8); },
end: () => { s.end(); },
destroy: () => { closed = true; s.destroy(); },
pause: () => { s.pause(); },
resume: () => { s.resume(); },
get buffered() { return s.writableLength; },
};
},
lookup(name) {
return new Promise((resolve, reject) => {
dns.lookup(name, { family: 4, all: true }, (err, addrs) => {
if (err) reject(err);
else resolve(addrs.map((a) => a.address));
});
});
},
/** Destroy every open socket. */
close() {
for (const s of sockets) s.destroy();
sockets.clear();
},
};
}