Skip to content

Commit

Permalink
stream: return readable stream on promisified pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Oct 29, 2021
1 parent c0a7020 commit 56ec619
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/stream/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ function pipeline(...streams) {
signal = options.signal;
}

pl(streams, (err, value) => {
const stream = pl(streams, (err, value) => {
if (err) {
reject(err);
} else {
} else if (value !== undefined) {
resolve(value);
} else if (stream.readable) {
resolve(stream);
} else {
resolve();
}
}, { signal });
});
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -1406,3 +1406,29 @@ const tsp = require('timers/promises');
}));
ac.abort();
}

{
const pipelinePromise = promisify(pipeline);

async function run() {
const read = new Readable({
read() {}
});

const duplex = new PassThrough();

read.push('data');
read.push(null);

const stream = await pipelinePromise(read, duplex);

let ret = ''
for await (const x of stream) {
ret += x;
}

assert.strictEqual(ret, 'data');
}

run();
}

0 comments on commit 56ec619

Please sign in to comment.