[Pkg-javascript-commits] [node-foreground-child] 39/69: Forward IPC messages to foregrounded child process

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 11:43:05 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-foreground-child.

commit ad373e0af1214ed61d60badf4cfe2015896ed016
Author: Anna Henningsen <anna at addaleax.net>
Date:   Sat Jun 11 18:26:41 2016 +0200

    Forward IPC messages to foregrounded child process
    
    If the current process has an IPC channel available as indicated
    by the presence of `process.send`, redirect all messages from the
    parent process to the foregrounded child and vice versa.
    
    Ref: https://github.com/tapjs/spawn-wrap/issues/23
---
 index.js      | 20 +++++++++++++++++++-
 test/basic.js | 28 ++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/index.js b/index.js
index 21a395a..cd52017 100644
--- a/index.js
+++ b/index.js
@@ -60,8 +60,14 @@ module.exports = function (program, args, cb) {
     args = [].slice.call(arguments, 1, arrayIndex)
   }
 
+  var spawnOpts = { stdio: ['inherit', 'inherit', 'inherit'] }
+
+  if (process.send) {
+    spawnOpts.stdio.push('ipc')
+  }
+
   var spawnfn = needsCrossSpawn(program) ? crossSpawn : spawn
-  var child = spawnfn(program, args, { stdio: 'inherit' })
+  var child = spawnfn(program, args, spawnOpts)
 
   var childExited = false
   signalExit(function (code, signal) {
@@ -89,5 +95,17 @@ module.exports = function (program, args, cb) {
     })
   })
 
+  if (process.send) {
+    process.removeAllListeners('message')
+
+    child.on('message', function (message, sendHandle) {
+      process.send(message, sendHandle)
+    })
+
+    process.on('message', function (message, sendHandle) {
+      child.send(message, sendHandle)
+    })
+  }
+
   return child
 }
diff --git a/test/basic.js b/test/basic.js
index 7ceff2a..ebd0da5 100644
--- a/test/basic.js
+++ b/test/basic.js
@@ -16,6 +16,14 @@ if (process.argv[2] === 'child') {
   case '2':
     process.exit(+process.argv[3])
     break
+
+  case 'ipc':
+    process.on('message', function(m) {
+      console.log('message received')
+      process.send(m)
+      process.exit(0)
+    })
+    break
   }
 
   return
@@ -150,6 +158,26 @@ t.test('beforeExitHandler', function (t) {
   t.end()
 })
 
+t.test('IPC forwarding', function (t) {
+  t.plan(5)
+  var prog = process.execPath
+  var args = [__filename, 'parent', 'ipc']
+  child = spawn(prog, args, { stdio: ['ipc', 'pipe', 'pipe'] })
+  var out = ''
+  var messages = []
+  child.on('message', function (m) { messages.push(m) })
+  child.stdout.on('data', function (c) { out += c })
+
+  child.send({ data: 'foobar' })
+  child.on('close', function (code, signal) {
+    t.equal(signal, null)
+    t.equal(code, 0)
+    t.equal(out, 'stdout\nmessage received\n')
+    t.equal(messages.length, 1)
+    t.equal(messages[0].data, 'foobar')
+  })
+})
+
 function isZero10OnTravis () {
   return process.env.TRAVIS && /^v0\.10\.[0-9]+$/.test(process.version) ?
     'skip on 0.10 on Travis' : false

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-foreground-child.git



More information about the Pkg-javascript-commits mailing list