camoufox-js 0.3.6 → 0.4.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/README.md CHANGED
@@ -8,8 +8,63 @@ 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
+ ## Usage
12
+
13
+ You can launch Playwright-controlled Camoufox using this package like this:
14
+
15
+ ```javascript
16
+ import { Camoufox } from 'camoufox-js';
17
+
18
+ // you might need to run `npx camoufox-js fetch` to download the browser after installing the package
19
+
20
+ const browser = await Camoufox({
21
+ // custom camoufox options
22
+ });
23
+
24
+ const page = await browser.newPage(); // `page` is a Playwright Page instance
25
+ ```
26
+
27
+ Alternatively, if you want to use additional Playwright launch options, you can launch the Camoufox instance like this:
28
+
29
+ ```javascript
30
+ import { launchOptions } from 'camoufox-js';
31
+ import { firefox } from 'playwright-core';
32
+
33
+ // you might need to run `npx camoufox-js fetch` to download the browser after installing the package
34
+
35
+ const browser = await firefox.launch({
36
+ ...await launchOptions({ /* Camoufox options */ }),
37
+ // other Playwright options, overriding the Camoufox options
38
+ });
39
+
40
+ const page = await browser.newPage(); // `page` is a Playwright Page instance
41
+ ```
42
+
43
+ ### Launching a Camoufox server
44
+
45
+ Camoufox can be ran as a remote websocket server. It can be accessed from other devices, and languages other than Python supporting the Playwright API.
46
+
47
+ ```javascript
48
+ import { launchServer } from 'camoufox-js';
49
+ import { firefox } from 'playwright-core';
50
+
51
+ // you might need to run `npx camoufox-js fetch` to download the browser after installing the package
52
+
53
+ const server = await launchServer({ port: 8888, ws_path: '/camoufox' });
54
+ const browser = await firefox.connect(server.wsEndpoint());
55
+
56
+ const page = await browser.newPage();
57
+
58
+ // ...
59
+ // Use your browser instance as usual
60
+ // ...
61
+
62
+ await browser.close();
63
+ await server.close(); // Close the server when done
64
+ ```
65
+
11
66
  ## More info
12
67
 
13
- Check out https://github.com/daijro/camoufox for more information on Camoufox.
68
+ See https://camoufox.com/ or https://github.com/daijro/camoufox for more information on Camoufox.
14
69
 
15
70
 
package/dist/__main__.js CHANGED
@@ -6,6 +6,7 @@ import { Command } from 'commander';
6
6
  import { Camoufox } from './sync_api.js';
7
7
  import { existsSync, rmSync } from 'fs';
8
8
  import { getAsBooleanFromENV } from './utils.js';
9
+ import { launchServer } from './server.js';
9
10
  class CamoufoxUpdate extends CamoufoxFetcher {
10
11
  currentVerStr;
11
12
  constructor() {
@@ -95,11 +96,15 @@ program
95
96
  }
96
97
  await page.pause();
97
98
  });
98
- // program
99
- // .command('server')
100
- // .action(() => {
101
- // launchServer();
102
- // });
99
+ program
100
+ .command('server')
101
+ .action(async () => {
102
+ const server = await launchServer({});
103
+ console.log(`Camoufox server started at ${server.wsEndpoint()}`);
104
+ console.log();
105
+ console.log(`You can connect to it using Playwright's BrowserType.connect() method.`);
106
+ console.log(`To stop the server, press Ctrl+C or close this terminal.`);
107
+ });
103
108
  program
104
109
  .command('path')
105
110
  .action(() => {
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { Camoufox, NewBrowser } from './sync_api.js';
2
2
  export { launchOptions } from './utils.js';
3
+ export { launchServer } from './server.js';
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export { Camoufox, NewBrowser } from './sync_api.js';
2
2
  export { launchOptions } from './utils.js';
3
+ export { launchServer } from './server.js';
package/dist/locale.js CHANGED
@@ -1,4 +1,4 @@
1
- import { LOCAL_DATA, GitHubDownloader, webdl } from './pkgman.js';
1
+ import { GitHubDownloader, webdl, INSTALL_DIR } from './pkgman.js';
2
2
  import { LeakWarning } from './warnings.js';
3
3
  import { InvalidLocale, MissingRelease, NotInstalledGeoIPExtra, UnknownIPLocation, UnknownLanguage, UnknownTerritory, } from './exceptions.js';
4
4
  import { validateIP } from './ip.js';
@@ -125,7 +125,7 @@ function joinUnique(seq) {
125
125
  const seen = new Set();
126
126
  return seq.filter(x => !seen.has(x) && seen.add(x)).join(', ');
127
127
  }
128
- const MMDB_FILE = path.join(LOCAL_DATA.toString(), 'GeoLite2-City.mmdb');
128
+ const MMDB_FILE = path.join(INSTALL_DIR.toString(), 'GeoLite2-City.mmdb');
129
129
  const MMDB_REPO = "P3TERX/GeoLite.mmdb";
130
130
  class MaxMindDownloader extends GitHubDownloader {
131
131
  checkAsset(asset) {
@@ -0,0 +1,6 @@
1
+ import { BrowserServer } from 'playwright-core';
2
+ import { LaunchOptions } from './utils.js';
3
+ export declare function launchServer({ port, ws_path, ...options }: LaunchOptions | {
4
+ port?: number;
5
+ ws_path?: string;
6
+ }): Promise<BrowserServer>;
package/dist/server.js ADDED
@@ -0,0 +1,9 @@
1
+ import { firefox } from 'playwright-core';
2
+ import { launchOptions } from './utils.js';
3
+ export async function launchServer({ port, ws_path, ...options }) {
4
+ return firefox.launchServer({
5
+ ...await launchOptions(options),
6
+ port,
7
+ wsPath: ws_path,
8
+ });
9
+ }
@@ -1,4 +1,6 @@
1
1
  import { Browser, BrowserContext, BrowserType } from 'playwright-core';
2
2
  import { LaunchOptions } from './utils.js';
3
- export declare function Camoufox(launch_options: LaunchOptions): Promise<BrowserContext | Browser>;
3
+ export declare function Camoufox(launch_options: LaunchOptions | {
4
+ headless?: boolean | 'virtual';
5
+ }): Promise<BrowserContext | Browser>;
4
6
  export declare function NewBrowser(playwright: BrowserType<Browser>, headless?: boolean | 'virtual', fromOptions?: Record<string, any>, persistentContext?: boolean, debug?: boolean, launch_options?: LaunchOptions): Promise<Browser | BrowserContext>;
package/dist/sync_api.js CHANGED
@@ -2,17 +2,21 @@ import { firefox } from 'playwright-core';
2
2
  import { launchOptions, syncAttachVD } from './utils.js';
3
3
  import { VirtualDisplay } from './virtdisplay.js';
4
4
  export async function Camoufox(launch_options) {
5
- return NewBrowser(firefox, false, {}, false, false, launch_options);
5
+ const { headless, ...launchOptions } = launch_options;
6
+ return NewBrowser(firefox, headless, {}, false, false, launchOptions);
6
7
  }
7
8
  export async function NewBrowser(playwright, headless = false, fromOptions = {}, persistentContext = false, debug = false, launch_options = {}) {
8
9
  let virtualDisplay = null;
9
10
  if (headless === 'virtual') {
10
11
  virtualDisplay = new VirtualDisplay(debug);
11
- launch_options['virtualDisplay'] = virtualDisplay.get();
12
- headless = false;
12
+ launch_options['virtual_display'] = virtualDisplay.get();
13
+ launch_options.headless = false;
14
+ }
15
+ else {
16
+ launch_options.headless ||= headless;
13
17
  }
14
18
  if (!fromOptions || Object.keys(fromOptions).length === 0) {
15
- fromOptions = await launchOptions({ headless, debug, ...launch_options });
19
+ fromOptions = await launchOptions({ debug, ...launch_options });
16
20
  }
17
21
  if (persistentContext) {
18
22
  const context = await playwright.launchPersistentContext('~/.crawlee/persistent-user-data-dir', fromOptions);
package/dist/utils.d.ts CHANGED
@@ -55,7 +55,6 @@ export interface LaunchOptions {
55
55
  */
56
56
  ff_version?: number;
57
57
  /** Whether to run the browser in headless mode. Defaults to `false`.
58
- * Note: On Linux, passing `headless='virtual'` will use Xvfb.
59
58
  */
60
59
  headless?: boolean;
61
60
  /** Whether to enable running scripts in the main world.
package/dist/utils.js CHANGED
@@ -221,7 +221,7 @@ async function asyncAttachVD(browser, virtualDisplay) {
221
221
  }
222
222
  const originalClose = browser.close;
223
223
  browser.close = async (...args) => {
224
- await originalClose(...args);
224
+ await originalClose.apply(browser, ...args);
225
225
  if (virtualDisplay) {
226
226
  virtualDisplay.kill();
227
227
  }
@@ -230,7 +230,6 @@ async function asyncAttachVD(browser, virtualDisplay) {
230
230
  return browser;
231
231
  }
232
232
  export function syncAttachVD(browser, virtualDisplay) {
233
- return browser;
234
233
  /**
235
234
  * Attaches the virtual display to the sync browser cleanup
236
235
  */
@@ -239,7 +238,7 @@ export function syncAttachVD(browser, virtualDisplay) {
239
238
  }
240
239
  const originalClose = browser.close;
241
240
  browser.close = (...args) => {
242
- originalClose(...args);
241
+ originalClose.apply(browser, ...args);
243
242
  if (virtualDisplay) {
244
243
  virtualDisplay.kill();
245
244
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "camoufox-js",
3
- "version": "0.3.6",
3
+ "version": "0.4.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -17,12 +17,12 @@
17
17
  "author": "",
18
18
  "license": "ISC",
19
19
  "description": "Experimental JS port of Camoufox for Python.",
20
- "packageManager": "yarn@4.6.0",
20
+ "packageManager": "yarn@4.9.2",
21
21
  "dependencies": {
22
22
  "adm-zip": "^0.5.16",
23
23
  "commander": "^13.1.0",
24
24
  "fingerprint-generator": "^2.1.66",
25
- "impit": "^0.4.6",
25
+ "impit": "^0.5.0",
26
26
  "js-yaml": "^4.1.0",
27
27
  "language-tags": "^2.0.1",
28
28
  "maxmind": "^4.3.24",