index.js 4.16 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
/*!
 * destroy
 * Copyright(c) 2014 Jonathan Ong
4
 * Copyright(c) 2015-2022 Douglas Christopher Wilson
Rosanny Sihombing's avatar
Rosanny Sihombing committed
5
6
7
8
9
10
11
12
13
14
 * MIT Licensed
 */

'use strict'

/**
 * Module dependencies.
 * @private
 */

15
var EventEmitter = require('events').EventEmitter
Rosanny Sihombing's avatar
Rosanny Sihombing committed
16
17
var ReadStream = require('fs').ReadStream
var Stream = require('stream')
18
var Zlib = require('zlib')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
19
20
21
22
23
24
25
26
27

/**
 * Module exports.
 * @public
 */

module.exports = destroy

/**
28
 * Destroy the given stream, and optionally suppress any future `error` events.
Rosanny Sihombing's avatar
Rosanny Sihombing committed
29
30
 *
 * @param {object} stream
31
 * @param {boolean} suppress
Rosanny Sihombing's avatar
Rosanny Sihombing committed
32
33
34
 * @public
 */

35
36
37
38
39
40
41
function destroy (stream, suppress) {
  if (isFsReadStream(stream)) {
    destroyReadStream(stream)
  } else if (isZlibStream(stream)) {
    destroyZlibStream(stream)
  } else if (hasDestroy(stream)) {
    stream.destroy()
Rosanny Sihombing's avatar
Rosanny Sihombing committed
42
43
  }

44
45
46
  if (isEventEmitter(stream) && suppress) {
    stream.removeAllListeners('error')
    stream.addListener('error', noop)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
47
48
49
50
51
52
53
54
55
56
57
58
  }

  return stream
}

/**
 * Destroy a ReadStream.
 *
 * @param {object} stream
 * @private
 */

59
function destroyReadStream (stream) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
60
61
62
63
64
65
  stream.destroy()

  if (typeof stream.close === 'function') {
    // node.js core bug work-around
    stream.on('open', onOpenClose)
  }
66
}
Rosanny Sihombing's avatar
Rosanny Sihombing committed
67

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 * Close a Zlib stream.
 *
 * Zlib streams below Node.js 4.5.5 have a buggy implementation
 * of .close() when zlib encountered an error.
 *
 * @param {object} stream
 * @private
 */

function closeZlibStream (stream) {
  if (stream._hadError === true) {
    var prop = stream._binding === null
      ? '_binding'
      : '_handle'

    stream[prop] = {
      close: function () { this[prop] = null }
    }
  }

  stream.close()
}

/**
 * Destroy a Zlib stream.
 *
 * Zlib streams don't have a destroy function in Node.js 6. On top of that
 * simply calling destroy on a zlib stream in Node.js 8+ will result in a
 * memory leak. So until that is fixed, we need to call both close AND destroy.
 *
 * PR to fix memory leak: https://github.com/nodejs/node/pull/23734
 *
 * In Node.js 6+8, it's important that destroy is called before close as the
 * stream would otherwise emit the error 'zlib binding closed'.
 *
 * @param {object} stream
 * @private
 */

function destroyZlibStream (stream) {
  if (typeof stream.destroy === 'function') {
    // node.js core bug work-around
    // istanbul ignore if: node.js 0.8
    if (stream._binding) {
      // node.js < 0.10.0
      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) {
      // node.js >= 12, ^11.1.0, ^10.15.1
      stream.destroy()
    } else if (stream._destroy && typeof stream.close === 'function') {
      // node.js 7, 8
      stream.destroyed = true
      stream.close()
    } else {
      // fallback
      // istanbul ignore next
      stream.destroy()
    }
  } else if (typeof stream.close === 'function') {
    // node.js < 8 fallback
    closeZlibStream(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.
 * @private
 */

// istanbul ignore next: node.js 0.8
function onDrainClearBinding () {
  this._binding.clear()
Rosanny Sihombing's avatar
Rosanny Sihombing committed
197
198
199
200
201
202
203
}

/**
 * On open handler to close stream.
 * @private
 */

204
function onOpenClose () {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
205
206
207
208
209
  if (typeof this.fd === 'number') {
    // actually close down the fd
    this.close()
  }
}