1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-10 08:09:06 -05:00

fix(ext/websocket): Fix close code without reason (#27578)

Fixes https://github.com/denoland/deno/issues/27566

The close code wasn't sent if reason was None, defaulting to 1005. This
patch allows sending close code without reason.
This commit is contained in:
Divy Srivastava 2025-01-08 20:07:47 +05:30 committed by GitHub
parent 1661ddd9ca
commit e233173653
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 4 deletions

View file

@ -758,9 +758,14 @@ pub async fn op_ws_close(
return Ok(()); return Ok(());
}; };
const EMPTY_PAYLOAD: &[u8] = &[];
let frame = reason let frame = reason
.map(|reason| Frame::close(code.unwrap_or(1005), reason.as_bytes())) .map(|reason| Frame::close(code.unwrap_or(1005), reason.as_bytes()))
.unwrap_or_else(|| Frame::close_raw(vec![].into())); .unwrap_or_else(|| match code {
Some(code) => Frame::close(code, EMPTY_PAYLOAD),
_ => Frame::close_raw(EMPTY_PAYLOAD.into()),
});
resource.closed.set(true); resource.closed.set(true);
let lock = resource.reserve_lock(); let lock = resource.reserve_lock();

View file

@ -2663,7 +2663,6 @@ async fn websocket_server_multi_field_connection_header() {
let message = socket.read_frame().await.unwrap(); let message = socket.read_frame().await.unwrap();
assert_eq!(message.opcode, fastwebsockets::OpCode::Close); assert_eq!(message.opcode, fastwebsockets::OpCode::Close);
assert!(message.payload.is_empty());
socket socket
.write_frame(fastwebsockets::Frame::close_raw(vec![].into())) .write_frame(fastwebsockets::Frame::close_raw(vec![].into()))
.await .await

View file

@ -262,7 +262,7 @@ Deno.test({
socket.onopen = () => socket.send("Hello"); socket.onopen = () => socket.send("Hello");
socket.onmessage = () => { socket.onmessage = () => {
socket.send("Bye"); socket.send("Bye");
socket.close(); socket.close(1000);
}; };
socket.onclose = () => ac.abort(); socket.onclose = () => ac.abort();
socket.onerror = () => fail(); socket.onerror = () => fail();
@ -288,7 +288,8 @@ Deno.test({
seenBye = true; seenBye = true;
} }
}; };
ws.onclose = () => { ws.onclose = (e) => {
assertEquals(e.code, 1000);
deferred.resolve(); deferred.resolve();
}; };
await Promise.all([deferred.promise, server.finished]); await Promise.all([deferred.promise, server.finished]);