Skip to content

Commit

Permalink
refactor(opts): use proxy-based opt lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jul 27, 2018
1 parent b048bff commit 33bf45b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 54 deletions.
8 changes: 4 additions & 4 deletions auth.js
Expand Up @@ -27,11 +27,11 @@ function getAuth (registry, opts) {
}

function addKey (opts, obj, scope, key, objKey) {
if (opts.get(key)) {
obj[objKey || key] = opts.get(key)
if (opts[key]) {
obj[objKey || key] = opts[key]
}
if (scope && opts.get(`${scope}:${key}`)) {
obj[objKey || key] = opts.get(`${scope}:${key}`)
if (scope && opts[`${scope}:${key}`]) {
obj[objKey || key] = opts[`${scope}:${key}`]
}
}

Expand Down
10 changes: 5 additions & 5 deletions check-response.js
Expand Up @@ -8,7 +8,7 @@ module.exports = checkResponse
function checkResponse (method, res, registry, startTime, opts) {
opts = config(opts)
if (res.headers.has('npm-notice') && !res.headers.has('x-local-cache')) {
opts.get('log').notice('', res.headers.get('npm-notice'))
opts.log.notice('', res.headers.get('npm-notice'))
}
checkWarnings(res, registry, opts)
if (res.status >= 400) {
Expand All @@ -25,7 +25,7 @@ function logRequest (method, res, startTime, opts) {
const attempt = res.headers.get('x-fetch-attempts')
const attemptStr = attempt && attempt > 1 ? ` attempt #${attempt}` : ''
const cacheStr = res.headers.get('x-local-cache') ? ' (from cache)' : ''
opts.get('log').http(
opts.log.http(
'fetch',
`${method.toUpperCase()} ${res.status} ${res.url} ${elapsedTime}ms${attemptStr}${cacheStr}`
)
Expand All @@ -51,14 +51,14 @@ function checkWarnings (res, registry, opts) {
BAD_HOSTS.set(registry, true)
if (warnings['199']) {
if (warnings['199'].message.match(/ENOTFOUND/)) {
opts.get('log').warn('registry', `Using stale data from ${registry} because the host is inaccessible -- are you offline?`)
opts.log.warn('registry', `Using stale data from ${registry} because the host is inaccessible -- are you offline?`)
} else {
opts.get('log').warn('registry', `Unexpected warning for ${registry}: ${warnings['199'].message}`)
opts.log.warn('registry', `Unexpected warning for ${registry}: ${warnings['199'].message}`)
}
}
if (warnings['111']) {
// 111 Revalidation failed -- we're using stale data
opts.get('log').warn(
opts.log.warn(
'registry',
`Using stale data from ${registry} due to a request error during revalidation.`
)
Expand Down
88 changes: 43 additions & 45 deletions index.js
Expand Up @@ -15,8 +15,8 @@ module.exports = regFetch
function regFetch (uri, opts) {
opts = config(opts)
const registry = (
(opts.get('spec') && pickRegistry(opts.get('spec'), opts)) ||
opts.get('registry') ||
(opts.spec && pickRegistry(opts.spec, opts)) ||
opts.registry ||
'https://registry.npmjs.org/'
)
uri = url.parse(uri).protocol
Expand All @@ -29,7 +29,7 @@ function regFetch (uri, opts) {
// through that takes into account the scope, the prefix of `uri`, etc
const startTime = Date.now()
const headers = getHeaders(registry, uri, opts)
let body = opts.get('body')
let body = opts.body
const bodyIsStream = body &&
typeof body === 'object' &&
typeof body.pipe === 'function'
Expand All @@ -51,8 +51,8 @@ function regFetch (uri, opts) {
})
}
}
if (opts.get('query')) {
let q = opts.get('query')
if (opts.query) {
let q = opts.query
if (typeof q === 'string') {
q = qs.parse(q)
}
Expand All @@ -65,36 +65,36 @@ function regFetch (uri, opts) {
uri = url.format(parsed)
}
return opts.Promise.resolve(body).then(body => fetch(uri, {
agent: opts.get('agent'),
algorithms: opts.get('algorithms'),
agent: opts.agent,
algorithms: opts.algorithms,
body,
cache: getCacheMode(opts),
cacheManager: opts.get('cache'),
ca: opts.get('ca'),
cert: opts.get('cert'),
cacheManager: opts.cache,
ca: opts.ca,
cert: opts.cert,
headers,
integrity: opts.get('integrity'),
key: opts.get('key'),
localAddress: opts.get('local-address'),
maxSockets: opts.get('maxsockets'),
memoize: opts.get('memoize'),
method: opts.get('method') || 'GET',
noProxy: opts.get('no-proxy') || opts.get('noproxy'),
Promise: opts.get('Promise'),
proxy: opts.get('https-proxy') || opts.get('proxy'),
referer: opts.get('refer'),
retry: opts.get('retry') || {
retries: opts.get('fetch-retries'),
factor: opts.get('fetch-retry-factor'),
minTimeout: opts.get('fetch-retry-mintimeout'),
maxTimeout: opts.get('fetch-retry-maxtimeout')
integrity: opts.integrity,
key: opts.key,
localAddress: opts['local-address'],
maxSockets: opts.maxsockets,
memoize: opts.memoize,
method: opts.method || 'GET',
noProxy: opts['no-proxy'] || opts.noproxy,
Promise: opts.Promise,
proxy: opts['https-proxy'] || opts.proxy,
referer: opts.refer,
retry: opts.retry || {
retries: opts['fetch-retries'],
factor: opts['fetch-retry-factor'],
minTimeout: opts['fetch-retry-mintimeout'],
maxTimeout: opts['fetch-retry-maxtimeout']
},
strictSSL: !!opts.get('strict-ssl'),
timeout: opts.get('timeout'),
uid: opts.get('uid'),
gid: opts.get('gid')
strictSSL: !!opts['strict-ssl'],
timeout: opts.timeout,
uid: opts.uid,
gid: opts.gid
}).then(res => checkResponse(
opts.get('method') || 'GET', res, registry, startTime, opts
opts.method || 'GET', res, registry, startTime, opts
)))
}

Expand All @@ -108,46 +108,44 @@ function pickRegistry (spec, opts) {
spec = npa(spec)
opts = config(opts)
let registry = spec.scope &&
opts.get(spec.scope.replace(/^@?/, '@') + ':registry')
opts[spec.scope.replace(/^@?/, '@') + ':registry']

if (!registry && opts.get('scope')) {
registry = opts.get(
opts.get('scope').replace(/^@?/, '@') + ':registry'
)
if (!registry && opts.scope) {
registry = opts[opts.scope.replace(/^@?/, '@') + ':registry']
}

if (!registry) {
registry = opts.get('registry') || 'https://registry.npmjs.org/'
registry = opts.registry || 'https://registry.npmjs.org/'
}

return registry
}

function getCacheMode (opts) {
return opts.get('offline')
return opts.offline
? 'only-if-cached'
: opts.get('prefer-offline')
: opts['prefer-offline']
? 'force-cache'
: opts.get('prefer-online')
: opts['prefer-online']
? 'no-cache'
: 'default'
}

function getHeaders (registry, uri, opts) {
const headers = Object.assign({
'npm-in-ci': !!(
opts.get('is-from-ci') ||
opts['is-from-ci'] ||
process.env['CI'] === 'true' ||
process.env['TDDIUM'] ||
process.env['JENKINS_URL'] ||
process.env['bamboo.buildKey'] ||
process.env['GO_PIPELINE_NAME']
),
'npm-scope': opts.get('project-scope'),
'npm-session': opts.get('npm-session'),
'user-agent': opts.get('user-agent'),
'referer': opts.get('refer')
}, opts.get('headers'))
'npm-scope': opts['project-scope'],
'npm-session': opts['npm-session'],
'user-agent': opts['user-agent'],
'referer': opts.refer
}, opts.headers)

const auth = getAuth(registry, opts)
// If a tarball is hosted on a different place than the manifest, only send
Expand Down

0 comments on commit 33bf45b

Please sign in to comment.