mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
refactor(cli): rename GlobalState to ProgramState (#7914)
This commit is contained in:
parent
000ac5c40b
commit
0bd3cea0ff
15 changed files with 187 additions and 186 deletions
82
cli/main.rs
82
cli/main.rs
|
@ -23,7 +23,6 @@ mod flags_allow_net;
|
||||||
mod fmt;
|
mod fmt;
|
||||||
mod fmt_errors;
|
mod fmt_errors;
|
||||||
mod fs;
|
mod fs;
|
||||||
mod global_state;
|
|
||||||
mod global_timer;
|
mod global_timer;
|
||||||
mod http_cache;
|
mod http_cache;
|
||||||
mod http_util;
|
mod http_util;
|
||||||
|
@ -38,15 +37,16 @@ mod media_type;
|
||||||
mod metrics;
|
mod metrics;
|
||||||
mod module_graph;
|
mod module_graph;
|
||||||
mod module_graph2;
|
mod module_graph2;
|
||||||
|
mod module_loader;
|
||||||
mod op_fetch_asset;
|
mod op_fetch_asset;
|
||||||
mod ops;
|
mod ops;
|
||||||
mod permissions;
|
mod permissions;
|
||||||
|
mod program_state;
|
||||||
mod repl;
|
mod repl;
|
||||||
mod resolve_addr;
|
mod resolve_addr;
|
||||||
mod signal;
|
mod signal;
|
||||||
mod source_maps;
|
mod source_maps;
|
||||||
mod specifier_handler;
|
mod specifier_handler;
|
||||||
mod state;
|
|
||||||
mod test_runner;
|
mod test_runner;
|
||||||
mod text_encoding;
|
mod text_encoding;
|
||||||
mod tokio_util;
|
mod tokio_util;
|
||||||
|
@ -61,9 +61,9 @@ use crate::coverage::PrettyCoverageReporter;
|
||||||
use crate::file_fetcher::SourceFile;
|
use crate::file_fetcher::SourceFile;
|
||||||
use crate::file_fetcher::SourceFileFetcher;
|
use crate::file_fetcher::SourceFileFetcher;
|
||||||
use crate::fs as deno_fs;
|
use crate::fs as deno_fs;
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::media_type::MediaType;
|
use crate::media_type::MediaType;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
|
use crate::program_state::ProgramState;
|
||||||
use crate::worker::MainWorker;
|
use crate::worker::MainWorker;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::futures::future::FutureExt;
|
use deno_core::futures::future::FutureExt;
|
||||||
|
@ -77,10 +77,10 @@ use deno_doc as doc;
|
||||||
use deno_doc::parser::DocFileLoader;
|
use deno_doc::parser::DocFileLoader;
|
||||||
use flags::DenoSubcommand;
|
use flags::DenoSubcommand;
|
||||||
use flags::Flags;
|
use flags::Flags;
|
||||||
use global_state::exit_unstable;
|
|
||||||
use import_map::ImportMap;
|
use import_map::ImportMap;
|
||||||
use log::Level;
|
use log::Level;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
|
use program_state::exit_unstable;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
@ -113,7 +113,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_cache_info(
|
fn print_cache_info(
|
||||||
state: &Arc<GlobalState>,
|
state: &Arc<ProgramState>,
|
||||||
json: bool,
|
json: bool,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
let deno_dir = &state.dir.root;
|
let deno_dir = &state.dir.root;
|
||||||
|
@ -167,19 +167,19 @@ async fn info_command(
|
||||||
if json && !flags.unstable {
|
if json && !flags.unstable {
|
||||||
exit_unstable("--json");
|
exit_unstable("--json");
|
||||||
}
|
}
|
||||||
let global_state = GlobalState::new(flags)?;
|
let program_state = ProgramState::new(flags)?;
|
||||||
if let Some(specifier) = maybe_specifier {
|
if let Some(specifier) = maybe_specifier {
|
||||||
let specifier = ModuleSpecifier::resolve_url_or_path(&specifier)?;
|
let specifier = ModuleSpecifier::resolve_url_or_path(&specifier)?;
|
||||||
let handler = Rc::new(RefCell::new(specifier_handler::FetchHandler::new(
|
let handler = Rc::new(RefCell::new(specifier_handler::FetchHandler::new(
|
||||||
&global_state,
|
&program_state,
|
||||||
Permissions::allow_all(),
|
Permissions::allow_all(),
|
||||||
)?));
|
)?));
|
||||||
let mut builder = module_graph2::GraphBuilder2::new(
|
let mut builder = module_graph2::GraphBuilder2::new(
|
||||||
handler,
|
handler,
|
||||||
global_state.maybe_import_map.clone(),
|
program_state.maybe_import_map.clone(),
|
||||||
);
|
);
|
||||||
builder.insert(&specifier).await?;
|
builder.insert(&specifier).await?;
|
||||||
let graph = builder.get_graph(&global_state.lockfile)?;
|
let graph = builder.get_graph(&program_state.lockfile)?;
|
||||||
let info = graph.info()?;
|
let info = graph.info()?;
|
||||||
|
|
||||||
if json {
|
if json {
|
||||||
|
@ -190,7 +190,7 @@ async fn info_command(
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
// If it was just "deno info" print location of caches and exit
|
// If it was just "deno info" print location of caches and exit
|
||||||
print_cache_info(&global_state, json)
|
print_cache_info(&program_state, json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,9 +202,9 @@ async fn install_command(
|
||||||
root: Option<PathBuf>,
|
root: Option<PathBuf>,
|
||||||
force: bool,
|
force: bool,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
let global_state = GlobalState::new(flags.clone())?;
|
let program_state = ProgramState::new(flags.clone())?;
|
||||||
let main_module = ModuleSpecifier::resolve_url_or_path(&module_url)?;
|
let main_module = ModuleSpecifier::resolve_url_or_path(&module_url)?;
|
||||||
let mut worker = MainWorker::new(&global_state, main_module.clone());
|
let mut worker = MainWorker::new(&program_state, main_module.clone());
|
||||||
// First, fetch and compile the module; this step ensures that the module exists.
|
// First, fetch and compile the module; this step ensures that the module exists.
|
||||||
worker.preload_module(&main_module).await?;
|
worker.preload_module(&main_module).await?;
|
||||||
installer::install(flags, &module_url, args, name, root, force)
|
installer::install(flags, &module_url, args, name, root, force)
|
||||||
|
@ -235,12 +235,12 @@ async fn cache_command(
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
let main_module =
|
let main_module =
|
||||||
ModuleSpecifier::resolve_url_or_path("./$deno$cache.ts").unwrap();
|
ModuleSpecifier::resolve_url_or_path("./$deno$cache.ts").unwrap();
|
||||||
let global_state = GlobalState::new(flags)?;
|
let program_state = ProgramState::new(flags)?;
|
||||||
let mut worker = MainWorker::new(&global_state, main_module.clone());
|
let mut worker = MainWorker::new(&program_state, main_module.clone());
|
||||||
|
|
||||||
for file in files {
|
for file in files {
|
||||||
let specifier = ModuleSpecifier::resolve_url_or_path(&file)?;
|
let specifier = ModuleSpecifier::resolve_url_or_path(&file)?;
|
||||||
// TODO(bartlomieju): don't use `preload_module` in favor of calling "GlobalState::prepare_module_load()"
|
// TODO(bartlomieju): don't use `preload_module` in favor of calling "ProgramState::prepare_module_load()"
|
||||||
// explicitly? Seems wasteful to create multiple worker just to run TS compiler
|
// explicitly? Seems wasteful to create multiple worker just to run TS compiler
|
||||||
worker.preload_module(&specifier).await.map(|_| ())?;
|
worker.preload_module(&specifier).await.map(|_| ())?;
|
||||||
}
|
}
|
||||||
|
@ -257,8 +257,8 @@ async fn eval_command(
|
||||||
// Force TypeScript compile.
|
// Force TypeScript compile.
|
||||||
let main_module =
|
let main_module =
|
||||||
ModuleSpecifier::resolve_url_or_path("./$deno$eval.ts").unwrap();
|
ModuleSpecifier::resolve_url_or_path("./$deno$eval.ts").unwrap();
|
||||||
let global_state = GlobalState::new(flags)?;
|
let program_state = ProgramState::new(flags)?;
|
||||||
let mut worker = MainWorker::new(&global_state, main_module.clone());
|
let mut worker = MainWorker::new(&program_state, main_module.clone());
|
||||||
let main_module_url = main_module.as_url().to_owned();
|
let main_module_url = main_module.as_url().to_owned();
|
||||||
// Create a dummy source file.
|
// Create a dummy source file.
|
||||||
let source_code = if print {
|
let source_code = if print {
|
||||||
|
@ -281,7 +281,7 @@ async fn eval_command(
|
||||||
};
|
};
|
||||||
// Save our fake file into file fetcher cache
|
// Save our fake file into file fetcher cache
|
||||||
// to allow module access by TS compiler.
|
// to allow module access by TS compiler.
|
||||||
global_state
|
program_state
|
||||||
.file_fetcher
|
.file_fetcher
|
||||||
.save_source_file_in_cache(&main_module, source_file);
|
.save_source_file_in_cache(&main_module, source_file);
|
||||||
debug!("main_module {}", &main_module);
|
debug!("main_module {}", &main_module);
|
||||||
|
@ -300,7 +300,7 @@ async fn bundle_command(
|
||||||
let module_specifier = ModuleSpecifier::resolve_url_or_path(&source_file)?;
|
let module_specifier = ModuleSpecifier::resolve_url_or_path(&source_file)?;
|
||||||
|
|
||||||
debug!(">>>>> bundle START");
|
debug!(">>>>> bundle START");
|
||||||
let global_state = GlobalState::new(flags)?;
|
let program_state = ProgramState::new(flags)?;
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"{} {}",
|
"{} {}",
|
||||||
|
@ -308,9 +308,9 @@ async fn bundle_command(
|
||||||
module_specifier.to_string()
|
module_specifier.to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
let output = global_state
|
let output = program_state
|
||||||
.ts_compiler
|
.ts_compiler
|
||||||
.bundle(&global_state, module_specifier)
|
.bundle(&program_state, module_specifier)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
debug!(">>>>> bundle END");
|
debug!(">>>>> bundle END");
|
||||||
|
@ -391,12 +391,12 @@ async fn doc_command(
|
||||||
maybe_filter: Option<String>,
|
maybe_filter: Option<String>,
|
||||||
private: bool,
|
private: bool,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
let global_state = GlobalState::new(flags.clone())?;
|
let program_state = ProgramState::new(flags.clone())?;
|
||||||
let source_file = source_file.unwrap_or_else(|| "--builtin".to_string());
|
let source_file = source_file.unwrap_or_else(|| "--builtin".to_string());
|
||||||
|
|
||||||
let loader = Box::new(DocLoader {
|
let loader = Box::new(DocLoader {
|
||||||
fetcher: global_state.file_fetcher.clone(),
|
fetcher: program_state.file_fetcher.clone(),
|
||||||
maybe_import_map: global_state.maybe_import_map.clone(),
|
maybe_import_map: program_state.maybe_import_map.clone(),
|
||||||
});
|
});
|
||||||
let doc_parser = doc::DocParser::new(loader, private);
|
let doc_parser = doc::DocParser::new(loader, private);
|
||||||
|
|
||||||
|
@ -455,18 +455,18 @@ async fn doc_command(
|
||||||
async fn run_repl(flags: Flags) -> Result<(), AnyError> {
|
async fn run_repl(flags: Flags) -> Result<(), AnyError> {
|
||||||
let main_module =
|
let main_module =
|
||||||
ModuleSpecifier::resolve_url_or_path("./$deno$repl.ts").unwrap();
|
ModuleSpecifier::resolve_url_or_path("./$deno$repl.ts").unwrap();
|
||||||
let global_state = GlobalState::new(flags)?;
|
let program_state = ProgramState::new(flags)?;
|
||||||
let mut worker = MainWorker::new(&global_state, main_module.clone());
|
let mut worker = MainWorker::new(&program_state, main_module.clone());
|
||||||
worker.run_event_loop().await?;
|
worker.run_event_loop().await?;
|
||||||
|
|
||||||
repl::run(&global_state, worker).await
|
repl::run(&program_state, worker).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
|
async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
|
||||||
let global_state = GlobalState::new(flags.clone())?;
|
let program_state = ProgramState::new(flags.clone())?;
|
||||||
let main_module =
|
let main_module =
|
||||||
ModuleSpecifier::resolve_url_or_path("./$deno$stdin.ts").unwrap();
|
ModuleSpecifier::resolve_url_or_path("./$deno$stdin.ts").unwrap();
|
||||||
let mut worker = MainWorker::new(&global_state.clone(), main_module.clone());
|
let mut worker = MainWorker::new(&program_state.clone(), main_module.clone());
|
||||||
|
|
||||||
let mut source = Vec::new();
|
let mut source = Vec::new();
|
||||||
std::io::stdin().read_to_end(&mut source)?;
|
std::io::stdin().read_to_end(&mut source)?;
|
||||||
|
@ -481,7 +481,7 @@ async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
|
||||||
};
|
};
|
||||||
// Save our fake file into file fetcher cache
|
// Save our fake file into file fetcher cache
|
||||||
// to allow module access by TS compiler
|
// to allow module access by TS compiler
|
||||||
global_state
|
program_state
|
||||||
.file_fetcher
|
.file_fetcher
|
||||||
.save_source_file_in_cache(&main_module, source_file);
|
.save_source_file_in_cache(&main_module, source_file);
|
||||||
|
|
||||||
|
@ -495,11 +495,11 @@ async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
|
||||||
|
|
||||||
async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
|
async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
|
||||||
let main_module = ModuleSpecifier::resolve_url_or_path(&script)?;
|
let main_module = ModuleSpecifier::resolve_url_or_path(&script)?;
|
||||||
let global_state = GlobalState::new(flags.clone())?;
|
let program_state = ProgramState::new(flags.clone())?;
|
||||||
|
|
||||||
let mut module_graph_loader = module_graph::ModuleGraphLoader::new(
|
let mut module_graph_loader = module_graph::ModuleGraphLoader::new(
|
||||||
global_state.file_fetcher.clone(),
|
program_state.file_fetcher.clone(),
|
||||||
global_state.maybe_import_map.clone(),
|
program_state.maybe_import_map.clone(),
|
||||||
Permissions::allow_all(),
|
Permissions::allow_all(),
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
|
@ -515,7 +515,7 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
|
||||||
.map(|url| url.to_file_path().unwrap())
|
.map(|url| url.to_file_path().unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if let Some(import_map) = global_state.flags.import_map_path.clone() {
|
if let Some(import_map) = program_state.flags.import_map_path.clone() {
|
||||||
paths_to_watch.push(
|
paths_to_watch.push(
|
||||||
Url::parse(&format!("file://{}", &import_map))?
|
Url::parse(&format!("file://{}", &import_map))?
|
||||||
.to_file_path()
|
.to_file_path()
|
||||||
|
@ -525,9 +525,9 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
|
||||||
|
|
||||||
// FIXME(bartlomieju): new file watcher is created on after each restart
|
// FIXME(bartlomieju): new file watcher is created on after each restart
|
||||||
file_watcher::watch_func(&paths_to_watch, move || {
|
file_watcher::watch_func(&paths_to_watch, move || {
|
||||||
// FIXME(bartlomieju): GlobalState must be created on each restart - otherwise file fetcher
|
// FIXME(bartlomieju): ProgramState must be created on each restart - otherwise file fetcher
|
||||||
// will use cached source files
|
// will use cached source files
|
||||||
let gs = GlobalState::new(flags.clone()).unwrap();
|
let gs = ProgramState::new(flags.clone()).unwrap();
|
||||||
let main_module = main_module.clone();
|
let main_module = main_module.clone();
|
||||||
async move {
|
async move {
|
||||||
let mut worker = MainWorker::new(&gs, main_module.clone());
|
let mut worker = MainWorker::new(&gs, main_module.clone());
|
||||||
|
@ -554,8 +554,8 @@ async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let main_module = ModuleSpecifier::resolve_url_or_path(&script)?;
|
let main_module = ModuleSpecifier::resolve_url_or_path(&script)?;
|
||||||
let global_state = GlobalState::new(flags.clone())?;
|
let program_state = ProgramState::new(flags.clone())?;
|
||||||
let mut worker = MainWorker::new(&global_state, main_module.clone());
|
let mut worker = MainWorker::new(&program_state, main_module.clone());
|
||||||
debug!("main_module {}", main_module);
|
debug!("main_module {}", main_module);
|
||||||
worker.execute_module(&main_module).await?;
|
worker.execute_module(&main_module).await?;
|
||||||
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
||||||
|
@ -572,7 +572,7 @@ async fn test_command(
|
||||||
allow_none: bool,
|
allow_none: bool,
|
||||||
filter: Option<String>,
|
filter: Option<String>,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
let global_state = GlobalState::new(flags.clone())?;
|
let program_state = ProgramState::new(flags.clone())?;
|
||||||
let cwd = std::env::current_dir().expect("No current directory");
|
let cwd = std::env::current_dir().expect("No current directory");
|
||||||
let include = include.unwrap_or_else(|| vec![".".to_string()]);
|
let include = include.unwrap_or_else(|| vec![".".to_string()]);
|
||||||
let test_modules = test_runner::prepare_test_modules_urls(include, &cwd)?;
|
let test_modules = test_runner::prepare_test_modules_urls(include, &cwd)?;
|
||||||
|
@ -596,7 +596,7 @@ async fn test_command(
|
||||||
);
|
);
|
||||||
let main_module =
|
let main_module =
|
||||||
ModuleSpecifier::resolve_url(&test_file_url.to_string()).unwrap();
|
ModuleSpecifier::resolve_url(&test_file_url.to_string()).unwrap();
|
||||||
let mut worker = MainWorker::new(&global_state, main_module.clone());
|
let mut worker = MainWorker::new(&program_state, main_module.clone());
|
||||||
// Create a dummy source file.
|
// Create a dummy source file.
|
||||||
let source_file = SourceFile {
|
let source_file = SourceFile {
|
||||||
filename: test_file_url.to_file_path().unwrap(),
|
filename: test_file_url.to_file_path().unwrap(),
|
||||||
|
@ -607,7 +607,7 @@ async fn test_command(
|
||||||
};
|
};
|
||||||
// Save our fake file into file fetcher cache
|
// Save our fake file into file fetcher cache
|
||||||
// to allow module access by TS compiler
|
// to allow module access by TS compiler
|
||||||
global_state
|
program_state
|
||||||
.file_fetcher
|
.file_fetcher
|
||||||
.save_source_file_in_cache(&main_module, source_file);
|
.save_source_file_in_cache(&main_module, source_file);
|
||||||
|
|
||||||
|
|
|
@ -588,16 +588,16 @@ impl ModuleGraphLoader {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::global_state::GlobalState;
|
use crate::program_state::ProgramState;
|
||||||
use deno_core::serde_json;
|
use deno_core::serde_json;
|
||||||
use deno_core::serde_json::json;
|
use deno_core::serde_json::json;
|
||||||
|
|
||||||
async fn build_graph(
|
async fn build_graph(
|
||||||
module_specifier: &ModuleSpecifier,
|
module_specifier: &ModuleSpecifier,
|
||||||
) -> Result<ModuleGraph, AnyError> {
|
) -> Result<ModuleGraph, AnyError> {
|
||||||
let global_state = GlobalState::new(Default::default()).unwrap();
|
let program_state = ProgramState::new(Default::default()).unwrap();
|
||||||
let mut graph_loader = ModuleGraphLoader::new(
|
let mut graph_loader = ModuleGraphLoader::new(
|
||||||
global_state.file_fetcher.clone(),
|
program_state.file_fetcher.clone(),
|
||||||
None,
|
None,
|
||||||
Permissions::allow_all(),
|
Permissions::allow_all(),
|
||||||
false,
|
false,
|
||||||
|
|
|
@ -785,7 +785,7 @@ impl GraphBuilder2 {
|
||||||
/// graph.
|
/// graph.
|
||||||
///
|
///
|
||||||
/// TODO(@kitsonk) this should really be owned by the graph, but currently
|
/// TODO(@kitsonk) this should really be owned by the graph, but currently
|
||||||
/// the lockfile is behind a mutex in global_state, which makes it really
|
/// the lockfile is behind a mutex in program_state, which makes it really
|
||||||
/// hard to not pass around as a reference, which if the Graph owned it, it
|
/// hard to not pass around as a reference, which if the Graph owned it, it
|
||||||
/// would need lifetime parameters and lifetime parameters are 😭
|
/// would need lifetime parameters and lifetime parameters are 😭
|
||||||
pub fn get_graph(
|
pub fn get_graph(
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::import_map::ImportMap;
|
use crate::import_map::ImportMap;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
|
use crate::program_state::ProgramState;
|
||||||
use crate::tsc::TargetLib;
|
use crate::tsc::TargetLib;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::futures::future::FutureExt;
|
use deno_core::futures::future::FutureExt;
|
||||||
|
@ -51,13 +51,13 @@ impl ModuleLoader for CliModuleLoader {
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
is_main: bool,
|
is_main: bool,
|
||||||
) -> Result<ModuleSpecifier, AnyError> {
|
) -> Result<ModuleSpecifier, AnyError> {
|
||||||
let global_state = {
|
let program_state = {
|
||||||
let state = op_state.borrow();
|
let state = op_state.borrow();
|
||||||
state.borrow::<Arc<GlobalState>>().clone()
|
state.borrow::<Arc<ProgramState>>().clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME(bartlomieju): hacky way to provide compatibility with repl
|
// FIXME(bartlomieju): hacky way to provide compatibility with repl
|
||||||
let referrer = if referrer.is_empty() && global_state.flags.repl {
|
let referrer = if referrer.is_empty() && program_state.flags.repl {
|
||||||
"<unknown>"
|
"<unknown>"
|
||||||
} else {
|
} else {
|
||||||
referrer
|
referrer
|
||||||
|
@ -87,14 +87,14 @@ impl ModuleLoader for CliModuleLoader {
|
||||||
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
||||||
let module_specifier = module_specifier.to_owned();
|
let module_specifier = module_specifier.to_owned();
|
||||||
let module_url_specified = module_specifier.to_string();
|
let module_url_specified = module_specifier.to_string();
|
||||||
let global_state = {
|
let program_state = {
|
||||||
let state = op_state.borrow();
|
let state = op_state.borrow();
|
||||||
state.borrow::<Arc<GlobalState>>().clone()
|
state.borrow::<Arc<ProgramState>>().clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO(bartlomieju): `fetch_compiled_module` should take `load_id` param
|
// TODO(bartlomieju): `fetch_compiled_module` should take `load_id` param
|
||||||
let fut = async move {
|
let fut = async move {
|
||||||
let compiled_module = global_state
|
let compiled_module = program_state
|
||||||
.fetch_compiled_module(module_specifier, maybe_referrer)
|
.fetch_compiled_module(module_specifier, maybe_referrer)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(deno_core::ModuleSource {
|
Ok(deno_core::ModuleSource {
|
||||||
|
@ -130,7 +130,7 @@ impl ModuleLoader for CliModuleLoader {
|
||||||
} else {
|
} else {
|
||||||
state.borrow::<Permissions>().clone()
|
state.borrow::<Permissions>().clone()
|
||||||
};
|
};
|
||||||
let global_state = state.borrow::<Arc<GlobalState>>().clone();
|
let program_state = state.borrow::<Arc<ProgramState>>().clone();
|
||||||
drop(state);
|
drop(state);
|
||||||
|
|
||||||
// TODO(bartlomieju): I'm not sure if it's correct to ignore
|
// TODO(bartlomieju): I'm not sure if it's correct to ignore
|
||||||
|
@ -144,7 +144,7 @@ impl ModuleLoader for CliModuleLoader {
|
||||||
|
|
||||||
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param
|
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param
|
||||||
async move {
|
async move {
|
||||||
global_state
|
program_state
|
||||||
.prepare_module_load(
|
.prepare_module_load(
|
||||||
module_specifier,
|
module_specifier,
|
||||||
maybe_referrer,
|
maybe_referrer,
|
|
@ -39,7 +39,7 @@ fn op_apply_source_map(
|
||||||
args.line_number.into(),
|
args.line_number.into(),
|
||||||
args.column_number.into(),
|
args.column_number.into(),
|
||||||
&mut mappings_map,
|
&mut mappings_map,
|
||||||
&super::global_state(state).ts_compiler,
|
&super::program_state(state).ts_compiler,
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(json!({
|
Ok(json!({
|
||||||
|
|
|
@ -26,8 +26,8 @@ pub mod web_worker;
|
||||||
pub mod websocket;
|
pub mod websocket;
|
||||||
pub mod worker_host;
|
pub mod worker_host;
|
||||||
|
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::metrics::metrics_op;
|
use crate::metrics::metrics_op;
|
||||||
|
use crate::program_state::ProgramState;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::json_op_async;
|
use deno_core::json_op_async;
|
||||||
use deno_core::json_op_sync;
|
use deno_core::json_op_sync;
|
||||||
|
@ -59,22 +59,22 @@ where
|
||||||
|
|
||||||
/// Helper for checking unstable features. Used for sync ops.
|
/// Helper for checking unstable features. Used for sync ops.
|
||||||
pub fn check_unstable(state: &OpState, api_name: &str) {
|
pub fn check_unstable(state: &OpState, api_name: &str) {
|
||||||
state.borrow::<Arc<GlobalState>>().check_unstable(api_name)
|
state.borrow::<Arc<ProgramState>>().check_unstable(api_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper for checking unstable features. Used for async ops.
|
/// Helper for checking unstable features. Used for async ops.
|
||||||
pub fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) {
|
pub fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) {
|
||||||
let state = state.borrow();
|
let state = state.borrow();
|
||||||
state.borrow::<Arc<GlobalState>>().check_unstable(api_name)
|
state.borrow::<Arc<ProgramState>>().check_unstable(api_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper for extracting the commonly used state. Used for sync ops.
|
/// Helper for extracting the commonly used state. Used for sync ops.
|
||||||
pub fn global_state(state: &OpState) -> Arc<GlobalState> {
|
pub fn program_state(state: &OpState) -> Arc<ProgramState> {
|
||||||
state.borrow::<Arc<GlobalState>>().clone()
|
state.borrow::<Arc<ProgramState>>().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper for extracting the commonly used state. Used for async ops.
|
/// Helper for extracting the commonly used state. Used for async ops.
|
||||||
pub fn global_state2(state: &Rc<RefCell<OpState>>) -> Arc<GlobalState> {
|
pub fn global_state2(state: &Rc<RefCell<OpState>>) -> Arc<ProgramState> {
|
||||||
let state = state.borrow();
|
let state = state.borrow();
|
||||||
state.borrow::<Arc<GlobalState>>().clone()
|
state.borrow::<Arc<ProgramState>>().clone()
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ fn op_start(
|
||||||
_args: Value,
|
_args: Value,
|
||||||
_zero_copy: &mut [ZeroCopyBuf],
|
_zero_copy: &mut [ZeroCopyBuf],
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<Value, AnyError> {
|
||||||
let gs = &super::global_state(state);
|
let gs = &super::program_state(state);
|
||||||
|
|
||||||
Ok(json!({
|
Ok(json!({
|
||||||
// TODO(bartlomieju): `cwd` field is not used in JS, remove?
|
// TODO(bartlomieju): `cwd` field is not used in JS, remove?
|
||||||
|
|
|
@ -37,14 +37,14 @@ async fn op_compile(
|
||||||
super::check_unstable2(&state, "Deno.compile");
|
super::check_unstable2(&state, "Deno.compile");
|
||||||
let args: CompileArgs = serde_json::from_value(args)?;
|
let args: CompileArgs = serde_json::from_value(args)?;
|
||||||
let cli_state = super::global_state2(&state);
|
let cli_state = super::global_state2(&state);
|
||||||
let global_state = cli_state.clone();
|
let program_state = cli_state.clone();
|
||||||
let permissions = {
|
let permissions = {
|
||||||
let state = state.borrow();
|
let state = state.borrow();
|
||||||
state.borrow::<Permissions>().clone()
|
state.borrow::<Permissions>().clone()
|
||||||
};
|
};
|
||||||
let fut = if args.bundle {
|
let fut = if args.bundle {
|
||||||
runtime_bundle(
|
runtime_bundle(
|
||||||
&global_state,
|
&program_state,
|
||||||
permissions,
|
permissions,
|
||||||
&args.root_name,
|
&args.root_name,
|
||||||
&args.sources,
|
&args.sources,
|
||||||
|
@ -53,7 +53,7 @@ async fn op_compile(
|
||||||
.boxed_local()
|
.boxed_local()
|
||||||
} else {
|
} else {
|
||||||
runtime_compile(
|
runtime_compile(
|
||||||
&global_state,
|
&program_state,
|
||||||
permissions,
|
permissions,
|
||||||
&args.root_name,
|
&args.root_name,
|
||||||
&args.sources,
|
&args.sources,
|
||||||
|
@ -79,8 +79,8 @@ async fn op_transpile(
|
||||||
super::check_unstable2(&state, "Deno.transpile");
|
super::check_unstable2(&state, "Deno.transpile");
|
||||||
let args: TranspileArgs = serde_json::from_value(args)?;
|
let args: TranspileArgs = serde_json::from_value(args)?;
|
||||||
let cli_state = super::global_state2(&state);
|
let cli_state = super::global_state2(&state);
|
||||||
let global_state = cli_state.clone();
|
let program_state = cli_state.clone();
|
||||||
let result =
|
let result =
|
||||||
runtime_transpile(global_state, &args.sources, &args.options).await?;
|
runtime_transpile(program_state, &args.sources, &args.options).await?;
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
use crate::fmt_errors::JsError;
|
use crate::fmt_errors::JsError;
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::ops::io::get_stdio;
|
use crate::ops::io::get_stdio;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
|
use crate::program_state::ProgramState;
|
||||||
use crate::tokio_util::create_basic_runtime;
|
use crate::tokio_util::create_basic_runtime;
|
||||||
use crate::worker::WebWorker;
|
use crate::worker::WebWorker;
|
||||||
use crate::worker::WebWorkerHandle;
|
use crate::worker::WebWorkerHandle;
|
||||||
|
@ -48,7 +48,7 @@ pub type WorkerId = u32;
|
||||||
fn create_web_worker(
|
fn create_web_worker(
|
||||||
worker_id: u32,
|
worker_id: u32,
|
||||||
name: String,
|
name: String,
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
specifier: ModuleSpecifier,
|
specifier: ModuleSpecifier,
|
||||||
has_deno_namespace: bool,
|
has_deno_namespace: bool,
|
||||||
|
@ -57,7 +57,7 @@ fn create_web_worker(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
permissions,
|
permissions,
|
||||||
specifier,
|
specifier,
|
||||||
global_state.clone(),
|
program_state.clone(),
|
||||||
has_deno_namespace,
|
has_deno_namespace,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -91,13 +91,13 @@ fn create_web_worker(
|
||||||
fn run_worker_thread(
|
fn run_worker_thread(
|
||||||
worker_id: u32,
|
worker_id: u32,
|
||||||
name: String,
|
name: String,
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
specifier: ModuleSpecifier,
|
specifier: ModuleSpecifier,
|
||||||
has_deno_namespace: bool,
|
has_deno_namespace: bool,
|
||||||
maybe_source_code: Option<String>,
|
maybe_source_code: Option<String>,
|
||||||
) -> Result<(JoinHandle<()>, WebWorkerHandle), AnyError> {
|
) -> Result<(JoinHandle<()>, WebWorkerHandle), AnyError> {
|
||||||
let global_state = global_state.clone();
|
let program_state = program_state.clone();
|
||||||
let (handle_sender, handle_receiver) =
|
let (handle_sender, handle_receiver) =
|
||||||
std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, AnyError>>(1);
|
std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, AnyError>>(1);
|
||||||
|
|
||||||
|
@ -111,7 +111,7 @@ fn run_worker_thread(
|
||||||
let result = create_web_worker(
|
let result = create_web_worker(
|
||||||
worker_id,
|
worker_id,
|
||||||
name,
|
name,
|
||||||
&global_state,
|
&program_state,
|
||||||
permissions,
|
permissions,
|
||||||
specifier.clone(),
|
specifier.clone(),
|
||||||
has_deno_namespace,
|
has_deno_namespace,
|
||||||
|
@ -211,7 +211,7 @@ fn op_create_worker(
|
||||||
|
|
||||||
let module_specifier = ModuleSpecifier::resolve_url(&specifier)?;
|
let module_specifier = ModuleSpecifier::resolve_url(&specifier)?;
|
||||||
let worker_name = args_name.unwrap_or_else(|| "".to_string());
|
let worker_name = args_name.unwrap_or_else(|| "".to_string());
|
||||||
let cli_state = super::global_state(state);
|
let cli_state = super::program_state(state);
|
||||||
|
|
||||||
let (join_handle, worker_handle) = run_worker_thread(
|
let (join_handle, worker_handle) = run_worker_thread(
|
||||||
worker_id,
|
worker_id,
|
||||||
|
|
|
@ -83,7 +83,7 @@ impl Default for PermissionState {
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
||||||
pub struct UnaryPermission<T: Eq + Hash> {
|
pub struct UnaryPermission<T: Eq + Hash> {
|
||||||
pub global_state: PermissionState,
|
pub program_state: PermissionState,
|
||||||
pub granted_list: HashSet<T>,
|
pub granted_list: HashSet<T>,
|
||||||
pub denied_list: HashSet<T>,
|
pub denied_list: HashSet<T>,
|
||||||
}
|
}
|
||||||
|
@ -110,17 +110,17 @@ impl Permissions {
|
||||||
pub fn from_flags(flags: &Flags) -> Self {
|
pub fn from_flags(flags: &Flags) -> Self {
|
||||||
Self {
|
Self {
|
||||||
read: UnaryPermission::<PathBuf> {
|
read: UnaryPermission::<PathBuf> {
|
||||||
global_state: PermissionState::from(flags.allow_read),
|
program_state: PermissionState::from(flags.allow_read),
|
||||||
granted_list: resolve_fs_allowlist(&flags.read_allowlist),
|
granted_list: resolve_fs_allowlist(&flags.read_allowlist),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
write: UnaryPermission::<PathBuf> {
|
write: UnaryPermission::<PathBuf> {
|
||||||
global_state: PermissionState::from(flags.allow_write),
|
program_state: PermissionState::from(flags.allow_write),
|
||||||
granted_list: resolve_fs_allowlist(&flags.write_allowlist),
|
granted_list: resolve_fs_allowlist(&flags.write_allowlist),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
net: UnaryPermission::<String> {
|
net: UnaryPermission::<String> {
|
||||||
global_state: PermissionState::from(flags.allow_net),
|
program_state: PermissionState::from(flags.allow_net),
|
||||||
granted_list: flags.net_allowlist.iter().cloned().collect(),
|
granted_list: flags.net_allowlist.iter().cloned().collect(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
@ -152,15 +152,15 @@ impl Permissions {
|
||||||
pub fn allow_all() -> Self {
|
pub fn allow_all() -> Self {
|
||||||
Self {
|
Self {
|
||||||
read: UnaryPermission {
|
read: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
write: UnaryPermission {
|
write: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
net: UnaryPermission {
|
net: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
env: PermissionState::Granted,
|
env: PermissionState::Granted,
|
||||||
|
@ -172,7 +172,7 @@ impl Permissions {
|
||||||
|
|
||||||
pub fn query_read(&self, path: &Option<&Path>) -> PermissionState {
|
pub fn query_read(&self, path: &Option<&Path>) -> PermissionState {
|
||||||
let path = path.map(|p| resolve_from_cwd(p).unwrap());
|
let path = path.map(|p| resolve_from_cwd(p).unwrap());
|
||||||
if self.read.global_state == PermissionState::Denied
|
if self.read.program_state == PermissionState::Denied
|
||||||
&& match path.as_ref() {
|
&& match path.as_ref() {
|
||||||
None => true,
|
None => true,
|
||||||
Some(path) => check_path_blocklist(path, &self.read.denied_list),
|
Some(path) => check_path_blocklist(path, &self.read.denied_list),
|
||||||
|
@ -180,7 +180,7 @@ impl Permissions {
|
||||||
{
|
{
|
||||||
return PermissionState::Denied;
|
return PermissionState::Denied;
|
||||||
}
|
}
|
||||||
if self.read.global_state == PermissionState::Granted
|
if self.read.program_state == PermissionState::Granted
|
||||||
|| match path.as_ref() {
|
|| match path.as_ref() {
|
||||||
None => false,
|
None => false,
|
||||||
Some(path) => check_path_allowlist(path, &self.read.granted_list),
|
Some(path) => check_path_allowlist(path, &self.read.granted_list),
|
||||||
|
@ -193,7 +193,7 @@ impl Permissions {
|
||||||
|
|
||||||
pub fn query_write(&self, path: &Option<&Path>) -> PermissionState {
|
pub fn query_write(&self, path: &Option<&Path>) -> PermissionState {
|
||||||
let path = path.map(|p| resolve_from_cwd(p).unwrap());
|
let path = path.map(|p| resolve_from_cwd(p).unwrap());
|
||||||
if self.write.global_state == PermissionState::Denied
|
if self.write.program_state == PermissionState::Denied
|
||||||
&& match path.as_ref() {
|
&& match path.as_ref() {
|
||||||
None => true,
|
None => true,
|
||||||
Some(path) => check_path_blocklist(path, &self.write.denied_list),
|
Some(path) => check_path_blocklist(path, &self.write.denied_list),
|
||||||
|
@ -201,7 +201,7 @@ impl Permissions {
|
||||||
{
|
{
|
||||||
return PermissionState::Denied;
|
return PermissionState::Denied;
|
||||||
}
|
}
|
||||||
if self.write.global_state == PermissionState::Granted
|
if self.write.program_state == PermissionState::Granted
|
||||||
|| match path.as_ref() {
|
|| match path.as_ref() {
|
||||||
None => false,
|
None => false,
|
||||||
Some(path) => check_path_allowlist(path, &self.write.granted_list),
|
Some(path) => check_path_allowlist(path, &self.write.granted_list),
|
||||||
|
@ -213,12 +213,12 @@ impl Permissions {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn query_net(&self, host: &str, port: Option<u16>) -> PermissionState {
|
pub fn query_net(&self, host: &str, port: Option<u16>) -> PermissionState {
|
||||||
if self.net.global_state == PermissionState::Denied
|
if self.net.program_state == PermissionState::Denied
|
||||||
|| check_host_and_port_list(host, port, &self.net.denied_list)
|
|| check_host_and_port_list(host, port, &self.net.denied_list)
|
||||||
{
|
{
|
||||||
return PermissionState::Denied;
|
return PermissionState::Denied;
|
||||||
}
|
}
|
||||||
if self.net.global_state == PermissionState::Granted
|
if self.net.program_state == PermissionState::Granted
|
||||||
|| check_host_and_port_list(host, port, &self.net.granted_list)
|
|| check_host_and_port_list(host, port, &self.net.granted_list)
|
||||||
{
|
{
|
||||||
return PermissionState::Granted;
|
return PermissionState::Granted;
|
||||||
|
@ -231,7 +231,7 @@ impl Permissions {
|
||||||
url: &Option<&str>,
|
url: &Option<&str>,
|
||||||
) -> Result<PermissionState, AnyError> {
|
) -> Result<PermissionState, AnyError> {
|
||||||
if url.is_none() {
|
if url.is_none() {
|
||||||
return Ok(self.net.global_state);
|
return Ok(self.net.program_state);
|
||||||
}
|
}
|
||||||
let url: &str = url.unwrap();
|
let url: &str = url.unwrap();
|
||||||
// If url is invalid, then throw a TypeError.
|
// If url is invalid, then throw a TypeError.
|
||||||
|
@ -287,7 +287,7 @@ impl Permissions {
|
||||||
.denied_list
|
.denied_list
|
||||||
.retain(|path| !resolved_path.starts_with(path));
|
.retain(|path| !resolved_path.starts_with(path));
|
||||||
self.read.denied_list.insert(resolved_path);
|
self.read.denied_list.insert(resolved_path);
|
||||||
self.read.global_state = PermissionState::Denied;
|
self.read.program_state = PermissionState::Denied;
|
||||||
return PermissionState::Denied;
|
return PermissionState::Denied;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -297,10 +297,10 @@ impl Permissions {
|
||||||
if state == PermissionState::Prompt {
|
if state == PermissionState::Prompt {
|
||||||
if permission_prompt("Deno requests read access") {
|
if permission_prompt("Deno requests read access") {
|
||||||
self.read.granted_list.clear();
|
self.read.granted_list.clear();
|
||||||
self.read.global_state = PermissionState::Granted;
|
self.read.program_state = PermissionState::Granted;
|
||||||
return PermissionState::Granted;
|
return PermissionState::Granted;
|
||||||
} else {
|
} else {
|
||||||
self.read.global_state = PermissionState::Denied;
|
self.read.program_state = PermissionState::Denied;
|
||||||
return PermissionState::Denied;
|
return PermissionState::Denied;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -329,7 +329,7 @@ impl Permissions {
|
||||||
.denied_list
|
.denied_list
|
||||||
.retain(|path| !resolved_path.starts_with(path));
|
.retain(|path| !resolved_path.starts_with(path));
|
||||||
self.write.denied_list.insert(resolved_path);
|
self.write.denied_list.insert(resolved_path);
|
||||||
self.write.global_state = PermissionState::Denied;
|
self.write.program_state = PermissionState::Denied;
|
||||||
return PermissionState::Denied;
|
return PermissionState::Denied;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -339,10 +339,10 @@ impl Permissions {
|
||||||
if state == PermissionState::Prompt {
|
if state == PermissionState::Prompt {
|
||||||
if permission_prompt("Deno requests write access") {
|
if permission_prompt("Deno requests write access") {
|
||||||
self.write.granted_list.clear();
|
self.write.granted_list.clear();
|
||||||
self.write.global_state = PermissionState::Granted;
|
self.write.program_state = PermissionState::Granted;
|
||||||
return PermissionState::Granted;
|
return PermissionState::Granted;
|
||||||
} else {
|
} else {
|
||||||
self.write.global_state = PermissionState::Denied;
|
self.write.program_state = PermissionState::Denied;
|
||||||
return PermissionState::Denied;
|
return PermissionState::Denied;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -365,7 +365,7 @@ impl Permissions {
|
||||||
return Ok(PermissionState::Granted);
|
return Ok(PermissionState::Granted);
|
||||||
} else {
|
} else {
|
||||||
self.net.denied_list.insert(url.to_string());
|
self.net.denied_list.insert(url.to_string());
|
||||||
self.net.global_state = PermissionState::Denied;
|
self.net.program_state = PermissionState::Denied;
|
||||||
return Ok(PermissionState::Denied);
|
return Ok(PermissionState::Denied);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -375,10 +375,10 @@ impl Permissions {
|
||||||
if state == PermissionState::Prompt {
|
if state == PermissionState::Prompt {
|
||||||
if permission_prompt("Deno requests network access") {
|
if permission_prompt("Deno requests network access") {
|
||||||
self.net.granted_list.clear();
|
self.net.granted_list.clear();
|
||||||
self.net.global_state = PermissionState::Granted;
|
self.net.program_state = PermissionState::Granted;
|
||||||
return Ok(PermissionState::Granted);
|
return Ok(PermissionState::Granted);
|
||||||
} else {
|
} else {
|
||||||
self.net.global_state = PermissionState::Denied;
|
self.net.program_state = PermissionState::Denied;
|
||||||
return Ok(PermissionState::Denied);
|
return Ok(PermissionState::Denied);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -439,8 +439,8 @@ impl Permissions {
|
||||||
.retain(|path_| !path_.starts_with(&path));
|
.retain(|path_| !path_.starts_with(&path));
|
||||||
} else {
|
} else {
|
||||||
self.read.granted_list.clear();
|
self.read.granted_list.clear();
|
||||||
if self.read.global_state == PermissionState::Granted {
|
if self.read.program_state == PermissionState::Granted {
|
||||||
self.read.global_state = PermissionState::Prompt;
|
self.read.program_state = PermissionState::Prompt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.query_read(path)
|
self.query_read(path)
|
||||||
|
@ -455,8 +455,8 @@ impl Permissions {
|
||||||
.retain(|path_| !path_.starts_with(&path));
|
.retain(|path_| !path_.starts_with(&path));
|
||||||
} else {
|
} else {
|
||||||
self.write.granted_list.clear();
|
self.write.granted_list.clear();
|
||||||
if self.write.global_state == PermissionState::Granted {
|
if self.write.program_state == PermissionState::Granted {
|
||||||
self.write.global_state = PermissionState::Prompt;
|
self.write.program_state = PermissionState::Prompt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.query_write(path)
|
self.query_write(path)
|
||||||
|
@ -470,8 +470,8 @@ impl Permissions {
|
||||||
self.net.granted_list.remove(*url);
|
self.net.granted_list.remove(*url);
|
||||||
} else {
|
} else {
|
||||||
self.net.granted_list.clear();
|
self.net.granted_list.clear();
|
||||||
if self.net.global_state == PermissionState::Granted {
|
if self.net.program_state == PermissionState::Granted {
|
||||||
self.net.global_state = PermissionState::Prompt;
|
self.net.program_state = PermissionState::Prompt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.query_net_url(url)
|
self.query_net_url(url)
|
||||||
|
@ -835,17 +835,17 @@ mod tests {
|
||||||
let json_perms = r#"
|
let json_perms = r#"
|
||||||
{
|
{
|
||||||
"read": {
|
"read": {
|
||||||
"global_state": "Granted",
|
"program_state": "Granted",
|
||||||
"granted_list": [],
|
"granted_list": [],
|
||||||
"denied_list": []
|
"denied_list": []
|
||||||
},
|
},
|
||||||
"write": {
|
"write": {
|
||||||
"global_state": "Granted",
|
"program_state": "Granted",
|
||||||
"granted_list": [],
|
"granted_list": [],
|
||||||
"denied_list": []
|
"denied_list": []
|
||||||
},
|
},
|
||||||
"net": {
|
"net": {
|
||||||
"global_state": "Granted",
|
"program_state": "Granted",
|
||||||
"granted_list": [],
|
"granted_list": [],
|
||||||
"denied_list": []
|
"denied_list": []
|
||||||
},
|
},
|
||||||
|
@ -857,15 +857,15 @@ mod tests {
|
||||||
"#;
|
"#;
|
||||||
let perms0 = Permissions {
|
let perms0 = Permissions {
|
||||||
read: UnaryPermission {
|
read: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
write: UnaryPermission {
|
write: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
net: UnaryPermission {
|
net: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
env: PermissionState::Granted,
|
env: PermissionState::Granted,
|
||||||
|
@ -882,15 +882,15 @@ mod tests {
|
||||||
fn test_query() {
|
fn test_query() {
|
||||||
let perms1 = Permissions {
|
let perms1 = Permissions {
|
||||||
read: UnaryPermission {
|
read: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
write: UnaryPermission {
|
write: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
net: UnaryPermission {
|
net: UnaryPermission {
|
||||||
global_state: PermissionState::Granted,
|
program_state: PermissionState::Granted,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
env: PermissionState::Granted,
|
env: PermissionState::Granted,
|
||||||
|
@ -900,17 +900,17 @@ mod tests {
|
||||||
};
|
};
|
||||||
let perms2 = Permissions {
|
let perms2 = Permissions {
|
||||||
read: UnaryPermission {
|
read: UnaryPermission {
|
||||||
global_state: PermissionState::Prompt,
|
program_state: PermissionState::Prompt,
|
||||||
granted_list: resolve_fs_allowlist(&[PathBuf::from("/foo")]),
|
granted_list: resolve_fs_allowlist(&[PathBuf::from("/foo")]),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
write: UnaryPermission {
|
write: UnaryPermission {
|
||||||
global_state: PermissionState::Prompt,
|
program_state: PermissionState::Prompt,
|
||||||
granted_list: resolve_fs_allowlist(&[PathBuf::from("/foo")]),
|
granted_list: resolve_fs_allowlist(&[PathBuf::from("/foo")]),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
net: UnaryPermission {
|
net: UnaryPermission {
|
||||||
global_state: PermissionState::Prompt,
|
program_state: PermissionState::Prompt,
|
||||||
granted_list: ["127.0.0.1:8000".to_string()].iter().cloned().collect(),
|
granted_list: ["127.0.0.1:8000".to_string()].iter().cloned().collect(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
@ -950,15 +950,15 @@ mod tests {
|
||||||
fn test_request() {
|
fn test_request() {
|
||||||
let mut perms = Permissions {
|
let mut perms = Permissions {
|
||||||
read: UnaryPermission {
|
read: UnaryPermission {
|
||||||
global_state: PermissionState::Prompt,
|
program_state: PermissionState::Prompt,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
write: UnaryPermission {
|
write: UnaryPermission {
|
||||||
global_state: PermissionState::Prompt,
|
program_state: PermissionState::Prompt,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
net: UnaryPermission {
|
net: UnaryPermission {
|
||||||
global_state: PermissionState::Prompt,
|
program_state: PermissionState::Prompt,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
env: PermissionState::Prompt,
|
env: PermissionState::Prompt,
|
||||||
|
@ -1006,17 +1006,17 @@ mod tests {
|
||||||
fn test_revoke() {
|
fn test_revoke() {
|
||||||
let mut perms = Permissions {
|
let mut perms = Permissions {
|
||||||
read: UnaryPermission {
|
read: UnaryPermission {
|
||||||
global_state: PermissionState::Prompt,
|
program_state: PermissionState::Prompt,
|
||||||
granted_list: resolve_fs_allowlist(&[PathBuf::from("/foo")]),
|
granted_list: resolve_fs_allowlist(&[PathBuf::from("/foo")]),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
write: UnaryPermission {
|
write: UnaryPermission {
|
||||||
global_state: PermissionState::Prompt,
|
program_state: PermissionState::Prompt,
|
||||||
granted_list: resolve_fs_allowlist(&[PathBuf::from("/foo")]),
|
granted_list: resolve_fs_allowlist(&[PathBuf::from("/foo")]),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
net: UnaryPermission {
|
net: UnaryPermission {
|
||||||
global_state: PermissionState::Denied,
|
program_state: PermissionState::Denied,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
env: PermissionState::Granted,
|
env: PermissionState::Granted,
|
||||||
|
|
|
@ -36,7 +36,7 @@ pub fn exit_unstable(api_name: &str) {
|
||||||
/// This structure represents state of single "deno" program.
|
/// This structure represents state of single "deno" program.
|
||||||
///
|
///
|
||||||
/// It is shared by all created workers (thus V8 isolates).
|
/// It is shared by all created workers (thus V8 isolates).
|
||||||
pub struct GlobalState {
|
pub struct ProgramState {
|
||||||
/// Flags parsed from `argv` contents.
|
/// Flags parsed from `argv` contents.
|
||||||
pub flags: flags::Flags,
|
pub flags: flags::Flags,
|
||||||
/// Permissions parsed from `flags`.
|
/// Permissions parsed from `flags`.
|
||||||
|
@ -49,7 +49,7 @@ pub struct GlobalState {
|
||||||
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GlobalState {
|
impl ProgramState {
|
||||||
pub fn new(flags: flags::Flags) -> Result<Arc<Self>, AnyError> {
|
pub fn new(flags: flags::Flags) -> Result<Arc<Self>, AnyError> {
|
||||||
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
||||||
let dir = deno_dir::DenoDir::new(custom_root)?;
|
let dir = deno_dir::DenoDir::new(custom_root)?;
|
||||||
|
@ -96,7 +96,7 @@ impl GlobalState {
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let global_state = GlobalState {
|
let program_state = ProgramState {
|
||||||
dir,
|
dir,
|
||||||
permissions: Permissions::from_flags(&flags),
|
permissions: Permissions::from_flags(&flags),
|
||||||
flags,
|
flags,
|
||||||
|
@ -106,7 +106,7 @@ impl GlobalState {
|
||||||
maybe_import_map,
|
maybe_import_map,
|
||||||
maybe_inspector_server,
|
maybe_inspector_server,
|
||||||
};
|
};
|
||||||
Ok(Arc::new(global_state))
|
Ok(Arc::new(program_state))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is called when new module load is
|
/// This function is called when new module load is
|
||||||
|
@ -269,8 +269,8 @@ impl GlobalState {
|
||||||
pub fn mock(
|
pub fn mock(
|
||||||
argv: Vec<String>,
|
argv: Vec<String>,
|
||||||
maybe_flags: Option<flags::Flags>,
|
maybe_flags: Option<flags::Flags>,
|
||||||
) -> Arc<GlobalState> {
|
) -> Arc<ProgramState> {
|
||||||
GlobalState::new(flags::Flags {
|
ProgramState::new(flags::Flags {
|
||||||
argv,
|
argv,
|
||||||
..maybe_flags.unwrap_or_default()
|
..maybe_flags.unwrap_or_default()
|
||||||
})
|
})
|
||||||
|
@ -334,7 +334,7 @@ fn needs_compilation(
|
||||||
#[test]
|
#[test]
|
||||||
fn thread_safe() {
|
fn thread_safe() {
|
||||||
fn f<S: Send + Sync>(_: S) {}
|
fn f<S: Send + Sync>(_: S) {}
|
||||||
f(GlobalState::mock(vec![], None));
|
f(ProgramState::mock(vec![], None));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::inspector::InspectorSession;
|
use crate::inspector::InspectorSession;
|
||||||
|
use crate::program_state::ProgramState;
|
||||||
use crate::worker::MainWorker;
|
use crate::worker::MainWorker;
|
||||||
use crate::worker::Worker;
|
use crate::worker::Worker;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
|
@ -86,12 +86,12 @@ async fn read_line_and_poll(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
global_state: &GlobalState,
|
program_state: &ProgramState,
|
||||||
mut worker: MainWorker,
|
mut worker: MainWorker,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
let mut session = worker.create_inspector_session();
|
let mut session = worker.create_inspector_session();
|
||||||
|
|
||||||
let history_file = global_state.dir.root.join("deno_history.txt");
|
let history_file = program_state.dir.root.join("deno_history.txt");
|
||||||
|
|
||||||
post_message_and_poll(&mut *worker, &mut session, "Runtime.enable", None)
|
post_message_and_poll(&mut *worker, &mut session, "Runtime.enable", None)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
use crate::deno_dir::DenoDir;
|
use crate::deno_dir::DenoDir;
|
||||||
use crate::disk_cache::DiskCache;
|
use crate::disk_cache::DiskCache;
|
||||||
use crate::file_fetcher::SourceFileFetcher;
|
use crate::file_fetcher::SourceFileFetcher;
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::media_type::MediaType;
|
use crate::media_type::MediaType;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
|
use crate::program_state::ProgramState;
|
||||||
|
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::futures::Future;
|
use deno_core::futures::Future;
|
||||||
|
@ -175,13 +175,13 @@ pub struct FetchHandler {
|
||||||
|
|
||||||
impl FetchHandler {
|
impl FetchHandler {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
) -> Result<Self, AnyError> {
|
) -> Result<Self, AnyError> {
|
||||||
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
||||||
let deno_dir = DenoDir::new(custom_root)?;
|
let deno_dir = DenoDir::new(custom_root)?;
|
||||||
let disk_cache = deno_dir.gen_cache;
|
let disk_cache = deno_dir.gen_cache;
|
||||||
let file_fetcher = global_state.file_fetcher.clone();
|
let file_fetcher = program_state.file_fetcher.clone();
|
||||||
|
|
||||||
Ok(FetchHandler {
|
Ok(FetchHandler {
|
||||||
disk_cache,
|
disk_cache,
|
||||||
|
|
54
cli/tsc.rs
54
cli/tsc.rs
|
@ -8,12 +8,12 @@ use crate::disk_cache::DiskCache;
|
||||||
use crate::file_fetcher::SourceFile;
|
use crate::file_fetcher::SourceFile;
|
||||||
use crate::file_fetcher::SourceFileFetcher;
|
use crate::file_fetcher::SourceFileFetcher;
|
||||||
use crate::flags::Flags;
|
use crate::flags::Flags;
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::js;
|
use crate::js;
|
||||||
use crate::media_type::MediaType;
|
use crate::media_type::MediaType;
|
||||||
use crate::module_graph::ModuleGraph;
|
use crate::module_graph::ModuleGraph;
|
||||||
use crate::module_graph::ModuleGraphLoader;
|
use crate::module_graph::ModuleGraphLoader;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
|
use crate::program_state::ProgramState;
|
||||||
use crate::source_maps::SourceMapGetter;
|
use crate::source_maps::SourceMapGetter;
|
||||||
use crate::tsc_config;
|
use crate::tsc_config;
|
||||||
use crate::version;
|
use crate::version;
|
||||||
|
@ -449,7 +449,7 @@ impl TsCompiler {
|
||||||
/// compiler.
|
/// compiler.
|
||||||
pub async fn compile(
|
pub async fn compile(
|
||||||
&self,
|
&self,
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
source_file: &SourceFile,
|
source_file: &SourceFile,
|
||||||
target: TargetLib,
|
target: TargetLib,
|
||||||
module_graph: &ModuleGraph,
|
module_graph: &ModuleGraph,
|
||||||
|
@ -533,7 +533,7 @@ impl TsCompiler {
|
||||||
|
|
||||||
let req_msg = j.to_string();
|
let req_msg = j.to_string();
|
||||||
|
|
||||||
let json_str = execute_in_tsc(global_state.clone(), req_msg)?;
|
let json_str = execute_in_tsc(program_state.clone(), req_msg)?;
|
||||||
|
|
||||||
let compile_response: CompileResponse = serde_json::from_str(&json_str)?;
|
let compile_response: CompileResponse = serde_json::from_str(&json_str)?;
|
||||||
|
|
||||||
|
@ -554,7 +554,7 @@ impl TsCompiler {
|
||||||
/// all the dependencies for that module.
|
/// all the dependencies for that module.
|
||||||
pub async fn bundle(
|
pub async fn bundle(
|
||||||
&self,
|
&self,
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
module_specifier: ModuleSpecifier,
|
module_specifier: ModuleSpecifier,
|
||||||
) -> Result<String, AnyError> {
|
) -> Result<String, AnyError> {
|
||||||
debug!(
|
debug!(
|
||||||
|
@ -565,7 +565,7 @@ impl TsCompiler {
|
||||||
let permissions = Permissions::allow_all();
|
let permissions = Permissions::allow_all();
|
||||||
let mut module_graph_loader = ModuleGraphLoader::new(
|
let mut module_graph_loader = ModuleGraphLoader::new(
|
||||||
self.file_fetcher.clone(),
|
self.file_fetcher.clone(),
|
||||||
global_state.maybe_import_map.clone(),
|
program_state.maybe_import_map.clone(),
|
||||||
permissions.clone(),
|
permissions.clone(),
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
|
@ -576,7 +576,7 @@ impl TsCompiler {
|
||||||
let module_graph = module_graph_loader.get_graph();
|
let module_graph = module_graph_loader.get_graph();
|
||||||
let module_graph_files = module_graph.values().collect::<Vec<_>>();
|
let module_graph_files = module_graph.values().collect::<Vec<_>>();
|
||||||
// Check integrity of every file in module graph
|
// Check integrity of every file in module graph
|
||||||
if let Some(ref lockfile) = global_state.lockfile {
|
if let Some(ref lockfile) = program_state.lockfile {
|
||||||
let mut g = lockfile.lock().unwrap();
|
let mut g = lockfile.lock().unwrap();
|
||||||
|
|
||||||
for graph_file in &module_graph_files {
|
for graph_file in &module_graph_files {
|
||||||
|
@ -592,7 +592,7 @@ impl TsCompiler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(ref lockfile) = global_state.lockfile {
|
if let Some(ref lockfile) = program_state.lockfile {
|
||||||
let g = lockfile.lock().unwrap();
|
let g = lockfile.lock().unwrap();
|
||||||
g.write()?;
|
g.write()?;
|
||||||
}
|
}
|
||||||
|
@ -602,7 +602,7 @@ impl TsCompiler {
|
||||||
let root_names = vec![module_specifier.to_string()];
|
let root_names = vec![module_specifier.to_string()];
|
||||||
let target = "main";
|
let target = "main";
|
||||||
let performance =
|
let performance =
|
||||||
matches!(global_state.flags.log_level, Some(Level::Debug));
|
matches!(program_state.flags.log_level, Some(Level::Debug));
|
||||||
let unstable = self.flags.unstable;
|
let unstable = self.flags.unstable;
|
||||||
|
|
||||||
let mut lib = if target == "main" {
|
let mut lib = if target == "main" {
|
||||||
|
@ -649,7 +649,7 @@ impl TsCompiler {
|
||||||
|
|
||||||
let req_msg = j.to_string();
|
let req_msg = j.to_string();
|
||||||
|
|
||||||
let json_str = execute_in_tsc(global_state.clone(), req_msg)?;
|
let json_str = execute_in_tsc(program_state.clone(), req_msg)?;
|
||||||
|
|
||||||
let bundle_response: BundleResponse = serde_json::from_str(&json_str)?;
|
let bundle_response: BundleResponse = serde_json::from_str(&json_str)?;
|
||||||
|
|
||||||
|
@ -945,7 +945,7 @@ struct CreateHashArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute_in_tsc(
|
fn execute_in_tsc(
|
||||||
global_state: Arc<GlobalState>,
|
program_state: Arc<ProgramState>,
|
||||||
req: String,
|
req: String,
|
||||||
) -> Result<String, AnyError> {
|
) -> Result<String, AnyError> {
|
||||||
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
||||||
|
@ -953,7 +953,7 @@ fn execute_in_tsc(
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
let debug_flag = global_state
|
let debug_flag = program_state
|
||||||
.flags
|
.flags
|
||||||
.log_level
|
.log_level
|
||||||
.map_or(false, |l| l == log::Level::Debug);
|
.map_or(false, |l| l == log::Level::Debug);
|
||||||
|
@ -1006,7 +1006,7 @@ fn execute_in_tsc(
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_runtime_module_graph(
|
async fn create_runtime_module_graph(
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
root_name: &str,
|
root_name: &str,
|
||||||
sources: &Option<HashMap<String, String>>,
|
sources: &Option<HashMap<String, String>>,
|
||||||
|
@ -1014,7 +1014,7 @@ async fn create_runtime_module_graph(
|
||||||
) -> Result<(Vec<String>, ModuleGraph), AnyError> {
|
) -> Result<(Vec<String>, ModuleGraph), AnyError> {
|
||||||
let mut root_names = vec![];
|
let mut root_names = vec![];
|
||||||
let mut module_graph_loader = ModuleGraphLoader::new(
|
let mut module_graph_loader = ModuleGraphLoader::new(
|
||||||
global_state.file_fetcher.clone(),
|
program_state.file_fetcher.clone(),
|
||||||
None,
|
None,
|
||||||
permissions,
|
permissions,
|
||||||
false,
|
false,
|
||||||
|
@ -1057,7 +1057,7 @@ fn extract_js_error(error: AnyError) -> AnyError {
|
||||||
|
|
||||||
/// This function is used by `Deno.compile()` API.
|
/// This function is used by `Deno.compile()` API.
|
||||||
pub async fn runtime_compile(
|
pub async fn runtime_compile(
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
root_name: &str,
|
root_name: &str,
|
||||||
sources: &Option<HashMap<String, String>>,
|
sources: &Option<HashMap<String, String>>,
|
||||||
|
@ -1082,7 +1082,7 @@ pub async fn runtime_compile(
|
||||||
vec![]
|
vec![]
|
||||||
};
|
};
|
||||||
|
|
||||||
let unstable = global_state.flags.unstable;
|
let unstable = program_state.flags.unstable;
|
||||||
|
|
||||||
let mut lib = vec![];
|
let mut lib = vec![];
|
||||||
if let Some(user_libs) = user_options["lib"].take().as_array() {
|
if let Some(user_libs) = user_options["lib"].take().as_array() {
|
||||||
|
@ -1119,7 +1119,7 @@ pub async fn runtime_compile(
|
||||||
tsc_config::json_merge(&mut compiler_options, &json!({ "lib": lib }));
|
tsc_config::json_merge(&mut compiler_options, &json!({ "lib": lib }));
|
||||||
|
|
||||||
let (root_names, module_graph) = create_runtime_module_graph(
|
let (root_names, module_graph) = create_runtime_module_graph(
|
||||||
&global_state,
|
&program_state,
|
||||||
permissions.clone(),
|
permissions.clone(),
|
||||||
root_name,
|
root_name,
|
||||||
sources,
|
sources,
|
||||||
|
@ -1138,10 +1138,10 @@ pub async fn runtime_compile(
|
||||||
})
|
})
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let compiler = global_state.ts_compiler.clone();
|
let compiler = program_state.ts_compiler.clone();
|
||||||
|
|
||||||
let json_str =
|
let json_str =
|
||||||
execute_in_tsc(global_state.clone(), req_msg).map_err(extract_js_error)?;
|
execute_in_tsc(program_state.clone(), req_msg).map_err(extract_js_error)?;
|
||||||
let response: RuntimeCompileResponse = serde_json::from_str(&json_str)?;
|
let response: RuntimeCompileResponse = serde_json::from_str(&json_str)?;
|
||||||
|
|
||||||
if response.diagnostics.0.is_empty() && sources.is_none() {
|
if response.diagnostics.0.is_empty() && sources.is_none() {
|
||||||
|
@ -1156,7 +1156,7 @@ pub async fn runtime_compile(
|
||||||
|
|
||||||
/// This function is used by `Deno.bundle()` API.
|
/// This function is used by `Deno.bundle()` API.
|
||||||
pub async fn runtime_bundle(
|
pub async fn runtime_bundle(
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
root_name: &str,
|
root_name: &str,
|
||||||
sources: &Option<HashMap<String, String>>,
|
sources: &Option<HashMap<String, String>>,
|
||||||
|
@ -1182,7 +1182,7 @@ pub async fn runtime_bundle(
|
||||||
};
|
};
|
||||||
|
|
||||||
let (root_names, module_graph) = create_runtime_module_graph(
|
let (root_names, module_graph) = create_runtime_module_graph(
|
||||||
&global_state,
|
&program_state,
|
||||||
permissions.clone(),
|
permissions.clone(),
|
||||||
root_name,
|
root_name,
|
||||||
sources,
|
sources,
|
||||||
|
@ -1192,7 +1192,7 @@ pub async fn runtime_bundle(
|
||||||
let module_graph_json =
|
let module_graph_json =
|
||||||
serde_json::to_value(module_graph).expect("Failed to serialize data");
|
serde_json::to_value(module_graph).expect("Failed to serialize data");
|
||||||
|
|
||||||
let unstable = global_state.flags.unstable;
|
let unstable = program_state.flags.unstable;
|
||||||
|
|
||||||
let mut lib = vec![];
|
let mut lib = vec![];
|
||||||
if let Some(user_libs) = user_options["lib"].take().as_array() {
|
if let Some(user_libs) = user_options["lib"].take().as_array() {
|
||||||
|
@ -1248,7 +1248,7 @@ pub async fn runtime_bundle(
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let json_str =
|
let json_str =
|
||||||
execute_in_tsc(global_state.clone(), req_msg).map_err(extract_js_error)?;
|
execute_in_tsc(program_state.clone(), req_msg).map_err(extract_js_error)?;
|
||||||
let _response: RuntimeBundleResponse = serde_json::from_str(&json_str)?;
|
let _response: RuntimeBundleResponse = serde_json::from_str(&json_str)?;
|
||||||
// We're returning `Ok()` instead of `Err()` because it's not runtime
|
// We're returning `Ok()` instead of `Err()` because it's not runtime
|
||||||
// error if there were diagnostics produced; we want to let user handle
|
// error if there were diagnostics produced; we want to let user handle
|
||||||
|
@ -1258,7 +1258,7 @@ pub async fn runtime_bundle(
|
||||||
|
|
||||||
/// This function is used by `Deno.transpileOnly()` API.
|
/// This function is used by `Deno.transpileOnly()` API.
|
||||||
pub async fn runtime_transpile(
|
pub async fn runtime_transpile(
|
||||||
global_state: Arc<GlobalState>,
|
program_state: Arc<ProgramState>,
|
||||||
sources: &HashMap<String, String>,
|
sources: &HashMap<String, String>,
|
||||||
maybe_options: &Option<String>,
|
maybe_options: &Option<String>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<Value, AnyError> {
|
||||||
|
@ -1285,7 +1285,7 @@ pub async fn runtime_transpile(
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let json_str =
|
let json_str =
|
||||||
execute_in_tsc(global_state, req_msg).map_err(extract_js_error)?;
|
execute_in_tsc(program_state, req_msg).map_err(extract_js_error)?;
|
||||||
let v = serde_json::from_str::<Value>(&json_str)
|
let v = serde_json::from_str::<Value>(&json_str)
|
||||||
.expect("Error decoding JSON string.");
|
.expect("Error decoding JSON string.");
|
||||||
Ok(v)
|
Ok(v)
|
||||||
|
@ -1451,8 +1451,8 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::deno_dir;
|
use crate::deno_dir;
|
||||||
use crate::fs as deno_fs;
|
use crate::fs as deno_fs;
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::http_cache;
|
use crate::http_cache;
|
||||||
|
use crate::program_state::ProgramState;
|
||||||
use deno_core::ModuleSpecifier;
|
use deno_core::ModuleSpecifier;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
@ -1533,7 +1533,7 @@ mod tests {
|
||||||
deno_dir::DenoDir::new(Some(test_util::new_deno_dir().path().to_owned()))
|
deno_dir::DenoDir::new(Some(test_util::new_deno_dir().path().to_owned()))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let http_cache = http_cache::HttpCache::new(&dir.root.join("deps"));
|
let http_cache = http_cache::HttpCache::new(&dir.root.join("deps"));
|
||||||
let mock_state = GlobalState::mock(
|
let mock_state = ProgramState::mock(
|
||||||
vec![String::from("deno"), String::from("hello.ts")],
|
vec![String::from("deno"), String::from("hello.ts")],
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
@ -1593,7 +1593,7 @@ mod tests {
|
||||||
let module_name =
|
let module_name =
|
||||||
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
|
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
|
||||||
|
|
||||||
let mock_state = GlobalState::mock(
|
let mock_state = ProgramState::mock(
|
||||||
vec![
|
vec![
|
||||||
String::from("deno"),
|
String::from("deno"),
|
||||||
p.to_string_lossy().into(),
|
p.to_string_lossy().into(),
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
use crate::fmt_errors::JsError;
|
use crate::fmt_errors::JsError;
|
||||||
use crate::global_state::GlobalState;
|
|
||||||
use crate::inspector::DenoInspector;
|
use crate::inspector::DenoInspector;
|
||||||
use crate::inspector::InspectorSession;
|
use crate::inspector::InspectorSession;
|
||||||
use crate::js;
|
use crate::js;
|
||||||
use crate::metrics::Metrics;
|
use crate::metrics::Metrics;
|
||||||
|
use crate::module_loader::CliModuleLoader;
|
||||||
use crate::ops;
|
use crate::ops;
|
||||||
use crate::ops::io::get_stdio;
|
use crate::ops::io::get_stdio;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
use crate::state::CliModuleLoader;
|
use crate::program_state::ProgramState;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::futures::channel::mpsc;
|
use deno_core::futures::channel::mpsc;
|
||||||
use deno_core::futures::future::poll_fn;
|
use deno_core::futures::future::poll_fn;
|
||||||
|
@ -110,11 +110,11 @@ impl Worker {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
name: String,
|
name: String,
|
||||||
startup_snapshot: Snapshot,
|
startup_snapshot: Snapshot,
|
||||||
global_state: Arc<GlobalState>,
|
program_state: Arc<ProgramState>,
|
||||||
module_loader: Rc<CliModuleLoader>,
|
module_loader: Rc<CliModuleLoader>,
|
||||||
is_main: bool,
|
is_main: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let global_state_ = global_state.clone();
|
let global_state_ = program_state.clone();
|
||||||
|
|
||||||
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
||||||
module_loader: Some(module_loader),
|
module_loader: Some(module_loader),
|
||||||
|
@ -131,12 +131,12 @@ impl Worker {
|
||||||
}
|
}
|
||||||
|
|
||||||
let inspector =
|
let inspector =
|
||||||
if let Some(inspector_server) = &global_state.maybe_inspector_server {
|
if let Some(inspector_server) = &program_state.maybe_inspector_server {
|
||||||
Some(DenoInspector::new(
|
Some(DenoInspector::new(
|
||||||
&mut js_runtime,
|
&mut js_runtime,
|
||||||
Some(inspector_server.clone()),
|
Some(inspector_server.clone()),
|
||||||
))
|
))
|
||||||
} else if global_state.flags.coverage || global_state.flags.repl {
|
} else if program_state.flags.coverage || program_state.flags.repl {
|
||||||
Some(DenoInspector::new(&mut js_runtime, None))
|
Some(DenoInspector::new(&mut js_runtime, None))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -144,7 +144,7 @@ impl Worker {
|
||||||
|
|
||||||
let should_break_on_first_statement = inspector.is_some()
|
let should_break_on_first_statement = inspector.is_some()
|
||||||
&& is_main
|
&& is_main
|
||||||
&& global_state.flags.inspect_brk.is_some();
|
&& program_state.flags.inspect_brk.is_some();
|
||||||
|
|
||||||
let (internal_channels, external_channels) = create_channels();
|
let (internal_channels, external_channels) = create_channels();
|
||||||
|
|
||||||
|
@ -250,14 +250,14 @@ pub struct MainWorker(Worker);
|
||||||
|
|
||||||
impl MainWorker {
|
impl MainWorker {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
global_state: &Arc<GlobalState>,
|
program_state: &Arc<ProgramState>,
|
||||||
main_module: ModuleSpecifier,
|
main_module: ModuleSpecifier,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let loader = CliModuleLoader::new(global_state.maybe_import_map.clone());
|
let loader = CliModuleLoader::new(program_state.maybe_import_map.clone());
|
||||||
let mut worker = Worker::new(
|
let mut worker = Worker::new(
|
||||||
"main".to_string(),
|
"main".to_string(),
|
||||||
js::deno_isolate_init(),
|
js::deno_isolate_init(),
|
||||||
global_state.clone(),
|
program_state.clone(),
|
||||||
loader,
|
loader,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
@ -268,15 +268,15 @@ impl MainWorker {
|
||||||
let op_state = js_runtime.op_state();
|
let op_state = js_runtime.op_state();
|
||||||
let mut op_state = op_state.borrow_mut();
|
let mut op_state = op_state.borrow_mut();
|
||||||
op_state.put::<Metrics>(Default::default());
|
op_state.put::<Metrics>(Default::default());
|
||||||
op_state.put::<Arc<GlobalState>>(global_state.clone());
|
op_state.put::<Arc<ProgramState>>(program_state.clone());
|
||||||
op_state.put::<Permissions>(global_state.permissions.clone());
|
op_state.put::<Permissions>(program_state.permissions.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
ops::runtime::init(js_runtime, main_module);
|
ops::runtime::init(js_runtime, main_module);
|
||||||
ops::fetch::init(js_runtime, global_state.flags.ca_file.as_deref());
|
ops::fetch::init(js_runtime, program_state.flags.ca_file.as_deref());
|
||||||
ops::timers::init(js_runtime);
|
ops::timers::init(js_runtime);
|
||||||
ops::worker_host::init(js_runtime);
|
ops::worker_host::init(js_runtime);
|
||||||
ops::random::init(js_runtime, global_state.flags.seed);
|
ops::random::init(js_runtime, program_state.flags.seed);
|
||||||
ops::reg_json_sync(js_runtime, "op_close", deno_core::op_close);
|
ops::reg_json_sync(js_runtime, "op_close", deno_core::op_close);
|
||||||
ops::reg_json_sync(js_runtime, "op_resources", deno_core::op_resources);
|
ops::reg_json_sync(js_runtime, "op_resources", deno_core::op_resources);
|
||||||
ops::reg_json_sync(
|
ops::reg_json_sync(
|
||||||
|
@ -399,14 +399,14 @@ impl WebWorker {
|
||||||
name: String,
|
name: String,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
main_module: ModuleSpecifier,
|
main_module: ModuleSpecifier,
|
||||||
global_state: Arc<GlobalState>,
|
program_state: Arc<ProgramState>,
|
||||||
has_deno_namespace: bool,
|
has_deno_namespace: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let loader = CliModuleLoader::new_for_worker();
|
let loader = CliModuleLoader::new_for_worker();
|
||||||
let mut worker = Worker::new(
|
let mut worker = Worker::new(
|
||||||
name,
|
name,
|
||||||
js::deno_isolate_init(),
|
js::deno_isolate_init(),
|
||||||
global_state.clone(),
|
program_state.clone(),
|
||||||
loader,
|
loader,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
@ -439,13 +439,13 @@ impl WebWorker {
|
||||||
let op_state = js_runtime.op_state();
|
let op_state = js_runtime.op_state();
|
||||||
let mut op_state = op_state.borrow_mut();
|
let mut op_state = op_state.borrow_mut();
|
||||||
op_state.put::<Metrics>(Default::default());
|
op_state.put::<Metrics>(Default::default());
|
||||||
op_state.put::<Arc<GlobalState>>(global_state.clone());
|
op_state.put::<Arc<ProgramState>>(program_state.clone());
|
||||||
op_state.put::<Permissions>(permissions);
|
op_state.put::<Permissions>(permissions);
|
||||||
}
|
}
|
||||||
|
|
||||||
ops::web_worker::init(js_runtime, sender, handle);
|
ops::web_worker::init(js_runtime, sender, handle);
|
||||||
ops::runtime::init(js_runtime, main_module);
|
ops::runtime::init(js_runtime, main_module);
|
||||||
ops::fetch::init(js_runtime, global_state.flags.ca_file.as_deref());
|
ops::fetch::init(js_runtime, program_state.flags.ca_file.as_deref());
|
||||||
ops::timers::init(js_runtime);
|
ops::timers::init(js_runtime);
|
||||||
ops::worker_host::init(js_runtime);
|
ops::worker_host::init(js_runtime);
|
||||||
ops::reg_json_sync(js_runtime, "op_close", deno_core::op_close);
|
ops::reg_json_sync(js_runtime, "op_close", deno_core::op_close);
|
||||||
|
@ -467,7 +467,7 @@ impl WebWorker {
|
||||||
ops::permissions::init(js_runtime);
|
ops::permissions::init(js_runtime);
|
||||||
ops::plugin::init(js_runtime);
|
ops::plugin::init(js_runtime);
|
||||||
ops::process::init(js_runtime);
|
ops::process::init(js_runtime);
|
||||||
ops::random::init(js_runtime, global_state.flags.seed);
|
ops::random::init(js_runtime, program_state.flags.seed);
|
||||||
ops::runtime_compiler::init(js_runtime);
|
ops::runtime_compiler::init(js_runtime);
|
||||||
ops::signal::init(js_runtime);
|
ops::signal::init(js_runtime);
|
||||||
ops::tls::init(js_runtime);
|
ops::tls::init(js_runtime);
|
||||||
|
@ -579,7 +579,7 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::flags::DenoSubcommand;
|
use crate::flags::DenoSubcommand;
|
||||||
use crate::flags::Flags;
|
use crate::flags::Flags;
|
||||||
use crate::global_state::GlobalState;
|
use crate::program_state::ProgramState;
|
||||||
use crate::tokio_util;
|
use crate::tokio_util;
|
||||||
use crate::worker::WorkerEvent;
|
use crate::worker::WorkerEvent;
|
||||||
use deno_core::serde_json::json;
|
use deno_core::serde_json::json;
|
||||||
|
@ -593,8 +593,9 @@ mod tests {
|
||||||
},
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let global_state = GlobalState::mock(vec!["deno".to_string()], Some(flags));
|
let program_state =
|
||||||
MainWorker::new(&global_state, main_module)
|
ProgramState::mock(vec!["deno".to_string()], Some(flags));
|
||||||
|
MainWorker::new(&program_state, main_module)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
@ -680,12 +681,12 @@ mod tests {
|
||||||
fn create_test_web_worker() -> WebWorker {
|
fn create_test_web_worker() -> WebWorker {
|
||||||
let main_module =
|
let main_module =
|
||||||
ModuleSpecifier::resolve_url_or_path("./hello.js").unwrap();
|
ModuleSpecifier::resolve_url_or_path("./hello.js").unwrap();
|
||||||
let global_state = GlobalState::mock(vec!["deno".to_string()], None);
|
let program_state = ProgramState::mock(vec!["deno".to_string()], None);
|
||||||
let mut worker = WebWorker::new(
|
let mut worker = WebWorker::new(
|
||||||
"TEST".to_string(),
|
"TEST".to_string(),
|
||||||
Permissions::allow_all(),
|
Permissions::allow_all(),
|
||||||
main_module,
|
main_module,
|
||||||
global_state,
|
program_state,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
worker
|
worker
|
||||||
|
|
Loading…
Reference in a new issue