stdio-server.js
1.15 KB
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
var STDIN_POLL_INTERVAL = 10;
var stdin = require('system').stdin;
var options = phantom.casperArgs.options;
var Stream = require(options.spooky_lib + 'lib/stream');
var stream = new Stream();
stream.write = function (s) {
console.log(s);
return true;
};
function loop() {
var line = stdin.readLine();
var message;
if (line) {
try {
message = JSON.parse(line);
} catch (e) {
throw new Error('Could not parse "' + line + '" as JSON');
}
stream.emit('data', line);
if (message.method === 'run') {
return;
}
}
setTimeout(loop, STDIN_POLL_INTERVAL);
}
var StreamServer = require(options.spooky_lib +
'node_modules/tiny-jsonrpc/lib/tiny-jsonrpc').StreamServer;
var server = new StreamServer();
server.listen(stream);
var loopTimer;
server.start = function() {
// stdin.readLine blocks until input is available, so we must wait until setup
// is complete before checking for input. Otherwise, the module will never
// finish executing.
loopTimer = setTimeout(loop, 0);
};
server.stop = function() {
clearTimeout(loopTimer);
};
server.start();
module.exports = server;