123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- '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()
- }
- }
|