summaryrefslogtreecommitdiff
path: root/cli/tools/installer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tools/installer.rs')
-rw-r--r--cli/tools/installer.rs140
1 files changed, 114 insertions, 26 deletions
diff --git a/cli/tools/installer.rs b/cli/tools/installer.rs
index f810e9ca0..34ecc66be 100644
--- a/cli/tools/installer.rs
+++ b/cli/tools/installer.rs
@@ -12,7 +12,7 @@ use crate::args::TypeCheckMode;
use crate::args::UninstallFlags;
use crate::args::UninstallKind;
use crate::factory::CliFactory;
-use crate::http_util::HttpClient;
+use crate::http_util::HttpClientProvider;
use crate::util::fs::canonicalize_path_maybe_not_exists;
use deno_config::ConfigFlag;
@@ -133,15 +133,21 @@ fn get_installer_root() -> Result<PathBuf, io::Error> {
Ok(home_path)
}
-pub async fn infer_name_from_url(url: &Url) -> Option<String> {
+pub async fn infer_name_from_url(
+ http_client_provider: &HttpClientProvider,
+ url: &Url,
+) -> Option<String> {
// If there's an absolute url with no path, eg. https://my-cli.com
// perform a request, and see if it redirects another file instead.
let mut url = url.clone();
if url.path() == "/" {
- let client = HttpClient::new(None, None);
- if let Ok(res) = client.get_redirected_response(url.clone(), None).await {
- url = res.url().clone();
+ if let Ok(client) = http_client_provider.get_or_create() {
+ if let Ok(redirected_url) =
+ client.get_redirected_url(url.clone(), None).await
+ {
+ url = redirected_url;
+ }
}
}
@@ -295,16 +301,20 @@ pub async fn install_command(
.await?
.load_and_type_check_files(&[install_flags_global.module_url.clone()])
.await?;
+ let http_client = factory.http_client_provider();
// create the install shim
- create_install_shim(flags, install_flags_global).await
+ create_install_shim(http_client, flags, install_flags_global).await
}
async fn create_install_shim(
+ http_client_provider: &HttpClientProvider,
flags: Flags,
install_flags_global: InstallFlagsGlobal,
) -> Result<(), AnyError> {
- let shim_data = resolve_shim_data(&flags, &install_flags_global).await?;
+ let shim_data =
+ resolve_shim_data(http_client_provider, &flags, &install_flags_global)
+ .await?;
// ensure directory exists
if let Ok(metadata) = fs::metadata(&shim_data.installation_dir) {
@@ -355,6 +365,7 @@ struct ShimData {
}
async fn resolve_shim_data(
+ http_client_provider: &HttpClientProvider,
flags: &Flags,
install_flags_global: &InstallFlagsGlobal,
) -> Result<ShimData, AnyError> {
@@ -372,7 +383,7 @@ async fn resolve_shim_data(
let name = if install_flags_global.name.is_some() {
install_flags_global.name.clone()
} else {
- infer_name_from_url(&module_url).await
+ infer_name_from_url(http_client_provider, &module_url).await
};
let name = match name {
@@ -561,8 +572,10 @@ mod tests {
#[tokio::test]
async fn install_infer_name_from_url() {
+ let http_client = HttpClientProvider::new(None, None);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/abc/server.ts").unwrap()
)
.await,
@@ -570,6 +583,7 @@ mod tests {
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/abc/main.ts").unwrap()
)
.await,
@@ -577,6 +591,7 @@ mod tests {
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/abc/mod.ts").unwrap()
)
.await,
@@ -584,6 +599,7 @@ mod tests {
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/ab%20c/mod.ts").unwrap()
)
.await,
@@ -591,6 +607,7 @@ mod tests {
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/abc/index.ts").unwrap()
)
.await,
@@ -598,42 +615,67 @@ mod tests {
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/abc/cli.ts").unwrap()
)
.await,
Some("abc".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("https://example.com/main.ts").unwrap())
- .await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("https://example.com/main.ts").unwrap()
+ )
+ .await,
Some("main".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("https://example.com").unwrap()).await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("https://example.com").unwrap()
+ )
+ .await,
None
);
assert_eq!(
- infer_name_from_url(&Url::parse("file:///abc/server.ts").unwrap()).await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("file:///abc/server.ts").unwrap()
+ )
+ .await,
Some("server".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("file:///abc/main.ts").unwrap()).await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("file:///abc/main.ts").unwrap()
+ )
+ .await,
Some("abc".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("file:///ab%20c/main.ts").unwrap()).await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("file:///ab%20c/main.ts").unwrap()
+ )
+ .await,
Some("ab c".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("file:///main.ts").unwrap()).await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("file:///main.ts").unwrap()
+ )
+ .await,
Some("main".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("file:///").unwrap()).await,
+ infer_name_from_url(&http_client, &Url::parse("file:///").unwrap()).await,
None
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/abc@0.1.0").unwrap()
)
.await,
@@ -641,6 +683,7 @@ mod tests {
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/abc@0.1.0/main.ts").unwrap()
)
.await,
@@ -648,47 +691,71 @@ mod tests {
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/abc@def@ghi").unwrap()
)
.await,
Some("abc".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("https://example.com/@abc.ts").unwrap())
- .await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("https://example.com/@abc.ts").unwrap()
+ )
+ .await,
Some("@abc".to_string())
);
assert_eq!(
infer_name_from_url(
+ &http_client,
&Url::parse("https://example.com/@abc/mod.ts").unwrap()
)
.await,
Some("@abc".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("file:///@abc.ts").unwrap()).await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("file:///@abc.ts").unwrap()
+ )
+ .await,
Some("@abc".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("file:///@abc/cli.ts").unwrap()).await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("file:///@abc/cli.ts").unwrap()
+ )
+ .await,
Some("@abc".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("npm:cowsay@1.2/cowthink").unwrap())
- .await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("npm:cowsay@1.2/cowthink").unwrap()
+ )
+ .await,
Some("cowthink".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("npm:cowsay@1.2/cowthink/test").unwrap())
- .await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("npm:cowsay@1.2/cowthink/test").unwrap()
+ )
+ .await,
Some("cowsay".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("npm:cowsay@1.2").unwrap()).await,
+ infer_name_from_url(&http_client, &Url::parse("npm:cowsay@1.2").unwrap())
+ .await,
Some("cowsay".to_string())
);
assert_eq!(
- infer_name_from_url(&Url::parse("npm:@types/node@1.2").unwrap()).await,
+ infer_name_from_url(
+ &http_client,
+ &Url::parse("npm:@types/node@1.2").unwrap()
+ )
+ .await,
None
);
}
@@ -700,6 +767,7 @@ mod tests {
std::fs::create_dir(&bin_dir).unwrap();
create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags {
unstable_config: UnstableConfig {
legacy_flag_enabled: true,
@@ -740,6 +808,7 @@ mod tests {
#[tokio::test]
async fn install_inferred_name() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags::default(),
&InstallFlagsGlobal {
module_url: "http://localhost:4545/echo_server.ts".to_string(),
@@ -762,6 +831,7 @@ mod tests {
#[tokio::test]
async fn install_unstable_legacy() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags {
unstable_config: UnstableConfig {
legacy_flag_enabled: true,
@@ -795,6 +865,7 @@ mod tests {
#[tokio::test]
async fn install_unstable_features() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags {
unstable_config: UnstableConfig {
features: vec!["kv".to_string(), "cron".to_string()],
@@ -829,6 +900,7 @@ mod tests {
#[tokio::test]
async fn install_inferred_name_from_parent() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags::default(),
&InstallFlagsGlobal {
module_url: "http://localhost:4545/subdir/main.ts".to_string(),
@@ -852,6 +924,7 @@ mod tests {
async fn install_inferred_name_after_redirect_for_no_path_url() {
let _http_server_guard = test_util::http_server();
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags::default(),
&InstallFlagsGlobal {
module_url: "http://localhost:4550/?redirect_to=/subdir/redirects/a.ts"
@@ -879,6 +952,7 @@ mod tests {
#[tokio::test]
async fn install_custom_dir_option() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags::default(),
&InstallFlagsGlobal {
module_url: "http://localhost:4545/echo_server.ts".to_string(),
@@ -901,6 +975,7 @@ mod tests {
#[tokio::test]
async fn install_with_flags() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags {
permissions: PermissionFlags {
allow_net: Some(vec![]),
@@ -940,6 +1015,7 @@ mod tests {
#[tokio::test]
async fn install_prompt() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags {
permissions: PermissionFlags {
no_prompt: true,
@@ -972,6 +1048,7 @@ mod tests {
#[tokio::test]
async fn install_allow_all() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags {
permissions: PermissionFlags {
allow_all: true,
@@ -1005,6 +1082,7 @@ mod tests {
async fn install_npm_lockfile_default() {
let temp_dir = canonicalize_path(&env::temp_dir()).unwrap();
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags {
permissions: PermissionFlags {
allow_all: true,
@@ -1041,6 +1119,7 @@ mod tests {
#[tokio::test]
async fn install_npm_no_lock() {
let shim_data = resolve_shim_data(
+ &HttpClientProvider::new(None, None),
&Flags {
permissions: PermissionFlags {
allow_all: true,
@@ -1083,6 +1162,7 @@ mod tests {
let local_module_str = local_module.to_string_lossy();
create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags::default(),
InstallFlagsGlobal {
module_url: local_module_str.to_string(),
@@ -1112,6 +1192,7 @@ mod tests {
std::fs::create_dir(&bin_dir).unwrap();
create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags::default(),
InstallFlagsGlobal {
module_url: "http://localhost:4545/echo_server.ts".to_string(),
@@ -1132,6 +1213,7 @@ mod tests {
// No force. Install failed.
let no_force_result = create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags::default(),
InstallFlagsGlobal {
module_url: "http://localhost:4545/cat.ts".to_string(), // using a different URL
@@ -1153,6 +1235,7 @@ mod tests {
// Force. Install success.
let force_result = create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags::default(),
InstallFlagsGlobal {
module_url: "http://localhost:4545/cat.ts".to_string(), // using a different URL
@@ -1180,6 +1263,7 @@ mod tests {
assert!(result.is_ok());
let result = create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags {
config_flag: ConfigFlag::Path(config_file_path.to_string()),
..Flags::default()
@@ -1212,6 +1296,7 @@ mod tests {
std::fs::create_dir(&bin_dir).unwrap();
create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags::default(),
InstallFlagsGlobal {
module_url: "http://localhost:4545/echo_server.ts".to_string(),
@@ -1252,6 +1337,7 @@ mod tests {
std::fs::write(&local_module, "// Some JavaScript I guess").unwrap();
create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags::default(),
InstallFlagsGlobal {
module_url: local_module_str.to_string(),
@@ -1293,6 +1379,7 @@ mod tests {
assert!(result.is_ok());
let result = create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags {
import_map_path: Some(import_map_path.to_string()),
..Flags::default()
@@ -1338,6 +1425,7 @@ mod tests {
assert!(file_module_string.starts_with("file:///"));
let result = create_install_shim(
+ &HttpClientProvider::new(None, None),
Flags::default(),
InstallFlagsGlobal {
module_url: file_module_string.to_string(),