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(
self: &Self,
&self,
specifier: &ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>,
) -> Pin<Box<SourceFileFuture>> {
@ -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.

View file

@ -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<ModuleSpecifier>,
) -> impl Future<Output = Result<CompiledModule, ErrBox>> {
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]

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

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