-
Notifications
You must be signed in to change notification settings - Fork 120
/
index.js
339 lines (283 loc) · 9.58 KB
/
index.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var fs = require('fs')
var path = require('path')
var which = require('which')
function isJSFlags (flag) {
return flag.indexOf('--js-flags=') === 0
}
function sanitizeJSFlags (flag) {
var test = /--js-flags=(['"])/.exec(flag)
if (!test) {
return flag
}
var escapeChar = test[1]
var endExp = new RegExp(escapeChar + '$')
var startExp = new RegExp('--js-flags=' + escapeChar)
return flag.replace(startExp, '--js-flags=').replace(endExp, '')
}
var ChromeBrowser = function (baseBrowserDecorator, args) {
baseBrowserDecorator(this)
var flags = args.flags || []
var userDataDir = args.chromeDataDir || this._tempDir
this._getOptions = function (url) {
// Chrome CLI options
// http://peter.sh/experiments/chromium-command-line-switches/
flags.forEach(function (flag, i) {
if (isJSFlags(flag)) {
flags[i] = sanitizeJSFlags(flag)
}
})
return [
'--user-data-dir=' + userDataDir,
// https://github.com/GoogleChrome/chrome-launcher/blob/master/docs/chrome-flags-for-tools.md#--enable-automation
'--enable-automation',
'--no-default-browser-check',
'--no-first-run',
'--disable-default-apps',
'--disable-popup-blocking',
'--disable-translate',
'--disable-background-timer-throttling',
// on macOS, disable-background-timer-throttling is not enough
// and we need disable-renderer-backgrounding too
// see https://github.com/karma-runner/karma-chrome-launcher/issues/123
'--disable-renderer-backgrounding',
'--disable-device-discovery-notifications'
].concat(flags, [url])
}
}
// Return location of chrome.exe file for a given Chrome directory (available: "Chrome", "Chrome SxS").
function getChromeExe (chromeDirName) {
// Only run these checks on win32
if (process.platform !== 'win32') {
return null
}
var windowsChromeDirectory, i, prefix
var suffix = '\\Google\\' + chromeDirName + '\\Application\\chrome.exe'
var prefixes = [process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)'], process.env.ProgramW6432]
for (i = 0; i < prefixes.length; i++) {
prefix = prefixes[i]
try {
windowsChromeDirectory = path.join(prefix, suffix)
fs.accessSync(windowsChromeDirectory)
return windowsChromeDirectory
} catch (e) {}
}
return windowsChromeDirectory
}
var ChromiumBrowser = function (baseBrowserDecorator, args) {
baseBrowserDecorator(this)
var flags = args.flags || []
var userDataDir = args.chromeDataDir || this._tempDir
this._getOptions = function (url) {
// Chromium CLI options
// http://peter.sh/experiments/chromium-command-line-switches/
flags.forEach(function (flag, i) {
if (isJSFlags(flag)) {
flags[i] = sanitizeJSFlags(flag)
}
})
return [
'--user-data-dir=' + userDataDir,
'--no-default-browser-check',
'--no-first-run',
'--disable-default-apps',
'--disable-popup-blocking',
'--disable-translate',
'--disable-background-timer-throttling'
].concat(flags, [url])
}
}
// Return location of Chromium's chrome.exe file.
function getChromiumExe (chromeDirName) {
// Only run these checks on win32
if (process.platform !== 'win32') {
return null
}
var windowsChromiumDirectory, i, prefix
var suffix = '\\Chromium\\Application\\chrome.exe'
var prefixes = [process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)'], process.env.ProgramW6432]
for (i = 0; i < prefixes.length; i++) {
prefix = prefixes[i]
try {
windowsChromiumDirectory = path.join(prefix, suffix)
fs.accessSync(windowsChromiumDirectory)
return windowsChromiumDirectory
} catch (e) {}
}
return windowsChromiumDirectory
}
function getBin (commands) {
// Don't run these checks on win32
if (process.platform !== 'linux') {
return null
}
var bin, i
for (i = 0; i < commands.length; i++) {
try {
if (which.sync(commands[i])) {
bin = commands[i]
break
}
} catch (e) {}
}
return bin
}
function getChromeDarwin (defaultPath) {
if (process.platform !== 'darwin') {
return null
}
try {
var homePath = path.join(process.env.HOME, defaultPath)
fs.accessSync(homePath)
return homePath
} catch (e) {
return defaultPath
}
}
ChromeBrowser.prototype = {
name: 'Chrome',
DEFAULT_CMD: {
linux: getBin(['google-chrome', 'google-chrome-stable']),
darwin: getChromeDarwin('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'),
win32: getChromeExe('Chrome')
},
ENV_CMD: 'CHROME_BIN'
}
ChromeBrowser.$inject = ['baseBrowserDecorator', 'args']
function headlessGetOptions (url, args, parent) {
var mergedArgs = parent.call(this, url, args).concat([
'--headless',
'--disable-gpu',
'--disable-dev-shm-usage'
])
var isRemoteDebuggingFlag = function (flag) {
return flag.indexOf('--remote-debugging-port=') !== -1
}
return mergedArgs.some(isRemoteDebuggingFlag)
? mergedArgs
: mergedArgs.concat(['--remote-debugging-port=9222'])
}
var ChromeHeadlessBrowser = function (baseBrowserDecorator, args) {
ChromeBrowser.apply(this, arguments)
var parentOptions = this._getOptions
this._getOptions = function (url) {
return headlessGetOptions.call(this, url, args, parentOptions)
}
}
ChromeHeadlessBrowser.prototype = {
name: 'ChromeHeadless',
DEFAULT_CMD: {
linux: getBin(['google-chrome', 'google-chrome-stable']),
darwin: getChromeDarwin('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'),
win32: getChromeExe('Chrome')
},
ENV_CMD: 'CHROME_BIN'
}
ChromeHeadlessBrowser.$inject = ['baseBrowserDecorator', 'args']
function canaryGetOptions (url, args, parent) {
// disable crankshaft optimizations, as it causes lot of memory leaks (as of Chrome 23.0)
var flags = args.flags || []
var augmentedFlags
var customFlags = '--nocrankshaft --noopt'
flags.forEach(function (flag) {
if (isJSFlags(flag)) {
augmentedFlags = sanitizeJSFlags(flag) + ' ' + customFlags
}
})
return parent.call(this, url).concat([augmentedFlags || '--js-flags=' + customFlags])
}
var ChromeCanaryBrowser = function (baseBrowserDecorator, args) {
ChromeBrowser.apply(this, arguments)
var parentOptions = this._getOptions
this._getOptions = function (url) {
return canaryGetOptions.call(this, url, args, parentOptions)
}
}
ChromeCanaryBrowser.prototype = {
name: 'ChromeCanary',
DEFAULT_CMD: {
linux: getBin(['google-chrome-canary', 'google-chrome-unstable']),
darwin: getChromeDarwin('/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'),
win32: getChromeExe('Chrome SxS')
},
ENV_CMD: 'CHROME_CANARY_BIN'
}
ChromeCanaryBrowser.$inject = ['baseBrowserDecorator', 'args']
var ChromeCanaryHeadlessBrowser = function (baseBrowserDecorator, args) {
ChromeCanaryBrowser.apply(this, arguments)
var parentOptions = this._getOptions
this._getOptions = function (url) {
return headlessGetOptions.call(this, url, args, parentOptions)
}
}
ChromeCanaryHeadlessBrowser.prototype = {
name: 'ChromeCanaryHeadless',
DEFAULT_CMD: {
linux: getBin(['google-chrome-canary', 'google-chrome-unstable']),
darwin: getChromeDarwin('/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'),
win32: getChromeExe('Chrome SxS')
},
ENV_CMD: 'CHROME_CANARY_BIN'
}
ChromeCanaryHeadlessBrowser.$inject = ['baseBrowserDecorator', 'args']
ChromiumBrowser.prototype = {
name: 'Chromium',
DEFAULT_CMD: {
// Try chromium-browser before chromium to avoid conflict with the legacy
// chromium-bsu package previously known as 'chromium' in Debian and Ubuntu.
linux: getBin(['chromium-browser', 'chromium']),
darwin: '/Applications/Chromium.app/Contents/MacOS/Chromium',
win32: getChromiumExe()
},
ENV_CMD: 'CHROMIUM_BIN'
}
ChromiumBrowser.$inject = ['baseBrowserDecorator', 'args']
var ChromiumHeadlessBrowser = function (baseBrowserDecorator, args) {
ChromiumBrowser.apply(this, arguments)
var parentOptions = this._getOptions
this._getOptions = function (url) {
return headlessGetOptions.call(this, url, args, parentOptions)
}
}
ChromiumHeadlessBrowser.prototype = {
name: 'ChromiumHeadless',
DEFAULT_CMD: {
// Try chromium-browser before chromium to avoid conflict with the legacy
// chromium-bsu package previously known as 'chromium' in Debian and Ubuntu.
linux: getBin(['chromium-browser', 'chromium']),
darwin: '/Applications/Chromium.app/Contents/MacOS/Chromium',
win32: getChromiumExe()
},
ENV_CMD: 'CHROMIUM_BIN'
}
var DartiumBrowser = function () {
ChromeBrowser.apply(this, arguments)
var checkedFlag = '--checked'
var dartFlags = process.env.DART_FLAGS || ''
var flags = dartFlags.split(' ')
if (flags.indexOf(checkedFlag) === -1) {
flags.push(checkedFlag)
process.env.DART_FLAGS = flags.join(' ')
}
}
DartiumBrowser.prototype = {
name: 'Dartium',
DEFAULT_CMD: {},
ENV_CMD: 'DARTIUM_BIN'
}
DartiumBrowser.$inject = ['baseBrowserDecorator', 'args']
// PUBLISH DI MODULE
module.exports = {
'launcher:Chrome': ['type', ChromeBrowser],
'launcher:ChromeHeadless': ['type', ChromeHeadlessBrowser],
'launcher:ChromeCanary': ['type', ChromeCanaryBrowser],
'launcher:ChromeCanaryHeadless': ['type', ChromeCanaryHeadlessBrowser],
'launcher:Chromium': ['type', ChromiumBrowser],
'launcher:ChromiumHeadless': ['type', ChromiumHeadlessBrowser],
'launcher:Dartium': ['type', DartiumBrowser]
}
module.exports.test = {
isJSFlags: isJSFlags,
sanitizeJSFlags: sanitizeJSFlags,
headlessGetOptions: headlessGetOptions,
canaryGetOptions: canaryGetOptions
}