github.com/orteth01/up@v0.2.0/internal/shim/index.js (about) 1 2 var child = require('child_process') 3 var byline = require('./byline') 4 5 /** 6 * Callback for the request. 7 */ 8 9 var callback 10 11 /** 12 * Child process for binary I/O. 13 */ 14 15 var proc = child.spawn('./main', { stdio: ['pipe', 'pipe', process.stderr] }) 16 17 proc.on('error', function(err){ 18 console.error('[shim] error: %s', err) 19 process.exit(1) 20 }) 21 22 proc.on('exit', function(code, signal){ 23 console.error('[shim] exit: code=%s signal=%s', code, signal) 24 process.exit(1) 25 }) 26 27 /** 28 * Newline-delimited JSON stdout. 29 */ 30 31 var out = byline(proc.stdout) 32 33 out.on('data', function(line){ 34 if (process.env.DEBUG_SHIM) console.log('[shim] parsing: `%s`', line) 35 var msg = JSON.parse(line) 36 callback(msg.error, msg.value) 37 }) 38 39 /** 40 * Handle events. 41 */ 42 43 exports.handle = function(event, ctx, cb) { 44 callback = cb 45 ctx.callbackWaitsForEmptyEventLoop = false 46 47 proc.stdin.write(JSON.stringify({ 48 "event": event, 49 "context": ctx 50 })+'\n'); 51 }