stream: fix finished writable/readable state
writable/readable does not indicate whether as stream is a Writable/Readable. This implements a better check for whether a object is a Writable/Readable. PR-URL: https://github.com/nodejs/node/pull/31527 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
70c32a6d19
commit
b851d7b986
@ -13,6 +13,18 @@ function isRequest(stream) {
|
|||||||
return stream.setHeader && typeof stream.abort === 'function';
|
return stream.setHeader && typeof stream.abort === 'function';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isReadable(stream) {
|
||||||
|
return typeof stream.readable === 'boolean' ||
|
||||||
|
typeof stream.readableEnded === 'boolean' ||
|
||||||
|
!!stream._readableState;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isWritable(stream) {
|
||||||
|
return typeof stream.writable === 'boolean' ||
|
||||||
|
typeof stream.writableEnded === 'boolean' ||
|
||||||
|
!!stream._writableState;
|
||||||
|
}
|
||||||
|
|
||||||
function eos(stream, opts, callback) {
|
function eos(stream, opts, callback) {
|
||||||
if (arguments.length === 2) {
|
if (arguments.length === 2) {
|
||||||
callback = opts;
|
callback = opts;
|
||||||
@ -28,8 +40,10 @@ function eos(stream, opts, callback) {
|
|||||||
|
|
||||||
callback = once(callback);
|
callback = once(callback);
|
||||||
|
|
||||||
let readable = opts.readable || (opts.readable !== false && stream.readable);
|
let readable = opts.readable ||
|
||||||
let writable = opts.writable || (opts.writable !== false && stream.writable);
|
(opts.readable !== false && isReadable(stream));
|
||||||
|
let writable = opts.writable ||
|
||||||
|
(opts.writable !== false && isWritable(stream));
|
||||||
|
|
||||||
const onlegacyfinish = () => {
|
const onlegacyfinish = () => {
|
||||||
if (!stream.writable) onfinish();
|
if (!stream.writable) onfinish();
|
||||||
|
@ -184,3 +184,21 @@ const { promisify } = require('util');
|
|||||||
finished(streamLike, common.mustCall);
|
finished(streamLike, common.mustCall);
|
||||||
streamLike.emit('close');
|
streamLike.emit('close');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const writable = new Writable({ write() {} });
|
||||||
|
writable.writable = false;
|
||||||
|
writable.destroy();
|
||||||
|
finished(writable, common.mustCall((err) => {
|
||||||
|
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const readable = new Readable();
|
||||||
|
readable.readable = false;
|
||||||
|
readable.destroy();
|
||||||
|
finished(readable, common.mustCall((err) => {
|
||||||
|
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user