-
Notifications
You must be signed in to change notification settings - Fork 4.4k
WebSocket client: support ws+unix:// and wss+unix:// #29203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
013cc23
Support ws+unix:// and wss+unix:// in WebSocket client
robobun 37d3462
[autofix.ci] apply automated fixes
autofix-ci[bot] 21821bb
Drop fragile stderr assertion in subprocess test
robobun 012e687
Fix misleading SNI comment for wss+unix://
robobun 95458e5
Ensure leading '/' on ws+unix request path
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| import { describe, test, expect } from "bun:test"; | ||
| import { bunEnv, bunExe, isWindows, tls as tlsCert } from "harness"; | ||
| import { join } from "node:path"; | ||
| import { tmpdir } from "node:os"; | ||
|
|
||
| // Unix domain sockets are not supported on Windows via ws+unix:// | ||
| // (uSockets uses AF_UNIX which has limited support there). | ||
| describe.skipIf(isWindows)("WebSocket over unix domain socket", () => { | ||
| function sockPath(name: string) { | ||
| // Keep it short to stay under sun_path limits on macOS/Linux. | ||
| return join(tmpdir(), `bun.ws.${name}.${process.pid}.${Date.now().toString(36)}.sock`); | ||
| } | ||
|
|
||
| test("ws+unix:// echoes through Bun.serve({ unix })", async () => { | ||
| const unix = sockPath("echo"); | ||
| await using server = Bun.serve({ | ||
| unix, | ||
| fetch(req, server) { | ||
| if (server.upgrade(req)) return; | ||
| return new Response("not a websocket", { status: 400 }); | ||
| }, | ||
| websocket: { | ||
| open(ws) { | ||
| ws.send("hello from server"); | ||
| }, | ||
| message(ws, message) { | ||
| ws.send(message); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const ws = new WebSocket(`ws+unix://${unix}`); | ||
| const received: string[] = []; | ||
| const { promise, resolve, reject } = Promise.withResolvers<void>(); | ||
|
|
||
| ws.onerror = e => reject(e); | ||
| ws.onopen = () => { | ||
| ws.send("ping over unix"); | ||
| }; | ||
| ws.onmessage = e => { | ||
| received.push(String(e.data)); | ||
| if (received.length === 2) { | ||
| ws.close(); | ||
| } | ||
| }; | ||
| ws.onclose = e => { | ||
| resolve(); | ||
| }; | ||
|
|
||
| await promise; | ||
|
|
||
| expect(received).toEqual(["hello from server", "ping over unix"]); | ||
| expect(ws.url).toBe(`ws+unix://${unix}`); | ||
| }); | ||
|
|
||
| test("ws+unix:// with ':path' after socket path", async () => { | ||
| const unix = sockPath("path"); | ||
| let seenUrl = ""; | ||
| let seenHost = ""; | ||
| await using server = Bun.serve({ | ||
| unix, | ||
| fetch(req, server) { | ||
| seenUrl = new URL(req.url).pathname + new URL(req.url).search; | ||
| seenHost = req.headers.get("host") ?? ""; | ||
| if (server.upgrade(req)) return; | ||
| return new Response("not a websocket", { status: 400 }); | ||
| }, | ||
| websocket: { | ||
| message(ws, message) { | ||
| ws.send(`echo:${message}`); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const ws = new WebSocket(`ws+unix://${unix}:/api/v1/stream?x=1`); | ||
| const { promise, resolve, reject } = Promise.withResolvers<string>(); | ||
| ws.onerror = e => reject(e); | ||
| ws.onopen = () => ws.send("hi"); | ||
| ws.onmessage = e => { | ||
| resolve(String(e.data)); | ||
| ws.close(); | ||
| }; | ||
|
|
||
| const got = await promise; | ||
| expect(got).toBe("echo:hi"); | ||
| expect(seenUrl).toBe("/api/v1/stream?x=1"); | ||
| // Host header defaults to "localhost" over a unix socket, matching Node. | ||
| expect(seenHost).toBe("localhost"); | ||
| }); | ||
|
|
||
| test("ws+unix:// sends binary data", async () => { | ||
| const unix = sockPath("bin"); | ||
| await using server = Bun.serve({ | ||
| unix, | ||
| fetch(req, server) { | ||
| if (server.upgrade(req)) return; | ||
| return new Response("no", { status: 400 }); | ||
| }, | ||
| websocket: { | ||
| message(ws, message) { | ||
| ws.sendBinary(message as Uint8Array); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const ws = new WebSocket(`ws+unix://${unix}`); | ||
| ws.binaryType = "arraybuffer"; | ||
| const payload = new Uint8Array([1, 2, 3, 4, 5, 255]); | ||
| const { promise, resolve, reject } = Promise.withResolvers<ArrayBuffer>(); | ||
| ws.onerror = e => reject(e); | ||
| ws.onopen = () => ws.send(payload); | ||
| ws.onmessage = e => { | ||
| resolve(e.data); | ||
| ws.close(); | ||
| }; | ||
| const got = new Uint8Array(await promise); | ||
| expect([...got]).toEqual([...payload]); | ||
| }); | ||
|
|
||
| test("ws+unix:// connection failure emits close when socket file does not exist", async () => { | ||
| const unix = sockPath("missing"); | ||
| const ws = new WebSocket(`ws+unix://${unix}`); | ||
| const { promise, resolve } = Promise.withResolvers<{ code: number; gotError: boolean }>(); | ||
| let gotError = false; | ||
| ws.onerror = () => { | ||
| gotError = true; | ||
| }; | ||
| ws.onclose = e => resolve({ code: e.code, gotError }); | ||
| const { code, gotError: sawError } = await promise; | ||
| expect(sawError).toBe(true); | ||
| expect(code).toBe(1006); | ||
| }); | ||
|
|
||
| test("ws+unix:// without a socket path throws SyntaxError", () => { | ||
| expect(() => new WebSocket("ws+unix://")).toThrow(SyntaxError); | ||
| }); | ||
|
|
||
| test("wss+unix:// connects to a TLS server over a unix socket", async () => { | ||
| const unix = sockPath("tls"); | ||
| await using server = Bun.serve({ | ||
| unix, | ||
| tls: tlsCert, | ||
| fetch(req, server) { | ||
| if (server.upgrade(req)) return; | ||
| return new Response("no", { status: 400 }); | ||
| }, | ||
| websocket: { | ||
| message(ws, message) { | ||
| ws.send(`secure:${message}`); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const ws = new WebSocket(`wss+unix://${unix}`, { | ||
| // @ts-expect-error bun extension | ||
| tls: { rejectUnauthorized: false }, | ||
| }); | ||
| const { promise, resolve, reject } = Promise.withResolvers<string>(); | ||
| ws.onerror = e => reject(e); | ||
| ws.onopen = () => ws.send("hello"); | ||
| ws.onmessage = e => { | ||
| resolve(String(e.data)); | ||
| ws.close(); | ||
| }; | ||
| const got = await promise; | ||
| expect(got).toBe("secure:hello"); | ||
| }); | ||
|
|
||
| test("works from a subprocess", async () => { | ||
| const unix = sockPath("sp"); | ||
| await using server = Bun.serve({ | ||
| unix, | ||
| fetch(req, server) { | ||
| if (server.upgrade(req)) return; | ||
| return new Response("no", { status: 400 }); | ||
| }, | ||
| websocket: { | ||
| message(ws, message) { | ||
| ws.send(`pong:${message}`); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [ | ||
| bunExe(), | ||
| "-e", | ||
| ` | ||
| const ws = new WebSocket(process.argv[1]); | ||
| ws.onopen = () => ws.send("from-child"); | ||
| ws.onmessage = e => { | ||
| console.log(String(e.data)); | ||
| ws.close(); | ||
| }; | ||
| ws.onerror = e => { | ||
| console.error("error", e && e.message); | ||
| process.exit(1); | ||
| }; | ||
| `, | ||
| `ws+unix://${unix}`, | ||
| ], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stdout.trim()).toBe("pong:from-child"); | ||
| expect(stderr).not.toContain("error"); | ||
| expect(exitCode).toBe(0); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.