1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

Use async-await at few places, fix spelling mistake (#3499)

This commit is contained in:
Gurwinder Singh 2019-12-15 03:42:34 +05:30 committed by Ry Dahl
parent 83f95fb8df
commit 22a2afe558
4 changed files with 63 additions and 63 deletions

View file

@ -135,7 +135,7 @@ impl SourceFileFetcher {
} }
pub fn fetch_source_file_async( pub fn fetch_source_file_async(
self: &Self, &self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>, maybe_referrer: Option<ModuleSpecifier>,
) -> Pin<Box<SourceFileFuture>> { ) -> Pin<Box<SourceFileFuture>> {
@ -145,61 +145,58 @@ impl SourceFileFetcher {
// Check if this file was already fetched and can be retrieved from in-process cache. // 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()) 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 source_file_cache = self.source_file_cache.clone();
let specifier_ = specifier.clone(); let specifier_ = specifier.clone();
let fut = self let source_file = self.get_source_file_async(
.get_source_file_async(
&module_url, &module_url,
self.use_disk_cache, self.use_disk_cache,
self.no_remote, self.no_remote,
self.cached_only, self.cached_only,
) );
.then(move |result| {
let mut out = match result.map_err(|err| { 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 err_kind = err.kind();
let referrer_suffix = if let Some(referrer) = maybe_referrer { let referrer_suffix = if let Some(referrer) = maybe_referrer {
format!(" from \"{}\"", referrer) format!(r#" from "{}""#, referrer)
} else { } else {
"".to_owned() "".to_owned()
}; };
if err_kind == ErrorKind::NotFound { let err = if err_kind == ErrorKind::NotFound {
let msg = format!( let msg = format!(
"Cannot resolve module \"{}\"{}", r#"Cannot resolve module "{}"{}"#,
module_url.to_string(), module_url, referrer_suffix
referrer_suffix
); );
DenoError::new(ErrorKind::NotFound, msg).into() DenoError::new(ErrorKind::NotFound, msg).into()
} else if err_kind == ErrorKind::PermissionDenied { } else if err_kind == ErrorKind::PermissionDenied {
let msg = format!( let msg = format!(
"Cannot find module \"{}\"{} in cache, --cached-only is specified", r#"Cannot find module "{}"{} in cache, --cached-only is specified"#,
module_url.to_string(), module_url, referrer_suffix
referrer_suffix
); );
DenoError::new(ErrorKind::PermissionDenied, msg).into() DenoError::new(ErrorKind::PermissionDenied, msg).into()
} else { } else {
err err
}
}) {
Ok(v) => v,
Err(e) => return futures::future::err(e),
}; };
Err(err)
// TODO: move somewhere?
if out.source_code.starts_with(b"#!") {
out.source_code = filter_shebang(out.source_code);
} }
}
// 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. /// This is main method that is responsible for fetching local or remote files.

View file

@ -15,7 +15,6 @@ use crate::permissions::DenoPermissions;
use crate::progress::Progress; use crate::progress::Progress;
use deno::ErrBox; use deno::ErrBox;
use deno::ModuleSpecifier; use deno::ModuleSpecifier;
use futures::future::TryFutureExt;
use std; use std;
use std::env; use std::env;
use std::future::Future; use std::future::Future;
@ -119,17 +118,20 @@ impl ThreadSafeGlobalState {
} }
pub fn fetch_compiled_module( pub fn fetch_compiled_module(
self: &Self, &self,
module_specifier: &ModuleSpecifier, module_specifier: &ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>, maybe_referrer: Option<ModuleSpecifier>,
) -> impl Future<Output = Result<CompiledModule, ErrBox>> { ) -> impl Future<Output = Result<CompiledModule, ErrBox>> {
let state1 = self.clone(); let state1 = self.clone();
let state2 = self.clone(); let state2 = self.clone();
self let source_file = self
.file_fetcher .file_fetcher
.fetch_source_file_async(&module_specifier, maybe_referrer) .fetch_source_file_async(&module_specifier, maybe_referrer);
.and_then(move |out| match out.media_type {
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::Unknown => state1.js_compiler.compile_async(&out),
msg::MediaType::Json => state1.json_compiler.compile_async(&out), msg::MediaType::Json => state1.json_compiler.compile_async(&out),
msg::MediaType::Wasm => { msg::MediaType::Wasm => {
@ -147,28 +149,29 @@ impl ThreadSafeGlobalState {
state1.js_compiler.compile_async(&out) state1.js_compiler.compile_async(&out)
} }
} }
}) }
.and_then(move |compiled_module| { .await?;
if let Some(ref lockfile) = state2.lockfile { if let Some(ref lockfile) = state2.lockfile {
let mut g = lockfile.lock().unwrap(); let mut g = lockfile.lock().unwrap();
if state2.flags.lock_write { if state2.flags.lock_write {
g.insert(&compiled_module); g.insert(&compiled_module);
} else { } else {
let check = match g.check(&compiled_module) { let check = match g.check(&compiled_module) {
Err(e) => return futures::future::err(ErrBox::from(e)), Err(e) => return Err(ErrBox::from(e)),
Ok(v) => v, Ok(v) => v,
}; };
if !check { if !check {
eprintln!( eprintln!(
"Subresource integrety check failed --lock={}\n{}", "Subresource integrity check failed --lock={}\n{}",
g.filename, compiled_module.name g.filename, compiled_module.name
); );
std::process::exit(10); std::process::exit(10);
} }
} }
} }
futures::future::ok(compiled_module) Ok(compiled_module)
}) }
} }
#[inline] #[inline]

View file

@ -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 http://127.0.0.1:4545/cli/tests/003_relative_import.ts

View file

@ -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 http://localhost:4545/cli/tests/subdir/mt_text_ecmascript.j3.js