Skip to content

Commit 19b33f9

Browse files
committed
fix(node): omit default port/host when node.path is set
Resolves #315
1 parent f74d220 commit 19b33f9

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎src/adapters/node.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,20 @@ class NodeServer implements Server {
126126
this.waitUntil = this.#wait.waitUntil;
127127

128128
const tls = resolveTLSOptions(this.options);
129-
const { port, hostname: host } = resolvePortAndHost(this.options);
129+
// `node.path` (unix socket / named pipe), `handle` or `fd` pick the listen
130+
// target themselves. Node prefers `port` over `path` when both are given,
131+
// so leave the `port`/`host` keys out entirely (`port: undefined` still
132+
// means a random port to Node).
133+
const nodeListen = this.options.node as { path?: string; handle?: unknown; fd?: number };
134+
const hasListenTarget =
135+
nodeListen?.path != null || nodeListen?.handle != null || nodeListen?.fd != null;
136+
let portAndHost: { port: number; host: string | undefined } | undefined;
137+
if (!hasListenTarget) {
138+
const { port, hostname: host } = resolvePortAndHost(this.options);
139+
portAndHost = { port, host };
140+
}
130141
this.serveOptions = {
131-
port,
132-
host,
142+
...portAndHost,
133143
exclusive: !this.options.reusePort,
134144
// Enable SO_REUSEPORT for cross-process port sharing (Node >= 22.12).
135145
// Older Node versions ignore this option; unsupported platforms throw.

‎test/node.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { connect as tlsConnect } from "node:tls";
2+
import { mkdtemp, rm } from "node:fs/promises";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
25
import { describe, beforeAll, afterAll, expect, test } from "vitest";
36
import { fetch, Agent } from "undici";
47
import { addTests } from "./_tests.ts";
@@ -141,6 +144,30 @@ describe.skipIf(isDeno || isBun)(`${runtime} (absolute-form over TLS)`, () => {
141144
});
142145
});
143146

147+
// `node.path` must win over the default `port` (https://github.com/h3js/srvx/issues/315)
148+
describe.skipIf(isDeno || isBun || process.platform === "win32")(`${runtime} (unix socket)`, () => {
149+
test("listens on node.path instead of the default port", async () => {
150+
const dir = await mkdtemp(join(tmpdir(), "srvx-"));
151+
const path = join(dir, "app.sock");
152+
const server = serve({
153+
node: { path },
154+
fetch: () => new Response("ok"),
155+
});
156+
try {
157+
await server.ready();
158+
expect(server.url).toBe(path);
159+
expect(server.node!.server!.address()).toBe(path);
160+
const body = await fetch("http://localhost/", {
161+
dispatcher: new Agent({ connect: { socketPath: path } }),
162+
}).then((r) => r.text());
163+
expect(body).toBe("ok");
164+
} finally {
165+
await server.close(true);
166+
await rm(dir, { recursive: true, force: true });
167+
}
168+
});
169+
});
170+
144171
function getHttpClient(h2?: boolean) {
145172
if (!h2) {
146173
return {

0 commit comments

Comments
 (0)