feat(script): add res.bytes() so JavaScript can read a binary body

This commit is contained in:
aykhans committed 2026-09-29 23:22:51 +04:00
1 parent a327c4978b
commit 17dfa16051
2 files changed
+13

No files matched your search

+3
View File
@@ -609,9 +609,12 @@ Unknown options and values of the wrong type raise an error.
| `header(name)` | First value of a header, case-insensitive; `nil`/`null` if missing | | `header(name)` | First value of a header, case-insensitive; `nil`/`null` if missing |
| `cookie(name)` | Value of a cookie from `Set-Cookie`; `nil`/`null` if missing | | `cookie(name)` | Value of a cookie from `Set-Cookie`; `nil`/`null` if missing |
| `json()` | JavaScript only: parses the body as JSON | | `json()` | JavaScript only: parses the body as JSON |
| `bytes()` | JavaScript only: the body as a `Uint8Array` |
In Lua, call the helpers as `res:header("name")` or `res.header("name")`. In Lua, call the helpers as `res:header("name")` or `res.header("name")`.
> **Note:** JavaScript strings hold UTF-16, so `body` replaces bytes that are not valid UTF-8 and `body.length` counts code units rather than bytes. Use `bytes()` for binary responses and for the real byte count. Lua strings keep the bytes as they arrived.
**Behavior:** **Behavior:**
- **Runs on every request:** the requests are sent each time `transform` runs, so every main request triggers them. Script globals persist across requests within a worker, so a script can cache a value itself (e.g. `if not token then token = login() end`). - **Runs on every request:** the requests are sent each time `transform` runs, so every main request triggers them. Script globals persist across requests within a worker, so a script can cache a value itself (e.g. `if not token then token = login() end`).
+10
View File
@@ -159,6 +159,16 @@ func (e *JsEngine) httpResponseToObject(resp *HTTPResponse) *goja.Object {
} }
return value return value
}) })
// body is a JavaScript string, so bytes that are not valid UTF-8 reach the script
// only through this.
_ = obj.Set("bytes", func(goja.FunctionCall) goja.Value {
buffer := rt.NewArrayBuffer([]byte(resp.Body))
value, err := rt.New(rt.Get("Uint8Array").ToObject(rt), rt.ToValue(buffer))
if err != nil {
panic(err)
}
return value
})
return obj return obj
} }