-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
child_process: support stdio option in fork()
This commit allows child_process.fork() to pass stdio options to spawn(). This allows fork() to more easily take advantage of additional stdio channels. Refs: nodejs/node-v0.x-archive#5727 PR-URL: #7811 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
- Loading branch information
Showing
3 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const cp = require('child_process'); | ||
const net = require('net'); | ||
|
||
if (process.argv[2] === 'child') { | ||
process.stdout.write('this should be ignored'); | ||
process.stderr.write('this should not be ignored'); | ||
|
||
const pipe = new net.Socket({ fd: 4 }); | ||
|
||
process.on('disconnect', () => { | ||
pipe.unref(); | ||
}); | ||
|
||
pipe.setEncoding('utf8'); | ||
pipe.on('data', (data) => { | ||
process.send(data); | ||
}); | ||
} else { | ||
assert.throws(() => { | ||
cp.fork(__filename, {stdio: ['pipe', 'pipe', 'pipe', 'pipe']}); | ||
}, /Forked processes must have an IPC channel/); | ||
|
||
let ipc = ''; | ||
let stderr = ''; | ||
const buf = Buffer.from('data to send via pipe'); | ||
const child = cp.fork(__filename, ['child'], { | ||
stdio: [0, 'ignore', 'pipe', 'ipc', 'pipe'] | ||
}); | ||
|
||
assert.strictEqual(child.stdout, null); | ||
|
||
child.on('message', (msg) => { | ||
ipc += msg; | ||
|
||
if (ipc === buf.toString()) { | ||
child.disconnect(); | ||
} | ||
}); | ||
|
||
child.stderr.on('data', (chunk) => { | ||
stderr += chunk; | ||
}); | ||
|
||
child.on('exit', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stderr, 'this should not be ignored'); | ||
})); | ||
|
||
child.stdio[4].write(buf); | ||
} |