|
1 | | -require('playwright-core').default = require('playwright-core'); |
| 1 | +import setEnvironmentVariables from './util/setEnvironmentVariables'; |
2 | 2 |
|
3 | | -import { promises as fsPromises } from 'fs'; |
4 | | -import { join } from 'path'; |
5 | | -import playwright from 'playwright-core'; |
6 | | -import isLambdaRuntimeEnvironment from './isLambdaRuntimeEnvironment'; |
7 | | -import { LaunchOptions } from 'playwright-core/lib/server/browserType'; |
| 3 | +setEnvironmentVariables(); |
8 | 4 |
|
9 | | -const { inflate } = require('lambdafs'); |
10 | | - |
11 | | -if (isLambdaRuntimeEnvironment()) { |
12 | | - if (process.env.FONTCONFIG_PATH === undefined) { |
13 | | - process.env.FONTCONFIG_PATH = '/tmp/aws'; |
14 | | - } |
15 | | - |
16 | | - if (process.env.LD_LIBRARY_PATH === undefined) { |
17 | | - process.env.LD_LIBRARY_PATH = '/tmp/aws/lib'; |
18 | | - } else if (process.env.LD_LIBRARY_PATH.startsWith('/tmp/aws/lib') !== true) { |
19 | | - process.env.LD_LIBRARY_PATH = [ |
20 | | - ...new Set(['/tmp/aws/lib', ...process.env.LD_LIBRARY_PATH.split(':')]), |
21 | | - ].join(':'); |
22 | | - } |
23 | | -} |
24 | | - |
25 | | -/** |
26 | | - * Returns a list of recommended additional Chromium flags. |
27 | | - */ |
28 | | -function getChromiumArgs(headless: boolean) { |
29 | | - const result = [ |
30 | | - '--disable-background-timer-throttling', |
31 | | - '--disable-breakpad', |
32 | | - '--disable-client-side-phishing-detection', |
33 | | - '--disable-cloud-import', |
34 | | - '--disable-default-apps', |
35 | | - '--disable-dev-shm-usage', |
36 | | - '--disable-extensions', |
37 | | - '--disable-gesture-typing', |
38 | | - '--disable-hang-monitor', |
39 | | - '--disable-infobars', |
40 | | - '--disable-notifications', |
41 | | - '--disable-offer-store-unmasked-wallet-cards', |
42 | | - '--disable-offer-upload-credit-cards', |
43 | | - '--disable-popup-blocking', |
44 | | - '--disable-print-preview', |
45 | | - '--disable-prompt-on-repost', |
46 | | - '--disable-setuid-sandbox', |
47 | | - '--disable-speech-api', |
48 | | - '--disable-sync', |
49 | | - '--disable-tab-for-desktop-share', |
50 | | - '--disable-translate', |
51 | | - '--disable-voice-input', |
52 | | - '--disable-wake-on-wifi', |
53 | | - '--disk-cache-size=33554432', |
54 | | - '--enable-async-dns', |
55 | | - '--enable-simple-cache-backend', |
56 | | - '--enable-tcp-fast-open', |
57 | | - '--enable-webgl', |
58 | | - '--hide-scrollbars', |
59 | | - '--ignore-gpu-blacklist', |
60 | | - '--media-cache-size=33554432', |
61 | | - '--metrics-recording-only', |
62 | | - '--mute-audio', |
63 | | - '--no-default-browser-check', |
64 | | - '--no-first-run', |
65 | | - '--no-pings', |
66 | | - '--no-sandbox', |
67 | | - '--no-zygote', |
68 | | - '--password-store=basic', |
69 | | - '--prerender-from-omnibox=disabled', |
70 | | - '--use-gl=swiftshader', |
71 | | - '--use-mock-keychain', |
72 | | - ]; |
73 | | - if ( |
74 | | - parseInt( |
75 | | - process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE || |
76 | | - process.env.FUNCTION_MEMORY_MB || |
77 | | - '1024', |
78 | | - 10 |
79 | | - ) >= 1024 |
80 | | - ) { |
81 | | - result.push('--memory-pressure-off'); |
82 | | - } |
83 | | - if (headless === true) { |
84 | | - result.push('--single-process'); |
85 | | - } else { |
86 | | - result.push('--start-maximized'); |
87 | | - } |
88 | | - return result; |
89 | | -} |
90 | | -async function fileExists(path: string) { |
91 | | - try { |
92 | | - await fsPromises.access(path); |
93 | | - return true; |
94 | | - } catch (err) { |
95 | | - return false; |
96 | | - } |
97 | | -} |
98 | | -function isHeadlessModeEnabled() { |
99 | | - if (process.env.IS_LOCAL !== undefined) { |
100 | | - return false; |
101 | | - } |
102 | | - return ['AWS_LAMBDA_FUNCTION_NAME', 'FUNCTION_NAME', 'FUNCTION_TARGET'].some( |
103 | | - key => process.env[key] !== undefined |
104 | | - ); |
105 | | -} |
106 | | - |
107 | | -async function getExecutablePath( |
108 | | - headless: boolean |
109 | | -): Promise<string | undefined> { |
110 | | - if (headless !== true) { |
111 | | - return Promise.resolve(undefined); |
112 | | - } |
113 | | - |
114 | | - if ((await fileExists('/tmp/chromium')) === true) { |
115 | | - for (const file of await fsPromises.readdir('/tmp')) { |
116 | | - if (file.startsWith('core.chromium') === true) { |
117 | | - await fsPromises.unlink(`/tmp/${file}`); |
118 | | - } |
119 | | - } |
120 | | - |
121 | | - return Promise.resolve('/tmp/chromium'); |
122 | | - } |
123 | | - |
124 | | - const input = join(__dirname, 'bin'); |
125 | | - const promises = [ |
126 | | - inflate(`${input}/chromium.br`), |
127 | | - inflate(`${input}/swiftshader.tar.br`), |
128 | | - ]; |
129 | | - |
130 | | - if (isLambdaRuntimeEnvironment()) { |
131 | | - promises.push(inflate(`${input}/aws.tar.br`)); |
132 | | - } |
133 | | - |
134 | | - const result = await Promise.all(promises); |
135 | | - return result.shift(); |
136 | | -} |
137 | | - |
138 | | -export async function launchChromium(launchOptions?: Partial<LaunchOptions>) { |
139 | | - const headless = isHeadlessModeEnabled(); |
140 | | - const browser = await playwright.chromium.launch({ |
141 | | - args: getChromiumArgs(headless), |
142 | | - // defaultViewport: chromium.defaultViewport, |
143 | | - executablePath: await getExecutablePath(headless), |
144 | | - headless, |
145 | | - // headless: true |
146 | | - ...launchOptions, |
147 | | - }); |
148 | | - return browser; |
149 | | -} |
| 5 | +export * from './chromium'; |
0 commit comments