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: add JsrVersionResolver trait to allow more control over version resolution #554

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ pub async fn js_create_graph(
imports,
reporter: None,
executor: Default::default(),
jsr_version_resolver: None,
},
)
.await;
Expand Down
26 changes: 26 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::module_specifier::SpecifierError;
use crate::packages::resolve_version;
use crate::packages::JsrPackageInfo;
use crate::packages::JsrPackageVersionInfo;
use crate::packages::JsrVersionResolver;
use crate::packages::PackageSpecifiers;
use crate::rt::Executor;

Expand Down Expand Up @@ -1212,6 +1213,7 @@ pub struct BuildOptions<'a> {
pub npm_resolver: Option<&'a dyn NpmResolver>,
pub reporter: Option<&'a dyn Reporter>,
pub resolver: Option<&'a dyn Resolver>,
pub jsr_version_resolver: Option<&'a dyn JsrVersionResolver>,
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -3403,6 +3405,7 @@ struct Builder<'a, 'graph> {
fill_pass_mode: FillPassMode,
executor: &'a dyn Executor,
resolved_roots: BTreeSet<ModuleSpecifier>,
jsr_version_resolver: Option<&'a dyn JsrVersionResolver>,
}

impl<'a, 'graph> Builder<'a, 'graph> {
Expand Down Expand Up @@ -3433,6 +3436,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
fill_pass_mode,
executor: options.executor,
resolved_roots: Default::default(),
jsr_version_resolver: options.jsr_version_resolver,
};
builder.fill(roots, options.imports).await;
}
Expand Down Expand Up @@ -3641,6 +3645,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
executor: self.executor,
jsr_url_provider: self.jsr_url_provider,
loader: self.loader,
jsr_version_resolver: self.jsr_version_resolver,
},
);
pending_resolutions.push_front(pending_resolution);
Expand Down Expand Up @@ -3951,6 +3956,25 @@ impl<'a, 'graph> Builder<'a, 'graph> {
package_info: &JsrPackageInfo,
package_req: &PackageReq,
) -> Option<Version> {
if let Some(version_resolver) = self.jsr_version_resolver {
let version =
version_resolver.resolve_package_version(package_info, package_req);
if let Some(version) = version {
let is_yanked = package_info
.versions
.get(version)
.map(|i| i.yanked)
.unwrap_or(false);
let version = version.clone();
if is_yanked {
self.graph.packages.add_used_yanked_package(PackageNv {
name: package_req.name.clone(),
version: version.clone(),
});
}
return Some(version);
}
}
// 1. try to resolve with the list of existing versions
if let Some(existing_versions) =
self.graph.packages.versions_by_name(&package_req.name)
Expand Down Expand Up @@ -4715,6 +4739,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
executor: self.executor,
jsr_url_provider: self.jsr_url_provider,
loader: self.loader,
jsr_version_resolver: self.jsr_version_resolver,
},
);
}
Expand All @@ -4728,6 +4753,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
executor: self.executor,
jsr_url_provider: self.jsr_url_provider,
loader: self.loader,
jsr_version_resolver: self.jsr_version_resolver,
},
);
}
Expand Down
12 changes: 12 additions & 0 deletions src/jsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use futures::FutureExt;
use crate::graph::JsrLoadError;
use crate::packages::JsrPackageInfo;
use crate::packages::JsrPackageVersionInfo;
use crate::packages::JsrVersionResolver;
use crate::rt::spawn;
use crate::rt::JoinHandle;
use crate::source::CacheSetting;
Expand All @@ -37,6 +38,7 @@ pub struct JsrMetadataStoreServices<'a> {
pub loader: &'a dyn Loader,
pub executor: &'a dyn Executor,
pub jsr_url_provider: &'a dyn JsrUrlProvider,
pub jsr_version_resolver: Option<&'a dyn JsrVersionResolver>,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -82,6 +84,16 @@ impl JsrMetadataStore {
.url()
.join(&format!("{}/meta.json", package_name))
.unwrap();
let cache_setting =
if let Some(version_resolver) = services.jsr_version_resolver {
if version_resolver.force_fetch_metadata(package_name) {
CacheSetting::Reload
} else {
cache_setting
}
} else {
cache_setting
};
let fut = self.load_data(
specifier,
services,
Expand Down
10 changes: 10 additions & 0 deletions src/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ use serde::Serialize;
use crate::analyzer::module_graph_1_to_2;
use crate::ModuleInfo;

pub trait JsrVersionResolver {
fn resolve_package_version<'a>(
&self,
package_info: &'a JsrPackageInfo,
package_req: &PackageReq,
) -> Option<&'a Version>;

fn force_fetch_metadata<'a>(&self, package_name: &str) -> bool;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meh on this, doesn't really feel like it's the right place but adding another trait is annoying

}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct JsrPackageInfo {
pub versions: HashMap<Version, JsrPackageInfoVersion>,
Expand Down
1 change: 1 addition & 0 deletions tests/ecosystem_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ async fn test_version(
passthrough_jsr_specifiers: true,
executor: Default::default(),
imports: vec![],
jsr_version_resolver: None,
},
)
.await;
Expand Down
Loading