1 | /*!
|
---|
2 | * destroy
|
---|
3 | * Copyright(c) 2014 Jonathan Ong
|
---|
4 | * Copyright(c) 2015-2022 Douglas Christopher Wilson
|
---|
5 | * MIT Licensed
|
---|
6 | */
|
---|
7 |
|
---|
8 | 'use strict'
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Module dependencies.
|
---|
12 | * @private
|
---|
13 | */
|
---|
14 |
|
---|
15 | var EventEmitter = require('events').EventEmitter
|
---|
16 | var ReadStream = require('fs').ReadStream
|
---|
17 | var Stream = require('stream')
|
---|
18 | var Zlib = require('zlib')
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Module exports.
|
---|
22 | * @public
|
---|
23 | */
|
---|
24 |
|
---|
25 | module.exports = destroy
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Destroy the given stream, and optionally suppress any future `error` events.
|
---|
29 | *
|
---|
30 | * @param {object} stream
|
---|
31 | * @param {boolean} suppress
|
---|
32 | * @public
|
---|
33 | */
|
---|
34 |
|
---|
35 | function destroy (stream, suppress) {
|
---|
36 | if (isFsReadStream(stream)) {
|
---|
37 | destroyReadStream(stream)
|
---|
38 | } else if (isZlibStream(stream)) {
|
---|
39 | destroyZlibStream(stream)
|
---|
40 | } else if (hasDestroy(stream)) {
|
---|
41 | stream.destroy()
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (isEventEmitter(stream) && suppress) {
|
---|
45 | stream.removeAllListeners('error')
|
---|
46 | stream.addListener('error', noop)
|
---|
47 | }
|
---|
48 |
|
---|
49 | return stream
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Destroy a ReadStream.
|
---|
54 | *
|
---|
55 | * @param {object} stream
|
---|
56 | * @private
|
---|
57 | */
|
---|
58 |
|
---|
59 | function destroyReadStream (stream) {
|
---|
60 | stream.destroy()
|
---|
61 |
|
---|
62 | if (typeof stream.close === 'function') {
|
---|
63 | // node.js core bug work-around
|
---|
64 | stream.on('open', onOpenClose)
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Close a Zlib stream.
|
---|
70 | *
|
---|
71 | * Zlib streams below Node.js 4.5.5 have a buggy implementation
|
---|
72 | * of .close() when zlib encountered an error.
|
---|
73 | *
|
---|
74 | * @param {object} stream
|
---|
75 | * @private
|
---|
76 | */
|
---|
77 |
|
---|
78 | function closeZlibStream (stream) {
|
---|
79 | if (stream._hadError === true) {
|
---|
80 | var prop = stream._binding === null
|
---|
81 | ? '_binding'
|
---|
82 | : '_handle'
|
---|
83 |
|
---|
84 | stream[prop] = {
|
---|
85 | close: function () { this[prop] = null }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | stream.close()
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Destroy a Zlib stream.
|
---|
94 | *
|
---|
95 | * Zlib streams don't have a destroy function in Node.js 6. On top of that
|
---|
96 | * simply calling destroy on a zlib stream in Node.js 8+ will result in a
|
---|
97 | * memory leak. So until that is fixed, we need to call both close AND destroy.
|
---|
98 | *
|
---|
99 | * PR to fix memory leak: https://github.com/nodejs/node/pull/23734
|
---|
100 | *
|
---|
101 | * In Node.js 6+8, it's important that destroy is called before close as the
|
---|
102 | * stream would otherwise emit the error 'zlib binding closed'.
|
---|
103 | *
|
---|
104 | * @param {object} stream
|
---|
105 | * @private
|
---|
106 | */
|
---|
107 |
|
---|
108 | function destroyZlibStream (stream) {
|
---|
109 | if (typeof stream.destroy === 'function') {
|
---|
110 | // node.js core bug work-around
|
---|
111 | // istanbul ignore if: node.js 0.8
|
---|
112 | if (stream._binding) {
|
---|
113 | // node.js < 0.10.0
|
---|
114 | stream.destroy()
|
---|
115 | if (stream._processing) {
|
---|
116 | stream._needDrain = true
|
---|
117 | stream.once('drain', onDrainClearBinding)
|
---|
118 | } else {
|
---|
119 | stream._binding.clear()
|
---|
120 | }
|
---|
121 | } else if (stream._destroy && stream._destroy !== Stream.Transform.prototype._destroy) {
|
---|
122 | // node.js >= 12, ^11.1.0, ^10.15.1
|
---|
123 | stream.destroy()
|
---|
124 | } else if (stream._destroy && typeof stream.close === 'function') {
|
---|
125 | // node.js 7, 8
|
---|
126 | stream.destroyed = true
|
---|
127 | stream.close()
|
---|
128 | } else {
|
---|
129 | // fallback
|
---|
130 | // istanbul ignore next
|
---|
131 | stream.destroy()
|
---|
132 | }
|
---|
133 | } else if (typeof stream.close === 'function') {
|
---|
134 | // node.js < 8 fallback
|
---|
135 | closeZlibStream(stream)
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Determine if stream has destroy.
|
---|
141 | * @private
|
---|
142 | */
|
---|
143 |
|
---|
144 | function hasDestroy (stream) {
|
---|
145 | return stream instanceof Stream &&
|
---|
146 | typeof stream.destroy === 'function'
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Determine if val is EventEmitter.
|
---|
151 | * @private
|
---|
152 | */
|
---|
153 |
|
---|
154 | function isEventEmitter (val) {
|
---|
155 | return val instanceof EventEmitter
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Determine if stream is fs.ReadStream stream.
|
---|
160 | * @private
|
---|
161 | */
|
---|
162 |
|
---|
163 | function isFsReadStream (stream) {
|
---|
164 | return stream instanceof ReadStream
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Determine if stream is Zlib stream.
|
---|
169 | * @private
|
---|
170 | */
|
---|
171 |
|
---|
172 | function isZlibStream (stream) {
|
---|
173 | return stream instanceof Zlib.Gzip ||
|
---|
174 | stream instanceof Zlib.Gunzip ||
|
---|
175 | stream instanceof Zlib.Deflate ||
|
---|
176 | stream instanceof Zlib.DeflateRaw ||
|
---|
177 | stream instanceof Zlib.Inflate ||
|
---|
178 | stream instanceof Zlib.InflateRaw ||
|
---|
179 | stream instanceof Zlib.Unzip
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * No-op function.
|
---|
184 | * @private
|
---|
185 | */
|
---|
186 |
|
---|
187 | function noop () {}
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * On drain handler to clear binding.
|
---|
191 | * @private
|
---|
192 | */
|
---|
193 |
|
---|
194 | // istanbul ignore next: node.js 0.8
|
---|
195 | function onDrainClearBinding () {
|
---|
196 | this._binding.clear()
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * On open handler to close stream.
|
---|
201 | * @private
|
---|
202 | */
|
---|
203 |
|
---|
204 | function onOpenClose () {
|
---|
205 | if (typeof this.fd === 'number') {
|
---|
206 | // actually close down the fd
|
---|
207 | this.close()
|
---|
208 | }
|
---|
209 | }
|
---|