From 9a46a824bd897e240af8a14f9d950ab6d95f42a5 Mon Sep 17 00:00:00 2001 From: Trevor Manz Date: Wed, 4 Oct 2023 07:05:20 -0400 Subject: feat(jupyter): send binary data with `Deno.jupyter.broadcast` (#20755) Adds `buffers` to the `Deno.jupyter.broadcast` API to send binary data via comms. This affords the ability to send binary data via websockets to the jupyter widget frontend. --- cli/tools/jupyter/jupyter_msg.rs | 58 +++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 22 deletions(-) (limited to 'cli/tools/jupyter/jupyter_msg.rs') diff --git a/cli/tools/jupyter/jupyter_msg.rs b/cli/tools/jupyter/jupyter_msg.rs index beb9f34e4..da6654cae 100644 --- a/cli/tools/jupyter/jupyter_msg.rs +++ b/cli/tools/jupyter/jupyter_msg.rs @@ -122,6 +122,7 @@ pub(crate) struct JupyterMessage { parent_header: serde_json::Value, metadata: serde_json::Value, content: serde_json::Value, + buffers: Vec, } const DELIMITER: &[u8] = b""; @@ -146,6 +147,11 @@ impl JupyterMessage { parent_header: serde_json::from_slice(&raw_message.jparts[1])?, metadata: serde_json::from_slice(&raw_message.jparts[2])?, content: serde_json::from_slice(&raw_message.jparts[3])?, + buffers: if raw_message.jparts.len() > 4 { + raw_message.jparts[4..].to_vec() + } else { + vec![] + }, }) } @@ -179,6 +185,7 @@ impl JupyterMessage { parent_header: self.header.clone(), metadata: json!({}), content: json!({}), + buffers: vec![], } } @@ -214,36 +221,43 @@ impl JupyterMessage { self } + pub(crate) fn with_buffers(mut self, buffers: Vec) -> JupyterMessage { + self.buffers = buffers; + self + } + pub(crate) async fn send( &self, connection: &mut Connection, ) -> Result<(), AnyError> { // If performance is a concern, we can probably avoid the clone and to_vec calls with a bit // of refactoring. + let mut jparts: Vec = vec![ + serde_json::to_string(&self.header) + .unwrap() + .as_bytes() + .to_vec() + .into(), + serde_json::to_string(&self.parent_header) + .unwrap() + .as_bytes() + .to_vec() + .into(), + serde_json::to_string(&self.metadata) + .unwrap() + .as_bytes() + .to_vec() + .into(), + serde_json::to_string(&self.content) + .unwrap() + .as_bytes() + .to_vec() + .into(), + ]; + jparts.extend_from_slice(&self.buffers); let raw_message = RawMessage { zmq_identities: self.zmq_identities.clone(), - jparts: vec![ - serde_json::to_string(&self.header) - .unwrap() - .as_bytes() - .to_vec() - .into(), - serde_json::to_string(&self.parent_header) - .unwrap() - .as_bytes() - .to_vec() - .into(), - serde_json::to_string(&self.metadata) - .unwrap() - .as_bytes() - .to_vec() - .into(), - serde_json::to_string(&self.content) - .unwrap() - .as_bytes() - .to_vec() - .into(), - ], + jparts, }; raw_message.send(connection).await } -- cgit v1.2.3