Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement/dev-tools-protocol-limit #622

Open
wants to merge 3 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions dist/index.cjs

This file was deleted.

2 changes: 0 additions & 2 deletions dist/index.esm.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/index.esm.js.map

This file was deleted.

74 changes: 39 additions & 35 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,45 +331,49 @@ export async function addPageResources(page, options) {
* to be cleared.
*/
export async function clearPageResources(page, injectedResources) {
for (const resource of injectedResources) {
await resource.dispose();
}
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, e,g, 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();
// 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, e,g, 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();
}
});
// 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.`);
}
}

/**
Expand Down
30 changes: 28 additions & 2 deletions lib/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,34 @@ const createSVG = (page) =>
*
* @returns {Promise<void>} Promise resolving after the configuration is set.
*/
const setAsConfig = async (page, chart, options, displayErrors) =>
page.evaluate(triggerExport, chart, options, displayErrors);
const setAsConfig = async (page, chart, options, displayErrors) => {
// Get rid of the redunant string data
options.export.instr = null;
options.export.infile = null;

// Get the size of the export input
const totalSize = Buffer.byteLength(
options.export?.strInj ? options.export?.strInj : JSON.stringify(chart),
'utf-8'
);

// Log the size in MB
log(
3,
`[export] The current total size of data passed to a page is around ${(
totalSize /
(1024 * 1024)
).toFixed(2)} MB`
);

// Check the size of data passed to the page
if (totalSize >= 100 * 1024 * 1024) {
throw new ExportError(`[export] The data passed to a page exceeded 100MB.`);
}

// Trigger the Highcharts chart creation
return page.evaluate(triggerExport, chart, options, displayErrors);
};

/**
* Exports to a chart from a page using Puppeteer.
Expand Down
6 changes: 1 addition & 5 deletions lib/highcharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ export async function triggerExport(chartOptions, options, displayErrors) {
let constr = options.export.constr || 'chart';
constr = typeof Highcharts[constr] !== 'undefined' ? constr : 'chart';

Highcharts[constr](
'container',
finalOptions,
finalCallback
);
Highcharts[constr]('container', finalOptions, finalCallback);

// Get the current global options
const defaultOptions = getOptions();
Expand Down
Loading