Improve startup time further by not using a reqwest client

feature/lookup-installed
trivernis 1 year ago
parent 643c5999f0
commit b53a343ebe
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -4,6 +4,7 @@ use std::{
str::FromStr, str::FromStr,
}; };
use futures::future;
use semver::{Version, VersionReq}; use semver::{Version, VersionReq};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use tokio::{ use tokio::{
@ -111,10 +112,18 @@ impl Repository {
&*BIN_DIR, &*BIN_DIR,
&*NODE_VERSIONS_DIR, &*NODE_VERSIONS_DIR,
]; ];
for dir in dirs { for result in future::join_all(dirs.into_iter().map(|dir| async move {
if !dir.exists() { if !dir.exists() {
fs::create_dir_all(dir).await.into_diagnostic()?; fs::create_dir_all(dir).await.into_diagnostic()?;
} }
Ok(())
}))
.await
{
if let Err(e) = result {
return Err(e);
}
} }
Ok(()) Ok(())

@ -9,8 +9,6 @@ use crate::{
utils::progress_bar, utils::progress_bar,
}; };
use reqwest::Client;
mod model; mod model;
use futures_util::StreamExt; use futures_util::StreamExt;
use miette::{miette, Context, IntoDiagnostic, Result}; use miette::{miette, Context, IntoDiagnostic, Result};
@ -23,7 +21,6 @@ mod test;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct WebApi { pub struct WebApi {
base_url: String, base_url: String,
client: Client,
} }
impl Default for WebApi { impl Default for WebApi {
@ -37,17 +34,13 @@ impl WebApi {
pub fn new<S: ToString>(base_url: S) -> Self { pub fn new<S: ToString>(base_url: S) -> Self {
Self { Self {
base_url: base_url.to_string(), base_url: base_url.to_string(),
client: Client::new(),
} }
} }
/// Returns the list of available node versions /// Returns the list of available node versions
#[tracing::instrument(level = "debug")] #[tracing::instrument(level = "debug")]
pub async fn get_versions(&self) -> Result<Vec<VersionInfo>> { pub async fn get_versions(&self) -> Result<Vec<VersionInfo>> {
let versions = self let versions = reqwest::get(format!("{}/index.json", self.base_url))
.client
.get(format!("{}/index.json", self.base_url))
.send()
.await .await
.map_err(ReqwestError::from) .map_err(ReqwestError::from)
.context("Fetching versions")? .context("Fetching versions")?
@ -67,16 +60,13 @@ impl WebApi {
version: S, version: S,
writer: &mut W, writer: &mut W,
) -> Result<u64> { ) -> Result<u64> {
let res = self let res = reqwest::get(format!(
.client "{}/v{version}/node-v{version}{}",
.get(format!( self.base_url, *NODE_ARCHIVE_SUFFIX
"{}/v{version}/node-v{version}{}", ))
self.base_url, *NODE_ARCHIVE_SUFFIX .await
)) .map_err(ReqwestError::from)
.send() .context("Downloading nodejs")?;
.await
.map_err(ReqwestError::from)
.context("Downloading nodejs")?;
let total_size = res let total_size = res
.content_length() .content_length()

Loading…
Cancel
Save