Skip to main content

JavaScript Scripting

Rockxy includes a JavaScript scripting engine (JavaScriptCore) that lets you write scripts to inspect, modify, mock, or filter traffic. Scripts run against matched requests in the proxy pipeline with a 5-second execution timeout. Enabled scripts are loaded at app launch and run automatically — you do not need to keep the Scripting window open.
JavaScript scripting window

Scripting window with the script list, editor, and matching rules

Entry Points

Public JavaScript API

Scripts may use either the multi-arg API (recommended) or the legacy single-arg API. Rockxy detects which by inspecting the JS function’s length at load time, so both work side-by-side.
request.headers and request.queries are plain JS dictionaries — assign directly. response.statusCode accepts any integer in [100, 599]. response.bodyFilePath accepts a path under ~; the file is loaded with the same size cap as captured response bodies.

Sharing state between the request and response of one flow

context.sharedState is an object that starts empty for every request and is handed to the same flow’s onResponse. Use it when the response decision depends on something only the request carries — for example, mocking a single GraphQL operation on a shared endpoint:
State is private to each script and each flow; concurrent requests never see each other’s values. It is released when the response hook runs, and state for flows that never receive a response (blocked, mocked, or failed requests) is discarded after newer flows replace it. Use $rockxy.storage for values that must outlive a single flow.

Showing your own data in the inspector

Push { title, text } entries onto context.previewTabs to add a Script tab to the request or response inspector — useful for a decrypted payload, a signature check, or one field of a large body. Nothing is sent differently on the wire.
text may be a string or any JSON value (shown pretty-printed). Hooks run on the request side add tabs to the Request inspector, response hooks to the Response inspector; each exchange keeps up to 8 tabs per side, titles are cut at 40 characters and text at 256 KB. Tabs are saved with the session but left out of redacted MCP output. This requires the (context, url, request[, response]) function shape.

Single-arg API (legacy)

Scripts define one or both of these functions, exported either as direct globals or via CommonJS module.exports:
Both styles are supported in the same script.

Request hook

onRequest(ctx) receives a request context. You may mutate it via ctx.setHeader(name, value), ctx.setURL(newURL), or ctx.setBody(newBody), then return the context. Returning null blocks the request locally with HTTP 403. Allowed mutations propagate to upstream: method, path, query, headers, body. Attempts to change host, port, or scheme are dropped (with a one-time warning per plugin) — use the Map Remote rule action for cross-host rewrites.

Response hook

onResponse(ctx) receives the buffered upstream response. Use ctx.setStatus(code), ctx.setHeader(name, value), or ctx.setBody(newBody) to mutate the response, then return the context. Mutations are reflected in both the bytes the client receives and the persisted transaction record.

Mock responses

When a script’s manifest sets runAsMock: true, the value returned from onRequest(ctx) is interpreted as the mock response object. The request never goes upstream:
Mock responses must include a numeric statusCode in [100, 599]. Invalid mock output fails locally with HTTP 502; the request is never forwarded upstream.

$rockxy bridge

The $rockxy global exposes utilities to scripts:

Per-Script Behavior (scriptBehavior manifest block)

Each script-type plugin’s plugin.json may include an optional scriptBehavior block:
Scripts execute in deterministic, id-sorted order. By default, the first matching request-side script wins. If you enable Allow Running Multiple Scripts for one Request from the Advance menu, Rockxy chains matching request-side scripts in that same deterministic order. This toggle maps to the allowMultipleScriptsPerRequest setting used by the scripting runtime.

Pipeline order

For each captured request:
  1. Rules engine evaluates the rule list. Matching Modify Headers rules change the request headers. If another rule action consumes the request (block, map, breakpoint, throttle, etc.), scripts are skipped on the request side.
  2. Request-side scripts run on the (possibly header-modified) request. Outcome:
    • forward → the mutated request is sent upstream.
    • null return → local 403.
    • mock return (when runAsMock=true) → local response, never upstream.
  3. Response header rules run on the upstream response, and on a script’s mock response.
  4. Response-side scripts run on the buffered response.
  5. Response breakpoint (if armed) operates on the script-mutated response.
  6. The response is relayed to the client and persisted in the transaction.

Bounded response bodies

Rockxy already caps captured response bodies at 100 MB. When a response exceeds that cap, response-side scripting is skipped for that request and the existing full streaming behavior is preserved. This guarantees scripting can never silently truncate the bytes the client receives.

Community limit

Rockxy Community allows up to 10 enabled scripts at a time. Attempting to enable an 11th surfaces a quota error. The total number of installed (disabled + enabled) scripts is not capped.

Error feedback

Rockxy surfaces script errors inline:
  • Timeout — scripts that exceed the 5-second limit are terminated with a timeout error.
  • JavaScript exceptions — runtime errors show the exception message in the per-plugin OSLog category.
  • Load failures — scripts that fail during initial load show the error in Settings > Plugins for that plugin.
  • Last run — while the Script Editor is open, its status line shows when the script last ran on live traffic and whether that run succeeded. A failed run stays visible in red until a later run succeeds; hover it to read the error.

Templates

Rockxy ships six starter templates. Pick one from the empty Scripting window, or choose New Script from Template in the footer’s more menu:
  • Modify Headers — add a header to every request.
  • Log Requests — console.log the request URL.
  • Block Pattern — return null for URLs matching a substring.
  • Custom Response — mock template; runAsMock=true is set automatically when the script is created.
  • Rewrite URL — modify the path of matching requests.
  • Conditional Mock JSON — return mock JSON for matching URLs; runAsMock=true is set automatically.

Limitations

  • Scripts run in a JavaScriptCore sandbox with no direct filesystem or network access.
  • Each script execution has a 5-second timeout.
  • Scripts cannot change a request’s host, port, or scheme. Use Map Remote for cross-host rewrites.
  • Response scripting is skipped when the upstream body exceeds Rockxy’s capture cap.

Next Steps

Traffic Rules

Declarative rules for blocking, mapping, and modifying traffic without code.

Traffic Capture

How Rockxy captures and displays network traffic.