From dc1456f4e836cbcfc3645e511aa8429c6baeaae4 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Fri, 7 Jun 2024 15:59:57 +0200 Subject: [PATCH] Handle CDP messages with different order The 'method' still needs to be the first or the second key (in this case after the 'id'). Signed-off-by: Francis Bouvier --- src/cdp/browser.zig | 32 +++++---- src/cdp/cdp.zig | 156 +++++++++++++++++++----------------------- src/cdp/emulation.zig | 25 ++++--- src/cdp/log.zig | 16 +++-- src/cdp/network.zig | 16 +++-- src/cdp/page.zig | 140 ++++++++++++++++++++++++++----------- src/cdp/runtime.zig | 41 +++++------ src/cdp/target.zig | 40 +++++------ 8 files changed, 255 insertions(+), 211 deletions(-) diff --git a/src/cdp/browser.zig b/src/cdp/browser.zig index c0dff5d8..0d8dcd8a 100644 --- a/src/cdp/browser.zig +++ b/src/cdp/browser.zig @@ -4,7 +4,7 @@ const server = @import("../server.zig"); const Ctx = server.Cmd; const cdp = @import("cdp.zig"); const result = cdp.result; -const getParams = cdp.getParams; +const getMsg = cdp.getMsg; const BrowserMethods = enum { getVersion, @@ -15,7 +15,7 @@ const BrowserMethods = enum { pub fn browser( alloc: std.mem.Allocator, - id: u64, + id: ?u16, action: []const u8, scanner: *std.json.Scanner, ctx: *Ctx, @@ -38,10 +38,12 @@ const JsVersion = "12.4.254.8"; fn browserGetVersion( alloc: std.mem.Allocator, - id: u64, - _: *std.json.Scanner, + id: ?u16, + scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { + const msg = try getMsg(alloc, void, scanner); + const Res = struct { protocolVersion: []const u8, product: []const u8, @@ -57,12 +59,12 @@ fn browserGetVersion( .userAgent = UserAgent, .jsVersion = JsVersion, }; - return result(alloc, id, Res, res, null); + return result(alloc, id orelse msg.id.?, Res, res, null); } fn browserSetDownloadBehavior( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { @@ -72,15 +74,15 @@ fn browserSetDownloadBehavior( downloadPath: ?[]const u8 = null, eventsEnabled: ?bool = null, }; - _ = try getParams(alloc, Params, scanner); - return result(alloc, id, null, null, null); + const msg = try getMsg(alloc, Params, scanner); + return result(alloc, id orelse msg.id.?, null, null, null); } const DevToolsWindowID = 1923710101; fn getWindowForTarget( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { @@ -89,8 +91,8 @@ fn getWindowForTarget( const Params = struct { targetId: ?[]const u8 = null, }; - const content = try cdp.getContent(alloc, ?Params, scanner); - std.debug.assert(content.sessionID != null); + const msg = try cdp.getMsg(alloc, ?Params, scanner); + std.debug.assert(msg.sessionID != null); // output const Resp = struct { @@ -103,16 +105,16 @@ fn getWindowForTarget( windowState: []const u8 = "normal", } = .{}, }; - return result(alloc, id, Resp, Resp{}, content.sessionID.?); + return result(alloc, id orelse msg.id.?, Resp, Resp{}, msg.sessionID.?); } fn setWindowBounds( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { // NOTE: noop - const content = try cdp.getContent(alloc, void, scanner); - return result(alloc, id, null, null, content.sessionID); + const msg = try cdp.getMsg(alloc, void, scanner); + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 32a303e3..7853a804 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -53,26 +53,22 @@ pub fn do( // handle 2 possible orders: // - id, method <...> // - method, id <...> - var id_key = (try scanner.next()).string; - var id_token = try scanner.next(); var method_key = (try scanner.next()).string; - var method_token = try scanner.next(); + var method_token: std.json.Token = undefined; + var id: ?u16 = null; // check swap order - if (!std.mem.eql(u8, id_key, "id")) { - const swap_key = method_key; - const swap_token = method_token; - method_key = id_key; - method_token = id_token; - id_key = swap_key; - id_token = swap_token; + if (std.mem.eql(u8, method_key, "id")) { + id = try getId(&scanner, method_key); + method_key = (try scanner.next()).string; + method_token = try scanner.next(); + } else { + method_token = try scanner.next(); } - try checkKey(id_key, "id"); try checkKey(method_key, "method"); - // retrieve id and method - const id = try std.fmt.parseUnsigned(u64, id_token.number, 10); + // retrieve method const method_name = method_token.string; - std.log.debug("cmd: id {any}, method {s}", .{ id, method_name }); + std.log.debug("cmd: method {s}, id {any}", .{ method_name, id }); // retrieve domain from method var iter = std.mem.splitScalar(u8, method_name, '.'); @@ -128,7 +124,7 @@ const resultNullSession = "{{\"id\": {d}, \"result\": {{}}, \"sessionId\": \"{s} // caller owns the slice returned pub fn result( alloc: std.mem.Allocator, - id: u64, + id: u16, comptime T: ?type, res: anytype, sessionID: ?[]const u8, @@ -142,7 +138,7 @@ pub fn result( } const Resp = struct { - id: u64, + id: u16, result: T.?, sessionId: ?[]const u8, }; @@ -171,108 +167,94 @@ pub fn sendEvent( try server.sendSync(ctx, event_msg); } -pub fn getParams( +fn getParams( alloc: std.mem.Allocator, comptime T: type, scanner: *std.json.Scanner, + key: []const u8, ) !?T { - // if next token is the end of the object, there is no "params" - const t = try scanner.next(); - if (t == .object_end) return null; + // check key key is "params" + if (!std.mem.eql(u8, "params", key)) return null; - // if next token is not "params" there is no "params" - if (!std.mem.eql(u8, "params", t.string)) return null; + // skip "params" if not requested + if (T == void) { + var finished: usize = 0; + while (true) { + switch (try scanner.next()) { + .object_begin => finished += 1, + .object_end => finished -= 1, + else => continue, + } + if (finished == 0) break; + } + return void{}; + } // parse "params" const options = std.json.ParseOptions{ .max_value_len = scanner.input.len, .allocate = .alloc_if_needed, }; - const params = try std.json.innerParse(T, alloc, scanner, options); - return params; + return try std.json.innerParse(T, alloc, scanner, options); } -pub fn getSessionID(scanner: *std.json.Scanner) !?[]const u8 { +fn getId(scanner: *std.json.Scanner, key: []const u8) !?u16 { - // if next token is the end of the object, there is no "sessionId" - const t = try scanner.next(); - if (t == .object_end) return null; + // check key is "id" + if (!std.mem.eql(u8, "id", key)) return null; - var n = t.string; + // parse "id" + return try std.fmt.parseUnsigned(u16, (try scanner.next()).number, 10); +} - // if next token is "params" ignore them - // NOTE: will panic if it's not an empty "params" object - // TODO: maybe we should return a custom error here - if (std.mem.eql(u8, n, "params")) { - // ignore empty params - _ = (try scanner.next()).object_begin; - _ = (try scanner.next()).object_end; - n = (try scanner.next()).string; - } +fn getSessionId(scanner: *std.json.Scanner, key: []const u8) !?[]const u8 { - // if next token is not "sessionId" there is no "sessionId" - if (!std.mem.eql(u8, n, "sessionId")) return null; + // check key is "sessionId" + if (!std.mem.eql(u8, "sessionId", key)) return null; // parse "sessionId" return (try scanner.next()).string; } -pub fn getContent( +pub fn getMsg( alloc: std.mem.Allocator, - comptime T: type, + comptime params_T: type, scanner: *std.json.Scanner, -) !struct { params: T, sessionID: ?[]const u8 } { - - // if next token is the end of the object, error - const t = try scanner.next(); - if (t == .object_end) return error.CDPNoContent; - - var params: T = undefined; +) !struct { id: ?u16, params: ?params_T, sessionID: ?[]const u8 } { + var id: ?u16 = null; + var params: ?params_T = null; var sessionID: ?[]const u8 = null; - var n = t.string; + var t: std.json.Token = undefined; - // params - if (std.mem.eql(u8, n, "params")) { - if (T == void) { - - // ignore params - var finished: usize = 0; - while (true) { - switch (try scanner.next()) { - .object_begin => finished += 1, - .object_end => finished -= 1, - else => continue, - } - if (finished == 0) break; - } - params = void{}; - } else { - - // parse "params" - const options = std.json.ParseOptions{ - .max_value_len = scanner.input.len, - .allocate = .alloc_if_needed, - }; - params = try std.json.innerParse(T, alloc, scanner, options); + while (true) { + t = try scanner.next(); + if (t == .object_end) break; + if (t != .string) { + return error.CDPMsgWrong; + } + if (id == null) { + id = try getId(scanner, t.string); + if (id != null) continue; + } + if (params == null) { + params = try getParams(alloc, params_T, scanner, t.string); + if (params != null) continue; + } + if (sessionID == null) { + sessionID = try getSessionId(scanner, t.string); } - - // go next - n = (try scanner.next()).string; - } else { - params = switch (@typeInfo(T)) { - .Void => void{}, - .Optional => null, - else => return error.CDPNoParams, - }; } - if (std.mem.eql(u8, n, "sessionId")) { - sessionID = (try scanner.next()).string; - } - - return .{ .params = params, .sessionID = sessionID }; + // end + std.log.debug( + "id {any}, params {any}, sessionID: {any}, token {any}", + .{ id, params, sessionID, t }, + ); + t = try scanner.next(); + if (t != .end_of_document) return error.CDPMsgEnd; + return .{ .id = id, .params = params, .sessionID = sessionID }; } // Common diff --git a/src/cdp/emulation.zig b/src/cdp/emulation.zig index f7daff4f..2eda18e8 100644 --- a/src/cdp/emulation.zig +++ b/src/cdp/emulation.zig @@ -4,7 +4,7 @@ const server = @import("../server.zig"); const Ctx = server.Cmd; const cdp = @import("cdp.zig"); const result = cdp.result; -const getParams = cdp.getParams; +const getMsg = cdp.getMsg; const stringify = cdp.stringify; const EmulationMethods = enum { @@ -15,7 +15,7 @@ const EmulationMethods = enum { pub fn emulation( alloc: std.mem.Allocator, - id: u64, + id: ?u16, action: []const u8, scanner: *std.json.Scanner, ctx: *Ctx, @@ -36,26 +36,26 @@ const MediaFeature = struct { fn setEmulatedMedia( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { + // input const Params = struct { media: ?[]const u8 = null, features: ?[]MediaFeature = null, }; - _ = try getParams(alloc, Params, scanner); - const sessionID = try cdp.getSessionID(scanner); + const msg = try getMsg(alloc, Params, scanner); // output // TODO: dummy - return result(alloc, id, null, null, sessionID); + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } fn setFocusEmulationEnabled( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { @@ -64,24 +64,23 @@ fn setFocusEmulationEnabled( const Params = struct { enabled: bool, }; - _ = try getParams(alloc, Params, scanner); - const sessionID = try cdp.getSessionID(scanner); + const msg = try getMsg(alloc, Params, scanner); // output // TODO: dummy - return result(alloc, id, null, null, sessionID); + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } fn setDeviceMetricsOverride( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { // input - const content = try cdp.getContent(alloc, void, scanner); + const msg = try cdp.getMsg(alloc, void, scanner); // output - return result(alloc, id, null, null, content.sessionID); + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } diff --git a/src/cdp/log.zig b/src/cdp/log.zig index 4bbcb140..b8ef7cff 100644 --- a/src/cdp/log.zig +++ b/src/cdp/log.zig @@ -4,7 +4,7 @@ const server = @import("../server.zig"); const Ctx = server.Cmd; const cdp = @import("cdp.zig"); const result = cdp.result; -const getParams = cdp.getParams; +const getMsg = cdp.getMsg; const stringify = cdp.stringify; const LogMethods = enum { @@ -13,24 +13,26 @@ const LogMethods = enum { pub fn log( alloc: std.mem.Allocator, - id: u64, + id: ?u16, action: []const u8, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { const method = std.meta.stringToEnum(LogMethods, action) orelse return error.UnknownMethod; + return switch (method) { - .enable => enable(alloc, id, scanner, ctx), + .enable => logEnable(alloc, id, scanner, ctx), }; } -fn enable( +fn logEnable( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - const sessionID = try cdp.getSessionID(scanner); - return result(alloc, id, null, null, sessionID); + const msg = try getMsg(alloc, void, scanner); + + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } diff --git a/src/cdp/network.zig b/src/cdp/network.zig index ebc21cca..830c090c 100644 --- a/src/cdp/network.zig +++ b/src/cdp/network.zig @@ -4,7 +4,7 @@ const server = @import("../server.zig"); const Ctx = server.Cmd; const cdp = @import("cdp.zig"); const result = cdp.result; -const getParams = cdp.getParams; +const getMsg = cdp.getMsg; const stringify = cdp.stringify; const NetworkMethods = enum { @@ -13,24 +13,26 @@ const NetworkMethods = enum { pub fn network( alloc: std.mem.Allocator, - id: u64, + id: ?u16, action: []const u8, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { const method = std.meta.stringToEnum(NetworkMethods, action) orelse return error.UnknownMethod; + return switch (method) { - .enable => enable(alloc, id, scanner, ctx), + .enable => networkEnable(alloc, id, scanner, ctx), }; } -fn enable( +fn networkEnable( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - const sessionID = try cdp.getSessionID(scanner); - return result(alloc, id, null, null, sessionID); + const msg = try getMsg(alloc, void, scanner); + + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } diff --git a/src/cdp/page.zig b/src/cdp/page.zig index 60ec80e7..2c1eddf1 100644 --- a/src/cdp/page.zig +++ b/src/cdp/page.zig @@ -4,7 +4,7 @@ const server = @import("../server.zig"); const Ctx = server.Cmd; const cdp = @import("cdp.zig"); const result = cdp.result; -const getParams = cdp.getParams; +const getMsg = cdp.getMsg; const stringify = cdp.stringify; const sendEvent = cdp.sendEvent; @@ -21,7 +21,7 @@ const PageMethods = enum { pub fn page( alloc: std.mem.Allocator, - id: u64, + id: ?u16, action: []const u8, scanner: *std.json.Scanner, ctx: *Ctx, @@ -40,12 +40,12 @@ pub fn page( fn enable( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - const sessionID = try cdp.getSessionID(scanner); - return result(alloc, id, null, null, sessionID); + const msg = try getMsg(alloc, void, scanner); + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } const Frame = struct { @@ -65,11 +65,12 @@ const Frame = struct { fn getFrameTree( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { - const sessionID = try cdp.getSessionID(scanner); + const msg = try cdp.getMsg(alloc, void, scanner); + const FrameTree = struct { frameTree: struct { frame: Frame, @@ -87,12 +88,12 @@ fn getFrameTree( }, }, }; - return result(alloc, id, FrameTree, frameTree, sessionID); + return result(alloc, id orelse msg.id.?, FrameTree, frameTree, msg.sessionID); } fn setLifecycleEventsEnabled( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { @@ -101,13 +102,12 @@ fn setLifecycleEventsEnabled( const Params = struct { enabled: bool, }; - _ = try getParams(alloc, Params, scanner); - const sessionID = try cdp.getSessionID(scanner); + const msg = try getMsg(alloc, Params, scanner); ctx.state.page_life_cycle_events = true; // output - return result(alloc, id, null, null, sessionID); + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } const LifecycleEvent = struct { @@ -119,7 +119,7 @@ const LifecycleEvent = struct { fn addScriptToEvaluateOnNewDocument( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { @@ -131,37 +131,34 @@ fn addScriptToEvaluateOnNewDocument( includeCommandLineAPI: bool = false, runImmediately: bool = false, }; - _ = try getParams(alloc, Params, scanner); - const sessionID = try cdp.getSessionID(scanner); + const msg = try getMsg(alloc, Params, scanner); // output const Res = struct { identifier: []const u8 = "1", }; - return result(alloc, id, Res, Res{}, sessionID); + return result(alloc, id orelse msg.id.?, Res, Res{}, msg.sessionID); } fn createIsolatedWorld( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - - // input - const content = try cdp.getContent(alloc, void, scanner); + const msg = try getMsg(alloc, void, scanner); // output const Resp = struct { executionContextId: u8 = 2, }; - return result(alloc, id, Resp, .{}, content.sessionID); + return result(alloc, id orelse msg.id.?, Resp, .{}, msg.sessionID); } fn navigate( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { @@ -174,12 +171,12 @@ fn navigate( frameId: ?[]const u8 = null, referrerPolicy: ?[]const u8 = null, // TODO: enum }; - const input = try cdp.getContent(alloc, Params, scanner); - const sessionID = input.sessionID; - std.debug.assert(sessionID != null); + const msg = try getMsg(alloc, Params, scanner); + std.debug.assert(msg.sessionID != null); + const params = msg.params.?; // change state - ctx.state.url = input.params.url; + ctx.state.url = params.url; ctx.state.loaderID = "AF8667A203C5392DBE9AC290044AA4C2"; var life_event = LifecycleEvent{ @@ -193,11 +190,25 @@ fn navigate( frameId: []const u8, }; const frame_started_loading = FrameStartedLoading{ .frameId = ctx.state.frameID }; - try sendEvent(alloc, ctx, "Page.frameStartedLoading", FrameStartedLoading, frame_started_loading, sessionID); + try sendEvent( + alloc, + ctx, + "Page.frameStartedLoading", + FrameStartedLoading, + frame_started_loading, + msg.sessionID, + ); if (ctx.state.page_life_cycle_events) { life_event.name = "init"; life_event.timestamp = 343721.796037; - try sendEvent(alloc, ctx, "Page.lifecycleEvent", LifecycleEvent, life_event, sessionID); + try sendEvent( + alloc, + ctx, + "Page.lifecycleEvent", + LifecycleEvent, + life_event, + msg.sessionID, + ); } // output @@ -210,16 +221,16 @@ fn navigate( .frameId = ctx.state.frameID, .loaderId = ctx.state.loaderID, }; - const res = try result(alloc, id, Resp, resp, sessionID); + const res = try result(alloc, id orelse msg.id.?, Resp, resp, msg.sessionID); std.log.debug("res {s}", .{res}); try server.sendSync(ctx, res); // Runtime.executionContextsCleared event - try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, sessionID); + try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, msg.sessionID); // launch navigate var p = try ctx.browser.currentSession().createPage(); - _ = try p.navigate(input.params.url); + _ = try p.navigate(params.url); // frameNavigated event const FrameNavigated = struct { @@ -235,11 +246,25 @@ fn navigate( .loaderId = ctx.state.loaderID, }, }; - try sendEvent(alloc, ctx, "Page.frameNavigated", FrameNavigated, frame_navigated, sessionID); + try sendEvent( + alloc, + ctx, + "Page.frameNavigated", + FrameNavigated, + frame_navigated, + msg.sessionID, + ); if (ctx.state.page_life_cycle_events) { life_event.name = "load"; life_event.timestamp = 343721.824655; - try sendEvent(alloc, ctx, "Page.lifecycleEvent", LifecycleEvent, life_event, sessionID); + try sendEvent( + alloc, + ctx, + "Page.lifecycleEvent", + LifecycleEvent, + life_event, + msg.sessionID, + ); } try Runtime.executionContextCreated( @@ -250,7 +275,7 @@ fn navigate( "", "7102379147004877974.3265385113993241162", .{ .frameId = ctx.state.frameID }, - sessionID, + msg.sessionID, ); try Runtime.executionContextCreated( @@ -261,30 +286,65 @@ fn navigate( "__playwright_utility_world__", "-4572718120346458707.6016875269626438350", .{ .isDefault = false, .type = "isolated", .frameId = ctx.state.frameID }, - sessionID, + msg.sessionID, ); // domContentEventFired event ts_event = .{ .timestamp = 343721.803338 }; - try sendEvent(alloc, ctx, "Page.domContentEventFired", cdp.TimestampEvent, ts_event, sessionID); + try sendEvent( + alloc, + ctx, + "Page.domContentEventFired", + cdp.TimestampEvent, + ts_event, + msg.sessionID, + ); if (ctx.state.page_life_cycle_events) { life_event.name = "DOMContentLoaded"; life_event.timestamp = 343721.803338; - try sendEvent(alloc, ctx, "Page.lifecycleEvent", LifecycleEvent, life_event, sessionID); + try sendEvent( + alloc, + ctx, + "Page.lifecycleEvent", + LifecycleEvent, + life_event, + msg.sessionID, + ); } // loadEventFired event ts_event = .{ .timestamp = 343721.824655 }; - try sendEvent(alloc, ctx, "Page.loadEventFired", cdp.TimestampEvent, ts_event, sessionID); + try sendEvent( + alloc, + ctx, + "Page.loadEventFired", + cdp.TimestampEvent, + ts_event, + msg.sessionID, + ); if (ctx.state.page_life_cycle_events) { life_event.name = "load"; life_event.timestamp = 343721.824655; - try sendEvent(alloc, ctx, "Page.lifecycleEvent", LifecycleEvent, life_event, sessionID); + try sendEvent( + alloc, + ctx, + "Page.lifecycleEvent", + LifecycleEvent, + life_event, + msg.sessionID, + ); } // frameStoppedLoading const FrameStoppedLoading = struct { frameId: []const u8 }; - try sendEvent(alloc, ctx, "Page.frameStoppedLoading", FrameStoppedLoading, .{ .frameId = ctx.state.frameID }, sessionID); + try sendEvent( + alloc, + ctx, + "Page.frameStoppedLoading", + FrameStoppedLoading, + .{ .frameId = ctx.state.frameID }, + msg.sessionID, + ); return ""; } diff --git a/src/cdp/runtime.zig b/src/cdp/runtime.zig index 857a1f72..bb071db1 100644 --- a/src/cdp/runtime.zig +++ b/src/cdp/runtime.zig @@ -6,7 +6,7 @@ const server = @import("../server.zig"); const Ctx = server.Cmd; const cdp = @import("cdp.zig"); const result = cdp.result; -const getParams = cdp.getParams; +const getMsg = cdp.getMsg; const stringify = cdp.stringify; const RuntimeMethods = enum { @@ -17,7 +17,7 @@ const RuntimeMethods = enum { pub fn runtime( alloc: std.mem.Allocator, - id: u64, + id: ?u16, action: []const u8, scanner: *std.json.Scanner, ctx: *Ctx, @@ -33,14 +33,13 @@ pub fn runtime( fn enable( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, - ctx: *Ctx, + _: *Ctx, ) ![]const u8 { - _ = ctx; // input - const sessionID = try cdp.getSessionID(scanner); + const msg = try getMsg(alloc, void, scanner); // output // const uniqueID = "1367118932354479079.-1471398151593995849"; @@ -56,7 +55,7 @@ fn enable( // std.log.debug("res {s}", .{mainCtx}); // try server.sendAsync(ctx, mainCtx); - return result(alloc, id, null, null, sessionID); + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } pub const AuxData = struct { @@ -76,7 +75,7 @@ const ExecutionContextDescription = struct { pub fn executionContextCreated( alloc: std.mem.Allocator, ctx: *Ctx, - id: u64, + id: u16, origin: []const u8, name: []const u8, uniqueID: []const u8, @@ -100,17 +99,18 @@ pub fn executionContextCreated( fn runIfWaitingForDebugger( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - const sessionID = try cdp.getSessionID(scanner); - return result(alloc, id, null, null, sessionID); + const msg = try getMsg(alloc, void, scanner); + + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } fn evaluate( alloc: std.mem.Allocator, - id: u64, + _id: ?u16, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { @@ -124,20 +124,21 @@ fn evaluate( contextId: ?u8, }; - const input = try cdp.getContent(alloc, Params, scanner); - const sessionID = input.sessionID; - std.debug.assert(sessionID != null); + const msg = try getMsg(alloc, Params, scanner); + std.debug.assert(msg.sessionID != null); + const params = msg.params.?; + const id = _id orelse msg.id.?; // save script in file at debug mode - std.log.debug("script {d} length: {d}", .{ id, input.params.expression.len }); + std.log.debug("script {d} length: {d}", .{ id, params.expression.len }); if (std.log.defaultLogEnabled(.debug)) { const name = try std.fmt.allocPrint(alloc, "id_{d}.js", .{id}); defer alloc.free(name); const dir = try std.fs.cwd().makeOpenPath("zig-cache/tmp", .{}); const f = try dir.createFile(name, .{}); defer f.close(); - const nb = try f.write(input.params.expression); - std.debug.assert(nb == input.params.expression.len); + const nb = try f.write(params.expression); + std.debug.assert(nb == params.expression.len); const p = try dir.realpathAlloc(alloc, name); defer alloc.free(p); std.log.debug("Script {d} saved at {s}", .{ id, p }); @@ -149,7 +150,7 @@ fn evaluate( // const page_alloc = ctx.browser.currentSession().page.?.arena.allocator(); const session_alloc = ctx.browser.currentSession().alloc; var res = jsruntime.JSResult{}; - try ctx.browser.currentSession().env.run(session_alloc, input.params.expression, "cdp", &res, null); + try ctx.browser.currentSession().env.run(session_alloc, params.expression, "cdp", &res, null); defer res.deinit(session_alloc); if (!res.success) { @@ -168,5 +169,5 @@ fn evaluate( description: []const u8 = "UtilityScript", objectId: []const u8 = "7481631759780215274.3.2", }; - return result(alloc, id, Resp, Resp{}, sessionID); + return result(alloc, id, Resp, Resp{}, msg.sessionID); } diff --git a/src/cdp/target.zig b/src/cdp/target.zig index 67fd6474..2c165c9c 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -4,7 +4,7 @@ const server = @import("../server.zig"); const Ctx = server.Cmd; const cdp = @import("cdp.zig"); const result = cdp.result; -const getParams = cdp.getParams; +const getMsg = cdp.getMsg; const stringify = cdp.stringify; const TargetMethods = enum { @@ -16,7 +16,7 @@ const TargetMethods = enum { pub fn target( alloc: std.mem.Allocator, - id: u64, + id: ?u16, action: []const u8, scanner: *std.json.Scanner, ctx: *Ctx, @@ -56,7 +56,7 @@ const TargetFilter = struct { fn tagetSetAutoAttach( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { @@ -66,12 +66,10 @@ fn tagetSetAutoAttach( flatten: bool = true, filter: ?[]TargetFilter = null, }; - const params = try getParams(alloc, Params, scanner); - std.log.debug("params {any}", .{params}); + const msg = try getMsg(alloc, Params, scanner); + std.log.debug("params {any}", .{msg.params}); - const sessionID = try cdp.getSessionID(scanner); - - if (sessionID == null) { + if (msg.sessionID == null) { const attached = AttachToTarget{ .sessionId = cdp.SessionID, .targetInfo = .{ @@ -84,12 +82,12 @@ fn tagetSetAutoAttach( try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, null); } - return result(alloc, id, null, null, sessionID); + return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } fn tagetGetTargetInfo( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { @@ -98,7 +96,7 @@ fn tagetGetTargetInfo( const Params = struct { targetId: ?[]const u8 = null, }; - _ = try getParams(alloc, Params, scanner); + const msg = try getMsg(alloc, Params, scanner); // output const TargetInfo = struct { @@ -117,7 +115,7 @@ fn tagetGetTargetInfo( .targetId = BrowserTargetID, .type = "browser", }; - return result(alloc, id, TargetInfo, targetInfo, null); + return result(alloc, id orelse msg.id.?, TargetInfo, targetInfo, null); } const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89"; @@ -125,9 +123,9 @@ const ContextSessionID = "4FDC2CB760A23A220497A05C95417CF4"; fn createBrowserContext( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, - _: *Ctx, + ctx: *Ctx, ) ![]const u8 { // input @@ -137,21 +135,20 @@ fn createBrowserContext( proxyBypassList: ?[]const u8 = null, originsWithUniversalNetworkAccess: ?[][]const u8 = null, }; - _ = try getParams(alloc, Params, scanner); - const sessionID = try cdp.getSessionID(scanner); + const msg = try getMsg(alloc, Params, scanner); // output const Resp = struct { browserContextId: []const u8 = ContextID, }; - return result(alloc, id, Resp, Resp{}, sessionID); + return result(alloc, id orelse msg.id.?, Resp, Resp{}, msg.sessionID); } const TargetID = "57356548460A8F29706A2ADF14316298"; fn createTarget( alloc: std.mem.Allocator, - id: u64, + id: ?u16, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { @@ -167,8 +164,7 @@ fn createTarget( background: bool = false, forTab: ?bool = null, }; - _ = try getParams(alloc, Params, scanner); - const sessionID = try cdp.getSessionID(scanner); + const msg = try getMsg(alloc, Params, scanner); // change CDP state ctx.state.frameID = TargetID; @@ -188,11 +184,11 @@ fn createTarget( }, .waitingForDebugger = true, }; - try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, sessionID); + try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, msg.sessionID); // output const Resp = struct { targetId: []const u8 = TargetID, }; - return result(alloc, id, Resp, Resp{}, sessionID); + return result(alloc, id orelse msg.id.?, Resp, Resp{}, msg.sessionID); }