diff --git a/index.js b/index.js index d1608da..2d33953 100644 --- a/index.js +++ b/index.js @@ -17,8 +17,8 @@ export default async function getStream(stream, options) { } const getStreamContents = async (stream, {convertChunk, getContents}, {maxBuffer = Number.POSITIVE_INFINITY} = {}) => { - if (!stream) { - throw new Error('Expected a stream'); + if (!isAsyncIterable(stream)) { + throw new Error('The first argument must be a Readable.'); } let length = 0; @@ -42,6 +42,8 @@ const getStreamContents = async (stream, {convertChunk, getContents}, {maxBuffer } }; +const isAsyncIterable = stream => typeof stream === 'object' && stream !== null && typeof stream[Symbol.asyncIterator] === 'function'; + const getBufferedData = (chunks, getContents, length) => { try { return getContents(chunks, length); diff --git a/test.js b/test.js index eef577d..4ac21d3 100644 --- a/test.js +++ b/test.js @@ -102,6 +102,15 @@ test('handles streams with a single chunk larger than string max length', async t.is(bufferedData, ''); }); +const firstArgumentCheck = async (t, firstArgument) => { + await t.throwsAsync(getStream(firstArgument), {message: /first argument/}); +}; + +test('Throws if the first argument is undefined', firstArgumentCheck, undefined); +test('Throws if the first argument is null', firstArgumentCheck, null); +test('Throws if the first argument is a string', firstArgumentCheck, ''); +test('Throws if the first argument is an array', firstArgumentCheck, []); + test('native string', async t => { const result = await text(compose(fixtureString)); t.is(result, fixtureString);