camoucore 0.3.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/LICENSE +4 -3
  2. package/dist/camoufox/__main__.d.ts +10 -0
  3. package/dist/camoufox/__main__.js +1175 -0
  4. package/dist/camoufox/__version__.d.ts +31 -0
  5. package/dist/camoufox/__version__.js +32 -0
  6. package/dist/camoufox/_humanize_playwright.d.ts +54 -0
  7. package/dist/camoufox/_humanize_playwright.js +429 -0
  8. package/dist/camoufox/addons.d.ts +21 -0
  9. package/dist/camoufox/addons.js +56 -0
  10. package/dist/camoufox/async_api.d.ts +9 -0
  11. package/dist/camoufox/async_api.js +9 -0
  12. package/dist/camoufox/browser-pin.d.ts +46 -0
  13. package/dist/camoufox/browser-pin.js +96 -0
  14. package/dist/camoufox/cli.d.ts +7 -0
  15. package/dist/camoufox/cli.js +20 -0
  16. package/dist/camoufox/coherence.d.ts +141 -0
  17. package/dist/camoufox/coherence.js +708 -0
  18. package/dist/camoufox/data-files/browser-pin.json +7 -0
  19. package/dist/camoufox/data-files/essential-fonts.json +5 -0
  20. package/dist/camoufox/data-files/fingerprint-presets-v150.json +14544 -0
  21. package/dist/camoufox/data-files/fingerprint-presets.json +5192 -0
  22. package/dist/camoufox/data-files/font-bases.json +1887 -0
  23. package/dist/camoufox/data-files/font-groups.json +716 -0
  24. package/dist/camoufox/data-files/fonts.json +240 -0
  25. package/dist/camoufox/data-files/fpgen.yml +44 -0
  26. package/dist/camoufox/data-files/launcher-constants.json +66 -0
  27. package/dist/camoufox/data-files/media-devices.json +148 -0
  28. package/dist/camoufox/data-files/repos.yml +40 -0
  29. package/dist/camoufox/data-files/voice-manifests.json +1864 -0
  30. package/dist/camoufox/data-files/voice-uris.json +188 -0
  31. package/dist/camoufox/data-files/warnings.yml +132 -0
  32. package/dist/camoufox/display_profile.d.ts +17 -0
  33. package/dist/camoufox/display_profile.js +72 -0
  34. package/dist/camoufox/driver_defaults.d.ts +25 -0
  35. package/dist/camoufox/driver_defaults.js +114 -0
  36. package/dist/camoufox/exceptions.d.ts +78 -0
  37. package/dist/camoufox/exceptions.js +135 -0
  38. package/dist/camoufox/fingerprints.d.ts +341 -0
  39. package/dist/camoufox/fingerprints.js +1682 -0
  40. package/dist/camoufox/gpudisplay.d.ts +158 -0
  41. package/dist/camoufox/gpudisplay.js +695 -0
  42. package/dist/camoufox/host_rendering.d.ts +38 -0
  43. package/dist/camoufox/host_rendering.js +180 -0
  44. package/dist/camoufox/humanize.d.ts +89 -0
  45. package/dist/camoufox/humanize.js +143 -0
  46. package/dist/camoufox/index.d.ts +21 -0
  47. package/dist/camoufox/index.js +21 -0
  48. package/dist/camoufox/ip.d.ts +45 -0
  49. package/dist/camoufox/ip.js +114 -0
  50. package/dist/camoufox/multiversion.d.ts +131 -0
  51. package/dist/camoufox/multiversion.js +489 -0
  52. package/dist/camoufox/node-floor.d.ts +5 -0
  53. package/dist/camoufox/node-floor.js +17 -0
  54. package/dist/camoufox/paths.d.ts +45 -0
  55. package/dist/camoufox/paths.js +59 -0
  56. package/dist/camoufox/pkgman.d.ts +331 -0
  57. package/dist/camoufox/pkgman.js +972 -0
  58. package/dist/camoufox/product.d.ts +8 -0
  59. package/dist/camoufox/product.js +8 -0
  60. package/dist/camoufox/pycompat.d.ts +97 -0
  61. package/dist/camoufox/pycompat.js +458 -0
  62. package/dist/camoufox/server.d.ts +36 -0
  63. package/dist/camoufox/server.js +82 -0
  64. package/dist/camoufox/sync_api.d.ts +77 -0
  65. package/dist/camoufox/sync_api.js +194 -0
  66. package/dist/camoufox/utils.d.ts +515 -0
  67. package/dist/camoufox/utils.js +1995 -0
  68. package/dist/camoufox/warnings.d.ts +16 -0
  69. package/dist/camoufox/warnings.js +65 -0
  70. package/dist/camoufox/webgl.d.ts +52 -0
  71. package/dist/camoufox/webgl.js +264 -0
  72. package/dist/camouwolf/__main__.d.ts +10 -0
  73. package/dist/camouwolf/__main__.js +1180 -0
  74. package/dist/camouwolf/__version__.d.ts +31 -0
  75. package/dist/camouwolf/__version__.js +32 -0
  76. package/dist/camouwolf/_humanize_playwright.d.ts +53 -0
  77. package/dist/camouwolf/_humanize_playwright.js +427 -0
  78. package/dist/camouwolf/addons.d.ts +42 -0
  79. package/dist/camouwolf/addons.js +113 -0
  80. package/dist/camouwolf/async_api.d.ts +9 -0
  81. package/dist/camouwolf/async_api.js +9 -0
  82. package/dist/camouwolf/browser-pin.d.ts +46 -0
  83. package/dist/camouwolf/browser-pin.js +96 -0
  84. package/dist/camouwolf/cli.d.ts +7 -0
  85. package/dist/camouwolf/cli.js +20 -0
  86. package/dist/camouwolf/clienthints.d.ts +61 -0
  87. package/dist/camouwolf/clienthints.js +176 -0
  88. package/dist/camouwolf/coherence.d.ts +152 -0
  89. package/dist/camouwolf/coherence.js +554 -0
  90. package/dist/camouwolf/data-files/browser-pin.json +7 -0
  91. package/dist/camouwolf/data-files/camouwolf.defaults.json +118 -0
  92. package/dist/camouwolf/data-files/data/bundle-fonts.json +1983 -0
  93. package/dist/camouwolf/data-files/data/chrome-languages.json +517 -0
  94. package/dist/camouwolf/data-files/data/properties.json +321 -0
  95. package/dist/camouwolf/data-files/data/system-colors.json +35 -0
  96. package/dist/camouwolf/data-files/data/ubo.json +6 -0
  97. package/dist/camouwolf/data-files/fpgen.yml +41 -0
  98. package/dist/camouwolf/data-files/launcher-constants.json +26 -0
  99. package/dist/camouwolf/data-files/repos.yml +23 -0
  100. package/dist/camouwolf/data-files/warnings.yml +96 -0
  101. package/dist/camouwolf/driver_defaults.d.ts +25 -0
  102. package/dist/camouwolf/driver_defaults.js +114 -0
  103. package/dist/camouwolf/exceptions.d.ts +95 -0
  104. package/dist/camouwolf/exceptions.js +150 -0
  105. package/dist/camouwolf/fingerprints.d.ts +227 -0
  106. package/dist/camouwolf/fingerprints.js +753 -0
  107. package/dist/camouwolf/gpudisplay.d.ts +171 -0
  108. package/dist/camouwolf/gpudisplay.js +711 -0
  109. package/dist/camouwolf/host_rendering.d.ts +18 -0
  110. package/dist/camouwolf/host_rendering.js +114 -0
  111. package/dist/camouwolf/humanize.d.ts +84 -0
  112. package/dist/camouwolf/humanize.js +128 -0
  113. package/dist/camouwolf/index.d.ts +22 -0
  114. package/dist/camouwolf/index.js +21 -0
  115. package/dist/camouwolf/multiversion.d.ts +131 -0
  116. package/dist/camouwolf/multiversion.js +491 -0
  117. package/dist/camouwolf/node-floor.d.ts +5 -0
  118. package/dist/camouwolf/node-floor.js +17 -0
  119. package/dist/camouwolf/paths.d.ts +34 -0
  120. package/dist/camouwolf/paths.js +58 -0
  121. package/dist/camouwolf/pkgman.d.ts +331 -0
  122. package/dist/camouwolf/pkgman.js +972 -0
  123. package/dist/camouwolf/product.d.ts +3 -0
  124. package/dist/camouwolf/product.js +3 -0
  125. package/dist/camouwolf/pycompat.d.ts +92 -0
  126. package/dist/camouwolf/pycompat.js +461 -0
  127. package/dist/camouwolf/server.d.ts +36 -0
  128. package/dist/camouwolf/server.js +81 -0
  129. package/dist/camouwolf/switches.d.ts +77 -0
  130. package/dist/camouwolf/switches.js +222 -0
  131. package/dist/camouwolf/sync_api.d.ts +84 -0
  132. package/dist/camouwolf/sync_api.js +173 -0
  133. package/dist/camouwolf/utils.d.ts +644 -0
  134. package/dist/camouwolf/utils.js +2057 -0
  135. package/dist/camouwolf/warnings.d.ts +14 -0
  136. package/dist/camouwolf/warnings.js +59 -0
  137. package/dist/camouwolf/webgl.d.ts +41 -0
  138. package/dist/camouwolf/webgl.js +198 -0
  139. package/dist/data.d.ts +1 -1
  140. package/dist/data.js +3 -3
  141. package/dist/download.js +1 -1
  142. package/dist/exceptions.d.ts +1 -1
  143. package/dist/exceptions.js +1 -1
  144. package/dist/geolocation.js +1 -1
  145. package/dist/host.js +1 -1
  146. package/dist/ip.js +1 -1
  147. package/dist/locales.js +1 -1
  148. package/dist/mcp/cli.d.ts +15 -0
  149. package/dist/mcp/cli.js +108 -0
  150. package/dist/mcp/index.d.ts +8 -0
  151. package/dist/mcp/index.js +8 -0
  152. package/dist/mcp/main.d.ts +2 -0
  153. package/dist/mcp/main.js +8 -0
  154. package/dist/mcp/secrets.d.ts +50 -0
  155. package/dist/mcp/secrets.js +193 -0
  156. package/dist/mcp/server.d.ts +48 -0
  157. package/dist/mcp/server.js +653 -0
  158. package/dist/mcp/tree.d.ts +10 -0
  159. package/dist/mcp/tree.js +36 -0
  160. package/dist/passthrough.d.ts +1 -1
  161. package/dist/product.js +1 -1
  162. package/dist/settings.js +1 -1
  163. package/dist/telemetry.d.ts +0 -1
  164. package/dist/telemetry.js +5 -4
  165. package/dist/virtdisplay.d.ts +1 -1
  166. package/dist/virtdisplay.js +1 -1
  167. package/dist/warnings.js +1 -1
  168. package/package.json +57 -13
@@ -0,0 +1,708 @@
1
+ /**
2
+ * Whole-identity coherence: the checks that look at more than one field.
3
+ *
4
+ * TypeScript twin of python/camoucore/src/camoucore/camoufox/coherence.py -- see there for the
5
+ * measurements behind each rule. Camoufox assembles an identity from several
6
+ * independently sampled pools, so a combination no machine has ever had can
7
+ * be built out of individually plausible parts; every identity passes through
8
+ * here, whatever it was built from.
9
+ *
10
+ * `validate()` reports what is still broken; `apply()` repairs what it can.
11
+ */
12
+ import { LAUNCHER_CONSTANTS } from "./paths.js";
13
+ import { comparePyStr, isPlainObject, isPyInt, num, pyRepr, pyStr, pyTruthy, ValueError, } from "./pycompat.js";
14
+ /** Core counts Apple Silicon actually ships. */
15
+ export const APPLE_SILICON_CORES = new Set(LAUNCHER_CONSTANTS.appleSiliconCores);
16
+ /**
17
+ * devicePixelRatio by platform, ascending as in coherence.py: the
18
+ * nearest-step repair keeps the first of two equally near steps, so a tie
19
+ * goes to the lower one.
20
+ */
21
+ export const PLAUSIBLE_DPR = LAUNCHER_CONSTANTS.plausibleDpr;
22
+ /**
23
+ * colorDepth: Firefox reports 24, or 30 on a deep-colour display. macOS
24
+ * defaults to deep colour, so a Mac reports 30, Intel or Apple Silicon.
25
+ */
26
+ export const PLAUSIBLE_COLOR_DEPTH = new Set([24, 30]);
27
+ /** maxTouchPoints: consumer digitisers top out at 10 contacts. */
28
+ export const MAX_PLAUSIBLE_TOUCH_POINTS = 10;
29
+ /** The browser's own chrome height, in CSS pixels (a property of the binary). */
30
+ export const BROWSER_CHROME_HEIGHT = 86;
31
+ /**
32
+ * GPU strings that are not possible on macOS: Firefox 152 has no ANGLE on a
33
+ * Mac. "Intel(R) HD Graphics 400" and "Radeon R9 200 Series" are the buckets
34
+ * an Intel Mac's UHD 630 and Radeon Pro 5300M report.
35
+ */
36
+ const NOT_A_MAC_GPU = ["ANGLE", "llvmpipe"];
37
+ /** navigator.hardwareConcurrency on an Intel Mac, physical or logical. */
38
+ export const INTEL_MAC_IGP_CORES = new Set(LAUNCHER_CONSTANTS.intelMacIgpCores);
39
+ /** A discrete GPU adds the desktops to those, up to the 2019 Mac Pro. */
40
+ export const INTEL_MAC_DGPU_CORES = new Set([
41
+ ...LAUNCHER_CONSTANTS.intelMacIgpCores,
42
+ ...LAUNCHER_CONSTANTS.intelMacDgpuExtraCores,
43
+ ]);
44
+ /** Screens only Apple Silicon Macs have: the notched MacBooks, the 24" iMac. */
45
+ export const APPLE_SILICON_PANELS = new Set(Object.values(LAUNCHER_CONSTANTS.appleSiliconPanels).flatMap((panels) => panels.map(([w, h]) => `${w}x${h}`)));
46
+ function isNone(value) {
47
+ return value === null || value === undefined;
48
+ }
49
+ function renderer(config) {
50
+ const r = config["webGl:renderer"];
51
+ return pyTruthy(r) ? pyStr(r) : "";
52
+ }
53
+ function isAppleSilicon(config) {
54
+ return renderer(config).includes("Apple M");
55
+ }
56
+ function checkAppleSiliconCores(config, _targetOs) {
57
+ if (!isAppleSilicon(config))
58
+ return null;
59
+ const cores = config["navigator.hardwareConcurrency"];
60
+ if (isPyInt(cores) && !APPLE_SILICON_CORES.has(Number(cores))) {
61
+ return `${pyRepr(renderer(config))} with hardwareConcurrency ${pyStr(cores)}; Apple Silicon starts at 8`;
62
+ }
63
+ return null;
64
+ }
65
+ function repairAppleSiliconCores(config, _targetOs) {
66
+ const cores = config["navigator.hardwareConcurrency"];
67
+ if (!isPyInt(cores))
68
+ return;
69
+ const n = Number(cores);
70
+ const sorted = [...APPLE_SILICON_CORES].sort((a, b) => a - b);
71
+ config["navigator.hardwareConcurrency"] =
72
+ sorted.find((c) => c >= n) ?? Math.max(...sorted);
73
+ }
74
+ /**
75
+ * Whether this renderer string is one the OS can report. Used both to check
76
+ * a finished identity and to filter the WebGL pool before sampling.
77
+ */
78
+ export function gpuFitsOs(rendererString, targetOs) {
79
+ const r = pyTruthy(rendererString) ? pyStr(rendererString) : "";
80
+ if (!r)
81
+ return true;
82
+ if (targetOs === "mac")
83
+ return !NOT_A_MAC_GPU.some((bad) => r.includes(bad));
84
+ if (targetOs === "win")
85
+ return r.startsWith("ANGLE");
86
+ if (targetOs === "lin")
87
+ return !r.includes("ANGLE") && !r.includes("Apple M");
88
+ return true;
89
+ }
90
+ function isAppleSiliconPanel(width, height) {
91
+ const w = num(width);
92
+ const h = num(height);
93
+ return (typeof w === "number" &&
94
+ typeof h === "number" &&
95
+ APPLE_SILICON_PANELS.has(`${w}x${h}`));
96
+ }
97
+ /**
98
+ * The hardwareConcurrency values an Intel Mac with this GPU reports, or null
99
+ * when the renderer is not an Intel Mac's (none, or Apple Silicon).
100
+ */
101
+ export function intelMacCores(rendererString) {
102
+ const r = pyTruthy(rendererString) ? pyStr(rendererString) : "";
103
+ if (!r || r.includes("Apple M"))
104
+ return null;
105
+ return r.includes("Intel") ? INTEL_MAC_IGP_CORES : INTEL_MAC_DGPU_CORES;
106
+ }
107
+ /** Why no Intel Mac reports this GPU beside these cores and this screen. */
108
+ function intelMacMisfit(rendererString, cores, width, height) {
109
+ const allowed = intelMacCores(rendererString);
110
+ if (!allowed)
111
+ return null;
112
+ const r = pyStr(rendererString);
113
+ if (isPyInt(cores) && !allowed.has(Number(cores))) {
114
+ return `${pyRepr(r)} with hardwareConcurrency ${pyStr(cores)}; no Intel Mac with that GPU reports it`;
115
+ }
116
+ if (isAppleSiliconPanel(width, height)) {
117
+ return `${pyRepr(r)} behind a ${pyStr(width)}x${pyStr(height)} panel, which only Apple Silicon Macs have`;
118
+ }
119
+ return null;
120
+ }
121
+ /**
122
+ * Whether a machine with this GPU has these cores and this screen. Only Intel
123
+ * Macs are modelled; serves the WebGL draw and the finished-identity check.
124
+ */
125
+ export function gpuFitsMachine(rendererString, targetOs, cores, width, height) {
126
+ return (targetOs !== "mac" ||
127
+ intelMacMisfit(rendererString, cores, width, height) === null);
128
+ }
129
+ function checkIntelMacHardware(config, targetOs) {
130
+ if (targetOs !== "mac")
131
+ return null;
132
+ return intelMacMisfit(renderer(config), config["navigator.hardwareConcurrency"], config["screen.width"], config["screen.height"]);
133
+ }
134
+ function checkGpuMatchesOs(config, targetOs) {
135
+ const r = renderer(config);
136
+ if (!r || gpuFitsOs(r, targetOs))
137
+ return null;
138
+ if (targetOs === "mac")
139
+ return `macOS identity with ${pyRepr(r)}, which no Mac reports`;
140
+ if (targetOs === "win") {
141
+ return `Windows identity with ${pyRepr(r)}; Firefox on Windows renders through ANGLE`;
142
+ }
143
+ return `Linux identity with ${pyRepr(r)}`;
144
+ }
145
+ function inColorDepths(depth) {
146
+ return typeof depth !== "boolean" && PLAUSIBLE_COLOR_DEPTH.has(num(depth));
147
+ }
148
+ function checkColorDepth(config, targetOs) {
149
+ const depth = config["screen.colorDepth"];
150
+ if (isNone(depth))
151
+ return null;
152
+ if (!inColorDepths(depth))
153
+ return `screen.colorDepth ${pyStr(depth)}; Firefox reports 24 or 30`;
154
+ if (targetOs === "mac" && num(depth) !== 30) {
155
+ return `macOS identity with colorDepth ${pyStr(depth)}; deep colour is the macOS default`;
156
+ }
157
+ return null;
158
+ }
159
+ function repairColorDepth(config, targetOs) {
160
+ if (targetOs === "mac")
161
+ config["screen.colorDepth"] = 30;
162
+ else if (!inColorDepths(config["screen.colorDepth"]))
163
+ config["screen.colorDepth"] = 24;
164
+ if (!("screen.pixelDepth" in config))
165
+ config["screen.pixelDepth"] = config["screen.colorDepth"];
166
+ }
167
+ function checkPixelDepth(config, _targetOs) {
168
+ // pixelDepth is the same number in every browser that reports both.
169
+ const pixel = config["screen.pixelDepth"];
170
+ const color = config["screen.colorDepth"] ?? 24;
171
+ if (!isNone(pixel) && num(pixel) !== num(color))
172
+ return `screen.pixelDepth ${pyStr(pixel)} differs from colorDepth ${pyStr(color)}`;
173
+ return null;
174
+ }
175
+ function repairPixelDepth(config, _targetOs) {
176
+ config["screen.pixelDepth"] = config["screen.colorDepth"] ?? 24;
177
+ }
178
+ function checkTouchPoints(config, targetOs) {
179
+ const touch = config["navigator.maxTouchPoints"];
180
+ if (isNone(touch))
181
+ return null;
182
+ if (!isPyInt(touch) ||
183
+ Number(touch) < 0 ||
184
+ Number(touch) > MAX_PLAUSIBLE_TOUCH_POINTS) {
185
+ return `navigator.maxTouchPoints ${pyStr(touch)}; a digitiser reports at most ${MAX_PLAUSIBLE_TOUCH_POINTS}`;
186
+ }
187
+ if (targetOs === "mac" && pyTruthy(touch)) {
188
+ return `macOS identity with maxTouchPoints ${pyStr(touch)}; no Mac has a touchscreen`;
189
+ }
190
+ return null;
191
+ }
192
+ function repairTouchPoints(config, targetOs) {
193
+ const touch = config["navigator.maxTouchPoints"];
194
+ const tooMany = isPyInt(touch) && Number(touch) > MAX_PLAUSIBLE_TOUCH_POINTS;
195
+ if (targetOs === "mac" || tooMany || (!isNone(touch) && !isPyInt(touch))) {
196
+ config["navigator.maxTouchPoints"] = 0;
197
+ }
198
+ }
199
+ function checkDevicePixelRatio(config, targetOs) {
200
+ const dpr = config["window.devicePixelRatio"];
201
+ if (isNone(dpr))
202
+ return null;
203
+ const allowed = PLAUSIBLE_DPR[targetOs];
204
+ if (allowed?.length && !allowed.includes(Number(num(dpr)))) {
205
+ return `window.devicePixelRatio ${pyStr(dpr)} is not a display mode ${targetOs} offers`;
206
+ }
207
+ return null;
208
+ }
209
+ // The CSS geometry a ratio scales: device pixels / devicePixelRatio.
210
+ const CSS_GEOMETRY_KEYS = [
211
+ "screen.width",
212
+ "screen.height",
213
+ "screen.availWidth",
214
+ "screen.availHeight",
215
+ "screen.availLeft",
216
+ "screen.availTop",
217
+ "window.outerWidth",
218
+ "window.outerHeight",
219
+ "window.innerWidth",
220
+ "window.innerHeight",
221
+ "window.screenX",
222
+ "window.screenY",
223
+ ];
224
+ /** Python's round(): half to even, so both launchers draw the same geometry. */
225
+ export function roundHalfEven(v) {
226
+ const r = Math.round(v);
227
+ return Math.abs(v % 1) === 0.5 && r % 2 !== 0 ? r - 1 : r;
228
+ }
229
+ /**
230
+ * A devicePixelRatio no display mode of the OS offers: a browser zoom folded
231
+ * into the ratio, which scaled every CSS size with it.
232
+ */
233
+ export function isZoomedRatio(dpr, targetOs) {
234
+ const allowed = PLAUSIBLE_DPR[targetOs];
235
+ if (!allowed?.length || typeof dpr !== "number" || !(dpr > 0))
236
+ return false;
237
+ return !allowed.some((v) => v === dpr);
238
+ }
239
+ /**
240
+ * Move the ratio to the nearest display mode, rescaling the CSS geometry so
241
+ * the screen stays the display behind it.
242
+ */
243
+ export function repairDevicePixelRatio(config, targetOs, target) {
244
+ const dpr = config["window.devicePixelRatio"];
245
+ const allowed = PLAUSIBLE_DPR[targetOs];
246
+ if (isNone(dpr) || !allowed?.length)
247
+ return;
248
+ // `target` is the scale a caller renders at; else the nearest real scaling
249
+ // step (min() keeps the first of equal distances).
250
+ const x = Number(num(dpr));
251
+ let best = allowed[0];
252
+ for (const v of allowed.slice(1)) {
253
+ if (Math.abs(v - x) < Math.abs(best - x))
254
+ best = v;
255
+ }
256
+ if (target !== undefined && target > 0)
257
+ best = target;
258
+ config["window.devicePixelRatio"] = best;
259
+ // The scraped ratio has a browser zoom folded in, which shrank every CSS
260
+ // size by it; the display behind it keeps its device pixels. Rescale to the
261
+ // step so the screen is still that display: 1760x990 at 1.0909 is a
262
+ // 1920x1080 monitor, so 1920x1080 at 1. Firefox rounds device / ratio.
263
+ const scale = x / best;
264
+ for (const key of CSS_GEOMETRY_KEYS) {
265
+ const value = config[key];
266
+ if (typeof value === "number") {
267
+ config[key] = roundHalfEven(value * scale);
268
+ }
269
+ }
270
+ }
271
+ function checkWindowChrome(config, _targetOs) {
272
+ const inner = config["window.innerHeight"];
273
+ const outer = config["window.outerHeight"];
274
+ if (!pyTruthy(inner) || !pyTruthy(outer))
275
+ return null;
276
+ const chrome = num(outer) - num(inner);
277
+ if (chrome < BROWSER_CHROME_HEIGHT) {
278
+ return (`window.outerHeight ${pyStr(outer)} - innerHeight ${pyStr(inner)} = ${pyStr(chrome)}, less than the ` +
279
+ `${BROWSER_CHROME_HEIGHT}px of chrome the window actually has; the bottom ` +
280
+ `${pyStr(BROWSER_CHROME_HEIGHT - chrome)}px of the claimed viewport cannot receive input`);
281
+ }
282
+ return null;
283
+ }
284
+ function repairWindowChrome(config, _targetOs) {
285
+ const inner = config["window.innerHeight"];
286
+ const outer = config["window.outerHeight"];
287
+ if (!pyTruthy(inner) || !pyTruthy(outer))
288
+ return;
289
+ const avail = pyTruthy(config["screen.availHeight"])
290
+ ? config["screen.availHeight"]
291
+ : config["screen.height"];
292
+ // Prefer growing the window, which keeps the viewport the identity drew.
293
+ if (!pyTruthy(avail) || num(inner) + BROWSER_CHROME_HEIGHT <= num(avail)) {
294
+ config["window.outerHeight"] = num(inner) + BROWSER_CHROME_HEIGHT;
295
+ return;
296
+ }
297
+ // No room on the claimed screen: shrink the viewport instead.
298
+ config["window.innerHeight"] = Math.max(num(outer) - BROWSER_CHROME_HEIGHT, 1);
299
+ }
300
+ function checkScreenShape(config, _targetOs) {
301
+ const width = config["screen.width"];
302
+ const height = config["screen.height"];
303
+ if (!pyTruthy(width) || !pyTruthy(height))
304
+ return null;
305
+ if (num(height) > num(width)) {
306
+ return `portrait screen ${pyStr(width)}x${pyStr(height)}; desktop panels are landscape`;
307
+ }
308
+ if (num(width) < 1024) {
309
+ return `screen ${pyStr(width)}x${pyStr(height)} is smaller than any current desktop panel`;
310
+ }
311
+ return null;
312
+ }
313
+ function checkAvailBounds(config, _targetOs) {
314
+ const width = config["screen.width"];
315
+ const height = config["screen.height"];
316
+ const availW = config["screen.availWidth"];
317
+ const availH = config["screen.availHeight"];
318
+ if (pyTruthy(width) && pyTruthy(availW) && num(availW) > num(width)) {
319
+ return `screen.availWidth ${pyStr(availW)} exceeds screen.width ${pyStr(width)}`;
320
+ }
321
+ if (pyTruthy(height) && pyTruthy(availH) && num(availH) > num(height)) {
322
+ return `screen.availHeight ${pyStr(availH)} exceeds screen.height ${pyStr(height)}`;
323
+ }
324
+ return null;
325
+ }
326
+ function repairAvailBounds(config, _targetOs) {
327
+ const width = config["screen.width"];
328
+ const height = config["screen.height"];
329
+ const availW = "screen.availWidth" in config ? config["screen.availWidth"] : 0;
330
+ const availH = "screen.availHeight" in config ? config["screen.availHeight"] : 0;
331
+ if (pyTruthy(width) && num(availW) > num(width))
332
+ config["screen.availWidth"] = width;
333
+ if (pyTruthy(height) && num(availH) > num(height))
334
+ config["screen.availHeight"] = height;
335
+ }
336
+ function checkArchAgreement(config, _targetOs) {
337
+ const str = (key) => pyTruthy(config[key]) ? pyStr(config[key]) : "";
338
+ const ua = str("navigator.userAgent");
339
+ const platform = str("navigator.platform");
340
+ const oscpu = str("navigator.oscpu");
341
+ if (!ua)
342
+ return null;
343
+ if (ua.includes("x86_64") &&
344
+ (platform.includes("armv") || oscpu.includes("armv"))) {
345
+ return `user agent claims x86_64 while platform/oscpu say ${pyRepr(platform)}/${pyRepr(oscpu)}`;
346
+ }
347
+ return null;
348
+ }
349
+ // The CPU the identity claims: its architecture (cpu:arch) and manufacturer
350
+ // (cpu:manufacturer); coherence.py explains why it shows (canvas, Web Audio,
351
+ // Opus, relaxed-SIMD wasm and NaN signs differ between x86-64 and ARM64, and
352
+ // canvas and Opus between Intel and AMD) and has the sources for the table of
353
+ // manufacturers per OS (launcher-constants.json, cpuManufacturers).
354
+ //
355
+ // The GPU decides where it can: Intel integrated graphics, Intel; "Radeon HD
356
+ // 3200 Graphics", the bucket of AMD's APU graphics, AMD; every x86 Mac, Intel;
357
+ // an Apple GPU, Apple ARM; an Adreno, Qualcomm; a VideoCore/V3D, Broadcom; a
358
+ // Mali, an ARM SoC of some other maker. Any other GPU leaves the CPU open. A
359
+ // core count only Intel's hybrids report (10, 14, 18, 22, 28) implies Intel,
360
+ // for x86 only (an M4 Pro has 14).
361
+ export const CPU_ARCHES = ["x86", "arm"];
362
+ export const CPU_MANUFACTURERS = LAUNCHER_CONSTANTS.cpuManufacturers;
363
+ export const CPU_VENDORS = ["intel", "amd"];
364
+ const INTEL_ONLY_CORE_COUNTS = new Set(LAUNCHER_CONSTANTS.intelOnlyCoreCounts);
365
+ const INTEL_IGPUS = [
366
+ "Intel(R) HD Graphics, or similar",
367
+ "Intel(R) HD Graphics 400, or similar",
368
+ "Intel(R) HD Graphics Direct3D11",
369
+ "Intel(R) HD Graphics 400 Direct3D11",
370
+ ];
371
+ const AMD_IGPUS = ["Radeon HD 3200 Graphics"];
372
+ /** ARM GPUs and the SoC makers that ship them. */
373
+ const ARM_GPUS = [
374
+ ["Apple M", ["apple"]],
375
+ ["Apple GPU", ["apple"]],
376
+ ["Adreno", ["qualcomm"]],
377
+ ["V3D", ["broadcom"]],
378
+ ["VideoCore", ["broadcom"]],
379
+ ["Mali", ["rockchip", "mediatek", "amlogic", "allwinner"]],
380
+ ["Immortalis", ["mediatek"]],
381
+ ["Tegra", ["nvidia"]],
382
+ ];
383
+ /** The cpuManufacturers key for a launcher OS name (win, mac, lin, ...). */
384
+ export function cpuOs(targetOs) {
385
+ const map = {
386
+ win: "windows",
387
+ windows: "windows",
388
+ mac: "mac",
389
+ macos: "mac",
390
+ };
391
+ return Object.hasOwn(map, targetOs) ? map[targetOs] : "linux";
392
+ }
393
+ export function cpuManufacturers(targetOs, arch) {
394
+ const table = CPU_MANUFACTURERS[cpuOs(targetOs)];
395
+ return typeof arch === "string" && Object.hasOwn(table, arch)
396
+ ? table[arch]
397
+ : [];
398
+ }
399
+ /**
400
+ * Check a caller's `cpu` option and fill in what it implies, as
401
+ * coherence.resolve_cpu does. Throws ValueError for anything no real machine
402
+ * running `targetOs` is: an unknown key, architecture or manufacturer, or a
403
+ * pair such as an AMD Mac or an Apple x86 CPU. A manufacturer alone implies
404
+ * its architecture.
405
+ */
406
+ export function resolveCpu(cpu, targetOs) {
407
+ if (!isPlainObject(cpu)) {
408
+ throw new ValueError(`cpu must be a dict such as {'arch': 'x86', 'manufacturer': 'intel'}, not ${pyRepr(cpu)}`);
409
+ }
410
+ const unknownKeys = Object.keys(cpu)
411
+ .filter((k) => k !== "arch" && k !== "manufacturer")
412
+ .sort(comparePyStr);
413
+ if (unknownKeys.length) {
414
+ throw new ValueError(`cpu takes 'arch' and 'manufacturer', not ${unknownKeys.map(pyRepr).join(", ")}`);
415
+ }
416
+ let arch = isNone(cpu.arch) ? null : cpu.arch;
417
+ const maker = isNone(cpu.manufacturer) ? null : cpu.manufacturer;
418
+ if (arch !== null && !CPU_ARCHES.includes(arch)) {
419
+ throw new ValueError(`cpu arch must be one of ('x86', 'arm'), not ${pyRepr(arch)}`);
420
+ }
421
+ const osName = cpuOs(targetOs);
422
+ const table = CPU_MANUFACTURERS[osName];
423
+ const makersOf = (a) => Object.hasOwn(table, a) ? table[a] : [];
424
+ if (maker !== null) {
425
+ const arches = CPU_ARCHES.filter((a) => makersOf(a).includes(maker));
426
+ if (!arches.length) {
427
+ const known = [...new Set(Object.values(table).flat())].sort(comparePyStr);
428
+ throw new ValueError(`no ${osName} machine has a CPU made by ${pyRepr(maker)}; on ${osName} it is one of ${known.join(", ")}`);
429
+ }
430
+ if (arch === null)
431
+ arch = arches[0];
432
+ else if (!arches.includes(arch)) {
433
+ throw new ValueError(`no ${osName} machine has an ${pyStr(arch)} CPU made by ${pyRepr(maker)}; ${osName} ${pyStr(arch)} CPUs are made by ` +
434
+ `${makersOf(arch).join(", ") || "nobody"}`);
435
+ }
436
+ }
437
+ else if (arch !== null && !makersOf(arch).length) {
438
+ throw new ValueError(`no ${osName} machine has an ${pyStr(arch)} CPU`);
439
+ }
440
+ const out = {};
441
+ if (arch !== null)
442
+ out["cpu:arch"] = arch;
443
+ if (maker !== null)
444
+ out["cpu:manufacturer"] = maker;
445
+ return out;
446
+ }
447
+ /**
448
+ * The CPU architecture this GPU implies and the manufacturers it allows, as
449
+ * coherence.gpu_cpu. [null, []] when the GPU leaves both open. An empty list
450
+ * with an architecture means any maker of that architecture on `targetOs`.
451
+ */
452
+ export function gpuCpu(gpuRenderer, targetOs) {
453
+ const r = pyTruthy(gpuRenderer) ? pyStr(gpuRenderer) : "";
454
+ if (!r)
455
+ return [null, []];
456
+ for (const [marker, makers] of ARM_GPUS) {
457
+ if (r.includes(marker))
458
+ return ["arm", makers];
459
+ }
460
+ if (cpuOs(targetOs) === "mac")
461
+ return ["x86", ["intel"]];
462
+ if (INTEL_IGPUS.some((igpu) => r.includes(igpu)))
463
+ return ["x86", ["intel"]];
464
+ if (AMD_IGPUS.some((igpu) => r.includes(igpu)))
465
+ return ["x86", ["amd"]];
466
+ return [null, []];
467
+ }
468
+ /** Whether a machine with this GPU can have this CPU. */
469
+ export function gpuFitsCpu(gpuRenderer, targetOs, arch, maker) {
470
+ const [byGpu, makers] = gpuCpu(gpuRenderer, targetOs);
471
+ let a = isNone(arch) ? null : arch;
472
+ const m = isNone(maker) ? null : maker;
473
+ if (a === null && m !== null) {
474
+ a =
475
+ CPU_ARCHES.find((x) => cpuManufacturers(targetOs, x).includes(m)) ?? null;
476
+ }
477
+ // Mesa's software renderer draws on whatever CPU the machine has.
478
+ if (a === null || pyStr(gpuRenderer ?? "").includes("llvmpipe"))
479
+ return true;
480
+ if (byGpu === null) {
481
+ // A discrete card or an open bucket: an x86 machine. No ARM laptop or
482
+ // board on sale carries a discrete GPU Firefox would name.
483
+ return a === "x86" && cpuOs(targetOs) !== "mac";
484
+ }
485
+ if (byGpu !== a)
486
+ return false;
487
+ return (m === null || !makers.length || makers.includes(m));
488
+ }
489
+ /** The x86 CPU vendor this GPU implies, or null when it leaves it open. */
490
+ export function gpuCpuVendor(gpuRenderer, targetOs) {
491
+ const [arch, makers] = gpuCpu(gpuRenderer, targetOs);
492
+ return arch === "x86" && makers.length === 1
493
+ ? makers[0]
494
+ : null;
495
+ }
496
+ /** The x86 CPU vendor this navigator.hardwareConcurrency implies, or null. */
497
+ export function coresCpuVendor(cores) {
498
+ return INTEL_ONLY_CORE_COUNTS.has(cores) ? "intel" : null;
499
+ }
500
+ /**
501
+ * The [arch, manufacturer] the identity's GPU or, failing that, its core
502
+ * count implies.
503
+ */
504
+ export function hardwareCpu(config, targetOs) {
505
+ const [arch, makers] = gpuCpu(renderer(config), targetOs);
506
+ if (arch === "arm")
507
+ return [arch, makers.length === 1 ? makers[0] : null];
508
+ const vendor = (makers.length ? makers[0] : null) ||
509
+ coresCpuVendor(config["navigator.hardwareConcurrency"]);
510
+ return [vendor || arch ? "x86" : null, vendor];
511
+ }
512
+ /** The x86 CPU vendor the identity's GPU or, failing that, its core count implies. */
513
+ export function hardwareCpuVendor(config, targetOs) {
514
+ const [arch, maker] = hardwareCpu(config, targetOs);
515
+ return arch === "x86" ? maker : null;
516
+ }
517
+ /** Why the claimed CPU cannot sit in this machine, or null. */
518
+ export function cpuConflict(config, targetOs) {
519
+ const arch = isNone(config["cpu:arch"]) ? null : config["cpu:arch"];
520
+ const maker = isNone(config["cpu:manufacturer"])
521
+ ? null
522
+ : config["cpu:manufacturer"];
523
+ if (arch === null && maker === null)
524
+ return null;
525
+ const cpu = {};
526
+ if (arch !== null)
527
+ cpu.arch = arch;
528
+ if (maker !== null)
529
+ cpu.manufacturer = maker;
530
+ try {
531
+ resolveCpu(cpu, targetOs);
532
+ }
533
+ catch (e) {
534
+ if (e instanceof ValueError)
535
+ return e.message;
536
+ throw e;
537
+ }
538
+ const r = renderer(config);
539
+ const claimed = [maker, arch]
540
+ .filter((v) => pyTruthy(v))
541
+ .map(pyStr)
542
+ .join(" ");
543
+ if (r && !gpuFitsCpu(r, targetOs, arch, maker)) {
544
+ return `CPU ${claimed} beside ${pyRepr(r)}`;
545
+ }
546
+ const cores = config["navigator.hardwareConcurrency"];
547
+ const byCores = coresCpuVendor(cores);
548
+ if ((pyTruthy(arch) ? arch : "x86") === "x86" &&
549
+ pyTruthy(maker) &&
550
+ maker !== byCores &&
551
+ byCores) {
552
+ return `CPU ${claimed} with hardwareConcurrency ${pyStr(cores)}, which only an Intel CPU reports`;
553
+ }
554
+ return null;
555
+ }
556
+ function checkCpu(config, targetOs) {
557
+ return cpuConflict(config, targetOs);
558
+ }
559
+ function repairCpu(config, targetOs) {
560
+ // The GPU decides. An AMD APU's GPU beside an Intel-only core count stays
561
+ // reported: no vendor matches both.
562
+ const [implied, maker] = hardwareCpu(config, targetOs);
563
+ const arch = implied ?? "x86";
564
+ config["cpu:arch"] = arch;
565
+ if (maker)
566
+ config["cpu:manufacturer"] = maker;
567
+ else if (!cpuManufacturers(targetOs, arch).includes(config["cpu:manufacturer"]) ||
568
+ cpuConflict(config, targetOs)) {
569
+ delete config["cpu:manufacturer"];
570
+ }
571
+ }
572
+ export const RULES = [
573
+ {
574
+ name: "apple-silicon-cores",
575
+ check: checkAppleSiliconCores,
576
+ repair: repairAppleSiliconCores,
577
+ },
578
+ { name: "gpu-matches-os", check: checkGpuMatchesOs, repair: null },
579
+ { name: "intel-mac-hardware", check: checkIntelMacHardware, repair: null },
580
+ { name: "color-depth", check: checkColorDepth, repair: repairColorDepth },
581
+ { name: "pixel-depth", check: checkPixelDepth, repair: repairPixelDepth },
582
+ { name: "touch-points", check: checkTouchPoints, repair: repairTouchPoints },
583
+ {
584
+ name: "device-pixel-ratio",
585
+ check: checkDevicePixelRatio,
586
+ repair: repairDevicePixelRatio,
587
+ adjusts: CSS_GEOMETRY_KEYS,
588
+ },
589
+ {
590
+ name: "window-chrome",
591
+ check: checkWindowChrome,
592
+ repair: repairWindowChrome,
593
+ },
594
+ { name: "screen-shape", check: checkScreenShape, repair: null },
595
+ { name: "avail-bounds", check: checkAvailBounds, repair: repairAvailBounds },
596
+ { name: "arch-agreement", check: checkArchAgreement, repair: null },
597
+ { name: "cpu-matches-hardware", check: checkCpu, repair: repairCpu },
598
+ ];
599
+ /** Whether the screen is one no desktop reports (portrait, or tiny). */
600
+ export function screenIsImplausible(config) {
601
+ return checkScreenShape(config, "") !== null;
602
+ }
603
+ /**
604
+ * Turn a portrait screen landscape, keeping the panel's own dimensions. Run
605
+ * before the window clamps, which then bound the window to the new screen.
606
+ */
607
+ export function repairScreenOrientation(config) {
608
+ const width = config["screen.width"];
609
+ const height = config["screen.height"];
610
+ if (!pyTruthy(width) || !pyTruthy(height) || num(height) <= num(width))
611
+ return false;
612
+ config["screen.width"] = height;
613
+ config["screen.height"] = width;
614
+ const availW = config["screen.availWidth"];
615
+ const availH = config["screen.availHeight"];
616
+ if (pyTruthy(availW) && pyTruthy(availH)) {
617
+ config["screen.availWidth"] = availH;
618
+ config["screen.availHeight"] = availW;
619
+ }
620
+ return true;
621
+ }
622
+ function changed(before, after, keys) {
623
+ return [...keys]
624
+ .filter((k) => k in before !== k in after ||
625
+ JSON.stringify(before[k]) !== JSON.stringify(after[k]))
626
+ .sort();
627
+ }
628
+ /**
629
+ * Discard values a source supplied that this identity cannot keep (a preset's
630
+ * GPU pair its OS, or its final core count and screen, rule out), so the
631
+ * normal WebGL sampling draws a coherent one instead. Run after the core count
632
+ * and the screen are final. Only values another pool can replace are dropped.
633
+ * A GPU the caller passed (a key in `keep`) is never dropped: it is reported,
634
+ * with the keys it kept, instead.
635
+ */
636
+ export function dropIncoherentSourceValues(config, targetOs, keep = new Set()) {
637
+ const r = renderer(config);
638
+ if (!r)
639
+ return [];
640
+ let rule;
641
+ if (!gpuFitsOs(r, targetOs))
642
+ rule = "gpu-matches-os";
643
+ else if (checkIntelMacHardware(config, targetOs))
644
+ rule = "intel-mac-hardware";
645
+ else
646
+ return [];
647
+ const kept = changed(config, {}, new Set(["webGl:renderer", "webGl:vendor"].filter((k) => keep.has(k))));
648
+ if (kept.length) {
649
+ return [
650
+ {
651
+ rule,
652
+ detail: `${pyRepr(r)} does not fit this ${targetOs} identity`,
653
+ kept,
654
+ },
655
+ ];
656
+ }
657
+ delete config["webGl:renderer"];
658
+ delete config["webGl:vendor"];
659
+ return [{ rule, detail: `dropped ${pyRepr(r)} for a ${targetOs} identity` }];
660
+ }
661
+ /** Every invariant this identity breaks. Empty means coherent. */
662
+ export function validate(config, targetOs) {
663
+ const violations = [];
664
+ for (const rule of RULES) {
665
+ const detail = rule.check(config, targetOs);
666
+ if (detail)
667
+ violations.push({ rule: rule.name, detail });
668
+ }
669
+ return violations;
670
+ }
671
+ /**
672
+ * Repair what is determined, and report what is left. A rule with no repair
673
+ * cannot be corrected without inventing a machine, so it is returned for the
674
+ * caller to decide about.
675
+ *
676
+ * `keep` names the keys the caller set. A repair that would change one of
677
+ * them is not made: the caller's value stands, and the violation comes back
678
+ * with those keys in `kept` so the launcher can warn about it (#956). A repair
679
+ * that only touches other keys still runs.
680
+ */
681
+ export function apply(config, targetOs, keep = new Set()) {
682
+ const kept = new Map();
683
+ for (const rule of RULES) {
684
+ if (!rule.repair || !rule.check(config, targetOs))
685
+ continue;
686
+ const repaired = { ...config };
687
+ rule.repair(repaired, targetOs);
688
+ if (rule.adjusts?.some((key) => keep.has(key))) {
689
+ for (const key of rule.adjusts) {
690
+ if (key in config)
691
+ repaired[key] = config[key];
692
+ else
693
+ delete repaired[key];
694
+ }
695
+ }
696
+ const blocked = changed(config, repaired, keep);
697
+ if (blocked.length) {
698
+ kept.set(rule.name, blocked);
699
+ }
700
+ else {
701
+ for (const key of Object.keys(config))
702
+ if (!(key in repaired))
703
+ delete config[key];
704
+ Object.assign(config, repaired);
705
+ }
706
+ }
707
+ return validate(config, targetOs).map((v) => kept.has(v.rule) ? { ...v, kept: kept.get(v.rule) } : v);
708
+ }