use std::collections::HashMap;
use std::sync::LazyLock;
use std::time::Duration;
use deno_core::op2;
use deno_error::JsErrorBox;
use serde::{Deserialize, Serialize};
const DEFAULT_METHOD: &str = "GET";
const DEFAULT_TIMEOUT_SECS: u64 = 30;
const MAX_BODY_BYTES: usize = 10 * 1024 * 1024;
static HTTP_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
reqwest::Client::builder()
.timeout(Duration::from_secs(DEFAULT_TIMEOUT_SECS))
.build()
.expect("failed to build reqwest client for chamberlain.http.fetch")
});
#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase")]
struct FetchOpts {
#[serde(default)]
method: Option<String>,
#[serde(default)]
headers: Option<HashMap<String, String>>,
#[serde(default)]
body: Option<String>,
}
#[derive(Serialize)]
struct FetchResponse {
status: u16,
body: String,
}
#[op2(async)]
#[serde]
pub async fn op_chamberlain_http_fetch(
#[string] url: String,
#[serde] opts: Option<FetchOpts>,
) -> Result<FetchResponse, JsErrorBox> {
let opts = opts.unwrap_or_default();
let method_str = opts
.method
.as_deref()
.unwrap_or(DEFAULT_METHOD)
.to_uppercase();
let method = reqwest::Method::from_bytes(method_str.as_bytes())
.map_err(|e| JsErrorBox::generic(format!("invalid method '{method_str}': {e}")))?;
let mut req = HTTP_CLIENT.request(method, &url);
if let Some(headers) = opts.headers {
for (k, v) in headers {
req = req.header(k, v);
}
}
if let Some(body) = opts.body {
req = req.body(body);
}
let mut resp = req
.send()
.await
.map_err(|e| JsErrorBox::generic(format!("http request failed: {e}")))?;
let status = resp.status().as_u16();
if let Some(len) = resp.content_length() {
if len as usize > MAX_BODY_BYTES {
return Err(JsErrorBox::generic(format!(
"response body size {len} exceeds limit {MAX_BODY_BYTES}"
)));
}
}
let mut buf: Vec<u8> = Vec::new();
while let Some(chunk) = resp
.chunk()
.await
.map_err(|e| JsErrorBox::generic(format!("failed to read response body: {e}")))?
{
if buf.len().saturating_add(chunk.len()) > MAX_BODY_BYTES {
return Err(JsErrorBox::generic(format!(
"response body exceeded limit {MAX_BODY_BYTES}"
)));
}
buf.extend_from_slice(&chunk);
}
let body = String::from_utf8_lossy(&buf).into_owned();
Ok(FetchResponse { status, body })
}