Browse Source

Merge pull request #11 from sindresorhus/do-not-destroy-response

Remove res.destroy and move read code to static method
http2
Vsevolod Strukchinsky 10 years ago
parent
commit
8175ef086c
  1. 57
      index.js
  2. 5
      readme.md
  3. 5
      test.js

57
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;

5
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.

5
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();
});

Loading…
Cancel
Save