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
  • Loading branch information
MrJithil committed Jan 30, 2022
1 parent 6edd9a5 commit c88b7c5
Show file tree
Hide file tree
Showing 3 changed files with 36 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 @@ -56,7 +57,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
24 changes: 24 additions & 0 deletions test/parallel/test-stream-end-of-streams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'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',
name: 'TypeError',
message: /stream/,
}
);

const streamObj = new Duplex();
streamObj.end();
// Below code should not throw any errors as the
// streamObj is `Stream`
finished(streamObj, () => {});
11 changes: 10 additions & 1 deletion test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,16 @@ 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',
name: 'TypeError',
message: /stream/,
}
);
streamLike.emit('close');
}

Expand Down

0 comments on commit c88b7c5

Please sign in to comment.