cdp: refacto message JSON read

This commit is contained in:
Pierre Tachoire
2024-10-29 09:49:36 +01:00
parent 1854074f64
commit 82c37fc71b
11 changed files with 431 additions and 409 deletions

View File

@@ -25,7 +25,7 @@ const server = @import("../server.zig");
const Ctx = server.Ctx;
const cdp = @import("cdp.zig");
const result = cdp.result;
const getMsg = cdp.getMsg;
const IncomingMessage = @import("msg.zig").IncomingMessage;
const stringify = cdp.stringify;
const log = std.log.scoped(.cdp);
@@ -41,27 +41,23 @@ const Methods = enum {
pub fn runtime(
alloc: std.mem.Allocator,
id: ?u16,
msg: *IncomingMessage,
action: []const u8,
scanner: *std.json.Scanner,
s: []const u8,
ctx: *Ctx,
) ![]const u8 {
const method = std.meta.stringToEnum(Methods, action) orelse
// NOTE: we could send it anyway to the JS runtime but it's good to check it
return error.UnknownMethod;
return switch (method) {
.runIfWaitingForDebugger => runIfWaitingForDebugger(alloc, id, scanner, ctx),
else => sendInspector(alloc, method, id, s, scanner, ctx),
.runIfWaitingForDebugger => runIfWaitingForDebugger(alloc, msg, ctx),
else => sendInspector(alloc, method, msg, ctx),
};
}
fn sendInspector(
alloc: std.mem.Allocator,
method: Methods,
_id: ?u16,
s: []const u8,
scanner: *std.json.Scanner,
msg: *IncomingMessage,
ctx: *Ctx,
) ![]const u8 {
@@ -69,8 +65,8 @@ fn sendInspector(
if (std.log.defaultLogEnabled(.debug)) {
// input
var script: ?[]const u8 = null;
var id: u16 = undefined;
var script: ?[]const u8 = null;
if (method == .evaluate) {
const Params = struct {
@@ -81,11 +77,10 @@ fn sendInspector(
userGesture: ?bool = null,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s} (script saved on cache)", .{ msg.id, "runtime.evaluate" });
const params = msg.params.?;
script = params.expression;
id = msg.id;
const input = try msg.getInput(alloc, Params);
log.debug("Req > id {d}, method {s} (script saved on cache)", .{ input.id, "runtime.evaluate" });
script = input.params.expression;
id = input.id;
} else if (method == .callFunctionOn) {
const Params = struct {
functionDeclaration: []const u8,
@@ -100,11 +95,10 @@ fn sendInspector(
userGesture: ?bool = null,
};
const msg = try getMsg(alloc, _id, Params, scanner);
log.debug("Req > id {d}, method {s} (script saved on cache)", .{ msg.id, "runtime.callFunctionOn" });
const params = msg.params.?;
script = params.functionDeclaration;
id = msg.id;
const input = try msg.getInput(alloc, Params);
log.debug("Req > id {d}, method {s} (script saved on cache)", .{ input.id, "runtime.callFunctionOn" });
script = input.params.functionDeclaration;
id = input.id;
}
if (script) |src| {
@@ -115,12 +109,12 @@ fn sendInspector(
// remove awaitPromise true params
// TODO: delete when Promise are correctly handled by zig-js-runtime
if (method == .callFunctionOn or method == .evaluate) {
const buf = try alloc.alloc(u8, s.len + 1);
const buf = try alloc.alloc(u8, msg.json.len + 1);
defer alloc.free(buf);
_ = std.mem.replace(u8, s, "\"awaitPromise\":true", "\"awaitPromise\":false", buf);
_ = std.mem.replace(u8, msg.json, "\"awaitPromise\":true", "\"awaitPromise\":false", buf);
ctx.sendInspector(buf);
} else {
ctx.sendInspector(s);
ctx.sendInspector(msg.json);
}
return "";
}
@@ -166,14 +160,11 @@ pub fn executionContextCreated(
// should we be passing this also to the JS Inspector?
fn runIfWaitingForDebugger(
alloc: std.mem.Allocator,
_id: ?u16,
scanner: *std.json.Scanner,
msg: *IncomingMessage,
_: *Ctx,
) ![]const u8 {
const input = try msg.getInput(alloc, void);
log.debug("Req > id {d}, method {s}", .{ input.id, "runtime.runIfWaitingForDebugger" });
// input
const msg = try getMsg(alloc, _id, void, scanner);
log.debug("Req > id {d}, method {s}", .{ msg.id, "runtime.runIfWaitingForDebugger" });
return result(alloc, msg.id, null, null, msg.sessionID);
return result(alloc, input.id, null, null, input.sessionId);
}