Skip to content

Commit

Permalink
Make loader files async
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed May 3, 2024
1 parent 86382d1 commit fa4077a
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/inner_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Default for InnerRuntimeOptions {
extensions: Default::default(),
default_entrypoint: Default::default(),
timeout: Duration::MAX,
module_cache: Some(Box::new(DefaultModuleCacheProvider::default())),
module_cache: Some(Box::<DefaultModuleCacheProvider>::default()),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
//! This crate is meant to provide a quick and simple way to integrate a runtime javacript or typescript component from within rust.
//!
//! - **By default, the code being run is entirely sandboxed from the host, having no filesystem or network access.**
Expand Down
10 changes: 5 additions & 5 deletions src/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ModuleCacheProvider for DefaultModuleCacheProvider {
fn get(&self, specifier: &ModuleSpecifier) -> Option<ModuleSource> {
let cache = self.0.borrow();
let source = cache.get(specifier)?;
Some(Self::clone_source(&self, specifier, source))
Some(Self::clone_source(self, specifier, source))
}
}

Expand All @@ -62,7 +62,7 @@ impl ModuleLoader for RustyLoader {
referrer: &str,
_kind: deno_core::ResolutionKind,
) -> Result<ModuleSpecifier, anyhow::Error> {
let url = deno_core::resolve_import(specifier, &referrer)?;
let url = deno_core::resolve_import(specifier, referrer)?;
if referrer == "." {
self.whitelist_add(url.as_str());
}
Expand Down Expand Up @@ -165,7 +165,7 @@ impl RustyLoader {
) -> Result<ModuleSource, deno_core::error::AnyError> {
let cache_provider = cache_provider.as_ref().as_ref().map(|p| p.as_ref());
match cache_provider.map(|p| p.get(&module_specifier)) {
Some(Some(source)) => return Ok(source),
Some(Some(source)) => Ok(source),
_ => {
let module_type = if module_specifier.path().ends_with(".json") {
ModuleType::Json
Expand Down Expand Up @@ -193,7 +193,7 @@ impl RustyLoader {
) -> Result<ModuleSource, deno_core::error::AnyError> {
let cache_provider = cache_provider.as_ref().as_ref().map(|p| p.as_ref());
match cache_provider.map(|p| p.get(&module_specifier)) {
Some(Some(source)) => return Ok(source),
Some(Some(source)) => Ok(source),
_ => {
let module_type = if module_specifier.path().ends_with(".json") {
ModuleType::Json
Expand All @@ -204,7 +204,7 @@ impl RustyLoader {
let path = module_specifier.to_file_path().map_err(|_| {
anyhow!("Provided module specifier \"{module_specifier}\" is not a file URL.")
})?;
let code = std::fs::read_to_string(path)?;
let code = tokio::fs::read_to_string(path).await?;
let code = transpiler::transpile(&module_specifier, &code)?;

Ok(ModuleSource::new(
Expand Down
2 changes: 1 addition & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait ToModuleSpecifier {

impl ToModuleSpecifier for str {
fn to_module_specifier(&self) -> Result<ModuleSpecifier, Error> {
resolve_path(self, &current_dir()?).map_err(|e| Error::from(e))
resolve_path(self, &current_dir()?).map_err(Error::from)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/transpiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn should_transpile(media_type: &MediaType) -> bool {
| MediaType::Dcts
| MediaType::Tsx => true,

_ => return false,
_ => false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn validate(javascript: &str) -> Result<bool, Error> {
let mut runtime = Runtime::new(Default::default())?;
match runtime.load_modules(&module, vec![]) {
Ok(_) => Ok(true),
Err(e) if matches!(e, Error::Runtime(_)) => Ok(false),
Err(Error::Runtime(_)) => Ok(false),
Err(e) => Err(e),
}
}
Expand Down

0 comments on commit fa4077a

Please sign in to comment.