camoufox-js 0.12.0 → 0.12.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.
package/README.md CHANGED
@@ -9,7 +9,7 @@ npm install camoufox-js
9
9
  ```
10
10
 
11
11
  The Camoufox browser itself is downloaded with `npx camoufox-js fetch` into the
12
- per-user cache directory (`~/.cache/camoufox` on Linux). Set the
12
+ per-user cache directory (`$XDG_CACHE_HOME/camoufox` or `~/.cache/camoufox` on Linux). Set the
13
13
  `CAMOUFOX_INSTALL_DIR` environment variable to install and resolve it from a
14
14
  custom location instead — useful in containers and CI images where the home
15
15
  directory is ephemeral or persisted separately (similar to Playwright's
@@ -6,6 +6,12 @@ export declare class CONSTRAINTS {
6
6
  * The minimum and maximum supported versions of the Camoufox browser.
7
7
  */
8
8
  static readonly MIN_VERSION: string;
9
+ /**
10
+ * Exclusive. The Firefox 156 builds (beta.32+) dropped properties this library still generates.
11
+ */
9
12
  static readonly MAX_VERSION: string;
10
- static asRange(): string;
13
+ /**
14
+ * Minimum version with Playwright 1.61+, which sends viewport fields that older builds reject.
15
+ */
16
+ static readonly PLAYWRIGHT_1_61_MIN_VERSION: string;
11
17
  }
@@ -6,11 +6,12 @@ export class CONSTRAINTS {
6
6
  * The minimum and maximum supported versions of the Camoufox browser.
7
7
  */
8
8
  static MIN_VERSION = "alpha.1";
9
- static MAX_VERSION = "1";
10
- static asRange() {
11
- /**
12
- * Returns the version range as a string.
13
- */
14
- return `>=${CONSTRAINTS.MIN_VERSION}, <${CONSTRAINTS.MAX_VERSION}`;
15
- }
9
+ /**
10
+ * Exclusive. The Firefox 156 builds (beta.32+) dropped properties this library still generates.
11
+ */
12
+ static MAX_VERSION = "beta.32";
13
+ /**
14
+ * Minimum version with Playwright 1.61+, which sends viewport fields that older builds reject.
15
+ */
16
+ static PLAYWRIGHT_1_61_MIN_VERSION = "beta.30";
16
17
  }
package/dist/pkgman.d.ts CHANGED
@@ -48,9 +48,10 @@ export declare class CamoufoxFetcher extends GitHubDownloader {
48
48
  static getPlatformArch(): string;
49
49
  fetchLatest(): Promise<void>;
50
50
  static downloadFile(url: string): Promise<Buffer>;
51
- extractZip(zipFile: string | Buffer): Promise<void>;
51
+ extractZip(zipFile: string | Buffer, destDir?: string): Promise<void>;
52
52
  static cleanup(): boolean;
53
- setVersion(): void;
53
+ setVersion(destDir?: string): void;
54
+ private static removeLeftovers;
54
55
  install(): Promise<void>;
55
56
  get url(): string;
56
57
  get version(): string;
package/dist/pkgman.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { execFileSync } from "node:child_process";
2
2
  import * as fs from "node:fs";
3
+ import { createRequire } from "node:module";
3
4
  import * as os from "node:os";
4
5
  import * as path from "node:path";
5
6
  import { setTimeout } from "node:timers/promises";
@@ -56,6 +57,16 @@ const LAUNCH_FILE = {
56
57
  mac: "../MacOS/camoufox",
57
58
  lin: "camoufox-bin",
58
59
  };
60
+ function isPlaywright161OrNewer() {
61
+ try {
62
+ const { version } = createRequire(import.meta.url)("playwright-core/package.json");
63
+ const [major, minor] = version.split(".").map(Number);
64
+ return major > 1 || (major === 1 && minor >= 61);
65
+ }
66
+ catch {
67
+ return false;
68
+ }
69
+ }
59
70
  class Version {
60
71
  release;
61
72
  version;
@@ -90,7 +101,7 @@ class Version {
90
101
  return false;
91
102
  }
92
103
  isSupported() {
93
- return VERSION_MIN.lessThan(this) && this.lessThan(VERSION_MAX);
104
+ return !this.lessThan(VERSION_MIN) && this.lessThan(VERSION_MAX);
94
105
  }
95
106
  static fromPath(filePath = INSTALL_DIR) {
96
107
  const versionPath = path.join(filePath.toString(), "version.json");
@@ -105,12 +116,15 @@ class Version {
105
116
  }
106
117
  static buildMinMax() {
107
118
  return [
108
- new Version(CONSTRAINTS.MIN_VERSION),
119
+ new Version(isPlaywright161OrNewer()
120
+ ? CONSTRAINTS.PLAYWRIGHT_1_61_MIN_VERSION
121
+ : CONSTRAINTS.MIN_VERSION),
109
122
  new Version(CONSTRAINTS.MAX_VERSION),
110
123
  ];
111
124
  }
112
125
  }
113
126
  const [VERSION_MIN, VERSION_MAX] = Version.buildMinMax();
127
+ const SUPPORTED_RANGE = `>=${VERSION_MIN.release}, <${VERSION_MAX.release}`;
114
128
  export class GitHubDownloader {
115
129
  githubRepo;
116
130
  apiUrl;
@@ -181,7 +195,7 @@ export class CamoufoxFetcher extends GitHubDownloader {
181
195
  return [version, asset.browser_download_url];
182
196
  }
183
197
  missingAssetError() {
184
- throw new MissingRelease(`No matching release found for ${OS_NAME} ${this.arch} in the supported range: (${CONSTRAINTS.asRange()}). Please update the library.`);
198
+ throw new MissingRelease(`No matching release found for ${OS_NAME} ${this.arch} in the supported range: (${SUPPORTED_RANGE}). Please update the library.`);
185
199
  }
186
200
  static getPlatformArch() {
187
201
  const platArch = os.arch().toLowerCase();
@@ -207,9 +221,9 @@ export class CamoufoxFetcher extends GitHubDownloader {
207
221
  });
208
222
  return Buffer.from(await response.arrayBuffer());
209
223
  }
210
- async extractZip(zipFile) {
224
+ async extractZip(zipFile, destDir = INSTALL_DIR.toString()) {
211
225
  const zip = new AdmZip(zipFile);
212
- zip.extractAllTo(INSTALL_DIR.toString(), true);
226
+ zip.extractAllTo(destDir, true);
213
227
  }
214
228
  static cleanup() {
215
229
  if (fs.existsSync(INSTALL_DIR)) {
@@ -218,30 +232,48 @@ export class CamoufoxFetcher extends GitHubDownloader {
218
232
  }
219
233
  return false;
220
234
  }
221
- setVersion() {
222
- fs.writeFileSync(path.join(INSTALL_DIR.toString(), "version.json"), JSON.stringify({ version: this.version, release: this.release }));
235
+ setVersion(destDir = INSTALL_DIR.toString()) {
236
+ fs.writeFileSync(path.join(destDir, "version.json"), JSON.stringify({ version: this.version, release: this.release }));
237
+ }
238
+ // Remove staging dirs left behind by an interrupted install.
239
+ static removeLeftovers(installDir) {
240
+ const parent = path.dirname(installDir);
241
+ const prefix = `${path.basename(installDir)}.staging-`;
242
+ for (const name of fs.readdirSync(parent)) {
243
+ if (name.startsWith(prefix)) {
244
+ fs.rmSync(path.join(parent, name), { recursive: true, force: true });
245
+ }
246
+ }
223
247
  }
224
248
  async install() {
225
249
  await this.init();
226
- await CamoufoxFetcher.cleanup();
227
- // Set up outside the try so finally can always tear down the ~600MB staging dir.
250
+ // Resolve a symlinked install dir so the swap replaces its target, not the link.
251
+ const installDir = fs.existsSync(INSTALL_DIR)
252
+ ? fs.realpathSync(INSTALL_DIR)
253
+ : INSTALL_DIR.toString();
254
+ fs.mkdirSync(path.dirname(installDir), { recursive: true });
255
+ CamoufoxFetcher.removeLeftovers(installDir);
256
+ // Staged next to the install dir so the final rename stays on one filesystem.
257
+ // Set up outside the try so finally can always tear down the ~600MB staging dirs.
258
+ const stagingDir = fs.mkdtempSync(`${installDir}.staging-`);
228
259
  const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "camoufox-"));
229
260
  const tempFilePath = path.join(tempDir, "camoufox.zip");
230
261
  const tempFileStream = fs.createWriteStream(tempFilePath);
231
262
  try {
232
- fs.mkdirSync(INSTALL_DIR, { recursive: true });
233
263
  await webdl(this.url, "Downloading Camoufox...", true, tempFileStream);
234
264
  await new Promise((r) => tempFileStream.close(r));
235
- await this.extractZip(tempFilePath);
236
- this.setVersion();
265
+ await this.extractZip(tempFilePath, stagingDir);
266
+ this.setVersion(stagingDir);
237
267
  if (OS_NAME !== "win") {
238
- execFileSync("chmod", ["-R", "755", INSTALL_DIR.toString()]);
268
+ execFileSync("chmod", ["-R", "755", stagingDir]);
239
269
  }
270
+ // Replace the previous install only once the new one is complete.
271
+ fs.rmSync(installDir, { recursive: true, force: true });
272
+ fs.renameSync(stagingDir, installDir);
240
273
  console.log("Camoufox successfully installed.");
241
274
  }
242
275
  catch (e) {
243
276
  console.error(`Error installing Camoufox: ${e}`);
244
- await CamoufoxFetcher.cleanup();
245
277
  throw e;
246
278
  }
247
279
  finally {
@@ -252,9 +284,10 @@ export class CamoufoxFetcher extends GitHubDownloader {
252
284
  await new Promise((resolve) => tempFileStream.close(() => resolve()));
253
285
  }
254
286
  fs.rmSync(tempDir, { recursive: true, force: true });
287
+ fs.rmSync(stagingDir, { recursive: true, force: true });
255
288
  }
256
289
  catch (cleanupErr) {
257
- console.error(`Failed to remove staging dir ${tempDir}: ${cleanupErr}`);
290
+ console.error(`Failed to remove staging dir: ${cleanupErr}`);
258
291
  }
259
292
  }
260
293
  }
@@ -291,7 +324,9 @@ function userCacheDir(appName) {
291
324
  return path.join(os.homedir(), "Library", "Caches", appName);
292
325
  }
293
326
  else {
294
- return path.join(os.homedir(), ".cache", appName);
327
+ // Per the XDG spec a relative XDG_CACHE_HOME is invalid and ignored.
328
+ const xdg = process.env.XDG_CACHE_HOME;
329
+ return path.join(xdg && path.isAbsolute(xdg) ? xdg : path.join(os.homedir(), ".cache"), appName);
295
330
  }
296
331
  }
297
332
  export function installedVerStr() {
@@ -309,9 +344,7 @@ export function camoufoxPath(downloadIfMissing = true) {
309
344
  return INSTALL_DIR;
310
345
  }
311
346
  else {
312
- if (!downloadIfMissing) {
313
- throw new UnsupportedVersion("Camoufox executable is outdated.");
314
- }
347
+ throw new UnsupportedVersion(`Camoufox v${installedVerStr()} is not supported by this library (supported range: ${SUPPORTED_RANGE}). Please run \`camoufox fetch\` to install a supported version.`);
315
348
  }
316
349
  // Install and recheck
317
350
  const fetcher = new CamoufoxFetcher();
package/dist/utils.js CHANGED
@@ -454,8 +454,10 @@ export async function launchOptions({ config, os, block_images, block_webrtc, bl
454
454
  // Set geolocation
455
455
  if (geoip) {
456
456
  geoipAllowed();
457
- // Find the user's IP address
458
- geoip = await publicIP(proxyUrl?.href);
457
+ // Use provided IP directly, or resolve the public IP via the proxy if provided
458
+ if (typeof geoip !== "string") {
459
+ geoip = await publicIP(proxyUrl?.href);
460
+ }
459
461
  // Spoof WebRTC if not blocked
460
462
  if (!block_webrtc) {
461
463
  if (validIPv4(geoip)) {
@@ -1,5 +1,5 @@
1
1
  import { execFileSync, spawn } from "node:child_process";
2
- import { accessSync, constants as fsConstants } from "node:fs";
2
+ import { accessSync, constants as fsConstants, unlinkSync } from "node:fs";
3
3
  import { CannotExecuteXvfb, CannotFindXvfb, VirtualDisplayNotSupported, } from "./exceptions.js";
4
4
  import { OS_NAME } from "./pkgman.js";
5
5
  // Safe timeout for xvfb writing display num, prevents infinite hang
@@ -116,12 +116,28 @@ export class VirtualDisplay {
116
116
  return `:${this._display}`;
117
117
  }
118
118
  kill() {
119
- if (this.proc && this.proc.exitCode === null && !this.proc.killed) {
120
- if (this.debug) {
121
- console.log("Terminating virtual display:", this._display);
122
- }
123
- this.proc.kill();
119
+ const proc = this.proc;
120
+ const display = this._display;
121
+ if (!proc || proc.exitCode !== null || proc.killed)
122
+ return;
123
+ if (this.debug) {
124
+ console.log("Terminating virtual display:", display);
124
125
  }
126
+ proc.once("exit", () => {
127
+ try {
128
+ unlinkSync(`/tmp/.X${display}-lock`);
129
+ }
130
+ catch {
131
+ // already gone
132
+ }
133
+ try {
134
+ unlinkSync(`/tmp/.X11-unix/X${display}`);
135
+ }
136
+ catch {
137
+ // already gone
138
+ }
139
+ });
140
+ proc.kill("SIGKILL");
125
141
  }
126
142
  static assert_linux() {
127
143
  if (OS_NAME !== "lin") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "camoufox-js",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "bin": "dist/__main__.js",
@@ -44,13 +44,13 @@
44
44
  "@types/language-tags": "^1.0.4",
45
45
  "@types/node": "^24.0.0",
46
46
  "@types/xml2js": "^0.4.14",
47
- "playwright-core": "<1.61.0",
47
+ "playwright-core": "<1.63.0",
48
48
  "rimraf": "^6.0.1",
49
49
  "typescript": "^5.8.3",
50
50
  "vitest": "^4.0.0"
51
51
  },
52
52
  "peerDependencies": {
53
- "playwright-core": "<1.61.0"
53
+ "playwright-core": "<1.63.0"
54
54
  },
55
55
  "devEngines": {
56
56
  "packageManager": {