-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathchromium.ts
148 lines (131 loc) · 3.93 KB
/
chromium.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { promises as fsPromises } from 'fs';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import * as https from 'https';
import * as playwright from 'playwright-core';
import { LaunchOptions } from 'playwright-core';
import isLambdaRuntimeEnvironment from './util/isLambdaRuntimeEnvironment';
import isHeadlessModeEnabled from './util/isHeadlessModeEnabled';
import fileExists from './util/fileExists';
import getEnvironmentVariables, {
AWS_FONT_DIR,
} from './util/getEnvironmentVariables';
const { inflate } = require('lambdafs');
/**
* Returns a list of recommended additional Chromium flags.
*/
export function getChromiumArgs(headless: boolean) {
const result = [
'--autoplay-policy=user-gesture-required',
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-component-update',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-domain-reliability',
'--disable-extensions',
'--disable-features=AudioServiceOutOfProcess',
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
'--disable-notifications',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-popup-blocking',
'--disable-print-preview',
'--disable-prompt-on-repost',
'--disable-renderer-backgrounding',
'--disable-setuid-sandbox',
'--disable-speech-api',
'--disable-sync',
'--disk-cache-size=33554432',
'--hide-scrollbars',
'--ignore-gpu-blacklist',
'--metrics-recording-only',
'--mute-audio',
'--no-default-browser-check',
'--no-first-run',
'--no-pings',
'--no-sandbox',
'--no-zygote',
'--password-store=basic',
'--use-gl=swiftshader',
'--use-mock-keychain',
];
if (headless === true) {
result.push('--single-process');
} else {
result.push('--start-maximized');
}
return result;
}
async function getChromiumExecutablePath(
headless: boolean
): Promise<string | undefined> {
if (headless !== true) {
return undefined;
}
if ((await fileExists('/tmp/chromium')) === true) {
for (const file of await fsPromises.readdir('/tmp')) {
if (file.startsWith('core.chromium') === true) {
await fsPromises.unlink(`/tmp/${file}`);
}
}
return '/tmp/chromium';
}
const input = path.join(__dirname, 'bin');
const promises = [
inflate(`${input}/chromium.br`),
inflate(`${input}/swiftshader.tar.br`),
];
if (isLambdaRuntimeEnvironment()) {
promises.push(inflate(`${input}/aws.tar.br`));
}
const result = await Promise.all(promises);
return result.shift();
}
export async function launchChromium(launchOptions?: Partial<LaunchOptions>) {
const headless = isHeadlessModeEnabled();
const args = getChromiumArgs(headless);
const executablePath = await getChromiumExecutablePath(headless);
const env: LaunchOptions['env'] = {
...(await getEnvironmentVariables()),
...(launchOptions?.env || {}),
};
const browser = await playwright.chromium.launch({
args,
executablePath,
headless,
env,
...launchOptions,
});
return browser;
}
export const loadFont = async (input: string) =>
new Promise(async (resolve, reject) => {
const url = new URL(input);
const output = path.join(AWS_FONT_DIR, url.pathname.split('/').pop()!);
if (await promisify(fs.exists)(output)) {
resolve();
return;
}
if (!fs.existsSync(AWS_FONT_DIR)) {
await fsPromises.mkdir(AWS_FONT_DIR);
}
const stream = fs.createWriteStream(output);
stream.once('error', (error) => {
return reject(error);
});
https.get(input, (response) => {
response.on('data', (chunk) => {
stream.write(chunk);
});
response.once('end', () => {
stream.end(() => {
return resolve();
});
});
});
});