mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 16:19:12 -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.
|
||||
use crate::deno_dir::DenoDir;
|
||||
use crate::deno_dir::SourceFile;
|
||||
use crate::deno_dir::SourceFileFetcher;
|
||||
use crate::diagnostics::Diagnostic;
|
||||
use crate::disk_cache::DiskCache;
|
||||
use crate::file_fetcher::SourceFile;
|
||||
use crate::file_fetcher::SourceFileFetcher;
|
||||
use crate::msg;
|
||||
use crate::resources;
|
||||
use crate::source_maps::SourceMapGetter;
|
||||
|
@ -160,7 +159,7 @@ fn load_config_file(
|
|||
}
|
||||
|
||||
pub struct TsCompiler {
|
||||
pub deno_dir: DenoDir,
|
||||
pub file_fetcher: SourceFileFetcher,
|
||||
pub config: CompilerConfig,
|
||||
pub config_hash: Vec<u8>,
|
||||
pub disk_cache: DiskCache,
|
||||
|
@ -174,7 +173,8 @@ pub struct TsCompiler {
|
|||
|
||||
impl TsCompiler {
|
||||
pub fn new(
|
||||
deno_dir: DenoDir,
|
||||
file_fetcher: SourceFileFetcher,
|
||||
disk_cache: DiskCache,
|
||||
use_disk_cache: bool,
|
||||
config_path: Option<String>,
|
||||
) -> Self {
|
||||
|
@ -189,8 +189,8 @@ impl TsCompiler {
|
|||
};
|
||||
|
||||
Self {
|
||||
disk_cache: deno_dir.clone().gen_cache,
|
||||
deno_dir,
|
||||
file_fetcher,
|
||||
disk_cache,
|
||||
config: compiler_config,
|
||||
config_hash: config_bytes,
|
||||
compiled: Mutex::new(HashSet::new()),
|
||||
|
@ -474,7 +474,7 @@ impl TsCompiler {
|
|||
self.mark_compiled(module_specifier.as_url());
|
||||
|
||||
let source_file = self
|
||||
.deno_dir
|
||||
.file_fetcher
|
||||
.fetch_source_file(&module_specifier)
|
||||
.expect("Source file not found");
|
||||
|
||||
|
@ -583,7 +583,7 @@ impl TsCompiler {
|
|||
script_name: &str,
|
||||
) -> Option<SourceFile> {
|
||||
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),
|
||||
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;
|
||||
mod disk_cache;
|
||||
mod dispatch_minimal;
|
||||
mod file_fetcher;
|
||||
pub mod flags;
|
||||
pub mod fmt_errors;
|
||||
mod fs;
|
||||
|
@ -47,7 +48,6 @@ mod tokio_write;
|
|||
pub mod version;
|
||||
pub mod worker;
|
||||
|
||||
use crate::deno_dir::SourceFileFetcher;
|
||||
use crate::progress::Progress;
|
||||
use crate::state::ThreadSafeState;
|
||||
use crate::worker::Worker;
|
||||
|
@ -107,7 +107,7 @@ pub fn print_file_info(
|
|||
let module_specifier_ = module_specifier.clone();
|
||||
|
||||
state_
|
||||
.dir
|
||||
.file_fetcher
|
||||
.fetch_source_file_async(&module_specifier)
|
||||
.map_err(|err| println!("{}", err))
|
||||
.and_then(move |out| {
|
||||
|
@ -191,7 +191,8 @@ fn create_worker_and_state(
|
|||
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(
|
||||
"main".to_string(),
|
||||
startup_data::deno_isolate_init(),
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
use atty;
|
||||
use crate::ansi;
|
||||
use crate::deno_dir::SourceFileFetcher;
|
||||
use crate::deno_error;
|
||||
use crate::deno_error::DenoError;
|
||||
use crate::deno_error::ErrorKind;
|
||||
|
@ -506,7 +505,7 @@ fn op_fetch_source_file(
|
|||
let resolved_specifier = state.resolve(specifier, referrer, false)?;
|
||||
|
||||
let fut = state
|
||||
.dir
|
||||
.file_fetcher
|
||||
.fetch_source_file_async(&resolved_specifier)
|
||||
.and_then(move |out| {
|
||||
let builder = &mut FlatBufferBuilder::new();
|
||||
|
@ -2064,7 +2063,7 @@ fn op_create_worker(
|
|||
parent_state.argv.clone(),
|
||||
op_selector_std,
|
||||
parent_state.progress.clone(),
|
||||
);
|
||||
)?;
|
||||
let rid = child_state.resource.rid;
|
||||
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.
|
||||
use crate::compiler::TsCompiler;
|
||||
use crate::deno_dir;
|
||||
use crate::deno_dir::SourceFile;
|
||||
use crate::deno_dir::SourceFileFetcher;
|
||||
use crate::file_fetcher::SourceFile;
|
||||
use crate::file_fetcher::SourceFileFetcher;
|
||||
use crate::flags;
|
||||
use crate::global_timer::GlobalTimer;
|
||||
use crate::import_map::ImportMap;
|
||||
|
@ -76,6 +76,7 @@ pub struct State {
|
|||
pub progress: Progress,
|
||||
pub seeded_rng: Option<Mutex<StdRng>>,
|
||||
|
||||
pub file_fetcher: SourceFileFetcher,
|
||||
pub ts_compiler: TsCompiler,
|
||||
}
|
||||
|
||||
|
@ -109,7 +110,7 @@ pub fn fetch_source_file_and_maybe_compile_async(
|
|||
let state_ = state.clone();
|
||||
|
||||
state_
|
||||
.dir
|
||||
.file_fetcher
|
||||
.fetch_source_file_async(&module_specifier)
|
||||
.and_then(move |out| {
|
||||
state_
|
||||
|
@ -168,7 +169,7 @@ impl ThreadSafeState {
|
|||
argv_rest: Vec<String>,
|
||||
dispatch_selector: ops::OpSelector,
|
||||
progress: Progress,
|
||||
) -> Self {
|
||||
) -> Result<Self, ErrBox> {
|
||||
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
||||
|
||||
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 resource = resources::add_worker(external_channels);
|
||||
|
||||
let dir = deno_dir::DenoDir::new(
|
||||
custom_root,
|
||||
let dir = deno_dir::DenoDir::new(custom_root)?;
|
||||
|
||||
let file_fetcher = SourceFileFetcher::new(
|
||||
dir.deps_cache.clone(),
|
||||
progress.clone(),
|
||||
!flags.reload,
|
||||
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 {
|
||||
None
|
||||
|
@ -220,10 +230,7 @@ impl ThreadSafeState {
|
|||
|
||||
let modules = Arc::new(Mutex::new(deno::Modules::new()));
|
||||
|
||||
let ts_compiler =
|
||||
TsCompiler::new(dir.clone(), !flags.reload, flags.config_path.clone());
|
||||
|
||||
ThreadSafeState(Arc::new(State {
|
||||
let state = State {
|
||||
main_module,
|
||||
modules,
|
||||
dir,
|
||||
|
@ -240,8 +247,11 @@ impl ThreadSafeState {
|
|||
dispatch_selector,
|
||||
progress,
|
||||
seeded_rng,
|
||||
file_fetcher,
|
||||
ts_compiler,
|
||||
}))
|
||||
};
|
||||
|
||||
Ok(ThreadSafeState(Arc::new(state)))
|
||||
}
|
||||
|
||||
/// Read main module from argv
|
||||
|
@ -289,7 +299,7 @@ impl ThreadSafeState {
|
|||
argv,
|
||||
ops::op_selector_std,
|
||||
Progress::new(),
|
||||
)
|
||||
).unwrap()
|
||||
}
|
||||
|
||||
pub fn metrics_op_dispatched(
|
||||
|
|
|
@ -126,7 +126,7 @@ mod tests {
|
|||
argv,
|
||||
op_selector_std,
|
||||
Progress::new(),
|
||||
);
|
||||
).unwrap();
|
||||
let state_ = state.clone();
|
||||
tokio_util::run(lazy(move || {
|
||||
let mut worker =
|
||||
|
@ -154,7 +154,7 @@ mod tests {
|
|||
argv,
|
||||
op_selector_std,
|
||||
Progress::new(),
|
||||
);
|
||||
).unwrap();
|
||||
let state_ = state.clone();
|
||||
tokio_util::run(lazy(move || {
|
||||
let mut worker =
|
||||
|
@ -180,7 +180,8 @@ mod tests {
|
|||
let mut flags = flags::DenoFlags::default();
|
||||
flags.reload = true;
|
||||
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();
|
||||
tokio_util::run(lazy(move || {
|
||||
let mut worker = Worker::new(
|
||||
|
|
Loading…
Reference in a new issue