| 1 |
'use strict' |
| 2 |
|
| 3 |
const { HttpErrorAuthOTP } = require('./errors.js') |
| 4 |
const checkResponse = require('./check-response.js') |
| 5 |
const getAuth = require('./auth.js') |
| 6 |
const fetch = require('make-fetch-happen') |
| 7 |
const JSONStream = require('./json-stream') |
| 8 |
const npa = require('npm-package-arg') |
| 9 |
const qs = require('querystring') |
| 10 |
const url = require('url') |
| 11 |
const zlib = require('minizlib') |
| 12 |
const { Minipass } = require('minipass') |
| 13 |
|
| 14 |
const defaultOpts = require('./default-opts.js') |
| 15 |
|
| 16 |
// WhatWG URL throws if it's not fully resolved |
| 17 |
const urlIsValid = u => { |
| 18 |
try { |
| 19 |
return !!new url.URL(u) |
| 20 |
} catch (_) { |
| 21 |
return false |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
module.exports = regFetch |
| 26 |
function regFetch (uri, /* istanbul ignore next */ opts_ = {}) { |
| 27 |
const opts = { |
| 28 |
...defaultOpts, |
| 29 |
...opts_, |
| 30 |
} |
| 31 |
|
| 32 |
// if we did not get a fully qualified URI, then we look at the registry |
| 33 |
// config or relevant scope to resolve it. |
| 34 |
const uriValid = urlIsValid(uri) |
| 35 |
let registry = opts.registry || defaultOpts.registry |
| 36 |
if (!uriValid) { |
| 37 |
registry = opts.registry = ( |
| 38 |
(opts.spec && pickRegistry(opts.spec, opts)) || |
| 39 |
opts.registry || |
| 40 |
registry |
| 41 |
) |
| 42 |
uri = `${ |
| 43 |
registry.trim().replace(/\/?$/g, '') |
| 44 |
}/${ |
| 45 |
uri.trim().replace(/^\//, '') |
| 46 |
}` |
| 47 |
// asserts that this is now valid |
| 48 |
new url.URL(uri) |
| 49 |
} |
| 50 |
|
| 51 |
const method = opts.method || 'GET' |
| 52 |
|
| 53 |
// through that takes into account the scope, the prefix of `uri`, etc |
| 54 |
const startTime = Date.now() |
| 55 |
const auth = getAuth(uri, opts) |
| 56 |
const headers = getHeaders(uri, auth, opts) |
| 57 |
let body = opts.body |
| 58 |
const bodyIsStream = Minipass.isStream(body) |
| 59 |
const bodyIsPromise = body && |
| 60 |
typeof body === 'object' && |
| 61 |
typeof body.then === 'function' |
| 62 |
|
| 63 |
if ( |
| 64 |
body && !bodyIsStream && !bodyIsPromise && typeof body !== 'string' && !Buffer.isBuffer(body) |
| 65 |
) { |
| 66 |
headers['content-type'] = headers['content-type'] || 'application/json' |
| 67 |
body = JSON.stringify(body) |
| 68 |
} else if (body && !headers['content-type']) { |
| 69 |
headers['content-type'] = 'application/octet-stream' |
| 70 |
} |
| 71 |
|
| 72 |
if (opts.gzip) { |
| 73 |
headers['content-encoding'] = 'gzip' |
| 74 |
if (bodyIsStream) { |
| 75 |
const gz = new zlib.Gzip() |
| 76 |
body.on('error', /* istanbul ignore next: unlikely and hard to test */ |
| 77 |
err => gz.emit('error', err)) |
| 78 |
body = body.pipe(gz) |
| 79 |
} else if (!bodyIsPromise) { |
| 80 |
body = new zlib.Gzip().end(body).concat() |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
const parsed = new url.URL(uri) |
| 85 |
|
| 86 |
if (opts.query) { |
| 87 |
const q = typeof opts.query === 'string' ? qs.parse(opts.query) |
| 88 |
: opts.query |
| 89 |
|
| 90 |
Object.keys(q).forEach(key => { |
| 91 |
if (q[key] !== undefined) { |
| 92 |
parsed.searchParams.set(key, q[key]) |
| 93 |
} |
| 94 |
}) |
| 95 |
uri = url.format(parsed) |
| 96 |
} |
| 97 |
|
| 98 |
if (parsed.searchParams.get('write') === 'true' && method === 'GET') { |
| 99 |
// do not cache, because this GET is fetching a rev that will be |
| 100 |
// used for a subsequent PUT or DELETE, so we need to conditionally |
| 101 |
// update cache. |
| 102 |
opts.offline = false |
| 103 |
opts.preferOffline = false |
| 104 |
opts.preferOnline = true |
| 105 |
} |
| 106 |
|
| 107 |
const doFetch = async fetchBody => { |
| 108 |
const p = fetch(uri, { |
| 109 |
agent: opts.agent, |
| 110 |
algorithms: opts.algorithms, |
| 111 |
body: fetchBody, |
| 112 |
cache: getCacheMode(opts), |
| 113 |
cachePath: opts.cache, |
| 114 |
ca: opts.ca, |
| 115 |
cert: auth.cert || opts.cert, |
| 116 |
headers, |
| 117 |
integrity: opts.integrity, |
| 118 |
key: auth.key || opts.key, |
| 119 |
localAddress: opts.localAddress, |
| 120 |
maxSockets: opts.maxSockets, |
| 121 |
memoize: opts.memoize, |
| 122 |
method: method, |
| 123 |
noProxy: opts.noProxy, |
| 124 |
proxy: opts.httpsProxy || opts.proxy, |
| 125 |
retry: opts.retry ? opts.retry : { |
| 126 |
retries: opts.fetchRetries, |
| 127 |
factor: opts.fetchRetryFactor, |
| 128 |
minTimeout: opts.fetchRetryMintimeout, |
| 129 |
maxTimeout: opts.fetchRetryMaxtimeout, |
| 130 |
}, |
| 131 |
strictSSL: opts.strictSSL, |
| 132 |
timeout: opts.timeout || 30 * 1000, |
| 133 |
signal: opts.signal, |
| 134 |
}).then(res => checkResponse({ |
| 135 |
method, |
| 136 |
uri, |
| 137 |
res, |
| 138 |
registry, |
| 139 |
startTime, |
| 140 |
auth, |
| 141 |
opts, |
| 142 |
})) |
| 143 |
|
| 144 |
if (typeof opts.otpPrompt === 'function') { |
| 145 |
return p.catch(async er => { |
| 146 |
if (er instanceof HttpErrorAuthOTP) { |
| 147 |
let otp |
| 148 |
// if otp fails to complete, we fail with that failure |
| 149 |
try { |
| 150 |
otp = await opts.otpPrompt() |
| 151 |
} catch (_) { |
| 152 |
// ignore this error |
| 153 |
} |
| 154 |
// if no otp provided, or otpPrompt errored, throw the original HTTP error |
| 155 |
if (!otp) { |
| 156 |
throw er |
| 157 |
} |
| 158 |
return regFetch(uri, { ...opts, otp }) |
| 159 |
} |
| 160 |
throw er |
| 161 |
}) |
| 162 |
} else { |
| 163 |
return p |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return Promise.resolve(body).then(doFetch) |
| 168 |
} |
| 169 |
|
| 170 |
module.exports.getAuth = getAuth |
| 171 |
|
| 172 |
module.exports.json = fetchJSON |
| 173 |
function fetchJSON (uri, opts) { |
| 174 |
return regFetch(uri, opts).then(res => res.json()) |
| 175 |
} |
| 176 |
|
| 177 |
module.exports.json.stream = fetchJSONStream |
| 178 |
function fetchJSONStream (uri, jsonPath, |
| 179 |
/* istanbul ignore next */ opts_ = {}) { |
| 180 |
const opts = { ...defaultOpts, ...opts_ } |
| 181 |
const parser = JSONStream.parse(jsonPath, opts.mapJSON) |
| 182 |
regFetch(uri, opts).then(res => |
| 183 |
res.body.on('error', |
| 184 |
/* istanbul ignore next: unlikely and difficult to test */ |
| 185 |
er => parser.emit('error', er)).pipe(parser) |
| 186 |
).catch(er => parser.emit('error', er)) |
| 187 |
return parser |
| 188 |
} |
| 189 |
|
| 190 |
module.exports.pickRegistry = pickRegistry |
| 191 |
function pickRegistry (spec, opts = {}) { |
| 192 |
spec = npa(spec) |
| 193 |
let registry = spec.scope && |
| 194 |
opts[spec.scope.replace(/^@?/, '@') + ':registry'] |
| 195 |
|
| 196 |
if (!registry && opts.scope) { |
| 197 |
registry = opts[opts.scope.replace(/^@?/, '@') + ':registry'] |
| 198 |
} |
| 199 |
|
| 200 |
if (!registry) { |
| 201 |
registry = opts.registry || defaultOpts.registry |
| 202 |
} |
| 203 |
|
| 204 |
return registry |
| 205 |
} |
| 206 |
|
| 207 |
function getCacheMode (opts) { |
| 208 |
return opts.offline ? 'only-if-cached' |
| 209 |
: opts.preferOffline ? 'force-cache' |
| 210 |
: opts.preferOnline ? 'no-cache' |
| 211 |
: 'default' |
| 212 |
} |
| 213 |
|
| 214 |
function getHeaders (uri, auth, opts) { |
| 215 |
const headers = Object.assign({ |
| 216 |
'user-agent': opts.userAgent, |
| 217 |
}, opts.headers || {}) |
| 218 |
|
| 219 |
if (opts.authType) { |
| 220 |
headers['npm-auth-type'] = opts.authType |
| 221 |
} |
| 222 |
|
| 223 |
if (opts.scope) { |
| 224 |
headers['npm-scope'] = opts.scope |
| 225 |
} |
| 226 |
|
| 227 |
if (opts.npmSession) { |
| 228 |
headers['npm-session'] = opts.npmSession |
| 229 |
} |
| 230 |
|
| 231 |
if (opts.npmCommand) { |
| 232 |
headers['npm-command'] = opts.npmCommand |
| 233 |
} |
| 234 |
|
| 235 |
// If a tarball is hosted on a different place than the manifest, only send |
| 236 |
// credentials on `alwaysAuth` |
| 237 |
if (auth.token) { |
| 238 |
headers.authorization = `Bearer ${auth.token}` |
| 239 |
} else if (auth.auth) { |
| 240 |
headers.authorization = `Basic ${auth.auth}` |
| 241 |
} |
| 242 |
|
| 243 |
if (opts.otp) { |
| 244 |
headers['npm-otp'] = opts.otp |
| 245 |
} |
| 246 |
|
| 247 |
return headers |
| 248 |
} |