summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/pacote/tarball.js
blob: f1f3ed93428f3eb7f1b10c6a087c2a813f04b625 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict'

const BB = require('bluebird')

const cacache = require('cacache')
const fs = require('fs')
const getStream = require('get-stream')
const mkdirp = BB.promisify(require('mkdirp'))
const npa = require('npm-package-arg')
const optCheck = require('./lib/util/opt-check')
const path = require('path')
const pipe = BB.promisify(require('mississippi').pipe)

module.exports = tarball
function tarball (spec, opts) {
  opts = optCheck(opts)
  spec = npa(spec, opts.where)
  const startTime = Date.now()
  if (opts.integrity && !opts.preferOnline) {
    opts.log.silly('tarball', 'checking if', opts.integrity, 'is already cached')
    return cacache.get.byDigest(opts.cache, opts.integrity).then(data => {
      if (data) {
        opts.log.silly('tarball', `cached content available for ${spec} (${Date.now() - startTime}ms)`)
        return data
      } else {
        return getStream.buffer(tarballByManifest(startTime, spec, opts))
      }
    })
  } else {
    opts.log.silly('tarball', `no integrity hash provided for ${spec} - fetching by manifest`)
    return getStream.buffer(tarballByManifest(startTime, spec, opts))
  }
}

module.exports.stream = tarballStream
function tarballStream (spec, opts) {
  opts = optCheck(opts)
  spec = npa(spec, opts.where)
  const startTime = Date.now()
  if (opts.integrity && !opts.preferOnline) {
    opts.log.silly('tarball', 'checking if', opts.integrity, 'is already cached')
    return cacache.get.hasContent(opts.cache, opts.integrity).then(info => {
      if (info) {
        opts.log.silly('tarball', `cached content available for ${spec} (${Date.now() - startTime}ms)`)
        return cacache.get.stream.byDigest(opts.cache, opts.integrity, opts)
      } else {
        return tarballByManifest(startTime, spec, opts)
      }
    })
  } else {
    opts.log.silly('tarball', `no integrity hash provided for ${spec} - fetching by manifest`)
    return tarballByManifest(startTime, spec, opts)
  }
}

module.exports.toFile = tarballToFile
function tarballToFile (spec, dest, opts) {
  opts = optCheck(opts)
  spec = npa(spec, opts.where)
  const startTime = Date.now()
  return mkdirp(path.dirname(dest)).then(() => {
    if (opts.integrity && !opts.preferOnline) {
      opts.log.silly('tarball', 'checking if', opts.integrity, 'is already cached')
      return cacache.get.copy.byDigest(opts.cache, opts.integrity, dest, opts)
      .then(() => {
        opts.log.silly('tarball', `cached content for ${spec} copied (${Date.now() - startTime}ms)`)
      }, err => {
        if (err.code === 'ENOENT') {
          return pipe(
            tarballByManifest(startTime, spec, opts),
            fs.createWriteStream(dest)
          )
        } else {
          throw err
        }
      })
    } else {
      opts.log.silly('tarball', `no integrity hash provided for ${spec} - fetching by manifest`)
      return pipe(
        tarballByManifest(startTime, spec, opts),
        fs.createWriteStream(dest)
      )
    }
  })
}

let fetch
function tarballByManifest (start, spec, opts) {
  if (!fetch) {
    fetch = require('./lib/fetch')
  }
  return fetch.tarball(spec, opts).on('end', () => {
    opts.log.silly('tarball', `${spec} done in ${Date.now() - start}ms`)
  })
}