Skip to content

Commit

Permalink
feat: Support Inspector / Chrome Devtools (denoland#4484)
Browse files Browse the repository at this point in the history
This is a first pass implementation which is still missing several important
features:
- support for --inspect-brk (denoland#4503)
- support for source maps (denoland#4501)
- support for piping console.log to devtools console (denoland#4502)

Co-authored-by: Bert Belder <[email protected]>
Co-authored-by: Matt Harrison <[email protected]>
Co-authored-by: Bartek Iwańczuk <[email protected]>
  • Loading branch information
4 people committed Mar 27, 2020
1 parent 8bcdb42 commit 2874664
Show file tree
Hide file tree
Showing 12 changed files with 1,153 additions and 37 deletions.
390 changes: 363 additions & 27 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ utime = "0.2.1"
webpki = "0.21.2"
webpki-roots = "0.19.0"
walkdir = "2.3.1"
warp = "0.2.2"
semver-parser = "0.9.0"
uuid = { version = "0.8", features = ["v4"] }


[target.'cfg(windows)'.dependencies]
winapi = "0.3.8"
Expand All @@ -72,6 +75,8 @@ nix = "0.14" # rustyline depends on 0.14, to avoid duplicates we do too.

[dev-dependencies]
os_pipe = "0.9.1"
# Used for testing inspector. Keep in-sync with warp.
tokio-tungstenite = { version = "0.10", features = ["connect"] }

[target.'cfg(unix)'.dev-dependencies]
pty = "0.2.2"
88 changes: 87 additions & 1 deletion cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub struct Flags {
pub no_prompts: bool,
pub no_remote: bool,
pub cached_only: bool,
pub inspect: Option<String>,
pub inspect_brk: Option<String>,
pub seed: Option<u64>,
pub v8_flags: Option<Vec<String>>,

Expand Down Expand Up @@ -474,6 +476,7 @@ fn run_test_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
no_remote_arg_parse(flags, matches);
permission_args_parse(flags, matches);
ca_file_arg_parse(flags, matches);
inspect_arg_parse(flags, matches);

if matches.is_present("cached-only") {
flags.cached_only = true;
Expand Down Expand Up @@ -825,7 +828,7 @@ fn permission_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
}

fn run_test_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
permission_args(app)
permission_args(inspect_args(app))
.arg(importmap_arg())
.arg(reload_arg())
.arg(config_arg())
Expand Down Expand Up @@ -956,6 +959,54 @@ fn ca_file_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
flags.ca_file = matches.value_of("cert").map(ToOwned::to_owned);
}

fn inspect_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
app
.arg(
Arg::with_name("inspect")
.long("inspect")
.value_name("HOST:PORT")
.help("activate inspector on host:port (default: 127.0.0.1:9229)")
.min_values(0)
.max_values(1)
.require_equals(true)
.takes_value(true),
)
.arg(
Arg::with_name("inspect-brk")
.long("inspect-brk")
.value_name("HOST:PORT")
.help(
"activate inspector on host:port and break at start of user script",
)
.min_values(0)
.max_values(1)
.require_equals(true)
.takes_value(true),
)
}

fn inspect_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
const DEFAULT: &str = "127.0.0.1:9229";
flags.inspect = if matches.is_present("inspect") {
if let Some(host) = matches.value_of("inspect") {
Some(host.to_string())
} else {
Some(DEFAULT.to_string())
}
} else {
None
};
flags.inspect_brk = if matches.is_present("inspect-brk") {
if let Some(host) = matches.value_of("inspect-brk") {
Some(host.to_string())
} else {
Some(DEFAULT.to_string())
}
} else {
None
};
}

fn reload_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name("reload")
.short("r")
Expand Down Expand Up @@ -2327,3 +2378,38 @@ fn repl_with_cafile() {
}
);
}

#[test]
fn inspect_default_host() {
let r = flags_from_vec_safe(svec!["deno", "run", "--inspect", "foo.js"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Run {
script: "foo.js".to_string(),
},
inspect: Some("127.0.0.1:9229".to_string()),
..Flags::default()
}
);
}

#[test]
fn inspect_custom_host() {
let r = flags_from_vec_safe(svec![
"deno",
"run",
"--inspect=deno.land:80",
"foo.js"
]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Run {
script: "foo.js".to_string(),
},
inspect: Some("deno.land:80".to_string()),
..Flags::default()
}
);
}
11 changes: 11 additions & 0 deletions cli/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::deno_dir;
use crate::file_fetcher::SourceFileFetcher;
use crate::flags;
use crate::http_cache;
use crate::inspector::InspectorServer;
use crate::lockfile::Lockfile;
use crate::msg;
use crate::permissions::DenoPermissions;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct GlobalStateInner {
pub wasm_compiler: WasmCompiler,
pub lockfile: Option<Mutex<Lockfile>>,
pub compiler_starts: AtomicUsize,
pub inspector_server: Option<InspectorServer>,
compile_lock: AsyncMutex<()>,
}

Expand Down Expand Up @@ -82,7 +84,16 @@ impl GlobalState {
None
};

let inspector_server = if let Some(ref host) = flags.inspect {
Some(InspectorServer::new(host, false))
} else if let Some(ref host) = flags.inspect_brk {
Some(InspectorServer::new(host, true))
} else {
None
};

let inner = GlobalStateInner {
inspector_server,
dir,
permissions: DenoPermissions::from_flags(&flags),
flags,
Expand Down
Loading

0 comments on commit 2874664

Please sign in to comment.