1'use strict' 2 3const BB = require('bluebird') 4 5const crypto = require('crypto') 6const figgyPudding = require('figgy-pudding') 7const log = require('npmlog') 8const npm = require('../npm.js') 9const pack = require('../pack.js') 10const path = require('path') 11 12const npmSession = npm.session = crypto.randomBytes(8).toString('hex') 13log.verbose('npm-session', npmSession) 14 15const SCOPE_REGISTRY_REGEX = /@.*:registry$/gi 16const NpmConfig = figgyPudding({}, { 17 other (key) { 18 return key.match(SCOPE_REGISTRY_REGEX) 19 } 20}) 21 22let baseConfig 23 24module.exports = mkConfig 25function mkConfig (...providers) { 26 if (!baseConfig) { 27 baseConfig = NpmConfig(npm.config, { 28 // Add some non-npm-config opts by hand. 29 cache: path.join(npm.config.get('cache'), '_cacache'), 30 // NOTE: npm has some magic logic around color distinct from the config 31 // value, so we have to override it here 32 color: !!npm.color, 33 dirPacker: pack.packGitDep, 34 hashAlgorithm: 'sha1', 35 includeDeprecated: false, 36 log, 37 'npm-session': npmSession, 38 'project-scope': npm.projectScope, 39 refer: npm.referer, 40 dmode: npm.modes.exec, 41 fmode: npm.modes.file, 42 umask: npm.modes.umask, 43 npmVersion: npm.version, 44 tmp: npm.tmp, 45 Promise: BB 46 }) 47 const ownerStats = calculateOwner() 48 if (ownerStats.uid != null || ownerStats.gid != null) { 49 baseConfig = baseConfig.concat(ownerStats) 50 } 51 } 52 let conf = baseConfig.concat(...providers) 53 // Adapt some other configs if missing 54 if (npm.config.get('prefer-online') === undefined) { 55 conf = conf.concat({ 56 'prefer-online': npm.config.get('cache-max') <= 0 57 }) 58 } 59 if (npm.config.get('prefer-online') === undefined) { 60 conf = conf.concat({ 61 'prefer-online': npm.config.get('cache-min') >= 9999 62 }) 63 } 64 return conf 65} 66 67let effectiveOwner 68function calculateOwner () { 69 if (!effectiveOwner) { 70 effectiveOwner = { uid: 0, gid: 0 } 71 72 // Pretty much only on windows 73 if (!process.getuid) { 74 return effectiveOwner 75 } 76 77 effectiveOwner.uid = +process.getuid() 78 effectiveOwner.gid = +process.getgid() 79 80 if (effectiveOwner.uid === 0) { 81 if (process.env.SUDO_UID) effectiveOwner.uid = +process.env.SUDO_UID 82 if (process.env.SUDO_GID) effectiveOwner.gid = +process.env.SUDO_GID 83 } 84 } 85 86 return effectiveOwner 87} 88