Browse Source

Merge pull request #12 from sindresorhus/read-response-on-error

Read response stream even if status is not 200
http2
Vsevolod Strukchinsky 10 years ago
parent
commit
6c7ddf80d7
  1. 1
      .gitignore
  2. 62
      index.js
  3. 7
      package.json
  4. 2
      readme.md
  5. 1
      test.js

1
.gitignore

@ -1 +1,2 @@
node_modules node_modules
coverage

62
index.js

@ -5,33 +5,9 @@ var urlLib = require('url');
var zlib = require('zlib'); var zlib = require('zlib');
var PassThrough = require('stream').PassThrough; var PassThrough = require('stream').PassThrough;
var assign = require('object-assign'); var assign = require('object-assign');
var read = require('read-all-stream');
function read(res, encoding, cb) { module.exports = function (url, opts, 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 (typeof opts === 'function') {
// if `cb` has been specified but `opts` has not // if `cb` has been specified but `opts` has not
cb = opts; cb = opts;
@ -70,9 +46,12 @@ function got (url, opts, cb) {
var fn = parsedUrl.protocol === 'https:' ? https : http; var fn = parsedUrl.protocol === 'https:' ? https : http;
var arg = assign({}, parsedUrl, opts); var arg = assign({}, parsedUrl, opts);
fn.get(arg, function (res) { fn.get(arg, function (response) {
var statusCode = response.statusCode;
var res = response;
// redirect // redirect
if (res.statusCode < 400 && res.statusCode >= 300 && res.headers.location) { if (statusCode < 400 && statusCode >= 300 && res.headers.location) {
if (++redirectCount > 10) { if (++redirectCount > 10) {
cb(new Error('Redirected 10 times. Aborting.'), undefined, res); cb(new Error('Redirected 10 times. Aborting.'), undefined, res);
return; return;
@ -82,37 +61,32 @@ function got (url, opts, cb) {
return; return;
} }
if (res.statusCode < 200 || res.statusCode > 299) {
var err = new Error('Couldn\'t connect to ' + url + '.');
err.code = res.statusCode;
cb(err, undefined, res);
return;
}
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) { if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
var unzip = zlib.createUnzip(); var unzip = zlib.createUnzip();
res.pipe(unzip); res.pipe(unzip);
res = unzip; res = unzip;
} }
if (statusCode < 200 || statusCode > 299) {
read(res, encoding, function (error, data) {
var err = error || new Error('Couldn\'t connect to ' + url + '.');
err.code = statusCode;
cb(err, data, response);
});
return;
}
// pipe the response to the proxy if in proxy mode // pipe the response to the proxy if in proxy mode
if (proxy) { if (proxy) {
res.on('error', proxy.emit.bind(proxy, 'error')).pipe(proxy); res.on('error', proxy.emit.bind(proxy, 'error')).pipe(proxy);
return; return;
} }
res.once('error', cb); read(res, encoding, cb, response);
read(res, encoding, cb);
}).once('error', cb); }).once('error', cb);
}; };
get(url, opts, cb); get(url, opts, cb);
return proxy; return proxy;
} };
got.read = read;
module.exports = got;

7
package.json

@ -13,7 +13,8 @@
"node": ">=0.10.0" "node": ">=0.10.0"
}, },
"scripts": { "scripts": {
"test": "mocha" "test": "mocha",
"coverage": "istanbul cover _mocha -- -R spec"
}, },
"files": [ "files": [
"index.js" "index.js"
@ -31,9 +32,11 @@
"simple" "simple"
], ],
"dependencies": { "dependencies": {
"object-assign": "^1.0.0" "object-assign": "^1.0.0",
"read-all-stream": "^0.1.0"
}, },
"devDependencies": { "devDependencies": {
"istanbul": "^0.3.2",
"mocha": "*" "mocha": "*"
} }
} }

2
readme.md

@ -61,7 +61,7 @@ Encoding to be used on `setEncoding` of the response data. If null, the body is
###### err ###### 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. `Error` object with `code` property.
###### data ###### data

1
test.js

@ -32,6 +32,7 @@ it('should should return status code as error code and response object when not
got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data, res) { got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data, res) {
assert.ok(res.headers); assert.ok(res.headers);
assert.strictEqual(err.code, 404); assert.strictEqual(err.code, 404);
assert.ok(/<!DOCTYPE html>/.test(data));
done(); done();
}); });
}); });

Loading…
Cancel
Save