Add a dumpFile utility function

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-06-17 16:34:47 +02:00
parent 409969621d
commit 4d756b5bfc
2 changed files with 18 additions and 10 deletions

View File

@@ -108,6 +108,23 @@ pub const State = struct {
// Utils // 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 { 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;
} }

View File

@@ -134,16 +134,7 @@ fn evaluate(
// save script in file at debug mode // save script in file at debug mode
std.log.debug("script {d} length: {d}", .{ id, params.expression.len }); std.log.debug("script {d} length: {d}", .{ id, params.expression.len });
if (std.log.defaultLogEnabled(.debug)) { if (std.log.defaultLogEnabled(.debug)) {
const name = try std.fmt.allocPrint(alloc, "id_{d}.js", .{id}); try cdp.dumpFile(alloc, id, params.expression);
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 });
} }
// evaluate the script in the context of the current page // evaluate the script in the context of the current page