Skip to content

Commit

Permalink
stream: throw invalid arg type from End Of Stream
Browse files Browse the repository at this point in the history
PR-URL: #41766
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Mary Marchini <oss@mmarchini.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
MrJithil authored Feb 10, 2022
1 parent bde184e commit fe7ca08
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
codes,
} = require('internal/errors');
const {
ERR_INVALID_ARG_TYPE,
ERR_STREAM_PREMATURE_CLOSE
} = codes;
const { once } = require('internal/util');
Expand Down Expand Up @@ -58,7 +59,7 @@ function eos(stream, options, callback) {

if (!isNodeStream(stream)) {
// TODO: Webstreams.
// TODO: Throw INVALID_ARG_TYPE.
throw new ERR_INVALID_ARG_TYPE('stream', 'Stream', stream);
}

const wState = stream._writableState;
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-stream-end-of-streams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
require('../common');
const assert = require('assert');

const { Duplex, finished } = require('stream');

assert.throws(
() => {
// Passing empty object to mock invalid stream
// should throw error
finished({}, () => {});
},
{ code: 'ERR_INVALID_ARG_TYPE' }
);

const streamObj = new Duplex();
streamObj.end();
// Below code should not throw any errors as the
// streamObj is `Stream`
finished(streamObj, () => {});
7 changes: 6 additions & 1 deletion test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,12 @@ const http = require('http');
const streamLike = new EE();
streamLike.readableEnded = true;
streamLike.readable = true;
finished(streamLike, common.mustCall());
assert.throws(
() => {
finished(streamLike, () => {});
},
{ code: 'ERR_INVALID_ARG_TYPE' }
);
streamLike.emit('close');
}

Expand Down

0 comments on commit fe7ca08

Please sign in to comment.