camoufox-js 0.11.0 → 0.11.2
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 +11 -0
- package/dist/ip.js +26 -5
- package/dist/pkgman.d.ts +6 -0
- package/dist/pkgman.js +30 -6
- package/dist/utils.js +28 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,6 +8,17 @@ This is the JavaScript client for Camoufox. It is a port of the Python wrapper (
|
|
|
8
8
|
npm install camoufox-js
|
|
9
9
|
```
|
|
10
10
|
|
|
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
|
|
13
|
+
`CAMOUFOX_INSTALL_DIR` environment variable to install and resolve it from a
|
|
14
|
+
custom location instead — useful in containers and CI images where the home
|
|
15
|
+
directory is ephemeral or persisted separately (similar to Playwright's
|
|
16
|
+
`PLAYWRIGHT_BROWSERS_PATH`):
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
CAMOUFOX_INSTALL_DIR=/opt/camoufox npx camoufox-js fetch
|
|
20
|
+
```
|
|
21
|
+
|
|
11
22
|
## Usage
|
|
12
23
|
|
|
13
24
|
You can launch Playwright-controlled Camoufox using this package like this:
|
package/dist/ip.js
CHANGED
|
@@ -55,6 +55,31 @@ export function validateIP(ip) {
|
|
|
55
55
|
throw new InvalidIP(`Invalid IP address: ${ip}`);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
+
// Impit has no close/dispose API: each instance's native client (Tokio
|
|
59
|
+
// runtime resources, one UDP resolver socket) is only reclaimed when the
|
|
60
|
+
// JS wrapper is GC'd — and V8 rarely collects the tiny wrappers, so
|
|
61
|
+
// per-call instances leak fds in long-running processes. Reuse instances
|
|
62
|
+
// via a small LRU keyed by proxy URL; evicted entries are reclaimed by GC.
|
|
63
|
+
const IMPIT_CACHE_MAX = 8;
|
|
64
|
+
const impitCache = new Map();
|
|
65
|
+
function getImpit(proxy) {
|
|
66
|
+
const key = proxy;
|
|
67
|
+
const cached = impitCache.get(key);
|
|
68
|
+
if (cached) {
|
|
69
|
+
impitCache.delete(key);
|
|
70
|
+
impitCache.set(key, cached);
|
|
71
|
+
return cached;
|
|
72
|
+
}
|
|
73
|
+
const impit = new Impit({
|
|
74
|
+
proxyUrl: proxy,
|
|
75
|
+
timeout: 5000,
|
|
76
|
+
});
|
|
77
|
+
impitCache.set(key, impit);
|
|
78
|
+
if (impitCache.size > IMPIT_CACHE_MAX) {
|
|
79
|
+
impitCache.delete(impitCache.keys().next().value);
|
|
80
|
+
}
|
|
81
|
+
return impit;
|
|
82
|
+
}
|
|
58
83
|
export async function publicIP(proxy) {
|
|
59
84
|
const URLS = [
|
|
60
85
|
"https://api.ipify.org",
|
|
@@ -67,11 +92,7 @@ export async function publicIP(proxy) {
|
|
|
67
92
|
const errors = [];
|
|
68
93
|
for (const url of URLS) {
|
|
69
94
|
try {
|
|
70
|
-
const
|
|
71
|
-
proxyUrl: proxy,
|
|
72
|
-
timeout: 5000,
|
|
73
|
-
});
|
|
74
|
-
const response = await impit.fetch(url);
|
|
95
|
+
const response = await getImpit(proxy).fetch(url);
|
|
75
96
|
if (!response.ok) {
|
|
76
97
|
continue;
|
|
77
98
|
}
|
package/dist/pkgman.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { PathLike } from "node:fs";
|
|
2
2
|
import type { Writable } from "node:stream";
|
|
3
3
|
export declare const OS_NAME: "mac" | "win" | "lin";
|
|
4
|
+
/**
|
|
5
|
+
* Directory the Camoufox browser is downloaded to and launched from.
|
|
6
|
+
* Defaults to the per-user cache directory; set CAMOUFOX_INSTALL_DIR to
|
|
7
|
+
* relocate it (e.g. into a container image layer when the home directory
|
|
8
|
+
* is ephemeral or persisted separately).
|
|
9
|
+
*/
|
|
4
10
|
export declare const INSTALL_DIR: PathLike;
|
|
5
11
|
export declare const LOCAL_DATA: PathLike;
|
|
6
12
|
export declare const OS_ARCH_MATRIX: {
|
package/dist/pkgman.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
3
|
import * as os from "node:os";
|
|
4
4
|
import * as path from "node:path";
|
|
@@ -35,7 +35,15 @@ if (!(process.platform in OS_MAP)) {
|
|
|
35
35
|
}
|
|
36
36
|
export const OS_NAME = OS_MAP[process.platform];
|
|
37
37
|
const currentDir = import.meta.dirname ?? path.dirname(fileURLToPath(import.meta.url));
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Directory the Camoufox browser is downloaded to and launched from.
|
|
40
|
+
* Defaults to the per-user cache directory; set CAMOUFOX_INSTALL_DIR to
|
|
41
|
+
* relocate it (e.g. into a container image layer when the home directory
|
|
42
|
+
* is ephemeral or persisted separately).
|
|
43
|
+
*/
|
|
44
|
+
export const INSTALL_DIR = process.env.CAMOUFOX_INSTALL_DIR
|
|
45
|
+
? path.resolve(process.env.CAMOUFOX_INSTALL_DIR)
|
|
46
|
+
: userCacheDir("camoufox");
|
|
39
47
|
const formatBytes = (v, _, type) => type === "total" || type === "value" ? prettyBytes(v) : String(v);
|
|
40
48
|
export const LOCAL_DATA = path.join(currentDir, "data-files");
|
|
41
49
|
export const OS_ARCH_MATRIX = {
|
|
@@ -138,6 +146,8 @@ export class GitHubDownloader {
|
|
|
138
146
|
}
|
|
139
147
|
const releases = await response.json();
|
|
140
148
|
for (const release of releases) {
|
|
149
|
+
if (release.prerelease || release.draft)
|
|
150
|
+
continue;
|
|
141
151
|
for (const asset of release.assets) {
|
|
142
152
|
const data = this.checkAsset(asset);
|
|
143
153
|
if (data) {
|
|
@@ -214,17 +224,18 @@ export class CamoufoxFetcher extends GitHubDownloader {
|
|
|
214
224
|
async install() {
|
|
215
225
|
await this.init();
|
|
216
226
|
await CamoufoxFetcher.cleanup();
|
|
227
|
+
// Set up outside the try so finally can always tear down the ~600MB staging dir.
|
|
228
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "camoufox-"));
|
|
229
|
+
const tempFilePath = path.join(tempDir, "camoufox.zip");
|
|
230
|
+
const tempFileStream = fs.createWriteStream(tempFilePath);
|
|
217
231
|
try {
|
|
218
232
|
fs.mkdirSync(INSTALL_DIR, { recursive: true });
|
|
219
|
-
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "camoufox-"));
|
|
220
|
-
const tempFilePath = path.join(tempDir, "camoufox.zip");
|
|
221
|
-
const tempFileStream = fs.createWriteStream(tempFilePath);
|
|
222
233
|
await webdl(this.url, "Downloading Camoufox...", true, tempFileStream);
|
|
223
234
|
await new Promise((r) => tempFileStream.close(r));
|
|
224
235
|
await this.extractZip(tempFilePath);
|
|
225
236
|
this.setVersion();
|
|
226
237
|
if (OS_NAME !== "win") {
|
|
227
|
-
|
|
238
|
+
execFileSync("chmod", ["-R", "755", INSTALL_DIR.toString()]);
|
|
228
239
|
}
|
|
229
240
|
console.log("Camoufox successfully installed.");
|
|
230
241
|
}
|
|
@@ -233,6 +244,19 @@ export class CamoufoxFetcher extends GitHubDownloader {
|
|
|
233
244
|
await CamoufoxFetcher.cleanup();
|
|
234
245
|
throw e;
|
|
235
246
|
}
|
|
247
|
+
finally {
|
|
248
|
+
// Best-effort teardown: a throw here would mask the real result, and the caller is fire-and-forget.
|
|
249
|
+
try {
|
|
250
|
+
// Close before removing: Windows can't unlink a file with an open handle.
|
|
251
|
+
if (!tempFileStream.closed) {
|
|
252
|
+
await new Promise((resolve) => tempFileStream.close(() => resolve()));
|
|
253
|
+
}
|
|
254
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
255
|
+
}
|
|
256
|
+
catch (cleanupErr) {
|
|
257
|
+
console.error(`Failed to remove staging dir ${tempDir}: ${cleanupErr}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
236
260
|
}
|
|
237
261
|
get url() {
|
|
238
262
|
if (!this._url) {
|
package/dist/utils.js
CHANGED
|
@@ -218,11 +218,15 @@ async function _asyncAttachVD(browser, virtualDisplay) {
|
|
|
218
218
|
if (!virtualDisplay) {
|
|
219
219
|
return browser;
|
|
220
220
|
}
|
|
221
|
-
const originalClose = browser.close;
|
|
221
|
+
const originalClose = browser.close.bind(browser);
|
|
222
222
|
browser.close = async (...args) => {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
223
|
+
try {
|
|
224
|
+
return await originalClose(...args);
|
|
225
|
+
}
|
|
226
|
+
finally {
|
|
227
|
+
if (virtualDisplay) {
|
|
228
|
+
virtualDisplay.kill();
|
|
229
|
+
}
|
|
226
230
|
}
|
|
227
231
|
};
|
|
228
232
|
browser._virtualDisplay = virtualDisplay;
|
|
@@ -236,11 +240,15 @@ export function syncAttachVD(browser, virtualDisplay) {
|
|
|
236
240
|
// Skip if no virtual display is provided
|
|
237
241
|
return browser;
|
|
238
242
|
}
|
|
239
|
-
const originalClose = browser.close;
|
|
240
|
-
browser.close = (...args) => {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
243
|
+
const originalClose = browser.close.bind(browser);
|
|
244
|
+
browser.close = async (...args) => {
|
|
245
|
+
try {
|
|
246
|
+
return await originalClose(...args);
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
if (virtualDisplay) {
|
|
250
|
+
virtualDisplay.kill();
|
|
251
|
+
}
|
|
244
252
|
}
|
|
245
253
|
};
|
|
246
254
|
browser._virtualDisplay = virtualDisplay;
|
|
@@ -356,6 +364,17 @@ export async function launchOptions({ config, os, block_images, block_webrtc, bl
|
|
|
356
364
|
}
|
|
357
365
|
// Inject the fingerprint into the config
|
|
358
366
|
mergeInto(config, fromBrowserforge(fingerprint, ff_version_str));
|
|
367
|
+
// Add seeds (BrowserForge doesn't generate these). Mirrors fingerprints.py,
|
|
368
|
+
// which seeds these right after from_browserforge() with setdefault. Range is
|
|
369
|
+
// 1..2^32-1 — 0 is excluded because it's a no-op in the C++ managers. Without
|
|
370
|
+
// a per-launch audio:seed the AudioFingerprintManager defaults to 0, so every
|
|
371
|
+
// spoofed context returns identical audio samples — a "same machine behind
|
|
372
|
+
// many identities" tell on CreepJS. setInto is "set only if unset", so a
|
|
373
|
+
// caller-supplied seed wins (the JS equivalent of setdefault).
|
|
374
|
+
const randint = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
|
|
375
|
+
setInto(config, "fonts:spacing_seed", randint(1, 4_294_967_295));
|
|
376
|
+
setInto(config, "audio:seed", randint(1, 4_294_967_295));
|
|
377
|
+
setInto(config, "canvas:seed", randint(1, 4_294_967_295));
|
|
359
378
|
const targetOS = getTargetOS(config);
|
|
360
379
|
// Set a random window.history.length
|
|
361
380
|
setInto(config, "window.history.length", Math.floor(Math.random() * 5) + 1);
|
|
@@ -377,8 +396,6 @@ export async function launchOptions({ config, os, block_images, block_webrtc, bl
|
|
|
377
396
|
else {
|
|
378
397
|
updateFonts(config, targetOS);
|
|
379
398
|
}
|
|
380
|
-
// Set a fixed font spacing seed
|
|
381
|
-
setInto(config, "fonts:spacing_seed", Math.floor(Math.random() * 1_073_741_824));
|
|
382
399
|
// Handle proxy
|
|
383
400
|
const proxyUrl = getProxyUrl(proxy);
|
|
384
401
|
// Set geolocation
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "camoufox-js",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"bin": "dist/__main__.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"commander": "^14.0.0",
|
|
30
30
|
"fingerprint-generator": "^2.1.66",
|
|
31
31
|
"glob": "^13.0.0",
|
|
32
|
-
"impit": "^0.
|
|
32
|
+
"impit": "^0.14.1",
|
|
33
33
|
"language-tags": "^2.0.1",
|
|
34
34
|
"maxmind": "^5.0.0",
|
|
35
35
|
"pretty-bytes": "^7.1.0",
|