Script
Rewrite HTTP

Rewrite HTTP

Users can modify HTTP requests and responses that pass through Stash using JavaScript scripts.

Configuration Format

http:
  script:
    - match: url-you-want-to-match
      name: your-fancy-script
      type: response # request / response
      require-body: true
      timeout: 20
      argument: ''
      binary-mode: false
      max-size: 1048576 # 1MB
 
script-providers:
  your-fancy-script:
    url: https://your-fancy-script.com/your-fancy-script.js
    interval: 86400

Parameters:

  • match: URL regular expression that the script matches.
  • type: Script type, can be request or response.
  • require-body: Whether the request body / response body is required. Processing the body in the script requires more memory space and should only be enabled when necessary.
  • timeout: Script execution timeout in seconds.
  • argument: Script execution argument, type is string.
  • binary-mode: Binary mode, body is passed to the script as a Uint8Array instead of a string.
  • max-size: In bytes, requests with a body larger than this size will not trigger the script.
💡

Binary mode is only supported in Stash iOS 2.0.2 and later versions.

Request Object

  • $request.url: Request URL.
  • $request.method: Request method.
  • $request.headers: Request headers.
  • $request.body: Request body, only available when requires-body: true. Depending on whether binary mode is enabled, it can be a string or a Uint8Array.

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 when requires-body: true. Depending on whether binary mode is enabled, it can be a string or a Uint8Array.

$done(value)

⚠️

For all scripts, $done(value) must be called at the end to release resources.

For request type scripts, calling $done(object) can rewrite the HTTP request. object can contain the following fields:

  • url: Modify the request URL.
  • headers: Modify the request headers.
  • body: Modify the request body.
  • response: Replace the HTTP response and no longer actually send an HTTP request.

You can call $done() to interrupt the request, or $done({}) to not modify any content of the request.

For response type scripts, calling $done(object) can rewrite the HTTP response. object can contain the following fields:

  • status: Modify the response status code.
  • headers: Modify the response headers.
  • body: Modify the response body.

You can call $done() to interrupt the request, or $done({}) to not modify any content of the response.