@open-wa/driver-puppeteer 5.1.0 → 5.2.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
@@ -7,9 +7,45 @@ Part of the [@open-wa v5 monorepo](https://github.com/open-wa/wa-automate-nodejs
7
7
  ## Install
8
8
 
9
9
  ```bash
10
- pnpm add @open-wa/driver-puppeteer
10
+ bun add @open-wa/driver-puppeteer
11
11
  ```
12
12
 
13
+ ## Browser setup
14
+
15
+ Startup reuses the Chrome version matched to the installed Puppeteer package,
16
+ or downloads it with progress when it is missing. Downloads go into Puppeteer's
17
+ configured cache, including `PUPPETEER_CACHE_DIR`. Concurrent open-wa starts share
18
+ one installation; an interrupted download is retried on the next start.
19
+
20
+ Use the same resolver to pre-provision a deployment cache:
21
+
22
+ ```typescript
23
+ import { ensureBrowser } from '@open-wa/driver-puppeteer';
24
+
25
+ const executablePath = await ensureBrowser({
26
+ browser: { cacheDirectory: '/app/browser-cache', download: 'auto' },
27
+ });
28
+ console.log(executablePath);
29
+ ```
30
+
31
+ Provision on the same operating system and architecture as the deployment.
32
+ Then pass `browser: { cacheDirectory: '/app/browser-cache', download: 'never' }`
33
+ to `create()` or the driver's `launch()` to forbid downloads at runtime. An
34
+ explicit `download` option takes precedence over Puppeteer's skip-download
35
+ configuration; otherwise `PUPPETEER_SKIP_DOWNLOAD` and Chrome's skip-download
36
+ setting are respected.
37
+
38
+ `executablePath` overrides managed Chrome. A missing or non-executable path
39
+ produces an error, so a typo cannot silently select a different browser.
40
+ `PUPPETEER_EXECUTABLE_PATH` is also respected. Downloads honor `HTTP_PROXY`,
41
+ `HTTPS_PROXY` and `NO_PROXY`; `browser.downloadBaseUrl` selects a Chrome for
42
+ Testing mirror.
43
+
44
+ Chrome's sandbox remains enabled by default. open-wa does not install operating
45
+ system packages. If Chrome reports a missing shared library or unavailable
46
+ sandbox, configure the host or deployment image using
47
+ [Puppeteer's troubleshooting guide](https://pptr.dev/troubleshooting).
48
+
13
49
  ## Documentation
14
50
 
15
51
  See the [docs site](https://openwa.dev).
package/dist/index.cjs CHANGED
@@ -1,4 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let node_fs = require("node:fs");
3
+ let node_fs_promises = require("node:fs/promises");
4
+ let node_os = require("node:os");
5
+ let node_path = require("node:path");
2
6
  //#region src/PuppeteerElementHandle.ts
3
7
  var PuppeteerElementHandle = class {
4
8
  constructor(element) {
@@ -402,6 +406,121 @@ var PuppeteerBrowser = class {
402
406
  }
403
407
  };
404
408
  //#endregion
409
+ //#region src/ensureBrowser.ts
410
+ async function isExecutable(path) {
411
+ try {
412
+ if (!(await (0, node_fs_promises.stat)(path)).isFile()) return false;
413
+ await (0, node_fs_promises.access)(path, node_fs.constants.X_OK);
414
+ return true;
415
+ } catch {
416
+ return false;
417
+ }
418
+ }
419
+ function report(ctx, message) {
420
+ if (ctx?.logger) ctx.logger.info(message);
421
+ else console.error(`[open-wa] ${message}`);
422
+ }
423
+ /**
424
+ * Resolve Chrome without launching it. The default revision comes from the
425
+ * installed Puppeteer package, so the same cache can be pre-provisioned during
426
+ * deployment and reused by startup with `browser.download: 'never'`.
427
+ */
428
+ async function ensureBrowser(options = {}, ctx) {
429
+ const { default: puppeteer, PUPPETEER_REVISIONS } = await import("puppeteer");
430
+ const configuration = await puppeteer.configuration();
431
+ const configuredPath = options.executablePath ?? configuration.executablePath;
432
+ if (configuredPath !== void 0) {
433
+ const executablePath = (0, node_path.resolve)(configuredPath);
434
+ if (!configuredPath || !await isExecutable(executablePath)) throw new Error(`The configured browser executable is missing or not executable: ${configuredPath || "(empty path)"}. Set executablePath (or PUPPETEER_EXECUTABLE_PATH) to the browser binary, or remove it to use managed Chrome.`);
435
+ return executablePath;
436
+ }
437
+ const { Browser, Cache, detectBrowserPlatform, install } = await import("@puppeteer/browsers");
438
+ const platform = detectBrowserPlatform();
439
+ if (!platform) throw new Error(`Automatic browser setup is unavailable on ${process.platform}/${process.arch}. Configure executablePath with a compatible Chrome installation.`);
440
+ const browser = Browser.CHROME;
441
+ const buildId = configuration.chrome?.version ?? PUPPETEER_REVISIONS.chrome;
442
+ if (!/^\d+\.\d+\.\d+\.\d+$/.test(buildId)) throw new Error("PUPPETEER_CHROME_VERSION must be an exact Chrome version for reproducible browser setup. Remove it to use the Puppeteer-matched version.");
443
+ const cacheDir = (0, node_path.resolve)(options.browser?.cacheDirectory ?? configuration.cacheDirectory ?? (0, node_path.join)((0, node_os.homedir)(), ".cache", "puppeteer"));
444
+ const cache = new Cache(cacheDir);
445
+ const executablePath = cache.computeExecutablePath({
446
+ browser,
447
+ buildId,
448
+ platform
449
+ });
450
+ if (await isExecutable(executablePath)) return executablePath;
451
+ if (!(options.browser?.download !== void 0 ? options.browser.download === "auto" : !configuration.skipDownload && !configuration.chrome?.skipDownload)) throw new Error(`Chrome ${buildId} is missing from ${cacheDir} and browser downloads are disabled. Pre-provision this cache with await ensureBrowser({ browser: { download: "auto", cacheDirectory: "..." } }) on a connected machine of the same platform, or configure executablePath.`);
452
+ await (0, node_fs_promises.mkdir)(cacheDir, { recursive: true });
453
+ const { lock } = await import("proper-lockfile");
454
+ let compromised;
455
+ const release = await lock(cache.installationDir(browser, platform, buildId), {
456
+ realpath: false,
457
+ lockfilePath: (0, node_path.join)(cacheDir, `.open-wa-${platform}-${buildId}.lock`),
458
+ stale: 12e4,
459
+ update: 1e4,
460
+ retries: {
461
+ retries: 600,
462
+ factor: 1,
463
+ minTimeout: 1e3,
464
+ maxTimeout: 1e3
465
+ },
466
+ onCompromised(error) {
467
+ compromised = error;
468
+ }
469
+ }).catch((cause) => {
470
+ throw new Error(`Could not acquire the browser installation lock in ${cacheDir}. Another startup may still be downloading Chrome; retry after it finishes.`, { cause });
471
+ });
472
+ let staging;
473
+ try {
474
+ if (await isExecutable(executablePath)) return executablePath;
475
+ const stagingPrefix = `.open-wa-${platform}-${buildId}-download-`;
476
+ for (const name of await (0, node_fs_promises.readdir)(cacheDir)) if (name.startsWith(stagingPrefix)) await (0, node_fs_promises.rm)((0, node_path.join)(cacheDir, name), {
477
+ recursive: true,
478
+ force: true
479
+ });
480
+ staging = await (0, node_fs_promises.mkdtemp)((0, node_path.join)(cacheDir, stagingPrefix));
481
+ report(ctx, `Downloading Chrome ${buildId} for ${platform}. It will be reused from ${cacheDir}.`);
482
+ let lastReport = 0;
483
+ const installed = await install({
484
+ browser,
485
+ buildId,
486
+ platform,
487
+ cacheDir: staging,
488
+ baseUrl: options.browser?.downloadBaseUrl ?? configuration.chrome?.downloadBaseUrl,
489
+ installDeps: false,
490
+ downloadProgressCallback(downloaded, total) {
491
+ const now = Date.now();
492
+ if (now - lastReport < 2e3 && downloaded !== total) return;
493
+ lastReport = now;
494
+ const progress = total > 0 ? `${Math.min(100, Math.floor(downloaded / total * 100))}%` : `${Math.round(downloaded / 1048576)} MiB`;
495
+ report(ctx, `Downloading Chrome ${buildId}: ${progress}`);
496
+ }
497
+ });
498
+ if (compromised) throw compromised;
499
+ if (!await isExecutable(installed.executablePath)) throw new Error("The browser archive did not contain an executable Chrome binary.");
500
+ const destination = cache.installationDir(browser, platform, buildId);
501
+ await (0, node_fs_promises.mkdir)(cache.browserRoot(browser), { recursive: true });
502
+ await (0, node_fs_promises.rm)(destination, {
503
+ recursive: true,
504
+ force: true
505
+ });
506
+ if (compromised) throw compromised;
507
+ await (0, node_fs_promises.rename)(installed.path, destination);
508
+ report(ctx, `Chrome ${buildId} is ready.`);
509
+ return executablePath;
510
+ } catch (cause) {
511
+ throw new Error(`Unable to provision Chrome ${buildId}. Check your connection, HTTP_PROXY/HTTPS_PROXY/NO_PROXY, browser.downloadBaseUrl and write access to ${cacheDir}, then retry. Alternatively, configure executablePath to an installed Chrome binary.`, { cause });
512
+ } finally {
513
+ try {
514
+ if (staging) await (0, node_fs_promises.rm)(staging, {
515
+ recursive: true,
516
+ force: true
517
+ });
518
+ } finally {
519
+ if (!compromised) await release();
520
+ }
521
+ }
522
+ }
523
+ //#endregion
405
524
  //#region src/PuppeteerDriver.ts
406
525
  var PuppeteerDriver = class {
407
526
  constructor() {
@@ -439,14 +558,24 @@ var PuppeteerDriver = class {
439
558
  }
440
559
  async launch(options) {
441
560
  if (!this.puppeteer) await this.init();
442
- const browser = await this.puppeteer.launch({
443
- headless: options?.headless ?? true,
444
- executablePath: options?.executablePath,
445
- args: options?.args || [],
446
- defaultViewport: options?.defaultViewport,
447
- userDataDir: options?.userDataDir,
448
- timeout: options?.timeoutMs
449
- });
561
+ const executablePath = await ensureBrowser(options, this.ctx);
562
+ let browser;
563
+ try {
564
+ browser = await this.puppeteer.launch({
565
+ browser: "chrome",
566
+ headless: options?.headless ?? true,
567
+ executablePath,
568
+ args: options?.args || [],
569
+ defaultViewport: options?.defaultViewport,
570
+ userDataDir: options?.userDataDir,
571
+ timeout: options?.timeoutMs
572
+ });
573
+ } catch (cause) {
574
+ const detail = cause instanceof Error ? cause.message : String(cause);
575
+ if (/error while loading shared libraries|Library not loaded|cannot open shared object file/i.test(detail)) throw new Error(`Chrome is installed but an operating-system dependency is missing. Install the library named in the browser error using your system administrator or deployment image; open-wa does not install OS packages. See https://pptr.dev/troubleshooting. Browser error: ${detail}`, { cause });
576
+ if (/No usable sandbox|Running as root without --no-sandbox|Failed to move to new namespace/i.test(detail)) throw new Error(`Chrome could not start with its sandbox enabled. Configure sandbox support and a non-root browser user in your deployment image. See https://pptr.dev/troubleshooting. Browser error: ${detail}`, { cause });
577
+ throw cause;
578
+ }
450
579
  try {
451
580
  const browserVersion = await browser.version();
452
581
  this.ctx?.logger?.info("Browser executable version", { version: browserVersion });
@@ -478,3 +607,4 @@ exports.PuppeteerBrowser = PuppeteerBrowser;
478
607
  exports.PuppeteerDriver = PuppeteerDriver;
479
608
  exports.PuppeteerElementHandle = PuppeteerElementHandle;
480
609
  exports.PuppeteerPage = PuppeteerPage;
610
+ exports.ensureBrowser = ensureBrowser;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { ConnectOptions, DisposableHandle, DriverCapabilities, IBrowser, IDriver, IDriverContext, IElementHandle, IFrame, IPage, LaunchOptions, NavigationWaitUntil, PageEventHandler, WaitForFunctionOptions } from "@open-wa/driver-interface";
1
+ import { BrowserProvisionOptions, ConnectOptions, DisposableHandle, DriverCapabilities, IBrowser, IDriver, IDriverContext, IElementHandle, IFrame, IPage, LaunchOptions, NavigationWaitUntil, PageEventHandler, WaitForFunctionOptions } from "@open-wa/driver-interface";
2
2
  import { Browser, CDPSession, ElementHandle, Page } from "puppeteer";
3
3
  //#region src/PuppeteerDriver.d.ts
4
4
  export declare class PuppeteerDriver implements IDriver {
@@ -16,6 +16,13 @@ export declare class PuppeteerDriver implements IDriver {
16
16
  require<C extends string>(cap: C): void;
17
17
  }
18
18
  //#endregion
19
+ //#region src/ensureBrowser.d.ts
20
+ export interface EnsureBrowserOptions {
21
+ executablePath?: string;
22
+ browser?: BrowserProvisionOptions;
23
+ }
24
+ export declare function ensureBrowser(options?: EnsureBrowserOptions, ctx?: IDriverContext): Promise<string>;
25
+ //#endregion
19
26
  //#region src/PuppeteerBrowser.d.ts
20
27
  export declare class PuppeteerBrowser implements IBrowser {
21
28
  private browser;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/PuppeteerDriver.ts","../src/PuppeteerBrowser.ts","../src/PuppeteerPage.ts","../src/PuppeteerElementHandle.ts"],"mappings":";;;qBAGa,2BAA2B;WAC3B;EACT;WACS,cAAc;UAEf;UACA;EAER;EAgBM,KAAK,MAAM,iBAAiB;EAgB5B,OAAO,UAAU,gBAAgB,QAAQ;EAoBzC,QAAQ,SAAS,iBAAiB,QAAQ;EAWhD;EAIA,IAAI,kBAAkB,KAAK;EAI3B,QAAQ,kBAAkB,KAAK;;;;qBC9EtB,4BAA4B;UAIzB;UACA;WAJH;EAET,YACY,SAAS,SACT,cAAc;EAGpB,QAAQ;IACV;MACA,QAAQ;EAWN,SAAS,QAAQ;EAKjB,SAAS;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vB;EAApB,YAAoB,SAAS;EAEvB,SAAS;EAIT,KAAK,eAAe;EAIpB,aAAa,eAAe;EAK5B,eAAe;EAKf,WAAW;EAIjB,UAAU"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/PuppeteerDriver.ts","../src/ensureBrowser.ts","../src/PuppeteerBrowser.ts","../src/PuppeteerPage.ts","../src/PuppeteerElementHandle.ts"],"mappings":";;;qBAIa,2BAA2B;WAC3B;EACT;WACS,cAAc;UAEf;UACA;EAER;EAgBM,KAAK,MAAM,iBAAiB;EAgB5B,OAAO,UAAU,gBAAgB,QAAQ;EAkCzC,QAAQ,SAAS,iBAAiB,QAAQ;EAWhD;EAIA,IAAI,kBAAkB,KAAK;EAI3B,QAAQ,kBAAkB,KAAK;;;;iBC3FlB;EACb;EACA,UAAU;;wBAuBQ,cAClB,UAAS,sBACT,MAAM,iBACP;;;qBC9BU,4BAA4B;UAIzB;UACA;WAJH;EAET,YACY,SAAS,SACT,cAAc;EAGpB,QAAQ;IACV;MACA,QAAQ;EAWN,SAAS,QAAQ;EAKjB,SAAS;EAIf;EAIM,iBAAiB;EAIvB;EAIA,UAAU;;;;qBCoHD,yBAAyB;UAKtB;UACA;WALH;mBACQ;EAEjB,YACY,MAAM,MACN,cAAc;EAGpB,KAAK,aAAa;IAAY,YAAY;IAAqB;MAAuB;EAO5F;EAIM,UAAU;EAIhB,aAAa;EAIP,sBAAsB,KAAK,KAC7B,KAAK,KAAK,QAAQ,MAAM,QAAQ,MAChC,KAAK,MACN;EAIG,SAAS,KAAK,KAAK,KAAK,KAAK,QAAQ,MAAM,QAAQ,MAAM,KAAK,MAAM,QAAQ;EAS5E,eAAe,eAAe,iBAAiB,QAAQ;EAQvD,cAAc,iBAAiB,QAAQ;EAiBvC,YAAY;IAAY;IAAe;MAAmB;EAI1D,aAAa,aAAa;EAI1B,uBAAuB,mBAAmB;EAK1C,gBAAgB,kBAAkB;IAAY;MAAuB,QAAQ;EAY7E,gBAAgB,gBAAgB,UAAU,yBAAyB;EACnE,gBAAgB,KAAK,KAAK,KAAK,iBAAiB,KAAK,KAAK,UAAU,yBAAyB;EAoC7F,EAAE,mBAAmB,QAAQ;EAK7B,GAAG,mBAAmB,QAAQ;EAK9B,MAAM,mBAAmB;EAIzB,KAAK,kBAAkB,cAAc;IAAY;MAAqB;EAItE,WAAW;IAAY;IAAuB;MAAuB,QAAQ;EAK7E,eAAe,cAAc,QAAQ,sBAAsB;EAIjE,GAAG,kBAAkB,OAAO,GAAG,cAAc,uBAAuB,iBAAiB,wBAAwB;EAiB7G,IAAI,kBAAkB,OAAO,GAAG,cAAc,uBAAuB,iBAAiB;EAchF,SAAS;EAIf;EAIA,IAAI,kBAAkB,KAAK;EAI3B,QAAQ,kBAAkB,KAAK;EAUzB,OAAO,QAAQ;EAKf,uBAAuB,kBAAkB;EAKzC,IAAI;IAAY;IAAe;MAAoB,QAAQ;EAM3D,aAAa;IAAY;IAAe;MAA0B;EAKlE,eAAe,QAAQ;EAM7B,UAAU;UAIF;UAgBA;UAkBA;UAcA;;;;qBCjcC,kCAAkC;UACvB;EAApB,YAAoB,SAAS;EAEvB,SAAS;EAIT,KAAK,eAAe;EAIpB,aAAa,eAAe;EAK5B,eAAe;EAKf,WAAW;EAIjB,UAAU"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/driver-puppeteer",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "Puppeteer driver implementation for open-wa",
5
5
  "main": "./dist/index.cjs",
6
6
  "types": "./dist/index.d.cts",
@@ -12,12 +12,16 @@
12
12
  }
13
13
  },
14
14
  "dependencies": {
15
- "@open-wa/driver-interface": "5.1.0",
15
+ "@open-wa/driver-interface": "5.2.0",
16
+ "@puppeteer/browsers": "^3.2.3",
17
+ "proper-lockfile": "^4.1.2",
18
+ "proxy-agent": "^8.0.1",
16
19
  "puppeteer": "^25.12.0",
17
20
  "puppeteer-extra": "^3.3.6"
18
21
  },
19
22
  "devDependencies": {
20
23
  "@types/node": "^26.6.3",
24
+ "@types/proper-lockfile": "^4.1.4",
21
25
  "typescript": "^7.0.2",
22
26
  "vitest": "5.0.2"
23
27
  },