-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
http_duplex.js
55 lines (42 loc) · 1.18 KB
/
http_duplex.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
module.exports = HttpDuplex;
var util = require('util'),
stream = require('readable-stream');
util.inherits(HttpDuplex, stream.Duplex);
function HttpDuplex(req, res, options) {
var self = this;
if (!(self instanceof HttpDuplex)) return new HttpDuplex(req, res, options);
stream.Duplex.call(self, options);
self._output = null;
self.connect(req, res);
}
HttpDuplex.prototype.connect = function(req, res) {
var self = this;
self.req = req;
self._output = res;
self.emit('response', res);
res.on('data', function(c) {
if (!self.push(c)) self._output.pause();
});
res.on('end', function() {
self.push(null);
});
};
HttpDuplex.prototype._read = function(n) {
if (this._output) this._output.resume();
};
HttpDuplex.prototype._write = function(chunk, encoding, cb) {
this.req.write(chunk, encoding);
cb();
};
HttpDuplex.prototype.end = function(chunk, encoding, cb) {
this._output.socket.destroySoon();
return this.req.end(chunk, encoding, cb);
};
HttpDuplex.prototype.destroy = function() {
this.req.destroy();
this._output.socket.destroy();
};
HttpDuplex.prototype.destroySoon = function() {
this.req.destroy();
this._output.socket.destroy();
};