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

fix(cli/WasmerEnv): strip registry. only if hostname starts with it #4992

Merged
merged 3 commits into from
Aug 7, 2024
Merged
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/cli/src/commands/auth/login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl Login {
}
#[cfg(test)]
{
_ = user;
false
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions lib/cli/src/commands/package/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ impl PackageDownload {

#[cfg(test)]
mod tests {
use crate::config::UserRegistry;

use super::*;

/// Download a package from the dev registry.
Expand Down
46 changes: 40 additions & 6 deletions lib/cli/src/config/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ impl WasmerEnv {
let mut url = self.registry_endpoint()?;
url.set_path("");

let domain = url
.host_str()
.context("url has no host")?
.strip_prefix("registry.")
.context("could not derive registry public url")?
.to_string();
let mut domain = url.host_str().context("url has no host")?.to_string();
if domain.starts_with("registry.") {
domain = domain.strip_prefix("registry.").unwrap().to_string();
}

url.set_host(Some(&domain))
.context("could not derive registry public url")?;

Expand Down Expand Up @@ -209,6 +208,10 @@ mod tests {
[[registry.tokens]]
registry = "https://registry.wasmer.io/graphql"
token = "prod-token"

[[registry.tokens]]
registry = "http://localhost:11/graphql"
token = "invalid"
"#;

#[test]
Expand Down Expand Up @@ -310,4 +313,35 @@ mod tests {
assert_eq!(env.token().unwrap(), "prod-token");
assert_eq!(env.cache_dir(), expected_cache_dir);
}

#[test]
fn registries_have_public_url() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();

let inputs = [
("https://wasmer.io/", "https://registry.wasmer.io/graphql"),
("https://wasmer.wtf/", "https://registry.wasmer.wtf/graphql"),
("https://wasmer.wtf/", "https://registry.wasmer.wtf/graphql"),
(
"https://wasmer.wtf/",
"https://registry.wasmer.wtf/something/else",
),
("https://wasmer.wtf/", "https://wasmer.wtf/graphql"),
("https://wasmer.wtf/", "https://wasmer.wtf/graphql"),
("http://localhost:8000/", "http://localhost:8000/graphql"),
("http://localhost:8000/", "http://localhost:8000/graphql"),
];

for (want, input) in inputs {
let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: Some(UserRegistry::from(input)),
cache_dir: temp.path().join("cache").to_path_buf(),
token: None,
};

assert_eq!(want, &env.registry_public_url().unwrap().to_string())
}
}
}
10 changes: 4 additions & 6 deletions lib/registry/src/wasmer_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ impl WasmerEnv {
let mut url = self.registry_endpoint()?;
url.set_path("");

let domain = url
.host_str()
.context("url has no host")?
.strip_prefix("registry.")
.context("could not derive registry public url")?
.to_string();
let mut domain = url.host_str().context("url has no host")?.to_string();
if domain.starts_with("registry.") {
domain = domain.strip_prefix("registry.").unwrap().to_string();
}
url.set_host(Some(&domain))
.context("could not derive registry public url")?;

Expand Down
Loading