Skip to content

Commit

Permalink
fix(npmrc): fix getting registry configuration for a scope (#55)
Browse files Browse the repository at this point in the history
Previous when trying to get the registry configuration for @scope/name we were
iterating over discovered registry configurations by starting at <registry_url>/<scope>/<name>,
trying to get a match, then stripping parts at the end.

This is incorrect, as the configuration should be the exact match on the defined
registry URL for given scope without appending scope and package names.
  • Loading branch information
bartlomieju authored May 30, 2024
1 parent c4b9c75 commit 568f315
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions src/npm_rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,8 @@ impl NpmRc {
let registry_url = original_registry_url
.split_once("//")
.map(|(_, right)| right)?;
let start_url = match maybe_scope_name {
Some(scope_name) => {
Cow::Owned(format!("{}{}/", registry_url, scope_name))
}
None => Cow::Borrowed(registry_url),
};
// Loop through all the paths in the url to find a match. Example:
// - example.com/myorg/pkg/
// - example.com/myorg/
// - example.com/
let mut url: &str = &start_url;
let mut url: &str = registry_url;

loop {
if let Some(config) = self.registry_configs.get(url) {
return Some((original_registry_url.into_owned(), config));
Expand Down Expand Up @@ -315,6 +306,7 @@ mod test {
@myorg:registry=https://example.com/myorg
@another:registry=https://example.com/another
@example:registry=https://example.com/example
@yet_another:registry=https://yet.another.com/
//registry.npmjs.org/:_authToken=MYTOKEN
; would apply to both @myorg and @another
//example.com/:_authToken=MYTOKEN0
Expand All @@ -328,6 +320,9 @@ mod test {
//example.com/myorg/:_authToken=MYTOKEN1
; would apply only to @another
//example.com/another/:_authToken=MYTOKEN2
; this should not apply to `@yet_another`, because the URL contains the name of the scope
; and not the URL of the registry root specified above
//yet.another.com/yet_another/:_authToken=MYTOKEN3
registry=https://registry.npmjs.org/
"#,
&|_| None,
Expand All @@ -347,6 +342,10 @@ registry=https://registry.npmjs.org/
"example".to_string(),
"https://example.com/example".to_string()
),
(
"yet_another".to_string(),
"https://yet.another.com/".to_string()
),
]),
registry_configs: HashMap::from([
(
Expand Down Expand Up @@ -375,6 +374,13 @@ registry=https://registry.npmjs.org/
..Default::default()
}
),
(
"yet.another.com/yet_another/".to_string(),
RegistryConfig {
auth_token: Some("MYTOKEN3".to_string()),
..Default::default()
}
),
(
"registry.npmjs.org/".to_string(),
RegistryConfig {
Expand Down Expand Up @@ -416,6 +422,19 @@ registry=https://registry.npmjs.org/
assert_eq!(registry_url, "https://example.com/myorg/");
assert_eq!(config.auth_token, Some("MYTOKEN1".to_string()));
}
// This should not return the token - the configuration is borked for `@yet_another` scope -
// it defines the registry url as root + scope_name and instead it should be matching the
// registry root.
{
let (registry_url, config) = npm_rc
.registry_url_and_config_for_package(
"@yet_another/pkg",
"https://deno.land/npm/",
)
.unwrap();
assert_eq!(registry_url, "https://yet.another.com/");
assert_eq!(config.auth_token, None);
}

let resolved_npm_rc = npm_rc
.as_resolved(&Url::parse("https://deno.land/npm/").unwrap())
Expand Down Expand Up @@ -466,6 +485,13 @@ registry=https://registry.npmjs.org/
}
}
),
(
"yet_another".to_string(),
RegistryConfigWithUrl {
registry_url: Url::parse("https://yet.another.com/").unwrap(),
config: Default::default()
}
),
])
}
);
Expand Down Expand Up @@ -636,8 +662,11 @@ registry=${VAR_FOUND}
fn test_scope_with_auth() {
let npm_rc = NpmRc::parse(
r#"
@example:registry=https://example.com/
//example.com/example/:_authToken=MY_AUTH_TOKEN
@example:registry=https://example.com/foo
@example2:registry=https://example2.com/
//example.com/foo/:_authToken=MY_AUTH_TOKEN
; This one is borked - the URL must match registry URL exactly
//example.com2/example/:_authToken=MY_AUTH_TOKEN2
"#,
&|_| None,
)
Expand All @@ -649,7 +678,7 @@ registry=${VAR_FOUND}
"https://deno.land/npm/",
)
.unwrap();
assert_eq!(registry_url, "https://example.com/");
assert_eq!(registry_url, "https://example.com/foo/");
assert_eq!(
config,
&RegistryConfig {
Expand All @@ -658,5 +687,15 @@ registry=${VAR_FOUND}
}
);
}
{
let (registry_url, config) = npm_rc
.registry_url_and_config_for_package(
"@example2/test",
"https://deno.land/npm/",
)
.unwrap();
assert_eq!(registry_url, "https://example2.com/");
assert_eq!(config, &Default::default());
}
}
}

0 comments on commit 568f315

Please sign in to comment.