diff options
Diffstat (limited to 'ext/websocket')
-rw-r--r-- | ext/websocket/01_websocket.js | 14 | ||||
-rw-r--r-- | ext/websocket/02_websocketstream.js | 12 | ||||
-rw-r--r-- | ext/websocket/lib.rs | 26 |
3 files changed, 23 insertions, 29 deletions
diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 79e4d923c..54e05c408 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -324,10 +324,10 @@ const sendTypedArray = (ta) => { this[_bufferedAmount] += ta.byteLength; PromisePrototypeThen( - core.opAsync("op_ws_send", { - rid: this[_rid], + core.opAsync("op_ws_send", this[_rid], { kind: "binary", - }, ta), + value: ta, + }), () => { this[_bufferedAmount] -= ta.byteLength; }, @@ -348,10 +348,9 @@ const d = core.encode(string); this[_bufferedAmount] += d.byteLength; PromisePrototypeThen( - core.opAsync("op_ws_send", { - rid: this[_rid], + core.opAsync("op_ws_send", this[_rid], { kind: "text", - text: string, + value: string, }), () => { this[_bufferedAmount] -= d.byteLength; @@ -456,8 +455,7 @@ break; } case "ping": { - core.opAsync("op_ws_send", { - rid: this[_rid], + core.opAsync("op_ws_send", this[_rid], { kind: "pong", }); break; diff --git a/ext/websocket/02_websocketstream.js b/ext/websocket/02_websocketstream.js index f7c4d4d0f..82f306333 100644 --- a/ext/websocket/02_websocketstream.js +++ b/ext/websocket/02_websocketstream.js @@ -190,15 +190,14 @@ const writable = new WritableStream({ write: async (chunk) => { if (typeof chunk === "string") { - await core.opAsync("op_ws_send", { - rid: this[_rid], + await core.opAsync("op_ws_send", this[_rid], { kind: "text", - text: chunk, + value: chunk, }); } else if (chunk instanceof Uint8Array) { - await core.opAsync("op_ws_send", { - rid: this[_rid], + await core.opAsync("op_ws_send", this[_rid], { kind: "binary", + value: chunk, }, chunk); } else { throw new TypeError( @@ -257,8 +256,7 @@ break; } case "ping": { - await core.opAsync("op_ws_send", { - rid: this[_rid], + await core.opAsync("op_ws_send", this[_rid], { kind: "pong", }); break; diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index ebb2186d0..32ac4cf03 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -1,7 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use deno_core::error::invalid_hostname; -use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::futures::stream::SplitSink; use deno_core::futures::stream::SplitStream; @@ -309,29 +308,28 @@ where } #[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct SendArgs { - rid: ResourceId, - kind: String, - text: Option<String>, +#[serde(tag = "kind", content = "value", rename_all = "camelCase")] +pub enum SendValue { + Text(String), + Binary(ZeroCopyBuf), + Pong, } pub async fn op_ws_send( state: Rc<RefCell<OpState>>, - args: SendArgs, - buf: Option<ZeroCopyBuf>, + rid: ResourceId, + value: SendValue, ) -> Result<(), AnyError> { - let msg = match args.kind.as_str() { - "text" => Message::Text(args.text.unwrap()), - "binary" => Message::Binary(buf.ok_or_else(null_opbuf)?.to_vec()), - "pong" => Message::Pong(vec![]), - _ => unreachable!(), + let msg = match value { + SendValue::Text(text) => Message::Text(text), + SendValue::Binary(buf) => Message::Binary(buf.to_vec()), + SendValue::Pong => Message::Pong(vec![]), }; let resource = state .borrow_mut() .resource_table - .get::<WsStreamResource>(args.rid)?; + .get::<WsStreamResource>(rid)?; resource.send(msg).await?; Ok(()) } |