Remove async-std and surf from client

Switch to isahc library. It's not as fancy, but it works and has a smaller footprint.
This commit is contained in:
Nathan Sobo 2022-04-26 21:19:15 -06:00
parent 1293b21b2d
commit 78afbb3599
14 changed files with 161 additions and 474 deletions

View file

@ -1,6 +1,6 @@
use super::installation::{latest_github_release, GitHubLspBinaryVersion};
use anyhow::{anyhow, Result};
use client::http::{HttpClient, Method};
use anyhow::{anyhow, Context, Result};
use client::http::HttpClient;
use futures::{future::BoxFuture, FutureExt, StreamExt};
pub use language::*;
use smol::fs::{self, File};
@ -41,14 +41,10 @@ impl super::LspAdapter for CLspAdapter {
let binary_path = version_dir.join("bin/clangd");
if fs::metadata(&binary_path).await.is_err() {
let response = http
.send(
surf::RequestBuilder::new(Method::Get, version.url)
.middleware(surf::middleware::Redirect::default())
.build(),
)
let mut response = http
.get(&version.url, Default::default())
.await
.map_err(|err| anyhow!("error downloading release: {}", err))?;
.context("error downloading release")?;
let mut file = File::create(&zip_path).await?;
if !response.status().is_success() {
Err(anyhow!(
@ -56,7 +52,7 @@ impl super::LspAdapter for CLspAdapter {
response.status().to_string()
))?;
}
futures::io::copy(response, &mut file).await?;
futures::io::copy(response.body_mut(), &mut file).await?;
let unzip_status = smol::process::Command::new("unzip")
.current_dir(&container_dir)

View file

@ -1,11 +1,13 @@
use anyhow::{anyhow, Context, Result};
use client::http::{self, HttpClient, Method};
use client::http::HttpClient;
use serde::Deserialize;
use smol::io::AsyncReadExt;
use std::{path::Path, sync::Arc};
pub struct GitHubLspBinaryVersion {
pub name: String,
pub url: http::Url,
pub url: String,
}
#[derive(Deserialize)]
@ -30,7 +32,7 @@ pub(crate) struct GithubRelease {
#[derive(Deserialize)]
pub(crate) struct GithubReleaseAsset {
name: String,
browser_download_url: http::Url,
browser_download_url: String,
}
pub async fn npm_package_latest_version(name: &str) -> Result<String> {
@ -81,23 +83,23 @@ pub async fn latest_github_release(
http: Arc<dyn HttpClient>,
asset_name: impl Fn(&str) -> String,
) -> Result<GitHubLspBinaryVersion> {
let release = http
.send(
surf::RequestBuilder::new(
Method::Get,
http::Url::parse(&format!(
"https://api.github.com/repos/{repo_name_with_owner}/releases/latest"
))
.unwrap(),
)
.middleware(surf::middleware::Redirect::default())
.build(),
let mut response = http
.get(
&format!("https://api.github.com/repos/{repo_name_with_owner}/releases/latest"),
Default::default(),
)
.await
.map_err(|err| anyhow!("error fetching latest release: {}", err))?
.body_json::<GithubRelease>()
.context("error fetching latest release")?;
let mut body = Vec::new();
response
.body_mut()
.read_to_end(&mut body)
.await
.map_err(|err| anyhow!("error parsing latest release: {}", err))?;
.context("error reading latest release")?;
let release: GithubRelease =
serde_json::from_slice(body.as_slice()).context("error deserializing latest release")?;
let asset_name = asset_name(&release.name);
let asset = release
.assets

View file

@ -1,8 +1,8 @@
use super::installation::{latest_github_release, GitHubLspBinaryVersion};
use anyhow::{anyhow, Result};
use async_compression::futures::bufread::GzipDecoder;
use client::http::{HttpClient, Method};
use futures::{future::BoxFuture, FutureExt, StreamExt};
use client::http::HttpClient;
use futures::{future::BoxFuture, io::BufReader, FutureExt, StreamExt};
pub use language::*;
use lazy_static::lazy_static;
use regex::Regex;
@ -42,15 +42,11 @@ impl LspAdapter for RustLspAdapter {
let destination_path = container_dir.join(format!("rust-analyzer-{}", version.name));
if fs::metadata(&destination_path).await.is_err() {
let response = http
.send(
surf::RequestBuilder::new(Method::Get, version.url)
.middleware(surf::middleware::Redirect::default())
.build(),
)
let mut response = http
.get(&version.url, Default::default())
.await
.map_err(|err| anyhow!("error downloading release: {}", err))?;
let decompressed_bytes = GzipDecoder::new(response);
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
let mut file = File::create(&destination_path).await?;
futures::io::copy(decompressed_bytes, &mut file).await?;
fs::set_permissions(