diff --git a/.editorconfig b/.editorconfig index 98a761d..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[{package.json,*.yml}] +[*.yml] indent_style = space indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index b18bae5..f3fa8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ -sudo: false language: node_js node_js: - - '6' - - '4' + - '10' + - '8' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..b61027f --- /dev/null +++ b/index.d.ts @@ -0,0 +1,80 @@ +/// +import * as stream from 'stream'; + +declare const isStream: { + /** + @returns Whether `stream` is a [`Stream`](https://nodejs.org/api/stream.html#stream_stream). + + @example + ``` + import * as fs from 'fs'; + import isStream = require('is-stream'); + + isStream(fs.createReadStream('unicorn.png')); + //=> true + + isStream({}); + //=> false + ``` + */ + (stream: unknown): stream is stream.Stream; + + /** + @returns Whether `stream` is a [`stream.Writable`](https://nodejs.org/api/stream.html#stream_class_stream_writable). + + @example + ``` + import * as fs from 'fs'; + import isStream = require('is-stream'); + + isStream.writable(fs.createWriteStrem('unicorn.txt')); + //=> true + ``` + */ + writable(stream: unknown): stream is stream.Writable; + + /** + @returns Whether `stream` is a [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable). + + @example + ``` + import * as fs from 'fs'; + import isStream = require('is-stream'); + + isStream.readable(fs.createReadStream('unicorn.png')); + //=> true + ``` + */ + readable(stream: unknown): stream is stream.Readable; + + /** + @returns Whether `stream` is a [`stream.Duplex`](https://nodejs.org/api/stream.html#stream_class_stream_duplex). + + @example + ``` + import {Duplex} from 'stream'; + import isStream = require('is-stream'); + + isStream.duplex(new Duplex()); + //=> true + ``` + */ + duplex(stream: unknown): stream is stream.Duplex; + + /** + @returns Whether `stream` is a [`stream.Transform`](https://nodejs.org/api/stream.html#stream_class_stream_transform). + + @example + ``` + import * as fs from 'fs'; + import Stringify = require('streaming-json-stringify'); + import isStream = require('is-stream'); + + isStream.transform(Stringify()); + //=> true + ``` + */ + transform(input: unknown): input is stream.Transform; +}; + +export = isStream; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..234c632 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,25 @@ +import {expectType} from 'tsd'; +import * as stream from 'stream'; +import isStream = require('.'); + +const foo = ''; + +if (isStream(foo)) { + expectType(foo); +} + +if (isStream.writable(foo)) { + expectType(foo); +} + +if (isStream.readable(foo)) { + expectType(foo); +} + +if (isStream.duplex(foo)) { + expectType(new stream.Duplex()); +} + +if (isStream.transform(foo)) { + expectType(foo); +} diff --git a/license b/license index 654d0bf..e7af2f7 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json index 43cbddf..400065d 100644 --- a/package.json +++ b/package.json @@ -1,38 +1,41 @@ { - "name": "is-stream", - "version": "1.1.0", - "description": "Check if something is a Node.js stream", - "license": "MIT", - "repository": "sindresorhus/is-stream", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - "engines": { - "node": ">=4" - }, - "scripts": { - "test": "xo && ava" - }, - "files": [ - "index.js" - ], - "keywords": [ - "stream", - "type", - "streams", - "writable", - "readable", - "duplex", - "transform", - "check", - "detect", - "is" - ], - "devDependencies": { - "ava": "*", - "tempy": "^0.1.0", - "xo": "*" - } + "name": "is-stream", + "version": "1.1.0", + "description": "Check if something is a Node.js stream", + "license": "MIT", + "repository": "sindresorhus/is-stream", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo && ava && tsd" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "stream", + "type", + "streams", + "writable", + "readable", + "duplex", + "transform", + "check", + "detect", + "is" + ], + "devDependencies": { + "@types/node": "^11.13.6", + "ava": "^1.4.1", + "tempy": "^0.3.0", + "tsd": "^0.7.2", + "xo": "^0.24.0" + } } diff --git a/readme.md b/readme.md index b2de9e7..fdeadb9 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ ## Install ``` -$ npm install --save is-stream +$ npm install is-stream ``` diff --git a/test.js b/test.js index 22030da..d2e61e3 100644 --- a/test.js +++ b/test.js @@ -3,66 +3,66 @@ import Stream from 'stream'; import net from 'net'; import test from 'ava'; import tempy from 'tempy'; -import m from '.'; +import isStream from '.'; test('isStream()', t => { - t.true(m(new Stream.Stream())); - t.true(m(new Stream.Readable())); - t.true(m(new Stream.Writable())); - t.true(m(new Stream.Duplex())); - t.true(m(new Stream.Transform())); - t.true(m(new Stream.PassThrough())); - t.true(m(fs.createReadStream('test.js'))); - t.true(m(fs.createWriteStream(tempy.file()))); - t.true(m(new net.Socket())); - t.false(m({})); - t.false(m(null)); - t.false(m(undefined)); - t.false(m('')); + t.true(isStream(new Stream.Stream())); + t.true(isStream(new Stream.Readable())); + t.true(isStream(new Stream.Writable())); + t.true(isStream(new Stream.Duplex())); + t.true(isStream(new Stream.Transform())); + t.true(isStream(new Stream.PassThrough())); + t.true(isStream(fs.createReadStream('test.js'))); + t.true(isStream(fs.createWriteStream(tempy.file()))); + t.true(isStream(new net.Socket())); + t.false(isStream({})); + t.false(isStream(null)); + t.false(isStream(undefined)); + t.false(isStream('')); }); test('isStream.writable()', t => { - t.true(m.writable(new Stream.Writable())); - t.true(m.writable(new Stream.Duplex())); - t.true(m.writable(new Stream.Transform())); - t.true(m.writable(new Stream.PassThrough())); - t.true(m.writable(fs.createWriteStream(tempy.file()))); - t.false(m.writable(new Stream.Stream())); - t.false(m.writable(new Stream.Readable())); - t.false(m.writable(fs.createReadStream('test.js'))); - t.false(m.writable(new net.Socket())); + t.true(isStream.writable(new Stream.Writable())); + t.true(isStream.writable(new Stream.Duplex())); + t.true(isStream.writable(new Stream.Transform())); + t.true(isStream.writable(new Stream.PassThrough())); + t.true(isStream.writable(fs.createWriteStream(tempy.file()))); + t.false(isStream.writable(new Stream.Stream())); + t.false(isStream.writable(new Stream.Readable())); + t.false(isStream.writable(fs.createReadStream('test.js'))); + t.false(isStream.writable(new net.Socket())); }); test('isStream.readable()', t => { - t.true(m.readable(new Stream.Readable())); - t.true(m.readable(new Stream.Duplex())); - t.true(m.readable(new Stream.Transform())); - t.true(m.readable(new Stream.PassThrough())); - t.true(m.readable(fs.createReadStream('test.js'))); - t.false(m.readable(new Stream.Stream())); - t.false(m.readable(new Stream.Writable())); - t.false(m.readable(fs.createWriteStream(tempy.file()))); - t.false(m.readable(new net.Socket())); + t.true(isStream.readable(new Stream.Readable())); + t.true(isStream.readable(new Stream.Duplex())); + t.true(isStream.readable(new Stream.Transform())); + t.true(isStream.readable(new Stream.PassThrough())); + t.true(isStream.readable(fs.createReadStream('test.js'))); + t.false(isStream.readable(new Stream.Stream())); + t.false(isStream.readable(new Stream.Writable())); + t.false(isStream.readable(fs.createWriteStream(tempy.file()))); + t.false(isStream.readable(new net.Socket())); }); test('isStream.duplex()', t => { - t.true(m.duplex(new Stream.Duplex())); - t.true(m.duplex(new Stream.Transform())); - t.true(m.duplex(new Stream.PassThrough())); - t.false(m.duplex(new Stream.Stream())); - t.false(m.duplex(new Stream.Readable())); - t.false(m.duplex(new Stream.Writable())); - t.false(m.duplex(fs.createReadStream('test.js'))); - t.false(m.duplex(fs.createWriteStream(tempy.file()))); + t.true(isStream.duplex(new Stream.Duplex())); + t.true(isStream.duplex(new Stream.Transform())); + t.true(isStream.duplex(new Stream.PassThrough())); + t.false(isStream.duplex(new Stream.Stream())); + t.false(isStream.duplex(new Stream.Readable())); + t.false(isStream.duplex(new Stream.Writable())); + t.false(isStream.duplex(fs.createReadStream('test.js'))); + t.false(isStream.duplex(fs.createWriteStream(tempy.file()))); }); test('isStream.transform()', t => { - t.true(m.transform(new Stream.Transform())); - t.true(m.transform(new Stream.PassThrough())); - t.false(m.transform(new Stream.Duplex())); - t.false(m.transform(new Stream.Stream())); - t.false(m.transform(new Stream.Readable())); - t.false(m.transform(new Stream.Writable())); - t.false(m.transform(fs.createReadStream('test.js'))); - t.false(m.transform(fs.createWriteStream(tempy.file()))); + t.true(isStream.transform(new Stream.Transform())); + t.true(isStream.transform(new Stream.PassThrough())); + t.false(isStream.transform(new Stream.Duplex())); + t.false(isStream.transform(new Stream.Stream())); + t.false(isStream.transform(new Stream.Readable())); + t.false(isStream.transform(new Stream.Writable())); + t.false(isStream.transform(fs.createReadStream('test.js'))); + t.false(isStream.transform(fs.createWriteStream(tempy.file()))); });