1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const { Readable, Duplex, pipeline } = require('stream');
5
6// Test that the callback for pipeline() is called even when the ._destroy()
7// method of the stream places an .end() request to itself that does not
8// get processed before the destruction of the stream (i.e. the 'close' event).
9// Refs: https://github.com/nodejs/node/issues/24456
10
11const readable = new Readable({
12  read: common.mustCall()
13});
14
15const duplex = new Duplex({
16  write(chunk, enc, cb) {
17    // Simulate messages queueing up.
18  },
19  read() {},
20  destroy(err, cb) {
21    // Call end() from inside the destroy() method, like HTTP/2 streams
22    // do at the time of writing.
23    this.end();
24    cb(err);
25  }
26});
27
28duplex.on('finished', common.mustNotCall());
29
30pipeline(readable, duplex, common.mustCall((err) => {
31  assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
32}));
33
34// Write one chunk of data, and destroy the stream later.
35// That should trigger the pipeline destruction.
36readable.push('foo');
37setImmediate(() => {
38  readable.destroy();
39});
40