UNPKG

deadpool

Version:

Spawn node.js programs with the speed of thought

74 lines (61 loc) 1.57 kB
var net = require('net'); var fs = require('fs'); var path = require('path'); var split = require('split'); var through = require('through2'); var deadpool = require('..'); var modules = { exit: function(proc) { proc.stdout.end('exit\n'); proc.stderr.end(); proc.exit(0); }, error: function(proc) { proc.stdout.end(); proc.stderr.end('error\n'); proc.exit(1); }, echo: function(proc) { var exited = false; proc.stdin .pipe(split()) .pipe(through(function(line, enc, next) { line = line.toString(); if (line === 'exit') { modules.exit(proc); exited = true; } else if (line === 'error') { modules.error(proc); exited = true; } else if (!exited) { proc.stdout.write('echo: ' + line + '\n'); } next(); })); proc.stdin.on('end', function() { proc.stdout.write('stdin closed! \n'); modules.exit(proc); }); }, args: function(proc) { proc.stdout.end(JSON.stringify(proc.args)); proc.stderr.end(); proc.exit(); } }; function loader(name) { return modules[name]; } var sock = path.join(__dirname, 'deadpool.sock'); if (fs.existsSync(sock)) fs.unlinkSync(sock); net.createServer(deadpool(loader)).listen(sock); process.on('SIGTERM', function() { console.error('received SIGTERM, exiting...'); process.exit(0); }); process.on('exit', function() { fs.unlinkSync(sock); }); process.on('uncaughtException', function(err) { console.error('Caught exception: ' + err); });