mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
add server ws.terminate (#9693)
* add server ws.terminate * [autofix.ci] apply automated fixes * it has to be blazing fast --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
17
src/js/thirdparty/ws.js
vendored
17
src/js/thirdparty/ws.js
vendored
@@ -700,6 +700,23 @@ class BunWebSocketMocked extends EventEmitter {
|
||||
this.#ws.close(code, reason);
|
||||
}
|
||||
}
|
||||
|
||||
terminate() {
|
||||
let state = this.#state;
|
||||
if (state === 3) return;
|
||||
if (state === 0) {
|
||||
const msg = "WebSocket was closed before the connection was established";
|
||||
abortHandshake(this, this._req, msg);
|
||||
return;
|
||||
}
|
||||
|
||||
let ws = this.#ws;
|
||||
if (ws) {
|
||||
this.#state = WebSocket.CLOSING;
|
||||
ws.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
get binaryType() {
|
||||
return this.#binaryType;
|
||||
}
|
||||
|
||||
@@ -276,6 +276,25 @@ describe("WebSocketServer", () => {
|
||||
new WebSocket("ws://localhost:" + wss.address().port);
|
||||
await promise;
|
||||
});
|
||||
|
||||
it("sockets can be terminated", async () => {
|
||||
const wss = new WebSocketServer({ port: 0 });
|
||||
const { resolve, reject, promise } = Promise.withResolvers();
|
||||
|
||||
wss.on("connection", ws => {
|
||||
ws.on("close", () => {
|
||||
resolve();
|
||||
});
|
||||
try {
|
||||
ws.terminate();
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
new WebSocket("ws://localhost:" + wss.address().port);
|
||||
await promise;
|
||||
});
|
||||
});
|
||||
|
||||
describe("Server", () => {
|
||||
|
||||
Reference in New Issue
Block a user