Skip to content

Commit fd5edc3

Browse files
authored
Merge pull request #630 from highcharts/567/user-data-dir-config
567/user-data-dir-config
2 parents be0c3eb + 34e8fd1 commit fd5edc3

File tree

10 files changed

+72
-8
lines changed

10 files changed

+72
-8
lines changed

.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# PUPPETEER CONFIG
2+
PUPPETEER_TEMP_DIR = ./tmp/
3+
14
# HIGHCHARTS CONFIG
25
HIGHCHARTS_VERSION = latest
36
HIGHCHARTS_CDN_URL = https://code.highcharts.com/

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ _New Features:_
1818

1919
- Added proxy authentication [(#631)](https://github.com/highcharts/node-export-server/issues/631).
2020

21+
_New features:_
22+
23+
- 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).
24+
2125
# 4.0.2
2226

2327
_Hotfix_:

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ The format, along with its default values, is as follows (using the recommended
9898
```
9999
{
100100
"puppeteer": {
101-
"args": []
101+
"args": [],
102+
"tempDir": "./tmp/"
102103
},
103104
"highcharts": {
104105
"version": "latest",
@@ -288,6 +289,10 @@ To load an additional JSON configuration file, use the `--loadConfig <filepath>`
288289

289290
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.
290291

292+
### Puppeteer Config
293+
294+
- `PUPPETEER_TEMP_DIR`: The directory for Puppeteer to store temporary files (defaults to `./tmp/`).
295+
291296
### Highcharts Config
292297

293298
- `HIGHCHARTS_VERSION`: Highcharts version to use (defaults to `latest`).

dist/index.cjs

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

dist/index.esm.js

Lines changed: 1 addition & 1 deletion
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: 2 additions & 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ export function get() {
5959
*/
6060
export async function create(puppeteerArgs) {
6161
// Get debug and other options
62-
const { debug, other } = getOptions();
62+
const { puppeteer: puppeteerOptions, debug, other } = getOptions();
6363

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

6767
const launchOptions = {
6868
headless: other.browserShellMode ? 'shell' : true,
69-
userDataDir: './tmp/',
69+
userDataDir: puppeteerOptions.tempDir || './tmp/',
7070
args: puppeteerArgs,
7171
handleSIGINT: false,
7272
handleSIGTERM: false,

lib/envs.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ const v = {
6464
)
6565
.transform((value) => (value !== '' ? value : undefined)),
6666

67+
// Checks if the string is a valid path directory (path format)
68+
path: () =>
69+
z
70+
.string()
71+
.trim()
72+
.refine(
73+
(value) => {
74+
// Simplified regex to match both absolute and relative paths
75+
return /^(\.\/|\.\.\/|\/|[a-zA-Z]:\\|[a-zA-Z]:\/)?(([\w]+|-)[\\/]?)+$/.test(
76+
value
77+
);
78+
},
79+
{},
80+
{
81+
message: 'The string is an invalid path directory string.'
82+
}
83+
),
84+
6785
// Allows positive numbers or no value in which case the returned value will
6886
// be undefined
6987
positiveNum: () =>
@@ -96,6 +114,9 @@ const v = {
96114
};
97115

98116
export const Config = z.object({
117+
// puppeteer
118+
PUPPETEER_TEMP_DIR: v.string(),
119+
99120
// highcharts
100121
HIGHCHARTS_VERSION: z
101122
.string()

lib/schemas/config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ export const defaultConfig = {
146146
],
147147
type: 'string[]',
148148
description: 'Arguments array to send to Puppeteer.'
149+
},
150+
tempDir: {
151+
value: './tmp/',
152+
type: 'string',
153+
envLink: 'PUPPETEER_TEMP_DIR',
154+
description: 'The directory for Puppeteer to store temporary files.'
149155
}
150156
},
151157
highcharts: {
@@ -355,7 +361,6 @@ export const defaultConfig = {
355361
envLink: 'SERVER_MAX_UPLOAD_SIZE',
356362
description:
357363
'The maximum upload size, in megabytes, for the server'
358-
359364
},
360365
enable: {
361366
value: false,

tests/unit/envs.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
import { Config } from '../../lib/envs';
22

33
describe('Environment variables should be correctly parsed', () => {
4+
test('PUPPETEER_TEMP_DIR should be a valid path', () => {
5+
const env = { PUPPETEER_TEMP_DIR: '/path/to/dir' };
6+
expect(Config.partial().parse(env).PUPPETEER_TEMP_DIR).toEqual(
7+
'/path/to/dir'
8+
);
9+
10+
env.PUPPETEER_TEMP_DIR = '/another/path/to/dir';
11+
expect(Config.partial().parse(env).PUPPETEER_TEMP_DIR).toEqual(
12+
'/another/path/to/dir'
13+
);
14+
15+
env.PUPPETEER_TEMP_DIR = '';
16+
expect(() => Config.partial().parse(env)).toThrow();
17+
});
18+
19+
test('PUPPETEER_TEMP_DIR can be a relative path', () => {
20+
const env = { PUPPETEER_TEMP_DIR: './tmp/' };
21+
expect(Config.partial().parse(env).PUPPETEER_TEMP_DIR).toEqual('./tmp/');
22+
23+
env.PUPPETEER_TEMP_DIR = '../custom-tmp/';
24+
expect(Config.partial().parse(env).PUPPETEER_TEMP_DIR).toEqual(
25+
'../custom-tmp/'
26+
);
27+
});
28+
429
test('HIGHCHARTS_VERSION accepts latests and not unrelated strings', () => {
530
const env = { HIGHCHARTS_VERSION: 'string-other-than-latest' };
631
expect(() => Config.partial().parse(env)).toThrow();

0 commit comments

Comments
 (0)