Browse Source

replace read-all-stream with get-stream

Closes #132
http2
Vsevolod Strukchinsky 9 years ago
parent
commit
9378d92120
  1. 42
      index.js
  2. 4
      package.json

42
index.js

@ -6,7 +6,7 @@ const querystring = require('querystring');
const objectAssign = require('object-assign'); const objectAssign = require('object-assign');
const duplexify = require('duplexify'); const duplexify = require('duplexify');
const isStream = require('is-stream'); const isStream = require('is-stream');
const readAllStream = require('read-all-stream'); const getStream = require('get-stream');
const timedOut = require('timed-out'); const timedOut = require('timed-out');
const urlParseLax = require('url-parse-lax'); const urlParseLax = require('url-parse-lax');
const lowercaseKeys = require('lowercase-keys'); const lowercaseKeys = require('lowercase-keys');
@ -87,31 +87,31 @@ function asCallback(opts, cb) {
req.end(opts.body); req.end(opts.body);
}); });
ee.on('response', res => ee.on('response', res => {
readAllStream(res, opts.encoding, (err, data) => { var stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts);
var statusCode = res.statusCode;
if (err) { stream
cb(new got.ReadError(err, opts), null, res); .then(data => {
return; var err;
} var statusCode = res.statusCode;
if (statusCode < 200 || statusCode > 299) { if (statusCode < 200 || statusCode > 299) {
err = new got.HTTPError(statusCode, opts); err = new got.HTTPError(statusCode, opts);
} }
if (opts.json && statusCode !== 204) { if (opts.json && statusCode !== 204) {
try { try {
data = parseJson(data); data = parseJson(data);
} catch (e) { } catch (e) {
e.fileName = urlLib.format(opts); e.fileName = urlLib.format(opts);
err = new got.ParseError(e, opts); err = new got.ParseError(e, opts);
}
} }
}
cb(err, data, res); cb(err, data, res);
}) })
); .catch(err => cb(new got.ReadError(err, opts), null, res));
});
ee.on('error', cb); ee.on('error', cb);
} }

4
package.json

@ -47,6 +47,7 @@
"dependencies": { "dependencies": {
"create-error-class": "^2.0.0", "create-error-class": "^2.0.0",
"duplexify": "^3.2.0", "duplexify": "^3.2.0",
"get-stream": "^1.1.0",
"is-plain-obj": "^1.0.0", "is-plain-obj": "^1.0.0",
"is-redirect": "^1.0.0", "is-redirect": "^1.0.0",
"is-stream": "^1.0.0", "is-stream": "^1.0.0",
@ -54,7 +55,6 @@
"node-status-codes": "^1.0.0", "node-status-codes": "^1.0.0",
"object-assign": "^4.0.1", "object-assign": "^4.0.1",
"parse-json": "^2.1.0", "parse-json": "^2.1.0",
"read-all-stream": "^3.0.0",
"timed-out": "^2.0.0", "timed-out": "^2.0.0",
"unzip-response": "^1.0.0", "unzip-response": "^1.0.0",
"url-parse-lax": "^1.0.0" "url-parse-lax": "^1.0.0"
@ -71,7 +71,7 @@
"xo": "*" "xo": "*"
}, },
"xo": { "xo": {
"esnext": true, "esnext": false,
"ignores": [ "ignores": [
"test/**" "test/**"
] ]

Loading…
Cancel
Save