Skip to content

Commit bb37f20

Browse files
committed
feat: refactor to use Minipass streams
BREAKING CHANGE: this replaces all core streams (except for some PassThrough streams in a few tests) with Minipass streams, and updates all deps to the latest and greatest Minipass versions of things.
1 parent b758555 commit bb37f20

File tree

5 files changed

+2147
-3980
lines changed

5 files changed

+2147
-3980
lines changed

Diff for: check-response.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const config = require('./config.js')
44
const errors = require('./errors.js')
55
const LRU = require('lru-cache')
6-
6+
const {Response} = require('minipass-fetch')
77
module.exports = checkResponse
88
function checkResponse (method, res, registry, startTime, opts) {
99
opts = config(opts)
@@ -18,7 +18,7 @@ function checkResponse (method, res, registry, startTime, opts) {
1818
res.body.on('end', () => logRequest(method, res, startTime, opts))
1919
if (opts.ignoreBody) {
2020
res.body.resume()
21-
res.body = null
21+
return new Response(null, res)
2222
}
2323
return res
2424
}
@@ -41,17 +41,22 @@ const BAD_HOSTS = new LRU({ max: 50 })
4141
function checkWarnings (res, registry, opts) {
4242
if (res.headers.has('warning') && !BAD_HOSTS.has(registry)) {
4343
const warnings = {}
44-
res.headers.raw()['warning'].forEach(w => {
45-
const match = w.match(WARNING_REGEXP)
46-
if (match) {
47-
warnings[match[1]] = {
48-
code: match[1],
49-
host: match[2],
50-
message: match[3],
51-
date: new Date(match[4])
44+
// note: headers.raw() will preserve case, so we might have a
45+
// key on the object like 'WaRnInG' if that was used first
46+
for (const [key, value] of Object.entries(res.headers.raw())) {
47+
if (key.toLowerCase() !== 'warning') { continue }
48+
value.forEach(w => {
49+
const match = w.match(WARNING_REGEXP)
50+
if (match) {
51+
warnings[match[1]] = {
52+
code: match[1],
53+
host: match[2],
54+
message: match[3],
55+
date: new Date(match[4])
56+
}
5257
}
53-
}
54-
})
58+
})
59+
}
5560
BAD_HOSTS.set(registry, true)
5661
if (warnings['199']) {
5762
if (warnings['199'].message.match(/ENOTFOUND/)) {

Diff for: index.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ const checkResponse = require('./check-response.js')
66
const config = require('./config.js')
77
const getAuth = require('./auth.js')
88
const fetch = require('make-fetch-happen')
9-
const JSONStream = require('JSONStream')
9+
const JSONStream = require('minipass-json-stream')
1010
const npa = require('npm-package-arg')
11-
const {PassThrough} = require('stream')
1211
const qs = require('querystring')
1312
const url = require('url')
1413
const zlib = require('zlib')
@@ -114,13 +113,10 @@ module.exports.json.stream = fetchJSONStream
114113
function fetchJSONStream (uri, jsonPath, opts) {
115114
opts = config(opts)
116115
const parser = JSONStream.parse(jsonPath, opts.mapJson)
117-
const pt = parser.pipe(new PassThrough({objectMode: true}))
118-
parser.on('error', err => pt.emit('error', err))
119-
regFetch(uri, opts).then(res => {
120-
res.body.on('error', err => parser.emit('error', err))
121-
res.body.pipe(parser)
122-
}, err => pt.emit('error', err))
123-
return pt
116+
regFetch(uri, opts).then(res =>
117+
res.body.on('error', er => parser.emit('error', er)).pipe(parser)
118+
).catch(er => parser.emit('error', er))
119+
return parser
124120
}
125121

126122
module.exports.pickRegistry = pickRegistry

0 commit comments

Comments
 (0)