1 |
3a515b92
|
cagy
|
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
|
4 |
|
|
|
5 |
|
|
var noop = function () {}
|
6 |
|
|
var ancient = /^v?\.0/.test(process.version)
|
7 |
|
|
|
8 |
|
|
var isFn = function (fn) {
|
9 |
|
|
return typeof fn === 'function'
|
10 |
|
|
}
|
11 |
|
|
|
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)
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
var isRequest = function (stream) {
|
19 |
|
|
return stream.setHeader && isFn(stream.abort)
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
var destroyer = function (stream, reading, writing, callback) {
|
23 |
|
|
callback = once(callback)
|
24 |
|
|
|
25 |
|
|
var closed = false
|
26 |
|
|
stream.on('close', function () {
|
27 |
|
|
closed = true
|
28 |
|
|
})
|
29 |
|
|
|
30 |
|
|
eos(stream, {readable: reading, writable: writing}, function (err) {
|
31 |
|
|
if (err) return callback(err)
|
32 |
|
|
closed = true
|
33 |
|
|
callback()
|
34 |
|
|
})
|
35 |
|
|
|
36 |
|
|
var destroyed = false
|
37 |
|
|
return function (err) {
|
38 |
|
|
if (closed) return
|
39 |
|
|
if (destroyed) return
|
40 |
|
|
destroyed = true
|
41 |
|
|
|
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
|
44 |
|
|
|
45 |
|
|
if (isFn(stream.destroy)) return stream.destroy()
|
46 |
|
|
|
47 |
|
|
callback(err || new Error('stream was destroyed'))
|
48 |
|
|
}
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
var call = function (fn) {
|
52 |
|
|
fn()
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
var pipe = function (from, to) {
|
56 |
|
|
return from.pipe(to)
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
var pump = function () {
|
60 |
|
|
var streams = Array.prototype.slice.call(arguments)
|
61 |
|
|
var callback = isFn(streams[streams.length - 1] || noop) && streams.pop() || noop
|
62 |
|
|
|
63 |
|
|
if (Array.isArray(streams[0])) streams = streams[0]
|
64 |
|
|
if (streams.length < 2) throw new Error('pump requires two streams per minimum')
|
65 |
|
|
|
66 |
|
|
var error
|
67 |
|
|
var destroys = streams.map(function (stream, i) {
|
68 |
|
|
var reading = i < streams.length - 1
|
69 |
|
|
var writing = i > 0
|
70 |
|
|
return destroyer(stream, reading, writing, function (err) {
|
71 |
|
|
if (!error) error = err
|
72 |
|
|
if (err) destroys.forEach(call)
|
73 |
|
|
if (reading) return
|
74 |
|
|
destroys.forEach(call)
|
75 |
|
|
callback(error)
|
76 |
|
|
})
|
77 |
|
|
})
|
78 |
|
|
|
79 |
|
|
return streams.reduce(pipe)
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
module.exports = pump
|