diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index d8647a4a..89f1bf3e 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -108,6 +108,23 @@ pub const State = struct { // Utils // ----- +pub fn dumpFile( + alloc: std.mem.Allocator, + id: u16, + script: []const u8, +) !void { + 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(script); + std.debug.assert(nb == script.len); + const p = try dir.realpathAlloc(alloc, name); + defer alloc.free(p); + std.log.debug("Script {d} saved at {s}", .{ id, p }); +} + fn checkKey(key: []const u8, token: []const u8) !void { if (!std.mem.eql(u8, key, token)) return error.WrongToken; } diff --git a/src/cdp/runtime.zig b/src/cdp/runtime.zig index 985b2484..ab8afdea 100644 --- a/src/cdp/runtime.zig +++ b/src/cdp/runtime.zig @@ -134,16 +134,7 @@ fn evaluate( // save script in file at debug mode 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(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 }); + try cdp.dumpFile(alloc, id, params.expression); } // evaluate the script in the context of the current page