Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
feat(warn): http warning headers now logged
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 18, 2017
1 parent 380b79a commit f22ce1d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/registry/check-warning-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const LRU = require('lru-cache')

const WARNING_REGEXP = /^\s*(\d{3})\s+(\S+)\s+"(.*)"\s+"([^"]+)"/
const BAD_HOSTS = new LRU({ max: 50 })

module.exports = checkWarnings
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])
}
}
})
BAD_HOSTS.set(registry, true)
if (warnings['199'] && warnings['199'].message.match(/ENOTFOUND/)) {
opts.log.warn('registry', `Using stale data from ${registry} because the host is inaccessible -- are you offline?`)
} else if (warnings['199']) {
opts.log.warn('registry', `Unexpected warning for ${registry}: ${warnings['199'].message}`)
} else if (warnings['111']) {
// 111 Revalidation failed -- we're using stale data
opts.log.warn(
'registry',
`Using stale package data from ${registry} due to a request error during revalidation.`
)
}
}
}
2 changes: 2 additions & 0 deletions lib/registry/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const BB = require('bluebird')

const checkWarnings = require('./check-warning-header')
const fetch = require('make-fetch-happen')
const optCheck = require('../util/opt-check')
const pickManifest = require('./pick-manifest')
Expand Down Expand Up @@ -103,6 +104,7 @@ function fetchPackument (uri, registry, opts) {
if (res.headers.get('npm-notice')) {
opts.log.warn('notice', res.headers.get('npm-notice'))
}
checkWarnings(res, registry, opts)
const elapsedTime = Date.now() - startTime
const attempt = res.headers.get('x-fetch-attempts')
const attemptStr = attempt && attempt > 1 ? ` attempt #${attempt}` : ''
Expand Down
2 changes: 2 additions & 0 deletions lib/registry/tarball.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const BB = require('bluebird')

const checkWarnings = require('./check-warning-header')
const fetch = require('make-fetch-happen')
const manifest = require('./manifest')
const optCheck = require('../util/opt-check')
Expand Down Expand Up @@ -77,6 +78,7 @@ function fromManifest (manifest, spec, opts) {
if (res.headers.get('npm-notice')) {
opts.log.warn('notice', res.headers.get('npm-notice'))
}
checkWarnings(res, registry, opts)
if (res.status >= 400) {
const err = new Error(`Failed with ${res.status} while fetching ${uri}.`)
err.code = `E${res.status}`
Expand Down

0 comments on commit f22ce1d

Please sign in to comment.