1'use strict' 2 3const BB = require('bluebird') 4 5const extractionWorker = require('./worker.js') 6const figgyPudding = require('figgy-pudding') 7const npa = require('npm-package-arg') 8const WORKER_PATH = require.resolve('./worker.js') 9let workerFarm 10 11// Broken for now, cause too many issues on some systems. 12const ENABLE_WORKERS = false 13 14const ExtractOpts = figgyPudding({ 15 log: {}, 16 dirPacker: {} 17}) 18 19module.exports = { 20 startWorkers () { 21 if (ENABLE_WORKERS) { 22 if (!workerFarm) { workerFarm = require('worker-farm') } 23 this._workers = workerFarm({ 24 maxConcurrentCallsPerWorker: 20, 25 maxRetries: 1 26 }, WORKER_PATH) 27 } 28 }, 29 30 stopWorkers () { 31 if (ENABLE_WORKERS) { 32 if (!workerFarm) { workerFarm = require('worker-farm') } 33 workerFarm.end(this._workers) 34 } 35 }, 36 37 child (name, child, childPath, opts) { 38 opts = ExtractOpts(opts) 39 const spec = npa.resolve(name, child.version) 40 let childOpts = opts.concat({ 41 integrity: child.integrity, 42 resolved: child.resolved 43 }) 44 const args = [spec, childPath, childOpts] 45 return BB.fromNode((cb) => { 46 let launcher = extractionWorker 47 let msg = args 48 const spec = typeof args[0] === 'string' ? npa(args[0]) : args[0] 49 if (ENABLE_WORKERS && (spec.registry || spec.type === 'remote')) { 50 if (!workerFarm) { workerFarm = require('worker-farm') } 51 // We can't serialize these options 52 childOpts = childOpts.concat({ 53 log: null, 54 dirPacker: null 55 }) 56 // workers will run things in parallel! 57 launcher = this._workers 58 try { 59 msg = JSON.stringify(msg) 60 } catch (e) { 61 return cb(e) 62 } 63 } 64 launcher(msg, cb) 65 }) 66 } 67} 68