2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-05-26 11:47:33 -04:00
|
|
|
|
2023-04-22 05:17:31 -04:00
|
|
|
// Alias for the future `!` type.
|
|
|
|
use core::convert::Infallible as Never;
|
2024-04-11 19:17:10 -04:00
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
2021-05-26 11:47:33 -04:00
|
|
|
use deno_core::futures::channel::mpsc;
|
|
|
|
use deno_core::futures::channel::mpsc::UnboundedReceiver;
|
|
|
|
use deno_core::futures::channel::mpsc::UnboundedSender;
|
|
|
|
use deno_core::futures::channel::oneshot;
|
|
|
|
use deno_core::futures::future;
|
|
|
|
use deno_core::futures::prelude::*;
|
|
|
|
use deno_core::futures::select;
|
|
|
|
use deno_core::futures::stream::StreamExt;
|
|
|
|
use deno_core::futures::task::Poll;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2023-08-23 19:03:05 -04:00
|
|
|
use deno_core::unsync::spawn;
|
2023-08-15 18:30:33 -04:00
|
|
|
use deno_core::url::Url;
|
2021-12-28 11:40:42 -05:00
|
|
|
use deno_core::InspectorMsg;
|
2021-05-26 15:07:12 -04:00
|
|
|
use deno_core::InspectorSessionProxy;
|
2021-08-25 07:39:23 -04:00
|
|
|
use deno_core::JsRuntime;
|
2023-12-26 15:53:28 -05:00
|
|
|
use fastwebsockets::Frame;
|
|
|
|
use fastwebsockets::OpCode;
|
|
|
|
use fastwebsockets::WebSocket;
|
2023-12-23 10:46:09 -05:00
|
|
|
use hyper::body::Bytes;
|
|
|
|
use hyper_util::rt::TokioIo;
|
2021-05-26 11:47:33 -04:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::net::SocketAddr;
|
2023-03-09 21:28:51 -05:00
|
|
|
use std::pin::pin;
|
2021-05-26 11:47:33 -04:00
|
|
|
use std::process;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::thread;
|
2023-12-23 10:46:09 -05:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::sync::broadcast;
|
2021-05-26 11:47:33 -04:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
/// Websocket server that is used to proxy connections from
|
|
|
|
/// devtools to the inspector.
|
|
|
|
pub struct InspectorServer {
|
|
|
|
pub host: SocketAddr,
|
|
|
|
register_inspector_tx: UnboundedSender<InspectorInfo>,
|
2023-12-23 10:46:09 -05:00
|
|
|
shutdown_server_tx: Option<broadcast::Sender<()>>,
|
2021-05-26 11:47:33 -04:00
|
|
|
thread_handle: Option<thread::JoinHandle<()>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InspectorServer {
|
2024-04-11 19:17:10 -04:00
|
|
|
pub fn new(host: SocketAddr, name: &'static str) -> Result<Self, AnyError> {
|
2021-05-26 11:47:33 -04:00
|
|
|
let (register_inspector_tx, register_inspector_rx) =
|
|
|
|
mpsc::unbounded::<InspectorInfo>();
|
|
|
|
|
2023-12-23 10:46:09 -05:00
|
|
|
let (shutdown_server_tx, shutdown_server_rx) = broadcast::channel(1);
|
2021-05-26 11:47:33 -04:00
|
|
|
|
2024-04-11 19:17:10 -04:00
|
|
|
let tcp_listener =
|
|
|
|
std::net::TcpListener::bind(host).with_context(|| {
|
|
|
|
format!("Failed to start inspector server at \"{}\"", host)
|
|
|
|
})?;
|
|
|
|
tcp_listener.set_nonblocking(true)?;
|
|
|
|
|
2021-05-26 11:47:33 -04:00
|
|
|
let thread_handle = thread::spawn(move || {
|
|
|
|
let rt = crate::tokio_util::create_basic_runtime();
|
|
|
|
let local = tokio::task::LocalSet::new();
|
|
|
|
local.block_on(
|
|
|
|
&rt,
|
2024-04-11 19:17:10 -04:00
|
|
|
server(
|
|
|
|
tcp_listener,
|
|
|
|
register_inspector_rx,
|
|
|
|
shutdown_server_rx,
|
|
|
|
name,
|
|
|
|
),
|
2021-05-26 11:47:33 -04:00
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2024-04-11 19:17:10 -04:00
|
|
|
Ok(Self {
|
2021-05-26 11:47:33 -04:00
|
|
|
host,
|
|
|
|
register_inspector_tx,
|
|
|
|
shutdown_server_tx: Some(shutdown_server_tx),
|
|
|
|
thread_handle: Some(thread_handle),
|
2024-04-11 19:17:10 -04:00
|
|
|
})
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_inspector(
|
|
|
|
&self,
|
2021-06-30 12:01:11 -04:00
|
|
|
module_url: String,
|
2021-08-25 07:39:23 -04:00
|
|
|
js_runtime: &mut JsRuntime,
|
2022-12-12 09:33:30 -05:00
|
|
|
wait_for_session: bool,
|
2021-05-26 11:47:33 -04:00
|
|
|
) {
|
2022-09-02 06:43:39 -04:00
|
|
|
let inspector_rc = js_runtime.inspector();
|
|
|
|
let mut inspector = inspector_rc.borrow_mut();
|
2021-08-25 07:39:23 -04:00
|
|
|
let session_sender = inspector.get_session_sender();
|
|
|
|
let deregister_rx = inspector.add_deregister_handler();
|
2021-12-17 12:43:25 -05:00
|
|
|
let info = InspectorInfo::new(
|
|
|
|
self.host,
|
|
|
|
session_sender,
|
|
|
|
deregister_rx,
|
|
|
|
module_url,
|
2022-12-12 09:33:30 -05:00
|
|
|
wait_for_session,
|
2021-12-17 12:43:25 -05:00
|
|
|
);
|
2021-05-26 11:47:33 -04:00
|
|
|
self.register_inspector_tx.unbounded_send(info).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for InspectorServer {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(shutdown_server_tx) = self.shutdown_server_tx.take() {
|
|
|
|
shutdown_server_tx
|
|
|
|
.send(())
|
|
|
|
.expect("unable to send shutdown signal");
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(thread_handle) = self.thread_handle.take() {
|
|
|
|
thread_handle.join().expect("unable to join thread");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_ws_request(
|
2023-12-27 11:59:57 -05:00
|
|
|
req: http::Request<hyper::body::Incoming>,
|
2021-08-25 07:39:23 -04:00
|
|
|
inspector_map_rc: Rc<RefCell<HashMap<Uuid, InspectorInfo>>>,
|
2023-12-27 11:59:57 -05:00
|
|
|
) -> http::Result<http::Response<Box<http_body_util::Full<Bytes>>>> {
|
2021-05-26 11:47:33 -04:00
|
|
|
let (parts, body) = req.into_parts();
|
2023-12-27 11:59:57 -05:00
|
|
|
let req = http::Request::from_parts(parts, ());
|
2021-05-26 11:47:33 -04:00
|
|
|
|
2021-08-25 07:39:23 -04:00
|
|
|
let maybe_uuid = req
|
2021-05-26 11:47:33 -04:00
|
|
|
.uri()
|
|
|
|
.path()
|
|
|
|
.strip_prefix("/ws/")
|
2021-08-25 07:39:23 -04:00
|
|
|
.and_then(|s| Uuid::parse_str(s).ok());
|
|
|
|
|
|
|
|
if maybe_uuid.is_none() {
|
2023-12-27 11:59:57 -05:00
|
|
|
return http::Response::builder()
|
|
|
|
.status(http::StatusCode::BAD_REQUEST)
|
2023-12-23 10:46:09 -05:00
|
|
|
.body(Box::new(Bytes::from("Malformed inspector UUID").into()));
|
2021-08-25 07:39:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// run in a block to not hold borrow to `inspector_map` for too long
|
|
|
|
let new_session_tx = {
|
|
|
|
let inspector_map = inspector_map_rc.borrow();
|
|
|
|
let maybe_inspector_info = inspector_map.get(&maybe_uuid.unwrap());
|
|
|
|
|
|
|
|
if maybe_inspector_info.is_none() {
|
2023-12-27 11:59:57 -05:00
|
|
|
return http::Response::builder()
|
|
|
|
.status(http::StatusCode::NOT_FOUND)
|
2023-12-23 10:46:09 -05:00
|
|
|
.body(Box::new(Bytes::from("Invalid inspector UUID").into()));
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
2021-08-25 07:39:23 -04:00
|
|
|
|
|
|
|
let info = maybe_inspector_info.unwrap();
|
|
|
|
info.new_session_tx.clone()
|
|
|
|
};
|
2023-04-22 05:17:31 -04:00
|
|
|
let (parts, _) = req.into_parts();
|
2023-12-27 11:59:57 -05:00
|
|
|
let mut req = http::Request::from_parts(parts, body);
|
2023-12-23 10:46:09 -05:00
|
|
|
|
2023-12-26 15:53:28 -05:00
|
|
|
let (resp, fut) = match fastwebsockets::upgrade::upgrade(&mut req) {
|
2023-12-23 10:46:09 -05:00
|
|
|
Ok((resp, fut)) => {
|
|
|
|
let (parts, _body) = resp.into_parts();
|
2023-12-27 11:59:57 -05:00
|
|
|
let resp = http::Response::from_parts(
|
2023-12-23 10:46:09 -05:00
|
|
|
parts,
|
|
|
|
Box::new(http_body_util::Full::new(Bytes::new())),
|
|
|
|
);
|
|
|
|
(resp, fut)
|
|
|
|
}
|
2023-04-22 05:17:31 -04:00
|
|
|
_ => {
|
2023-12-27 11:59:57 -05:00
|
|
|
return http::Response::builder()
|
|
|
|
.status(http::StatusCode::BAD_REQUEST)
|
2023-12-23 10:46:09 -05:00
|
|
|
.body(Box::new(
|
|
|
|
Bytes::from("Not a valid Websocket Request").into(),
|
|
|
|
));
|
2023-04-22 05:17:31 -04:00
|
|
|
}
|
|
|
|
};
|
2021-08-25 07:39:23 -04:00
|
|
|
|
|
|
|
// spawn a task that will wait for websocket connection and then pump messages between
|
|
|
|
// the socket and inspector proxy
|
2023-05-14 17:40:01 -04:00
|
|
|
spawn(async move {
|
2023-12-23 10:46:09 -05:00
|
|
|
let websocket = match fut.await {
|
|
|
|
Ok(w) => w,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!(
|
|
|
|
"Inspector server failed to upgrade to WS connection: {:?}",
|
|
|
|
err
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-08-25 07:39:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// The 'outbound' channel carries messages sent to the websocket.
|
|
|
|
let (outbound_tx, outbound_rx) = mpsc::unbounded();
|
|
|
|
// The 'inbound' channel carries messages received from the websocket.
|
|
|
|
let (inbound_tx, inbound_rx) = mpsc::unbounded();
|
|
|
|
|
|
|
|
let inspector_session_proxy = InspectorSessionProxy {
|
|
|
|
tx: outbound_tx,
|
|
|
|
rx: inbound_rx,
|
|
|
|
};
|
|
|
|
|
|
|
|
eprintln!("Debugger session started.");
|
|
|
|
let _ = new_session_tx.unbounded_send(inspector_session_proxy);
|
|
|
|
pump_websocket_messages(websocket, inbound_tx, outbound_rx).await;
|
|
|
|
});
|
|
|
|
|
2022-01-15 01:10:12 -05:00
|
|
|
Ok(resp)
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_json_request(
|
|
|
|
inspector_map: Rc<RefCell<HashMap<Uuid, InspectorInfo>>>,
|
2023-08-15 18:30:33 -04:00
|
|
|
host: Option<String>,
|
2023-12-27 11:59:57 -05:00
|
|
|
) -> http::Result<http::Response<Box<http_body_util::Full<Bytes>>>> {
|
2021-05-26 11:47:33 -04:00
|
|
|
let data = inspector_map
|
|
|
|
.borrow()
|
|
|
|
.values()
|
2023-08-15 18:30:33 -04:00
|
|
|
.map(move |info| info.get_json_metadata(&host))
|
2021-05-26 11:47:33 -04:00
|
|
|
.collect::<Vec<_>>();
|
2023-12-23 10:46:09 -05:00
|
|
|
let body: http_body_util::Full<Bytes> =
|
|
|
|
Bytes::from(serde_json::to_string(&data).unwrap()).into();
|
2023-12-27 11:59:57 -05:00
|
|
|
http::Response::builder()
|
|
|
|
.status(http::StatusCode::OK)
|
|
|
|
.header(http::header::CONTENT_TYPE, "application/json")
|
2023-12-23 10:46:09 -05:00
|
|
|
.body(Box::new(body))
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_json_version_request(
|
|
|
|
version_response: Value,
|
2023-12-27 11:59:57 -05:00
|
|
|
) -> http::Result<http::Response<Box<http_body_util::Full<Bytes>>>> {
|
2023-12-23 10:46:09 -05:00
|
|
|
let body = Box::new(http_body_util::Full::from(
|
|
|
|
serde_json::to_string(&version_response).unwrap(),
|
|
|
|
));
|
|
|
|
|
2023-12-27 11:59:57 -05:00
|
|
|
http::Response::builder()
|
|
|
|
.status(http::StatusCode::OK)
|
|
|
|
.header(http::header::CONTENT_TYPE, "application/json")
|
2023-12-23 10:46:09 -05:00
|
|
|
.body(body)
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn server(
|
2024-04-11 19:17:10 -04:00
|
|
|
listener: std::net::TcpListener,
|
2021-05-26 11:47:33 -04:00
|
|
|
register_inspector_rx: UnboundedReceiver<InspectorInfo>,
|
2023-12-23 10:46:09 -05:00
|
|
|
shutdown_server_rx: broadcast::Receiver<()>,
|
2023-03-23 18:27:58 -04:00
|
|
|
name: &str,
|
2021-05-26 11:47:33 -04:00
|
|
|
) {
|
|
|
|
let inspector_map_ =
|
|
|
|
Rc::new(RefCell::new(HashMap::<Uuid, InspectorInfo>::new()));
|
|
|
|
|
|
|
|
let inspector_map = Rc::clone(&inspector_map_);
|
2023-03-09 21:28:51 -05:00
|
|
|
let mut register_inspector_handler = pin!(register_inspector_rx
|
2021-05-26 11:47:33 -04:00
|
|
|
.map(|info| {
|
|
|
|
eprintln!(
|
|
|
|
"Debugger listening on {}",
|
2023-08-15 18:30:33 -04:00
|
|
|
info.get_websocket_debugger_url(&info.host.to_string())
|
2021-05-26 11:47:33 -04:00
|
|
|
);
|
2021-12-17 12:43:25 -05:00
|
|
|
eprintln!("Visit chrome://inspect to connect to the debugger.");
|
2022-12-12 09:33:30 -05:00
|
|
|
if info.wait_for_session {
|
2021-12-17 12:43:25 -05:00
|
|
|
eprintln!("Deno is waiting for debugger to connect.");
|
|
|
|
}
|
2021-05-26 11:47:33 -04:00
|
|
|
if inspector_map.borrow_mut().insert(info.uuid, info).is_some() {
|
|
|
|
panic!("Inspector UUID already in map");
|
|
|
|
}
|
|
|
|
})
|
2023-03-09 21:28:51 -05:00
|
|
|
.collect::<()>());
|
2021-05-26 11:47:33 -04:00
|
|
|
|
|
|
|
let inspector_map = Rc::clone(&inspector_map_);
|
2023-03-09 21:28:51 -05:00
|
|
|
let mut deregister_inspector_handler = pin!(future::poll_fn(|cx| {
|
2021-05-26 11:47:33 -04:00
|
|
|
inspector_map
|
|
|
|
.borrow_mut()
|
|
|
|
.retain(|_, info| info.deregister_rx.poll_unpin(cx) == Poll::Pending);
|
|
|
|
Poll::<Never>::Pending
|
|
|
|
})
|
2023-03-09 21:28:51 -05:00
|
|
|
.fuse());
|
2021-05-26 11:47:33 -04:00
|
|
|
|
|
|
|
let json_version_response = json!({
|
|
|
|
"Browser": name,
|
|
|
|
"Protocol-Version": "1.3",
|
|
|
|
"V8-Version": deno_core::v8_version(),
|
|
|
|
});
|
|
|
|
|
2023-12-23 10:46:09 -05:00
|
|
|
// Create the server manually so it can use the Local Executor
|
2024-04-11 19:17:10 -04:00
|
|
|
let listener = match TcpListener::from_std(listener) {
|
2023-12-23 10:46:09 -05:00
|
|
|
Ok(l) => l,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Cannot start inspector server: {:?}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut server_handler = pin!(deno_core::unsync::spawn(async move {
|
|
|
|
loop {
|
|
|
|
let mut rx = shutdown_server_rx.resubscribe();
|
|
|
|
let mut shutdown_rx = pin!(rx.recv());
|
|
|
|
let mut accept = pin!(listener.accept());
|
|
|
|
|
|
|
|
let stream = tokio::select! {
|
|
|
|
accept_result = &mut accept => {
|
|
|
|
match accept_result {
|
|
|
|
Ok((s, _)) => s,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Failed to accept inspector connection: {:?}", err);
|
|
|
|
continue;
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
2023-12-23 10:46:09 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ = &mut shutdown_rx => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let io = TokioIo::new(stream);
|
|
|
|
|
|
|
|
let inspector_map = Rc::clone(&inspector_map_);
|
|
|
|
let json_version_response = json_version_response.clone();
|
|
|
|
let mut shutdown_server_rx = shutdown_server_rx.resubscribe();
|
|
|
|
|
2023-12-27 11:59:57 -05:00
|
|
|
let service = hyper::service::service_fn(
|
|
|
|
move |req: http::Request<hyper::body::Incoming>| {
|
2023-12-23 10:46:09 -05:00
|
|
|
future::ready({
|
|
|
|
// If the host header can make a valid URL, use it
|
|
|
|
let host = req
|
|
|
|
.headers()
|
|
|
|
.get("host")
|
|
|
|
.and_then(|host| host.to_str().ok())
|
|
|
|
.and_then(|host| Url::parse(&format!("http://{host}")).ok())
|
|
|
|
.and_then(|url| match (url.host(), url.port()) {
|
|
|
|
(Some(host), Some(port)) => Some(format!("{host}:{port}")),
|
|
|
|
(Some(host), None) => Some(format!("{host}")),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
match (req.method(), req.uri().path()) {
|
2023-12-27 11:59:57 -05:00
|
|
|
(&http::Method::GET, path) if path.starts_with("/ws/") => {
|
2023-12-23 10:46:09 -05:00
|
|
|
handle_ws_request(req, Rc::clone(&inspector_map))
|
|
|
|
}
|
2023-12-27 11:59:57 -05:00
|
|
|
(&http::Method::GET, "/json/version") => {
|
2023-12-23 10:46:09 -05:00
|
|
|
handle_json_version_request(json_version_response.clone())
|
|
|
|
}
|
2023-12-27 11:59:57 -05:00
|
|
|
(&http::Method::GET, "/json") => {
|
2023-12-23 10:46:09 -05:00
|
|
|
handle_json_request(Rc::clone(&inspector_map), host)
|
|
|
|
}
|
2023-12-27 11:59:57 -05:00
|
|
|
(&http::Method::GET, "/json/list") => {
|
2023-12-23 10:46:09 -05:00
|
|
|
handle_json_request(Rc::clone(&inspector_map), host)
|
|
|
|
}
|
2023-12-27 11:59:57 -05:00
|
|
|
_ => http::Response::builder()
|
|
|
|
.status(http::StatusCode::NOT_FOUND)
|
2023-12-23 10:46:09 -05:00
|
|
|
.body(Box::new(http_body_util::Full::new(Bytes::from(
|
|
|
|
"Not Found",
|
|
|
|
)))),
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
2023-12-23 10:46:09 -05:00
|
|
|
})
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
deno_core::unsync::spawn(async move {
|
2023-12-27 11:59:57 -05:00
|
|
|
let server = hyper::server::conn::http1::Builder::new();
|
2023-12-23 10:46:09 -05:00
|
|
|
|
|
|
|
let mut conn =
|
|
|
|
pin!(server.serve_connection(io, service).with_upgrades());
|
|
|
|
let mut shutdown_rx = pin!(shutdown_server_rx.recv());
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
result = conn.as_mut() => {
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("Failed to serve connection: {:?}", err);
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
2023-12-23 10:46:09 -05:00
|
|
|
},
|
|
|
|
_ = &mut shutdown_rx => {
|
|
|
|
conn.as_mut().graceful_shutdown();
|
|
|
|
let _ = conn.await;
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
2023-12-23 10:46:09 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-05-26 11:47:33 -04:00
|
|
|
})
|
2023-03-09 21:28:51 -05:00
|
|
|
.fuse());
|
2021-05-26 11:47:33 -04:00
|
|
|
|
|
|
|
select! {
|
|
|
|
_ = register_inspector_handler => {},
|
|
|
|
_ = deregister_inspector_handler => unreachable!(),
|
|
|
|
_ = server_handler => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 07:39:23 -04:00
|
|
|
/// The pump future takes care of forwarding messages between the websocket
|
|
|
|
/// and channels. It resolves when either side disconnects, ignoring any
|
|
|
|
/// errors.
|
|
|
|
///
|
|
|
|
/// The future proxies messages sent and received on a warp WebSocket
|
|
|
|
/// to a UnboundedSender/UnboundedReceiver pair. We need these "unbounded" channel ends to sidestep
|
2021-05-26 11:47:33 -04:00
|
|
|
/// Tokio's task budget, which causes issues when JsRuntimeInspector::poll_sessions()
|
|
|
|
/// needs to block the thread because JavaScript execution is paused.
|
|
|
|
///
|
|
|
|
/// This works because UnboundedSender/UnboundedReceiver are implemented in the
|
|
|
|
/// 'futures' crate, therefore they can't participate in Tokio's cooperative
|
|
|
|
/// task yielding.
|
2021-08-25 07:39:23 -04:00
|
|
|
async fn pump_websocket_messages(
|
2023-12-27 11:59:57 -05:00
|
|
|
mut websocket: WebSocket<TokioIo<hyper::upgrade::Upgraded>>,
|
2021-12-28 11:40:42 -05:00
|
|
|
inbound_tx: UnboundedSender<String>,
|
2023-04-22 05:17:31 -04:00
|
|
|
mut outbound_rx: UnboundedReceiver<InspectorMsg>,
|
2021-08-25 07:39:23 -04:00
|
|
|
) {
|
2023-04-22 05:17:31 -04:00
|
|
|
'pump: loop {
|
|
|
|
tokio::select! {
|
|
|
|
Some(msg) = outbound_rx.next() => {
|
2023-08-10 00:29:06 -04:00
|
|
|
let msg = Frame::text(msg.content.into_bytes().into());
|
2023-04-22 05:17:31 -04:00
|
|
|
let _ = websocket.write_frame(msg).await;
|
2021-12-28 11:40:42 -05:00
|
|
|
}
|
2023-04-22 05:17:31 -04:00
|
|
|
Ok(msg) = websocket.read_frame() => {
|
|
|
|
match msg.opcode {
|
|
|
|
OpCode::Text => {
|
2023-08-10 00:29:06 -04:00
|
|
|
if let Ok(s) = String::from_utf8(msg.payload.to_vec()) {
|
2023-04-22 05:17:31 -04:00
|
|
|
let _ = inbound_tx.unbounded_send(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OpCode::Close => {
|
|
|
|
// Users don't care if there was an error coming from debugger,
|
|
|
|
// just about the fact that debugger did disconnect.
|
|
|
|
eprintln!("Debugger session ended");
|
|
|
|
break 'pump;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Ignore other messages.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-06 17:33:14 -04:00
|
|
|
else => {
|
|
|
|
break 'pump;
|
|
|
|
}
|
2023-04-22 05:17:31 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Inspector information that is sent from the isolate thread to the server
|
|
|
|
/// thread when a new inspector is created.
|
|
|
|
pub struct InspectorInfo {
|
|
|
|
pub host: SocketAddr,
|
|
|
|
pub uuid: Uuid,
|
|
|
|
pub thread_name: Option<String>,
|
2021-05-26 15:07:12 -04:00
|
|
|
pub new_session_tx: UnboundedSender<InspectorSessionProxy>,
|
2021-05-26 11:47:33 -04:00
|
|
|
pub deregister_rx: oneshot::Receiver<()>,
|
2021-06-30 12:01:11 -04:00
|
|
|
pub url: String,
|
2022-12-12 09:33:30 -05:00
|
|
|
pub wait_for_session: bool,
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InspectorInfo {
|
|
|
|
pub fn new(
|
|
|
|
host: SocketAddr,
|
2021-05-26 15:07:12 -04:00
|
|
|
new_session_tx: mpsc::UnboundedSender<InspectorSessionProxy>,
|
2021-05-26 11:47:33 -04:00
|
|
|
deregister_rx: oneshot::Receiver<()>,
|
2021-06-30 12:01:11 -04:00
|
|
|
url: String,
|
2022-12-12 09:33:30 -05:00
|
|
|
wait_for_session: bool,
|
2021-05-26 11:47:33 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
host,
|
|
|
|
uuid: Uuid::new_v4(),
|
|
|
|
thread_name: thread::current().name().map(|n| n.to_owned()),
|
|
|
|
new_session_tx,
|
|
|
|
deregister_rx,
|
2021-06-30 12:01:11 -04:00
|
|
|
url,
|
2022-12-12 09:33:30 -05:00
|
|
|
wait_for_session,
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 18:30:33 -04:00
|
|
|
fn get_json_metadata(&self, host: &Option<String>) -> Value {
|
|
|
|
let host_listen = format!("{}", self.host);
|
|
|
|
let host = host.as_ref().unwrap_or(&host_listen);
|
2021-05-26 11:47:33 -04:00
|
|
|
json!({
|
|
|
|
"description": "deno",
|
2023-08-15 18:30:33 -04:00
|
|
|
"devtoolsFrontendUrl": self.get_frontend_url(host),
|
2021-05-26 11:47:33 -04:00
|
|
|
"faviconUrl": "https://deno.land/favicon.ico",
|
|
|
|
"id": self.uuid.to_string(),
|
|
|
|
"title": self.get_title(),
|
|
|
|
"type": "node",
|
2021-06-30 12:01:11 -04:00
|
|
|
"url": self.url.to_string(),
|
2023-08-15 18:30:33 -04:00
|
|
|
"webSocketDebuggerUrl": self.get_websocket_debugger_url(host),
|
2021-05-26 11:47:33 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-15 18:30:33 -04:00
|
|
|
pub fn get_websocket_debugger_url(&self, host: &str) -> String {
|
|
|
|
format!("ws://{}/ws/{}", host, &self.uuid)
|
2021-05-26 11:47:33 -04:00
|
|
|
}
|
|
|
|
|
2023-08-15 18:30:33 -04:00
|
|
|
fn get_frontend_url(&self, host: &str) -> String {
|
2021-05-26 11:47:33 -04:00
|
|
|
format!(
|
|
|
|
"devtools://devtools/bundled/js_app.html?ws={}/ws/{}&experiments=true&v8only=true",
|
2023-08-15 18:30:33 -04:00
|
|
|
host, &self.uuid
|
2021-05-26 11:47:33 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_title(&self) -> String {
|
|
|
|
format!(
|
2021-06-30 12:01:11 -04:00
|
|
|
"deno{} [pid: {}]",
|
2021-05-26 11:47:33 -04:00
|
|
|
self
|
|
|
|
.thread_name
|
|
|
|
.as_ref()
|
2023-01-27 10:43:16 -05:00
|
|
|
.map(|n| format!(" - {n}"))
|
2021-06-30 12:01:11 -04:00
|
|
|
.unwrap_or_default(),
|
|
|
|
process::id(),
|
2021-05-26 11:47:33 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|