forked from isaacs/http-duplex-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
40 lines (32 loc) · 1002 Bytes
/
client.js
File metadata and controls
40 lines (32 loc) · 1002 Bytes
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
module.exports = HTTPDuplex
var http = require('http')
, util = require('util')
, stream = require('stream')
if (process.version.match(/^\v0\.8/)) stream = require('readable-stream')
util.inherits(HTTPDuplex, stream.Duplex)
function HTTPDuplex(req, options) {
var self = this
if (! (self instanceof HTTPDuplex)) return new HTTPDuplex(req, options)
stream.Duplex.call(self, options)
self.req = http.request(req)
self.req.on('response', function (resp) {
self._resp = resp
self.emit('response', resp)
resp.on('data', function (c) {
if (!self.push(c)) self._resp.pause()
})
resp.on('end', function() {
self.push(null)
})
})
self._resp = null
}
HTTPDuplex.prototype._read = function (n) {
if (this._resp) this._resp.resume()
}
HTTPDuplex.prototype._write = function (chunk, encoding, cb) {
return this.req.write(chunk, encoding, cb)
}
HTTPDuplex.prototype.end = function (chunk, encoding, cb) {
return this.req.end(chunk, encoding, cb)
}