Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
stream: add promises version to utility functions
PR-URL: #33991 Fixes: #33582 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
- Loading branch information
Showing
with
193 additions
and 8 deletions.
- +14 −8 doc/api/stream.md
- +36 −0 lib/stream.js
- +39 −0 lib/stream/promises.js
- +1 −0 node.gyp
- +103 −0 test/parallel/test-stream-promises.js
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
| @@ -0,0 +1,39 @@ | ||
| 'use strict'; | ||
|
|
||
| const { | ||
| Promise, | ||
| } = primordials; | ||
|
|
||
| let pl; | ||
| let eos; | ||
|
|
||
| function pipeline(...streams) { | ||
| if (!pl) pl = require('internal/streams/pipeline'); | ||
| return new Promise((resolve, reject) => { | ||
| pl(...streams, (err, value) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(value); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function finished(stream, opts) { | ||
| if (!eos) eos = require('internal/streams/end-of-stream'); | ||
| return new Promise((resolve, reject) => { | ||
| eos(stream, opts, (err) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| finished, | ||
| pipeline, | ||
| }; |
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
| @@ -78,6 +78,7 @@ | ||
| 'lib/readline.js', | ||
| 'lib/repl.js', | ||
| 'lib/stream.js', | ||
| 'lib/stream/promises.js', | ||
| 'lib/_stream_readable.js', | ||
| 'lib/_stream_writable.js', | ||
| 'lib/_stream_duplex.js', | ||
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
| @@ -0,0 +1,103 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const stream = require('stream'); | ||
| const { | ||
| Readable, | ||
| Writable, | ||
| promises, | ||
| } = stream; | ||
| const { | ||
| finished, | ||
| pipeline, | ||
| } = require('stream/promises'); | ||
| const fs = require('fs'); | ||
| const assert = require('assert'); | ||
| const { promisify } = require('util'); | ||
|
|
||
| assert.strictEqual(promises.pipeline, pipeline); | ||
| assert.strictEqual(promises.finished, finished); | ||
| assert.strictEqual(pipeline, promisify(stream.pipeline)); | ||
| assert.strictEqual(finished, promisify(stream.finished)); | ||
|
|
||
| // pipeline success | ||
| { | ||
| let finished = false; | ||
| const processed = []; | ||
| const expected = [ | ||
| Buffer.from('a'), | ||
| Buffer.from('b'), | ||
| Buffer.from('c') | ||
| ]; | ||
|
|
||
| const read = new Readable({ | ||
| read() { } | ||
| }); | ||
|
|
||
| const write = new Writable({ | ||
| write(data, enc, cb) { | ||
| processed.push(data); | ||
| cb(); | ||
| } | ||
| }); | ||
|
|
||
| write.on('finish', () => { | ||
| finished = true; | ||
| }); | ||
|
|
||
| for (let i = 0; i < expected.length; i++) { | ||
| read.push(expected[i]); | ||
| } | ||
| read.push(null); | ||
|
|
||
| pipeline(read, write).then(common.mustCall((value) => { | ||
| assert.ok(finished); | ||
| assert.deepStrictEqual(processed, expected); | ||
| })); | ||
| } | ||
|
|
||
| // pipeline error | ||
| { | ||
| const read = new Readable({ | ||
| read() { } | ||
| }); | ||
|
|
||
| const write = new Writable({ | ||
| write(data, enc, cb) { | ||
| cb(); | ||
| } | ||
| }); | ||
|
|
||
| read.push('data'); | ||
| setImmediate(() => read.destroy()); | ||
|
|
||
| pipeline(read, write).catch(common.mustCall((err) => { | ||
| assert.ok(err, 'should have an error'); | ||
| })); | ||
| } | ||
|
|
||
| // finished success | ||
| { | ||
| async function run() { | ||
| const rs = fs.createReadStream(__filename); | ||
|
|
||
| let ended = false; | ||
| rs.resume(); | ||
| rs.on('end', () => { | ||
| ended = true; | ||
| }); | ||
| await finished(rs); | ||
| assert(ended); | ||
| } | ||
|
|
||
| run().then(common.mustCall()); | ||
| } | ||
|
|
||
| // finished error | ||
| { | ||
| const rs = fs.createReadStream('file-does-not-exist'); | ||
|
|
||
| assert.rejects(finished(rs), { | ||
| code: 'ENOENT' | ||
| }).then(common.mustCall()); | ||
| } |

