Skip to content

The headless browser for AI agents and web scraping

fix(js): expose Response.body and Response.bodyUsed (#818)

xrip068676fd65d9
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index f6381771..f7b89f34 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -7480,11 +7480,49 @@ if (typeof Response === 'undefined') {
this.ok = this.status >= 200 && this.status < 300;
this.headers = new Headers(init.headers);
this.type = init.type || 'basic'; this.url = init.url || ''; this.redirected = !!init.redirected;
+ // #818: body/bodyUsed. A null-body response (null or no body passed)
+ // has body === null; every other body is a one-chunk stream, created
+ // lazily so merely touching .body does not copy the bytes.
+ this._bodyNull = body === null || body === undefined;
+ this._bodyStream = null;
+ this._bodyUsed = false;
+ }
+ _consumeBody() {
+ if (this._bodyUsed) throw new TypeError("Body is already consumed");
+ this._bodyUsed = true;
+ }
+ get body() {
+ if (this._bodyNull) return null;
+ if (this._bodyUsed) throw new TypeError("Body is already consumed");
+ if (!this._bodyStream) {
+ this._bodyStream = new ReadableStream({
+ start: (controller) => {
+ if (this._bodyBytes.length) controller.enqueue(this._bodyBytes);
+ controller.close();
+ },
+ });
+ // bodyUsed flips the moment the stream is locked for reading
+ // (spec: the body becomes "disturbed"), which no state probe can
+ // observe on a native ReadableStream, so hook getReader instead.
+ const response = this;
+ const stream = this._bodyStream;
+ const getReader = stream.getReader.bind(stream);
+ stream.getReader = function () {
+ response._bodyUsed = true;
+ return getReader();
+ };
+ stream.releaseLock = function () {
+ response._bodyUsed = true;
+ stream.locked = false;
+ };
+ }
+ return this._bodyStream;
}
- async text() { return _decodeBodyWithCharset(this._bodyBytes, this.headers); }
- async json() { return JSON.parse(await this.text()); }
- async arrayBuffer() { return _arrayBufferFromBytes(this._bodyBytes); }
- async blob() { return new Blob([this._bodyBytes]); }
+ get bodyUsed() { return this._bodyUsed; }
+ async text() { this._consumeBody(); return _decodeBodyWithCharset(this._bodyBytes, this.headers); }
+ async json() { this._consumeBody(); return JSON.parse(await _decodeBodyWithCharset(this._bodyBytes, this.headers)); }
+ async arrayBuffer() { this._consumeBody(); return _arrayBufferFromBytes(this._bodyBytes); }
+ async blob() { this._consumeBody(); return new Blob([this._bodyBytes]); }
clone() { return new Response(this._bodyBytes, { status: this.status, statusText: this.statusText, headers: this.headers, type: this.type, url: this.url, redirected: this.redirected }); }
static error() { return new Response(null, { status: 0 }); }
static redirect(url, status) { return new Response(null, { status: status || 302, headers: { Location: url } }); }
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 8b2df06a..8411b143 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -13690,6 +13690,46 @@ mod tests {
);
}
+ #[tokio::test(flavor = "current_thread")]
+ async fn response_body_exposes_stream_and_consumption_state() {
+ // #818: a non-null Response body must expose a ReadableStream through
+ // .body, a boolean .bodyUsed, and a working getReader(); consuming
+ // the body marks it used and a second consumption throws.
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .evaluate_for_cdp(
+ r#"(async () => {
+ const r = new Response("hello");
+ const meta = {
+ bodyType: r.body === null ? "null" : typeof r.body,
+ bodyUsed: r.bodyUsed,
+ hasReader: !!(r.body && r.body.getReader),
+ };
+ const reader = r.body.getReader();
+ const first = await reader.read();
+ const second = await reader.read();
+ const chunkText = first.value ? new TextDecoder().decode(first.value) : "";
+ const consumed = r.bodyUsed;
+ let doubleThrew = false;
+ try { await r.text(); } catch (e) { doubleThrew = true; }
+ return { meta, chunkText, done: second.done, consumed, doubleThrew, nullBody: new Response(null).body === null };
+ })()"#,
+ true,
+ true,
+ )
+ .await
+ .expect("evaluation must succeed");
+ let out = result.value.unwrap();
+ assert_eq!(out["meta"]["bodyType"], "object");
+ assert_eq!(out["meta"]["bodyUsed"], false);
+ assert_eq!(out["meta"]["hasReader"], true);
+ assert_eq!(out["chunkText"], "hello");
+ assert_eq!(out["done"], true);
+ assert_eq!(out["consumed"], true);
+ assert_eq!(out["doubleThrew"], true);
+ assert_eq!(out["nullBody"], true);
+ }
+
#[test]
fn test_navigator() {
let mut rt = setup_runtime("<html><body></body></html>");