Files
browser/src/main_snapshot_creator.zig
T
Adrià Arrufat c55afc1df9 cli: add --locale and --timezone, derive language signals from one value
navigator.language was hard-coded to en-US and Accept-Language was a
constant, while Intl, toLocaleString and Date followed the host process
environment. On a de_DE host a page saw navigator.language === "en-US"
next to German number formatting, a mismatch fingerprinting scripts look
for, and the same page rendered differently across machines.

Follow Chrome's --lang rule: one configured tag drives navigator.language(s),
the Accept-Language header and ICU's default locale. --locale defaults to
en-US, so Intl is now en-US on every host instead of whatever LANG says.
--timezone sets the IANA zone Date and Intl use; absent, the host zone stays.

Both are applied by writing LC_ALL and TZ before V8 initializes ICU, which
reads them lazily. Platform.init is the first call in App.init, before any
thread exists, so setenv is safe there.

CDP Emulation.setUserAgentOverride.acceptLanguage, which Playwright sends
for its locale option, now overrides the header and navigator.languages
for the browser context's lifetime, mirroring the user agent override, and
applies even when the Mozilla user agent is refused.

Emulation.setLocaleOverride and setTimezoneOverride stay no-ops: changing
ICU's defaults at runtime needs new zig-v8-fork bindings.
2026-09-09 17:49:02 +02:00

63 lines
2.2 KiB
Zig

// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
pub fn main(init: std.process.Init) !void {
// usage: snapshot_creator [--v8-flags-unsafe "<flags>"] [outfile]
// A snapshot only deserializes correctly under the flags it was created
// with, so a runtime using --v8-flags-unsafe needs a snapshot built with
// the same value.
var v8_flags: ?[]const u8 = null;
var out_path: ?[]const u8 = null;
var args = std.process.Args.Iterator.init(init.minimal.args);
_ = args.next(); // executable name
while (args.next()) |arg| {
if (std.mem.eql(u8, arg, "--v8-flags-unsafe")) {
v8_flags = args.next() orelse {
std.debug.print("--v8-flags-unsafe requires a value\n", .{});
return error.MissingArgument;
};
} else {
out_path = arg;
}
}
var platform = try lp.js.Platform.init(.{ .v8_flags = v8_flags });
defer platform.deinit();
const snapshot = try lp.js.Snapshot.create();
defer snapshot.deinit();
var is_stdout = true;
var file = std.Io.File.stdout();
if (out_path) |n| {
is_stdout = false;
file = try std.Io.Dir.cwd().createFile(lp.io, n, .{});
}
defer if (!is_stdout) {
file.close(lp.io);
};
var buffer: [4096]u8 = undefined;
var writer = file.writerStreaming(lp.io, &buffer);
try snapshot.write(&writer.interface);
try writer.end();
}