Skip to content

Commit

Permalink
feat: refactor to use Minipass streams
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
isaacs committed Oct 4, 2019
1 parent b758555 commit bb37f20
Show file tree
Hide file tree
Showing 5 changed files with 2,147 additions and 3,980 deletions.
29 changes: 17 additions & 12 deletions check-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const config = require('./config.js')
const errors = require('./errors.js')
const LRU = require('lru-cache')

const {Response} = require('minipass-fetch')
module.exports = checkResponse
function checkResponse (method, res, registry, startTime, opts) {
opts = config(opts)
Expand All @@ -18,7 +18,7 @@ function checkResponse (method, res, registry, startTime, opts) {
res.body.on('end', () => logRequest(method, res, startTime, opts))
if (opts.ignoreBody) {
res.body.resume()
res.body = null
return new Response(null, res)
}
return res
}
Expand All @@ -41,17 +41,22 @@ const BAD_HOSTS = new LRU({ max: 50 })
function checkWarnings (res, registry, opts) {
if (res.headers.has('warning') && !BAD_HOSTS.has(registry)) {
const warnings = {}
res.headers.raw()['warning'].forEach(w => {
const match = w.match(WARNING_REGEXP)
if (match) {
warnings[match[1]] = {
code: match[1],
host: match[2],
message: match[3],
date: new Date(match[4])
// note: headers.raw() will preserve case, so we might have a
// key on the object like 'WaRnInG' if that was used first
for (const [key, value] of Object.entries(res.headers.raw())) {
if (key.toLowerCase() !== 'warning') { continue }
value.forEach(w => {
const match = w.match(WARNING_REGEXP)
if (match) {
warnings[match[1]] = {
code: match[1],
host: match[2],
message: match[3],
date: new Date(match[4])
}
}
}
})
})
}
BAD_HOSTS.set(registry, true)
if (warnings['199']) {
if (warnings['199'].message.match(/ENOTFOUND/)) {
Expand Down
14 changes: 5 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ const checkResponse = require('./check-response.js')
const config = require('./config.js')
const getAuth = require('./auth.js')
const fetch = require('make-fetch-happen')
const JSONStream = require('JSONStream')
const JSONStream = require('minipass-json-stream')
const npa = require('npm-package-arg')
const {PassThrough} = require('stream')
const qs = require('querystring')
const url = require('url')
const zlib = require('zlib')
Expand Down Expand Up @@ -114,13 +113,10 @@ module.exports.json.stream = fetchJSONStream
function fetchJSONStream (uri, jsonPath, opts) {
opts = config(opts)
const parser = JSONStream.parse(jsonPath, opts.mapJson)
const pt = parser.pipe(new PassThrough({objectMode: true}))
parser.on('error', err => pt.emit('error', err))
regFetch(uri, opts).then(res => {
res.body.on('error', err => parser.emit('error', err))
res.body.pipe(parser)
}, err => pt.emit('error', err))
return pt
regFetch(uri, opts).then(res =>
res.body.on('error', er => parser.emit('error', er)).pipe(parser)
).catch(er => parser.emit('error', er))
return parser
}

module.exports.pickRegistry = pickRegistry
Expand Down
Loading

0 comments on commit bb37f20

Please sign in to comment.