Skip to content

Commit

Permalink
Added docs for debug mode along with further corrections.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulDalek committed May 13, 2024
1 parent 067e026 commit df5f150
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 36 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ The format, along with its default values, is as follows (using the recommended
"listenToProcessExits": true,
"noLogo": false,
"hardResetPage": false
},
"debug": {
"enable": false,
"headless": true,
"devtools": false,
"listenToConsole": false,
"dumpio": false,
"slowMo": 0,
"debuggingPort": 9222
}
}
```
Expand Down Expand Up @@ -353,6 +362,15 @@ These variables are set in your environment and take precedence over options fro
- `OTHER_NO_LOGO`: Skip printing the logo on a startup. Will be replaced by a simple text (defaults to `false`).
- `OTHER_HARD_RESET_PAGE`: Determines whether the page's content should be reset from scratch, including Highcharts scripts (defaults to `false`).

### Debugging Config
- `DEBUG_ENABLE`: Enables or disables debug mode for the underlying browser (defaults to `false`).
- `DEBUG_HEADLESS`: Controls the mode in which the browser is launched when in the debug mode (defaults to `true`).
- `DEBUG_DEVTOOLS`: Decides whether to enable DevTools when the browser is in a headful state (defaults to `false`).
- `DEBUG_LISTEN_TO_CONSOLE`: Decides whether to enable a listener for console messages sent from the browser (defaults to `false`).
- `DEBUG_DUMPIO`: Redirects browser process stdout and stderr to process.stdout and process.stderr (defaults to `false`).
- `DEBUG_SLOW_MO`: Slows down Puppeteer operations by the specified number of milliseconds (defaults to `0`).
- `DEBUG_DEBUGGING_PORT`: Specifies the debugging port (defaults to `9222`).

## Command Line Arguments

To supply command line arguments, add them as flags when running the application:
Expand Down Expand Up @@ -417,6 +435,13 @@ _Available options:_
- `--listenToProcessExits`: Decides whether or not to attach process.exit handlers (defaults to `true`).
- `--noLogo`: Skip printing the logo on a startup. Will be replaced by a simple text (defaults to `false`).
- `--hardResetPage`: Determines whether the page's content should be reset from scratch, including Highcharts scripts (defaults to `false`).
- `--enableDebug`: Enables or disables debug mode for the underlying browser (defaults to `false`).
- `--headless`: Controls the mode in which the browser is launched when in the debug mode (defaults to `true`).
- `--devtools`: Decides whether to enable DevTools when the browser is in a headful state (defaults to `false`).
- `--listenToConsole`: Decides whether to enable a listener for console messages sent from the browser (defaults to `false`).
- `--dumpio`: Redirects browser process stdout and stderr to process.stdout and process.stderr (defaults to `false`).
- `--slowMo`: Slows down Puppeteer operations by the specified number of milliseconds (defaults to `0`).
- `--debuggingPort`: Specifies the debugging port (defaults to `9222`).

# HTTP Server

Expand Down Expand Up @@ -555,7 +580,7 @@ This package supports both CommonJS and ES modules.
**highcharts-export-server module**

- `server`: The server instance which offers the following functions:
- `async startServer(serverConfig)`: The same as `startServer` describe below.
- `async startServer(serverConfig)`: The same as `startServer` described below.

- `closeServers()`: Closes all servers associated with Express app instance.

Expand Down Expand Up @@ -596,6 +621,11 @@ This package supports both CommonJS and ES modules.
- `{Object} settings`: The settings object containing export configuration.
- `{function} endCallback`: The callback function to be invoked upon finalizing work or upon error occurance of the exporting process.

- `async initPool(config)`: Initializes the export pool with the provided configuration, creating a browser instance and setting up worker resources.
- `{Object} config`: Configuration options for the export pool along with custom puppeteer arguments for the puppeteer.launch function.

- `async killPool()`: Kills all workers in the pool, destroys the pool, and closes the browser instance.

- `setOptions(userOptions, args)`: Initializes and sets the general options for the server instace, keeping the principle of the options load priority. It accepts optional userOptions and args from the CLI.
- `{Object} userOptions`: User-provided options for customization.
- `{Array} args`: Command-line arguments for additional configuration (CLI usage).
Expand Down
4 changes: 2 additions & 2 deletions dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.esm.js.map

Large diffs are not rendered by default.

36 changes: 28 additions & 8 deletions lib/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,28 @@ const createImage = (page, type, encoding, clip, rasterizationTimeout) =>
*
* @returns {Promise<Buffer>} Promise resolving to the PDF buffer.
*/
const createPDF = async (page, height, width, encoding) => {
const createPDF = async (
page,
height,
width,
encoding,
rasterizationTimeout
) => {
await page.emulateMediaType('screen');
return page.pdf({
// This will remove an extra empty page in PDF exports
height: height + 1,
width,
encoding
});
return Promise.race([
page.pdf({
// This will remove an extra empty page in PDF exports
height: height + 1,
width,
encoding
}),
new Promise((_resolve, reject) =>
setTimeout(
() => reject(new ExportError('Rasterization timeout')),
rasterizationTimeout || 1500
)
)
]);
};

/**
Expand Down Expand Up @@ -407,7 +421,13 @@ export default async (page, chart, options) => {
);
} else if (exportOptions.type === 'pdf') {
// PDF
data = await createPDF(page, viewportHeight, viewportWidth, 'base64');
data = await createPDF(
page,
viewportHeight,
viewportWidth,
'base64',
exportOptions.rasterizationTimeout
);
} else {
throw new ExportError(
`[export] Unsupported output format ${exportOptions.type}.`
Expand Down
20 changes: 15 additions & 5 deletions lib/highcharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ See LICENSE file in root for details.

/* eslint-disable no-undef */

/** Called when initing the page */
/**
* Setting the animObject. Called when initing the page.
*/
export function setupHighcharts() {
Highcharts.animObject = function () {
return { duration: 0 };
};
}

/** Create the actual chart */
/**
* Creates the actual chart.
*
* @param {object} chartOptions - The options for the Highcharts chart.
* @param {object} options - The export options.
* @param {boolean} displayErrors - A flag indicating whether to display errors.
*/
export function triggerExport(chartOptions, options, displayErrors) {
// Display errors flag taken from chart options nad debugger module
window._displayErrors = displayErrors;
Expand Down Expand Up @@ -49,9 +57,8 @@ export function triggerExport(chartOptions, options, displayErrors) {
chart.width = chartOptions.chart.width;
}

// NOTE: Is this used for anything useful??
// NOTE: Is this used for anything useful?
window.isRenderComplete = false;

wrap(Highcharts.Chart.prototype, 'init', function (proceed, userOptions, cb) {
// Override userOptions with image friendly options
userOptions = merge(userOptions, {
Expand Down Expand Up @@ -109,7 +116,10 @@ export function triggerExport(chartOptions, options, displayErrors) {
: undefined;

// Set the global options if exist
setOptions(JSON.parse(options.export.globalOptions));
const globalOptions = JSON.parse(options.export.globalOptions);
if (globalOptions) {
setOptions(globalOptions);
}

Highcharts[options.export.constr || 'chart'](
'container',
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ export default {
initPool,
killPool,

// Other
setOptions,
shutdownCleanUp,

// Logs
log,
logWithStack,
setLogLevel,
enableFileLogging,

// Other
setOptions,
shutdownCleanUp,

// Utils
mapToNewConfig,
manualConfig,
Expand Down
33 changes: 19 additions & 14 deletions lib/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,43 +626,48 @@ export const defaultConfig = {
type: 'boolean',
envLink: 'DEBUG_ENABLE',
cliName: 'enableDebug',
description: '.'
description: 'Enables or disables debug mode for the underlying browser.'
},
headless: {
value: true,
type: 'boolean',
envLink: 'DEBUG_HEADLESS',
description: '.'
description:
'Controls the mode in which the browser is launched when in the debug mode.'
},
devtools: {
value: false,
type: 'boolean',
envLink: 'DEBUG_DEVTOOLS',
description: '.'
description:
'Decides whether to enable DevTools when the browser is in a headful state.'
},
listenToConsole: {
value: false,
type: 'boolean',
envLink: 'DEBUG_LISTEN_TO_CONSOLE',
description: '.'
description:
'Decides whether to enable a listener for console messages sent from the browser.'
},
dumpio: {
value: false,
type: 'boolean',
envLink: 'DEBUG_DUMPIO',
description: '.'
description:
'Redirects browser process stdout and stderr to process.stdout and process.stderr.'
},
slowMo: {
value: 0,
type: 'number',
envLink: 'DEBUG_SLOW_MO',
description: '.'
description:
'Slows down Puppeteer operations by the specified number of milliseconds.'
},
debuggingPort: {
value: 9222,
type: 'number',
envLink: 'DEBUG_DEBUGGING_PORT',
description: '.'
description: 'Specifies the debugging port.'
}
}
};
Expand Down Expand Up @@ -1041,43 +1046,43 @@ export const promptsConfig = {
{
type: 'toggle',
name: 'enable',
message: 'Enable debug mode for the browser instance',
message: 'Enables debug mode for the browser instance',
initial: defaultConfig.debug.enable.value
},
{
type: 'toggle',
name: 'headless',
message: '',
message: 'The mode setting for the browser',
initial: defaultConfig.debug.headless.value
},
{
type: 'toggle',
name: 'devtools',
message: '',
message: 'The DevTools for the headful browser',
initial: defaultConfig.debug.devtools.value
},
{
type: 'toggle',
name: 'listenToConsole',
message: '',
message: 'The event listener for console messages from the browser',
initial: defaultConfig.debug.listenToConsole.value
},
{
type: 'toggle',
name: 'dumpio',
message: '',
message: 'Redirects the browser stdout and stderr to NodeJS process',
initial: defaultConfig.debug.dumpio.value
},
{
type: 'number',
name: 'slowMo',
message: '',
message: 'Puppeteer operations slow down in milliseconds',
initial: defaultConfig.debug.slowMo.value
},
{
type: 'number',
name: 'debuggingPort',
message: '',
message: 'The port number for debugging',
initial: defaultConfig.debug.debuggingPort.value
}
]
Expand Down

0 comments on commit df5f150

Please sign in to comment.