Skip to content

Commit

Permalink
Added the PUPPETEER_TEMP_DIR option, touch #567.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulDalek committed Feb 10, 2025
1 parent 48d3794 commit ac4fd60
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# PUPPETEER CONFIG
PUPPETEER_TEMP_DIR = ./tmp/

# HIGHCHARTS CONFIG
HIGHCHARTS_VERSION = latest
HIGHCHARTS_CDN_URL = https://code.highcharts.com/
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 4.0.3

_New features:_

- 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).

# 4.0.2

_Hotfix_:
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ The format, along with its default values, is as follows (using the recommended
```
{
"puppeteer": {
"args": []
"args": [],
"tempDir": "./tmp/"
},
"highcharts": {
"version": "latest",
Expand Down Expand Up @@ -285,6 +286,10 @@ To load an additional JSON configuration file, use the `--loadConfig <filepath>`

These variables are set in your environment and take precedence over options from the `lib/schemas/config.js` file. They can be set in the `.env` file (refer to the `.env.sample` file). If you prefer setting these variables through the `package.json`, use `export` command on Linux/Mac OS X and `set` command on Windows.

### Puppeteer Config

- `PUPPETEER_TEMP_DIR`: The directory for Puppeteer to store temporary files (defaults to `./tmp/`).

### Highcharts Config

- `HIGHCHARTS_VERSION`: Highcharts version to use (defaults to `latest`).
Expand Down
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.

4 changes: 2 additions & 2 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ export function get() {
*/
export async function create(puppeteerArgs) {
// Get debug and other options
const { debug, other } = getOptions();
const { puppeteer: puppeteerOptions, debug, other } = getOptions();

// Get the debug options
const { enable: enabledDebug, ...debugOptions } = debug;

const launchOptions = {
headless: other.browserShellMode ? 'shell' : true,
userDataDir: './tmp/',
userDataDir: puppeteerOptions.tempDir || './tmp/',
args: puppeteerArgs,
handleSIGINT: false,
handleSIGTERM: false,
Expand Down
21 changes: 21 additions & 0 deletions lib/envs.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ const v = {
)
.transform((value) => (value !== '' ? value : undefined)),

// Checks if the string is a valid path directory (path format)
path: () =>
z
.string()
.trim()
.refine(
(value) => {
// Simplified regex to match both absolute and relative paths
return /^(\.\/|\.\.\/|\/|[a-zA-Z]:\\|[a-zA-Z]:\/)?(([\w]+|-)[\\/]?)+$/.test(

Check failure

Code scanning / CodeQL

Inefficient regular expression High

This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '0'.
value
);
},
{},
{
message: 'The string is an invalid path directory string.'
}
),

// Allows positive numbers or no value in which case the returned value will
// be undefined
positiveNum: () =>
Expand Down Expand Up @@ -96,6 +114,9 @@ const v = {
};

export const Config = z.object({
// puppeteer
PUPPETEER_TEMP_DIR: v.path(),

// highcharts
HIGHCHARTS_VERSION: z
.string()
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
6 changes: 6 additions & 0 deletions lib/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ export const defaultConfig = {
],
type: 'string[]',
description: 'Arguments array to send to Puppeteer.'
},
tempDir: {
value: './tmp/',
type: 'string',
envLink: 'PUPPETEER_TEMP_DIR',
description: 'The directory for Puppeteer to store temporary files.'
}
},
highcharts: {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Highsoft AS <[email protected]> (http://www.highcharts.com/about)",
"license": "MIT",
"type": "module",
"version": "4.0.2",
"version": "4.0.3",
"main": "./dist/index.esm.js",
"engines": {
"node": ">=18.12.0"
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/envs.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import { Config } from '../../lib/envs';

describe('Environment variables should be correctly parsed', () => {
test('PUPPETEER_TEMP_DIR should be a valid path', () => {
const env = { PUPPETEER_TEMP_DIR: '/path/to/dir' };
expect(Config.partial().parse(env).PUPPETEER_TEMP_DIR).toEqual(
'/path/to/dir'
);

env.PUPPETEER_TEMP_DIR = '/another/path/to/dir';
expect(Config.partial().parse(env).PUPPETEER_TEMP_DIR).toEqual(
'/another/path/to/dir'
);

env.PUPPETEER_TEMP_DIR = '';
expect(() => Config.partial().parse(env)).toThrow();
});

test('PUPPETEER_TEMP_DIR can be a relative path', () => {
const env = { PUPPETEER_TEMP_DIR: './tmp/' };
expect(Config.partial().parse(env).PUPPETEER_TEMP_DIR).toEqual('./tmp/');

env.PUPPETEER_TEMP_DIR = '../custom-tmp/';
expect(Config.partial().parse(env).PUPPETEER_TEMP_DIR).toEqual(
'../custom-tmp/'
);
});

test('HIGHCHARTS_VERSION accepts latests and not unrelated strings', () => {
const env = { HIGHCHARTS_VERSION: 'string-other-than-latest' };
expect(() => Config.partial().parse(env)).toThrow();
Expand Down

0 comments on commit ac4fd60

Please sign in to comment.