Skip to content

Commit 4d756b5

Browse files
Add a dumpFile utility function
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 4099696 commit 4d756b5

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/cdp/cdp.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ pub const State = struct {
108108
// Utils
109109
// -----
110110

111+
pub fn dumpFile(
112+
alloc: std.mem.Allocator,
113+
id: u16,
114+
script: []const u8,
115+
) !void {
116+
const name = try std.fmt.allocPrint(alloc, "id_{d}.js", .{id});
117+
defer alloc.free(name);
118+
const dir = try std.fs.cwd().makeOpenPath("zig-cache/tmp", .{});
119+
const f = try dir.createFile(name, .{});
120+
defer f.close();
121+
const nb = try f.write(script);
122+
std.debug.assert(nb == script.len);
123+
const p = try dir.realpathAlloc(alloc, name);
124+
defer alloc.free(p);
125+
std.log.debug("Script {d} saved at {s}", .{ id, p });
126+
}
127+
111128
fn checkKey(key: []const u8, token: []const u8) !void {
112129
if (!std.mem.eql(u8, key, token)) return error.WrongToken;
113130
}

src/cdp/runtime.zig

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,7 @@ fn evaluate(
134134
// save script in file at debug mode
135135
std.log.debug("script {d} length: {d}", .{ id, params.expression.len });
136136
if (std.log.defaultLogEnabled(.debug)) {
137-
const name = try std.fmt.allocPrint(alloc, "id_{d}.js", .{id});
138-
defer alloc.free(name);
139-
const dir = try std.fs.cwd().makeOpenPath("zig-cache/tmp", .{});
140-
const f = try dir.createFile(name, .{});
141-
defer f.close();
142-
const nb = try f.write(params.expression);
143-
std.debug.assert(nb == params.expression.len);
144-
const p = try dir.realpathAlloc(alloc, name);
145-
defer alloc.free(p);
146-
std.log.debug("Script {d} saved at {s}", .{ id, p });
137+
try cdp.dumpFile(alloc, id, params.expression);
147138
}
148139

149140
// evaluate the script in the context of the current page

0 commit comments

Comments
 (0)