-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (49 loc) · 1.09 KB
/
index.js
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
57
58
59
60
exports = module.exports = function(src) {
return new Simple(src)
}
exports.Stream = Simple
function Simple(src) {
this.src = src
this.closed = false
this.ended = false
this.queue = []
this._push = null
this.init()
}
Simple.prototype.init = function() {
var self = this
this.src.on('error', function(err) {
self.push(err)
})
this.src.on('data', function(data) {
this.pause()
self.push(null, data)
})
this.src.on('end', function() {
self.ended = true
self.push()
})
this.src.on('close', function() {
self.closed = true
self.push(new Error('The stream is destroyed'))
})
this.src.pause()
}
Simple.prototype.read = function(cb) {
var call = this.queue.shift()
if (call) return cb.apply(null, call)
this._push = cb
this.src.resume()
}
Simple.prototype.abort = function(cb) {
this.src.destroy()
if (!cb) return
if (this.closed || this.ended) return cb()
this.src.on('close', cb)
}
Simple.prototype.push = function(err, data) {
var push = this._push
this._push = null
if (push) return push(err, data)
this.queue.push(arguments)
}