Skip to content

Commit

Permalink
fix(op_crates/websocket): default to close code 1005 (denoland#9339)
Browse files Browse the repository at this point in the history
Currently if WebSocket is closed without code, it will error 
while on Chrome it would close with code 1005 instead.

Co-authored-by: crowlKats <[email protected]>
  • Loading branch information
DjDeveloperr and crowlKats authored Feb 21, 2021
1 parent eefd522 commit fe1b512
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
11 changes: 11 additions & 0 deletions cli/tests/websocket_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,14 @@ Deno.test("Event Handlers order", async () => {
};
await promise;
});

Deno.test("Close without frame", async () => {
const promise = deferred();
const ws = new WebSocket("ws://localhost:4244");
ws.onerror = (): void => fail();
ws.onclose = (e): void => {
assertEquals(e.code, 1005);
promise.resolve();
};
await promise;
});
2 changes: 2 additions & 0 deletions op_crates/websocket/01_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
});
event.target = this;
this.dispatchEvent(event);
core.close(this.#rid);

break;
}
Expand All @@ -364,6 +365,7 @@
const closeEv = new CloseEvent("close");
closeEv.target = this;
this.dispatchEvent(closeEv);
core.close(this.#rid);

break;
}
Expand Down
8 changes: 7 additions & 1 deletion op_crates/websocket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,13 @@ pub async fn op_ws_next_event(
"reason": frame.reason.as_ref()
}
}),
Some(Ok(Message::Close(None))) => json!({ "kind": "close" }),
Some(Ok(Message::Close(None))) => json!({
"kind": "close",
"data": {
"code": 1005,
"reason": ""
}
}),
Some(Ok(Message::Ping(_))) => json!({ "kind": "ping" }),
Some(Ok(Message::Pong(_))) => json!({ "kind": "pong" }),
Some(Err(_)) => json!({ "kind": "error" }),
Expand Down
18 changes: 18 additions & 0 deletions test_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const AUTH_REDIRECT_PORT: u16 = 4551;
const HTTPS_PORT: u16 = 5545;
const WS_PORT: u16 = 4242;
const WSS_PORT: u16 = 4243;
const WS_CLOSE_PORT: u16 = 4244;

pub const PERMISSION_VARIANTS: [&str; 5] =
["read", "write", "env", "net", "run"];
Expand Down Expand Up @@ -243,6 +244,20 @@ async fn run_ws_server(addr: &SocketAddr) {
}
}

async fn run_ws_close_server(addr: &SocketAddr) {
let listener = TcpListener::bind(addr).await.unwrap();
while let Ok((stream, _addr)) = listener.accept().await {
tokio::spawn(async move {
let ws_stream_fut = accept_async(stream);

let ws_stream = ws_stream_fut.await;
if let Ok(mut ws_stream) = ws_stream {
ws_stream.close(None).await.unwrap();
}
});
}
}

async fn get_tls_config(
cert: &str,
key: &str,
Expand Down Expand Up @@ -781,6 +796,8 @@ pub async fn run_all_servers() {
let ws_server_fut = run_ws_server(&ws_addr);
let wss_addr = SocketAddr::from(([127, 0, 0, 1], WSS_PORT));
let wss_server_fut = run_wss_server(&wss_addr);
let ws_close_addr = SocketAddr::from(([127, 0, 0, 1], WS_CLOSE_PORT));
let ws_close_server_fut = run_ws_close_server(&ws_close_addr);

let main_server_fut = wrap_main_server();
let main_server_https_fut = wrap_main_https_server();
Expand All @@ -790,6 +807,7 @@ pub async fn run_all_servers() {
redirect_server_fut,
ws_server_fut,
wss_server_fut,
ws_close_server_fut,
another_redirect_server_fut,
auth_redirect_server_fut,
inf_redirects_server_fut,
Expand Down

0 comments on commit fe1b512

Please sign in to comment.