Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/auto-ready-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr merge "${{ github.event.pull_request.html_url }}" --auto --merge
gh pr merge "${{ github.event.pull_request.html_url }}" --auto --merge || true
130 changes: 46 additions & 84 deletions stdlib/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,50 @@ use techscript_runtime::{
value::RuntimeValue,
};

fn perform_http_request(
ctx: &techscript_runtime::context::RuntimeContext,
method: &str,
url: &str,
body_opt: Option<&str>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied".to_string(),
),
None,
None,
));
}
let response = if let Some(body) = body_opt {
ureq::post(url).send_string(body)
} else {
ureq::get(url).call()
}
.map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!("HTTP {} request failed: {}", method, e)),
None,
None,
)
})?;
let status = response.status();
let res_body = response.into_string().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!("Failed to read HTTP response body: {}", e)),
None,
None,
)
})?;
let mut res_map = IndexMap::new();
res_map.insert("status".to_string(), RuntimeValue::Int(status as i64));
res_map.insert("body".to_string(), RuntimeValue::Str(res_body));
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(res_map)),
is_const: false,
})
}

impl StdlibRegistry {
pub fn register_http(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
Expand All @@ -20,49 +64,8 @@ impl StdlibRegistry {
name: "get".to_string(),
arity: 1,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied"
.to_string(),
),
None,
None,
));
}
let url = args[0].try_into_string()?;
let response = ureq::get(&url).call().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"HTTP GET request failed: {}",
e
)),
None,
None,
)
})?;
let status = response.status();
let body = response.into_string().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"Failed to read HTTP response body: {}",
e
)),
None,
None,
)
})?;

// âš¡ Bolt Performance Optimization:
// Pre-allocating the IndexMap capacity matching the expected number of keys
// avoids intermediate allocations and re-hashing during insertion.
let mut res_map = IndexMap::with_capacity(2);
res_map.insert("status".to_string(), RuntimeValue::Int(status as i64));
res_map.insert("body".to_string(), RuntimeValue::Str(body));
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(res_map)),
is_const: false,
})
perform_http_request(ctx, "GET", &url, None)
},
}),
);
Expand All @@ -73,50 +76,9 @@ impl StdlibRegistry {
name: "post".to_string(),
arity: 2,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied"
.to_string(),
),
None,
None,
));
}
let url = args[0].try_into_string()?;
let body = args[1].try_into_string()?;
let response = ureq::post(&url).send_string(&body).map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"HTTP POST request failed: {}",
e
)),
None,
None,
)
})?;
let status = response.status();
let res_body = response.into_string().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"Failed to read HTTP response body: {}",
e
)),
None,
None,
)
})?;

// âš¡ Bolt Performance Optimization:
// Pre-allocating the IndexMap capacity matching the expected number of keys
// avoids intermediate allocations and re-hashing during insertion.
let mut res_map = IndexMap::with_capacity(2);
res_map.insert("status".to_string(), RuntimeValue::Int(status as i64));
res_map.insert("body".to_string(), RuntimeValue::Str(res_body));
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(res_map)),
is_const: false,
})
perform_http_request(ctx, "POST", &url, Some(&body))
},
}),
);
Expand Down