diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index 2fe69207c6a841..24bde591ec34fe 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -8,7 +8,8 @@ const { codes, } = require('internal/errors'); const { - ERR_STREAM_PREMATURE_CLOSE + ERR_STREAM_PREMATURE_CLOSE, + ERR_INVALID_ARG_TYPE } = codes; const { once } = require('internal/util'); const { @@ -55,8 +56,7 @@ function eos(stream, options, callback) { const writable = options.writable ?? isWritableNodeStream(stream); 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..5ff6ae38f3c2ec --- /dev/null +++ b/test/parallel/test-stream-end-of-streams.js @@ -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, () => {});