(outputStream, callback)
| 1 | module.exports = startEchoServer; |
| 2 | |
| 3 | function startEchoServer(outputStream, callback) { |
| 4 | if ('function' === typeof outputStream) { |
| 5 | callback = outputStream; |
| 6 | outputStream = null; |
| 7 | } |
| 8 | if ('function' !== typeof callback) { |
| 9 | callback = function(){}; |
| 10 | } |
| 11 | |
| 12 | var path = require('path').join(__dirname + '/../scripts/echo-server.js'); |
| 13 | |
| 14 | console.log(path); |
| 15 | |
| 16 | var echoServer = require('child_process').spawn('node', [ path ]); |
| 17 | |
| 18 | var state = 'starting'; |
| 19 | |
| 20 | var processProxy = { |
| 21 | kill: function(signal) { |
| 22 | state = 'exiting'; |
| 23 | echoServer.kill(signal); |
| 24 | } |
| 25 | }; |
| 26 | |
| 27 | if (outputStream) { |
| 28 | echoServer.stdout.pipe(outputStream); |
| 29 | echoServer.stderr.pipe(outputStream); |
| 30 | } |
| 31 | |
| 32 | echoServer.stdout.on('data', function(chunk) { |
| 33 | chunk = chunk.toString(); |
| 34 | if (/Server is listening/.test(chunk)) { |
| 35 | if (state === 'starting') { |
| 36 | state = 'ready'; |
| 37 | callback(null, processProxy); |
| 38 | } |
| 39 | } |
| 40 | }); |
| 41 | |
| 42 | echoServer.on('exit', function(code, signal) { |
| 43 | echoServer = null; |
| 44 | if (state !== 'exiting') { |
| 45 | state = 'exited'; |
| 46 | callback(new Error('Echo Server exited unexpectedly with code ' + code)); |
| 47 | process.exit(1); |
| 48 | } |
| 49 | }); |
| 50 | |
| 51 | process.on('exit', function() { |
| 52 | if (echoServer && state === 'ready') { |
| 53 | echoServer.kill(); |
| 54 | } |
| 55 | }); |
| 56 | } |
no outgoing calls
no test coverage detected