diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index d36b1a894da531..0b2044d53e735b 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -8,6 +8,7 @@ const { codes, } = require('internal/errors'); const { + ERR_INVALID_ARG_TYPE, ERR_STREAM_PREMATURE_CLOSE } = codes; const { once } = require('internal/util'); @@ -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; diff --git a/test/parallel/test-stream-end-of-streams.js b/test/parallel/test-stream-end-of-streams.js new file mode 100644 index 00000000000000..80a39d052bf8b4 --- /dev/null +++ b/test/parallel/test-stream-end-of-streams.js @@ -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, () => {}); diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index b05b8c542190eb..47138e878e4407 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -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'); }