libs/scully/src/lib/renderPlugins/launchedBrowser.js
/**
 * Function that creates an observable with the puppeteer browser inside
 * @param options
 */
Function that creates an observable with the puppeteer browser inside
@param options
Function that creates an observable with the puppeteer browser inside @param options
function obsBrowser(options = scullyConfig.puppeteerLaunchOptions || {}) {
    if (showBrowser) {
        options.headless = false;
    }
    options.ignoreHTTPSErrors = true;
    options.args = options.args || [];
    const { SCULLY_PUPPETEER_EXECUTABLE_PATH } = process.env;
    if (SCULLY_PUPPETEER_EXECUTABLE_PATH) {
        log(`Launching puppeteer with executablePath ${SCULLY_PUPPETEER_EXECUTABLE_PATH}`);
        options.executablePath = SCULLY_PUPPETEER_EXECUTABLE_PATH;
        options.args = [...options.args, '--disable-dev-shm-usage'];
    }
    let isLaunching = false;
    return new Observable((obs) => {
        const startPupetteer = () => {
            if (!isLaunching) {
                isLaunching = true;
                launchPuppeteerWithRetry(options).then((b) => {
                    browser = b;
                    b.on('disconnected', () => reLaunch('disconnect'));
                    obs.next(b);
                    setTimeout(() => (isLaunching = false), 1000);
                });
                `Puppeteer launch error`, e);
                ;
            }
        };
        launches
            .pipe(
        filter(() => !isLaunching), 
        throttleTime(15000), 
        delayWhen(() => merge(
        timer(25000), 
        interval(500).pipe(
        switchMap(() => (browser ? browser.pages() : of([]))), 
        filter((p) => browser === undefined || p.length <= 1))).pipe(
        take(1), 
        catchError(() => of([])))))
            .subscribe({
            next: () => {
                try {
                    if (browser && browser.process() != null) {
                        browser.process().kill('SIGINT');
                    }
                }
                catch (_a) {
                }
                startPupetteer();
            },
        });
        return () => {
            if (browser) {
                browser.close();
                browser = undefined;
            }
        };
    });
}
function obsBrowser ( options = scullyConfig . puppeteerLaunchOptions || { } ) { if ( showBrowser ) { options . headless = false ; } options . ignoreHTTPSErrors = true ; options . args = options . args || [ ] ; const { SCULLY_PUPPETEER_EXECUTABLE_PATH } = process . env ; if ( SCULLY_PUPPETEER_EXECUTABLE_PATH ) { log ( ` ${ SCULLY_PUPPETEER_EXECUTABLE_PATH } ` ) ; options . executablePath = SCULLY_PUPPETEER_EXECUTABLE_PATH ; options . args = [ ... options . args , '--disable-dev-shm-usage' ] ; } let isLaunching = false ; return new Observable ( ( obs ) => { const startPupetteer = ( ) => { if ( ! isLaunching ) { isLaunching = true ; launchPuppeteerWithRetry ( options ) . then ( ( b ) => { browser = b ; b . on ( 'disconnected' , ( ) => reLaunch ( 'disconnect' ) ) ; obs . next ( b ) ; setTimeout ( ( ) => ( isLaunching = false ) , 1000 ) ; } ) ; } } ; launches . pipe ( filter ( ( ) => ! isLaunching ) , throttleTime ( 15000 ) , delayWhen ( ( ) => merge ( timer ( 25000 ) , interval ( 500 ) . pipe ( switchMap ( ( ) => ( browser ? browser . pages ( ) : of ( [ ] ) ) ) , filter ( ( p ) => browser === undefined || p . length <= 1 ) ) ) . pipe ( take ( 1 ) , catchError ( ( ) => of ( [ ] ) ) ) ) ) . subscribe ( { next : ( ) => { try { if ( browser && browser . process ( ) != null ) { browser . process ( ) . kill ( 'SIGINT' ) ; } } catch ( _a ) { } startPupetteer ( ) ; } , } ) ; return ( ) => { if ( browser ) { browser . close ( ) ; browser = undefined ; } } ; } ) ; }
Function that creates an observable with the puppeteer browser inside
 @param options
Function that creates an observable with the puppeteer browser inside @param options
// options.args = ['--no-sandbox', '--disable-setuid-sandbox']; /** I will only come here when puppeteer is actually launched */ /** only allow a relaunch in a next cycle */ // launch(options) //   .then((b) => { //     browser = b; //     b.on('disconnected', () => reLaunch('disconnect')); //     // logWarn(green('Browser successfully launched')); //     obs.next(b); //     setTimeout(() => (isLaunching = false), 1000); //     /** reset fail counter on successful launch */ //     failedLaunces = 0; //     return b; //   }) //   .catch((e) => { //     if (e.message.includes('Could not find browser revision')) { //       logError( //         `Puppeteer cannot find chromium installation.  Try adding 'puppeteerLaunchOptions: {executablePath: CHROMIUM_PATH}' to your scully.*.config.ts file.` //       ); //     } else if (++failedLaunces < 3) { //       logError(`Puppeteer launch error`, e); //       return launches.next(); //     } //     captureException(e); //     logError(`Puppeteer launch error.`, e); //     obs.error(e); //     process.exit(15); //   }); /** ignore request while the browser is already starting, we can only launch 1 */ /** the long throttleTime is to cater for the concurrently running browsers to crash and burn. */ // provide enough time for the current async operations to finish before killing the current browser instance /** timout at 25 seconds */ /** or then the number of pages hits <=1  */ /** if the browser is active get the pages promise */ /** only go ahead when there is <=1 pages (the browser itself) */ /** use take 1 to make sure we complete when one of the above fires */ /** if something _really_ unwieldy happens with the browser, ignore and go ahead */ /** ignored */