2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-09-26 20:21:06 -04:00
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use crate::tools::jupyter::jupyter_msg::Connection;
|
|
|
|
use crate::tools::jupyter::jupyter_msg::JupyterMessage;
|
|
|
|
use crate::tools::jupyter::server::StdioMsg;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::op2;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
deno_core::extension!(deno_jupyter,
|
|
|
|
ops = [
|
|
|
|
op_jupyter_broadcast,
|
|
|
|
],
|
|
|
|
options = {
|
|
|
|
sender: mpsc::UnboundedSender<StdioMsg>,
|
|
|
|
},
|
|
|
|
middleware = |op| match op.name {
|
2024-04-12 15:45:38 -04:00
|
|
|
"op_print" => op_print(),
|
2023-09-26 20:21:06 -04:00
|
|
|
_ => op,
|
|
|
|
},
|
|
|
|
state = |state, options| {
|
|
|
|
state.put(options.sender);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
#[op2(async)]
|
|
|
|
pub async fn op_jupyter_broadcast(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
#[string] message_type: String,
|
|
|
|
#[serde] content: serde_json::Value,
|
2023-09-29 18:24:09 -04:00
|
|
|
#[serde] metadata: serde_json::Value,
|
2023-10-04 07:05:20 -04:00
|
|
|
#[serde] buffers: Vec<deno_core::JsBuffer>,
|
2023-09-26 20:21:06 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let (iopub_socket, last_execution_request) = {
|
|
|
|
let s = state.borrow();
|
|
|
|
|
|
|
|
(
|
|
|
|
s.borrow::<Arc<Mutex<Connection<zeromq::PubSocket>>>>()
|
|
|
|
.clone(),
|
|
|
|
s.borrow::<Rc<RefCell<Option<JupyterMessage>>>>().clone(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let maybe_last_request = last_execution_request.borrow().clone();
|
|
|
|
if let Some(last_request) = maybe_last_request {
|
2024-04-30 22:30:40 -04:00
|
|
|
(*iopub_socket.lock().await)
|
|
|
|
.send(
|
|
|
|
&last_request
|
|
|
|
.new_message(&message_type)
|
|
|
|
.with_content(content)
|
|
|
|
.with_metadata(metadata)
|
|
|
|
.with_buffers(
|
|
|
|
buffers.into_iter().map(|b| b.to_vec().into()).collect(),
|
|
|
|
),
|
|
|
|
)
|
2023-09-26 20:21:06 -04:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_print(
|
|
|
|
state: &mut OpState,
|
|
|
|
#[string] msg: &str,
|
|
|
|
is_err: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let sender = state.borrow_mut::<mpsc::UnboundedSender<StdioMsg>>();
|
|
|
|
|
|
|
|
if is_err {
|
|
|
|
if let Err(err) = sender.send(StdioMsg::Stderr(msg.into())) {
|
2024-05-08 22:45:06 -04:00
|
|
|
log::error!("Failed to send stderr message: {}", err);
|
2023-09-26 20:21:06 -04:00
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = sender.send(StdioMsg::Stdout(msg.into())) {
|
2024-05-08 22:45:06 -04:00
|
|
|
log::error!("Failed to send stdout message: {}", err);
|
2023-09-26 20:21:06 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|