From 863cdbdd085f65da2d5393f449067e671a5be264 Mon Sep 17 00:00:00 2001 From: Kat Marchán Date: Mon, 6 Jul 2015 00:00:48 -0700 Subject: deps: upgrade to npm 2.12.1 PR-URL: https://github.com/nodejs/io.js/pull/2112 Reviewed-By: Jeremiah Senkpiel --- deps/npm/lib/cache/add-remote-git.js | 36 +++++------- deps/npm/lib/cache/get-stat.js | 74 +---------------------- deps/npm/lib/outdated.js | 2 +- deps/npm/lib/utils/correct-mkdir.js | 110 +++++++++++++++++++++++++++++++++++ deps/npm/lib/utils/locker.js | 32 +++++----- deps/npm/lib/view.js | 8 ++- 6 files changed, 149 insertions(+), 113 deletions(-) create mode 100644 deps/npm/lib/utils/correct-mkdir.js (limited to 'deps/npm/lib') diff --git a/deps/npm/lib/cache/add-remote-git.js b/deps/npm/lib/cache/add-remote-git.js index dc40cb3b64..d3ecccdce9 100644 --- a/deps/npm/lib/cache/add-remote-git.js +++ b/deps/npm/lib/cache/add-remote-git.js @@ -15,7 +15,7 @@ var npa = require('npm-package-arg') var realizePackageSpecifier = require('realize-package-specifier') var addLocal = require('./add-local.js') -var getCacheStat = require('./get-stat.js') +var correctMkdir = require('../utils/correct-mkdir.js') var git = require('../utils/git.js') var npm = require('../npm.js') var rm = require('../utils/gently-rm.js') @@ -380,7 +380,7 @@ function checkoutTreeish (from, resolvedURL, resolvedTreeish, tmpdir, cb) { } function getGitDir (cb) { - getCacheStat(function (er, stats) { + correctMkdir(remotes, function (er, stats) { if (er) return cb(er) // We don't need global templates when cloning. Use an empty directory for @@ -391,11 +391,7 @@ function getGitDir (cb) { // Ensure that both the template and remotes directories have the correct // permissions. fs.chown(templates, stats.uid, stats.gid, function (er) { - if (er) return cb(er) - - fs.chown(remotes, stats.uid, stats.gid, function (er) { - cb(er, stats) - }) + cb(er, stats) }) }) }) @@ -429,25 +425,19 @@ function getResolved (uri, treeish) { var parsed = url.parse(uri) - // non-hosted SSH strings that are not URLs (git@whatever.com:foo.git) are - // no bueno - // https://github.com/npm/npm/issues/7961 - if (!parsed.protocol) return - - parsed.hash = treeish - if (!/^git[+:]/.test(parsed.protocol)) { - parsed.protocol = 'git+' + parsed.protocol + // Checks for known protocols: + // http:, https:, ssh:, and git:, with optional git+ prefix. + if (!parsed.protocol || + !parsed.protocol.match(/^(((git\+)?(https?|ssh))|git|file):$/)) { + uri = 'git+ssh://' + uri } - // node incorrectly sticks a / at the start of the path We know that the host - // won't change, so split and detect this - // https://github.com/npm/npm/issues/3224 - var spo = uri.split(parsed.host) - var spr = url.format(parsed).split(parsed.host) - if (spo[1] && spo[1].charAt(0) === ':' && spr[1] && spr[1].charAt(0) === '/') { - spr[1] = spr[1].slice(1) + if (!/^git[+:]/.test(uri)) { + uri = 'git+' + uri } - return spr.join(parsed.host) + + // Not all URIs are actually URIs, so use regex for the treeish. + return uri.replace(/(?:#.*)?$/, '#' + treeish) } // similar to chmodr except it add permissions rather than overwriting them diff --git a/deps/npm/lib/cache/get-stat.js b/deps/npm/lib/cache/get-stat.js index 98f95ad6ae..6ea797a4da 100644 --- a/deps/npm/lib/cache/get-stat.js +++ b/deps/npm/lib/cache/get-stat.js @@ -1,74 +1,6 @@ -var mkdir = require("mkdirp") - , fs = require("graceful-fs") - , log = require("npmlog") - , chownr = require("chownr") - , npm = require("../npm.js") - , inflight = require("inflight") +var npm = require('../npm.js') +var correctMkdir = require('../utils/correct-mkdir.js') -// to maintain the cache dir's permissions consistently. -var cacheStat = null module.exports = function getCacheStat (cb) { - if (cacheStat) return cb(null, cacheStat) - - fs.stat(npm.cache, function (er, st) { - if (er) return makeCacheDir(cb) - if (!st.isDirectory()) { - log.error("getCacheStat", "invalid cache dir %j", npm.cache) - return cb(er) - } - return cb(null, cacheStat = st) - }) -} - -function makeCacheDir (cb) { - cb = inflight("makeCacheDir", cb) - if (!cb) { - return log.verbose( - "getCacheStat", - "cache creation already in flight; waiting" - ) - } - log.verbose("getCacheStat", "cache creation not in flight; initializing") - - if (!process.getuid) return mkdir(npm.cache, function (er) { - log.verbose("makeCacheDir", "UID & GID are irrelevant on", process.platform) - cacheStat = { uid : 0, gid : 0 } - return cb(er, cacheStat) - }) - - var uid = +process.getuid() - , gid = +process.getgid() - - if (uid === 0) { - if (process.env.SUDO_UID) uid = +process.env.SUDO_UID - if (process.env.SUDO_GID) gid = +process.env.SUDO_GID - } - - if (uid !== 0 || !process.env.HOME) { - cacheStat = { uid : uid, gid : gid } - return mkdir(npm.cache, afterMkdir) - } - - fs.stat(process.env.HOME, function (er, st) { - if (er) { - log.error("makeCacheDir", "homeless?") - return cb(er) - } - cacheStat = st - log.silly("makeCacheDir", "cache dir uid, gid", [st.uid, st.gid]) - return mkdir(npm.cache, afterMkdir) - }) - - function afterMkdir (er, made) { - if (er || !cacheStat || isNaN(cacheStat.uid) || isNaN(cacheStat.gid)) { - return cb(er, cacheStat) - } - - if (!made) return cb(er, cacheStat) - - // ensure that the ownership is correct. - chownr(made, cacheStat.uid, cacheStat.gid, function (er) { - return cb(er, cacheStat) - }) - } + correctMkdir(npm.cache, cb) } diff --git a/deps/npm/lib/outdated.js b/deps/npm/lib/outdated.js index fa27dfc802..ab49d10969 100644 --- a/deps/npm/lib/outdated.js +++ b/deps/npm/lib/outdated.js @@ -344,7 +344,7 @@ function shouldUpdate (args, dir, dep, has, req, depth, cb, type) { function updateDeps (er, d) { if (er) { - if (parsed.type !== 'local') return cb() + if (parsed.type !== 'local') return cb(er) return updateLocalDeps() } diff --git a/deps/npm/lib/utils/correct-mkdir.js b/deps/npm/lib/utils/correct-mkdir.js new file mode 100644 index 0000000000..650c56fb17 --- /dev/null +++ b/deps/npm/lib/utils/correct-mkdir.js @@ -0,0 +1,110 @@ +var chownr = require('chownr') +var dezalgo = require('dezalgo') +var fs = require('graceful-fs') +var inflight = require('inflight') +var log = require('npmlog') +var mkdirp = require('mkdirp') + +// memoize the directories created by this step +var stats = {} +var effectiveOwner +module.exports = function correctMkdir (path, cb) { + cb = dezalgo(cb) + if (stats[path]) return cb(null, stats[path]) + + fs.stat(path, function (er, st) { + if (er) return makeDirectory(path, cb) + + if (!st.isDirectory()) { + log.error('correctMkdir', 'invalid dir %s', path) + return cb(er) + } + + var ownerStats = calculateOwner() + // there's always a chance the permissions could have been frobbed, so fix + if (st.uid !== ownerStats.uid) { + stats[path] = ownerStats + setPermissions(path, ownerStats, cb) + } else { + stats[path] = st + cb(null, stats[path]) + } + }) +} + +function calculateOwner () { + if (!effectiveOwner) { + effectiveOwner = { uid: 0, gid: 0 } + + if (process.getuid) effectiveOwner.uid = +process.getuid() + if (process.getgid) effectiveOwner.gid = +process.getgid() + + if (effectiveOwner.uid === 0) { + if (process.env.SUDO_UID) effectiveOwner.uid = +process.env.SUDO_UID + if (process.env.SUDO_GID) effectiveOwner.gid = +process.env.SUDO_GID + } + } + + return effectiveOwner +} + +function makeDirectory (path, cb) { + cb = inflight('makeDirectory:' + path, cb) + if (!cb) { + return log.verbose('makeDirectory', path, 'creation already in flight; waiting') + } else { + log.verbose('makeDirectory', path, 'creation not in flight; initializing') + } + + var owner = calculateOwner() + + if (!process.getuid) { + return mkdirp(path, function (er) { + log.verbose('makeCacheDir', 'UID & GID are irrelevant on', process.platform) + + stats[path] = owner + return cb(er, stats[path]) + }) + } + + if (owner.uid !== 0 || !process.env.HOME) { + log.silly( + 'makeDirectory', path, + 'uid:', owner.uid, + 'gid:', owner.gid + ) + stats[path] = owner + mkdirp(path, afterMkdir) + } else { + fs.stat(process.env.HOME, function (er, st) { + if (er) { + log.error('makeDirectory', 'homeless?') + return cb(er) + } + + log.silly( + 'makeDirectory', path, + 'uid:', st.uid, + 'gid:', st.gid + ) + stats[path] = st + mkdirp(path, afterMkdir) + }) + } + + function afterMkdir (er, made) { + if (er || !stats[path] || isNaN(stats[path].uid) || isNaN(stats[path].gid)) { + return cb(er, stats[path]) + } + + if (!made) return cb(er, stats[path]) + + setPermissions(made, stats[path], cb) + } +} + +function setPermissions (path, st, cb) { + chownr(path, st.uid, st.gid, function (er) { + return cb(er, st) + }) +} diff --git a/deps/npm/lib/utils/locker.js b/deps/npm/lib/utils/locker.js index 4479f241da..293d2da052 100644 --- a/deps/npm/lib/utils/locker.js +++ b/deps/npm/lib/utils/locker.js @@ -6,7 +6,7 @@ var log = require("npmlog") var mkdirp = require("mkdirp") var npm = require("../npm.js") -var getStat = require("../cache/get-stat.js") +var correctMkdir = require('../utils/correct-mkdir.js') var installLocks = {} @@ -20,25 +20,23 @@ function lockFileName (base, name) { } function lock (base, name, cb) { - getStat(function (er) { - var lockDir = resolve(npm.cache, "_locks") - mkdirp(lockDir, function () { - if (er) return cb(er) + var lockDir = resolve(npm.cache, "_locks") + correctMkdir(lockDir, function (er) { + if (er) return cb(er) - var opts = { stale: npm.config.get("cache-lock-stale") - , retries: npm.config.get("cache-lock-retries") - , wait: npm.config.get("cache-lock-wait") } - var lf = lockFileName(base, name) - lockfile.lock(lf, opts, function (er) { - if (er) log.warn("locking", lf, "failed", er) + var opts = { stale: npm.config.get("cache-lock-stale") + , retries: npm.config.get("cache-lock-retries") + , wait: npm.config.get("cache-lock-wait") } + var lf = lockFileName(base, name) + lockfile.lock(lf, opts, function (er) { + if (er) log.warn("locking", lf, "failed", er) - if (!er) { - log.verbose("lock", "using", lf, "for", resolve(base, name)) - installLocks[lf] = true - } + if (!er) { + log.verbose("lock", "using", lf, "for", resolve(base, name)) + installLocks[lf] = true + } - cb(er) - }) + cb(er) }) }) } diff --git a/deps/npm/lib/view.js b/deps/npm/lib/view.js index 47da39b642..9199d352ae 100644 --- a/deps/npm/lib/view.js +++ b/deps/npm/lib/view.js @@ -254,7 +254,13 @@ function printData (data, name, cb) { }) }) - console.log(msg) + // preserve output symmetry by adding a whitespace-only line at the end if + // there's one at the beginning + if (/^\s*\n/.test(msg)) msg += "\n" + + // print directly to stdout to not unnecessarily add blank lines + process.stdout.write(msg) + cb(null, data) } function cleanup (data) { -- cgit v1.2.3