• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4if (common.isWindows) {
5  // https://github.com/nodejs/node/issues/48300
6  common.skip('Does not work with cygwin quirks on Windows');
7}
8
9const assert = require('assert');
10const { spawn } = require('child_process');
11
12// Check that, once a child process has ended, it’s safe to read from a pipe
13// that the child had used as input.
14// We simulate that using cat | (head -n1; ...)
15
16const p1 = spawn('cat', { stdio: ['pipe', 'pipe', 'inherit'] });
17const p2 = spawn('head', ['-n1'], { stdio: [p1.stdout, 'pipe', 'inherit'] });
18
19// First, write the line that gets passed through p2, making 'head' exit.
20p1.stdin.write('hello\n');
21p2.stdout.setEncoding('utf8');
22p2.stdout.on('data', common.mustCall((chunk) => {
23  assert.strictEqual(chunk, 'hello\n');
24}));
25p2.on('exit', common.mustCall(() => {
26  // We can now use cat’s output, because 'head' is no longer reading from it.
27  p1.stdin.end('world\n');
28  p1.stdout.setEncoding('utf8');
29  p1.stdout.on('data', common.mustCall((chunk) => {
30    assert.strictEqual(chunk, 'world\n');
31  }));
32  p1.stdout.resume();
33}));
34