Skip to content

Commit 4238e6a

Browse files
style: cargo fmt
1 parent 7a25f41 commit 4238e6a

2 files changed

Lines changed: 48 additions & 41 deletions

File tree

src/lib.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ impl JsAgent {
246246
.map_err(|e| anyhow::anyhow!("Lock poisoned: {}", e))?;
247247
*limit = max_mem;
248248

249-
info!(memory_limit_mb = max_mem / (1024 * 1024), "Memory limit updated");
249+
info!(
250+
memory_limit_mb = max_mem / (1024 * 1024),
251+
"Memory limit updated"
252+
);
250253
}
251254

252255
// Rebuild cached context if script changed
@@ -363,42 +366,43 @@ impl JsAgent {
363366
.as_ref()
364367
.ok_or_else(|| anyhow::anyhow!("No cached context available"))?;
365368

366-
ctx_ref.with(|ctx| {
367-
// Check if function exists (already defined from cached script eval)
368-
let globals = ctx.globals();
369-
let func: Option<Function> = globals.get(fn_name).ok();
369+
ctx_ref
370+
.with(|ctx| {
371+
// Check if function exists (already defined from cached script eval)
372+
let globals = ctx.globals();
373+
let func: Option<Function> = globals.get(fn_name).ok();
370374

371-
let Some(func) = func else {
372-
debug!(function = fn_name, "Function not defined in script");
373-
return Ok(None);
374-
};
375+
let Some(func) = func else {
376+
debug!(function = fn_name, "Function not defined in script");
377+
return Ok(None);
378+
};
375379

376-
// Convert argument to JS value
377-
let js_arg = Self::json_to_js(&ctx, &arg)?;
380+
// Convert argument to JS value
381+
let js_arg = Self::json_to_js(&ctx, &arg)?;
378382

379-
// Call the function
380-
let result: Value = func.call((js_arg,))?;
383+
// Call the function
384+
let result: Value = func.call((js_arg,))?;
381385

382-
// Convert result to ScriptResult
383-
let json_result = Self::js_to_json(&result);
386+
// Convert result to ScriptResult
387+
let json_result = Self::js_to_json(&result);
384388

385-
if json_result.is_null() {
386-
return Ok(Some(ScriptResult {
387-
decision: "allow".to_string(),
388-
..Default::default()
389-
}));
390-
}
389+
if json_result.is_null() {
390+
return Ok(Some(ScriptResult {
391+
decision: "allow".to_string(),
392+
..Default::default()
393+
}));
394+
}
391395

392-
let script_result: ScriptResult =
393-
serde_json::from_value(json_result).map_err(|e| rquickjs::Error::FromJs {
394-
from: "object",
395-
to: "ScriptResult",
396-
message: Some(format!("Failed to parse result: {}", e)),
397-
})?;
396+
let script_result: ScriptResult =
397+
serde_json::from_value(json_result).map_err(|e| rquickjs::Error::FromJs {
398+
from: "object",
399+
to: "ScriptResult",
400+
message: Some(format!("Failed to parse result: {}", e)),
401+
})?;
398402

399-
Ok(Some(script_result))
400-
})
401-
.map_err(|e: rquickjs::Error| anyhow::anyhow!("JavaScript error: {}", e))
403+
Ok(Some(script_result))
404+
})
405+
.map_err(|e: rquickjs::Error| anyhow::anyhow!("JavaScript error: {}", e))
402406
}
403407

404408
/// Build AgentResponse from ScriptResult
@@ -825,10 +829,9 @@ impl AgentHandlerV2 for JsAgent {
825829
let total = self.requests_total.load(Ordering::Relaxed);
826830
let errors = self.script_errors.load(Ordering::Relaxed);
827831

828-
report.counters.push(CounterMetric::new(
829-
"js_agent_requests_total",
830-
total,
831-
));
832+
report
833+
.counters
834+
.push(CounterMetric::new("js_agent_requests_total", total));
832835

833836
report.counters.push(CounterMetric::new(
834837
"js_agent_requests_allowed_total",
@@ -840,10 +843,9 @@ impl AgentHandlerV2 for JsAgent {
840843
self.requests_blocked.load(Ordering::Relaxed),
841844
));
842845

843-
report.counters.push(CounterMetric::new(
844-
"js_agent_script_errors_total",
845-
errors,
846-
));
846+
report
847+
.counters
848+
.push(CounterMetric::new("js_agent_script_errors_total", errors));
847849

848850
// Gauges
849851
let error_rate = if total > 0 {

tests/integration.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
//! These tests directly invoke the handler methods to verify the JavaScript
44
//! scripting logic works correctly with the v2 protocol.
55
6+
use std::collections::HashMap;
67
use zentinel_agent_js::JsAgent;
78
use zentinel_agent_protocol::v2::{AgentHandlerV2, DrainReason, ShutdownReason};
8-
use zentinel_agent_protocol::{Decision, EventType, HeaderOp, RequestHeadersEvent, RequestMetadata, ResponseHeadersEvent};
9-
use std::collections::HashMap;
9+
use zentinel_agent_protocol::{
10+
Decision, EventType, HeaderOp, RequestHeadersEvent, RequestMetadata, ResponseHeadersEvent,
11+
};
1012

1113
/// Create a basic request metadata
1214
fn make_metadata() -> RequestMetadata {
@@ -611,7 +613,10 @@ async fn test_on_drain() {
611613
// Requests while draining should still be allowed
612614
let event = make_request_headers("GET", "/api", HashMap::new());
613615
let response = agent.on_request_headers(event).await;
614-
assert!(is_allow(&response.decision), "Expected Allow while draining");
616+
assert!(
617+
is_allow(&response.decision),
618+
"Expected Allow while draining"
619+
);
615620
}
616621

617622
// ============================================================================

0 commit comments

Comments
 (0)