-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
94 lines (76 loc) · 1.96 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
var q = require('q');
var fs = require('fs');
var spawn = require('child_process').spawn;
var whichSync = require('which').sync;
var Xvfb = require('xvfb');
var chromeLocation = require('chrome-location');
var HeadlessChromeBrowser = function (baseBrowserDecorator, config, emitter, args) {
var self = this;
this.name = 'HeadlessChrome';
baseBrowserDecorator(self);
var xvfb = new Xvfb({
silent: true,
reuse: true,
xvfb_args: [
'-screen',
'0',
'1024x768x24',
'-ac'
]
});
fs.openSync(xvfb._lockFile(), 'a');
var display = xvfb.display();
var chromeProcess;
this.start = function (url) {
if (!chromeLocation) {
console.error('Cannot find Chrome executable.');
self.forceKill();
}
try {
whichSync('Xvfb');
} catch (e) {
console.error('Cannot find Xvfb executable.');
self.forceKill();
}
var display = xvfb.display();
var chromeFlags = [
'--user-data-dir=' + this._tempDir,
'--no-default-browser-check',
'--no-first-run',
'--disable-default-apps',
'--disable-popup-blocking',
'--disable-translate',
'--disable-background-timer-throttling',
'--display=' + display
].concat(args.chromeFlags || [], [url]);
xvfb.start(function (err) {
if (err) {
console.error('Failed to start Xvfb: ' + err);
self.forceKill();
return;
}
chromeProcess = spawn(chromeLocation, chromeFlags);
});
};
this.on('kill', function (done) {
function allDone() {
self._done();
if (done) {
done();
}
}
var deferred = q.defer();
xvfb.stop(function () {
chromeProcess && chromeProcess.kill('SIGINT');
deferred.promise.then(allDone);
});
});
this.forceKill = function () {
return q.promise(function (resolve) {
self.emit('kill', resolve);
});
}
};
module.exports = {
'launcher:HeadlessChrome': ['type', HeadlessChromeBrowser]
};