From 22a2afe5588ae71301db6b9a6000d241ef1e762a Mon Sep 17 00:00:00 2001 From: Gurwinder Singh Date: Sun, 15 Dec 2019 03:42:34 +0530 Subject: [PATCH] Use async-await at few places, fix spelling mistake (#3499) --- cli/file_fetcher.rs | 69 +++++++++++++++++------------------ cli/global_state.rs | 53 ++++++++++++++------------- cli/tests/lock_check_err.out | 2 +- cli/tests/lock_check_err2.out | 2 +- 4 files changed, 63 insertions(+), 63 deletions(-) diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs index 5c68b33867..1e147ab323 100644 --- a/cli/file_fetcher.rs +++ b/cli/file_fetcher.rs @@ -135,7 +135,7 @@ impl SourceFileFetcher { } pub fn fetch_source_file_async( - self: &Self, + &self, specifier: &ModuleSpecifier, maybe_referrer: Option, ) -> Pin> { @@ -145,61 +145,58 @@ impl SourceFileFetcher { // Check if this file was already fetched and can be retrieved from in-process cache. if let Some(source_file) = self.source_file_cache.get(specifier.to_string()) { - return futures::future::ok(source_file).boxed(); + return Box::pin(async { Ok(source_file) }); } let source_file_cache = self.source_file_cache.clone(); let specifier_ = specifier.clone(); - let fut = self - .get_source_file_async( - &module_url, - self.use_disk_cache, - self.no_remote, - self.cached_only, - ) - .then(move |result| { - let mut out = match result.map_err(|err| { + let source_file = self.get_source_file_async( + &module_url, + self.use_disk_cache, + self.no_remote, + self.cached_only, + ); + + Box::pin(async move { + match source_file.await { + Ok(mut file) => { + // TODO: move somewhere? + if file.source_code.starts_with(b"#!") { + file.source_code = filter_shebang(file.source_code); + } + + // Cache in-process for subsequent access. + source_file_cache.set(specifier_.to_string(), file.clone()); + + Ok(file) + } + Err(err) => { let err_kind = err.kind(); let referrer_suffix = if let Some(referrer) = maybe_referrer { - format!(" from \"{}\"", referrer) + format!(r#" from "{}""#, referrer) } else { "".to_owned() }; - if err_kind == ErrorKind::NotFound { + let err = if err_kind == ErrorKind::NotFound { let msg = format!( - "Cannot resolve module \"{}\"{}", - module_url.to_string(), - referrer_suffix + r#"Cannot resolve module "{}"{}"#, + module_url, referrer_suffix ); DenoError::new(ErrorKind::NotFound, msg).into() } else if err_kind == ErrorKind::PermissionDenied { let msg = format!( - "Cannot find module \"{}\"{} in cache, --cached-only is specified", - module_url.to_string(), - referrer_suffix + r#"Cannot find module "{}"{} in cache, --cached-only is specified"#, + module_url, referrer_suffix ); DenoError::new(ErrorKind::PermissionDenied, msg).into() } else { err - } - }) { - Ok(v) => v, - Err(e) => return futures::future::err(e), - }; - - // TODO: move somewhere? - if out.source_code.starts_with(b"#!") { - out.source_code = filter_shebang(out.source_code); + }; + Err(err) } - - // Cache in-process for subsequent access. - source_file_cache.set(specifier_.to_string(), out.clone()); - - futures::future::ok(out) - }); - - fut.boxed() + } + }) } /// This is main method that is responsible for fetching local or remote files. diff --git a/cli/global_state.rs b/cli/global_state.rs index 981d73788e..bafee47ede 100644 --- a/cli/global_state.rs +++ b/cli/global_state.rs @@ -15,7 +15,6 @@ use crate::permissions::DenoPermissions; use crate::progress::Progress; use deno::ErrBox; use deno::ModuleSpecifier; -use futures::future::TryFutureExt; use std; use std::env; use std::future::Future; @@ -119,17 +118,20 @@ impl ThreadSafeGlobalState { } pub fn fetch_compiled_module( - self: &Self, + &self, module_specifier: &ModuleSpecifier, maybe_referrer: Option, ) -> impl Future> { let state1 = self.clone(); let state2 = self.clone(); - self + let source_file = self .file_fetcher - .fetch_source_file_async(&module_specifier, maybe_referrer) - .and_then(move |out| match out.media_type { + .fetch_source_file_async(&module_specifier, maybe_referrer); + + async move { + let out = source_file.await?; + let compiled_module = match out.media_type { msg::MediaType::Unknown => state1.js_compiler.compile_async(&out), msg::MediaType::Json => state1.json_compiler.compile_async(&out), msg::MediaType::Wasm => { @@ -147,28 +149,29 @@ impl ThreadSafeGlobalState { state1.js_compiler.compile_async(&out) } } - }) - .and_then(move |compiled_module| { - if let Some(ref lockfile) = state2.lockfile { - let mut g = lockfile.lock().unwrap(); - if state2.flags.lock_write { - g.insert(&compiled_module); - } else { - let check = match g.check(&compiled_module) { - Err(e) => return futures::future::err(ErrBox::from(e)), - Ok(v) => v, - }; - if !check { - eprintln!( - "Subresource integrety check failed --lock={}\n{}", - g.filename, compiled_module.name - ); - std::process::exit(10); - } + } + .await?; + + if let Some(ref lockfile) = state2.lockfile { + let mut g = lockfile.lock().unwrap(); + if state2.flags.lock_write { + g.insert(&compiled_module); + } else { + let check = match g.check(&compiled_module) { + Err(e) => return Err(ErrBox::from(e)), + Ok(v) => v, + }; + if !check { + eprintln!( + "Subresource integrity check failed --lock={}\n{}", + g.filename, compiled_module.name + ); + std::process::exit(10); } } - futures::future::ok(compiled_module) - }) + } + Ok(compiled_module) + } } #[inline] diff --git a/cli/tests/lock_check_err.out b/cli/tests/lock_check_err.out index 823df3183b..87f0242f7a 100644 --- a/cli/tests/lock_check_err.out +++ b/cli/tests/lock_check_err.out @@ -1,2 +1,2 @@ -[WILDCARD]Subresource integrety check failed --lock=lock_check_err.json +[WILDCARD]Subresource integrity check failed --lock=lock_check_err.json http://127.0.0.1:4545/cli/tests/003_relative_import.ts diff --git a/cli/tests/lock_check_err2.out b/cli/tests/lock_check_err2.out index fd8635a7f8..6b81c9713a 100644 --- a/cli/tests/lock_check_err2.out +++ b/cli/tests/lock_check_err2.out @@ -1,2 +1,2 @@ -[WILDCARD]Subresource integrety check failed --lock=lock_check_err2.json +[WILDCARD]Subresource integrity check failed --lock=lock_check_err2.json http://localhost:4545/cli/tests/subdir/mt_text_ecmascript.j3.js