From 726aec8c8c5489accd881eb14708c479ddb0dbdf Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 2 May 2019 19:40:29 +0200 Subject: [PATCH] Bring in is-stream (#397) --- lib/index.js | 15 ++++++++++++++- package.json | 1 - test/index.js | 32 ++++++++++++++++++++++++++++++++ yarn.lock | 2 +- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 2a727d3b..fbe7c9fc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,7 +5,20 @@ const {Stream} = require('stream'); // Packages const contentType = require('content-type'); const getRawBody = require('raw-body'); -const {readable} = require('is-stream'); + +// based on is-stream https://github.com/sindresorhus/is-stream/blob/c918e3795ea2451b5265f331a00fb6a8aaa27816/license +function isStream(stream) { + return stream !== null && + typeof stream === 'object' && + typeof stream.pipe === 'function'; +} + +function readable(stream) { + return isStream(stream) && + stream.readable !== false && + typeof stream._read === 'function' && + typeof stream._readableState === 'object'; +} const {NODE_ENV} = process.env; const DEV = NODE_ENV === 'development'; diff --git a/package.json b/package.json index f12f48b5..cc602556 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "dependencies": { "arg": "4.1.0", "content-type": "1.0.4", - "is-stream": "1.1.0", "raw-body": "2.3.2" }, "eslintConfig": { diff --git a/test/index.js b/test/index.js index 00d4cde5..10a2e748 100644 --- a/test/index.js +++ b/test/index.js @@ -261,6 +261,38 @@ test('send(200, ) with error on same tick', async t => { } }); +test('send(200, ) custom stream', async t => { + const fn = async (req, res) => { + const handlers = {}; + const stream = { + readable: true, + _read: () => '', + _readableState: {}, + on: (key, fns) => { + handlers[key] = fns; + }, + emit: key => { + handlers[key](); + }, + pipe: () => {}, + end: () => {} + }; + + send(res, 200, stream); + + stream.emit('close'); + }; + + const url = await getUrl(fn); + + try { + await request(url); + t.fail(); + } catch (err) { + t.deepEqual(err.statusCode, 500); + } +}); + test('custom error', async t => { const fn = () => { sleep(50); diff --git a/yarn.lock b/yarn.lock index b0351e11..3d160096 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2307,7 +2307,7 @@ is-retry-allowed@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-1.1.0.tgz#11a060568b67339444033d0125a61a20d564fb34" -is-stream@1.1.0, is-stream@^1.0.0, is-stream@^1.1.0: +is-stream@^1.0.0, is-stream@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"