1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

fix(extension/websocket): better websocket errors (#10458)

This commit is contained in:
crowlKats 2021-05-01 20:52:13 +02:00 committed by GitHub
parent 012da3ae1e
commit da911e741c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 55 deletions

View file

@ -118,44 +118,32 @@
url: wsURL.href,
protocols: protocols.join(", "),
}).then((create) => {
if (create.success) {
this.#rid = create.rid;
this.#extensions = create.extensions;
this.#protocol = create.protocol;
this.#rid = create.rid;
this.#extensions = create.extensions;
this.#protocol = create.protocol;
if (this.#readyState === CLOSING) {
core.opAsync("op_ws_close", {
rid: this.#rid,
}).then(() => {
this.#readyState = CLOSED;
if (this.#readyState === CLOSING) {
core.opAsync("op_ws_close", {
rid: this.#rid,
}).then(() => {
this.#readyState = CLOSED;
const errEvent = new ErrorEvent("error");
errEvent.target = this;
this.dispatchEvent(errEvent);
const errEvent = new ErrorEvent("error");
errEvent.target = this;
this.dispatchEvent(errEvent);
const event = new CloseEvent("close");
event.target = this;
this.dispatchEvent(event);
tryClose(this.#rid);
});
} else {
this.#readyState = OPEN;
const event = new Event("open");
const event = new CloseEvent("close");
event.target = this;
this.dispatchEvent(event);
this.#eventLoop();
}
tryClose(this.#rid);
});
} else {
this.#readyState = CLOSED;
this.#readyState = OPEN;
const event = new Event("open");
event.target = this;
this.dispatchEvent(event);
const errEvent = new ErrorEvent("error");
errEvent.target = this;
this.dispatchEvent(errEvent);
const closeEvent = new CloseEvent("close");
closeEvent.target = this;
this.dispatchEvent(closeEvent);
this.#eventLoop();
}
}).catch((err) => {
this.#readyState = CLOSED;
@ -362,7 +350,9 @@
case "error": {
this.#readyState = CLOSED;
const errorEv = new ErrorEvent("error");
const errorEv = new ErrorEvent("error", {
message: value,
});
errorEv.target = this;
this.dispatchEvent(errorEv);

View file

@ -35,7 +35,6 @@ use std::rc::Rc;
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio_rustls::{rustls::ClientConfig, TlsConnector};
use tokio_tungstenite::tungstenite::Error as TungsteniteError;
use tokio_tungstenite::tungstenite::{
handshake::client::Response, protocol::frame::coding::CloseCode,
protocol::CloseFrame, Message,
@ -110,10 +109,9 @@ pub struct CreateArgs {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateResponse {
success: bool,
rid: Option<ResourceId>,
protocol: Option<String>,
extensions: Option<String>,
rid: ResourceId,
protocol: String,
extensions: String,
}
pub async fn op_ws_create<WP>(
@ -152,18 +150,7 @@ where
_ => unreachable!(),
});
let addr = format!("{}:{}", domain, port);
let try_socket = TcpStream::connect(addr).await;
let tcp_socket = match try_socket.map_err(TungsteniteError::Io) {
Ok(socket) => socket,
Err(_) => {
return Ok(CreateResponse {
success: false,
rid: None,
protocol: None,
extensions: None,
})
}
};
let tcp_socket = TcpStream::connect(addr).await?;
let socket: MaybeTlsStream<TcpStream> = match uri.scheme_str() {
Some("ws") => MaybeTlsStream::Plain(tcp_socket),
@ -215,10 +202,9 @@ where
.map(|header| header.to_str().unwrap())
.collect::<String>();
Ok(CreateResponse {
success: true,
rid: Some(rid),
protocol: Some(protocol.to_string()),
extensions: Some(extensions),
rid,
protocol: protocol.to_string(),
extensions,
})
}
@ -293,7 +279,7 @@ pub enum NextEventResponse {
Close { code: u16, reason: String },
Ping,
Pong,
Error,
Error(String),
Closed,
}
@ -324,7 +310,7 @@ pub async fn op_ws_next_event(
},
Some(Ok(Message::Ping(_))) => NextEventResponse::Ping,
Some(Ok(Message::Pong(_))) => NextEventResponse::Pong,
Some(Err(_)) => NextEventResponse::Error,
Some(Err(e)) => NextEventResponse::Error(e.to_string()),
None => {
state.borrow_mut().resource_table.close(rid).unwrap();
NextEventResponse::Closed