@push.rocks/smartbrowser 2.0.8 → 2.0.11

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.
@@ -3,7 +3,7 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartbrowser',
6
- version: '2.0.8',
6
+ version: '2.0.11',
7
7
  description: 'A simplified Puppeteer wrapper for easy automation and testing tasks.'
8
8
  };
9
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSwwQkFBMEI7SUFDaEMsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLHVFQUF1RTtDQUNyRixDQUFBIn0=
9
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSwwQkFBMEI7SUFDaEMsT0FBTyxFQUFFLFFBQVE7SUFDakIsV0FBVyxFQUFFLHVFQUF1RTtDQUNyRixDQUFBIn0=
@@ -10,6 +10,10 @@ export declare class SmartBrowser {
10
10
  * start the SmartBrowser instance
11
11
  */
12
12
  start(): Promise<void>;
13
+ /**
14
+ * ensure SmartPdf is initialized (lazy)
15
+ */
16
+ private ensureSmartPdf;
13
17
  /**
14
18
  * stop the SmartBrowser instance
15
19
  */
package/dist_ts/index.js CHANGED
@@ -9,21 +9,33 @@ export class SmartBrowser {
9
9
  */
10
10
  async start() {
11
11
  this.headlessBrowser = await plugins.smartpuppeteer.getEnvAwareBrowserInstance();
12
- this.smartpdf = new plugins.smartpdf.SmartPdf();
13
- await this.smartpdf.start(this.headlessBrowser);
12
+ // SmartPdf is lazy-initialized only when PDF methods are called
13
+ }
14
+ /**
15
+ * ensure SmartPdf is initialized (lazy)
16
+ */
17
+ async ensureSmartPdf() {
18
+ if (!this.smartpdf) {
19
+ this.smartpdf = new plugins.smartpdf.SmartPdf();
20
+ await this.smartpdf.start(this.headlessBrowser);
21
+ }
14
22
  }
15
23
  /**
16
24
  * stop the SmartBrowser instance
17
25
  */
18
26
  async stop() {
27
+ if (this.smartpdf) {
28
+ await this.smartpdf.stop();
29
+ this.smartpdf = null;
30
+ }
19
31
  await this.headlessBrowser.close();
20
- await this.smartpdf.stop();
21
32
  }
22
33
  /**
23
34
  * create a pdf from page
24
35
  * @param urlArg
25
36
  */
26
37
  async pdfFromPage(urlArg) {
38
+ await this.ensureSmartPdf();
27
39
  const result = await this.smartpdf.getFullWebsiteAsSinglePdf(urlArg);
28
40
  return result;
29
41
  }
@@ -37,9 +49,10 @@ export class SmartBrowser {
37
49
  await page.goto(urlArg, {
38
50
  waitUntil: 'networkidle2',
39
51
  });
40
- const screenshotBuffer = (await page.screenshot({
52
+ const screenshotResult = await page.screenshot({
41
53
  encoding: 'binary',
42
- }));
54
+ });
55
+ const screenshotBuffer = Buffer.from(screenshotResult);
43
56
  await page.close();
44
57
  return {
45
58
  name: pageId,
@@ -54,14 +67,21 @@ export class SmartBrowser {
54
67
  */
55
68
  async evaluateOnPage(urlArg, funcArg) {
56
69
  const page = await this.headlessBrowser.newPage();
57
- await page.goto(urlArg, {
58
- waitUntil: 'networkidle2',
59
- });
60
- const result = await page.evaluate(funcArg);
61
- await page.close();
62
- return result;
70
+ try {
71
+ await page.goto(urlArg, {
72
+ waitUntil: 'networkidle2',
73
+ });
74
+ const result = await page.evaluate(funcArg);
75
+ return result;
76
+ }
77
+ finally {
78
+ try {
79
+ await page.close();
80
+ }
81
+ catch (e) { /* page may already be closed */ }
82
+ }
63
83
  }
64
84
  }
65
85
  import { smartpuppeteer } from './smartbrowser.plugins.js';
66
86
  export { smartpuppeteer };
67
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssT0FBTyxNQUFNLDJCQUEyQixDQUFDO0FBRXJELE9BQU8sS0FBSyxVQUFVLE1BQU0sdUJBQXVCLENBQUM7QUFFcEQ7O0dBRUc7QUFDSCxNQUFNLE9BQU8sWUFBWTtJQUl2Qjs7T0FFRztJQUNJLEtBQUssQ0FBQyxLQUFLO1FBQ2hCLElBQUksQ0FBQyxlQUFlLEdBQUcsTUFBTSxPQUFPLENBQUMsY0FBYyxDQUFDLDBCQUEwQixFQUFFLENBQUM7UUFDakYsSUFBSSxDQUFDLFFBQVEsR0FBRyxJQUFJLE9BQU8sQ0FBQyxRQUFRLENBQUMsUUFBUSxFQUFFLENBQUM7UUFDaEQsTUFBTSxJQUFJLENBQUMsUUFBUSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLENBQUM7SUFDbEQsQ0FBQztJQUVEOztPQUVHO0lBQ0ksS0FBSyxDQUFDLElBQUk7UUFDZixNQUFNLElBQUksQ0FBQyxlQUFlLENBQUMsS0FBSyxFQUFFLENBQUM7UUFDbkMsTUFBTSxJQUFJLENBQUMsUUFBUSxDQUFDLElBQUksRUFBRSxDQUFDO0lBQzdCLENBQUM7SUFFRDs7O09BR0c7SUFDSSxLQUFLLENBQUMsV0FBVyxDQUFDLE1B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FLENBQUM7UUFDbkIsT0FBTztZQUNMLElBQUksRUFBRSxNQUFNO1lBQ1osRUFBRSxFQUFFLEdBQUcsTUFBTSxLQUFLO1lBQ2xCLE1BQU0sRUFBRSxnQkFBZ0I7U0FDekIsQ0FBQztJQUNKLENBQUM7SUFFRDs7OztPQUlHO0lBQ0ksS0FBSyxDQUFDLGNBQWMsQ0FBSSxNQUFjLEVBQUUsT0FBeUI7UUFDdEUsTUFBTSxJQUFJLEdBQUcsTUFBTSxJQUFJLENBQUMsZUFBZSxDQUFDLE9BQU8sRUFBRSxDQUFDO1FBQ2xELElBQUksQ0FBQztZQUNILE1BQU0sSUFBSSxDQUFDLElBQUksQ0FBQyxNQUFNLEVBQUU7Z0JBQ3RCLFNBQVMsRUFBRSxjQUFjO2FBQzFCLENBQUMsQ0FBQztZQUNILE1BQU0sTUFBTSxHQUFHLE1BQU0sSUFBSSxDQUFDLFFBQVEsQ0FBQyxPQUFPLENBQUMsQ0FBQztZQUM1QyxPQUFPLE1BQU0sQ0FBQztRQUNoQixDQUFDO2dCQUFTLENBQUM7WUFDVCxJQUFJLENBQUM7Z0JBQUMsTUFBTSxJQUFJLENBQUMsS0FBSyxFQUFFLENBQUM7WUFBQyxDQUFDO1lBQUMsT0FBTyxDQUFDLEVBQUUsQ0FBQyxDQUFDLGdDQUFnQyxDQUFDLENBQUM7UUFDNUUsQ0FBQztJQUNILENBQUM7Q0FDRjtBQUVELE9BQU8sRUFBRSxjQUFjLEVBQUUsTUFBTSwyQkFBMkIsQ0FBQztBQUMzRCxPQUFPLEVBQUUsY0FBYyxFQUFFLENBQUMifQ==
package/npmextra.json CHANGED
@@ -1,8 +1,5 @@
1
1
  {
2
- "npmci": {
3
- "npmAccessLevel": "public"
4
- },
5
- "gitzone": {
2
+ "@git.zone/cli": {
6
3
  "projectType": "npm",
7
4
  "module": {
8
5
  "githost": "code.foss.global",
@@ -20,9 +17,17 @@
20
17
  "web scraping",
21
18
  "browser automation"
22
19
  ]
20
+ },
21
+ "release": {
22
+ "registries": [
23
+ "https://verdaccio.lossless.digital",
24
+ "https://registry.npmjs.org"
25
+ ],
26
+ "accessLevel": "public"
23
27
  }
24
28
  },
25
- "tsdoc": {
29
+ "@git.zone/tsdoc": {
26
30
  "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
27
- }
31
+ },
32
+ "@ship.zone/szci": {}
28
33
  }
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "@push.rocks/smartbrowser",
3
- "version": "2.0.8",
3
+ "version": "2.0.11",
4
4
  "description": "A simplified Puppeteer wrapper for easy automation and testing tasks.",
5
5
  "main": "dist_ts/index.js",
6
6
  "typings": "dist_ts/index.d.ts",
7
7
  "type": "module",
8
+ "scripts": {
9
+ "test": "(tstest test/ --web)",
10
+ "build": "(tsbuild --web)",
11
+ "buildDocs": "tsdoc"
12
+ },
8
13
  "repository": {
9
14
  "type": "git",
10
15
  "url": "https://code.foss.global/push.rocks/smartbrowser.git"
@@ -17,16 +22,16 @@
17
22
  "homepage": "https://code.foss.global/push.rocks/smartbrowser",
18
23
  "dependencies": {
19
24
  "@push.rocks/smartdelay": "^3.0.5",
20
- "@push.rocks/smartpdf": "^3.1.8",
21
- "@push.rocks/smartpuppeteer": "^2.0.0",
25
+ "@push.rocks/smartpdf": "^4.2.0",
26
+ "@push.rocks/smartpuppeteer": "^2.0.5",
22
27
  "@push.rocks/smartunique": "^3.0.9"
23
28
  },
24
29
  "devDependencies": {
25
- "@git.zone/tsbuild": "^2.2.0",
26
- "@git.zone/tsrun": "^1.3.3",
27
- "@git.zone/tstest": "^1.0.90",
28
- "@push.rocks/tapbundle": "^5.5.4",
29
- "@types/node": "^22.10.4"
30
+ "@git.zone/tsbuild": "^4.3.0",
31
+ "@git.zone/tsrun": "^2.0.1",
32
+ "@git.zone/tstest": "^3.3.1",
33
+ "@push.rocks/tapbundle": "^6.0.3",
34
+ "@types/node": "^25.3.5"
30
35
  },
31
36
  "private": false,
32
37
  "files": [
@@ -52,10 +57,5 @@
52
57
  "screenshot",
53
58
  "web scraping",
54
59
  "browser automation"
55
- ],
56
- "scripts": {
57
- "test": "(tstest test/ --web)",
58
- "build": "(tsbuild --web --allowimplicitany)",
59
- "buildDocs": "tsdoc"
60
- }
61
- }
60
+ ]
61
+ }
package/readme.hints.md CHANGED
@@ -1 +1,16 @@
1
-
1
+ # Smartbrowser Hints
2
+
3
+ ## Dependencies (as of 2026-03-09)
4
+ - `@push.rocks/smartpdf` v4.2.x: Has `SmartPdf.create()` factory method, but `new SmartPdf()` + `start(browser)` still works. Lazy init pattern used.
5
+ - `@push.rocks/smartpuppeteer` v2.x: `getEnvAwareBrowserInstance()` main entry point
6
+ - `@push.rocks/tapbundle` v6.x: No longer exports `expectAsync`, only `tap`, `expect`, `TapWrap`, `webhelpers`
7
+ - `@git.zone/tsbuild` v4.x: No `--allowimplicitany` flag (defaults to `noImplicitAny: false`), `--web` flag still passed but ignored if no ts_web
8
+ - `@git.zone/tstest` v3.3.x: Still supports `--web` flag, file suffix conventions changed (`.chromium.ts` for browser tests)
9
+ - `@types/node` v25.x: Latest Node.js type definitions
10
+ - Puppeteer `page.screenshot({ encoding: 'binary' })` returns `Uint8Array`, not `Buffer` - wrap with `Buffer.from()`
11
+
12
+ ## Build
13
+ - `pnpm build` uses `tsbuild --web`
14
+ - `pnpm test` uses `tstest test/ --web`
15
+ - No ts_web directory exists in this project
16
+ - Duplicate dayjs versions in transitive deps cause build errors; fix with `pnpm dedupe`
package/readme.md CHANGED
@@ -1,102 +1,145 @@
1
1
  # @push.rocks/smartbrowser
2
- simplified puppeteer
2
+ A simplified Puppeteer wrapper for easy browser automation, PDF generation, screenshots, and page evaluation.
3
3
 
4
4
  ## Install
5
- To install `@push.rocks/smartbrowser`, use the following npm command:
5
+
6
+ To install `@push.rocks/smartbrowser`, use the following command with your preferred package manager:
6
7
 
7
8
  ```bash
8
- npm install @push.rocks/smartbrowser --save
9
+ npm install @push.rocks/smartbrowser
10
+ # or
11
+ pnpm install @push.rocks/smartbrowser
9
12
  ```
10
13
 
11
- This will add `@push.rocks/smartbrowser` to your project's dependencies.
14
+ This module works with Node.js and requires a Chromium-compatible browser available in the environment. It uses `@push.rocks/smartpuppeteer` under the hood, which automatically detects your environment and configures the browser accordingly (including CI/Docker scenarios).
12
15
 
13
16
  ## Usage
14
17
 
15
- `@push.rocks/smartbrowser` simplifies interactions with Puppeteer for tasks like generating PDFs or capturing screenshots from webpages. Below are examples illustrating how you can use `@push.rocks/smartbrowser` in your projects.
18
+ `@push.rocks/smartbrowser` provides a high-level `SmartBrowser` class that wraps Puppeteer for common browser automation tasks: generating PDFs, capturing screenshots, and evaluating JavaScript on web pages.
16
19
 
17
20
  ### Getting Started
18
21
 
19
- First, import `SmartBrowser` from `@push.rocks/smartbrowser`:
22
+ Import and initialize a `SmartBrowser` instance:
20
23
 
21
24
  ```typescript
22
25
  import { SmartBrowser } from '@push.rocks/smartbrowser';
23
- ```
24
-
25
- Then, initialize and start the `SmartBrowser` instance:
26
26
 
27
- ```typescript
28
27
  const smartBrowser = new SmartBrowser();
29
28
  await smartBrowser.start();
30
29
  ```
31
30
 
32
31
  ### Generating a PDF from a Webpage
33
32
 
34
- You can generate a PDF from any webpage URL. This can be particularly useful for generating reports, invoices, or snapshot captures of web content.
33
+ Generate a full-page PDF from any URL. The result includes a `Buffer` with the PDF contents:
35
34
 
36
35
  ```typescript
37
- // Generate a PDF from a page
38
36
  const pdfResult = await smartBrowser.pdfFromPage('https://example.com');
39
- console.log(pdfResult.buffer); // This holds the PDF file's buffer
37
+ console.log(pdfResult.buffer); // PDF file buffer
38
+ console.log(pdfResult.name); // Generated name identifier
40
39
  ```
41
40
 
41
+ The PDF generation is powered by `@push.rocks/smartpdf`, which is lazily initialized on first use. This means the SmartPdf server is only started when you actually call `pdfFromPage()`, keeping resource usage minimal.
42
+
42
43
  ### Capturing a Screenshot of a Webpage
43
44
 
44
- Similarly, you can capture a screenshot of a webpage by passing the URL. This is useful for capturing the current state of a web application, for audits, or for keeping visual records.
45
+ Capture a PNG screenshot of any webpage:
45
46
 
46
47
  ```typescript
47
- // Capture a screenshot from a page
48
48
  const screenshotResult = await smartBrowser.screenshotFromPage('https://example.com');
49
- console.log(screenshotResult.buffer); // This is the screenshot's buffer
49
+ console.log(screenshotResult.buffer); // Screenshot buffer (PNG)
50
+ console.log(screenshotResult.name); // Short unique identifier
51
+ console.log(screenshotResult.id); // Identifier with extension
50
52
  ```
51
53
 
52
54
  ### Evaluating JavaScript on a Webpage
53
55
 
54
- `SmartBrowser` also allows you to evaluate JavaScript on the webpage. This can be useful for scraping data, testing web applications, or automating interactions with webpages.
56
+ Run arbitrary JavaScript inside a page context and retrieve the result:
55
57
 
56
58
  ```typescript
57
- // Evaluate JavaScript on a page
58
- const pageTitle = await smartBrowser.evaluateOnPage('https://example.com', () => {
59
- return document.title; // Gets the title of the page
59
+ const pageTitle = await smartBrowser.evaluateOnPage('https://example.com', async () => {
60
+ return document.title;
60
61
  });
61
- console.log(pageTitle); // Logs the page title to the console
62
+ console.log(pageTitle); // "Example Domain"
63
+ ```
64
+
65
+ The `evaluateOnPage` method supports generic return types:
66
+
67
+ ```typescript
68
+ const metrics = await smartBrowser.evaluateOnPage<{ width: number; height: number }>(
69
+ 'https://example.com',
70
+ async () => {
71
+ return {
72
+ width: window.innerWidth,
73
+ height: window.innerHeight,
74
+ };
75
+ }
76
+ );
77
+ console.log(metrics.width, metrics.height);
78
+ ```
79
+
80
+ Pages are automatically closed after evaluation, even if an error occurs.
81
+
82
+ ### Accessing the Underlying Puppeteer Browser
83
+
84
+ For advanced use cases, you can access the Puppeteer browser instance directly:
85
+
86
+ ```typescript
87
+ const page = await smartBrowser.headlessBrowser.newPage();
88
+ await page.goto('https://example.com');
89
+ // ... custom Puppeteer operations
90
+ await page.close();
91
+ ```
92
+
93
+ You can also import the `smartpuppeteer` module directly for lower-level browser management:
94
+
95
+ ```typescript
96
+ import { smartpuppeteer } from '@push.rocks/smartbrowser';
97
+
98
+ const browser = await smartpuppeteer.getEnvAwareBrowserInstance();
62
99
  ```
63
100
 
64
101
  ### Shutting Down
65
102
 
66
- Once your tasks are complete, it's important to properly shut down the `SmartBrowser` instance to free up resources:
103
+ Always stop the browser instance when done to free resources:
67
104
 
68
105
  ```typescript
69
106
  await smartBrowser.stop();
70
107
  ```
71
108
 
72
- ### Full Example
109
+ This cleanly shuts down the SmartPdf server (if it was initialized) and closes the browser.
73
110
 
74
- Combining the above steps, here's a full example of using `@push.rocks/smartbrowser` to generate a PDF and capture a screenshot of a webpage:
111
+ ### Full Example
75
112
 
76
113
  ```typescript
77
114
  import { SmartBrowser } from '@push.rocks/smartbrowser';
78
115
 
79
- async function generateWebAssets() {
116
+ async function main() {
80
117
  const smartBrowser = new SmartBrowser();
81
118
  await smartBrowser.start();
82
119
 
120
+ // Generate a PDF
83
121
  const pdfResult = await smartBrowser.pdfFromPage('https://example.com');
84
- console.log('PDF Generated:', pdfResult.buffer);
122
+ console.log('PDF size:', pdfResult.buffer.length, 'bytes');
123
+
124
+ // Take a screenshot
125
+ const screenshot = await smartBrowser.screenshotFromPage('https://example.com');
126
+ console.log('Screenshot size:', screenshot.buffer.length, 'bytes');
85
127
 
86
- const screenshotResult = await smartBrowser.screenshotFromPage('https://example.com');
87
- console.log('Screenshot Captured:', screenshotResult.buffer);
128
+ // Evaluate JavaScript
129
+ const title = await smartBrowser.evaluateOnPage('https://example.com', async () => {
130
+ return document.title;
131
+ });
132
+ console.log('Page title:', title);
88
133
 
89
134
  await smartBrowser.stop();
90
135
  }
91
136
 
92
- generateWebAssets();
137
+ main();
93
138
  ```
94
139
 
95
- In this guide, you've learned how to use `@push.rocks/smartbrowser` for common browser automation tasks. Follow this pattern to incorporate web automation into your applications efficiently.
96
-
97
140
  ## License and Legal Information
98
141
 
99
- This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
142
+ This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
100
143
 
101
144
  **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
102
145
 
@@ -106,7 +149,7 @@ This project is owned and maintained by Task Venture Capital GmbH. The names and
106
149
 
107
150
  ### Company Information
108
151
 
109
- Task Venture Capital GmbH
152
+ Task Venture Capital GmbH
110
153
  Registered at District court Bremen HRB 35230 HB, Germany
111
154
 
112
155
  For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
@@ -3,6 +3,6 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartbrowser',
6
- version: '2.0.8',
6
+ version: '2.0.11',
7
7
  description: 'A simplified Puppeteer wrapper for easy automation and testing tasks.'
8
8
  }
package/ts/index.ts CHANGED
@@ -14,16 +14,28 @@ export class SmartBrowser {
14
14
  */
15
15
  public async start() {
16
16
  this.headlessBrowser = await plugins.smartpuppeteer.getEnvAwareBrowserInstance();
17
- this.smartpdf = new plugins.smartpdf.SmartPdf();
18
- await this.smartpdf.start(this.headlessBrowser);
17
+ // SmartPdf is lazy-initialized only when PDF methods are called
18
+ }
19
+
20
+ /**
21
+ * ensure SmartPdf is initialized (lazy)
22
+ */
23
+ private async ensureSmartPdf() {
24
+ if (!this.smartpdf) {
25
+ this.smartpdf = new plugins.smartpdf.SmartPdf();
26
+ await this.smartpdf.start(this.headlessBrowser);
27
+ }
19
28
  }
20
29
 
21
30
  /**
22
31
  * stop the SmartBrowser instance
23
32
  */
24
33
  public async stop() {
34
+ if (this.smartpdf) {
35
+ await this.smartpdf.stop();
36
+ this.smartpdf = null;
37
+ }
25
38
  await this.headlessBrowser.close();
26
- await this.smartpdf.stop();
27
39
  }
28
40
 
29
41
  /**
@@ -31,6 +43,7 @@ export class SmartBrowser {
31
43
  * @param urlArg
32
44
  */
33
45
  public async pdfFromPage(urlArg: string): Promise<plugins.smartpdf.IPdf> {
46
+ await this.ensureSmartPdf();
34
47
  const result = await this.smartpdf.getFullWebsiteAsSinglePdf(urlArg);
35
48
  return result;
36
49
  }
@@ -45,9 +58,10 @@ export class SmartBrowser {
45
58
  await page.goto(urlArg, {
46
59
  waitUntil: 'networkidle2',
47
60
  });
48
- const screenshotBuffer = (await page.screenshot({
61
+ const screenshotResult = await page.screenshot({
49
62
  encoding: 'binary',
50
- })) as Buffer;
63
+ });
64
+ const screenshotBuffer = Buffer.from(screenshotResult);
51
65
  await page.close();
52
66
  return {
53
67
  name: pageId,
@@ -63,12 +77,15 @@ export class SmartBrowser {
63
77
  */
64
78
  public async evaluateOnPage<T>(urlArg: string, funcArg: () => Promise<T>) {
65
79
  const page = await this.headlessBrowser.newPage();
66
- await page.goto(urlArg, {
67
- waitUntil: 'networkidle2',
68
- });
69
- const result = await page.evaluate(funcArg);
70
- await page.close();
71
- return result;
80
+ try {
81
+ await page.goto(urlArg, {
82
+ waitUntil: 'networkidle2',
83
+ });
84
+ const result = await page.evaluate(funcArg);
85
+ return result;
86
+ } finally {
87
+ try { await page.close(); } catch (e) { /* page may already be closed */ }
88
+ }
72
89
  }
73
90
  }
74
91