Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LoaderCacheSetting -> CacheSetting #291

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use std::collections::HashMap;
use deno_graph::resolve_import;
use deno_graph::source::load_data_url;
use deno_graph::source::CacheInfo;
use deno_graph::source::CacheSetting;
use deno_graph::source::LoadFuture;
use deno_graph::source::Loader;
use deno_graph::source::LoaderCacheSetting;
use deno_graph::source::Resolver;
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
use deno_graph::BuildOptions;
Expand Down Expand Up @@ -60,11 +60,11 @@ impl Loader for JsLoader {
}
}

fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
cache_setting: LoaderCacheSetting,
cache_setting: CacheSetting,
) -> LoadFuture {
if specifier.scheme() == "data" {
Box::pin(future::ready(load_data_url(specifier)))
Expand All @@ -73,12 +73,7 @@ impl Loader for JsLoader {
let context = JsValue::null();
let arg1 = JsValue::from(specifier.to_string());
let arg2 = JsValue::from(is_dynamic);
let arg3 = JsValue::from(match cache_setting {
// note: keep these values aligned with deno_cache
LoaderCacheSetting::Only => "only",
LoaderCacheSetting::Prefer => "prefer",
LoaderCacheSetting::Reload => "reload",
});
let arg3 = JsValue::from(cache_setting.as_js_str());
let result = self.load.call3(&context, &arg1, &arg2, &arg3);
let f = async move {
let response = match result {
Expand Down
63 changes: 33 additions & 30 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2139,11 +2139,11 @@ enum FillPassMode {
}

impl FillPassMode {
fn to_cache_setting(self) -> LoaderCacheSetting {
fn to_cache_setting(self) -> CacheSetting {
if self == FillPassMode::CacheBusting {
LoaderCacheSetting::Reload
CacheSetting::Reload
} else {
LoaderCacheSetting::Prefer
CacheSetting::Use
}
}
}
Expand Down Expand Up @@ -2728,10 +2728,10 @@ impl<'a, 'graph> Builder<'a, 'graph> {
// Check if this specifier is in the cache. If it is, then
// don't use the module information as it may be out of date
// with what's in the cache
let fut = self.loader.load_with_cache_setting(
let fut = self.loader.load(
specifier,
self.in_dynamic_branch,
LoaderCacheSetting::Only,
CacheSetting::Only,
);
self.state.pending.push_back({
let specifier = specifier.clone();
Expand Down Expand Up @@ -2947,16 +2947,15 @@ impl<'a, 'graph> Builder<'a, 'graph> {
.insert(requested_specifier.clone(), ModuleSlot::Pending);
let load_specifier = load_specifier.clone();
let requested_specifier = requested_specifier.clone();
let fut =
self
.loader
.load(&load_specifier, is_dynamic)
.map(move |result| PendingInfo {
specifier: requested_specifier,
maybe_range,
result: result.map(|r| r.map(Into::into)),
maybe_version_info: None,
});
let fut = self
.loader
.load(&load_specifier, is_dynamic, CacheSetting::Use)
.map(move |result| PendingInfo {
specifier: requested_specifier,
maybe_range,
result: result.map(|r| r.map(Into::into)),
maybe_version_info: None,
});
self.state.pending.push_back(Box::pin(fut));
}

Expand All @@ -2976,7 +2975,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
.registry_url()
.join(&format!("{}/meta.json", package_name))
.unwrap();
let fut = self.loader.load_with_cache_setting(
let fut = self.loader.load(
&specifier,
false,
self.fill_pass_mode.to_cache_setting(),
Expand Down Expand Up @@ -3018,7 +3017,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
package_nv.name, package_nv.version
))
.unwrap();
let fut = self.loader.load_with_cache_setting(
let fut = self.loader.load(
&specifier,
false,
self.fill_pass_mode.to_cache_setting(),
Expand Down Expand Up @@ -3126,7 +3125,11 @@ impl<'a, 'graph> Builder<'a, 'graph> {
let specifier = specifier.clone();
let maybe_range = maybe_referrer.clone();
let module_info = info.clone();
let fut = self.loader.load(&specifier, self.in_dynamic_branch);
let fut = self.loader.load(
&specifier,
self.in_dynamic_branch,
CacheSetting::Use,
);
async move {
let result = fut.await;
PendingContentLoadItem {
Expand Down Expand Up @@ -3689,11 +3692,11 @@ mod tests {
loaded_baz: bool,
}
impl Loader for TestLoader {
fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
_cache_setting: LoaderCacheSetting,
_cache_setting: CacheSetting,
) -> LoadFuture {
let specifier = specifier.clone();
match specifier.as_str() {
Expand Down Expand Up @@ -3758,11 +3761,11 @@ mod tests {
async fn missing_module_is_error() {
struct TestLoader;
impl Loader for TestLoader {
fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
_is_dynamic: bool,
_cache_setting: LoaderCacheSetting,
_cache_setting: CacheSetting,
) -> LoadFuture {
let specifier = specifier.clone();
match specifier.as_str() {
Expand Down Expand Up @@ -3845,11 +3848,11 @@ mod tests {
async fn redirected_specifiers() {
struct TestLoader;
impl Loader for TestLoader {
fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
_is_dynamic: bool,
_cache_setting: LoaderCacheSetting,
_cache_setting: CacheSetting,
) -> LoadFuture {
let specifier = specifier.clone();
match specifier.as_str() {
Expand Down Expand Up @@ -3913,11 +3916,11 @@ mod tests {
async fn local_import_remote_module() {
struct TestLoader;
impl Loader for TestLoader {
fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
_is_dynamic: bool,
_cache_setting: LoaderCacheSetting,
_cache_setting: CacheSetting,
) -> LoadFuture {
let specifier = specifier.clone();
match specifier.as_str() {
Expand Down Expand Up @@ -4038,11 +4041,11 @@ mod tests {
loaded_bar: bool,
}
impl Loader for TestLoader {
fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
_cache_setting: LoaderCacheSetting,
_cache_setting: CacheSetting,
) -> LoadFuture {
let specifier = specifier.clone();
match specifier.as_str() {
Expand Down Expand Up @@ -4085,11 +4088,11 @@ mod tests {
async fn dependency_imports() {
struct TestLoader;
impl Loader for TestLoader {
fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
_cache_setting: LoaderCacheSetting,
_cache_setting: CacheSetting,
) -> LoadFuture {
let specifier = specifier.clone();
match specifier.as_str() {
Expand Down
34 changes: 17 additions & 17 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub type LoadResult = Result<Option<LoadResponse>>;
pub type LoadFuture = LocalBoxFuture<'static, LoadResult>;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LoaderCacheSetting {
pub enum CacheSetting {
/// Attempts to load a specifier from the cache.
///
/// This is used to see whether the specifier is in the cache for `deno:` specifiers.
Expand All @@ -81,14 +81,26 @@ pub enum LoaderCacheSetting {
/// issue a separate request to the `load` method in order to get the source.
Only,
/// The implementation should prefer using the cache.
Prefer,
Use,
/// Loads a specifier where the implementation should not load
/// from an internal cache. This is only ever done when loading
/// `deno:` specifier module information and the version constraint
/// cannot be resolved.
Reload,
}

impl CacheSetting {
/// String representation that can be sent to JS for consumption in deno_cache.
pub fn as_js_str(&self) -> &'static str {
// note: keep these values aligned with deno_cache
match self {
CacheSetting::Only => "only",
CacheSetting::Use => "use",
CacheSetting::Reload => "reload",
}
}
}

pub static DEFAULT_DENO_REGISTRY_URL: Lazy<Url> =
Lazy::new(|| Url::parse("https://registry-staging.deno.com").unwrap());

Expand All @@ -111,19 +123,7 @@ pub trait Loader {
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
) -> LoadFuture {
self.load_with_cache_setting(
specifier,
is_dynamic,
LoaderCacheSetting::Prefer,
)
}

fn load_with_cache_setting(
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
cache_setting: LoaderCacheSetting,
cache_setting: CacheSetting,
) -> LoadFuture;

/// Cache the module info for the provided specifier if the loader
Expand Down Expand Up @@ -369,11 +369,11 @@ impl Loader for MemoryLoader {
self.cache_info.get(specifier).cloned()
}

fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
_is_dynamic: bool,
_cache_setting: LoaderCacheSetting,
_cache_setting: CacheSetting,
) -> LoadFuture {
let response = match self.sources.get(specifier) {
Some(Ok(response)) => Ok(Some(response.clone())),
Expand Down
28 changes: 11 additions & 17 deletions tests/helpers/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use deno_ast::ModuleSpecifier;
use deno_graph::source::CacheInfo;
use deno_graph::source::CacheSetting;
use deno_graph::source::LoadFuture;
use deno_graph::source::Loader;
use deno_graph::source::LoaderCacheSetting;
use deno_graph::source::MemoryLoader;
use deno_graph::BuildDiagnostic;
use deno_graph::GraphKind;
Expand All @@ -22,29 +22,23 @@ impl Loader for TestLoader {
self.cache.get_cache_info(specifier)
}

fn load_with_cache_setting(
fn load(
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
cache_setting: LoaderCacheSetting,
cache_setting: CacheSetting,
) -> LoadFuture {
match cache_setting {
// todo(dsherret): in the future, actually make this use the cache
LoaderCacheSetting::Prefer => self.remote.load_with_cache_setting(
specifier,
is_dynamic,
cache_setting,
),
CacheSetting::Use => {
self.remote.load(specifier, is_dynamic, cache_setting)
}
// todo(dsherret): in the future, make this update the cache
LoaderCacheSetting::Reload => self.remote.load_with_cache_setting(
specifier,
is_dynamic,
cache_setting,
),
LoaderCacheSetting::Only => {
self
.cache
.load_with_cache_setting(specifier, is_dynamic, cache_setting)
CacheSetting::Reload => {
self.remote.load(specifier, is_dynamic, cache_setting)
}
CacheSetting::Only => {
self.cache.load(specifier, is_dynamic, cache_setting)
}
}
}
Expand Down
Loading
Loading