/*! * destroy * Copyright(c) 2014 Jonathan Ong * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var ReadStream = require('fs').ReadStream var Stream = require('stream') var Zlib = require('zlib') /** * Module exports. * @public */ module.exports = destroy /** * Destroy a stream. * * @param {object} stream * @public */ function destroy (stream) { if (stream instanceof ReadStream) { return destroyReadStream(stream) } 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 (!(stream instanceof Stream)) { return stream } if (typeof stream.destroy === 'function') { stream.destroy() } return stream } /** * Destroy a ReadStream. * * @param {object} stream * @private */ function destroyReadStream (stream) { stream.destroy() if (typeof stream.close === 'function') { // node.js core bug work-around stream.on('open', onOpenClose) } return stream } /** * 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 stream.close() } return stream } /** * On drain handler to clear binding. * @private */ // istanbul ignore next: node.js 0.8 function onDrainClearBinding () { this._binding.clear() } /** * On open handler to close stream. * @private */ function onOpenClose () { if (typeof this.fd === 'number') { // actually close down the fd this.close() } }