Skip to content

Commit 6923702

Browse files
committed
feat(opts.ignoreBody): add a boolean to throw away response bodies
1 parent aa62fce commit 6923702

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ Additional headers for the outgoing request. This option can also be used to
232232
override headers automatically generated by `npm-registry-fetch`, such as
233233
`Content-Type`.
234234

235+
##### <a name="opts-ignore-body"></a> `opts.ignore-body`
236+
237+
* Alias: `opts.ignoreBody`
238+
* Type: Boolean
239+
* Default: false
240+
241+
If true, the **response body** will be thrown away and `res.body` set to `null`.
242+
This will prevent dangling response sockets for requests where you don't usually
243+
care what the response body is.
244+
235245
##### <a name="opts-integrity"></a> `opts.integrity`
236246

237247
* Type: String | [SRI object](https://npm.im/ssri)

check-response.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function checkResponse (method, res, registry, startTime, opts) {
1616
return checkErrors(method, res, startTime, opts)
1717
} else {
1818
res.body.on('end', () => logRequest(method, res, startTime, opts))
19+
if (opts.ignoreBody) {
20+
res.body.resume()
21+
res.body = null
22+
}
1923
return res
2024
}
2125
}

config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module.exports = figgyPudding({
2323
'gzip': {},
2424
'headers': {},
2525
'https-proxy': {},
26+
'ignore-body': {},
27+
ignoreBody: 'ignore-body',
2628
'integrity': {},
2729
'is-from-ci': 'isFromCI',
2830
'isFromCI': {

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ test('json()', t => {
237237
.then(json => t.deepEqual(json, {hello: 'world'}, 'got json body'))
238238
})
239239

240+
test('opts.ignoreBody', t => {
241+
tnock(t, OPTS.registry)
242+
.get('/hello')
243+
.reply(200, {hello: 'world'})
244+
return fetch('/hello', OPTS.concat({ignoreBody: true}))
245+
.then(res => {
246+
t.equal(res.body, null, 'body omitted')
247+
})
248+
})
249+
240250
test('method configurable', t => {
241251
tnock(t, OPTS.registry)
242252
.delete('/hello')

0 commit comments

Comments
 (0)