-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathencoder.js
More file actions
65 lines (56 loc) · 1.76 KB
/
encoder.js
File metadata and controls
65 lines (56 loc) · 1.76 KB
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
61
62
63
64
65
'use strict'
var util = require('util')
var stream = require('readable-stream')
var Response = require('./lib/response')
var Request = require('./lib/request')
var Encoder = module.exports = function (opts) {
if (!(this instanceof Encoder)) return new Encoder(opts)
stream.Readable.call(this, opts)
this._messageQueue = []
}
util.inherits(Encoder, stream.Readable)
Encoder.prototype._read = function (size) {
if (this._drainedCallback) {
var cb = this._drainedCallback
this._drainedCallback = null
cb()
}
}
Encoder.prototype._push = function (chunk, encoding, cb) {
var drained = this.push(chunk, encoding)
if (drained) return cb()
this._drainedCallback = cb
}
// Build a new response. We have to take extra care if more than one
// response is active at the same time. In that case, the order in which
// the responses was created should be the same as the order of which
// their data is emitted from the encoder stream. Also, data from one
// response must not be mixed with data from another response.
Encoder.prototype.response = function () {
var res = new Response(this)
this._pushQueue(res)
return res
}
// Options:
// - method
// - uri
// - headers (optional)
// - body (optional)
Encoder.prototype.request = function (opts, cb) {
var req = new Request(this, opts)
if (cb) req.on('finish', cb)
this._pushQueue(req)
return req
}
Encoder.prototype._pushQueue = function (msg) {
this._messageQueue.push(msg)
if (this._messageQueue.length > 1) return
msg.once('finish', this._shiftQueue.bind(this))
msg._kick()
}
Encoder.prototype._shiftQueue = function () {
this._messageQueue.shift()
if (this._messageQueue.length === 0) return
this._messageQueue[0].once('finish', this._shiftQueue.bind(this))
this._messageQueue[0]._kick()
}