Skip to content

Commit

Permalink
Bring in is-stream (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed May 2, 2019
1 parent 11d8691 commit 726aec8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
15 changes: 14 additions & 1 deletion lib/index.js
Expand Up @@ -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';
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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": {
Expand Down
32 changes: 32 additions & 0 deletions test/index.js
Expand Up @@ -261,6 +261,38 @@ test('send(200, <Stream>) with error on same tick', async t => {
}
});

test('send(200, <Stream>) 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);
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -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"

Expand Down

0 comments on commit 726aec8

Please sign in to comment.