1const PLATFORMS: Partial<Record<NodeJS.Platform, string>> = {
2 darwin: "macOS",
3 linux: "Linux",
4 win32: "Windows",
5};
6
7/** HTTP, navigator and client hints describe the same bundled Chromium version. */
8export function browserChromeIdentity(
9 nativeUserAgent: string,
10 host: {
11 readonly platform: NodeJS.Platform;
12 readonly arch: string;
13 readonly chromeVersion?: string;
14 },
15) {
16 const version = /(?:Headless)?Chrome\/(\d[\d.]*)/.exec(nativeUserAgent);
17 const fullVersion = host.chromeVersion ?? version?.[1];
18 const major = fullVersion?.split(".")[0];
19 if (!version || !fullVersion || !major) return null;
20
21 const brands = [
22 { brand: "Chromium", version: major },
23 { brand: "Google Chrome", version: major },
24 { brand: "Not A(Brand", version: "99" },
25 ];
26 return {
27 userAgent: nativeUserAgent
28 .replace(/\s+(?:Electron|T3Code[^\s/]*)\/[^\s]+/gi, "")
29 .replace(version[0], `Chrome/${major}.0.0.0`),
30 userAgentMetadata: {
31 brands,
32 fullVersionList: brands.map(({ brand, version }) => ({
33 brand,
34 version: version === major ? fullVersion : `${version}.0.0.0`,
35 })),
36 platform: PLATFORMS[host.platform] ?? "Unknown",
37 platformVersion: "",
38 architecture: host.arch.startsWith("arm") ? "arm" : "x86",
39 bitness: host.arch.endsWith("64") ? "64" : "32",
40 model: "",
41 mobile: false,
42 wow64: false,
43 },
44 };
45}