summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/pacote/prefetch.js
blob: d17725993f9de0d62febf92b8ed54bd30403f141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict'

const BB = require('bluebird')

const cacache = require('cacache')
const finished = BB.promisify(require('mississippi').finished)
const optCheck = require('./lib/util/opt-check')
const npa = require('npm-package-arg')

module.exports = prefetch
function prefetch (spec, opts) {
  opts = optCheck(opts)
  spec = npa(spec, opts.where)
  opts.log.warn('prefetch', 'pacote.prefetch() is deprecated. Please use pacote.tarball() instead.')
  const startTime = Date.now()
  if (!opts.cache) {
    opts.log.info('prefetch', 'skipping prefetch: no cache provided')
    return BB.resolve({spec})
  }
  if (opts.integrity && !opts.preferOnline) {
    opts.log.silly('prefetch', 'checking if', opts.integrity, 'is already cached')
    return cacache.get.hasContent(opts.cache, opts.integrity).then(info => {
      if (info) {
        opts.log.silly('prefetch', `content already exists for ${spec} (${Date.now() - startTime}ms)`)
        return {
          spec,
          integrity: info.integrity,
          size: info.size,
          byDigest: true
        }
      } else {
        return prefetchByManifest(startTime, spec, opts)
      }
    })
  } else {
    opts.log.silly('prefetch', `no integrity hash provided for ${spec} - fetching by manifest`)
    return prefetchByManifest(startTime, spec, opts)
  }
}

let fetch
function prefetchByManifest (start, spec, opts) {
  let manifest
  let integrity
  return BB.resolve().then(() => {
    if (!fetch) {
      fetch = require('./lib/fetch')
    }
    const stream = fetch.tarball(spec, opts)
    if (!stream) { return }
    stream.on('data', function () {})
    stream.on('manifest', m => { manifest = m })
    stream.on('integrity', i => { integrity = i })
    return finished(stream)
  }).then(() => {
    opts.log.silly('prefetch', `${spec} done in ${Date.now() - start}ms`)
    return {
      manifest,
      spec,
      integrity: integrity || (manifest && manifest._integrity),
      byDigest: false
    }
  })
}