fix(IncognitoBrowser): Enhance IncognitoBrowser error handling and process management
This commit is contained in:
parent
1839c56130
commit
6ef281c871
27
changelog.md
Normal file
27
changelog.md
Normal file
@ -0,0 +1,27 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-02-25 - 2.0.4 - fix(IncognitoBrowser)
|
||||
Enhance IncognitoBrowser error handling and process management
|
||||
|
||||
- Improve error handling during browser disconnection and process management.
|
||||
- Ensure safe re-launch of the browser if it disconnects while the status is 'started'.
|
||||
- Add logging for critical operations like launching without sandbox.
|
||||
|
||||
## 2024-04-12 to 2024-05-29 - 2.0.3 - Minor Updates
|
||||
Various minor updates and configuration changes were made during this period.
|
||||
|
||||
- Updated project description.
|
||||
- Updated TypeScript configuration.
|
||||
|
||||
## 2023-07-10 to 2024-04-12 - 2.0.2 - Organizational and Configuration Updates
|
||||
This release cycle focused on organizational changes and configuration updates.
|
||||
|
||||
- Switched to new organization scheme.
|
||||
- Updated `npmextra.json` to include new git host information.
|
||||
- Made updates to TypeScript configuration.
|
||||
|
||||
## 2022-03-24 to 2022-07-18 - 2.0.0 to 2.0.1 - Core Updates and Fixes
|
||||
During these versions, significant internal changes were made with continued improvements.
|
||||
|
||||
- **BREAKING CHANGE:** Switched to ECMAScript Modules (ESM) format.
|
||||
- Multiple fixes in the core module, enhancing stability.
|
11488
pnpm-lock.yaml
generated
11488
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartpuppeteer',
|
||||
version: '2.0.3',
|
||||
description: 'simplified access to puppeteer'
|
||||
version: '2.0.4',
|
||||
description: 'Provides simplified access to Puppeteer for automation and testing purposes.'
|
||||
}
|
||||
|
@ -3,20 +3,24 @@ import * as plugins from './smartpuppeteer.plugins.js';
|
||||
|
||||
export class IncognitoBrowser {
|
||||
public status: 'started' | 'stopped' = 'stopped';
|
||||
public browser: plugins.puppeteer.Browser;
|
||||
public browser!: plugins.puppeteer.Browser;
|
||||
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* starts the IncognitoBrowser
|
||||
* Starts the IncognitoBrowser instance.
|
||||
* It launches the browser using environment-aware options and sets up a listener
|
||||
* to automatically re-launch if the browser disconnects while the status is 'started'.
|
||||
*/
|
||||
public async start() {
|
||||
public async start(): Promise<void> {
|
||||
this.status = 'started';
|
||||
this.browser = await getEnvAwareBrowserInstance();
|
||||
this.browser.on('disconnected', async (eventArg) => {
|
||||
this.browser.on('disconnected', async () => {
|
||||
try {
|
||||
this.browser.removeAllListeners();
|
||||
} catch (err) {}
|
||||
} catch (err) {
|
||||
// Optionally handle the error.
|
||||
}
|
||||
if (this.status === 'started') {
|
||||
this.browser = await getEnvAwareBrowserInstance();
|
||||
}
|
||||
@ -24,27 +28,41 @@ export class IncognitoBrowser {
|
||||
}
|
||||
|
||||
/**
|
||||
* stops the IncognitoBrowser
|
||||
* Stops the IncognitoBrowser instance.
|
||||
* It forcefully kills the browser process (if needed) and then closes the browser.
|
||||
*/
|
||||
public async stop() {
|
||||
public async stop(): Promise<void> {
|
||||
this.status = 'stopped';
|
||||
plugins.treeKill(this.browser.process()?.pid as number, 'SIGKILL');
|
||||
const pid = this.browser.process()?.pid;
|
||||
if (pid) {
|
||||
plugins.treeKill(pid, 'SIGKILL');
|
||||
}
|
||||
await this.browser.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* rotate
|
||||
* Rotates the browser instance.
|
||||
* It closes the current browser and launches a new one.
|
||||
*/
|
||||
public async rotateBrowser() {
|
||||
this.browser.close().catch();
|
||||
public async rotateBrowser(): Promise<void> {
|
||||
try {
|
||||
await this.browser.close();
|
||||
} catch (err) {
|
||||
// Ignore errors if the browser is already closed.
|
||||
}
|
||||
this.browser = await getEnvAwareBrowserInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new incognito browser context.
|
||||
* This uses Puppeteer's createIncognitoBrowserContext() API, which is the
|
||||
* correct method for creating isolated sessions.
|
||||
*/
|
||||
public async getNewIncognitoContext(): Promise<plugins.puppeteer.BrowserContext> {
|
||||
if (this.browser) {
|
||||
return this.browser.createIncognitoBrowserContext();
|
||||
} else {
|
||||
throw new Error('you need to start the IncognitoBrowser instance first');
|
||||
if (!this.browser) {
|
||||
throw new Error('You need to start the IncognitoBrowser instance first');
|
||||
}
|
||||
// @ts-ignore
|
||||
return this.browser.createIncognitoBrowserContext();
|
||||
}
|
||||
}
|
@ -9,32 +9,43 @@ export const getEnvAwareBrowserInstance = async (
|
||||
optionsArg: IEnvAwareOptions = {}
|
||||
): Promise<plugins.puppeteer.Browser> => {
|
||||
const options: IEnvAwareOptions = {
|
||||
...{
|
||||
forceNoSandbox: false,
|
||||
},
|
||||
forceNoSandbox: false,
|
||||
...optionsArg,
|
||||
};
|
||||
|
||||
let chromeArgs: string[] = [];
|
||||
if (process.env.CI || options.forceNoSandbox || plugins.os.userInfo().username === 'root') {
|
||||
if (
|
||||
process.env.CI ||
|
||||
options.forceNoSandbox ||
|
||||
plugins.os.userInfo().username === 'root'
|
||||
) {
|
||||
chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']);
|
||||
console.warn('********************************************************');
|
||||
console.warn('WARNING: Launching browser without sandbox. This can be insecure!');
|
||||
console.warn('********************************************************');
|
||||
}
|
||||
|
||||
let headlessBrowser: plugins.puppeteer.Browser;
|
||||
console.log('launching puppeteer bundled chrome with arguments:');
|
||||
// Automatically choose an executable if available: prefer google-chrome, then chromium, then chromium-browser.
|
||||
const execPath =
|
||||
plugins.smartshell.which.sync('google-chrome') ||
|
||||
plugins.smartshell.which.sync('chromium') ||
|
||||
plugins.smartshell.which.sync('chromium-browser');
|
||||
|
||||
const executablePathOptions = execPath ? { executablePath: execPath } : {};
|
||||
|
||||
console.log('Launching puppeteer browser with arguments:');
|
||||
console.log(chromeArgs);
|
||||
headlessBrowser = await plugins.puppeteer.launch({
|
||||
if (execPath) {
|
||||
console.log(`Using executable: ${execPath}`);
|
||||
} else {
|
||||
console.log('No specific browser executable found; falling back to Puppeteer default.');
|
||||
}
|
||||
|
||||
const headlessBrowser = await plugins.puppeteer.launch({
|
||||
args: chromeArgs,
|
||||
pipe: options.usePipe !== undefined ? options.usePipe : true,
|
||||
headless: true,
|
||||
...(() => {
|
||||
const returnObject: any = {};
|
||||
const googleChrome = plugins.smartshell.which.sync('google-chrome');
|
||||
if (googleChrome) {
|
||||
returnObject.executablePath = googleChrome;
|
||||
}
|
||||
return returnObject;
|
||||
})(),
|
||||
...executablePathOptions,
|
||||
});
|
||||
|
||||
return headlessBrowser;
|
||||
|
Loading…
x
Reference in New Issue
Block a user