From 1a81b2826d7e0f64831c7a96d3cfb113ea6e7eb7 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 5 Oct 2023 03:13:58 +0530 Subject: refactor: rewrite websocket to use op2 macro (#20140) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bartek IwaƄczuk --- ext/websocket/01_websocket.js | 8 +--- ext/websocket/lib.rs | 93 +++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 35 deletions(-) (limited to 'ext/websocket') diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 5f0f648a7..3417eff67 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -26,7 +26,6 @@ const { ArrayPrototypeJoin, ArrayPrototypeMap, ArrayPrototypeSome, - DataView, ErrorPrototypeToString, ObjectDefineProperties, ObjectPrototypeIsPrototypeOf, @@ -50,6 +49,7 @@ const { op_ws_create, op_ws_close, op_ws_send_binary, + op_ws_send_binary_ab, op_ws_send_text, op_ws_next_event, op_ws_get_buffer, @@ -336,11 +336,7 @@ class WebSocket extends EventTarget { PromisePrototypeThen( // deno-lint-ignore prefer-primordials data.slice().arrayBuffer(), - (ab) => - op_ws_send_binary( - this[_rid], - new DataView(ab), - ), + (ab) => op_ws_send_binary_ab(this[_rid], ab), ); } else { const string = String(data); diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index 479cae7ec..48a22431b 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -5,6 +5,7 @@ use deno_core::error::invalid_hostname; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op; +use deno_core::op2; use deno_core::url; use deno_core::AsyncMutFuture; use deno_core::AsyncRefCell; @@ -108,11 +109,12 @@ impl Resource for WsCancelResource { // This op is needed because creating a WS instance in JavaScript is a sync // operation and should throw error when permissions are not fulfilled, // but actual op that connects WS is async. -#[op] +#[op2] +#[smi] pub fn op_ws_check_permission_and_cancel_handle( state: &mut OpState, - api_name: String, - url: String, + #[string] api_name: String, + #[string] url: String, cancel_handle: bool, ) -> Result, AnyError> where @@ -167,14 +169,15 @@ async fn handshake( Ok((stream, response)) } -#[op] +#[op2(async)] +#[serde] pub async fn op_ws_create( state: Rc>, - api_name: String, - url: String, - protocols: String, - cancel_handle: Option, - headers: Option>, + #[string] api_name: String, + #[string] url: String, + #[string] protocols: String, + #[smi] cancel_handle: Option, + #[serde] headers: Option>, ) -> Result where WP: WebSocketPermissions + 'static, @@ -407,8 +410,7 @@ pub fn ws_create_server_stream( Ok(rid) } -#[op(fast)] -pub fn op_ws_send_binary(state: &mut OpState, rid: ResourceId, data: &[u8]) { +fn send_binary(state: &mut OpState, rid: ResourceId, data: &[u8]) { let resource = state.resource_table.get::(rid).unwrap(); let data = data.to_vec(); let len = data.len(); @@ -426,8 +428,30 @@ pub fn op_ws_send_binary(state: &mut OpState, rid: ResourceId, data: &[u8]) { }); } -#[op(fast)] -pub fn op_ws_send_text(state: &mut OpState, rid: ResourceId, data: String) { +#[op2(fast)] +pub fn op_ws_send_binary( + state: &mut OpState, + #[smi] rid: ResourceId, + #[anybuffer] data: &[u8], +) { + send_binary(state, rid, data) +} + +#[op2(fast)] +pub fn op_ws_send_binary_ab( + state: &mut OpState, + #[smi] rid: ResourceId, + #[arraybuffer] data: &[u8], +) { + send_binary(state, rid, data) +} + +#[op2(fast)] +pub fn op_ws_send_text( + state: &mut OpState, + #[smi] rid: ResourceId, + #[string] data: String, +) { let resource = state.resource_table.get::(rid).unwrap(); let len = data.len(); resource.buffered.set(resource.buffered.get() + len); @@ -487,8 +511,12 @@ pub async fn op_ws_send_text_async( const EMPTY_PAYLOAD: &[u8] = &[]; -#[op(fast)] -pub fn op_ws_get_buffered_amount(state: &mut OpState, rid: ResourceId) -> u32 { +#[op2(fast)] +#[smi] +pub fn op_ws_get_buffered_amount( + state: &mut OpState, + #[smi] rid: ResourceId, +) -> u32 { state .resource_table .get::(rid) @@ -497,10 +525,10 @@ pub fn op_ws_get_buffered_amount(state: &mut OpState, rid: ResourceId) -> u32 { .get() as u32 } -#[op] +#[op2(async)] pub async fn op_ws_send_pong( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { let resource = state .borrow_mut() @@ -512,10 +540,10 @@ pub async fn op_ws_send_pong( .await } -#[op] +#[op2(async)] pub async fn op_ws_send_ping( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { let resource = state .borrow_mut() @@ -530,12 +558,12 @@ pub async fn op_ws_send_ping( .await } -#[op(deferred)] +#[op2(async(lazy))] pub async fn op_ws_close( state: Rc>, - rid: ResourceId, - code: Option, - reason: Option, + #[smi] rid: ResourceId, + #[smi] code: Option, + #[string] reason: Option, ) -> Result<(), AnyError> { let resource = state .borrow_mut() @@ -551,23 +579,29 @@ pub async fn op_ws_close( Ok(()) } -#[op] -pub fn op_ws_get_buffer(state: &mut OpState, rid: ResourceId) -> ToJsBuffer { +#[op2] +#[serde] +pub fn op_ws_get_buffer( + state: &mut OpState, + #[smi] rid: ResourceId, +) -> ToJsBuffer { let resource = state.resource_table.get::(rid).unwrap(); resource.buffer.take().unwrap().into() } -#[op] +#[op2] +#[string] pub fn op_ws_get_buffer_as_string( state: &mut OpState, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> String { let resource = state.resource_table.get::(rid).unwrap(); resource.string.take().unwrap() } -#[op] -pub fn op_ws_get_error(state: &mut OpState, rid: ResourceId) -> String { +#[op2] +#[string] +pub fn op_ws_get_error(state: &mut OpState, #[smi] rid: ResourceId) -> String { let Ok(resource) = state.resource_table.get::(rid) else { return "Bad resource".into(); }; @@ -660,6 +694,7 @@ deno_core::extension!(deno_websocket, op_ws_get_buffer_as_string, op_ws_get_error, op_ws_send_binary, + op_ws_send_binary_ab, op_ws_send_text, op_ws_send_binary_async, op_ws_send_text_async, -- cgit v1.2.3