The headless browser for AI agents and web scraping
fix(browser): preserve response bodies during runtime suspension
SGavrl73358fdb402d
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rsindex 9c5f229d..698cbf01 100644--- a/crates/obscura-browser/src/page.rs+++ b/crates/obscura-browser/src/page.rs@@ -4674,6 +4674,19 @@ impl Page {}// Completed module and JS requests must survive the realm's teardown.self.sync_js_network_events();+ let max_entries = response_body_entry_limit();+ for (id, body) in self.js.as_ref().unwrap().take_network_response_bodies() {+ self.response_bodies.insert(id.clone(), StoredResponseBody {+ body: body.body,+ base64_encoded: body.base64_encoded,+ });+ self.response_body_order.push_back(id);+ }+ while self.response_body_order.len() > max_entries {+ if let Some(oldest) = self.response_body_order.pop_front() {+ self.response_bodies.remove(&oldest);+ }+ }// Suspension rebuilds the renderer cache on resume (`take_dom`), so// results for the suspended realm would have nowhere to go.self.retire_render_resources();diff --git a/crates/obscura-cdp/tests/module_network_events.rs b/crates/obscura-cdp/tests/module_network_events.rsindex 24f1056c..d3af5eb6 100644--- a/crates/obscura-cdp/tests/module_network_events.rs+++ b/crates/obscura-cdp/tests/module_network_events.rs@@ -111,20 +111,23 @@ async fn static_graph(stealth: bool) {const ROOT: &str = "import '/child.js'; globalThis.rootRuns=(globalThis.rootRuns||0)+1;";const CHILD: &str = "globalThis.childRuns=(globalThis.childRuns||0)+1;";const ASYNC: &str = "globalThis.asyncRan=true;";+ const CLASSIC: &str = "globalThis.classicRan=true;";let base = serve(vec![- ("/", 200, "", b"<script type='module' src='/root.js'></script><script type='module' src='/child.js'></script><script type='module' src='/root.js'></script><script type='module' async src='/async.js'></script>"),+ ("/", 200, "", b"<script src='/classic.js'></script><script type='module' src='/root.js'></script><script type='module' src='/child.js'></script><script type='module' src='/root.js'></script><script type='module' async src='/async.js'></script>"),("/root.js", 201, "Content-Disposition: attachment\r\n", ROOT.as_bytes()),("/child.js", 200, "", CHILD.as_bytes()),("/async.js", 200, "", ASYNC.as_bytes()),+ ("/classic.js", 200, "Content-Disposition: attachment\r\n", CLASSIC.as_bytes()),]).await;let mut ctx = navigate(&base, stealth).await;let result = cdp(&mut ctx, "Runtime.evaluate", json!({- "expression": "[globalThis.rootRuns,globalThis.childRuns,globalThis.asyncRan]", "returnByValue": true,+ "expression": "[globalThis.rootRuns,globalThis.childRuns,globalThis.asyncRan,globalThis.classicRan]", "returnByValue": true,})).await;- assert_eq!(result["result"]["value"], json!([1,1,true]));+ assert_eq!(result["result"]["value"], json!([1,1,true,true]));assert_module(&mut ctx, &base, "/root.js", 201, ROOT).await;assert_module(&mut ctx, &base, "/child.js", 200, CHILD).await;assert_module(&mut ctx, &base, "/async.js", 200, ASYNC).await;+ assert_module(&mut ctx, &base, "/classic.js", 200, CLASSIC).await;}#[tokio::test(flavor = "current_thread")]@@ -250,10 +253,57 @@ async fn module_records_survive_page_runtime_suspension() {page.evaluate("import('/lazy.js')");page.settle_for_duration(100).await;assert_eq!(page.evaluate("globalThis.lazyRan"), json!(true));+ page.sync_js_network_events();+ let bodies = [+ ("/root.js", "import '/child.js';"),+ ("/child.js", "globalThis.childRan=true;"),+ ("/lazy.js", "globalThis.lazyRan=true;"),+ ].map(|(path, body)| {+ let id = page.network_events.iter().find(|e| e.url == format!("{base}{path}"))+ .unwrap().request_id.clone();+ assert_eq!(page.get_response_body(&id).unwrap().body, body);+ (id, body)+ });page.suspend_js();for path in ["/root.js", "/child.js", "/lazy.js"] {assert_eq!(page.network_events.iter().filter(|e| e.url == format!("{base}{path}") && e.body_size > 0).count(), 1);}+ for (id, body) in &bodies {+ assert_eq!(page.get_response_body(id).map(|b| b.body), Some(body.to_string()),+ "retained body must survive runtime suspension: {id}");+ }+ page.resume_js();+ page.suspend_js();+ for (id, body) in &bodies {+ assert_eq!(page.get_response_body(id).map(|b| b.body), Some(body.to_string()),+ "resuming must not discard retained bodies: {id}");+ }+}++#[tokio::test(flavor = "current_thread")]+async fn suspension_enforces_combined_response_body_entry_limit() {+ let _env = EnvOverride::set("OBSCURA_NETWORK_BODY_BUFFER_ENTRIES", "1");+ const ROOT: &str = "import '/child.js';";+ const CHILD: &str = "export const ready=true;";+ let base = serve(vec![+ ("/", 200, "", b"<script type='module' src='/root.js'></script>"),+ ("/root.js", 200, "", ROOT.as_bytes()),+ ("/child.js", 200, "", CHILD.as_bytes()),+ ]).await;+ let mut ctx = navigate(&base, false).await;+ let root = module_request_id(&ctx, &format!("{base}/root.js"), 200, ROOT.len());+ let child = module_request_id(&ctx, &format!("{base}/child.js"), 200, CHILD.len());+ let document = ctx.pending_events.iter()+ .find(|e| e.method == "Network.responseReceived" && e.params["type"] == "Document")+ .unwrap().params["requestId"].as_str().unwrap().to_string();+ let page = &mut ctx.pages[0];+ assert!(page.get_response_body(&document).is_some());+ assert!(page.get_response_body(&root).is_none());+ assert_eq!(page.get_response_body(&child).unwrap().body, CHILD);+ page.suspend_js();+ assert!(page.get_response_body(&document).is_none(), "older page cache entry must be evicted");+ assert!(page.get_response_body(&root).is_none(), "evicted runtime entry must not reappear");+ assert_eq!(page.get_response_body(&child).map(|b| b.body), Some(CHILD.to_string()));}#[tokio::test(flavor = "current_thread")]@@ -283,4 +333,8 @@ async fn module_body_buffer_preserves_non_utf8_bytes() {assert_eq!(stored, json!({"body": base64::engine::general_purpose::STANDARD.encode(BODY), "base64Encoded": true,}));+ ctx.pages[0].suspend_js();+ let retained = ctx.pages[0].get_response_body(&id).unwrap();+ assert!(retained.base64_encoded);+ assert_eq!(base64::engine::general_purpose::STANDARD.decode(retained.body).unwrap(), BODY);}diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex b7c574ab..31ea4df2 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -1371,6 +1371,15 @@ impl ObscuraJsRuntime {.cloned()}+ /// Move retained bodies in completion order before the runtime is suspended.+ pub fn take_network_response_bodies(&self) -> Vec<(String, StoredNetworkResponseBody)> {+ let mut state = self.state.borrow_mut();+ let order = std::mem::take(&mut state.network_response_body_order);+ order.into_iter().filter_map(|id| {+ state.network_response_bodies.remove(&id).map(|body| (id, body))+ }).collect()+ }+pub fn clear_network_response_bodies(&self) {let mut state = self.state.borrow_mut();state.network_response_bodies.clear();diff --git a/docs/Use-with-Puppeteer.md b/docs/Use-with-Puppeteer.mdindex 24f56d02..19ab48a7 100644--- a/docs/Use-with-Puppeteer.md+++ b/docs/Use-with-Puppeteer.md@@ -180,3 +180,9 @@ second navigation. Request IDs and loader IDs match the driving session, so`Network.getResponseBody` uses the same ID. Fetch interception control and thenavigation lifecycle keep their existing owner; sessions on other targets donot receive this traffic.++ES-module roots, dependencies, and dynamic imports report their actual HTTP+status, response headers, and body size. Retained bodies remain readable by+request ID when a page's JavaScript runtime is suspended, subject to+`OBSCURA_NETWORK_BODY_BUFFER_ENTRIES` and `OBSCURA_NETWORK_BODY_BUFFER_BYTES`.+Cached imports do not produce another network request.