Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: emit worker network requests

Ppsoft1991e3ec937f7886
diff --git a/src/browser/tests/cdp/worker_network.html b/src/browser/tests/cdp/worker_network.html
new file mode 100644
index 000000000..0863bd183
--- /dev/null
+++ b/src/browser/tests/cdp/worker_network.html
@@ -0,0 +1,4 @@
+<!doctype html>
+<script>
+ new Worker("./worker_network.js");
+</script>
diff --git a/src/browser/tests/cdp/worker_network.js b/src/browser/tests/cdp/worker_network.js
new file mode 100644
index 000000000..432ab3761
--- /dev/null
+++ b/src/browser/tests/cdp/worker_network.js
@@ -0,0 +1 @@
+fetch("/echo_method");
diff --git a/src/cdp/domains/network.zig b/src/cdp/domains/network.zig
index 597eb92b2..744c8856a 100644
--- a/src/cdp/domains/network.zig
+++ b/src/cdp/domains/network.zig
@@ -345,7 +345,16 @@ pub fn httpRequestStart(bc: *CDP.BrowserContext, msg: *const Notification.Reques
const transfer = msg.transfer;
const req = &transfer.req;
const frame_id = req.frame_id;
- const frame = bc.session.findFrameByFrameId(frame_id) orelse return;
+ // Worker requests use their WorkerGlobalScope id rather than a document
+ // frame id. They still belong to this page session and must be visible to
+ // Network clients; otherwise responseReceived/loadingFinished are emitted
+ // without the corresponding requestWillBeSent event and CDP clients drop
+ // the whole request. A worker has no document URL, so use its request URL
+ // when the id does not resolve to a document frame.
+ const document_url = if (bc.session.findFrameByFrameId(frame_id)) |frame|
+ frame.url
+ else
+ req.url;
// Modify request with extra CDP headers. Use set (replace by name) so a
// caller-supplied header overrides a built-in default of the same name
@@ -360,7 +369,7 @@ pub fn httpRequestStart(bc: *CDP.BrowserContext, msg: *const Notification.Reques
.requestId = &id.toRequestId(transfer),
.loaderId = &id.toLoaderId(req.loader_id),
.type = req.resource_type.string(),
- .documentURL = frame.url,
+ .documentURL = document_url,
.request = RequestWriter.init(transfer),
.initiator = .{ .type = "other" },
.redirectHasExtraInfo = false, // TODO change after adding Network.requestWillBeSentExtraInfo
@@ -1052,3 +1061,57 @@ test "cdp.Network: setBlockedURLs blocks requests with inspector reason" {
}, .{ .session_id = "SID-BLOCK" });
try testing.expectEqual(error.UrlBlocked, error_context.err.?);
}
+
+test "cdp.Network: worker requests emit network events" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ const cdp = ctx.cdp();
+ _ = try cdp.createBrowserContext();
+ var bc = &cdp.browser_context.?;
+ bc.id = "BID-NW";
+ bc.session_id = "SID-NW";
+ bc.target_id = "TID-NW-0000000".*;
+
+ try ctx.processMessage(.{ .id = 1, .method = "Network.enable" });
+ try ctx.expectSentResult(null, .{ .id = 1 });
+
+ const fixture_root = "http://127.0.0.1:9582/src/browser/tests/cdp/";
+ const worker_url = fixture_root ++ "worker_network.js";
+ const api_url = "http://127.0.0.1:9582/echo_method";
+ const page = try bc.session.createPage();
+ try page.navigate(fixture_root ++ "worker_network.html", .{});
+ try testing.waitForPage(bc);
+
+ try ctx.expectSentEvent("Network.requestWillBeSent", .{
+ .documentURL = worker_url,
+ .type = "Script",
+ .request = .{
+ .url = worker_url,
+ .initialPriority = "High",
+ .referrerPolicy = "strict-origin-when-cross-origin",
+ },
+ }, .{ .session_id = "SID-NW" });
+ try ctx.expectSentEvent("Network.responseReceived", .{
+ .type = "Script",
+ .response = .{
+ .url = worker_url,
+ .connectionReused = false,
+ .connectionId = 0,
+ .securityState = "unknown",
+ .timing = .{
+ .workerStart = -1,
+ .workerReady = -1,
+ .workerFetchStart = -1,
+ .workerRespondWithSettled = -1,
+ .pushStart = -1,
+ .pushEnd = -1,
+ },
+ },
+ }, .{ .session_id = "SID-NW" });
+ try ctx.expectSentEvent("Network.requestWillBeSent", .{
+ .documentURL = api_url,
+ .type = "Fetch",
+ .request = .{ .url = api_url },
+ }, .{ .session_id = "SID-NW" });
+}