From c88b7c56db320147fa8bac88adeb58f9adca7b40 Mon Sep 17 00:00:00 2001 From: MrJithil Date: Sun, 30 Jan 2022 11:08:24 +0530 Subject: [PATCH] stream: throw invalid arg type from End Of Stream --- lib/internal/streams/end-of-stream.js | 3 ++- test/parallel/test-stream-end-of-streams.js | 24 +++++++++++++++++++++ test/parallel/test-stream-finished.js | 11 +++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-stream-end-of-streams.js diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index 2fe69207c6a841..7276bd31e639be 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'); @@ -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; 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, () => {}); diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index b05b8c542190eb..7e7764ddaae2c3 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -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'); }