Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

[dev] fix windows builds and timeouts #991

Merged
merged 11 commits into from
Jan 29, 2020
151 changes: 146 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cloudflare = "0.4.1"
env_logger = "0.6.1"
failure = "0.1.5"
log = "0.4.6"
openssl = "0.10.26"
openssl = { version = "0.10.26", optional = true }
reqwest = "0.9.18"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.39"
Expand Down Expand Up @@ -50,13 +50,18 @@ indicatif = "0.13.0"
hyper = "0.13.1"
hyper-tls = "0.4.0"
chrono = "0.4.9"
tokio = "0.2.0"
tokio = {version = "0.2", default-features = false, features = ["io-std", "time"]}
# chrome-devtools-rs = { path = "../chrome-devtools-rs" }
chrome-devtools-rs = { git = "https://github.com/everlastingbugstopper/chrome-devtools-rs", rev = "b7a0e9f" }
chrome-devtools-rs = { git = "https://github.com/everlastingbugstopper/chrome-devtools-rs", rev = "cb63ee7" }
ws = "0.9.0"
futures = "0.3"
futures-util = "0.3"
tungstenite = {version = "0.9.2", default-features = false}

[dependencies.ws]
version = "0.9.0"
features = ["ssl"]
[dependencies.tokio-tungstenite]
git = "https://github.com/snapview/tokio-tungstenite"
rev = "adb4f43"
features = ["tls"]

[dev-dependencies]
assert_cmd = "0.11.1"
Expand Down
18 changes: 10 additions & 8 deletions src/commands/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use server_config::ServerConfig;
mod headers;
use headers::{destructure_response, structure_request};

use std::thread;

use chrono::prelude::*;

use hyper::client::{HttpConnector, ResponseFuture};
Expand Down Expand Up @@ -43,14 +41,18 @@ pub fn dev(
let session_id = get_session_id()?;
let preview_id = get_preview_id(target, user, &server_config, &session_id)?;

// create a new thread to listen for devtools messages
thread::spawn(move || socket::listen(session_id));

// spawn tokio runtime on the main thread to handle incoming HTTP requests
let mut runtime = TokioRuntime::new()?;
runtime.block_on(serve(server_config, preview_id))?;

Ok(())
let devtools_listener = socket::listen(session_id);
let server = serve(server_config, preview_id);

let runners = futures::future::join(devtools_listener, server);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎊


runtime.block_on(async {
let (devtools_listener, server) = runners.await;
devtools_listener?;
server
})
}

async fn serve(server_config: ServerConfig, preview_id: String) -> Result<(), failure::Error> {
Expand Down
86 changes: 86 additions & 0 deletions src/commands/dev/socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::time::Duration;

use chrome_devtools as protocol;

use console::style;

use futures::{future, pin_mut, StreamExt};
use futures_util::sink::SinkExt;

use tokio::time;
use tokio_tungstenite::connect_async;
use tungstenite::protocol::Message;

use url::Url;

const KEEP_ALIVE_INTERVAL: u64 = 10;

pub async fn listen(session_id: String) -> Result<(), failure::Error> {
let socket_url = format!("wss://rawhttp.cloudflareworkers.com/inspect/{}", session_id);
let socket_url = Url::parse(&socket_url)?;

let (ws_stream, _) = connect_async(socket_url)
.await
.expect("Failed to connect to devtools instance");

let (mut write, read) = ws_stream.split();

let enable_runtime = protocol::runtime::SendMethod::Enable(1.into());
let enable_runtime = serde_json::to_string(&enable_runtime)?;
let enable_runtime = Message::Text(enable_runtime.into());
write.send(enable_runtime).await?;

let (keep_alive_tx, keep_alive_rx) = futures::channel::mpsc::unbounded();
tokio::spawn(keep_alive(keep_alive_tx));
let keep_alive_to_ws = keep_alive_rx.map(Ok).forward(write);

let print_ws_messages = {
read.for_each(|message| {
async {
let message = message.unwrap().into_text().unwrap();
log::info!("{}", message);
let message: Result<protocol::Runtime, failure::Error> =
serde_json::from_str(&message).map_err(|e| {
failure::format_err!("this event could not be parsed:\n{}", e)
});
if let Ok(protocol::Runtime::Event(event)) = message {
match event {
protocol::runtime::Event::ConsoleAPICalled(event) => {
match event.r#type.as_str() {
"log" => println!("{}", style(event).green()),
"error" => println!("{}", style(event).red()),
_ => println!("{}", style(event).yellow()),
}
}
protocol::runtime::Event::ExceptionThrown(event) => {
println!("{}", style(event).bold().red())
}
_ => (),
}
}
}
})
};
pin_mut!(keep_alive_to_ws, print_ws_messages);
future::select(keep_alive_to_ws, print_ws_messages).await;
Ok(())
}

async fn keep_alive(tx: futures::channel::mpsc::UnboundedSender<Message>) -> ! {
let duration = Duration::from_millis(1000 * KEEP_ALIVE_INTERVAL);
let mut interval = time::interval(duration);

// this is set to 2 because we have already sent an id of 1 to enable the runtime
// eventually this logic should be moved to the chrome-devtools-rs library
let mut id = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am interested why this is 2. not because it's wrong, but it feels unusual! Might deserve a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point - the reason it's 2 is because we have already sent id = 1 earlier to enable the runtime. Eventually I plan on making the ID handling automatic and in the chrome-devtools-rs library.


loop {
interval.tick().await;
let keep_alive_message = protocol::runtime::SendMethod::GetIsolateId(id.into());
let keep_alive_message = serde_json::to_string(&keep_alive_message)
.expect("Could not convert keep alive message to JSON");
let keep_alive_message = Message::Text(keep_alive_message.into());
tx.unbounded_send(keep_alive_message).unwrap();
id += 1;
}
}
85 changes: 0 additions & 85 deletions src/commands/dev/socket/client.rs

This file was deleted.

9 changes: 0 additions & 9 deletions src/commands/dev/socket/mod.rs

This file was deleted.