1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 17:19:06 -05:00

fix(op_crates/websocket): default to close code 1005 (#9339)

Currently if WebSocket is closed without code, it will error 
while on Chrome it would close with code 1005 instead.

Co-authored-by: crowlKats <13135287+crowlKats@users.noreply.github.com>
This commit is contained in:
DjDeveloper 2021-02-21 22:21:46 +05:30 committed by GitHub
parent eefd522f04
commit fe1b512820
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 1 deletions

View file

@ -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;
});

View file

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

View file

@ -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" }),

View file

@ -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"];
@ -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,
@ -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();
@ -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,