lib/grover/js/processor.cjs in grover-1.1.6 vs lib/grover/js/processor.cjs in grover-1.1.7

- old
+ new

@@ -1,64 +1,86 @@ // Setup imports try { const Module = require('module'); - // resolve puppeteer from the CWD instead of where this script is located - var puppeteer = require(require.resolve('puppeteer', { paths: Module._nodeModulePaths(process.cwd()) })); + + try { + // resolve puppeteer from the CWD instead of where this script is located + var puppeteer = require(require.resolve('puppeteer', { paths: Module._nodeModulePaths(process.cwd()) })); + } catch (puppeteerError) { + try { + // try resolve `puppeteer-core` library instead + var puppeteer = require(require.resolve('puppeteer-core', { paths: Module._nodeModulePaths(process.cwd()) })); + } catch (coreError) { + // raise the original puppeteer load issue so we don't send people don't the wrong rabbit hole by default. + throw puppeteerError; + } + } } catch (e) { process.stdout.write(JSON.stringify(['err', e.toString()])); process.stdout.write("\n"); process.exit(1); } + process.stdout.write("[\"ok\"]\n"); const fs = require('fs'); const os = require('os'); const path = require('path'); const _processPage = (async (convertAction, urlOrHtml, options) => { - let browser, errors = [], tmpDir; + let browser, page, errors = [], tmpDir, wsConnection = false; try { // Configure puppeteer debugging options const debug = options.debug; delete options.debug; const browserWsEndpoint = options.browserWsEndpoint; delete options.browserWsEndpoint; if (typeof browserWsEndpoint === "string") { const connectParams = { browserWSEndpoint: browserWsEndpoint, }; + wsConnection = true; - browser = await puppeteer.connect(connectParams); + try { + browser = await puppeteer.connect(connectParams); + } catch { + function WsConnectFailedError() { + this.name = "WsConnectFailedError"; + this.message = `Failed to connect to browser WS endpoint: ${browserWsEndpoint}`; + } + WsConnectFailedError.prototype = Error.prototype; + throw new WsConnectFailedError(); + } } else { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'grover-')); const launchParams = { args: process.env.GROVER_NO_SANDBOX === 'true' ? ['--no-sandbox', '--disable-setuid-sandbox'] : [], userDataDir: tmpDir }; - + if (typeof debug === 'object' && !!debug) { if (debug.headless !== undefined) { launchParams.headless = debug.headless; } if (debug.devtools !== undefined) { launchParams.devtools = debug.devtools; } } - + // Configure additional launch arguments const args = options.launchArgs; delete options.launchArgs; if (Array.isArray(args)) { launchParams.args = launchParams.args.concat(args); } - + // Set executable path if given const executablePath = options.executablePath; delete options.executablePath; if (executablePath) { launchParams.executablePath = executablePath; } - + // Launch the browser and create a page browser = await puppeteer.launch(launchParams); } - const page = await browser.newPage(); + page = await browser.newPage(); // Basic auth const username = options.username; delete options.username const password = options.password; delete options.password if (username !== undefined && password !== undefined) { @@ -259,10 +281,15 @@ if (debug === undefined || (typeof debug === 'object' && (debug.headless === undefined || debug.headless))) { return await page[convertAction](options); } } finally { if (browser) { - await browser.close(); + if (wsConnection) { + if (page) await page.close(); + await browser.disconnect(); + } else { + await browser.close(); + } } try { if (tmpDir) fs.rmSync(tmpDir, { recursive: true }); } catch { }