blob: 45bb987e85817664d53e6e7c01ead217ecd220b8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
var pump = require('pump')
var inherits = require('inherits')
var Duplexify = require('duplexify')
var toArray = function(args) {
if (!args.length) return []
return Array.isArray(args[0]) ? args[0] : Array.prototype.slice.call(args)
}
var define = function(opts) {
var Pumpify = function() {
var streams = toArray(arguments)
if (!(this instanceof Pumpify)) return new Pumpify(streams)
Duplexify.call(this, null, null, opts)
if (streams.length) this.setPipeline(streams)
}
inherits(Pumpify, Duplexify)
Pumpify.prototype.setPipeline = function() {
var streams = toArray(arguments)
var self = this
var ended = false
var w = streams[0]
var r = streams[streams.length-1]
r = r.readable ? r : null
w = w.writable ? w : null
var onclose = function() {
streams[0].emit('error', new Error('stream was destroyed'))
}
this.on('close', onclose)
this.on('prefinish', function() {
if (!ended) self.cork()
})
pump(streams, function(err) {
self.removeListener('close', onclose)
if (err) return self.destroy(err)
ended = true
self.uncork()
})
if (this.destroyed) return onclose()
this.setWritable(w)
this.setReadable(r)
}
return Pumpify
}
module.exports = define({destroy:false})
module.exports.obj = define({destroy:false, objectMode:true, highWaterMark:16})
module.exports.ctor = define
|