fix(IncognitoBrowser): Enhance IncognitoBrowser error handling and process management
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user