|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict' |
|
|
|
|
|
|
|
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter |
|
var ReadStream = require('fs').ReadStream |
|
var Stream = require('stream') |
|
var Zlib = require('zlib') |
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = destroy |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function destroy (stream, suppress) { |
|
if (isFsReadStream(stream)) { |
|
destroyReadStream(stream) |
|
} else if (isZlibStream(stream)) { |
|
destroyZlibStream(stream) |
|
} else if (hasDestroy(stream)) { |
|
stream.destroy() |
|
} |
|
|
|
if (isEventEmitter(stream) && suppress) { |
|
stream.removeAllListeners('error') |
|
stream.addListener('error', noop) |
|
} |
|
|
|
return stream |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function destroyReadStream (stream) { |
|
stream.destroy() |
|
|
|
if (typeof stream.close === 'function') { |
|
|
|
stream.on('open', onOpenClose) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function closeZlibStream (stream) { |
|
if (stream._hadError === true) { |
|
var prop = stream._binding === null |
|
? '_binding' |
|
: '_handle' |
|
|
|
stream[prop] = { |
|
close: function () { this[prop] = null } |
|
} |
|
} |
|
|
|
stream.close() |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function destroyZlibStream (stream) { |
|
if (typeof stream.destroy === 'function') { |
|
|
|
|
|
if (stream._binding) { |
|
|
|
stream.destroy() |
|
if (stream._processing) { |
|
stream._needDrain = true |
|
stream.once('drain', onDrainClearBinding) |
|
} else { |
|
stream._binding.clear() |
|
} |
|
} else if (stream._destroy && stream._destroy !== Stream.Transform.prototype._destroy) { |
|
|
|
stream.destroy() |
|
} else if (stream._destroy && typeof stream.close === 'function') { |
|
|
|
stream.destroyed = true |
|
stream.close() |
|
} else { |
|
|
|
|
|
stream.destroy() |
|
} |
|
} else if (typeof stream.close === 'function') { |
|
|
|
closeZlibStream(stream) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function hasDestroy (stream) { |
|
return stream instanceof Stream && |
|
typeof stream.destroy === 'function' |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function isEventEmitter (val) { |
|
return val instanceof EventEmitter |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function isFsReadStream (stream) { |
|
return stream instanceof ReadStream |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function noop () {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onDrainClearBinding () { |
|
this._binding.clear() |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function onOpenClose () { |
|
if (typeof this.fd === 'number') { |
|
|
|
this.close() |
|
} |
|
} |
|
|