Skip to content

Commit

Permalink
feat(opts.ignoreBody): add a boolean to throw away response bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 22, 2018
1 parent aa62fce commit 6923702
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -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`.

##### <a name="opts-ignore-body"></a> `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.

##### <a name="opts-integrity"></a> `opts.integrity`

* Type: String | [SRI object](https://npm.im/ssri)
Expand Down
4 changes: 4 additions & 0 deletions check-response.js
Expand Up @@ -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
}
}
Expand Down
2 changes: 2 additions & 0 deletions config.js
Expand Up @@ -23,6 +23,8 @@ module.exports = figgyPudding({
'gzip': {},
'headers': {},
'https-proxy': {},
'ignore-body': {},
ignoreBody: 'ignore-body',
'integrity': {},
'is-from-ci': 'isFromCI',
'isFromCI': {
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Expand Up @@ -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')
Expand Down

0 comments on commit 6923702

Please sign in to comment.