Skip to content

Commit

Permalink
feat: LoaderCacheSetting -> CacheSetting
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Aug 31, 2023
1 parent 0cb06c5 commit 2741457
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 84 deletions.
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::Prefer
}
}
}
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::Prefer)
.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::Prefer,
);
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
32 changes: 16 additions & 16 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 @@ -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::Prefer => "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::Prefer => {
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
24 changes: 12 additions & 12 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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::Prefer => {
// 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()
}
Expand Down Expand Up @@ -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::Prefer),
(
"https://registry-staging.deno.com/@scope/a/meta.json".to_string(),
LoaderCacheSetting::Prefer
CacheSetting::Prefer
),
("file:///main.ts".to_string(), LoaderCacheSetting::Prefer),
("file:///main.ts".to_string(), CacheSetting::Prefer),
(
"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::Prefer
),
]
);
Expand Down

0 comments on commit 2741457

Please sign in to comment.