From 015785bccf6983e35b246f8a2e4429eeaab9cf11 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Thu, 30 Oct 2014 12:25:55 +0500 Subject: [PATCH] Remove res.destroy and move read code to static method --- index.js | 57 +++++++++++++++++++++++++++++++++---------------------- readme.md | 5 ++++- test.js | 5 +++-- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 6d34162..3ca348b 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,32 @@ var zlib = require('zlib'); var PassThrough = require('stream').PassThrough; var assign = require('object-assign'); -module.exports = function (url, opts, cb) { +function read(res, encoding, cb) { + if (!cb) { + cb = encoding; + encoding = undefined; + } + + var chunks = []; + var len = 0; + + res.on('data', function (chunk) { + chunks.push(chunk); + len += chunk.length; + }); + + res.once('end', function () { + var data = Buffer.concat(chunks, len); + + if (encoding !== null) { + data = data.toString(encoding || 'utf8'); + } + + cb(null, data, res); + }); +} + +function got (url, opts, cb) { if (typeof opts === 'function') { // if `cb` has been specified but `opts` has not cb = opts; @@ -48,10 +73,8 @@ module.exports = function (url, opts, cb) { fn.get(arg, function (res) { // redirect if (res.statusCode < 400 && res.statusCode >= 300 && res.headers.location) { - res.destroy(); - if (++redirectCount > 10) { - cb(new Error('Redirected 10 times. Aborting.')); + cb(new Error('Redirected 10 times. Aborting.'), undefined, res); return; } @@ -60,10 +83,9 @@ module.exports = function (url, opts, cb) { } if (res.statusCode < 200 || res.statusCode > 299) { - res.destroy(); var err = new Error('Couldn\'t connect to ' + url + '.'); err.code = res.statusCode; - cb(err); + cb(err, undefined, res); return; } @@ -81,27 +103,16 @@ module.exports = function (url, opts, cb) { res.once('error', cb); - var chunks = []; - var len = 0; - - res.on('data', function (chunk) { - chunks.push(chunk); - len += chunk.length; - }); + read(res, encoding, cb); - res.once('end', function () { - var data = Buffer.concat(chunks, len); - - if (encoding !== null) { - data = data.toString(encoding || 'utf8'); - } - - cb(null, data, res); - }); }).once('error', cb); }; get(url, opts, cb); return proxy; -}; +} + +got.read = read; + +module.exports = got; diff --git a/readme.md b/readme.md index 51bec27..4de0d85 100644 --- a/readme.md +++ b/readme.md @@ -59,6 +59,10 @@ Encoding to be used on `setEncoding` of the response data. If null, the body is ##### callback(err, data, response) +###### err + +`Error` object with `code` property. If error happens, callback will receive `undefined` as `data` and bare [response](http://nodejs.org/api/http.html#http_http_incomingmessage) object, which can be destroyed or queried for data. + ###### data The data you requested. @@ -67,7 +71,6 @@ The data you requested. The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage). - ## Related See [sent](https://github.com/floatdrop/sent) if you need to upload something. diff --git a/test.js b/test.js index e5d51ea..d31d9ef 100644 --- a/test.js +++ b/test.js @@ -28,8 +28,9 @@ it('should do HTTPS request', function (done) { }); }); -it('should should return status code as error code when not 200', function (done) { - got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data) { +it('should should return status code as error code and response object when not 200', function (done) { + got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data, res) { + assert.ok(res.headers); assert.strictEqual(err.code, 404); done(); });