forked from cypress-io/node-xvfb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (123 loc) · 3.74 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
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
fs.exists = fs.exists || path.exists;
fs.existsSync = fs.existsSync || path.existsSync;
function Xvfb(options) {
options = options || {};
this._display = (options.displayNum ? ':' + options.displayNum : null);
this._reuse = options.reuse;
this._timeout = options.timeout || 500;
this._silent = options.silent;
this._xvfb_args = options.xvfb_args || [];
}
Xvfb.prototype = {
start: function(cb) {
if (!this._process) {
var lockFile = this._lockFile();
this._setDisplayEnvVariable();
fs.exists(lockFile, function(exists) {
var didSpawnFail = false;
try {
this._spawnProcess(exists, function(e) {
didSpawnFail = true;
if (cb) cb(e);
});
} catch (e) {
return cb && cb(e);
}
var totalTime = 0;
(function checkIfStarted() {
fs.exists(lockFile, function(exists) {
if (didSpawnFail) {
// When spawn fails, the callback will immediately be called.
// So we don't have to check whether the lock file exists.
return;
}
if (exists) {
return cb && cb(null, this._process);
} else {
totalTime += 10;
if (totalTime > this._timeout) {
return cb && cb(new Error('Could not start Xvfb.'));
} else {
setTimeout(checkIfStarted.bind(this), 10);
}
}
});
}).bind(this)();
}.bind(this));
}
},
stop: function(cb) {
if (this._process) {
this._killProcess();
this._restoreDisplayEnvVariable();
var lockFile = this._lockFile();
var totalTime = 0;
(function checkIfStopped() {
fs.exists(lockFile, function(exists) {
if (!exists) {
return cb && cb(null, this._process);
} else {
totalTime += 10;
if (totalTime > this._timeout) {
return cb && cb(new Error('Could not stop Xvfb.'));
} else {
setTimeout(checkIfStopped.bind(this), 10);
}
}
});
}).bind(this)();
} else {
return cb && cb(null);
}
},
display: function() {
if (!this._display) {
var displayNum = 98;
var lockFile;
do {
displayNum++;
lockFile = this._lockFile(displayNum);
} while (!this._reuse && fs.existsSync(lockFile));
this._display = ':' + displayNum;
}
return this._display;
},
_setDisplayEnvVariable: function() {
this._oldDisplay = process.env.DISPLAY;
process.env.DISPLAY = this.display();
},
_restoreDisplayEnvVariable: function() {
process.env.DISPLAY = this._oldDisplay;
},
_spawnProcess: function(lockFileExists, onAsyncSpawnError) {
var display = this.display();
if (lockFileExists) {
if (!this._reuse) {
throw new Error('Display ' + display + ' is already in use and the "reuse" option is false.');
}
} else {
this._process = spawn('Xvfb', [ display ].concat(this._xvfb_args));
this._process.stderr.on('data', function(data) {
if (!this._silent) {
process.stderr.write(data);
}
}.bind(this));
// Bind an error listener to prevent an error from crashing node.
this._process.once('error', function(e) {
onAsyncSpawnError(e);
});
}
},
_killProcess: function() {
this._process.kill();
this._process = null;
},
_lockFile: function(displayNum) {
displayNum = displayNum || this.display().toString().replace(/^:/, '');
return '/tmp/.X' + displayNum + '-lock';
}
}
module.exports = Xvfb;