destroy
Advanced tools
Comparing version 1.1.1 to 1.2.0
85
index.js
@@ -15,2 +15,3 @@ /*! | ||
var EventEmitter = require('events').EventEmitter | ||
var ReadStream = require('fs').ReadStream | ||
@@ -28,31 +29,23 @@ var Stream = require('stream') | ||
/** | ||
* Destroy a stream. | ||
* Destroy the given stream, and optionally suppress any future `error` events. | ||
* | ||
* @param {object} stream | ||
* @param {boolean} suppress | ||
* @public | ||
*/ | ||
function destroy (stream) { | ||
if (stream instanceof ReadStream) { | ||
return destroyReadStream(stream) | ||
function destroy (stream, suppress) { | ||
if (isFsReadStream(stream)) { | ||
destroyReadStream(stream) | ||
} else if (isZlibStream(stream)) { | ||
destroyZlibStream(stream) | ||
} else if (hasDestroy(stream)) { | ||
stream.destroy() | ||
} | ||
if (stream instanceof Zlib.Gzip || | ||
stream instanceof Zlib.Gunzip || | ||
stream instanceof Zlib.Deflate || | ||
stream instanceof Zlib.DeflateRaw || | ||
stream instanceof Zlib.Inflate || | ||
stream instanceof Zlib.InflateRaw || | ||
stream instanceof Zlib.Unzip) { | ||
return destroyZlibStream(stream) | ||
if (isEventEmitter(stream) && suppress) { | ||
stream.removeAllListeners('error') | ||
stream.addListener('error', noop) | ||
} | ||
if (!(stream instanceof Stream)) { | ||
return stream | ||
} | ||
if (typeof stream.destroy === 'function') { | ||
stream.destroy() | ||
} | ||
return stream | ||
@@ -75,4 +68,2 @@ } | ||
} | ||
return stream | ||
} | ||
@@ -149,7 +140,55 @@ | ||
} | ||
} | ||
return stream | ||
/** | ||
* Determine if stream has destroy. | ||
* @private | ||
*/ | ||
function hasDestroy (stream) { | ||
return stream instanceof Stream && | ||
typeof stream.destroy === 'function' | ||
} | ||
/** | ||
* Determine if val is EventEmitter. | ||
* @private | ||
*/ | ||
function isEventEmitter (val) { | ||
return val instanceof EventEmitter | ||
} | ||
/** | ||
* Determine if stream is fs.ReadStream stream. | ||
* @private | ||
*/ | ||
function isFsReadStream (stream) { | ||
return stream instanceof ReadStream | ||
} | ||
/** | ||
* Determine if stream is Zlib stream. | ||
* @private | ||
*/ | ||
function isZlibStream (stream) { | ||
return stream instanceof Zlib.Gzip || | ||
stream instanceof Zlib.Gunzip || | ||
stream instanceof Zlib.Deflate || | ||
stream instanceof Zlib.DeflateRaw || | ||
stream instanceof Zlib.Inflate || | ||
stream instanceof Zlib.InflateRaw || | ||
stream instanceof Zlib.Unzip | ||
} | ||
/** | ||
* No-op function. | ||
* @private | ||
*/ | ||
function noop () {} | ||
/** | ||
* On drain handler to clear binding. | ||
@@ -156,0 +195,0 @@ * @private |
{ | ||
"name": "destroy", | ||
"description": "destroy a stream if possible", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -23,3 +23,3 @@ "name": "Jonathan Ong", | ||
"eslint-plugin-standard": "4.1.0", | ||
"mocha": "9.2.1", | ||
"mocha": "9.2.2", | ||
"nyc": "15.1.0" | ||
@@ -26,0 +26,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# Destroy | ||
# destroy | ||
@@ -20,7 +20,9 @@ [![NPM version][npm-image]][npm-url] | ||
### destroy(stream) | ||
### destroy(stream [, suppress]) | ||
Destroy the given stream. In most cases, this is identical to a simple | ||
`stream.destroy()` call. The rules are as follows for a given stream: | ||
Destroy the given stream, and optionally suppress any future `error` events. | ||
In most cases, this is identical to a simple `stream.destroy()` call. The rules | ||
are as follows for a given stream: | ||
1. If the `stream` is an instance of `ReadStream`, then call `stream.destroy()` | ||
@@ -27,0 +29,0 @@ and add a listener to the `open` event to call `stream.close()` if it is |
9018
177
64