Rewrite HTTP
Users can modify the HTTP requests and responses flowing through Stash via JavaScript scripts.
Parameters:
match: The URL regular expression that the script matches.type: The type of script, optional values arerequestorresponse.require-body: Whether the request body / response body is needed, processing the body in the script requires more memory space, enable only when necessary.timeout: The script execution timeout, in seconds.argument: The argument when the script is executed, type isstring.engine: The script engine. Available values areauto,webkit, andjsc. The default isauto.binary-mode: Binary mode,bodywill be passed to the script asUint8Arrayinstead ofstring.iOS/tvOS2.0.2+max-size: In bytes, requests with a body size exceeding this will not trigger the script.
Script Engine
iOS/tvOS3.6+macOS4.3+You can select a JavaScript engine for each request or response script:
http:
script:
- name: your-script-name
match: ^https://example\.com/
type: response
engine: autoauto: The default. It prefers WebKit on iOS and macOS and uses JavaScriptCore on tvOS.webkit: Uses WebKit. Choose it for scripts that depend on browser Web APIs such asfetch,crypto, orTextEncoder.jsc: Uses JavaScriptCore. It runs scripts that rely only on the Stash scripting APIs on iOS, tvOS, and macOS.
Keep auto for most scripts. Specify webkit or jsc only when a script depends on a particular runtime environment.
Request Object
$request.url: Request URL$request.method: Request method$request.headers: Request headers$request.body: Request body, only available whenrequire-body: true, can bestringorUint8Arraydepending on whether binary mode is enabled.
Response Object
$request.url: Request URL$request.method: Request method$request.headers: Request headers$response.status: Response status code$response.headers: Response headers$response.body: Response body, only available whenrequire-body: true, can bestringorUint8Arraydepending on whether binary mode is enabled.
$done(value)
For all scripts, you must call the $done(value) method to release resources
at the end.
For scripts of the request type, calling $done(object) can rewrite the HTTP request, object can include the following fields:
url: Modify the request URLheaders: Modify the request headersbody: Modify the request bodyresponse: Replace the HTTP response, no longer actually send the HTTP request
You can call $done() to interrupt the request, or $done({}) to not modify any content of the request.
For scripts of the response type, calling $done(object) can rewrite the HTTP response, object can include the following fields:
status: Modify the response status codeheaders: Modify the response headersbody: Modify the response body
You can call $done() to interrupt the request, or $done({}) to not modify any content of the response.