1'use strict' 2 3const BB = require('bluebird') 4 5const cacache = require('cacache') 6const finished = BB.promisify(require('mississippi').finished) 7const optCheck = require('./lib/util/opt-check') 8const npa = require('npm-package-arg') 9 10module.exports = prefetch 11function prefetch (spec, opts) { 12 opts = optCheck(opts) 13 spec = npa(spec, opts.where) 14 opts.log.warn('prefetch', 'pacote.prefetch() is deprecated. Please use pacote.tarball() instead.') 15 const startTime = Date.now() 16 if (!opts.cache) { 17 opts.log.info('prefetch', 'skipping prefetch: no cache provided') 18 return BB.resolve({ spec }) 19 } 20 if (opts.integrity && !opts.preferOnline) { 21 opts.log.silly('prefetch', 'checking if', opts.integrity, 'is already cached') 22 return cacache.get.hasContent(opts.cache, opts.integrity).then(info => { 23 if (info) { 24 opts.log.silly('prefetch', `content already exists for ${spec} (${Date.now() - startTime}ms)`) 25 return { 26 spec, 27 integrity: info.integrity, 28 size: info.size, 29 byDigest: true 30 } 31 } else { 32 return prefetchByManifest(startTime, spec, opts) 33 } 34 }) 35 } else { 36 opts.log.silly('prefetch', `no integrity hash provided for ${spec} - fetching by manifest`) 37 return prefetchByManifest(startTime, spec, opts) 38 } 39} 40 41let fetch 42function prefetchByManifest (start, spec, opts) { 43 let manifest 44 let integrity 45 return BB.resolve().then(() => { 46 if (!fetch) { 47 fetch = require('./lib/fetch') 48 } 49 const stream = fetch.tarball(spec, opts) 50 if (!stream) { return } 51 stream.on('data', function () {}) 52 stream.on('manifest', m => { manifest = m }) 53 stream.on('integrity', i => { integrity = i }) 54 return finished(stream) 55 }).then(() => { 56 opts.log.silly('prefetch', `${spec} done in ${Date.now() - start}ms`) 57 return { 58 manifest, 59 spec, 60 integrity: integrity || (manifest && manifest._integrity), 61 byDigest: false 62 } 63 }) 64} 65