mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 14:25:18 +00:00

Closing the underlying resource completely has the unwanted side effect that the stream can no longer be used at all, including passing it to other child processes. What we want to avoid is accidentally reading from the stream; accordingly, it should be sufficient to stop its readable side manually, and otherwise leave the underlying resource intact. Fixes: https://github.com/nodejs/node/issues/27097 Refs: https://github.com/nodejs/node/pull/21209 PR-URL: https://github.com/nodejs/node/pull/27373 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
28 lines
973 B
JavaScript
28 lines
973 B
JavaScript
'use strict';
|
||
const common = require('../common');
|
||
const assert = require('assert');
|
||
const { spawn } = require('child_process');
|
||
|
||
// Check that, once a child process has ended, it’s safe to read from a pipe
|
||
// that the child had used as input.
|
||
// We simulate that using cat | (head -n1; ...)
|
||
|
||
const p1 = spawn('cat', { stdio: ['pipe', 'pipe', 'inherit'] });
|
||
const p2 = spawn('head', ['-n1'], { stdio: [p1.stdout, 'pipe', 'inherit'] });
|
||
|
||
// First, write the line that gets passed through p2, making 'head' exit.
|
||
p1.stdin.write('hello\n');
|
||
p2.stdout.setEncoding('utf8');
|
||
p2.stdout.on('data', common.mustCall((chunk) => {
|
||
assert.strictEqual(chunk, 'hello\n');
|
||
}));
|
||
p2.on('exit', common.mustCall(() => {
|
||
// We can now use cat’s output, because 'head' is no longer reading from it.
|
||
p1.stdin.end('world\n');
|
||
p1.stdout.setEncoding('utf8');
|
||
p1.stdout.on('data', common.mustCall((chunk) => {
|
||
assert.strictEqual(chunk, 'world\n');
|
||
}));
|
||
p1.stdout.resume();
|
||
}));
|