Comparing version
@@ -40,5 +40,9 @@ | ||
var agent = (uri.protocol === 'https:' ? Https : Http); | ||
var req = agent.request(uri); | ||
var client = (uri.protocol === 'https:' ? Https : Http); | ||
if (options.agent) { | ||
uri.agent = options.agent; | ||
} | ||
var req = client.request(uri); | ||
var shadow = null; // A copy of the streamed request payload when redirects are enabled | ||
@@ -162,3 +166,2 @@ | ||
this.buffers = []; | ||
this.length = 0; | ||
}; | ||
@@ -171,3 +174,2 @@ | ||
this.length += chunk.length; | ||
this.buffers.push(chunk); | ||
@@ -180,4 +182,3 @@ next(null, chunk); | ||
var buffer = (this.buffers.length === 0 ? new Buffer(0) : (this.buffers.length === 1 ? this.buffers[0] : Buffer.concat(this.buffers, this.length))); | ||
return buffer; | ||
return exports.toReadableStream(this.buffers); | ||
}; | ||
@@ -198,7 +199,9 @@ | ||
if (clientTimeout) { | ||
if (clientTimeout && | ||
clientTimeout > 0) { | ||
clientTimeoutId = setTimeout(function () { | ||
finish(Boom.clientTimeout()); | ||
}, clientTimeout < 0 ? 0 : clientTimeout); | ||
}, clientTimeout); | ||
} | ||
@@ -301,3 +304,3 @@ | ||
Stream.Readable.call(this); | ||
this._data = payload || ''; | ||
this._data = [].concat(payload || ''); | ||
this._encoding = encoding || 'utf8'; | ||
@@ -311,3 +314,6 @@ }; | ||
this.push(this._data, this._encoding); | ||
for (var i = 0, il = this._data.length; i < il; ++i) { | ||
this.push(this._data[i], this._encoding); | ||
} | ||
this.push(null); | ||
@@ -328,3 +334,3 @@ }; | ||
// 1: directive = 2: token 3: quoted-string | ||
// 1: directive = 2: token 3: quoted-string | ||
var regex = /(?:^|(?:\s*\,\s*))([^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)(?:\=(?:([^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)|(?:\"((?:[^"\\]|\\.)*)\")))?/g; | ||
@@ -380,2 +386,2 @@ | ||
exports.put = function (uri, options, callback) { internals.shortcut('PUT', uri, options, callback); }; | ||
exports.delete = function (uri, options, callback) { internals.shortcut('DELETE', uri, options, callback); }; | ||
exports.delete = function (uri, options, callback) { internals.shortcut('DELETE', uri, options, callback); }; |
{ | ||
"name": "nipple", | ||
"description": "HTTP Client Utilities", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"repository": "git://github.com/spumko/nipple", | ||
@@ -13,3 +13,3 @@ "main": "index", | ||
"engines": { | ||
"node": "0.10.x" | ||
"node": ">=0.10.22" | ||
}, | ||
@@ -16,0 +16,0 @@ "dependencies": { |
@@ -8,1 +8,53 @@ <a href="https://github.com/spumko"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a> | ||
## Usage | ||
### Basic | ||
```javascript | ||
var Nipple = require('nipple'); | ||
Nipple.get('https://google.com/', function (err, res, payload) { | ||
/* do stuff */ | ||
}); | ||
``` | ||
### Advanced | ||
```javascript | ||
var Nipple = require('nipple'); | ||
var Http = require('http'); | ||
var method = 'GET'; // GET, POST, PUT, DELETE | ||
var uri = 'https://google.com/'; | ||
var readableStream = Nipple.toReadableStream('foo=bar'); | ||
// all attributes are optional | ||
var options = { | ||
payload: readableStream || 'foo=bar' || new Buffer('foo=bar'), | ||
headers: { /* http headers */ }, | ||
redirects: 3, | ||
timeout: 1000, // 1 second, default: unlimited | ||
maxBytes: 1048576, // 1 MB, default: unlimited | ||
rejectUnauthorized: true || false, | ||
downstreamRes: null, | ||
agent: null // Http.Agent | ||
}; | ||
var optionalCallback = function (err, res) { | ||
// buffer the response stream | ||
Nipple.read(res, function (err, body) { | ||
/* do stuff */ | ||
}); | ||
}; | ||
Nipple.request(method, uri, options, optionalCallback); | ||
``` | ||
### Stream Dependencies | ||
```javascript | ||
var Nipple = require('nipple'); | ||
Nipple.get('http://google.com', { downstreamRes: dependentStream }, function (err, res) { | ||
expect(err).to.not.exist; | ||
res.pipe(dependentStream); | ||
}); | ||
``` |
@@ -486,5 +486,17 @@ // Load modules | ||
}); | ||
it('uses agent option', function (done) { | ||
var agent = new Http.Agent(); | ||
expect(Object.keys(agent.sockets).length).to.equal(0); | ||
Nipple.request('get', 'http://localhost/', { agent: agent }, function (err, res) { | ||
expect(Object.keys(agent.sockets).length).to.equal(1); | ||
done(); | ||
}); | ||
}) | ||
}); | ||
describe('#parse', function () { | ||
describe('#read', function () { | ||
@@ -599,2 +611,36 @@ it('handles errors with a boom response', function (done) { | ||
it('reads a multiple buffers response', function (done) { | ||
var path = Path.join(__dirname, '../images/nipple.png'); | ||
var stats = Fs.statSync(path); | ||
var file = Fs.readFileSync(path); | ||
var server = Http.createServer(function (req, res) { | ||
res.writeHead(200); | ||
res.write(file); | ||
setTimeout(function () { | ||
res.write(file); | ||
res.end(); | ||
}, 100); | ||
}); | ||
server.listen(0, function () { | ||
Nipple.request('get', 'http://localhost:' + server.address().port, {}, function (err, res) { | ||
expect(err).to.not.exist; | ||
expect(res.statusCode).to.equal(200); | ||
Nipple.read(res, function (err, body) { | ||
expect(body.length).to.equal(stats.size * 2); | ||
server.close(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('writes a file streamed via HTTP', function (done) { | ||
@@ -656,3 +702,3 @@ | ||
var header = Nipple.parseCacheControl('must-revalidate, max-age =3600'); | ||
var header = Nipple.parseCacheControl('must-revalidate, b =3600'); | ||
expect(header).to.not.exist; | ||
@@ -659,0 +705,0 @@ done(); |
85856
3.16%819
4.6%60
650%