Skip to content

Commit

Permalink
refactor: upgrade to deno_semver 0.5 (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Sep 13, 2023
1 parent 4ce8603 commit 2a829e6
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 181 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ anyhow = "1.0.43"
async-trait = "0.1.68"
data-url = "0.3.0"
deno_ast = { version = "0.29.0", features = ["dep_graph", "module_specifier"] }
deno_semver = "0.4.0"
deno_semver = "0.5.0"
futures = "0.3.26"
indexmap = { version = "2", features = ["serde"] }
monch = "0.4.3"
Expand Down
94 changes: 46 additions & 48 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use crate::DefaultModuleAnalyzer;
use crate::ImportAttributes;
use crate::ReferrerImports;

use crate::deno::resolve_version;
use crate::deno::DenoPackageInfo;
use crate::deno::DenoPackageVersionInfo;
use crate::deno::DenoSpecifierSnapshot;
use crate::module_specifier::resolve_import;
use crate::module_specifier::ModuleSpecifier;
use crate::module_specifier::SpecifierError;
use crate::packages::resolve_version;
use crate::packages::JsrPackageInfo;
use crate::packages::JsrPackageVersionInfo;
use crate::packages::PackageSpecifiers;
use crate::source::*;

use anyhow::anyhow;
Expand All @@ -28,7 +28,7 @@ use deno_ast::LineAndColumnIndex;
use deno_ast::MediaType;
use deno_ast::SourcePos;
use deno_ast::SourceTextInfo;
use deno_semver::deno::DenoPackageReqReference;
use deno_semver::jsr::JsrPackageReqReference;
use deno_semver::npm::NpmPackageNvReference;
use deno_semver::npm::NpmPackageReqReference;
use deno_semver::package::PackageNv;
Expand Down Expand Up @@ -1237,9 +1237,9 @@ pub struct ModuleGraph {
pub npm_packages: Vec<PackageNv>,
#[serde(skip_serializing)]
pub has_node_specifier: bool,
#[serde(rename = "deno")]
#[serde(skip_serializing_if = "DenoSpecifierSnapshot::is_empty")]
pub deno_specifiers: DenoSpecifierSnapshot,
#[serde(rename = "packages")]
#[serde(skip_serializing_if = "PackageSpecifiers::is_empty")]
pub packages: PackageSpecifiers,
}

impl ModuleGraph {
Expand All @@ -1252,7 +1252,7 @@ impl ModuleGraph {
redirects: Default::default(),
npm_packages: Default::default(),
has_node_specifier: false,
deno_specifiers: Default::default(),
packages: Default::default(),
}
}

Expand Down Expand Up @@ -2053,7 +2053,7 @@ impl From<LoadResponse> for PendingInfoResponse {
#[derive(Clone)]
struct DenoPackageVersionInfoExt {
base_url: Url,
inner: Arc<DenoPackageVersionInfo>,
inner: Arc<JsrPackageVersionInfo>,
}

struct PendingInfo {
Expand Down Expand Up @@ -2087,9 +2087,9 @@ struct PendingNpmState {
pending_resolutions: Vec<PendingNpmResolutionItem>,
}

struct PendingDenoResolutionItem {
struct PendingJsrResolutionItem {
specifier: ModuleSpecifier,
package_ref: DenoPackageReqReference,
package_ref: JsrPackageReqReference,
maybe_range: Option<Range>,
}

Expand All @@ -2104,20 +2104,20 @@ type PendingResult<T> =
Shared<LocalBoxFuture<'static, Result<T, Arc<anyhow::Error>>>>;

#[derive(Default)]
struct PendingDenoState {
struct PendingJsrState {
pending_package_info_loads:
HashMap<String, PendingResult<Option<Arc<DenoPackageInfo>>>>,
HashMap<String, PendingResult<Option<Arc<JsrPackageInfo>>>>,
pending_package_version_info_loads:
HashMap<PackageNv, PendingResult<Arc<DenoPackageVersionInfo>>>,
pending_resolutions: Vec<PendingDenoResolutionItem>,
HashMap<PackageNv, PendingResult<Arc<JsrPackageVersionInfo>>>,
pending_resolutions: Vec<PendingJsrResolutionItem>,
pending_content_loads:
FuturesUnordered<LocalBoxFuture<'static, PendingContentLoadItem>>,
}

#[derive(Default)]
struct PendingState {
pending: FuturesOrdered<PendingInfoFuture>,
deno: PendingDenoState,
jsr: PendingJsrState,
npm: PendingNpmState,
pending_specifiers:
HashMap<ModuleSpecifier, HashSet<Option<AttributeTypeWithRange>>>,
Expand Down Expand Up @@ -2167,7 +2167,7 @@ impl std::fmt::Display for BuildDiagnostic {
#[derive(Debug, Clone)]
pub enum BuildDiagnosticKind {
ConstraintNotMatchedWorkspaceVersion {
reference: DenoPackageReqReference,
reference: JsrPackageReqReference,
workspace_version: Version,
},
}
Expand Down Expand Up @@ -2321,7 +2321,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
}

if self.state.pending.is_empty() {
let should_restart = self.resolve_pending_deno_specifiers().await;
let should_restart = self.resolve_pending_jsr_specifiers().await;
if should_restart {
self.restart(provided_roots, provided_imports).await;
return;
Expand All @@ -2338,7 +2338,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
}

// handle any pending content loads from the Deno registry
self.handle_deno_registry_pending_content_loads().await;
self.handle_jsr_registry_pending_content_loads().await;

// enrich with cache info from the loader
self.fill_graph_with_cache_info();
Expand Down Expand Up @@ -2367,17 +2367,17 @@ impl<'a, 'graph> Builder<'a, 'graph> {
}
}

async fn resolve_pending_deno_specifiers(&mut self) -> bool {
async fn resolve_pending_jsr_specifiers(&mut self) -> bool {
// first load the package information
let pending_resolutions =
std::mem::take(&mut self.state.deno.pending_resolutions);
std::mem::take(&mut self.state.jsr.pending_resolutions);
let mut pending_version_resolutions =
Vec::with_capacity(pending_resolutions.len());
for pending_resolution in pending_resolutions {
let package_name = &pending_resolution.package_ref.req().name;
let fut = self
.state
.deno
.jsr
.pending_package_info_loads
.get(package_name)
.unwrap()
Expand All @@ -2386,7 +2386,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
Ok(Some(info)) => {
// resolve the best version out of the existing versions first
let package_req = pending_resolution.package_ref.req();
match self.resolve_deno_version(&info, package_req) {
match self.resolve_jsr_version(&info, package_req) {
Some(version) => {
// now queue a pending load for that version information
let package_nv = PackageNv {
Expand All @@ -2395,7 +2395,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
};
self
.graph
.deno_specifiers
.packages
.add(package_req.clone(), package_nv.clone());

self.queue_load_package_version_info(&package_nv);
Expand Down Expand Up @@ -2451,7 +2451,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
for (nv, resolution_item) in pending_version_resolutions {
let version_info_result = self
.state
.deno
.jsr
.pending_package_version_info_loads
.get_mut(&nv)
.unwrap()
Expand Down Expand Up @@ -2526,8 +2526,8 @@ impl<'a, 'graph> Builder<'a, 'graph> {
}
}

async fn handle_deno_registry_pending_content_loads(&mut self) {
while let Some(item) = self.state.deno.pending_content_loads.next().await {
async fn handle_jsr_registry_pending_content_loads(&mut self) {
while let Some(item) = self.state.jsr.pending_content_loads.next().await {
match item.result {
Ok(Some(response)) => {
match response {
Expand All @@ -2540,7 +2540,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
ModuleError::LoadingErr(
item.specifier,
item.maybe_range,
Arc::new(anyhow!("Loader should never return an external specifier for a deno: specifier.")),
Arc::new(anyhow!("Loader should never return an external specifier for a jsr: specifier.")),
),
)),
);
Expand Down Expand Up @@ -2655,16 +2655,14 @@ impl<'a, 'graph> Builder<'a, 'graph> {
async move { self.fill(roots, imports).await }.boxed_local()
}

fn resolve_deno_version(
fn resolve_jsr_version(
&mut self,
package_info: &DenoPackageInfo,
package_info: &JsrPackageInfo,
package_req: &PackageReq,
) -> Option<Version> {
// try to find in the list of existing versions first
if let Some(existing_versions) = self
.graph
.deno_specifiers
.versions_by_name(&package_req.name)
if let Some(existing_versions) =
self.graph.packages.versions_by_name(&package_req.name)
{
if let Some(version) = resolve_version(
&package_req.version_req,
Expand Down Expand Up @@ -2772,8 +2770,8 @@ impl<'a, 'graph> Builder<'a, 'graph> {
}

let maybe_range = maybe_range.map(ToOwned::to_owned);
if specifier.scheme() == "deno" {
self.load_deno_specifier(specifier.clone(), maybe_range, is_dynamic);
if specifier.scheme() == "jsr" {
self.load_jsr_specifier(specifier.clone(), maybe_range, is_dynamic);
return;
} else if let Some(npm_resolver) = self.npm_resolver {
if specifier.scheme() == "npm" {
Expand Down Expand Up @@ -2816,13 +2814,13 @@ impl<'a, 'graph> Builder<'a, 'graph> {
self.load_pending_module(&specifier, maybe_range, &specifier, is_dynamic);
}

fn load_deno_specifier(
fn load_jsr_specifier(
&mut self,
specifier: Url,
maybe_range: Option<Range>,
is_dynamic: bool,
) {
match DenoPackageReqReference::from_specifier(&specifier) {
match JsrPackageReqReference::from_specifier(&specifier) {
Ok(package_ref) => {
for workspace_member in &self.workspace_members {
if workspace_member.nv.name == package_ref.req().name {
Expand Down Expand Up @@ -2860,9 +2858,9 @@ impl<'a, 'graph> Builder<'a, 'graph> {
self.queue_load_package_info(package_name);
self
.state
.deno
.jsr
.pending_resolutions
.push(PendingDenoResolutionItem {
.push(PendingJsrResolutionItem {
specifier,
package_ref,
maybe_range,
Expand Down Expand Up @@ -2962,7 +2960,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
fn queue_load_package_info(&mut self, package_name: &str) {
if self
.state
.deno
.jsr
.pending_package_info_loads
.contains_key(package_name)
{
Expand All @@ -2984,7 +2982,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
let data = fut.await.map_err(Arc::new)?;
match data {
Some(LoadResponse::Module { content, .. }) => {
let package_info: DenoPackageInfo =
let package_info: JsrPackageInfo =
serde_json::from_str(&content).map_err(|e| Arc::new(e.into()))?;
Ok(Some(Arc::new(package_info)))
}
Expand All @@ -2994,15 +2992,15 @@ impl<'a, 'graph> Builder<'a, 'graph> {
.boxed_local();
self
.state
.deno
.jsr
.pending_package_info_loads
.insert(package_name.to_string(), fut.shared());
}

fn queue_load_package_version_info(&mut self, package_nv: &PackageNv) {
if self
.state
.deno
.jsr
.pending_package_version_info_loads
.contains_key(package_nv)
{
Expand All @@ -3026,7 +3024,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
let data = fut.await.map_err(Arc::new)?;
match data {
Some(LoadResponse::Module { content, .. }) => {
let version_info: DenoPackageVersionInfo =
let version_info: JsrPackageVersionInfo =
serde_json::from_str(&content).map_err(|e| Arc::new(e.into()))?;
Ok(Arc::new(version_info))
}
Expand All @@ -3036,7 +3034,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
.boxed_local();
self
.state
.deno
.jsr
.pending_package_version_info_loads
.insert(package_nv.clone(), fut.shared());
}
Expand Down Expand Up @@ -3121,7 +3119,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
let (content, maybe_module_analyzer) = match content_or_module_info {
ContentOrModuleInfo::Content(content) => (content, None),
ContentOrModuleInfo::ModuleInfo(info) => {
self.state.deno.pending_content_loads.push({
self.state.jsr.pending_content_loads.push({
let specifier = specifier.clone();
let maybe_range = maybe_referrer.clone();
let module_info = info.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod module_specifier;
#[cfg(feature = "type_tracing")]
pub mod type_tracer;

pub mod deno;
pub mod packages;
pub mod source;
mod text_encoding;

Expand Down
14 changes: 7 additions & 7 deletions src/deno.rs → src/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ use serde::Serialize;
use crate::ModuleInfo;

#[derive(Serialize, Deserialize, Clone)]
pub struct DenoPackageInfo {
pub versions: HashMap<Version, DenoPackageInfoVersion>,
pub struct JsrPackageInfo {
pub versions: HashMap<Version, JsrPackageInfoVersion>,
}

#[derive(Serialize, Deserialize, Clone, Default)]
pub struct DenoPackageInfoVersion {
pub struct JsrPackageInfoVersion {
// currently not supported because it doesn't work in workspaces
// pub main: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Default)]
pub struct DenoPackageVersionInfo {
pub struct JsrPackageVersionInfo {
#[serde(rename = "moduleGraph1")]
pub module_graph: Option<serde_json::Value>,
}

impl DenoPackageVersionInfo {
impl JsrPackageVersionInfo {
pub fn module_info(&self, specifier: &str) -> Option<ModuleInfo> {
let module_graph = self.module_graph.as_ref()?.as_object()?;
let module_info = module_graph.get(specifier)?;
Expand All @@ -38,14 +38,14 @@ impl DenoPackageVersionInfo {
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct DenoSpecifierSnapshot {
pub struct PackageSpecifiers {
#[serde(flatten)]
package_reqs: BTreeMap<PackageReq, PackageNv>,
#[serde(skip_serializing)]
packages_by_name: HashMap<String, Vec<PackageNv>>,
}

impl DenoSpecifierSnapshot {
impl PackageSpecifiers {
pub fn is_empty(&self) -> bool {
self.package_reqs.is_empty()
}
Expand Down
Loading

0 comments on commit 2a829e6

Please sign in to comment.