summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/libcipm/lib/extract.js
blob: 5681d1ce8cacd112c75f9c2bdb3bb5e110d701d0 (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
'use strict'

const BB = require('bluebird')

const extractionWorker = require('./worker.js')
const figgyPudding = require('figgy-pudding')
const npa = require('npm-package-arg')
const WORKER_PATH = require.resolve('./worker.js')
let workerFarm

// Broken for now, cause too many issues on some systems.
const ENABLE_WORKERS = false

const ExtractOpts = figgyPudding({
  log: {}
})

module.exports = {
  startWorkers () {
    if (ENABLE_WORKERS) {
      if (!workerFarm) { workerFarm = require('worker-farm') }
      this._workers = workerFarm({
        maxConcurrentCallsPerWorker: 20,
        maxRetries: 1
      }, WORKER_PATH)
    }
  },

  stopWorkers () {
    if (ENABLE_WORKERS) {
      if (!workerFarm) { workerFarm = require('worker-farm') }
      workerFarm.end(this._workers)
    }
  },

  child (name, child, childPath, opts) {
    opts = ExtractOpts(opts)
    const spec = npa.resolve(name, child.version)
    let childOpts = opts.concat({
      integrity: child.integrity,
      resolved: child.resolved
    })
    const args = [spec, childPath, childOpts]
    return BB.fromNode((cb) => {
      let launcher = extractionWorker
      let msg = args
      const spec = typeof args[0] === 'string' ? npa(args[0]) : args[0]
      if (ENABLE_WORKERS && (spec.registry || spec.type === 'remote')) {
        if (!workerFarm) { workerFarm = require('worker-farm') }
        // We can't serialize these options
        childOpts = childOpts.concat({
          log: null,
          dirPacker: null
        })
        // workers will run things in parallel!
        launcher = this._workers
        try {
          msg = JSON.stringify(msg)
        } catch (e) {
          return cb(e)
        }
      }
      launcher(msg, cb)
    })
  }
}