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