From 995d1666ffe44c5f8694f9ef43390ca3ab64bb26 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 4 Apr 2022 15:19:50 +0530 Subject: [PATCH] chore(ext/websocket): custom arity (#14202) --- ext/http/01_http.js | 2 +- ext/websocket/01_websocket.js | 25 +++++----------- ext/websocket/02_websocketstream.js | 23 ++++++--------- ext/websocket/lib.rs | 46 +++++++++++------------------ 4 files changed, 35 insertions(+), 61 deletions(-) diff --git a/ext/http/01_http.js b/ext/http/01_http.js index 2ab2fea7b0..217bfc0614 100644 --- a/ext/http/01_http.js +++ b/ext/http/01_http.js @@ -328,7 +328,7 @@ httpConn.close(); if (ws[_readyState] === WebSocket.CLOSING) { - await core.opAsync("op_ws_close", { rid: wsRid }); + await core.opAsync("op_ws_close", wsRid); ws[_readyState] = WebSocket.CLOSED; diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 2c6337eef9..19c5e28758 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -223,10 +223,11 @@ } PromisePrototypeThen( - core.opAsync("op_ws_create", { - url: wsURL.href, - protocols: ArrayPrototypeJoin(protocols, ", "), - }), + core.opAsync( + "op_ws_create", + wsURL.href, + ArrayPrototypeJoin(protocols, ", "), + ), (create) => { this[_rid] = create.rid; this[_extensions] = create.extensions; @@ -234,9 +235,7 @@ if (this[_readyState] === CLOSING) { PromisePrototypeThen( - core.opAsync("op_ws_close", { - rid: this[_rid], - }), + core.opAsync("op_ws_close", this[_rid]), () => { this[_readyState] = CLOSED; @@ -369,11 +368,7 @@ this[_readyState] = CLOSING; PromisePrototypeThen( - core.opAsync("op_ws_close", { - rid: this[_rid], - code, - reason, - }), + core.opAsync("op_ws_close", this[_rid], code, reason), () => { this[_readyState] = CLOSED; const event = new CloseEvent("close", { @@ -473,11 +468,7 @@ this[_idleTimeoutTimeout] = setTimeout(async () => { this[_readyState] = CLOSING; const reason = "No response from ping frame."; - await core.opAsync("op_ws_close", { - rid: this[_rid], - code: 1001, - reason, - }); + await core.opAsync("op_ws_close", this[_rid], 1001, reason); this[_readyState] = CLOSED; const errEvent = new ErrorEvent("error", { diff --git a/ext/websocket/02_websocketstream.js b/ext/websocket/02_websocketstream.js index d41dcceef9..50dfac284e 100644 --- a/ext/websocket/02_websocketstream.js +++ b/ext/websocket/02_websocketstream.js @@ -145,21 +145,20 @@ }; options.signal?.[add](abort); PromisePrototypeThen( - core.opAsync("op_ws_create", { - url: this[_url], - protocols: options.protocols + core.opAsync( + "op_ws_create", + this[_url], + options.protocols ? ArrayPrototypeJoin(options.protocols, ", ") : "", - cancelHandle: cancelRid, - headers: headerListFromHeaders(headers), - }), + cancelRid, + headerListFromHeaders(headers), + ), (create) => { options.signal?.[remove](abort); if (this[_earlyClose]) { PromisePrototypeThen( - core.opAsync("op_ws_close", { - rid: create.rid, - }), + core.opAsync("op_ws_close", create.rid), () => { PromisePrototypeThen( (async () => { @@ -369,11 +368,7 @@ this[_earlyClose] = true; } else if (this[_closed].state === "pending") { PromisePrototypeCatch( - core.opAsync("op_ws_close", { - rid: this[_rid], - code, - reason: closeInfo.reason, - }), + core.opAsync("op_ws_close", this[_rid], code, closeInfo.reason), (err) => { this[_rid] && core.tryClose(this[_rid]); this[_closed].reject(err); diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index 95afd79bd2..163ba46dd0 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -214,15 +214,6 @@ where } } -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CreateArgs { - url: String, - protocols: String, - cancel_handle: Option, - headers: Option>, -} - #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct CreateResponse { @@ -234,7 +225,10 @@ pub struct CreateResponse { #[op] pub async fn op_ws_create( state: Rc>, - args: CreateArgs, + url: String, + protocols: String, + cancel_handle: Option, + headers: Option>, ) -> Result where WP: WebSocketPermissions + 'static, @@ -242,13 +236,13 @@ where { let mut s = state.borrow_mut(); s.borrow_mut::() - .check_net_url(&url::Url::parse(&args.url)?) + .check_net_url(&url::Url::parse(&url)?) .expect( "Permission check should have been done in op_ws_check_permission", ); } - let cancel_resource = if let Some(cancel_rid) = args.cancel_handle { + let cancel_resource = if let Some(cancel_rid) = cancel_handle { let r = state .borrow_mut() .resource_table @@ -264,16 +258,16 @@ where .and_then(|it| it.0.clone()); let root_cert_store = state.borrow().borrow::().0.clone(); let user_agent = state.borrow().borrow::().0.clone(); - let uri: Uri = args.url.parse()?; + let uri: Uri = url.parse()?; let mut request = Request::builder().method(Method::GET).uri(&uri); request = request.header("User-Agent", user_agent); - if !args.protocols.is_empty() { - request = request.header("Sec-WebSocket-Protocol", args.protocols); + if !protocols.is_empty() { + request = request.header("Sec-WebSocket-Protocol", protocols); } - if let Some(headers) = args.headers { + if let Some(headers) = headers { for (key, value) in headers { let name = HeaderName::from_bytes(&key) .map_err(|err| type_error(err.to_string()))?; @@ -339,7 +333,7 @@ where )) })?; - if let Some(cancel_rid) = args.cancel_handle { + if let Some(cancel_rid) = cancel_handle { state.borrow_mut().resource_table.close(cancel_rid).ok(); } @@ -401,23 +395,17 @@ pub async fn op_ws_send( Ok(()) } -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CloseArgs { - rid: ResourceId, - code: Option, - reason: Option, -} - #[op] pub async fn op_ws_close( state: Rc>, - args: CloseArgs, + rid: ResourceId, + code: Option, + reason: Option, ) -> Result<(), AnyError> { - let rid = args.rid; - let msg = Message::Close(args.code.map(|c| CloseFrame { + let rid = rid; + let msg = Message::Close(code.map(|c| CloseFrame { code: CloseCode::from(c), - reason: match args.reason { + reason: match reason { Some(reason) => Cow::from(reason), None => Default::default(), },