headfox-js 0.1.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 (42) hide show
  1. package/LICENSE.md +373 -0
  2. package/README.md +121 -0
  3. package/bin/headfox-js.mjs +17 -0
  4. package/dist/__main__.d.ts +2 -0
  5. package/dist/__main__.js +131 -0
  6. package/dist/__version__.d.ts +8 -0
  7. package/dist/__version__.js +10 -0
  8. package/dist/addons.d.ts +17 -0
  9. package/dist/addons.js +74 -0
  10. package/dist/data-files/territoryInfo.xml +2024 -0
  11. package/dist/data-files/webgl_data.db +0 -0
  12. package/dist/exceptions.d.ts +82 -0
  13. package/dist/exceptions.js +165 -0
  14. package/dist/fingerprints.d.ts +4 -0
  15. package/dist/fingerprints.js +82 -0
  16. package/dist/index.d.ts +4 -0
  17. package/dist/index.js +4 -0
  18. package/dist/ip.d.ts +25 -0
  19. package/dist/ip.js +90 -0
  20. package/dist/locale.d.ts +26 -0
  21. package/dist/locale.js +285 -0
  22. package/dist/mappings/browserforge.config.d.ts +47 -0
  23. package/dist/mappings/browserforge.config.js +72 -0
  24. package/dist/mappings/fonts.config.d.ts +6 -0
  25. package/dist/mappings/fonts.config.js +822 -0
  26. package/dist/mappings/warnings.config.d.ts +16 -0
  27. package/dist/mappings/warnings.config.js +27 -0
  28. package/dist/pkgman.d.ts +67 -0
  29. package/dist/pkgman.js +421 -0
  30. package/dist/server.d.ts +7 -0
  31. package/dist/server.js +24 -0
  32. package/dist/sync_api.d.ts +10 -0
  33. package/dist/sync_api.js +35 -0
  34. package/dist/utils.d.ts +109 -0
  35. package/dist/utils.js +540 -0
  36. package/dist/virtdisplay.d.ts +20 -0
  37. package/dist/virtdisplay.js +123 -0
  38. package/dist/warnings.d.ts +4 -0
  39. package/dist/warnings.js +33 -0
  40. package/dist/webgl/sample.d.ts +19 -0
  41. package/dist/webgl/sample.js +121 -0
  42. package/package.json +94 -0
@@ -0,0 +1,16 @@
1
+ declare const _default: {
2
+ navigator: string;
3
+ locale: string;
4
+ geolocation: string;
5
+ "header-ua": string;
6
+ viewport: string;
7
+ custom_fingerprint: string;
8
+ proxy_without_geoip: string;
9
+ ff_version: string;
10
+ no_region: string;
11
+ block_webgl: string;
12
+ block_images: string;
13
+ custom_fonts_only: string;
14
+ disable_coop: string;
15
+ };
16
+ export default _default;
@@ -0,0 +1,27 @@
1
+ export default {
2
+ navigator: "Manually setting navigator properties is not recommended." +
3
+ " Device information is automatically generated within Headfox" +
4
+ " based on the provided `os`.",
5
+ locale: "Use the `locale` parameter in Headfox instead of setting the config manually.",
6
+ geolocation: "Please use the `geoip` parameter in Headfox instead of setting your geolocation manually." +
7
+ " This can lead to detection if your target geolocation does not match your IP." +
8
+ " Pass `geoip=True` or a target IP (ex: geoip='123.45.67.89') to let Headfox populate this data for you.",
9
+ "header-ua": "Do not set the header.User-Agent manually. Headfox will generate a User-Agent for you.",
10
+ viewport: "Manually setting screen & window properties is not recommended." +
11
+ " Screen dimensions are randomly generated within Headfox" +
12
+ " based on the provided screen constraints.",
13
+ custom_fingerprint: "Passing your own fingerprint is not recommended." +
14
+ " BrowserForge fingerprints are automatically generated within Headfox" +
15
+ " based on the provided `os` and `screen` constraints.",
16
+ proxy_without_geoip: "When using a proxy, it is heavily recommended that you pass `geoip=True`.",
17
+ ff_version: "Spoofing the Firefox version will likely lead to detection." +
18
+ " If rotating the Firefox version is absolutely necessary, it would be more advisable to" +
19
+ " rotate between older versions of Headfox instead.",
20
+ no_region: "Because you did not pass in a locale region, Headfox will generate one for you." +
21
+ " This can cause suspicion if your IP does not match your locale region.",
22
+ block_webgl: "Disabling WebGL is not recommended. Many WAFs will check if WebGL is enabled.",
23
+ block_images: "Blocking image requests has been reported to cause detection issues on major WAFs.",
24
+ custom_fonts_only: "Disabling OS-specific fonts while spoofing your OS will make your browser fingerprint inconsistent." +
25
+ " WAFs can detect this mismatch between your claimed OS and available system fonts.",
26
+ disable_coop: "Disabling Cross-Origin-Opener-Policy (COOP) handling can potentially be detected by sophisticated WAFs.",
27
+ };
@@ -0,0 +1,67 @@
1
+ import type { PathLike } from "node:fs";
2
+ import type { Writable } from "node:stream";
3
+ export declare const OS_NAME: "mac" | "win" | "lin";
4
+ export declare const INSTALL_DIR: string;
5
+ export declare const LOCAL_DATA: PathLike;
6
+ export declare const OS_ARCH_MATRIX: {
7
+ [key: string]: string[];
8
+ };
9
+ declare class Version {
10
+ release: string;
11
+ version?: string;
12
+ sorted_rel: number[];
13
+ constructor(release: string, version?: string);
14
+ private buildSortedRel;
15
+ get fullString(): string;
16
+ equals(other: Version): boolean;
17
+ lessThan(other: Version): boolean;
18
+ isSupported(): boolean;
19
+ static fromPath(filePath?: PathLike): Version;
20
+ static isSupportedPath(path: PathLike): boolean;
21
+ static buildMinMax(): [Version, Version];
22
+ }
23
+ export declare class GitHubDownloader {
24
+ githubRepo: string;
25
+ apiUrl: string;
26
+ constructor(githubRepo: string);
27
+ checkAsset(asset: any): any;
28
+ missingAssetError(): void;
29
+ getAsset({ retries }?: {
30
+ retries: number;
31
+ }): Promise<any>;
32
+ }
33
+ export declare class HeadfoxFetcher extends GitHubDownloader {
34
+ arch: string;
35
+ _version_obj?: Version;
36
+ pattern: RegExp;
37
+ _url?: string;
38
+ constructor();
39
+ init(): Promise<void>;
40
+ checkAsset(asset: any): [Version, string] | null;
41
+ missingAssetError(): void;
42
+ static getPlatformArch(): string;
43
+ fetchLatest(): Promise<void>;
44
+ static downloadFile(url: string): Promise<Buffer>;
45
+ extractZip(zipFile: string | Buffer): Promise<void>;
46
+ static cleanup(): boolean;
47
+ setVersion(): void;
48
+ install(): Promise<void>;
49
+ get url(): string;
50
+ get version(): string;
51
+ get release(): string;
52
+ get verstr(): string;
53
+ }
54
+ export declare function installedVerStr(): string;
55
+ export declare function isBrowserInstalled(): boolean;
56
+ export declare function ensureBrowserInstalled(): Promise<string>;
57
+ export declare function getInstalledBrowserDir(): string;
58
+ export declare function headfoxPath(downloadIfMissing?: boolean): string;
59
+ export declare const camoufoxPath: typeof headfoxPath;
60
+ export declare const CamoufoxFetcher: typeof HeadfoxFetcher;
61
+ export declare function getPath(file: string): string;
62
+ export declare function launchPath(): string;
63
+ export declare function webdl(url: string, desc?: string, bar?: boolean, buffer?: Writable | null, { retries }?: {
64
+ retries: number;
65
+ }): Promise<Buffer>;
66
+ export declare function unzip(zipFile: Buffer, extractPath: string, desc?: string, bar?: boolean): Promise<void>;
67
+ export {};
package/dist/pkgman.js ADDED
@@ -0,0 +1,421 @@
1
+ import * as fs from "node:fs";
2
+ import { chmod, readdir } from "node:fs/promises";
3
+ import * as os from "node:os";
4
+ import * as path from "node:path";
5
+ import { setTimeout } from "node:timers/promises";
6
+ import { fileURLToPath } from "node:url";
7
+ import AdmZip from "adm-zip";
8
+ import cliProgress from "cli-progress";
9
+ import prettyBytes from "pretty-bytes";
10
+ import { CONSTRAINTS } from "./__version__.js";
11
+ import { FileNotFoundError, HeadfoxNotInstalled, MissingRelease, UnsupportedArchitecture, UnsupportedOS, UnsupportedVersion, } from "./exceptions.js";
12
+ const ARCH_MAP = {
13
+ x64: "x86_64",
14
+ ia32: "i686",
15
+ arm64: "arm64",
16
+ arm: "arm64",
17
+ };
18
+ const OS_MAP = {
19
+ darwin: "mac",
20
+ linux: "lin",
21
+ win32: "win",
22
+ };
23
+ if (!(process.platform in OS_MAP)) {
24
+ throw new UnsupportedOS(`OS ${process.platform} is not supported`);
25
+ }
26
+ export const OS_NAME = OS_MAP[process.platform];
27
+ const RELEASE_REPO = process.env.HEADFOX_JS_RELEASE_REPO ??
28
+ process.env.CAMOUFOX_JS_RELEASE_REPO ??
29
+ "CloverLabsAI/camoufox";
30
+ const CACHE_DIR_NAME = process.env.HEADFOX_JS_CACHE_DIR ??
31
+ process.env.CAMOUFOX_JS_CACHE_DIR ??
32
+ "headfox";
33
+ const ASSET_PREFIXES = Array.from(new Set((process.env.HEADFOX_JS_ASSET_PREFIXES ??
34
+ process.env.CAMOUFOX_JS_ASSET_PREFIXES ??
35
+ "camoufox,headfox")
36
+ .split(",")
37
+ .map((value) => value.trim())
38
+ .filter(Boolean)));
39
+ const APP_BUNDLE_CANDIDATES = ["Headfox.app", "Camoufox.app"];
40
+ const currentDir = import.meta.dirname ?? path.dirname(fileURLToPath(import.meta.url));
41
+ export const INSTALL_DIR = userCacheDir(CACHE_DIR_NAME);
42
+ const formatBytes = (v, _, type) => type === "total" || type === "value" ? prettyBytes(v) : String(v);
43
+ export const LOCAL_DATA = path.join(currentDir, "data-files");
44
+ export const OS_ARCH_MATRIX = {
45
+ win: ["x86_64", "i686"],
46
+ mac: ["x86_64", "arm64"],
47
+ lin: ["x86_64", "arm64", "i686"],
48
+ };
49
+ const LAUNCH_FILE_CANDIDATES = {
50
+ win: ["headfox.exe", "camoufox.exe"],
51
+ mac: ["../MacOS/headfox", "../MacOS/camoufox"],
52
+ lin: ["headfox-bin", "camoufox-bin"],
53
+ };
54
+ function installMissingMessage(product = "Headfox") {
55
+ return `${product} is not installed at ${INSTALL_DIR}. Please run \`headfox-js fetch\` to install.`;
56
+ }
57
+ function isInstallDirReady() {
58
+ return fs.existsSync(INSTALL_DIR) && fs.readdirSync(INSTALL_DIR).length > 0;
59
+ }
60
+ async function chmodTree(targetPath, mode) {
61
+ await chmod(targetPath, mode);
62
+ const entries = await readdir(targetPath, { withFileTypes: true });
63
+ for (const entry of entries) {
64
+ const nextPath = path.join(targetPath, entry.name);
65
+ if (entry.isDirectory()) {
66
+ await chmodTree(nextPath, mode);
67
+ continue;
68
+ }
69
+ await chmod(nextPath, mode);
70
+ }
71
+ }
72
+ function resolveMacAppBundle(installDir) {
73
+ return (APP_BUNDLE_CANDIDATES.find((candidate) => fs.existsSync(path.join(installDir, candidate))) ?? APP_BUNDLE_CANDIDATES[0]);
74
+ }
75
+ function resolveLaunchBinary(installDir) {
76
+ const candidates = LAUNCH_FILE_CANDIDATES[OS_NAME].map((launchFile) => {
77
+ if (OS_NAME === "mac") {
78
+ return path.resolve(installDir, resolveMacAppBundle(installDir), "Contents", "Resources", launchFile);
79
+ }
80
+ return path.join(installDir, launchFile);
81
+ });
82
+ return candidates.find((candidate) => fs.existsSync(candidate)) ?? null;
83
+ }
84
+ class Version {
85
+ release;
86
+ version;
87
+ sorted_rel;
88
+ constructor(release, version) {
89
+ this.release = release;
90
+ this.version = version;
91
+ this.sorted_rel = this.buildSortedRel();
92
+ }
93
+ buildSortedRel() {
94
+ const parts = this.release
95
+ .split(".")
96
+ .map((x) => Number.isNaN(Number(x)) ? x.charCodeAt(0) - 1024 : Number(x));
97
+ while (parts.length < 5) {
98
+ parts.push(0);
99
+ }
100
+ return parts;
101
+ }
102
+ get fullString() {
103
+ return `${this.version}-${this.release}`;
104
+ }
105
+ equals(other) {
106
+ return this.sorted_rel.join(".") === other.sorted_rel.join(".");
107
+ }
108
+ lessThan(other) {
109
+ for (let i = 0; i < this.sorted_rel.length; i++) {
110
+ if (this.sorted_rel[i] < other.sorted_rel[i])
111
+ return true;
112
+ if (this.sorted_rel[i] > other.sorted_rel[i])
113
+ return false;
114
+ }
115
+ return false;
116
+ }
117
+ isSupported() {
118
+ return VERSION_MIN.lessThan(this) && this.lessThan(VERSION_MAX);
119
+ }
120
+ static fromPath(filePath = INSTALL_DIR) {
121
+ const versionPath = path.join(filePath.toString(), "version.json");
122
+ if (!fs.existsSync(versionPath)) {
123
+ throw new FileNotFoundError(`Version information not found at ${versionPath}. Please run \`headfox-js fetch\` to install.`);
124
+ }
125
+ const versionData = JSON.parse(fs.readFileSync(versionPath, "utf-8"));
126
+ return new Version(versionData.release, versionData.version);
127
+ }
128
+ static isSupportedPath(path) {
129
+ return Version.fromPath(path).isSupported();
130
+ }
131
+ static buildMinMax() {
132
+ return [
133
+ new Version(CONSTRAINTS.MIN_VERSION),
134
+ new Version(CONSTRAINTS.MAX_VERSION),
135
+ ];
136
+ }
137
+ }
138
+ const [VERSION_MIN, VERSION_MAX] = Version.buildMinMax();
139
+ export class GitHubDownloader {
140
+ githubRepo;
141
+ apiUrl;
142
+ constructor(githubRepo) {
143
+ this.githubRepo = githubRepo;
144
+ this.apiUrl = `https://api.github.com/repos/${githubRepo}/releases`;
145
+ }
146
+ checkAsset(asset) {
147
+ return asset.browser_download_url;
148
+ }
149
+ missingAssetError() {
150
+ throw new MissingRelease(`Could not find a release asset in ${this.githubRepo}.`);
151
+ }
152
+ async getAsset({ retries } = { retries: 5 }) {
153
+ let attempts = 0;
154
+ let response;
155
+ while (attempts < retries) {
156
+ try {
157
+ response = await fetch(this.apiUrl);
158
+ if (response.ok)
159
+ break;
160
+ }
161
+ catch (e) {
162
+ console.error(e, `retrying (${attempts + 1}/${retries})...`);
163
+ await setTimeout(5e3);
164
+ }
165
+ attempts++;
166
+ }
167
+ if (!response || !response.ok) {
168
+ throw new Error(`Failed to fetch releases from ${this.apiUrl} after ${retries} attempts`);
169
+ }
170
+ const releases = await response.json();
171
+ for (const release of releases) {
172
+ for (const asset of release.assets) {
173
+ const data = this.checkAsset(asset);
174
+ if (data) {
175
+ return data;
176
+ }
177
+ }
178
+ }
179
+ this.missingAssetError();
180
+ }
181
+ }
182
+ export class HeadfoxFetcher extends GitHubDownloader {
183
+ arch;
184
+ _version_obj;
185
+ pattern;
186
+ _url;
187
+ constructor() {
188
+ super(RELEASE_REPO);
189
+ this.arch = HeadfoxFetcher.getPlatformArch();
190
+ this.pattern = new RegExp(`(?:${ASSET_PREFIXES.join("|")})-(.+)-(.+)-${OS_NAME}\\.${this.arch}\\.zip`);
191
+ }
192
+ async init() {
193
+ await this.fetchLatest();
194
+ }
195
+ checkAsset(asset) {
196
+ const match = asset.name.match(this.pattern);
197
+ if (!match)
198
+ return null;
199
+ const version = new Version(match[2], match[1]);
200
+ if (!version.isSupported())
201
+ return null;
202
+ return [version, asset.browser_download_url];
203
+ }
204
+ missingAssetError() {
205
+ throw new MissingRelease(`No matching release found for ${OS_NAME} ${this.arch} in the supported range: (${CONSTRAINTS.asRange()}). Please update the library.`);
206
+ }
207
+ static getPlatformArch() {
208
+ const platArch = os.arch().toLowerCase();
209
+ if (!(platArch in ARCH_MAP)) {
210
+ throw new UnsupportedArchitecture(`Architecture ${platArch} is not supported`);
211
+ }
212
+ const arch = ARCH_MAP[platArch];
213
+ if (!OS_ARCH_MATRIX[OS_NAME].includes(arch)) {
214
+ throw new UnsupportedArchitecture(`Architecture ${arch} is not supported for ${OS_NAME}`);
215
+ }
216
+ return arch;
217
+ }
218
+ async fetchLatest() {
219
+ if (this._version_obj)
220
+ return;
221
+ const releaseData = await this.getAsset();
222
+ this._version_obj = releaseData[0];
223
+ this._url = releaseData[1];
224
+ }
225
+ static async downloadFile(url) {
226
+ const response = await fetch(url);
227
+ return Buffer.from(await response.arrayBuffer());
228
+ }
229
+ async extractZip(zipFile) {
230
+ const zip = new AdmZip(zipFile);
231
+ zip.extractAllTo(INSTALL_DIR.toString(), true);
232
+ }
233
+ static cleanup() {
234
+ if (fs.existsSync(INSTALL_DIR)) {
235
+ fs.rmSync(INSTALL_DIR, { recursive: true });
236
+ return true;
237
+ }
238
+ return false;
239
+ }
240
+ setVersion() {
241
+ fs.writeFileSync(path.join(INSTALL_DIR.toString(), "version.json"), JSON.stringify({ version: this.version, release: this.release }));
242
+ }
243
+ async install() {
244
+ await this.init();
245
+ HeadfoxFetcher.cleanup();
246
+ try {
247
+ fs.mkdirSync(INSTALL_DIR, { recursive: true });
248
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "headfox-"));
249
+ const tempFilePath = path.join(tempDir, "headfox.zip");
250
+ const tempFileStream = fs.createWriteStream(tempFilePath);
251
+ await webdl(this.url, "Downloading Headfox...", true, tempFileStream);
252
+ await new Promise((r) => tempFileStream.close(r));
253
+ await this.extractZip(tempFilePath);
254
+ this.setVersion();
255
+ if (OS_NAME !== "win") {
256
+ await chmodTree(INSTALL_DIR, 0o755);
257
+ }
258
+ console.log("Headfox successfully installed.");
259
+ }
260
+ catch (e) {
261
+ console.error(`Error installing Headfox: ${e}`);
262
+ HeadfoxFetcher.cleanup();
263
+ throw e;
264
+ }
265
+ }
266
+ get url() {
267
+ if (!this._url) {
268
+ throw new Error("Url is not available. Make sure to run fetchLatest first.");
269
+ }
270
+ return this._url;
271
+ }
272
+ get version() {
273
+ if (!this._version_obj || !this._version_obj.version) {
274
+ throw new Error("Version is not available. Make sure to run fetchLatest first.");
275
+ }
276
+ return this._version_obj.version;
277
+ }
278
+ get release() {
279
+ if (!this._version_obj) {
280
+ throw new Error("Release information is not available. Make sure to run the installation first.");
281
+ }
282
+ return this._version_obj.release;
283
+ }
284
+ get verstr() {
285
+ if (!this._version_obj) {
286
+ throw new Error("Version is not available. Make sure to run the installation first.");
287
+ }
288
+ return this._version_obj.fullString;
289
+ }
290
+ }
291
+ function userCacheDir(appName) {
292
+ if (OS_NAME === "win") {
293
+ return path.join(os.homedir(), "AppData", "Local", appName, appName, "Cache");
294
+ }
295
+ else if (OS_NAME === "mac") {
296
+ return path.join(os.homedir(), "Library", "Caches", appName);
297
+ }
298
+ else {
299
+ return path.join(os.homedir(), ".cache", appName);
300
+ }
301
+ }
302
+ export function installedVerStr() {
303
+ return Version.fromPath(getInstalledBrowserDir()).fullString;
304
+ }
305
+ export function isBrowserInstalled() {
306
+ if (!isInstallDirReady()) {
307
+ return false;
308
+ }
309
+ try {
310
+ return Version.isSupportedPath(INSTALL_DIR);
311
+ }
312
+ catch {
313
+ return false;
314
+ }
315
+ }
316
+ export async function ensureBrowserInstalled() {
317
+ if (isBrowserInstalled()) {
318
+ return INSTALL_DIR;
319
+ }
320
+ const fetcher = new HeadfoxFetcher();
321
+ await fetcher.install();
322
+ return getInstalledBrowserDir();
323
+ }
324
+ export function getInstalledBrowserDir() {
325
+ if (!isInstallDirReady()) {
326
+ throw new HeadfoxNotInstalled(installMissingMessage());
327
+ }
328
+ if (!Version.isSupportedPath(INSTALL_DIR)) {
329
+ throw new UnsupportedVersion("The installed Headfox binary is outdated.");
330
+ }
331
+ return INSTALL_DIR;
332
+ }
333
+ export function headfoxPath(downloadIfMissing = false) {
334
+ if (downloadIfMissing) {
335
+ throw new Error("headfoxPath() no longer performs background installs. Await `ensureBrowserInstalled()` or call `launchOptions()` / `Headfox()` instead.");
336
+ }
337
+ return getInstalledBrowserDir();
338
+ }
339
+ export const camoufoxPath = headfoxPath;
340
+ export const CamoufoxFetcher = HeadfoxFetcher;
341
+ export function getPath(file) {
342
+ const installDir = headfoxPath();
343
+ if (OS_NAME === "mac") {
344
+ return path.resolve(installDir, resolveMacAppBundle(installDir), "Contents", "Resources", file);
345
+ }
346
+ return path.join(installDir, file);
347
+ }
348
+ export function launchPath() {
349
+ const installDir = headfoxPath();
350
+ const executablePath = resolveLaunchBinary(installDir);
351
+ if (!executablePath) {
352
+ throw new HeadfoxNotInstalled(installMissingMessage("Headfox"));
353
+ }
354
+ return executablePath;
355
+ }
356
+ export async function webdl(url, desc = "", bar = true, buffer = null, { retries } = { retries: 5 }) {
357
+ let attempts = 0;
358
+ let response;
359
+ while (attempts < retries) {
360
+ try {
361
+ response = await fetch(url);
362
+ if (response.ok)
363
+ break;
364
+ }
365
+ catch (e) {
366
+ console.error(e, `retrying (${attempts + 1}/${retries})...`);
367
+ await setTimeout(5e3);
368
+ }
369
+ attempts++;
370
+ }
371
+ if (!response || !response.ok) {
372
+ throw new Error(`Failed to download from ${url} after ${retries} attempts`);
373
+ }
374
+ const totalSize = parseInt(response.headers.get("content-length") || "0", 10);
375
+ let progressBar = null;
376
+ if (bar && totalSize > 0) {
377
+ progressBar = new cliProgress.SingleBar({
378
+ format: `${desc} [{bar}] {percentage}% | ETA: {eta_formatted} | {value}/{total}`,
379
+ formatValue: formatBytes,
380
+ noTTYOutput: true,
381
+ }, cliProgress.Presets.shades_classic);
382
+ progressBar.start(totalSize, 0);
383
+ }
384
+ const chunks = [];
385
+ try {
386
+ if (!response.body) {
387
+ throw new Error(`Response body was empty while downloading ${desc}`);
388
+ }
389
+ for await (const chunk of response.body) {
390
+ if (buffer) {
391
+ buffer.write(chunk);
392
+ }
393
+ else {
394
+ chunks.push(chunk);
395
+ }
396
+ if (progressBar) {
397
+ progressBar.increment(chunk.length);
398
+ }
399
+ }
400
+ }
401
+ finally {
402
+ progressBar?.stop();
403
+ }
404
+ return Buffer.concat(chunks);
405
+ }
406
+ export async function unzip(zipFile, extractPath, desc, bar = true) {
407
+ const zip = new AdmZip(zipFile);
408
+ const zipEntries = zip.getEntries();
409
+ if (bar) {
410
+ console.log(desc || "Extracting files...");
411
+ }
412
+ for (const entry of zipEntries) {
413
+ if (bar) {
414
+ console.log(`Extracting ${entry.entryName}`);
415
+ }
416
+ zip.extractEntryTo(entry, extractPath, false, true);
417
+ }
418
+ if (bar) {
419
+ console.log("Extraction complete.");
420
+ }
421
+ }
@@ -0,0 +1,7 @@
1
+ import { type BrowserServer } from "playwright-core";
2
+ import { type LaunchOptions } from "./utils.js";
3
+ export declare function launchHeadfoxServer({ port, ws_path, ...options }: LaunchOptions | {
4
+ port?: number;
5
+ ws_path?: string;
6
+ }): Promise<BrowserServer>;
7
+ export declare const launchServer: typeof launchHeadfoxServer;
package/dist/server.js ADDED
@@ -0,0 +1,24 @@
1
+ import { firefox } from "playwright-core";
2
+ import { InvalidDebugPort } from "./exceptions.js";
3
+ import { launchOptions } from "./utils.js";
4
+ function assertValidServerOptions(port, wsPath) {
5
+ if (port !== undefined &&
6
+ (!Number.isInteger(port) || port < 1 || port > 65535)) {
7
+ throw new InvalidDebugPort(`Headfox server port must be an integer between 1 and 65535. Received: ${port}`);
8
+ }
9
+ if (wsPath && !wsPath.startsWith("/")) {
10
+ throw new Error(`Headfox server ws_path must start with "/". Received: ${wsPath}`);
11
+ }
12
+ }
13
+ export async function launchHeadfoxServer({ port, ws_path, ...options }) {
14
+ // Extract and normalize headless (virtual is treated as true for server mode)
15
+ const { headless, ...restOptions } = options;
16
+ const normalizedHeadless = headless === "virtual" ? true : headless;
17
+ assertValidServerOptions(port, ws_path);
18
+ return firefox.launchServer({
19
+ ...(await launchOptions({ ...restOptions, headless: normalizedHeadless })),
20
+ port,
21
+ wsPath: ws_path,
22
+ });
23
+ }
24
+ export const launchServer = launchHeadfoxServer;
@@ -0,0 +1,10 @@
1
+ import { type Browser, type BrowserContext, type BrowserType } from "playwright-core";
2
+ import { type LaunchOptions } from "./utils.js";
3
+ export declare function Camoufox<UserDataDir extends string | undefined = undefined, ReturnType = UserDataDir extends string ? BrowserContext : Browser>(launch_options?: LaunchOptions & {
4
+ user_data_dir?: UserDataDir;
5
+ }): Promise<ReturnType>;
6
+ export declare function Headfox<UserDataDir extends string | undefined = undefined, ReturnType = UserDataDir extends string ? BrowserContext : Browser>(launch_options?: LaunchOptions & {
7
+ user_data_dir?: UserDataDir;
8
+ }): Promise<ReturnType>;
9
+ export declare function NewBrowser<UserDataDir extends string | false = false, ReturnType = UserDataDir extends string ? BrowserContext : Browser>(playwright: BrowserType<Browser>, headless?: boolean | "virtual", fromOptions?: Record<string, any>, userDataDir?: UserDataDir, debug?: boolean, launch_options?: LaunchOptions): Promise<ReturnType>;
10
+ export declare function launchWithHeadfox<UserDataDir extends string | false = false, ReturnType = UserDataDir extends string ? BrowserContext : Browser>(playwright: BrowserType<Browser>, headless?: boolean | "virtual", fromOptions?: Record<string, any>, userDataDir?: UserDataDir, debug?: boolean, launch_options?: LaunchOptions): Promise<ReturnType>;
@@ -0,0 +1,35 @@
1
+ import { firefox, } from "playwright-core";
2
+ import { launchOptions, syncAttachVD } from "./utils.js";
3
+ import { VirtualDisplay } from "./virtdisplay.js";
4
+ export async function Camoufox(launch_options = {}) {
5
+ return Headfox(launch_options);
6
+ }
7
+ export async function Headfox(launch_options = {}) {
8
+ const { headless, user_data_dir, ...launchOptions } = launch_options;
9
+ return launchWithHeadfox(firefox, headless, {}, user_data_dir ?? false, false, launchOptions);
10
+ }
11
+ export async function NewBrowser(playwright, headless = false, fromOptions = {}, userDataDir = false, debug = false, launch_options = {}) {
12
+ return launchWithHeadfox(playwright, headless, fromOptions, userDataDir, debug, launch_options);
13
+ }
14
+ export async function launchWithHeadfox(playwright, headless = false, fromOptions = {}, userDataDir = false, debug = false, launch_options = {}) {
15
+ let virtualDisplay = null;
16
+ // Normalize headless to boolean and prepare options for launchOptions function
17
+ const normalizedHeadless = headless === "virtual" ? false : headless || false;
18
+ if (headless === "virtual") {
19
+ virtualDisplay = new VirtualDisplay(debug);
20
+ launch_options.virtual_display = virtualDisplay.get();
21
+ }
22
+ if (!fromOptions || Object.keys(fromOptions).length === 0) {
23
+ fromOptions = await launchOptions({
24
+ debug,
25
+ ...launch_options,
26
+ headless: normalizedHeadless,
27
+ });
28
+ }
29
+ if (typeof userDataDir === "string") {
30
+ const context = await playwright.launchPersistentContext(userDataDir, fromOptions);
31
+ return syncAttachVD(context, virtualDisplay);
32
+ }
33
+ const browser = await playwright.launch(fromOptions);
34
+ return syncAttachVD(browser, virtualDisplay);
35
+ }