Skip to content

Commit 76722d0

Browse files
authored
Merge pull request #622 from highcharts/enhancement/dev-tools-protocol-limit
enhancement/dev-tools-protocol-limit
2 parents 69ea56b + 53a2e2f commit 76722d0

6 files changed

Lines changed: 75 additions & 44 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ _Fixes:_
1111
- Fixed an issue where the chart constructor was sometimes incorrectly set, causing the export to fail
1212
- Added referrers to CDN cache fetches on first startup/install.
1313
- Fixed an issue that would sometimes cause cause a crash due to fail due to `Accept-Ranges` headers
14-
- Corrected the `Node.js Module` example in the README.
14+
- Wrapped the `clearPageResources` function in a try-catch to handle potential page resources errors.
15+
- Secured against errors caused by `dev-tools` protocol data size limitations.
16+
- Corrected the `Node.js Module` example in the README.
1517
- Fixed the warning message when the the default `resources.json` file is not found.
1618
- Fixed the problem with the lack of the `instr` value, when the `options` is set instead
1719

@@ -20,7 +22,6 @@ _New Features:_
2022
- Added proxy authentication [(#631)](https://github.com/highcharts/node-export-server/issues/631).
2123
- Made the temporary Puppeteer directory (`PUPPETEER_TEMP_DIR`) (till now, `'./tmp'`) configurable by the user [(#567)](https://github.com/highcharts/node-export-server/issues/567).
2224

23-
2425
# 4.0.2
2526

2627
_Hotfix_:

dist/index.cjs

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

dist/index.esm.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.esm.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/browser.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -331,45 +331,49 @@ export async function addPageResources(page, options) {
331331
* to be cleared.
332332
*/
333333
export async function clearPageResources(page, injectedResources) {
334-
for (const resource of injectedResources) {
335-
await resource.dispose();
336-
}
334+
try {
335+
for (const resource of injectedResources) {
336+
await resource.dispose();
337+
}
337338

338-
// Destroy old charts after export is done and reset all CSS and script tags
339-
await page.evaluate(() => {
340-
// We are not guaranteed that Highcharts is loaded, e,g, when doing SVG
341-
// exports
342-
if (typeof Highcharts !== 'undefined') {
343-
// eslint-disable-next-line no-undef
344-
const oldCharts = Highcharts.charts;
345-
346-
// Check in any already existing charts
347-
if (Array.isArray(oldCharts) && oldCharts.length) {
348-
// Destroy old charts
349-
for (const oldChart of oldCharts) {
350-
oldChart && oldChart.destroy();
351-
// eslint-disable-next-line no-undef
352-
Highcharts.charts.shift();
339+
// Destroy old charts after export is done and reset all CSS and script tags
340+
await page.evaluate(() => {
341+
// We are not guaranteed that Highcharts is loaded, e,g, when doing SVG
342+
// exports
343+
if (typeof Highcharts !== 'undefined') {
344+
// eslint-disable-next-line no-undef
345+
const oldCharts = Highcharts.charts;
346+
347+
// Check in any already existing charts
348+
if (Array.isArray(oldCharts) && oldCharts.length) {
349+
// Destroy old charts
350+
for (const oldChart of oldCharts) {
351+
oldChart && oldChart.destroy();
352+
// eslint-disable-next-line no-undef
353+
Highcharts.charts.shift();
354+
}
353355
}
354356
}
355-
}
356357

357-
// eslint-disable-next-line no-undef
358-
const [...scriptsToRemove] = document.getElementsByTagName('script');
359-
// eslint-disable-next-line no-undef
360-
const [, ...stylesToRemove] = document.getElementsByTagName('style');
361-
// eslint-disable-next-line no-undef
362-
const [...linksToRemove] = document.getElementsByTagName('link');
363-
364-
// Remove tags
365-
for (const element of [
366-
...scriptsToRemove,
367-
...stylesToRemove,
368-
...linksToRemove
369-
]) {
370-
element.remove();
371-
}
372-
});
358+
// eslint-disable-next-line no-undef
359+
const [...scriptsToRemove] = document.getElementsByTagName('script');
360+
// eslint-disable-next-line no-undef
361+
const [, ...stylesToRemove] = document.getElementsByTagName('style');
362+
// eslint-disable-next-line no-undef
363+
const [...linksToRemove] = document.getElementsByTagName('link');
364+
365+
// Remove tags
366+
for (const element of [
367+
...scriptsToRemove,
368+
...stylesToRemove,
369+
...linksToRemove
370+
]) {
371+
element.remove();
372+
}
373+
});
374+
} catch (error) {
375+
logWithStack(2, error, `[browser] Could not clear page's resources.`);
376+
}
373377
}
374378

375379
/**

lib/export.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,34 @@ const createSVG = (page) =>
128128
*
129129
* @returns {Promise<void>} Promise resolving after the configuration is set.
130130
*/
131-
const setAsConfig = async (page, chart, options, displayErrors) =>
132-
page.evaluate(triggerExport, chart, options, displayErrors);
131+
const setAsConfig = async (page, chart, options, displayErrors) => {
132+
// Get rid of the redunant string data
133+
options.export.instr = null;
134+
options.export.infile = null;
135+
136+
// Get the size of the export input
137+
const totalSize = Buffer.byteLength(
138+
options.export?.strInj ? options.export?.strInj : JSON.stringify(chart),
139+
'utf-8'
140+
);
141+
142+
// Log the size in MB
143+
log(
144+
3,
145+
`[export] The current total size of data passed to a page is around ${(
146+
totalSize /
147+
(1024 * 1024)
148+
).toFixed(2)} MB`
149+
);
150+
151+
// Check the size of data passed to the page
152+
if (totalSize >= 100 * 1024 * 1024) {
153+
throw new ExportError(`[export] The data passed to a page exceeded 100MB.`);
154+
}
155+
156+
// Trigger the Highcharts chart creation
157+
return page.evaluate(triggerExport, chart, options, displayErrors);
158+
};
133159

134160
/**
135161
* Exports to a chart from a page using Puppeteer.

0 commit comments

Comments
 (0)