Skip to content

The headless browser for AI agents and web scraping

Merge main and preserve wasm streaming regressions

SGavrl3361fe10e019
diff --cc crates/obscura-js/src/runtime.rs
index 0d15814b,84592db2..2a4cc746
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@@ -20590,67 -20958,88 +20958,149 @@@ mod tests
assert_eq!(result.value.unwrap(), serde_json::json!(true));
}
+ // Native compileStreaming reached deno_core's wasm streaming callback with
+ // no JS handler registered: a panic inside a V8 callback, which aborted
+ // the whole process.
+ #[tokio::test(flavor = "current_thread")]
+ async fn test_wasm_compile_streaming_uses_response_array_buffer() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .call_function_on_for_cdp(
+ r#"async () => {
+ const bytes = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]);
+ const module = await WebAssembly.compileStreaming(
+ Promise.resolve(new Response(bytes)),
+ );
+ return module instanceof WebAssembly.Module;
+ }"#,
+ None,
+ &[],
+ true,
+ true,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(result.value.unwrap(), serde_json::json!(true));
+ }
+
+ #[tokio::test(flavor = "current_thread")]
+ async fn test_wasm_streaming_rejects_a_non_response_source() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .call_function_on_for_cdp(
+ r#"async () => {
+ const bytes = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]);
+ const names = [];
+ for (const call of [
+ () => WebAssembly.compileStreaming(Promise.resolve(bytes)),
+ () => WebAssembly.instantiateStreaming(Promise.resolve(bytes), {}),
+ ]) {
+ try {
+ await call();
+ names.push("resolved");
+ } catch (error) {
+ names.push(error.name);
+ }
+ }
+ return names;
+ }"#,
+ None,
+ &[],
+ true,
+ true,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ result.value.unwrap(),
+ serde_json::json!(["TypeError", "TypeError"])
+ );
+ }
+
+ // body.offsetParent returned body itself, so walking offsetParent to
+ // the top (how pages compute an element's page position) looped forever.
+ #[test]
+ fn test_offset_parent_of_body_and_root_is_null() {
+ let mut rt = setup_runtime(
+ "<html><body><div style=\"position:relative\"><p id=\"p\">x</p></div></body></html>",
+ );
+ let result = rt
+ .evaluate(
+ r#"(() => {
+ const walk = [];
+ for (let el = document.getElementById("p"); el && walk.length < 10; el = el.offsetParent) {
+ walk.push(el.tagName);
+ }
+ return {
+ body: document.body.offsetParent,
+ root: document.documentElement.offsetParent,
+ walk: walk.join(">"),
+ };
+ })()"#,
+ )
+ .unwrap();
+ assert_eq!(
+ result,
+ serde_json::json!({"body": null, "root": null, "walk": "P>DIV>BODY"})
+ );
+ }
+
+ #[test]
+ fn style_set_property_converts_value_once_before_comparing_and_storing() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt.evaluate(r#"(() => {
+ let conversions = 0;
+ document.body.style.setProperty('top', {
+ toString() { return `${++conversions * 10}px`; }
+ });
+ return {conversions:String(conversions), value:document.body.style.top};
+ })()"#).unwrap();
+ assert_eq!(result, serde_json::json!({"conversions":"1", "value":"10px"}));
+ }
+
+ // An unchanged style.setProperty rewrote the style attribute and queued a
+ // mutation record, so an observer that re-applies the same style (pinning
+ // body `top: 0`) re-triggered itself forever.
+ #[tokio::test(flavor = "current_thread")]
+ async fn test_style_set_property_without_change_queues_no_mutation() {
+ let mut rt = setup_runtime("<html><body><p>x</p></body></html>");
+ let result = rt
+ .call_function_on_for_cdp(
+ r#"async () => {
+ const body = document.body;
+ let calls = 0, records = 0;
+ const observer = new MutationObserver((mutations) => {
+ calls++;
+ records += mutations.length;
+ if (calls < 50) body.style.setProperty("top", "0px", "important");
+ });
+ observer.observe(document.documentElement, {
+ subtree: true, attributes: true, attributeFilter: ["style"],
+ });
+ body.style.setProperty("top", "0px", "important");
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ body.style.removeProperty("left");
+ body.style.top = "0px";
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ observer.disconnect();
+ return { calls, records, style: body.getAttribute("style") };
+ }"#,
+ None,
+ &[],
+ true,
+ true,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ result.value.unwrap(),
+ serde_json::json!({"calls": 1, "records": 1, "style": "top: 0px;"})
+ );
+ }
+
#[test]
fn test_text_decoder_respects_typed_array_view() {
let mut rt = setup_runtime("<html><body></body></html>");