quickjs-jit-stdlib 0.14.0

LLRT's standard modules for quickjs-jit, vendored into one crate
Documentation
// Vendored from LLRT d9fd873e793c (modules/llrt_http/src/client.rs); module paths and
// feature names adapted by scripts/import-stdlib.py.
use std::convert::Infallible;

use crate::dns_cache::CachedDnsResolver;
use bytes::Bytes;
use http_body_util::combinators::BoxBody;
use hyper_util::{
    client::legacy::{connect::HttpConnector, Client},
    rt::{TokioExecutor, TokioTimer},
};
use once_cell::sync::Lazy;

use crate::http::get_pool_idle_timeout;

#[cfg(any(
    feature = "http-tls-ring",
    feature = "http-tls-aws-lc",
    feature = "http-tls-graviola"
))]
use crate::http::get_http_version;

// Rustls-based TLS backends
#[cfg(any(
    feature = "http-tls-ring",
    feature = "http-tls-aws-lc",
    feature = "http-tls-graviola"
))]
mod rustls_client {
    use super::*;
    use crate::tls::TLS_CONFIG;
    use hyper_rustls::HttpsConnector;
    use rustls::ClientConfig;

    #[cfg(feature = "http-http2")]
    use crate::http::HttpVersion;

    pub type HyperClient =
        Client<HttpsConnector<HttpConnector<CachedDnsResolver>>, BoxBody<Bytes, Infallible>>;

    pub static HTTP_CLIENT: Lazy<Result<HyperClient, Box<dyn std::error::Error + Send + Sync>>> =
        Lazy::new(|| build_client(None));

    pub fn build_client(
        tls_config: Option<ClientConfig>,
    ) -> Result<HyperClient, Box<dyn std::error::Error + Send + Sync>> {
        let pool_idle_timeout = get_pool_idle_timeout();

        let config = if let Some(tls_config) = tls_config {
            tls_config
        } else {
            match &*TLS_CONFIG {
                Ok(tls_config) => tls_config.clone(),
                Err(e) => return Err(e.to_string().into()),
            }
        };

        let builder = hyper_rustls::HttpsConnectorBuilder::new()
            .with_tls_config(config)
            .https_or_http();

        let mut cache_dns_connector = CachedDnsResolver::new().into_http_connector();
        cache_dns_connector.enforce_http(false);

        let https = match get_http_version() {
            #[cfg(feature = "http-http2")]
            HttpVersion::Http2 => builder
                .enable_all_versions()
                .wrap_connector(cache_dns_connector),
            _ => builder.enable_http1().wrap_connector(cache_dns_connector),
        };

        Ok(Client::builder(TokioExecutor::new())
            .pool_idle_timeout(pool_idle_timeout)
            .pool_timer(TokioTimer::new())
            .build(https))
    }
}

// OpenSSL TLS backend
#[cfg(feature = "http-tls-openssl")]
mod openssl_client {
    use super::*;
    use crate::tls::TLS_CONFIG;
    use hyper_openssl::client::legacy::HttpsConnector;
    use openssl::ssl::SslConnectorBuilder;

    pub type HyperClient =
        Client<HttpsConnector<HttpConnector<CachedDnsResolver>>, BoxBody<Bytes, Infallible>>;

    pub static HTTP_CLIENT: Lazy<Result<HyperClient, Box<dyn std::error::Error + Send + Sync>>> =
        Lazy::new(|| build_client(None));

    pub fn build_client(
        tls_config: Option<SslConnectorBuilder>,
    ) -> Result<HyperClient, Box<dyn std::error::Error + Send + Sync>> {
        let pool_idle_timeout = get_pool_idle_timeout();

        let connector = if let Some(tls_config) = tls_config {
            tls_config
        } else {
            match TLS_CONFIG.as_ref() {
                Ok(_builder) => {
                    // Clone the builder by creating a new one with same settings
                    crate::tls::build_client_config(crate::tls::BuildClientConfigOptions {
                        reject_unauthorized: true,
                        ca: None,
                    })?
                }
                Err(e) => return Err(e.to_string().into()),
            }
        };

        let mut cache_dns_connector = CachedDnsResolver::new().into_http_connector();
        cache_dns_connector.enforce_http(false);

        let mut https = HttpsConnector::with_connector(cache_dns_connector, connector)?;
        https.set_callback(|ssl, _| {
            #[cfg(feature = "http-http2")]
            ssl.set_alpn_protos(b"\x02h2\x08http/1.1")?;
            #[cfg(not(feature = "http-http2"))]
            ssl.set_alpn_protos(b"\x08http/1.1")?;
            Ok(())
        });

        Ok(Client::builder(TokioExecutor::new())
            .pool_idle_timeout(pool_idle_timeout)
            .pool_timer(TokioTimer::new())
            .build(https))
    }
}

// Re-export based on feature
#[cfg(any(
    feature = "http-tls-ring",
    feature = "http-tls-aws-lc",
    feature = "http-tls-graviola"
))]
pub use rustls_client::*;

#[cfg(feature = "http-tls-openssl")]
pub use openssl_client::*;