-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathbrowser.js
More file actions
624 lines (550 loc) · 17.6 KB
/
browser.js
File metadata and controls
624 lines (550 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*******************************************************************************
Highcharts Export Server
Copyright (c) 2016-2025, Highsoft
Licenced under the MIT licence.
Additionally a valid Highcharts license is required for use.
See LICENSE file in root for details.
*******************************************************************************/
/**
* @overview This module provides functions for managing Puppeteer browser
* instance, creating and clearing pages, injecting custom JS and CSS resources,
* and setting up Highcharts for server-side rendering. The module ensures
* that the browser and pages are correctly managed and can handle failures
* during operations like launching the browser or creating new pages.
*/
import { readFileSync } from 'fs';
import { join } from 'path';
import puppeteer from 'puppeteer';
import { getCachePath } from './cache.js';
import { getOptions } from './config.js';
import { setupHighcharts } from './highcharts.js';
import { log, logWithStack } from './logger.js';
import { __dirname, expBackoff, getAbsolutePath } from './utils.js';
import { envs } from './validation.js';
import ExportError from './errors/ExportError.js';
// Get the template for pages
const pageTemplate = readFileSync(
join(__dirname, 'templates', 'template.html'),
'utf8'
);
// To save the browser
let browser = null;
// To save the WebSocket endpoint in case of a sudden disconnect
let wsEndpoint;
/**
* Retrieves the existing Puppeteer browser instance.
*
* @function getBrowser
*
* @returns {Object} The Puppeteer browser instance.
*
* @throws {ExportError} Throws an `ExportError` if no valid browser
* has been created.
*/
export function getBrowser() {
if (!browser) {
throw new ExportError('[browser] No valid browser has been created.', 500);
}
return browser;
}
/**
* Creates a Puppeteer browser instance with the specified arguments.
*
* @async
* @function createBrowser
*
* @param {Array<string>} puppeteerArgs - Additional arguments for Puppeteer
* browser's launch.
*
* @returns {Promise<Object>} A Promise that resolves to the created Puppeteer
* browser instance.
*
* @throws {ExportError} Throws an `ExportError` if max retries to open
* a browser instance are reached, or if no browser instance is found after
* retries.
*/
export async function createBrowser(puppeteerArgs) {
// Get `debug` and `other` options
const { debug, other } = getOptions();
// Get the `debug` options
const { enable: enabledDebug, ...debugOptions } = debug;
// Launch options for the browser instance
const launchOptions = {
headless: other.browserShellMode ? 'shell' : true,
userDataDir: 'tmp',
args: puppeteerArgs || [],
// Must be disabled for debugging to work
pipe: envs.OTHER_CONNECTION_OVER_PIPE,
handleSIGINT: false,
handleSIGTERM: false,
handleSIGHUP: false,
waitForInitialPage: false,
defaultViewport: null,
...(enabledDebug && debugOptions)
};
// Create a browser
if (!browser) {
// A counter for the browser's launch retries
let tryCount = 0;
const openBrowser = async () => {
try {
log(
3,
`[browser] Attempting to launch and get a browser instance (try ${++tryCount}).`
);
// Launch the browser
browser = await puppeteer.launch(launchOptions);
// Close the initial pages if any found
const pages = await browser.pages();
if (pages) {
for (const page of pages) {
await page.close();
}
}
// Only for the WebSocket connection
if (!launchOptions.pipe) {
// Save WebSocket endpoint
wsEndpoint = browser.wsEndpoint();
// Attach the disconnected event
browser.on('disconnected', _reconnect);
}
} catch (error) {
logWithStack(
1,
error,
'[browser] Failed to launch a browser instance.'
);
// Retry to launch browser until reaching max attempts
if (tryCount < 25) {
log(3, `[browser] Retry to open a browser (${tryCount} out of 25).`);
// Wait for a 4 seconds before trying again
await new Promise((response) => setTimeout(response, 4000));
await openBrowser();
} else {
throw error;
}
}
};
try {
// Try to open a browser
await openBrowser();
// Shell mode inform
if (launchOptions.headless === 'shell') {
log(3, `[browser] Launched browser in shell mode.`);
}
// Debug mode inform
if (enabledDebug) {
log(3, `[browser] Launched browser in debug mode.`);
}
} catch (error) {
throw new ExportError(
'[browser] Maximum retries to open a browser instance reached.',
500
).setError(error);
}
// No correct browser
if (!browser) {
throw new ExportError('[browser] Cannot find a browser to open.', 500);
}
}
// Return a browser instance
return browser;
}
/**
* Closes the Puppeteer browser instance if it is connected.
*
* @async
* @function closeBrowser
*/
export async function closeBrowser() {
// Close the browser when connected
if (browser && browser.connected) {
await browser.close();
}
browser = null;
log(4, '[browser] Closed the browser.');
}
/**
* Creates a new Puppeteer page within an existing browser instance.
* The function creates a new page, disables caching, sets content using
* the `_setPageContent()`, and returns the created Puppeteer page.
*
* @async
* @function newPage
*
* @param {Object} poolResource - The pool resource that contains `id`,
* `workCount`, and `page`.
*
* @throws {ExportError} Throws an `ExportError` if no valid browser
* has been connected or if a page is invalid or closed.
*/
export async function newPage(poolResource) {
// Error in case of no connected browser
if (!browser || !browser.connected) {
throw new ExportError(`[browser] Browser is not yet connected.`, 500);
}
// Create a page
poolResource.page = await browser.newPage();
// Disable cache
await poolResource.page.setCacheEnabled(false);
// Set the content
await _setPageContent(poolResource.page);
// Set page events
_setPageEvents(poolResource);
// Check if the page is correctly created
if (!poolResource.page || poolResource.page.isClosed()) {
throw new ExportError('[browser] The page is invalid or closed.', 400);
}
}
/**
* Clears the content of a Puppeteer page based on the specified mode. Logs
* thrown error if clearing of a page's content fails.
*
* @async
* @function clearPage
*
* @param {Object} poolResource - The pool resource that contains page and id.
* @param {boolean} [hardReset=false] - A flag indicating the type of clearing
* to be performed. If `true`, navigates to `about:blank` and resets content
* and scripts. If `false`, clears the body content by setting a predefined HTML
* structure. The default value is `false`.
*
* @returns {Promise<boolean>} A Promise that resolves to `true` when page
* is correctly cleared and `false` when it is not.
*/
export async function clearPage(poolResource, hardReset = false) {
try {
if (poolResource.page && !poolResource.page.isClosed()) {
if (hardReset) {
// Navigate to `about:blank`
await poolResource.page.goto('about:blank', {
waitUntil: 'domcontentloaded'
});
// Set the content and and scripts again
await _setPageContent(poolResource.page);
} else {
// Clear body content
await poolResource.page.evaluate(() => {
document.body.innerHTML =
'<div id="chart-container"><div id="container"></div></div>';
});
}
return true;
}
} catch (error) {
logWithStack(
2,
error,
`[pool] Pool resource [${poolResource.id}] - Content of the page could not be cleared.`
);
// Set the `workLimit` to exceeded in order to recreate the resource
poolResource.workCount = getOptions().pool.workLimit + 1;
}
return false;
}
/**
* Adds custom JS and CSS resources to a Puppeteer page based on the specified
* options.
*
* @async
* @function addPageResources
*
* @param {Object} page - The Puppeteer page object to which resources will
* be added.
* @param {Object} customLogicOptions - The configuration object containing
* `customLogic` options.
*
* @returns {Promise<Array<Object>>} A Promise that resolves to an array
* of injected resources.
*/
export async function addPageResources(page, customLogicOptions) {
// Injected resources array
const injectedResources = [];
// Use the content of the `resources`
const resources = customLogicOptions.resources;
if (resources) {
const injectedJs = [];
// Load custom JS code
if (resources.js) {
injectedJs.push({
content: resources.js
});
}
// Load scripts from all custom files
if (resources.files) {
for (const file of resources.files) {
const isLocal = file.startsWith('http') ? false : true;
// Add each custom script from resources' files
injectedJs.push(
isLocal
? {
content: readFileSync(getAbsolutePath(file), 'utf8')
}
: {
url: file
}
);
}
}
// The actual injection of collected scripts
for (const jsResource of injectedJs) {
try {
injectedResources.push(await page.addScriptTag(jsResource));
} catch (error) {
logWithStack(2, error, `[browser] The JS resource cannot be loaded.`);
}
}
injectedJs.length = 0;
// Load CSS
const injectedCss = [];
if (resources.css) {
const cssImports = resources.css.match(/@import\s*([^;]*);/g);
if (cssImports) {
// Handle css section
for (let cssImportPath of cssImports) {
if (cssImportPath) {
cssImportPath = cssImportPath
.replace('url(', '')
.replace('@import', '')
.replace(/"/g, '')
.replace(/'/g, '')
.replace(/;/, '')
.replace(/\)/g, '')
.trim();
// Add each custom css from resources
if (cssImportPath.startsWith('http')) {
injectedCss.push({
url: cssImportPath
});
} else if (customLogicOptions.allowFileResources) {
injectedCss.push({
path: getAbsolutePath(cssImportPath)
});
}
}
}
}
// The rest of the CSS section will be content by now
injectedCss.push({
content: resources.css.replace(/@import\s*([^;]*);/g, '') || ' '
});
// The actual injection of collected CSS
for (const cssResource of injectedCss) {
try {
injectedResources.push(await page.addStyleTag(cssResource));
} catch (error) {
logWithStack(
2,
error,
`[browser] The CSS resource cannot be loaded.`
);
}
}
injectedCss.length = 0;
}
}
return injectedResources;
}
/**
* Clears out all state set on the page with `addScriptTag` and `addStyleTag`.
* Removes injected resources and resets CSS and script tags on the page.
* Additionally, it destroys previously existing charts.
*
* @async
* @function clearPageResources
*
* @param {Object} page - The Puppeteer page object from which resources will
* be cleared.
* @param {Array<Object>} injectedResources - Array of injected resources
* to be cleared.
*/
export async function clearPageResources(page, injectedResources) {
try {
for (const resource of injectedResources) {
await resource.dispose();
}
// Destroy old charts after export is done and reset all CSS and script tags
await page.evaluate(() => {
// We are not guaranteed that Highcharts is loaded, when doing SVG exports
if (typeof Highcharts !== 'undefined') {
// eslint-disable-next-line no-undef
const oldCharts = Highcharts.charts;
// Check in any already existing charts
if (Array.isArray(oldCharts) && oldCharts.length) {
// Destroy old charts
for (const oldChart of oldCharts) {
oldChart && oldChart.destroy();
// eslint-disable-next-line no-undef
Highcharts.charts.shift();
}
}
}
// eslint-disable-next-line no-undef
const [...scriptsToRemove] = document.getElementsByTagName('script');
// eslint-disable-next-line no-undef
const [, ...stylesToRemove] = document.getElementsByTagName('style');
// eslint-disable-next-line no-undef
const [...linksToRemove] = document.getElementsByTagName('link');
// Remove tags
for (const element of [
...scriptsToRemove,
...stylesToRemove,
...linksToRemove
]) {
element.remove();
}
});
} catch (error) {
logWithStack(2, error, `[browser] Could not clear page's resources.`);
}
}
/**
* Reconnects to the browser instance when it is disconnected. If the current
* browser connection is lost, it attempts to reconnect using the previous
* WebSocket endpoint. If the reconnection fails, it will try to close
* the browser and relaunch a new instance.
*
* @async
* @function _reconnect
*/
async function _reconnect() {
try {
// Start the reconnecting
log(3, `[browser] Restarting the browser connection.`);
// Try to reconnect the browser
if (browser && !browser.connected) {
browser = await puppeteer.connect({
browserWSEndpoint: wsEndpoint
});
}
// Save a new WebSocket endpoint
wsEndpoint = browser.wsEndpoint();
// Add the reconnect event again
browser.on('disconnected', _reconnect);
// Log the success message
log(3, `[browser] Browser reconnected successfully.`);
} catch (error) {
logWithStack(
1,
error,
'[browser] Could not restore the browser connection, attempting to relaunch.'
);
// Try to close the browser before relaunching
try {
await close();
} catch (error) {
logWithStack(
1,
error,
'[browser] Could not close the browser before relaunching (probably is already closed).'
);
}
// Try to relaunch the browser
await createBrowser(getOptions().puppeteer.args || []);
// Log the success message
log(3, `[browser] Browser relaunched successfully.`);
}
}
/**
* Sets the content for a Puppeteer page using a predefined template
* and additional scripts.
*
* @async
* @function _setPageContent
*
* @param {Object} page - The Puppeteer page object to which the content
* is being set.
*/
async function _setPageContent(page) {
// Set the initial page content
await page.setContent(pageTemplate, { waitUntil: 'domcontentloaded' });
// Add all registered Higcharts scripts, quite demanding
await page.addScriptTag({ path: join(getCachePath(), 'sources.js') });
// Set the initial `animObject` for Highcharts
await page.evaluate(setupHighcharts);
}
/**
* Set events (like `pageerror` and `console`) for a Puppeteer page in order
* to catch and display errors and console logs from the window context.
*
* @function _setPageEvents
*
* @param {Object} poolResource - The pool resource that contains `id`,
* `workCount`, and `page`.
*/
function _setPageEvents(poolResource) {
// Get `debug` options
const { debug, pool } = getOptions();
// Set the `pageerror` listener
poolResource.page.on('pageerror', async () => {
// It would seem like this may fire at the same time or shortly before
// a page is closed.
if (poolResource.page.isClosed()) {
return;
}
});
// Set the `console` listener, if needed
if (debug.enable && debug.listenToConsole) {
poolResource.page.on('console', (message) => {
console.log(`[debug] ${message.text()}`);
});
}
// Add the framedetached event if the connection is over WebSocket
if (envs.OTHER_CONNECTION_OVER_PIPE === false) {
poolResource.page.on('framedetached', async (frame) => {
// Get the main frame
const mainFrame = poolResource.page.mainFrame();
// Check if a page's frame is detached and requires to be recreated
if (
frame === mainFrame &&
mainFrame.detached &&
poolResource.workCount <= pool.workLimit
) {
log(
3,
`[browser] Pool resource [${poolResource.id}] - Page's frame detached.`
);
try {
// Try to connect to a new page using exponential backoff strategy
expBackoff(
async (poolResourceId, poolResource) => {
try {
// Try to close the page with a detached frame
if (!poolResource.page.isClosed()) {
await poolResource.page.close();
}
} catch (error) {
log(
3,
`[browser] Pool resource [${poolResourceId}] - Could not close the page with a detached frame.`
);
}
// Trigger a page creation
await newPage(poolResource);
},
0,
poolResource.id,
poolResource
);
} catch (error) {
logWithStack(
3,
error,
`[browser] Pool resource [${poolResource.id}] - Could not create a new page.`
);
// Set the `workLimit` to exceeded in order to recreate the resource
poolResource.workCount = pool.workLimit + 1;
}
}
});
}
}
export default {
getBrowser,
createBrowser,
closeBrowser,
newPage,
clearPage,
addPageResources,
clearPageResources
};