Handle sessionId in result

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-04-17 14:18:18 +02:00
parent 21afa1f4b3
commit 4d8cdc6dc8
8 changed files with 67 additions and 58 deletions

View File

@@ -52,7 +52,7 @@ fn browserGetVersion(
.userAgent = UserAgent, .userAgent = UserAgent,
.jsVersion = JsVersion, .jsVersion = JsVersion,
}; };
return result(alloc, id, Res, res); return result(alloc, id, Res, res, null);
} }
fn browserSetDownloadBehavior( fn browserSetDownloadBehavior(
@@ -69,5 +69,5 @@ fn browserSetDownloadBehavior(
}; };
const params = try getParams(alloc, Params, scanner); const params = try getParams(alloc, Params, scanner);
std.log.debug("params {any}", .{params}); std.log.debug("params {any}", .{params});
return result(alloc, id, null, null); return result(alloc, id, null, null, null);
} }

View File

@@ -78,8 +78,6 @@ fn checkKey(key: []const u8, token: []const u8) !void {
if (!std.mem.eql(u8, key, token)) return error.WrongToken; if (!std.mem.eql(u8, key, token)) return error.WrongToken;
} }
const resultNull = "{{\"id\": {d}, \"result\": {{}}}}";
// caller owns the slice returned // caller owns the slice returned
pub fn stringify(alloc: std.mem.Allocator, res: anytype) ![]const u8 { pub fn stringify(alloc: std.mem.Allocator, res: anytype) ![]const u8 {
var out = std.ArrayList(u8).init(alloc); var out = std.ArrayList(u8).init(alloc);
@@ -94,20 +92,31 @@ pub fn stringify(alloc: std.mem.Allocator, res: anytype) ![]const u8 {
return ret; return ret;
} }
const resultNull = "{{\"id\": {d}, \"result\": {{}}}}";
const resultNullSession = "{{\"id\": {d}, \"result\": {{}}, \"sessionId\": \"{s}\"}}";
// caller owns the slice returned // caller owns the slice returned
pub fn result( pub fn result(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
id: u64, id: u64,
comptime T: ?type, comptime T: ?type,
res: anytype, res: anytype,
sessionID: ?[]const u8,
) ![]const u8 { ) ![]const u8 {
if (T == null) return try std.fmt.allocPrint(alloc, resultNull, .{id}); if (T == null) {
// No need to stringify a custom JSON msg, just use string templates
if (sessionID) |sID| {
return try std.fmt.allocPrint(alloc, resultNullSession, .{ id, sID });
}
return try std.fmt.allocPrint(alloc, resultNull, .{id});
}
const Resp = struct { const Resp = struct {
id: u64, id: u64,
result: T.?, result: T.?,
sessionId: ?[]const u8,
}; };
const resp = Resp{ .id = id, .result = res }; const resp = Resp{ .id = id, .result = res, .sessionId = sessionID };
return stringify(alloc, resp); return stringify(alloc, resp);
} }
@@ -125,32 +134,32 @@ pub fn getParams(
return std.json.innerParse(T, alloc, scanner, options); return std.json.innerParse(T, alloc, scanner, options);
} }
pub fn getSessionID( pub fn getSessionID(scanner: *std.json.Scanner) !?[]const u8 {
alloc: std.mem.Allocator,
scanner: *std.json.Scanner, // if next token is the end of the object, there is no "sessionId"
) ![]const u8 { const t = try scanner.next();
var n = (try scanner.next()).string; if (t == .object_end) return null;
var n = t.string;
// 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")) { if (std.mem.eql(u8, n, "params")) {
// ignore empty params // ignore empty params
_ = (try scanner.next()).object_begin; _ = (try scanner.next()).object_begin;
_ = (try scanner.next()).object_end; _ = (try scanner.next()).object_end;
n = (try scanner.next()).string; n = (try scanner.next()).string;
} }
try checkKey("sessionId", n);
const options = std.json.ParseOptions{ // if next token is not "sessionId" there is no "sessionId"
.max_value_len = scanner.input.len, if (!std.mem.eql(u8, n, "sessionId")) return null;
.allocate = .alloc_if_needed,
}; // parse "sessionId"
return std.json.innerParse([]const u8, alloc, scanner, options); return (try scanner.next()).string;
} }
// Common // Common
// ------ // ------
pub const SessionID = "9559320D92474062597D9875C664CAC0"; pub const SessionID = "9559320D92474062597D9875C664CAC0";
pub const SessionIDResp = struct {
id: u64,
result: struct {} = .{},
sessionId: []const u8,
};

View File

@@ -32,7 +32,7 @@ fn setEmulatedMedia(
_: *std.json.Scanner, _: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
return result(alloc, id, null, null); return result(alloc, id, null, null, null);
} }
fn setFocusEmulationEnabled( fn setFocusEmulationEnabled(
@@ -41,5 +41,5 @@ fn setFocusEmulationEnabled(
_: *std.json.Scanner, _: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
return result(alloc, id, null, null); return result(alloc, id, null, null, null);
} }

View File

@@ -31,8 +31,6 @@ fn enable(
scanner: *std.json.Scanner, scanner: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
return stringify(alloc, cdp.SessionIDResp{ const sessionID = try cdp.getSessionID(scanner);
.id = id, return result(alloc, id, null, null, sessionID);
.sessionId = try cdp.getSessionID(alloc, scanner),
});
} }

View File

@@ -31,8 +31,6 @@ fn enable(
scanner: *std.json.Scanner, scanner: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
return stringify(alloc, cdp.SessionIDResp{ const sessionID = try cdp.getSessionID(scanner);
.id = id, return result(alloc, id, null, null, sessionID);
.sessionId = try cdp.getSessionID(alloc, scanner),
});
} }

View File

@@ -37,10 +37,8 @@ fn enable(
scanner: *std.json.Scanner, scanner: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
return stringify(alloc, cdp.SessionIDResp{ const sessionID = try cdp.getSessionID(scanner);
.id = id, return result(alloc, id, null, null, sessionID);
.sessionId = try cdp.getSessionID(alloc, scanner),
});
} }
fn getFrameTree( fn getFrameTree(
@@ -50,17 +48,26 @@ fn getFrameTree(
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
// TODO: dummy // TODO: dummy
return result(alloc, id, null, null); return result(alloc, id, null, null, null);
} }
fn setLifecycleEventsEnabled( fn setLifecycleEventsEnabled(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
id: u64, id: u64,
_: *std.json.Scanner, scanner: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
// input
const Params = struct {
enabled: bool,
};
_ = try getParams(alloc, Params, scanner);
const sessionID = try cdp.getSessionID(scanner);
// output
// TODO: dummy // TODO: dummy
return result(alloc, id, null, null); return result(alloc, id, null, null, sessionID);
} }
fn addScriptToEvaluateOnNewDocument( fn addScriptToEvaluateOnNewDocument(
@@ -69,20 +76,18 @@ fn addScriptToEvaluateOnNewDocument(
scanner: *std.json.Scanner, scanner: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
// input
const Params = struct { const Params = struct {
source: []const u8, source: []const u8,
worldName: ?[]const u8 = null, worldName: ?[]const u8 = null,
}; };
_ = try getParams(alloc, Params, scanner); _ = try getParams(alloc, Params, scanner);
const sessionID = try cdp.getSessionID(scanner);
// output
const Res = struct { const Res = struct {
id: u64, identifier: []const u8 = "1",
result: struct {
identifier: []const u8 = "1",
} = .{},
sessionId: []const u8,
}; };
return stringify(alloc, Res{ return result(alloc, id, Res, Res{}, sessionID);
.id = id,
.sessionId = try cdp.getSessionID(alloc, scanner),
});
} }

View File

@@ -33,10 +33,8 @@ fn enable(
scanner: *std.json.Scanner, scanner: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
return stringify(alloc, cdp.SessionIDResp{ const sessionID = try cdp.getSessionID(scanner);
.id = id, return result(alloc, id, null, null, sessionID);
.sessionId = try cdp.getSessionID(alloc, scanner),
});
} }
fn runIfWaitingForDebugger( fn runIfWaitingForDebugger(
@@ -45,5 +43,5 @@ fn runIfWaitingForDebugger(
_: *std.json.Scanner, _: *std.json.Scanner,
_: *Ctx, _: *Ctx,
) ![]const u8 { ) ![]const u8 {
return result(alloc, id, null, null); return result(alloc, id, null, null, null);
} }

View File

@@ -2,9 +2,10 @@ const std = @import("std");
const server = @import("../server.zig"); const server = @import("../server.zig");
const Ctx = server.Cmd; const Ctx = server.Cmd;
const result = @import("cdp.zig").result; const cdp = @import("cdp.zig");
const getParams = @import("cdp.zig").getParams; const result = cdp.result;
const stringify = @import("cdp.zig").stringify; const getParams = cdp.getParams;
const stringify = cdp.stringify;
const TargetMethods = enum { const TargetMethods = enum {
setAutoAttach, setAutoAttach,
@@ -63,7 +64,7 @@ fn tagetSetAutoAttach(
const attached = try stringify(alloc, AttachToTarget{}); const attached = try stringify(alloc, AttachToTarget{});
try server.sendSync(ctx, attached); try server.sendSync(ctx, attached);
return result(alloc, id, null, null); return result(alloc, id, null, null, null);
} }
fn tagetGetTargetInfo( fn tagetGetTargetInfo(