diff --git a/lib/registry/check-warning-header.js b/lib/registry/check-warning-header.js new file mode 100644 index 0000000..bd4dbc5 --- /dev/null +++ b/lib/registry/check-warning-header.js @@ -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.` + ) + } + } +} diff --git a/lib/registry/manifest.js b/lib/registry/manifest.js index ccccae9..4feb66a 100644 --- a/lib/registry/manifest.js +++ b/lib/registry/manifest.js @@ -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') @@ -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}` : '' diff --git a/lib/registry/tarball.js b/lib/registry/tarball.js index b9e67fa..652b3a9 100644 --- a/lib/registry/tarball.js +++ b/lib/registry/tarball.js @@ -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') @@ -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}`