mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
chore: cleanup EmbeddedModuleLoader (#19911)
Source code is now not cloned anymore between eszip and deno_core, as eszip now returns `Arc<[u8]>`, that can be trivially casted into `Arc<str>`, which `deno_core` can consume without copying.
This commit is contained in:
parent
cfc0c80642
commit
7b29f1c934
1 changed files with 55 additions and 41 deletions
|
@ -30,6 +30,7 @@ use crate::worker::HasNodeSpecifierChecker;
|
||||||
use crate::worker::ModuleLoaderFactory;
|
use crate::worker::ModuleLoaderFactory;
|
||||||
use deno_ast::MediaType;
|
use deno_ast::MediaType;
|
||||||
use deno_core::anyhow::Context;
|
use deno_core::anyhow::Context;
|
||||||
|
use deno_core::error::generic_error;
|
||||||
use deno_core::error::type_error;
|
use deno_core::error::type_error;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::futures::FutureExt;
|
use deno_core::futures::FutureExt;
|
||||||
|
@ -85,15 +86,19 @@ impl ModuleLoader for EmbeddedModuleLoader {
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
kind: ResolutionKind,
|
kind: ResolutionKind,
|
||||||
) -> Result<ModuleSpecifier, AnyError> {
|
) -> Result<ModuleSpecifier, AnyError> {
|
||||||
// Try to follow redirects when resolving.
|
let referrer = if referrer == "." {
|
||||||
let referrer = match self.shared.eszip.get_module(referrer) {
|
if kind != ResolutionKind::MainModule {
|
||||||
Some(eszip::Module { ref specifier, .. }) => {
|
return Err(generic_error(format!(
|
||||||
ModuleSpecifier::parse(specifier)?
|
"Expected to resolve main module, got {:?} instead.",
|
||||||
}
|
kind
|
||||||
None => {
|
)));
|
||||||
let cwd = std::env::current_dir().context("Unable to get CWD")?;
|
|
||||||
deno_core::resolve_url_or_path(referrer, &cwd)?
|
|
||||||
}
|
}
|
||||||
|
let current_dir = std::env::current_dir().unwrap();
|
||||||
|
deno_core::resolve_path(".", ¤t_dir)?
|
||||||
|
} else {
|
||||||
|
ModuleSpecifier::parse(referrer).map_err(|err| {
|
||||||
|
type_error(format!("Referrer uses invalid specifier: {}", err))
|
||||||
|
})?
|
||||||
};
|
};
|
||||||
|
|
||||||
let permissions = if matches!(kind, ResolutionKind::DynamicImport) {
|
let permissions = if matches!(kind, ResolutionKind::DynamicImport) {
|
||||||
|
@ -101,7 +106,6 @@ impl ModuleLoader for EmbeddedModuleLoader {
|
||||||
} else {
|
} else {
|
||||||
&self.root_permissions
|
&self.root_permissions
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(result) = self
|
if let Some(result) = self
|
||||||
.shared
|
.shared
|
||||||
.npm_module_loader
|
.npm_module_loader
|
||||||
|
@ -137,20 +141,29 @@ impl ModuleLoader for EmbeddedModuleLoader {
|
||||||
|
|
||||||
fn load(
|
fn load(
|
||||||
&self,
|
&self,
|
||||||
module_specifier: &ModuleSpecifier,
|
original_specifier: &ModuleSpecifier,
|
||||||
maybe_referrer: Option<&ModuleSpecifier>,
|
maybe_referrer: Option<&ModuleSpecifier>,
|
||||||
is_dynamic: bool,
|
is_dynamic: bool,
|
||||||
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
||||||
let is_data_uri = get_source_from_data_url(module_specifier).ok();
|
let is_data_uri = get_source_from_data_url(original_specifier).ok();
|
||||||
|
if let Some((source, _)) = is_data_uri {
|
||||||
|
return Box::pin(deno_core::futures::future::ready(Ok(
|
||||||
|
deno_core::ModuleSource::new(
|
||||||
|
deno_core::ModuleType::JavaScript,
|
||||||
|
source.into(),
|
||||||
|
original_specifier,
|
||||||
|
),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
let permissions = if is_dynamic {
|
let permissions = if is_dynamic {
|
||||||
&self.dynamic_permissions
|
&self.dynamic_permissions
|
||||||
} else {
|
} else {
|
||||||
&self.root_permissions
|
&self.root_permissions
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(result) =
|
if let Some(result) =
|
||||||
self.shared.npm_module_loader.load_sync_if_in_npm_package(
|
self.shared.npm_module_loader.load_sync_if_in_npm_package(
|
||||||
module_specifier,
|
original_specifier,
|
||||||
maybe_referrer,
|
maybe_referrer,
|
||||||
permissions,
|
permissions,
|
||||||
)
|
)
|
||||||
|
@ -163,7 +176,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
|
||||||
_ => ModuleType::JavaScript,
|
_ => ModuleType::JavaScript,
|
||||||
},
|
},
|
||||||
code_source.code,
|
code_source.code,
|
||||||
module_specifier,
|
original_specifier,
|
||||||
&code_source.found_url,
|
&code_source.found_url,
|
||||||
),
|
),
|
||||||
))),
|
))),
|
||||||
|
@ -171,33 +184,22 @@ impl ModuleLoader for EmbeddedModuleLoader {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let module = self
|
let Some(module) = self.shared.eszip.get_module(original_specifier.as_str()) else {
|
||||||
.shared
|
return Box::pin(deno_core::futures::future::ready(Err(type_error(
|
||||||
.eszip
|
format!("Module not found: {}", original_specifier),
|
||||||
.get_module(module_specifier.as_str())
|
))))
|
||||||
.ok_or_else(|| {
|
};
|
||||||
type_error(format!("Module not found: {}", module_specifier))
|
let original_specifier = original_specifier.clone();
|
||||||
});
|
let found_specifier =
|
||||||
// TODO(mmastrac): This clone can probably be removed in the future if ModuleSpecifier is no longer a full-fledged URL
|
ModuleSpecifier::parse(&module.specifier).expect("invalid url in eszip");
|
||||||
let module_specifier = module_specifier.clone();
|
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
if let Some((source, _)) = is_data_uri {
|
let code = module.source().await.ok_or_else(|| {
|
||||||
return Ok(deno_core::ModuleSource::new(
|
type_error(format!("Module not found: {}", original_specifier))
|
||||||
deno_core::ModuleType::JavaScript,
|
})?;
|
||||||
source.into(),
|
let code = arc_u8_to_arc_str(code)
|
||||||
&module_specifier,
|
.map_err(|_| type_error("Module source is not utf-8"))?;
|
||||||
));
|
Ok(deno_core::ModuleSource::new_with_redirect(
|
||||||
}
|
|
||||||
|
|
||||||
let module = module?;
|
|
||||||
let code = module.source().await.unwrap_or_else(|| Arc::new([]));
|
|
||||||
let code = std::str::from_utf8(&code)
|
|
||||||
.map_err(|_| type_error("Module source is not utf-8"))?
|
|
||||||
.to_owned()
|
|
||||||
.into();
|
|
||||||
|
|
||||||
Ok(deno_core::ModuleSource::new(
|
|
||||||
match module.kind {
|
match module.kind {
|
||||||
eszip::ModuleKind::JavaScript => ModuleType::JavaScript,
|
eszip::ModuleKind::JavaScript => ModuleType::JavaScript,
|
||||||
eszip::ModuleKind::Json => ModuleType::Json,
|
eszip::ModuleKind::Json => ModuleType::Json,
|
||||||
|
@ -208,14 +210,26 @@ impl ModuleLoader for EmbeddedModuleLoader {
|
||||||
unreachable!();
|
unreachable!();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
code,
|
code.into(),
|
||||||
&module_specifier,
|
&original_specifier,
|
||||||
|
&found_specifier,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
.boxed_local()
|
.boxed_local()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn arc_u8_to_arc_str(
|
||||||
|
arc_u8: Arc<[u8]>,
|
||||||
|
) -> Result<Arc<str>, std::str::Utf8Error> {
|
||||||
|
// Check that the string is valid UTF-8.
|
||||||
|
std::str::from_utf8(&arc_u8)?;
|
||||||
|
// SAFETY: the string is valid UTF-8, and the layout Arc<[u8]> is the same as
|
||||||
|
// Arc<str>. This is proven by the From<Arc<str>> impl for Arc<[u8]> from the
|
||||||
|
// standard library.
|
||||||
|
Ok(unsafe { std::mem::transmute(arc_u8) })
|
||||||
|
}
|
||||||
|
|
||||||
struct StandaloneModuleLoaderFactory {
|
struct StandaloneModuleLoaderFactory {
|
||||||
shared: Arc<SharedModuleLoaderState>,
|
shared: Arc<SharedModuleLoaderState>,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue