From 41d147baaf395025025bfd4ec639c4a22a0a7b5e Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 7 Jun 2015 18:10:49 +0200 Subject: [PATCH] minor code style improvements --- index.js | 10 ++++++++-- test/test-post.js | 7 +++++-- test/test-redirects.js | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 7bd2c7c..88862a4 100644 --- a/index.js +++ b/index.js @@ -65,10 +65,12 @@ function got(url, opts, cb) { } opts.method = opts.method || 'GET'; + // returns a proxy stream to the response // if no callback has been provided if (!cb) { proxy = duplexify(); + // forward errors on the stream cb = function (err, data, response) { proxy.emit('error', err, data, response); @@ -101,7 +103,7 @@ function got(url, opts, cb) { typeof opts.passphrase !== 'undefined' || typeof opts.pfx !== 'undefined' || typeof opts.rejectUnauthorized !== 'undefined')) { - arg.agent = new (infinityAgent.https.Agent)(opts); + arg.agent = new infinityAgent.https.Agent(opts); } } @@ -117,9 +119,11 @@ function got(url, opts, cb) { if (proxy) { proxy.emit('response', res); } + // auto-redirect only for GET and HEAD methods if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) { - res.resume(); // discard response + // discard response + res.resume(); if (++redirectCount > 10) { cb(new GotError('Redirected 10 times. Aborting.'), undefined, res); @@ -161,6 +165,7 @@ function got(url, opts, cb) { return; } + // pipe the response to the proxy if in proxy mode if (proxy) { proxy.setReadable(res); @@ -221,6 +226,7 @@ function got(url, opts, cb) { } get(url, opts, cb); + return proxy; } diff --git a/test/test-post.js b/test/test-post.js index c10c04a..a0b7b62 100644 --- a/test/test-post.js +++ b/test/test-post.js @@ -30,7 +30,8 @@ test('GET can have body', function (t) { var stream = from2Array(['wow']); stream.on('end', function () { - t.ok(true); // Ensure, that stream was dumped + // ensure that stream was dumped + t.ok(true); }); got.get(s.url + '/method', {body: stream}, function (err, data, res) { @@ -83,7 +84,9 @@ test('throws on write to stream with body specified', function (t) { t.throws(function () { got(s.url, {body: 'wow'}).write('wow'); }); - setTimeout(t.end.bind(t), 10); // wait for request to end + + // wait for request to end + setTimeout(t.end.bind(t), 10); }); test('cleanup', function (t) { diff --git a/test/test-redirects.js b/test/test-redirects.js index 187d40f..5c8e27e 100644 --- a/test/test-redirects.js +++ b/test/test-redirects.js @@ -83,7 +83,7 @@ test('host+path in options are not breaking redirects', function (t) { }); test('redirect only GET and HEAD requests', function (t) { - got(s.url + '/relative', {body: 'wow'}, function (err, data) { + got(s.url + '/relative', {body: 'wow'}, function (err) { t.equal(err.message, 'POST http://localhost:6767/relative response code is 302 (Found)'); t.equal(err.code, 302); t.end();