blob: f64c06dfb85ccdc1c7d245c1e657d75d79142625 (
plain)
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
|
module.exports = startEchoServer;
function startEchoServer(outputStream, callback) {
if ('function' === typeof outputStream) {
callback = outputStream;
outputStream = null;
}
if ('function' !== typeof callback) {
callback = function(){};
}
var path = require('path').join(__dirname + '/../scripts/echo-server.js');
console.log(path);
var echoServer = require('child_process').spawn('nodejs', [ path ]);
var state = 'starting';
var processProxy = {
kill: function(signal) {
state = 'exiting';
echoServer.kill(signal);
}
};
if (outputStream) {
echoServer.stdout.pipe(outputStream);
echoServer.stderr.pipe(outputStream);
}
echoServer.stdout.on('data', function(chunk) {
chunk = chunk.toString();
if (/Server is listening/.test(chunk)) {
if (state === 'starting') {
state = 'ready';
callback(null, processProxy);
}
}
});
echoServer.on('exit', function(code, signal) {
echoServer = null;
if (state !== 'exiting') {
state = 'exited';
callback(new Error('Echo Server exited unexpectedly with code ' + code));
process.exit(1);
}
});
process.on('exit', function() {
if (echoServer && state === 'ready') {
echoServer.kill();
}
});
}
|