-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathconfig.ts
119 lines (101 loc) · 5.35 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as fs from 'fs';
import * as _ from 'lodash';
import * as os from 'os';
import * as puppeteer from 'puppeteer';
const debug = require('debug');
const untildify = require('untildify');
const packageJson = require('puppeteer/package.json');
// Required, by default, to make certain API's work
const REQUIRED_INTERNALS = ['url'];
const REQUIRED_EXTERNALS = ['lighthouse', 'node-pdftk'];
const IS_DOCKER = fs.existsSync('/.dockerenv');
const CHROME_BINARY_DEFAULT_LOCATION = '/usr/bin/google-chrome';
const getDebug = () => {
if (typeof process.env.DEBUG !== 'undefined') {
return process.env.DEBUG;
}
if (process.env.CI) {
return process.env.DEBUG;
}
process.env.DEBUG = 'browserless*';
debug.enable(process.env.DEBUG);
return process.env.DEBUG;
};
const parseJSONParam = (param: string | undefined, defaultParam: boolean | string[]) => {
if (param) {
try {
return JSON.parse(param);
} catch (err) {
console.warn(`Couldn't parse parameter: "${param}". Saw error "${err}"`);
return defaultParam;
}
}
return defaultParam;
};
const parseNumber = (param: string | undefined, defaultParam: number): number => {
const parsed = _.parseInt(param || '');
if (_.isNaN(parsed)) {
return defaultParam;
}
return parsed;
};
const thirtyMinutes = 30 * 60 * 1000;
// Timers/Queue/Concurrency
export const CHROME_REFRESH_TIME: number = parseNumber(process.env.CHROME_REFRESH_TIME, thirtyMinutes);
export const CONNECTION_TIMEOUT: number = parseNumber(process.env.CONNECTION_TIMEOUT, 30000);
export const MAX_CONCURRENT_SESSIONS: number = parseNumber(process.env.MAX_CONCURRENT_SESSIONS, 10);
export const QUEUE_LENGTH: number = parseNumber(process.env.MAX_QUEUE_LENGTH, 10);
// Preboot/Default Launch Options
export const KEEP_ALIVE: boolean = parseJSONParam(process.env.KEEP_ALIVE, false);
export const MAX_CHROME_REFRESH_RETRIES: number = parseNumber(process.env.MAX_CHROME_REFRESH_RETRIES, 5);
export const DEFAULT_BLOCK_ADS: boolean = parseJSONParam(process.env.DEFAULT_BLOCK_ADS, false);
export const DEFAULT_HEADLESS: boolean = parseJSONParam(process.env.DEFAULT_HEADLESS, true);
export const DEFAULT_LAUNCH_ARGS: string[] = parseJSONParam(process.env.DEFAULT_LAUNCH_ARGS, []);
export const DEFAULT_IGNORE_DEFAULT_ARGS: boolean = parseJSONParam(process.env.DEFAULT_IGNORE_DEFAULT_ARGS, false);
export const DEFAULT_IGNORE_HTTPS_ERRORS: boolean = parseJSONParam(process.env.DEFAULT_IGNORE_HTTPS_ERRORS, false);
export const DEFAULT_USER_DATA_DIR: string | undefined = process.env.DEFAULT_USER_DATA_DIR ?
untildify(process.env.DEFAULT_USER_DATA_DIR) :
undefined;
export const PREBOOT_CHROME: boolean = parseJSONParam(process.env.PREBOOT_CHROME, false);
export const CHROME_BINARY_LOCATION: string = (() => {
// If it's installed already (docker) use it
if (IS_DOCKER) {
return CHROME_BINARY_DEFAULT_LOCATION;
} else {
// Use puppeteer's copy otherwise
const browserFetcher = puppeteer.createBrowserFetcher();
return browserFetcher.revisionInfo(packageJson.puppeteer.chromium_revision).executablePath;
}
})();
// Security and accessibility
export const DEBUG: string | undefined = getDebug();
export const DEMO_MODE: boolean = parseJSONParam(process.env.DEMO_MODE, false);
export const ENABLE_CORS: boolean = parseJSONParam(process.env.ENABLE_CORS, false);
export const ENABLE_DEBUGGER: boolean = parseJSONParam(process.env.ENABLE_DEBUGGER, true);
export const ENABLE_DEBUG_VIEWER: boolean = parseJSONParam(process.env.ENABLE_DEBUG_VIEWER, true);
export const ENABLE_XVBF: boolean = parseJSONParam(process.env.ENABLE_XVBF, false);
export const TOKEN: string | null = process.env.TOKEN || null;
// Puppeteer behavior
export const DISABLE_AUTO_SET_DOWNLOAD_BEHAVIOR = parseJSONParam(process.env.DISABLE_AUTO_SET_DOWNLOAD_BEHAVIOR, false);
export const FUNCTION_BUILT_INS: string[] = parseJSONParam(process.env.FUNCTION_BUILT_INS, REQUIRED_INTERNALS);
export const FUNCTION_EXTERNALS: string[] = parseJSONParam(process.env.FUNCTION_EXTERNALS, REQUIRED_EXTERNALS);
export const WORKSPACE_DIR: string = process.env.WORKSPACE_DIR ? untildify(process.env.WORKSPACE_DIR) : os.tmpdir();
export const WORKSPACE_DELETE_EXPIRED: boolean = parseJSONParam(process.env.WORKSPACE_DELETE_EXPIRED, false);
export const WORKSPACE_EXPIRE_DAYS: number = parseNumber(process.env.WORKSPACE_EXPIRE_DAYS, 30);
// Webhooks
export const FAILED_HEALTH_URL: string | null = process.env.FAILED_HEALTH_URL || null;
export const QUEUE_ALERT_URL: string | null = process.env.QUEUE_ALERT_URL || null;
export const REJECT_ALERT_URL: string | null = process.env.REJECT_ALERT_URL || null;
export const TIMEOUT_ALERT_URL: string | null = process.env.TIMEOUT_ALERT_URL || null;
export const ERROR_ALERT_URL: string | null = process.env.ERROR_ALERT_URL || null;
// Health
export const EXIT_ON_HEALTH_FAILURE: boolean = parseJSONParam(process.env.EXIT_ON_HEALTH_FAILURE, false);
export const MAX_CPU_PERCENT: number = parseNumber(process.env.MAX_CPU_PERCENT, 99);
export const MAX_MEMORY_PERCENT: number = parseNumber(process.env.MAX_MEMORY_PERCENT, 99);
export const METRICS_JSON_PATH: string | null = process.env.METRICS_JSON_PATH ?
untildify(process.env.METRICS_JSON_PATH) :
null;
// Server Options
export const HOST: string | undefined = process.env.HOST;
export const MAX_PAYLOAD_SIZE: string = process.env.MAX_PAYLOAD_SIZE || '5mb';
export const PORT: number = parseNumber(process.env.PORT, 8080);