mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 00:29:09 -05:00
factor out FileFetcher to separate module (#2683)
* merge SourceFileFetcher trait and FileFetcher struct * move logic related to source file fetching to //cli/file_fetcher.rs * use Result when creating new ThreadSafeState
This commit is contained in:
parent
ef63ec763a
commit
421cbd39b4
7 changed files with 1499 additions and 1496 deletions
|
@ -1,9 +1,8 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
use crate::deno_dir::DenoDir;
|
|
||||||
use crate::deno_dir::SourceFile;
|
|
||||||
use crate::deno_dir::SourceFileFetcher;
|
|
||||||
use crate::diagnostics::Diagnostic;
|
use crate::diagnostics::Diagnostic;
|
||||||
use crate::disk_cache::DiskCache;
|
use crate::disk_cache::DiskCache;
|
||||||
|
use crate::file_fetcher::SourceFile;
|
||||||
|
use crate::file_fetcher::SourceFileFetcher;
|
||||||
use crate::msg;
|
use crate::msg;
|
||||||
use crate::resources;
|
use crate::resources;
|
||||||
use crate::source_maps::SourceMapGetter;
|
use crate::source_maps::SourceMapGetter;
|
||||||
|
@ -160,7 +159,7 @@ fn load_config_file(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TsCompiler {
|
pub struct TsCompiler {
|
||||||
pub deno_dir: DenoDir,
|
pub file_fetcher: SourceFileFetcher,
|
||||||
pub config: CompilerConfig,
|
pub config: CompilerConfig,
|
||||||
pub config_hash: Vec<u8>,
|
pub config_hash: Vec<u8>,
|
||||||
pub disk_cache: DiskCache,
|
pub disk_cache: DiskCache,
|
||||||
|
@ -174,7 +173,8 @@ pub struct TsCompiler {
|
||||||
|
|
||||||
impl TsCompiler {
|
impl TsCompiler {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
deno_dir: DenoDir,
|
file_fetcher: SourceFileFetcher,
|
||||||
|
disk_cache: DiskCache,
|
||||||
use_disk_cache: bool,
|
use_disk_cache: bool,
|
||||||
config_path: Option<String>,
|
config_path: Option<String>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -189,8 +189,8 @@ impl TsCompiler {
|
||||||
};
|
};
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
disk_cache: deno_dir.clone().gen_cache,
|
file_fetcher,
|
||||||
deno_dir,
|
disk_cache,
|
||||||
config: compiler_config,
|
config: compiler_config,
|
||||||
config_hash: config_bytes,
|
config_hash: config_bytes,
|
||||||
compiled: Mutex::new(HashSet::new()),
|
compiled: Mutex::new(HashSet::new()),
|
||||||
|
@ -474,7 +474,7 @@ impl TsCompiler {
|
||||||
self.mark_compiled(module_specifier.as_url());
|
self.mark_compiled(module_specifier.as_url());
|
||||||
|
|
||||||
let source_file = self
|
let source_file = self
|
||||||
.deno_dir
|
.file_fetcher
|
||||||
.fetch_source_file(&module_specifier)
|
.fetch_source_file(&module_specifier)
|
||||||
.expect("Source file not found");
|
.expect("Source file not found");
|
||||||
|
|
||||||
|
@ -583,7 +583,7 @@ impl TsCompiler {
|
||||||
script_name: &str,
|
script_name: &str,
|
||||||
) -> Option<SourceFile> {
|
) -> Option<SourceFile> {
|
||||||
if let Some(module_specifier) = self.try_to_resolve(script_name) {
|
if let Some(module_specifier) = self.try_to_resolve(script_name) {
|
||||||
return match self.deno_dir.fetch_source_file(&module_specifier) {
|
return match self.file_fetcher.fetch_source_file(&module_specifier) {
|
||||||
Ok(out) => Some(out),
|
Ok(out) => Some(out),
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
};
|
};
|
||||||
|
|
1473
cli/deno_dir.rs
1473
cli/deno_dir.rs
File diff suppressed because it is too large
Load diff
1449
cli/file_fetcher.rs
Normal file
1449
cli/file_fetcher.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -22,6 +22,7 @@ pub mod deno_error;
|
||||||
pub mod diagnostics;
|
pub mod diagnostics;
|
||||||
mod disk_cache;
|
mod disk_cache;
|
||||||
mod dispatch_minimal;
|
mod dispatch_minimal;
|
||||||
|
mod file_fetcher;
|
||||||
pub mod flags;
|
pub mod flags;
|
||||||
pub mod fmt_errors;
|
pub mod fmt_errors;
|
||||||
mod fs;
|
mod fs;
|
||||||
|
@ -47,7 +48,6 @@ mod tokio_write;
|
||||||
pub mod version;
|
pub mod version;
|
||||||
pub mod worker;
|
pub mod worker;
|
||||||
|
|
||||||
use crate::deno_dir::SourceFileFetcher;
|
|
||||||
use crate::progress::Progress;
|
use crate::progress::Progress;
|
||||||
use crate::state::ThreadSafeState;
|
use crate::state::ThreadSafeState;
|
||||||
use crate::worker::Worker;
|
use crate::worker::Worker;
|
||||||
|
@ -107,7 +107,7 @@ pub fn print_file_info(
|
||||||
let module_specifier_ = module_specifier.clone();
|
let module_specifier_ = module_specifier.clone();
|
||||||
|
|
||||||
state_
|
state_
|
||||||
.dir
|
.file_fetcher
|
||||||
.fetch_source_file_async(&module_specifier)
|
.fetch_source_file_async(&module_specifier)
|
||||||
.map_err(|err| println!("{}", err))
|
.map_err(|err| println!("{}", err))
|
||||||
.and_then(move |out| {
|
.and_then(move |out| {
|
||||||
|
@ -191,7 +191,8 @@ fn create_worker_and_state(
|
||||||
s.status(status, msg).expect("shell problem");
|
s.status(status, msg).expect("shell problem");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let state = ThreadSafeState::new(flags, argv, ops::op_selector_std, progress);
|
let state =
|
||||||
|
ThreadSafeState::new(flags, argv, ops::op_selector_std, progress).unwrap();
|
||||||
let worker = Worker::new(
|
let worker = Worker::new(
|
||||||
"main".to_string(),
|
"main".to_string(),
|
||||||
startup_data::deno_isolate_init(),
|
startup_data::deno_isolate_init(),
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
use atty;
|
use atty;
|
||||||
use crate::ansi;
|
use crate::ansi;
|
||||||
use crate::deno_dir::SourceFileFetcher;
|
|
||||||
use crate::deno_error;
|
use crate::deno_error;
|
||||||
use crate::deno_error::DenoError;
|
use crate::deno_error::DenoError;
|
||||||
use crate::deno_error::ErrorKind;
|
use crate::deno_error::ErrorKind;
|
||||||
|
@ -506,7 +505,7 @@ fn op_fetch_source_file(
|
||||||
let resolved_specifier = state.resolve(specifier, referrer, false)?;
|
let resolved_specifier = state.resolve(specifier, referrer, false)?;
|
||||||
|
|
||||||
let fut = state
|
let fut = state
|
||||||
.dir
|
.file_fetcher
|
||||||
.fetch_source_file_async(&resolved_specifier)
|
.fetch_source_file_async(&resolved_specifier)
|
||||||
.and_then(move |out| {
|
.and_then(move |out| {
|
||||||
let builder = &mut FlatBufferBuilder::new();
|
let builder = &mut FlatBufferBuilder::new();
|
||||||
|
@ -2064,7 +2063,7 @@ fn op_create_worker(
|
||||||
parent_state.argv.clone(),
|
parent_state.argv.clone(),
|
||||||
op_selector_std,
|
op_selector_std,
|
||||||
parent_state.progress.clone(),
|
parent_state.progress.clone(),
|
||||||
);
|
)?;
|
||||||
let rid = child_state.resource.rid;
|
let rid = child_state.resource.rid;
|
||||||
let name = format!("USER-WORKER-{}", specifier);
|
let name = format!("USER-WORKER-{}", specifier);
|
||||||
|
|
||||||
|
|
36
cli/state.rs
36
cli/state.rs
|
@ -1,8 +1,8 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
use crate::compiler::TsCompiler;
|
use crate::compiler::TsCompiler;
|
||||||
use crate::deno_dir;
|
use crate::deno_dir;
|
||||||
use crate::deno_dir::SourceFile;
|
use crate::file_fetcher::SourceFile;
|
||||||
use crate::deno_dir::SourceFileFetcher;
|
use crate::file_fetcher::SourceFileFetcher;
|
||||||
use crate::flags;
|
use crate::flags;
|
||||||
use crate::global_timer::GlobalTimer;
|
use crate::global_timer::GlobalTimer;
|
||||||
use crate::import_map::ImportMap;
|
use crate::import_map::ImportMap;
|
||||||
|
@ -76,6 +76,7 @@ pub struct State {
|
||||||
pub progress: Progress,
|
pub progress: Progress,
|
||||||
pub seeded_rng: Option<Mutex<StdRng>>,
|
pub seeded_rng: Option<Mutex<StdRng>>,
|
||||||
|
|
||||||
|
pub file_fetcher: SourceFileFetcher,
|
||||||
pub ts_compiler: TsCompiler,
|
pub ts_compiler: TsCompiler,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +110,7 @@ pub fn fetch_source_file_and_maybe_compile_async(
|
||||||
let state_ = state.clone();
|
let state_ = state.clone();
|
||||||
|
|
||||||
state_
|
state_
|
||||||
.dir
|
.file_fetcher
|
||||||
.fetch_source_file_async(&module_specifier)
|
.fetch_source_file_async(&module_specifier)
|
||||||
.and_then(move |out| {
|
.and_then(move |out| {
|
||||||
state_
|
state_
|
||||||
|
@ -168,7 +169,7 @@ impl ThreadSafeState {
|
||||||
argv_rest: Vec<String>,
|
argv_rest: Vec<String>,
|
||||||
dispatch_selector: ops::OpSelector,
|
dispatch_selector: ops::OpSelector,
|
||||||
progress: Progress,
|
progress: Progress,
|
||||||
) -> Self {
|
) -> Result<Self, ErrBox> {
|
||||||
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
||||||
|
|
||||||
let (worker_in_tx, worker_in_rx) = async_mpsc::channel::<Buf>(1);
|
let (worker_in_tx, worker_in_rx) = async_mpsc::channel::<Buf>(1);
|
||||||
|
@ -177,12 +178,21 @@ impl ThreadSafeState {
|
||||||
let external_channels = (worker_in_tx, worker_out_rx);
|
let external_channels = (worker_in_tx, worker_out_rx);
|
||||||
let resource = resources::add_worker(external_channels);
|
let resource = resources::add_worker(external_channels);
|
||||||
|
|
||||||
let dir = deno_dir::DenoDir::new(
|
let dir = deno_dir::DenoDir::new(custom_root)?;
|
||||||
custom_root,
|
|
||||||
|
let file_fetcher = SourceFileFetcher::new(
|
||||||
|
dir.deps_cache.clone(),
|
||||||
progress.clone(),
|
progress.clone(),
|
||||||
!flags.reload,
|
!flags.reload,
|
||||||
flags.no_fetch,
|
flags.no_fetch,
|
||||||
).unwrap();
|
)?;
|
||||||
|
|
||||||
|
let ts_compiler = TsCompiler::new(
|
||||||
|
file_fetcher.clone(),
|
||||||
|
dir.gen_cache.clone(),
|
||||||
|
!flags.reload,
|
||||||
|
flags.config_path.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
let main_module: Option<ModuleSpecifier> = if argv_rest.len() <= 1 {
|
let main_module: Option<ModuleSpecifier> = if argv_rest.len() <= 1 {
|
||||||
None
|
None
|
||||||
|
@ -220,10 +230,7 @@ impl ThreadSafeState {
|
||||||
|
|
||||||
let modules = Arc::new(Mutex::new(deno::Modules::new()));
|
let modules = Arc::new(Mutex::new(deno::Modules::new()));
|
||||||
|
|
||||||
let ts_compiler =
|
let state = State {
|
||||||
TsCompiler::new(dir.clone(), !flags.reload, flags.config_path.clone());
|
|
||||||
|
|
||||||
ThreadSafeState(Arc::new(State {
|
|
||||||
main_module,
|
main_module,
|
||||||
modules,
|
modules,
|
||||||
dir,
|
dir,
|
||||||
|
@ -240,8 +247,11 @@ impl ThreadSafeState {
|
||||||
dispatch_selector,
|
dispatch_selector,
|
||||||
progress,
|
progress,
|
||||||
seeded_rng,
|
seeded_rng,
|
||||||
|
file_fetcher,
|
||||||
ts_compiler,
|
ts_compiler,
|
||||||
}))
|
};
|
||||||
|
|
||||||
|
Ok(ThreadSafeState(Arc::new(state)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read main module from argv
|
/// Read main module from argv
|
||||||
|
@ -289,7 +299,7 @@ impl ThreadSafeState {
|
||||||
argv,
|
argv,
|
||||||
ops::op_selector_std,
|
ops::op_selector_std,
|
||||||
Progress::new(),
|
Progress::new(),
|
||||||
)
|
).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn metrics_op_dispatched(
|
pub fn metrics_op_dispatched(
|
||||||
|
|
|
@ -126,7 +126,7 @@ mod tests {
|
||||||
argv,
|
argv,
|
||||||
op_selector_std,
|
op_selector_std,
|
||||||
Progress::new(),
|
Progress::new(),
|
||||||
);
|
).unwrap();
|
||||||
let state_ = state.clone();
|
let state_ = state.clone();
|
||||||
tokio_util::run(lazy(move || {
|
tokio_util::run(lazy(move || {
|
||||||
let mut worker =
|
let mut worker =
|
||||||
|
@ -154,7 +154,7 @@ mod tests {
|
||||||
argv,
|
argv,
|
||||||
op_selector_std,
|
op_selector_std,
|
||||||
Progress::new(),
|
Progress::new(),
|
||||||
);
|
).unwrap();
|
||||||
let state_ = state.clone();
|
let state_ = state.clone();
|
||||||
tokio_util::run(lazy(move || {
|
tokio_util::run(lazy(move || {
|
||||||
let mut worker =
|
let mut worker =
|
||||||
|
@ -180,7 +180,8 @@ mod tests {
|
||||||
let mut flags = flags::DenoFlags::default();
|
let mut flags = flags::DenoFlags::default();
|
||||||
flags.reload = true;
|
flags.reload = true;
|
||||||
let state =
|
let state =
|
||||||
ThreadSafeState::new(flags, argv, op_selector_std, Progress::new());
|
ThreadSafeState::new(flags, argv, op_selector_std, Progress::new())
|
||||||
|
.unwrap();
|
||||||
let state_ = state.clone();
|
let state_ = state.clone();
|
||||||
tokio_util::run(lazy(move || {
|
tokio_util::run(lazy(move || {
|
||||||
let mut worker = Worker::new(
|
let mut worker = Worker::new(
|
||||||
|
|
Loading…
Reference in a new issue