-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
5 changed files
with
193 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) => { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} |
This assumes top-level streams only and disregards subpipes. For example:
Promise would be resolved before completion of StreamB in StreamAB. Can potentially "flatten" the list of streams if it's possible to detect if one stream is being piped to another stream before resolving parent level stream.