From 654cd2619ce60ee2fbc1ae0c7292877287626581 Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Mon, 9 Mar 2026 16:16:15 +0000 Subject: [PATCH] fix(smartpdf): lazily initialize SmartPdf, stop it only when present, and make evaluateOnPage reliably close pages --- changelog.md | 8 ++++++++ ts/00_commitinfo_data.ts | 2 +- ts/index.ts | 34 +++++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/changelog.md b/changelog.md index 998d3f3..64a058d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,13 @@ # Changelog +## 2026-03-09 - 2.0.9 - fix(smartpdf) +lazily initialize SmartPdf, stop it only when present, and make evaluateOnPage reliably close pages + +- Add private ensureSmartPdf() to lazily initialize and start SmartPdf only when PDF methods are called +- Call ensureSmartPdf() in pdfFromPage() before using smartpdf +- Stop SmartPdf in stop() only if it was initialized and clear the reference +- Wrap evaluateOnPage() page operations in try/finally and safely ignore errors from page.close() to avoid resource leaks or uncaught exceptions + ## 2025-01-03 - 2.0.8 - fix(core) Ensure consistent browser automation functionality diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index e3ab36a..fd71c44 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartbrowser', - version: '2.0.8', + version: '2.0.9', description: 'A simplified Puppeteer wrapper for easy automation and testing tasks.' } diff --git a/ts/index.ts b/ts/index.ts index b602903..1e48eb1 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -14,16 +14,28 @@ export class SmartBrowser { */ public async start() { this.headlessBrowser = await plugins.smartpuppeteer.getEnvAwareBrowserInstance(); - this.smartpdf = new plugins.smartpdf.SmartPdf(); - await this.smartpdf.start(this.headlessBrowser); + // SmartPdf is lazy-initialized only when PDF methods are called + } + + /** + * ensure SmartPdf is initialized (lazy) + */ + private async ensureSmartPdf() { + if (!this.smartpdf) { + this.smartpdf = new plugins.smartpdf.SmartPdf(); + await this.smartpdf.start(this.headlessBrowser); + } } /** * stop the SmartBrowser instance */ public async stop() { + if (this.smartpdf) { + await this.smartpdf.stop(); + this.smartpdf = null; + } await this.headlessBrowser.close(); - await this.smartpdf.stop(); } /** @@ -31,6 +43,7 @@ export class SmartBrowser { * @param urlArg */ public async pdfFromPage(urlArg: string): Promise { + await this.ensureSmartPdf(); const result = await this.smartpdf.getFullWebsiteAsSinglePdf(urlArg); return result; } @@ -63,12 +76,15 @@ export class SmartBrowser { */ public async evaluateOnPage(urlArg: string, funcArg: () => Promise) { const page = await this.headlessBrowser.newPage(); - await page.goto(urlArg, { - waitUntil: 'networkidle2', - }); - const result = await page.evaluate(funcArg); - await page.close(); - return result; + try { + await page.goto(urlArg, { + waitUntil: 'networkidle2', + }); + const result = await page.evaluate(funcArg); + return result; + } finally { + try { await page.close(); } catch (e) { /* page may already be closed */ } + } } }