Skip to content

Commit

Permalink
browser: write a cache file of failed fetched js
Browse files Browse the repository at this point in the history
  • Loading branch information
krichprollsch committed Mar 8, 2024
1 parent a81e10f commit 0e08533
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ zig-out
/vendor/netsurf/lib/
/vendor/netsurf/include/
/vendor/libiconv/
/*.cache
27 changes: 27 additions & 0 deletions src/browser/browser.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");

const Types = @import("root").Types;

Expand Down Expand Up @@ -430,6 +431,13 @@ pub const Page = struct {
if (res.success) {
log.debug("eval remote {s}: {s}", .{ src, res.result });
} else {
// In debug mode only, save the file in a temp file.
if (comptime builtin.mode == .Debug) {
writeCache(alloc, u.path, fetchres.body.?) catch |e| {
log.debug("cache: {any}", .{e});
};
}

log.info("eval remote {s}: {s}", .{ src, res.result });
return FetchError.JsErr;
}
Expand All @@ -448,3 +456,22 @@ pub const Page = struct {
return false;
}
};

fn writeCache(alloc: std.mem.Allocator, name: []const u8, data: []const u8) !void {
const fname = try std.mem.concat(alloc, u8, &.{ name, ".cache" });
defer alloc.free(fname);

// clear invalid char.
for (fname, 0..) |c, i| {
if (!std.ascii.isPrint(c) or std.ascii.isWhitespace(c) or c == '/') {
fname[i] = '_';
}
}

log.debug("cache {s}", .{fname});

const f = try std.fs.cwd().createFile(fname, .{ .read = false, .truncate = true });
defer f.close();

try f.writeAll(data);
}

0 comments on commit 0e08533

Please sign in to comment.