diff options
author | Trevor Manz <trevor.j.manz@gmail.com> | 2023-10-04 07:05:20 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-04 13:05:20 +0200 |
commit | 9a46a824bd897e240af8a14f9d950ab6d95f42a5 (patch) | |
tree | 9c6bf70579452767b194112d6a531c14feec5652 /cli/tools | |
parent | da0b945804f19903beac71b23ff1040ebdb9b554 (diff) |
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.
Diffstat (limited to 'cli/tools')
-rw-r--r-- | cli/tools/jupyter/jupyter_msg.rs | 58 |
1 files changed, 36 insertions, 22 deletions
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<Bytes>, } const DELIMITER: &[u8] = b"<IDS|MSG>"; @@ -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<Bytes>) -> JupyterMessage { + self.buffers = buffers; + self + } + pub(crate) async fn send<S: zeromq::SocketSend>( &self, connection: &mut Connection<S>, ) -> 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<Bytes> = 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 } |