lib: require a callback for end-of-stream

Make the callback mandatory as mostly done in all other Node.js
callback APIs so users explicitly have to decide what to do in error
cases.

This also documents the options for `Stream.finished()`.

When originally implemented it was missed that Stream.finished() has
an optional options object.

PR-URL: https://github.com/nodejs/node/pull/21058
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Mathias Buus <mathiasbuus@gmail.com>
This commit is contained in:
Ruben Bridgewater 2018-05-31 16:00:24 +02:00
parent 9dae0ae22b
commit 36468ca928
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
3 changed files with 60 additions and 9 deletions

View File

@ -1294,12 +1294,21 @@ implementors should not override this method, but instead implement
[`readable._destroy()`][readable-_destroy]. [`readable._destroy()`][readable-_destroy].
The default implementation of `_destroy()` for `Transform` also emit `'close'`. The default implementation of `_destroy()` for `Transform` also emit `'close'`.
### stream.finished(stream, callback) ### stream.finished(stream[, options], callback)
<!-- YAML <!-- YAML
added: v10.0.0 added: v10.0.0
--> -->
* `stream` {Stream} A readable and/or writable stream. * `stream` {Stream} A readable and/or writable stream.
* `options` {Object}
* `error` {boolean} If set to `false`, then a call to `emit('error', err)` is
not treated as finished. **Default**: `true`.
* `readable` {boolean} When set to `false`, the callback will be called when
the stream ends even though the stream might still be readable.
**Default**: `true`.
* `writable` {boolean} When set to `false`, the callback will be called when
the stream ends even though the stream might still be writable.
**Default**: `true`.
* `callback` {Function} A callback function that takes an optional error * `callback` {Function} A callback function that takes an optional error
argument. argument.
@ -2438,7 +2447,7 @@ contain multi-byte characters.
[zlib]: zlib.html [zlib]: zlib.html
[hwm-gotcha]: #stream_highwatermark_discrepancy_after_calling_readable_setencoding [hwm-gotcha]: #stream_highwatermark_discrepancy_after_calling_readable_setencoding
[pipeline]: #stream_stream_pipeline_streams_callback [pipeline]: #stream_stream_pipeline_streams_callback
[finished]: #stream_stream_finished_stream_callback [finished]: #stream_stream_finished_stream_options_callback
[stream-_flush]: #stream_transform_flush_callback [stream-_flush]: #stream_transform_flush_callback
[stream-_read]: #stream_readable_read_size_1 [stream-_read]: #stream_readable_read_size_1
[stream-_transform]: #stream_transform_transform_chunk_encoding_callback [stream-_transform]: #stream_transform_transform_chunk_encoding_callback

View File

@ -4,11 +4,10 @@
'use strict'; 'use strict';
const { const {
ERR_INVALID_ARG_TYPE,
ERR_STREAM_PREMATURE_CLOSE ERR_STREAM_PREMATURE_CLOSE
} = require('internal/errors').codes; } = require('internal/errors').codes;
function noop() {}
function isRequest(stream) { function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function'; return stream.setHeader && typeof stream.abort === 'function';
} }
@ -23,10 +22,19 @@ function once(callback) {
} }
function eos(stream, opts, callback) { function eos(stream, opts, callback) {
if (typeof opts === 'function') return eos(stream, null, opts); if (arguments.length === 2) {
if (!opts) opts = {}; callback = opts;
opts = {};
} else if (opts == null) {
opts = {};
} else if (typeof opts !== 'object') {
throw new ERR_INVALID_ARG_TYPE('opts', 'object', opts);
}
if (typeof callback !== 'function') {
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
}
callback = once(callback || noop); callback = once(callback);
const ws = stream._writableState; const ws = stream._writableState;
const rs = stream._readableState; const rs = stream._readableState;

View File

@ -91,8 +91,8 @@ const { promisify } = require('util');
{ {
const rs = fs.createReadStream('file-does-not-exist'); const rs = fs.createReadStream('file-does-not-exist');
finished(rs, common.mustCall((err) => { finished(rs, common.expectsError({
assert.strictEqual(err.code, 'ENOENT'); code: 'ENOENT'
})); }));
} }
@ -119,3 +119,37 @@ const { promisify } = require('util');
rs.push(null); rs.push(null);
rs.resume(); rs.resume();
} }
// Test faulty input values and options.
{
const rs = new Readable({
read() {}
});
assert.throws(
() => finished(rs, 'foo'),
{
name: /ERR_INVALID_ARG_TYPE/,
message: /callback/
}
);
assert.throws(
() => finished(rs, 'foo', () => {}),
{
name: /ERR_INVALID_ARG_TYPE/,
message: /opts/
}
);
assert.throws(
() => finished(rs, {}, 'foo'),
{
name: /ERR_INVALID_ARG_TYPE/,
message: /callback/
}
);
finished(rs, null, common.mustCall());
rs.push(null);
rs.resume();
}