| 1 |
'use strict' |
| 2 |
|
| 3 |
const errors = require('./errors.js') |
| 4 |
const { Response } = require('minipass-fetch') |
| 5 |
const defaultOpts = require('./default-opts.js') |
| 6 |
const { log } = require('proc-log') |
| 7 |
const { redact: cleanUrl } = require('@npmcli/redact') |
| 8 |
|
| 9 |
/* eslint-disable-next-line max-len */ |
| 10 |
const moreInfoUrl = 'https://github.com/npm/cli/wiki/No-auth-for-URI,-but-auth-present-for-scoped-registry' |
| 11 |
const checkResponse = |
| 12 |
async ({ method, uri, res, startTime, auth, opts }) => { |
| 13 |
opts = { ...defaultOpts, ...opts } |
| 14 |
if (res.headers.has('npm-notice') && !res.headers.has('x-local-cache')) { |
| 15 |
log.notice('', res.headers.get('npm-notice')) |
| 16 |
} |
| 17 |
|
| 18 |
if (res.status >= 400) { |
| 19 |
logRequest(method, res, startTime) |
| 20 |
if (auth && auth.scopeAuthKey && !auth.token && !auth.auth) { |
| 21 |
// we didn't have auth for THIS request, but we do have auth for |
| 22 |
// requests to the registry indicated by the spec's scope value. |
| 23 |
// Warn the user. |
| 24 |
log.warn('registry', `No auth for URI, but auth present for scoped registry. |
| 25 |
|
| 26 |
URI: ${uri} |
| 27 |
Scoped Registry Key: ${auth.scopeAuthKey} |
| 28 |
|
| 29 |
More info here: ${moreInfoUrl}`) |
| 30 |
} |
| 31 |
return checkErrors(method, res, startTime, opts) |
| 32 |
} else { |
| 33 |
res.body.on('end', () => logRequest(method, res, startTime, opts)) |
| 34 |
if (opts.ignoreBody) { |
| 35 |
res.body.resume() |
| 36 |
return new Response(null, res) |
| 37 |
} |
| 38 |
return res |
| 39 |
} |
| 40 |
} |
| 41 |
module.exports = checkResponse |
| 42 |
|
| 43 |
function logRequest (method, res, startTime) { |
| 44 |
const elapsedTime = Date.now() - startTime |
| 45 |
const attempt = res.headers.get('x-fetch-attempts') |
| 46 |
const attemptStr = attempt && attempt > 1 ? ` attempt #${attempt}` : '' |
| 47 |
const cacheStatus = res.headers.get('x-local-cache-status') |
| 48 |
const cacheStr = cacheStatus ? ` (cache ${cacheStatus})` : '' |
| 49 |
const urlStr = cleanUrl(res.url) |
| 50 |
|
| 51 |
// If make-fetch-happen reports a cache hit, then there was no fetch |
| 52 |
if (cacheStatus === 'hit') { |
| 53 |
log.http( |
| 54 |
'cache', |
| 55 |
`${urlStr} ${elapsedTime}ms${attemptStr}${cacheStr}` |
| 56 |
) |
| 57 |
} else { |
| 58 |
log.http( |
| 59 |
'fetch', |
| 60 |
`${method.toUpperCase()} ${res.status} ${urlStr} ${elapsedTime}ms${attemptStr}${cacheStr}` |
| 61 |
) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
function checkErrors (method, res, startTime, opts) { |
| 66 |
return res.buffer() |
| 67 |
.catch(() => null) |
| 68 |
.then(body => { |
| 69 |
let parsed = body |
| 70 |
try { |
| 71 |
parsed = JSON.parse(body.toString('utf8')) |
| 72 |
} catch { |
| 73 |
// ignore errors |
| 74 |
} |
| 75 |
if (res.status === 401 && res.headers.get('www-authenticate')) { |
| 76 |
const auth = res.headers.get('www-authenticate') |
| 77 |
.split(/,\s*/) |
| 78 |
.map(s => s.toLowerCase()) |
| 79 |
if (auth.indexOf('ipaddress') !== -1) { |
| 80 |
throw new errors.HttpErrorAuthIPAddress( |
| 81 |
method, res, parsed, opts.spec |
| 82 |
) |
| 83 |
} else if (auth.indexOf('otp') !== -1) { |
| 84 |
throw new errors.HttpErrorAuthOTP( |
| 85 |
method, res, parsed, opts.spec |
| 86 |
) |
| 87 |
} else { |
| 88 |
throw new errors.HttpErrorAuthUnknown( |
| 89 |
method, res, parsed, opts.spec |
| 90 |
) |
| 91 |
} |
| 92 |
} else if ( |
| 93 |
res.status === 401 && |
| 94 |
body != null && |
| 95 |
/one-time pass/.test(body.toString('utf8')) |
| 96 |
) { |
| 97 |
// Heuristic for malformed OTP responses that don't include the |
| 98 |
// www-authenticate header. |
| 99 |
throw new errors.HttpErrorAuthOTP( |
| 100 |
method, res, parsed, opts.spec |
| 101 |
) |
| 102 |
} else { |
| 103 |
throw new errors.HttpErrorGeneral( |
| 104 |
method, res, parsed, opts.spec |
| 105 |
) |
| 106 |
} |
| 107 |
}) |
| 108 |
} |