Scripting
Cheolsu Proxy includes a JavaScript/TypeScript scripting engine powered by Deno Core (V8). Scripts let you programmatically intercept and modify HTTP requests, responses, and WebSocket messages as they flow through the proxy.
When to Use Scripting
Intercept rules handle straightforward cases like blocking URLs, adding headers, or mapping to local files. Scripting is the right choice when you need logic that rules cannot express:
- Conditional modifications -- Add a header only when the request body contains a specific field, or change the response based on the request URL.
- Data transformation -- Parse JSON responses and mask sensitive fields, rewrite URLs in response bodies, or transform data formats.
- Stateful behavior -- Count requests, aggregate data across multiple calls, or implement rate limiting.
- Dynamic responses -- Generate mock responses with timestamps, random data, or values computed from the request.
- Complex matching -- Use regular expressions, inspect nested JSON fields, or match against multiple conditions.
Practical Recipes
Add CORS Headers to All Responses
When developing a frontend against an API that lacks CORS headers, this script adds them to every response:
Inject an Authorization Token
Automatically add a bearer token to all requests to your API, avoiding the need to configure it in your app during development:
Mask Sensitive Fields in Responses
Replace personal data in API responses so you can share screenshots or recordings without exposing real user information:
Simulate Network Delay
Add artificial latency to test how your application handles slow responses:
Hook API
Hook functions support both synchronous and asynchronous (async/await) patterns.
cheolsu.onRequest(handler)
Called before an HTTP request is forwarded to the server. The handler receives a request object and must return an action.
Request object:
Return values:
cheolsu.onResponse(handler)
Called before a server response is delivered to the client. The handler receives the original request and the response.
Response object:
Return values:
cheolsu.onWebSocketMessage(handler)
Called before a WebSocket message is forwarded. The handler receives a message object.
Message object:
Return values:
Supported File Types
Scripts can be written in JavaScript or TypeScript:
.js,.ts,.mjs,.mts
TypeScript is automatically transpiled using oxc. No build step is required.
Timer API
Note: Timers only fire during hook execution while the event loop is active. They cannot be used as background timers between hook invocations.
Console API
Scripts can log output to the GUI/TUI console panel:
console.log()-- General outputconsole.warn()-- Warning messagesconsole.error()-- Error messagesconsole.info()-- Informational messagesconsole.debug()-- Debug messages
Logs appear in real time, making it easy to trace script behavior as traffic flows through the proxy.
API Reference
Available APIs
Unavailable APIs
Usage
Desktop
- Select Script from the sidebar.
- Write code directly in the built-in Monaco editor, or enter a file path to load a script from disk.
- Run the script with
Cmd/Ctrl + Enter. - Monitor output in the console panel below the editor.
- API documentation is available in the API Reference tab for quick lookup.
TUI
- Navigate to the Script tab.
- Enter the path to a script file.
- Load or unload the script.
MCP
When using the MCP server with an AI assistant, you can ask it to write and load scripts:
- "Write a script that adds CORS headers to all API responses"
- "Create a script that logs all POST request bodies"
- "Write a script that masks email addresses in responses"
Auto Reload
When a script is loaded from a file, Cheolsu Proxy watches the file for changes and automatically reloads the script when the file is saved. A 500ms debounce is applied to avoid reloading during rapid edits.
This works well with an external editor workflow: keep Cheolsu Proxy running, edit your script in your preferred editor, and see changes take effect immediately on the next request.
