1 var once = require('once')
2 var eos = require('end-of-stream')
3 var fs = require('fs') // we only need fs to get the ReadStream and WriteStream prototypes
5 var noop = function () {}
6 var ancient = /^v?\.0/.test(process.version)
8 var isFn = function (fn) {
9 return typeof fn === 'function'
12 var isFS = function (stream) {
13 if (!ancient) return false // newer node version do not need to care about fs is a special way
14 if (!fs) return false // browser
15 return (stream instanceof (fs.ReadStream || noop) || stream instanceof (fs.WriteStream || noop)) && isFn(stream.close)
18 var isRequest = function (stream) {
19 return stream.setHeader && isFn(stream.abort)
22 var destroyer = function (stream, reading, writing, callback) {
23 callback = once(callback)
26 stream.on('close', function () {
30 eos(stream, {readable: reading, writable: writing}, function (err) {
31 if (err) return callback(err)
37 return function (err) {
42 if (isFS(stream)) return stream.close(noop) // use close for fs streams to avoid fd leaks
43 if (isRequest(stream)) return stream.abort() // request.destroy just do .end - .abort is what we want
45 if (isFn(stream.destroy)) return stream.destroy()
47 callback(err || new Error('stream was destroyed'))
51 var call = function (fn) {
55 var pipe = function (from, to) {
59 var pump = function () {
60 var streams = Array.prototype.slice.call(arguments)
61 var callback = isFn(streams[streams.length - 1] || noop) && streams.pop() || noop
63 if (Array.isArray(streams[0])) streams = streams[0]
64 if (streams.length < 2) throw new Error('pump requires two streams per minimum')
67 var destroys = streams.map(function (stream, i) {
68 var reading = i < streams.length - 1
70 return destroyer(stream, reading, writing, function (err) {
71 if (!error) error = err
72 if (err) destroys.forEach(call)
74 destroys.forEach(call)
79 return streams.reduce(pipe)