sentinel-agent-js
JavaScript scripting agent for Sentinel reverse proxy. Write custom request/response processing logic in JavaScript.
Features
- Execute JavaScript scripts on request/response lifecycle events
- Fast, lightweight QuickJS engine (via rquickjs)
- Console API for logging (console.log, console.warn, console.error)
- Return-based decision model (allow, block, redirect)
- Header manipulation (add/remove request and response headers)
- Fail-open mode for graceful error handling
Installation
From crates.io
From source
Usage
Command Line Options
| Option | Environment Variable | Description | Default |
|---|---|---|---|
--socket |
AGENT_SOCKET |
Unix socket path | /tmp/sentinel-js.sock |
--script |
JS_SCRIPT |
JavaScript script file | (required) |
--verbose |
JS_VERBOSE |
Enable debug logging | false |
--fail-open |
FAIL_OPEN |
Allow requests on script errors | false |
Writing Scripts
Basic Example
Available Hooks
| Hook | Description |
|---|---|
on_request_headers(request) |
Called when request headers are received |
on_response_headers(response) |
Called when response headers are received |
Request Object
Response Object
Return Values
Scripts should return a decision object:
// Allow the request
return ;
// Block with custom status and body
return ;
// Redirect to another URL
return ;
Header Manipulation
Audit Tags
Add tags for logging and analytics:
Console Logging
Examples
Block Bad User-Agents
Rate Limit by Path
Require Authentication
Add Security Headers
Sentinel Proxy Configuration
agents {
agent "js" {
type "custom"
transport "unix_socket" {
path "/var/run/sentinel/js.sock"
}
events ["request_headers", "response_headers"]
timeout-ms 100
failure-mode "open"
}
}
Error Handling
When --fail-open is enabled, script errors will:
- Log the error
- Allow the request to proceed
- Add
js-errorandfail-opentags to audit metadata
When --fail-open is disabled (default), script errors will:
- Log the error
- Block the request with 500 status
- Add
js-errortag to audit metadata
Comparison with sentinel-agent-lua
| Feature | sentinel-agent-js | sentinel-agent-lua |
|---|---|---|
| Engine | QuickJS | mlua (Lua 5.4) |
| Scripting | Single script file | Multiple scripts with metadata |
| Hot Reload | No | Yes |
| VM Pooling | No | Yes |
| Resource Limits | Minimal | Comprehensive (memory, CPU, time) |
| Standard Library | Basic (console) | Rich (JSON, crypto, regex, etc.) |
| Use Case | Simple scripts | Production workloads |
Use sentinel-agent-js for:
- Simple request filtering logic
- Quick prototyping
- Lightweight deployments
Use sentinel-agent-lua for:
- Complex processing logic
- Production environments with strict resource limits
- Multiple scripts with hot reload
Development
# Run with debug logging
RUST_LOG=debug
# Run tests
License
Apache-2.0