Skip to content

The headless browser for AI agents and web scraping

fix(js): never reach the native wasm streaming entry points

Florian Lepratb23d89a71931
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 5ed990db..a2cbedee 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -7640,19 +7640,26 @@ function _arrayBufferFromBytes(bytes) {
function _installWasmStreamingFallback() {
if (typeof WebAssembly === 'undefined') return;
if (WebAssembly.instantiateStreaming && WebAssembly.instantiateStreaming.__obscuraFallback) return;
- const nativeInstantiateStreaming = WebAssembly.instantiateStreaming;
- const fallback = async function instantiateStreaming(source, imports) {
+ // The native streaming entry points call deno_core's isolate-wide wasm
+ // streaming callback, which panics (and, inside a V8 callback, aborts the
+ // process) because no JS handler is registered. Never reach them: read the
+ // Response body and compile it, and reject a non-Response source with a
+ // TypeError as browsers do.
+ const responseBytes = async (source, name) => {
const response = await source;
- if (response && typeof response.arrayBuffer === 'function') {
- return WebAssembly.instantiate(await response.arrayBuffer(), imports);
- }
- if (typeof nativeInstantiateStreaming === 'function') {
- return nativeInstantiateStreaming.call(WebAssembly, response, imports);
- }
- return WebAssembly.instantiate(response, imports);
+ if (response && typeof response.arrayBuffer === 'function') return response.arrayBuffer();
+ throw new TypeError(`Failed to execute '${name}' on 'WebAssembly': An argument must be provided, which must be a Response or Promise<Response> object.`);
+ };
+ const instantiateStreaming = async function instantiateStreaming(source, imports) {
+ return WebAssembly.instantiate(await responseBytes(source, 'instantiateStreaming'), imports);
+ };
+ const compileStreaming = async function compileStreaming(source) {
+ return WebAssembly.compile(await responseBytes(source, 'compileStreaming'));
};
- fallback.__obscuraFallback = true;
- WebAssembly.instantiateStreaming = fallback;
+ instantiateStreaming.__obscuraFallback = true;
+ compileStreaming.__obscuraFallback = true;
+ WebAssembly.instantiateStreaming = instantiateStreaming;
+ WebAssembly.compileStreaming = compileStreaming;
}
_installWasmStreamingFallback();
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 7193f86e..0d15814b 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -20590,6 +20590,67 @@ 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"])
+ );
+ }
+
#[test]
fn test_text_decoder_respects_typed_array_view() {
let mut rt = setup_runtime("<html><body></body></html>");