camoufox-js 0.10.2 → 0.11.0

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/dist/__main__.js CHANGED
@@ -38,8 +38,16 @@ class CamoufoxUpdate extends CamoufoxFetcher {
38
38
  }
39
39
  async update() {
40
40
  if (!this.isUpdateNeeded()) {
41
- console.log("Camoufox binaries up to date!");
42
- console.log(`Current version: v${this.currentVerStr}`);
41
+ if (this.currentVerStr === null) {
42
+ console.log(`No cached Camoufox binary found at ${INSTALL_DIR}. Provision it before launching, or unset PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD to download.`);
43
+ }
44
+ else if (getAsBooleanFromENV("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", false)) {
45
+ console.log(`Using cached Camoufox v${this.currentVerStr} (update check skipped).`);
46
+ }
47
+ else {
48
+ console.log("Camoufox binaries up to date!");
49
+ console.log(`Current version: v${this.currentVerStr}`);
50
+ }
43
51
  return;
44
52
  }
45
53
  if (this.currentVerStr !== null) {
@@ -5,7 +5,7 @@ export class CONSTRAINTS {
5
5
  /**
6
6
  * The minimum and maximum supported versions of the Camoufox browser.
7
7
  */
8
- static MIN_VERSION = "beta.19";
8
+ static MIN_VERSION = "alpha.1";
9
9
  static MAX_VERSION = "1";
10
10
  static asRange() {
11
11
  /**
package/dist/sync_api.js CHANGED
@@ -11,7 +11,7 @@ export async function NewBrowser(playwright, headless = false, fromOptions = {},
11
11
  const normalizedHeadless = headless === "virtual" ? false : headless || false;
12
12
  if (headless === "virtual") {
13
13
  virtualDisplay = new VirtualDisplay(debug);
14
- launch_options.virtual_display = virtualDisplay.get();
14
+ launch_options.virtual_display = await virtualDisplay.get();
15
15
  }
16
16
  if (!fromOptions || Object.keys(fromOptions).length === 0) {
17
17
  fromOptions = await launchOptions({
@@ -5,16 +5,12 @@ export declare class VirtualDisplay {
5
5
  constructor(debug?: boolean);
6
6
  private get xvfb_args();
7
7
  private get xvfb_path();
8
- private get xvfb_cmd();
9
- private execute_xvfb;
10
- get(): string;
11
- kill(): void;
12
8
  /**
13
- * Get list of lock files in /tmp
14
- * @returns List of lock file paths
9
+ * Launch Xvfb with -displayfd 3 so the kernel/Xvfb itself picks a free
10
+ * display number atomically and reports it back to us. Avoids userspace race conditions
15
11
  */
16
- static _get_lock_files(): string[];
17
- private static _free_display;
18
- private get display();
12
+ private spawnXvfb;
13
+ get(): Promise<string>;
14
+ kill(): void;
19
15
  private static assert_linux;
20
16
  }
@@ -1,16 +1,13 @@
1
1
  import { execFileSync, spawn } from "node:child_process";
2
- import { randomInt } from "node:crypto";
3
- import { existsSync, statSync } from "node:fs";
4
- import { tmpdir } from "node:os";
5
- import path from "node:path";
6
- import { globSync } from "glob";
2
+ import { accessSync, constants as fsConstants } from "node:fs";
7
3
  import { CannotExecuteXvfb, CannotFindXvfb, VirtualDisplayNotSupported, } from "./exceptions.js";
8
4
  import { OS_NAME } from "./pkgman.js";
5
+ // Safe timeout for xvfb writing display num, prevents infinite hang
6
+ const DISPLAYFD_READ_TIMEOUT_MS = 10_000;
9
7
  export class VirtualDisplay {
10
8
  debug;
11
9
  proc = null;
12
10
  _display = null;
13
- // private _lock = new Lock();
14
11
  constructor(debug = false) {
15
12
  this.debug = debug;
16
13
  }
@@ -41,78 +38,90 @@ export class VirtualDisplay {
41
38
  ];
42
39
  }
43
40
  get xvfb_path() {
44
- const path = execFileSync("which", ["Xvfb"]).toString().trim();
45
- if (!path) {
41
+ let resolved;
42
+ try {
43
+ resolved = execFileSync("which", ["Xvfb"]).toString().trim();
44
+ }
45
+ catch {
46
46
  throw new CannotFindXvfb("Please install Xvfb to use headless mode.");
47
47
  }
48
- if (!existsSync(path) || !execFileSync("test", ["-x", path])) {
49
- throw new CannotExecuteXvfb(`I do not have permission to execute Xvfb: ${path}`);
48
+ if (!resolved) {
49
+ throw new CannotFindXvfb("Please install Xvfb to use headless mode.");
50
50
  }
51
- return path;
52
- }
53
- get xvfb_cmd() {
54
- return [this.xvfb_path, `:${this.display}`, ...this.xvfb_args];
51
+ try {
52
+ accessSync(resolved, fsConstants.X_OK);
53
+ }
54
+ catch {
55
+ throw new CannotExecuteXvfb(`I do not have permission to execute Xvfb: ${resolved}`);
56
+ }
57
+ return resolved;
55
58
  }
56
- execute_xvfb() {
59
+ /**
60
+ * Launch Xvfb with -displayfd 3 so the kernel/Xvfb itself picks a free
61
+ * display number atomically and reports it back to us. Avoids userspace race conditions
62
+ */
63
+ spawnXvfb() {
64
+ const xvfbPath = this.xvfb_path;
65
+ const cmd = [xvfbPath, "-displayfd", "3", ...this.xvfb_args];
57
66
  if (this.debug) {
58
- console.log("Starting virtual display:", this.xvfb_cmd.join(" "));
67
+ console.log("Starting virtual display:", cmd.join(" "));
59
68
  }
60
- this.proc = spawn(this.xvfb_cmd[0], this.xvfb_cmd.slice(1), {
61
- stdio: this.debug ? "inherit" : "ignore",
69
+ // Force Mesa software GLX to avoid GPU contention delays, we don't use the GPU anyways
70
+ return spawn(cmd[0], cmd.slice(1), {
71
+ stdio: [
72
+ "ignore",
73
+ this.debug ? "inherit" : "ignore",
74
+ this.debug ? "inherit" : "ignore",
75
+ "pipe", // fd 3 — Xvfb writes "<display>\n" here
76
+ ],
62
77
  detached: true,
78
+ env: {
79
+ ...process.env,
80
+ __GLX_VENDOR_LIBRARY_NAME: "mesa",
81
+ LIBGL_ALWAYS_SOFTWARE: "1",
82
+ },
63
83
  });
64
84
  }
65
- get() {
85
+ async get() {
66
86
  VirtualDisplay.assert_linux();
67
- // this._lock.runExclusive(() => {
68
87
  if (!this.proc) {
69
- this.execute_xvfb();
88
+ this.proc = this.spawnXvfb();
89
+ const stream = this.proc.stdio[3];
90
+ const timer = setTimeout(() => stream.destroy(new CannotExecuteXvfb(`Xvfb did not report a display within ${DISPLAYFD_READ_TIMEOUT_MS}ms`)), DISPLAYFD_READ_TIMEOUT_MS);
91
+ let buf = "";
92
+ try {
93
+ for await (const chunk of stream) {
94
+ buf += chunk;
95
+ if (buf.includes("\n"))
96
+ break;
97
+ }
98
+ }
99
+ catch (err) {
100
+ this.kill();
101
+ throw err;
102
+ }
103
+ finally {
104
+ clearTimeout(timer);
105
+ }
106
+ const n = Number.parseInt(buf, 10);
107
+ if (!Number.isFinite(n)) {
108
+ this.kill();
109
+ throw new CannotExecuteXvfb(`Xvfb did not report a display (got ${JSON.stringify(buf)}, exit=${this.proc.exitCode})`);
110
+ }
111
+ this._display = n;
70
112
  }
71
113
  else if (this.debug) {
72
- console.log(`Using virtual display: ${this.display}`);
114
+ console.log(`Using virtual display: ${this._display}`);
73
115
  }
74
- // });
75
- return `:${this.display}`;
116
+ return `:${this._display}`;
76
117
  }
77
118
  kill() {
78
- // this._lock.runExclusive(() => {
79
- if (this.proc && !this.proc.killed) {
119
+ if (this.proc && this.proc.exitCode === null && !this.proc.killed) {
80
120
  if (this.debug) {
81
- console.log("Terminating virtual display:", this.display);
121
+ console.log("Terminating virtual display:", this._display);
82
122
  }
83
123
  this.proc.kill();
84
124
  }
85
- // });
86
- }
87
- /**
88
- * Get list of lock files in /tmp
89
- * @returns List of lock file paths
90
- */
91
- static _get_lock_files() {
92
- const tmpd = process.env.TMPDIR || tmpdir();
93
- try {
94
- return globSync(path.join(tmpd, ".X*-lock")).filter((p) => {
95
- try {
96
- return statSync(p).isFile();
97
- }
98
- catch {
99
- return false;
100
- }
101
- });
102
- }
103
- catch {
104
- return [];
105
- }
106
- }
107
- static _free_display() {
108
- const ls = VirtualDisplay._get_lock_files().map((x) => parseInt(x.split("X")[1].split("-")[0], 10));
109
- return ls.length ? Math.max(99, Math.max(...ls) + randomInt(3, 20)) : 99;
110
- }
111
- get display() {
112
- if (this._display === null) {
113
- this._display = VirtualDisplay._free_display();
114
- }
115
- return this._display;
116
125
  }
117
126
  static assert_linux() {
118
127
  if (OS_NAME !== "lin") {
package/package.json CHANGED
@@ -1,62 +1,68 @@
1
1
  {
2
- "name": "camoufox-js",
3
- "version": "0.10.2",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "scripts": {
7
- "build": "rimraf dist && tsc && npm run copy-files",
8
- "test": "vitest",
9
- "copy-files": "cp -r src/data-files dist/data-files",
10
- "check": "biome check"
11
- },
12
- "bin": "dist/__main__.js",
13
- "files": [
14
- "dist"
15
- ],
16
- "type": "module",
17
- "keywords": [],
18
- "author": "",
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/apify/camoufox-js"
22
- },
23
- "bugs": {
24
- "url": "https://github.com/apify/camoufox-js/issues"
25
- },
26
- "engines": {
27
- "node": ">= 20"
28
- },
29
- "license": "MPL-2.0",
30
- "description": "Experimental JS port of Camoufox for Python.",
31
- "packageManager": "yarn@4.13.0",
32
- "dependencies": {
33
- "adm-zip": "^0.5.16",
34
- "better-sqlite3": "^12.2.0",
35
- "cli-progress": "^3.12.0",
36
- "commander": "^14.0.0",
37
- "fingerprint-generator": "^2.1.66",
38
- "glob": "^13.0.0",
39
- "impit": "^0.13.0",
40
- "language-tags": "^2.0.1",
41
- "maxmind": "^5.0.0",
42
- "pretty-bytes": "^7.1.0",
43
- "ua-parser-js": "^2.0.2",
44
- "xml2js": "^0.6.2"
45
- },
46
- "devDependencies": {
47
- "@biomejs/biome": "2.4.10",
48
- "@types/adm-zip": "^0.5.7",
49
- "@types/better-sqlite3": "^7.6.13",
50
- "@types/cli-progress": "^3.11.6",
51
- "@types/language-tags": "^1.0.4",
52
- "@types/node": "^24.0.0",
53
- "@types/xml2js": "^0.4.14",
54
- "playwright-core": "^1.53.1",
55
- "rimraf": "^6.0.1",
56
- "typescript": "^5.8.3",
57
- "vitest": "^4.0.0"
58
- },
59
- "peerDependencies": {
60
- "playwright-core": "*"
61
- }
62
- }
2
+ "name": "camoufox-js",
3
+ "version": "0.11.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "bin": "dist/__main__.js",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "type": "module",
11
+ "keywords": [],
12
+ "author": "",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/apify/camoufox-js"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/apify/camoufox-js/issues"
19
+ },
20
+ "engines": {
21
+ "node": ">= 22"
22
+ },
23
+ "license": "MPL-2.0",
24
+ "description": "Experimental JS port of Camoufox for Python.",
25
+ "dependencies": {
26
+ "adm-zip": "^0.5.16",
27
+ "better-sqlite3": "^12.10.0",
28
+ "cli-progress": "^3.12.0",
29
+ "commander": "^14.0.0",
30
+ "fingerprint-generator": "^2.1.66",
31
+ "glob": "^13.0.0",
32
+ "impit": "^0.13.0",
33
+ "language-tags": "^2.0.1",
34
+ "maxmind": "^5.0.0",
35
+ "pretty-bytes": "^7.1.0",
36
+ "ua-parser-js": "^2.0.2",
37
+ "xml2js": "^0.6.2"
38
+ },
39
+ "devDependencies": {
40
+ "@biomejs/biome": "2.4.10",
41
+ "@types/adm-zip": "^0.5.7",
42
+ "@types/better-sqlite3": "^7.6.13",
43
+ "@types/cli-progress": "^3.11.6",
44
+ "@types/language-tags": "^1.0.4",
45
+ "@types/node": "^24.0.0",
46
+ "@types/xml2js": "^0.4.14",
47
+ "playwright-core": "^1.53.1",
48
+ "rimraf": "^6.0.1",
49
+ "typescript": "^5.8.3",
50
+ "vitest": "^4.0.0"
51
+ },
52
+ "peerDependencies": {
53
+ "playwright-core": "*"
54
+ },
55
+ "devEngines": {
56
+ "packageManager": {
57
+ "name": "pnpm",
58
+ "version": "10.33.4",
59
+ "onFail": "warn"
60
+ }
61
+ },
62
+ "scripts": {
63
+ "build": "rimraf dist && tsc && pnpm copy-files",
64
+ "test": "vitest",
65
+ "copy-files": "cp -r src/data-files dist/data-files",
66
+ "check": "biome check"
67
+ }
68
+ }