Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(workers): expose the native WebSocketError interface

ldm017181208237c
diff --git a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/metadata.rs b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/metadata.rs
index 054194130..10fbcd0f4 100644
--- a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/metadata.rs
+++ b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/metadata.rs
@@ -43,6 +43,7 @@ pub(in crate::context_bootstrap) const WORKER_SHARED_INTERFACE_NAMES: &[&str] =
"WebTransportBidirectionalStream",
"WebTransportDatagramDuplexStream",
"WebSocket",
+ "WebSocketError",
"AbortSignal",
"AbortController",
"Headers",
diff --git a/moli-renderer-v8/src/worker/thread/tests/network/websockets.rs b/moli-renderer-v8/src/worker/thread/tests/network/websockets.rs
index fca62a9fb..da8aeaf2f 100644
--- a/moli-renderer-v8/src/worker/thread/tests/network/websockets.rs
+++ b/moli-renderer-v8/src/worker/thread/tests/network/websockets.rs
@@ -1,6 +1,61 @@
// Tests grouped by behavior. Shared fixtures live in the parent module.
use super::*;
+#[tokio::test]
+async fn worker_websocket_error_uses_native_dom_exception_bindings() {
+ ensure_v8();
+ for kind in [WorkerScriptKind::Classic, WorkerScriptKind::Module] {
+ let source = r#"
+ const NativeError = WebSocketError;
+ const defaults = new NativeError();
+ const full = new NativeError('message', {closeCode: 3456, reason: 'reason'});
+ const reasonOnly = new NativeError('', {reason: 'specified'});
+ const getter = Object.getOwnPropertyDescriptor(NativeError.prototype, 'closeCode').get;
+ const outcome = callback => {
+ try {callback(); return 'accepted';}
+ catch (error) {return [error.name, error instanceof DOMException, error instanceof TypeError];}
+ };
+ postMessage({
+ defaults: [defaults.name, defaults.message, defaults.code, defaults.closeCode, defaults.reason],
+ full: [full.message, full.closeCode, full.reason],
+ reasonOnly: [reasonOnly.closeCode, reasonOnly.reason],
+ inheritance: [full instanceof NativeError, full instanceof DOMException, full instanceof Error,
+ Object.getPrototypeOf(NativeError.prototype) === DOMException.prototype],
+ identity: [WebSocketError === NativeError, getter.call(full),
+ new DOMException('message', 'WebSocketError') instanceof NativeError],
+ invalidCode: outcome(() => new NativeError('', {closeCode: 999})),
+ invalidReason: outcome(() => new NativeError('', {reason: '🔌'.repeat(32)})),
+ forged: outcome(() => getter.call(Object.create(NativeError.prototype))),
+ proxy: outcome(() => getter.call(new Proxy(full, {}))),
+ });
+ close();
+ "#;
+ let mut handle = spawn_worker_with_request_client_and_kind(
+ source.to_owned(),
+ "https://worker-websocket-error.test/worker.js".to_owned(),
+ worker_test_request_client(),
+ kind,
+ );
+ let actual: serde_json::Value =
+ serde_json::from_str(&recv_post_json(&mut handle).await).unwrap();
+ assert_eq!(
+ actual,
+ serde_json::json!({
+ "defaults": ["WebSocketError", "", 0, null, ""],
+ "full": ["message", 3456, "reason"],
+ "reasonOnly": [1000, "specified"],
+ "inheritance": [true, true, true, true],
+ "identity": [true, 3456, false],
+ "invalidCode": ["InvalidAccessError", true, false],
+ "invalidReason": ["SyntaxError", true, false],
+ "forged": ["TypeError", false, true],
+ "proxy": ["TypeError", false, true],
+ }),
+ "{kind:?}",
+ );
+ }
+}
+
#[tokio::test]
async fn worker_classic_websocket_echo_round_trips_on_worker_loop() {
ensure_v8();