mirror of https://github.com/Trivernis/nenv
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.6 KiB
Rust
97 lines
2.6 KiB
Rust
use std::{
|
|
cmp::min,
|
|
fmt::{Debug, Display},
|
|
};
|
|
|
|
use crate::{
|
|
consts::{NODE_ARCHIVE_SUFFIX, NODE_DIST_URL},
|
|
error::ReqwestError,
|
|
utils::progress_bar,
|
|
};
|
|
|
|
mod model;
|
|
use futures_util::StreamExt;
|
|
use miette::{miette, Context, IntoDiagnostic, Result};
|
|
pub use model::*;
|
|
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
|
|
|
#[cfg(test)]
|
|
mod test;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct WebApi {
|
|
base_url: String,
|
|
}
|
|
|
|
impl Default for WebApi {
|
|
fn default() -> Self {
|
|
Self::new(NODE_DIST_URL)
|
|
}
|
|
}
|
|
|
|
impl WebApi {
|
|
/// Creates a new instance to access the nodejs website
|
|
pub fn new<S: ToString>(base_url: S) -> Self {
|
|
Self {
|
|
base_url: base_url.to_string(),
|
|
}
|
|
}
|
|
|
|
/// Returns the list of available node versions
|
|
#[tracing::instrument(level = "debug")]
|
|
pub async fn get_versions(&self) -> Result<Vec<VersionInfo>> {
|
|
let versions = reqwest::get(format!("{}/index.json", self.base_url))
|
|
.await
|
|
.map_err(ReqwestError::from)
|
|
.context("Fetching versions")?
|
|
.json()
|
|
.await
|
|
.map_err(ReqwestError::from)
|
|
.context("Parsing versions response")?;
|
|
|
|
Ok(versions)
|
|
}
|
|
|
|
/// Downloads a specific node version
|
|
/// and writes it to the given writer
|
|
#[tracing::instrument(level = "debug", skip(writer))]
|
|
pub async fn download_version<W: AsyncWrite + Unpin, S: Display + Debug>(
|
|
&self,
|
|
version: S,
|
|
writer: &mut W,
|
|
) -> Result<u64> {
|
|
let res = reqwest::get(format!(
|
|
"{}/v{version}/node-v{version}{}",
|
|
self.base_url, *NODE_ARCHIVE_SUFFIX
|
|
))
|
|
.await
|
|
.map_err(ReqwestError::from)
|
|
.context("Downloading nodejs")?;
|
|
|
|
let total_size = res
|
|
.content_length()
|
|
.ok_or_else(|| miette!("Missing content_length header"))?;
|
|
|
|
let pb = progress_bar(total_size);
|
|
pb.set_message(format!("Downloading node v{version}"));
|
|
let mut stream = res.bytes_stream();
|
|
let mut total_downloaded = 0;
|
|
|
|
while let Some(item) = stream.next().await {
|
|
let chunk = item.map_err(ReqwestError::from)?;
|
|
writer
|
|
.write_all(&chunk)
|
|
.await
|
|
.into_diagnostic()
|
|
.context("Writing download chunk to file")?;
|
|
total_downloaded = min(chunk.len() as u64 + total_downloaded, total_size);
|
|
pb.set_position(total_downloaded);
|
|
}
|
|
|
|
writer.flush().await.into_diagnostic()?;
|
|
pb.finish_with_message(format!("Downloaded node v{version}."));
|
|
|
|
Ok(total_downloaded)
|
|
}
|
|
}
|