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

Revert "prefetch-npm-deps: check response status and fail on error" #365658

Merged
merged 1 commit into from
Dec 16, 2024
Merged
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
32 changes: 8 additions & 24 deletions pkgs/build-support/node/fetch-npm-deps/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use anyhow::bail;
use backoff::{retry, ExponentialBackoff};
use data_encoding::BASE64;
use digest::Digest;
use isahc::{
config::{CaCertificate, Configurable, RedirectPolicy, SslOption},
Body, Request, RequestExt,
};
use log::info;
use nix_nar::{Encoder, NarError};
use serde_json::{Map, Value};
use sha2::Sha256;
Expand All @@ -17,7 +15,7 @@ use std::{
};
use url::Url;

pub fn get_url(url: &Url) -> Result<Body, anyhow::Error> {
pub fn get_url(url: &Url) -> Result<Body, isahc::Error> {
let mut request = Request::get(url.as_str()).redirect_policy(RedirectPolicy::Limit(10));

// Respect SSL_CERT_FILE if environment variable exists
Expand All @@ -39,27 +37,16 @@ pub fn get_url(url: &Url) -> Result<Body, anyhow::Error> {
if let Ok(npm_tokens) = env::var("NIX_NPM_TOKENS") {
if let Ok(tokens) = serde_json::from_str::<Map<String, Value>>(&npm_tokens) {
if let Some(token) = tokens.get(host).and_then(serde_json::Value::as_str) {
info!("Found NPM token for {}. Adding authorization header to request.", host);
request = request.header("Authorization", format!("Bearer {token}"));
}
}
}
}

let res = request.body(())?.send()?;
if !res.status().is_success() {
if res.status().is_client_error() {
bail!("Client error: {}", res.status());
}
if res.status().is_server_error() {
bail!("Server error: {}", res.status());
}
bail!("{}", res.status());
}
Ok(res.into_body())
Ok(request.body(())?.send()?.into_body())
}

pub fn get_url_body_with_retry(url: &Url) -> Result<Vec<u8>, anyhow::Error> {
pub fn get_url_body_with_retry(url: &Url) -> Result<Vec<u8>, isahc::Error> {
retry(ExponentialBackoff::default(), || {
get_url(url)
.and_then(|mut body| {
Expand All @@ -69,15 +56,12 @@ pub fn get_url_body_with_retry(url: &Url) -> Result<Vec<u8>, anyhow::Error> {

Ok(buf)
})
.map_err(|err| match err.downcast_ref::<isahc::Error>() {
Some(isahc_err) => {
if isahc_err.is_network() || isahc_err.is_timeout() {
backoff::Error::transient(err)
} else {
backoff::Error::permanent(err)
}
.map_err(|err| {
if err.is_network() || err.is_timeout() {
backoff::Error::transient(err)
} else {
backoff::Error::permanent(err)
}
None => backoff::Error::permanent(err),
})
})
.map_err(|backoff_err| match backoff_err {
Expand Down
Loading