From 9378d921202e345aaedb14b2c66da0fbd79ebe21 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Fri, 20 Nov 2015 23:34:43 +0500 Subject: [PATCH] replace read-all-stream with get-stream Closes #132 --- index.js | 42 +++++++++++++++++++++--------------------- package.json | 4 ++-- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 712b9e2..e258b36 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ const querystring = require('querystring'); const objectAssign = require('object-assign'); const duplexify = require('duplexify'); const isStream = require('is-stream'); -const readAllStream = require('read-all-stream'); +const getStream = require('get-stream'); const timedOut = require('timed-out'); const urlParseLax = require('url-parse-lax'); const lowercaseKeys = require('lowercase-keys'); @@ -87,31 +87,31 @@ function asCallback(opts, cb) { req.end(opts.body); }); - ee.on('response', res => - readAllStream(res, opts.encoding, (err, data) => { - var statusCode = res.statusCode; + ee.on('response', res => { + var stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts); - if (err) { - cb(new got.ReadError(err, opts), null, res); - return; - } + stream + .then(data => { + var err; + var statusCode = res.statusCode; - if (statusCode < 200 || statusCode > 299) { - err = new got.HTTPError(statusCode, opts); - } + if (statusCode < 200 || statusCode > 299) { + err = new got.HTTPError(statusCode, opts); + } - if (opts.json && statusCode !== 204) { - try { - data = parseJson(data); - } catch (e) { - e.fileName = urlLib.format(opts); - err = new got.ParseError(e, opts); + if (opts.json && statusCode !== 204) { + try { + data = parseJson(data); + } catch (e) { + e.fileName = urlLib.format(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); } diff --git a/package.json b/package.json index 1d61b22..8888db7 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "dependencies": { "create-error-class": "^2.0.0", "duplexify": "^3.2.0", + "get-stream": "^1.1.0", "is-plain-obj": "^1.0.0", "is-redirect": "^1.0.0", "is-stream": "^1.0.0", @@ -54,7 +55,6 @@ "node-status-codes": "^1.0.0", "object-assign": "^4.0.1", "parse-json": "^2.1.0", - "read-all-stream": "^3.0.0", "timed-out": "^2.0.0", "unzip-response": "^1.0.0", "url-parse-lax": "^1.0.0" @@ -71,7 +71,7 @@ "xo": "*" }, "xo": { - "esnext": true, + "esnext": false, "ignores": [ "test/**" ]