Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/js/thirdparty/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,20 @@ class BunWebSocket extends EventEmitter {

#onHandshake(data) {
const { statusCode, statusMessage, rawHeaders, body } = data;
const res = makeHandshakeResponse(statusCode, statusMessage, rawHeaders, statusCode === 101 ? null : body);
if (statusCode === 101) {
this.emit("upgrade", res);
if (this.listenerCount("upgrade") > 0) {
this.emit("upgrade", makeHandshakeResponse(statusCode, statusMessage, rawHeaders, null));
}
return;
}
if (this.listenerCount("unexpected-response") > 0) {
this.#unexpectedResponseEmitted = true;
this.#unexpectedResponseHandled = true;
this.emit("unexpected-response", this.#getSyntheticRequest(), res);
this.emit(
"unexpected-response",
this.#getSyntheticRequest(),
makeHandshakeResponse(statusCode, statusMessage, rawHeaders, body),
);
} else if (this.listenerCount("error") > 0) {
this.#unexpectedResponseEmitted = true;
this.emit("error", new Error("Unexpected server response: " + statusCode));
Expand Down Expand Up @@ -424,6 +429,7 @@ class BunWebSocket extends EventEmitter {
once,
);
} else if (event === "error") {
this.#ensureHandshakeListener();
this.#ws.addEventListener(
"error",
err => {
Expand Down
51 changes: 35 additions & 16 deletions test/js/first_party/ws/ws-upgrade-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,46 @@ test.concurrent("ws emits 'unexpected-response' with status, headers and body on
expect(exitCode).toBe(0);
});

// Diverges from real ws: with no 'unexpected-response' listener, real ws emits
// "Unexpected server response: 503". Bun's shim only registers the native
// handshake listener when the user subscribes to 'upgrade'/'unexpected-response',
// so the unmodified native error surfaces instead.
test.concurrent("ws emits native 'error' on non-101 when no 'unexpected-response' listener", async () => {
const { stdout, exitCode } = await run(/* js */ `
// With no 'unexpected-response' listener, real ws emits
// "Unexpected server response: <code>" to on('error'). Subscribing to 'error'
// now arms the handshake bridge so the ws-style message is delivered whether
// or not the user also subscribed to 'upgrade'.
test.concurrent(
"ws on('error') gets 'Unexpected server response: <code>' on non-101 with no other listeners",
async () => {
const { stdout, exitCode } = await run(/* js */ `
const { createServer } = require("net");
const { once } = require("events");
const { WebSocket } = require("ws");

const server = createServer(s =>
s.once("data", () => s.end("HTTP/1.1 503 Service Unavailable\\r\\n\\r\\n")),
).listen(0, "127.0.0.1");
const server = createServer(s => {
s.once("data", () => s.end("HTTP/1.1 503 Service Unavailable\\r\\n\\r\\n"));
s.on("error", () => {});
}).listen(0, "127.0.0.1");
await once(server, "listening");

const ws = new WebSocket("ws://127.0.0.1:" + server.address().port);
const [err] = await once(ws, "error");
console.log(/Expected 101/.test(err.message) ? "got native 101 error" : "unexpected: " + err.message);
async function runOne(armUpgrade) {
const ws = new WebSocket("ws://127.0.0.1:" + server.address().port);
if (armUpgrade) ws.on("upgrade", () => {});
const messages = [];
ws.on("error", err => messages.push(err.message));
await once(ws, "close").catch(() => {});
return { count: messages.length, first: messages[0] };
}

const errorOnly = await runOne(false);
const withUpgrade = await runOne(true);
console.log(JSON.stringify({ errorOnly, withUpgrade }));
server.close();
process.exit(0);
`);
expect(stdout).toMatchInlineSnapshot(`"got native 101 error"`);
expect(exitCode).toBe(0);
});
// Both paths deliver the same ws-style error exactly once.
expect(stdout).toMatchInlineSnapshot(
`"{"errorOnly":{"count":1,"first":"Unexpected server response: 503"},"withUpgrade":{"count":1,"first":"Unexpected server response: 503"}}"`,
);
expect(exitCode).toBe(0);
},
);

test.concurrent("ws emits 'upgrade' with headers before 'open' on 101", async () => {
const { stdout, exitCode } = await run(/* js */ `
Expand Down Expand Up @@ -497,7 +513,10 @@ test.concurrent(
const h = () => { count++; };
ws.addEventListener("error", h);
ws.addEventListener("error", h); // duplicate — must be a no-op
await once(ws, "close").catch(() => {});
// Wait for the native close via addEventListener so no EventEmitter 'error'
// listener is added (which would arm the handshake bridge and resolve
// early on the synthetic error, before the native error reaches h).
await new Promise(resolve => ws.addEventListener("close", resolve, { once: true }));
server.close();
try { ws.terminate(); } catch {}
// removeEventListener must fully detach (no leaked wrapper left behind).
Expand Down
Loading