@@ -21,43 +21,37 @@ export interface RenderResponse {
2121 filePath : string ;
2222}
2323
24- export interface BrowserTimings {
25- launch ( callback : ( ) => Promise < PuppeteerBrowser > ) : Promise < PuppeteerBrowser > ;
26- newPage ( callback : ( ) => Promise < Page > ) : Promise < Page > ;
27- navigate ( callback : ( ) => Promise < void > ) : Promise < void > ;
28- panelsRendered ( callback : ( ) => Promise < void > ) : Promise < void > ;
29- screenshot ( callback : ( ) => Promise < void > ) : Promise < void > ;
30- }
31-
32- export class NoOpBrowserTiming {
33- async launch ( callback : ( ) => Promise < PuppeteerBrowser > ) {
34- return await callback ( ) ;
35- }
36-
37- async newPage ( callback : ( ) => Promise < void > ) {
38- return await callback ( ) ;
39- }
40-
41- async navigate ( callback : ( ) => Promise < void > ) {
42- return await callback ( ) ;
43- }
44-
45- async panelsRendered ( callback : ( ) => Promise < void > ) {
46- return await callback ( ) ;
47- }
48-
49- async screenshot ( callback : ( ) => Promise < void > ) {
50- return await callback ( ) ;
51- }
52- }
53-
5424export class Browser {
55- constructor ( protected config : RenderingConfig , protected log : Logger , protected timings : BrowserTimings ) { }
25+ constructor ( protected config : RenderingConfig , protected log : Logger ) {
26+ this . log . info (
27+ 'Browser initiated' ,
28+ 'chromeBin' ,
29+ this . config . chromeBin ,
30+ 'ignoresHttpsErrors' ,
31+ this . config . ignoresHttpsErrors ,
32+ 'timezone' ,
33+ this . config . timezone ,
34+ 'args' ,
35+ this . config . args ,
36+ 'dumpio' ,
37+ this . config . dumpio ,
38+ 'verboseLogging' ,
39+ this . config . verboseLogging
40+ ) ;
41+ }
5642
5743 async getBrowserVersion ( ) : Promise < string > {
58- const launcherOptions = this . getLauncherOptions ( { } ) ;
59- const browser = await puppeteer . launch ( launcherOptions ) ;
60- return browser . version ( ) ;
44+ let browser ;
45+
46+ try {
47+ const launcherOptions = this . getLauncherOptions ( { } ) ;
48+ browser = await puppeteer . launch ( launcherOptions ) ;
49+ return await browser . version ( ) ;
50+ } finally {
51+ if ( browser ) {
52+ await browser . close ( ) ;
53+ }
54+ }
6155 }
6256
6357 async start ( ) : Promise < void > { }
@@ -84,7 +78,8 @@ export class Browser {
8478 const launcherOptions : any = {
8579 env : env ,
8680 ignoreHTTPSErrors : this . config . ignoresHttpsErrors ,
87- args : [ '--no-sandbox' ] ,
81+ dumpio : this . config . dumpio ,
82+ args : this . config . args ,
8883 } ;
8984
9085 if ( this . config . chromeBin ) {
@@ -101,18 +96,8 @@ export class Browser {
10196 try {
10297 this . validateOptions ( options ) ;
10398 const launcherOptions = this . getLauncherOptions ( options ) ;
104-
105- browser = await this . timings . launch (
106- async ( ) =>
107- // launch browser
108- await puppeteer . launch ( launcherOptions )
109- ) ;
110- page = await this . timings . newPage (
111- async ( ) =>
112- // open a new page
113- await browser . newPage ( )
114- ) ;
115-
99+ browser = await puppeteer . launch ( launcherOptions ) ;
100+ page = await browser . newPage ( ) ;
116101 this . addPageListeners ( page ) ;
117102
118103 return await this . takeScreenshot ( page , options ) ;
@@ -139,32 +124,22 @@ export class Browser {
139124 domain : options . domain ,
140125 } ) ;
141126 await page . mouse . move ( options . width , options . height ) ;
142-
143- await this . timings . navigate ( async ( ) => {
144- // wait until all data was loaded
145- await page . goto ( options . url , { waitUntil : 'networkidle0' } ) ;
146- } ) ;
147-
148- await this . timings . panelsRendered ( async ( ) => {
149- // wait for all panels to render
150- await page . waitForFunction (
151- ( ) => {
152- const panelCount = document . querySelectorAll ( '.panel' ) . length || document . querySelectorAll ( '.panel-container' ) . length ;
153- return ( window as any ) . panelsRendered >= panelCount ;
154- } ,
155- {
156- timeout : options . timeout * 1000 ,
157- }
158- ) ;
159- } ) ;
127+ await page . goto ( options . url , { waitUntil : 'networkidle0' } ) ;
128+ await page . waitForFunction (
129+ ( ) => {
130+ const panelCount = document . querySelectorAll ( '.panel' ) . length || document . querySelectorAll ( '.panel-container' ) . length ;
131+ return ( window as any ) . panelsRendered >= panelCount ;
132+ } ,
133+ {
134+ timeout : options . timeout * 1000 ,
135+ }
136+ ) ;
160137
161138 if ( ! options . filePath ) {
162139 options . filePath = uniqueFilename ( os . tmpdir ( ) ) + '.png' ;
163140 }
164141
165- await this . timings . screenshot ( async ( ) => {
166- await page . screenshot ( { path : options . filePath } ) ;
167- } ) ;
142+ await page . screenshot ( { path : options . filePath } ) ;
168143
169144 return { filePath : options . filePath } ;
170145 }
0 commit comments