-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.js
59 lines (54 loc) · 1.71 KB
/
reader.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
var Deserializer = require('./deserializer')
var cadence = require('cadence')
var Staccato = require('staccato')
var coalesce = require('extant')
function Reader (inbox, stream, sip) {
this.truncated = false
this.error = null
this.readable = new Staccato.Readable(stream)
this._sip = coalesce(sip, Buffer.alloc(0))
this.inbox = inbox.shifter()
this._inbox = inbox
this.state = 'created'
}
Reader.prototype.destroy = function () {
this.readable.destroy()
this.destroyed = true
}
Reader.prototype.read = cadence(function (async) {
var deserializer = new Deserializer
async(function () {
var envelopes = []
deserializer.parse(this._sip, envelopes)
async.loop([], function () {
async(function () {
async.forEach([ envelopes ], function (envelope) {
this.state = 'enqueuing'
this._inbox.enqueue(envelope, async())
})
}, function () {
envelopes.length = 0
async(function () {
this.state = 'reading'
this.readable.read(async())
}, function (buffer) {
if (buffer == null) {
this.error = this.readable.error
return [ async.break ]
}
deserializer.parse(buffer, envelopes)
})
})
})
}, function () {
this.truncated = !deserializer.atBoundry
this._inbox.push(null)
this.destroy()
this.state = 'compeleted'
return []
})
})
Reader.prototype.raise = function () {
this.readable.raise()
}
module.exports = Reader