diff options
Diffstat (limited to 'deps/npm/node_modules/npm-registry-fetch/index.js')
-rw-r--r-- | deps/npm/node_modules/npm-registry-fetch/index.js | 149 |
1 files changed, 88 insertions, 61 deletions
diff --git a/deps/npm/node_modules/npm-registry-fetch/index.js b/deps/npm/node_modules/npm-registry-fetch/index.js index cf6e8d3bf3..4ba3c19243 100644 --- a/deps/npm/node_modules/npm-registry-fetch/index.js +++ b/deps/npm/node_modules/npm-registry-fetch/index.js @@ -6,19 +6,19 @@ const checkResponse = require('./check-response.js') const config = require('./config.js') const getAuth = require('./auth.js') const fetch = require('make-fetch-happen') +const JSONStream = require('JSONStream') const npa = require('npm-package-arg') +const {PassThrough} = require('stream') const qs = require('querystring') -const silentLog = require('./silentlog.js') const url = require('url') +const zlib = require('zlib') module.exports = regFetch function regFetch (uri, opts) { - opts = config(Object.assign({ - log: silentLog - }, 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 @@ -31,7 +31,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' @@ -41,51 +41,70 @@ function regFetch (uri, opts) { } else if (body && !headers['content-type']) { headers['content-type'] = 'application/octet-stream' } - if (opts.get('query')) { - let q = opts.get('query') + if (opts.gzip) { + headers['content-encoding'] = 'gzip' + if (bodyIsStream) { + const gz = zlib.createGzip() + body.on('error', err => gz.emit('error', err)) + body = body.pipe(gz) + } else { + body = new opts.Promise((resolve, reject) => { + zlib.gzip(body, (err, gz) => err ? reject(err) : resolve(gz)) + }) + } + } + if (opts.query) { + let q = opts.query if (typeof q === 'string') { q = qs.parse(q) } - const parsed = url.parse(uri) - parsed.search = '?' + qs.stringify( - parsed.query - ? Object.assign(qs.parse(parsed.query), q) - : q - ) - uri = url.format(parsed) + Object.keys(q).forEach(key => { + if (q[key] === undefined) { + delete q[key] + } + }) + if (Object.keys(q).length) { + const parsed = url.parse(uri) + parsed.search = '?' + qs.stringify( + parsed.query + ? Object.assign(qs.parse(parsed.query), q) + : q + ) + uri = url.format(parsed) + } } - return fetch(uri, { - agent: opts.get('agent'), - algorithms: opts.get('algorithms'), + return opts.Promise.resolve(body).then(body => fetch(uri, { + 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('noproxy'), - Promise: opts.get('Promise'), - 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 != null ? 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 + ))) } module.exports.json = fetchJSON @@ -93,35 +112,43 @@ function fetchJSON (uri, opts) { return regFetch(uri, opts).then(res => res.json()) } +module.exports.json.stream = fetchJSONStream +function fetchJSONStream (uri, jsonPath, opts) { + opts = config(opts) + const parser = JSONStream.parse(jsonPath, opts.mapJson) + const pt = parser.pipe(new PassThrough({objectMode: true})) + parser.on('error', err => pt.emit('error', err)) + regFetch(uri, opts).then(res => { + res.body.on('error', err => parser.emit('error', err)) + res.body.pipe(parser) + }, err => pt.emit('error', err)) + return pt +} + module.exports.pickRegistry = pickRegistry function pickRegistry (spec, opts) { spec = npa(spec) opts = config(opts) - if (!spec.registry) { - throw new Error(`${spec} is not a valid registry dependency spec`) - } 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' } @@ -129,18 +156,18 @@ function getCacheMode (opts) { 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 |