From 69237020f952d230e31968c27f53deee4d48ffbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Wed, 22 Aug 2018 14:52:55 -0700 Subject: [PATCH] feat(opts.ignoreBody): add a boolean to throw away response bodies --- README.md | 10 ++++++++++ check-response.js | 4 ++++ config.js | 2 ++ test/index.js | 10 ++++++++++ 4 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 693b5a1..847b9b1 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,16 @@ Additional headers for the outgoing request. This option can also be used to override headers automatically generated by `npm-registry-fetch`, such as `Content-Type`. +##### `opts.ignore-body` + +* Alias: `opts.ignoreBody` +* Type: Boolean +* Default: false + +If true, the **response body** will be thrown away and `res.body` set to `null`. +This will prevent dangling response sockets for requests where you don't usually +care what the response body is. + ##### `opts.integrity` * Type: String | [SRI object](https://npm.im/ssri) diff --git a/check-response.js b/check-response.js index 15e851f..bfde699 100644 --- a/check-response.js +++ b/check-response.js @@ -16,6 +16,10 @@ function checkResponse (method, res, registry, startTime, opts) { return checkErrors(method, res, startTime, opts) } else { res.body.on('end', () => logRequest(method, res, startTime, opts)) + if (opts.ignoreBody) { + res.body.resume() + res.body = null + } return res } } diff --git a/config.js b/config.js index e3a2c8c..5f35e06 100644 --- a/config.js +++ b/config.js @@ -23,6 +23,8 @@ module.exports = figgyPudding({ 'gzip': {}, 'headers': {}, 'https-proxy': {}, + 'ignore-body': {}, + ignoreBody: 'ignore-body', 'integrity': {}, 'is-from-ci': 'isFromCI', 'isFromCI': { diff --git a/test/index.js b/test/index.js index 67963b7..938f79b 100644 --- a/test/index.js +++ b/test/index.js @@ -237,6 +237,16 @@ test('json()', t => { .then(json => t.deepEqual(json, {hello: 'world'}, 'got json body')) }) +test('opts.ignoreBody', t => { + tnock(t, OPTS.registry) + .get('/hello') + .reply(200, {hello: 'world'}) + return fetch('/hello', OPTS.concat({ignoreBody: true})) + .then(res => { + t.equal(res.body, null, 'body omitted') + }) +}) + test('method configurable', t => { tnock(t, OPTS.registry) .delete('/hello')