diff options
author | Kat Marchán <kzm@sykosomatic.org> | 2017-05-09 14:46:02 -0700 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-05-23 19:39:43 +0200 |
commit | c0d858f8bb8ba5212548da2fba6a7bc02db0462b (patch) | |
tree | 99f043ec5aec3f5150a2aed0f62597234b158140 /deps/npm/node_modules/pacote/lib/fetchers/registry/fetch.js | |
parent | 994617370e8e66f3ea9488fec32fd912e7902396 (diff) | |
download | android-node-v8-c0d858f8bb8ba5212548da2fba6a7bc02db0462b.tar.gz android-node-v8-c0d858f8bb8ba5212548da2fba6a7bc02db0462b.tar.bz2 android-node-v8-c0d858f8bb8ba5212548da2fba6a7bc02db0462b.zip |
deps: upgrade npm beta to 5.0.0-beta.56
PR-URL: https://github.com/nodejs/node/pull/12936
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'deps/npm/node_modules/pacote/lib/fetchers/registry/fetch.js')
-rw-r--r-- | deps/npm/node_modules/pacote/lib/fetchers/registry/fetch.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/deps/npm/node_modules/pacote/lib/fetchers/registry/fetch.js b/deps/npm/node_modules/pacote/lib/fetchers/registry/fetch.js new file mode 100644 index 0000000000..914053c79f --- /dev/null +++ b/deps/npm/node_modules/pacote/lib/fetchers/registry/fetch.js @@ -0,0 +1,92 @@ +'use strict' + +const Buffer = require('safe-buffer').Buffer + +const checkWarnings = require('./check-warning-header') +const fetch = require('make-fetch-happen') +const registryKey = require('./registry-key') +const url = require('url') + +module.exports = regFetch +function regFetch (uri, registry, opts) { + const startTime = Date.now() + return fetch(uri, { + agent: opts.agent, + algorithms: opts.algorithms, + cache: getCacheMode(opts), + cacheManager: opts.cache, + headers: getHeaders(uri, registry, opts), + integrity: opts.integrity, + memoize: opts.memoize, + noProxy: opts.noProxy, + proxy: opts.proxy, + referer: opts.refer, + retry: opts.retry, + timeout: opts.timeout + }).then(res => { + if (res.headers.has('npm-notice') && !res.headers.has('x-local-cache')) { + opts.log.warn('notice', res.headers.get('npm-notice')) + } + checkWarnings(res, registry, opts) + if (res.status >= 400) { + const err = new Error(`${res.status} ${res.statusText}: ${ + opts.spec ? opts.spec : uri + }`) + err.code = `E${res.status}` + err.uri = uri + err.response = res + err.spec = opts.spec + logRequest(uri, res, startTime, opts) + throw err + } else { + res.body.on('end', () => logRequest(uri, res, startTime, opts)) + return res + } + }) +} + +function logRequest (uri, res, startTime, opts) { + const elapsedTime = Date.now() - startTime + 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.log.http( + 'fetch', + `GET ${res.status} ${uri} ${elapsedTime}ms${attemptStr}${cacheStr}` + ) +} + +function getCacheMode (opts) { + return opts.offline + ? 'only-if-cached' + : opts.preferOffline + ? 'force-cache' + : opts.preferOnline + ? 'no-cache' + : 'default' +} + +function getHeaders (uri, registry, opts) { + const headers = Object.assign({ + 'npm-in-ci': opts.isFromCI, + 'npm-scope': opts.projectScope, + 'user-agent': opts.userAgent, + 'referer': opts.refer + }, opts.headers) + const auth = ( + opts.auth && + // If these two are on different hosts, don't send credentials. + // This is mainly used by the tarball fetcher. + url.parse(uri).host === url.parse(registry).host && + opts.auth[registryKey(registry)] + ) + if (auth && auth.token) { + headers.authorization = `Bearer ${auth.token}` + } else if (auth && opts.alwaysAuth && auth.username && auth.password) { + const encoded = Buffer.from( + `${auth.username}:${auth.password}`, 'utf8' + ).toString('base64') + headers.authorization = `Basic ${encoded}` + } + return headers +} |