diff --git a/lib/lib.rs b/lib/lib.rs index db96ce5d7..344b59555 100644 --- a/lib/lib.rs +++ b/lib/lib.rs @@ -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; @@ -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))) @@ -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 { diff --git a/src/graph.rs b/src/graph.rs index 47c9dbe76..cedd5b064 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -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 } } } @@ -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(); @@ -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)); } @@ -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(), @@ -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(), @@ -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 { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { diff --git a/src/source.rs b/src/source.rs index cf34dae01..a497cf2a0 100644 --- a/src/source.rs +++ b/src/source.rs @@ -72,7 +72,7 @@ pub type LoadResult = Result>; 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. @@ -81,7 +81,7 @@ 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 @@ -89,6 +89,18 @@ pub enum LoaderCacheSetting { 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 = Lazy::new(|| Url::parse("https://registry-staging.deno.com").unwrap()); @@ -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 @@ -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())), diff --git a/tests/helpers/test_builder.rs b/tests/helpers/test_builder.rs index c13290064..14055eb18 100644 --- a/tests/helpers/test_builder.rs +++ b/tests/helpers/test_builder.rs @@ -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; @@ -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) } } } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 8f59483fe..e88628046 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -10,9 +10,9 @@ use std::rc::Rc; use anyhow::anyhow; use deno_ast::ModuleSpecifier; +use deno_graph::source::CacheSetting; use deno_graph::source::LoadFuture; use deno_graph::source::LoadResponse; -use deno_graph::source::LoaderCacheSetting; use deno_graph::source::MemoryLoader; use deno_graph::source::NpmResolver; use deno_graph::source::UnknownBuiltInNodeModuleError; @@ -236,15 +236,15 @@ async fn test_npm_version_not_found_then_found() { async fn test_deno_version_not_found_then_found() { #[derive(Default)] struct TestLoader { - requests: Vec<(String, LoaderCacheSetting)>, + requests: Vec<(String, CacheSetting)>, } impl deno_graph::source::Loader for TestLoader { - fn load_with_cache_setting( + fn load( &mut self, specifier: &ModuleSpecifier, is_dynamic: bool, - cache_setting: LoaderCacheSetting, + cache_setting: CacheSetting, ) -> LoadFuture { assert!(!is_dynamic); self.requests.push((specifier.to_string(), cache_setting)); @@ -263,11 +263,11 @@ async fn test_deno_version_not_found_then_found() { specifier: specifier.clone(), maybe_headers: None, content: match cache_setting { - LoaderCacheSetting::Only | LoaderCacheSetting::Prefer => { + CacheSetting::Only | CacheSetting::Use => { // first time it won't have the version r#"{ "versions": { "1.0.0": {} } }"#.into() } - LoaderCacheSetting::Reload => { + CacheSetting::Reload => { // then on reload it will r#"{ "versions": { "1.0.0": {}, "1.2.0": {} } }"#.into() } @@ -311,24 +311,24 @@ async fn test_deno_version_not_found_then_found() { assert_eq!( loader.requests, vec![ - ("file:///main.ts".to_string(), LoaderCacheSetting::Prefer), + ("file:///main.ts".to_string(), CacheSetting::Use), ( "https://registry-staging.deno.com/@scope/a/meta.json".to_string(), - LoaderCacheSetting::Prefer + CacheSetting::Use ), - ("file:///main.ts".to_string(), LoaderCacheSetting::Prefer), + ("file:///main.ts".to_string(), CacheSetting::Use), ( "https://registry-staging.deno.com/@scope/a/meta.json".to_string(), - LoaderCacheSetting::Reload + CacheSetting::Reload ), ( "https://registry-staging.deno.com/@scope/a/1.2.0_meta.json" .to_string(), - LoaderCacheSetting::Reload + CacheSetting::Reload ), ( "https://registry-staging.deno.com/@scope/a/1.2.0/mod.ts".to_string(), - LoaderCacheSetting::Prefer + CacheSetting::Use ), ] );