diff options
Diffstat (limited to 'lib/cache')
-rw-r--r-- | lib/cache/add-local-tarball.js | 110 | ||||
-rw-r--r-- | lib/cache/add-local.js | 93 | ||||
-rw-r--r-- | lib/cache/add-named.js | 153 | ||||
-rw-r--r-- | lib/cache/add-remote-git.js | 2 | ||||
-rw-r--r-- | lib/cache/add-remote-tarball.js | 71 | ||||
-rw-r--r-- | lib/cache/cached-package-root.js | 12 | ||||
-rw-r--r-- | lib/cache/caching-client.js | 142 |
7 files changed, 293 insertions, 290 deletions
diff --git a/lib/cache/add-local-tarball.js b/lib/cache/add-local-tarball.js index 7628c1102..9f252b8ca 100644 --- a/lib/cache/add-local-tarball.js +++ b/lib/cache/add-local-tarball.js @@ -1,27 +1,27 @@ -var mkdir = require("mkdirp") - , assert = require("assert") - , fs = require("graceful-fs") - , writeFileAtomic = require("write-file-atomic") - , path = require("path") - , sha = require("sha") - , npm = require("../npm.js") - , log = require("npmlog") - , tar = require("../utils/tar.js") - , pathIsInside = require("path-is-inside") - , getCacheStat = require("./get-stat.js") - , cachedPackageRoot = require("./cached-package-root.js") - , chownr = require("chownr") - , inflight = require("inflight") - , once = require("once") - , writeStream = require("fs-write-stream-atomic") - , tempFilename = require("../utils/temp-filename.js") - , rimraf = require("rimraf") +var mkdir = require('mkdirp') +var assert = require('assert') +var fs = require('graceful-fs') +var writeFileAtomic = require('write-file-atomic') +var path = require('path') +var sha = require('sha') +var npm = require('../npm.js') +var log = require('npmlog') +var tar = require('../utils/tar.js') +var pathIsInside = require('path-is-inside') +var getCacheStat = require('./get-stat.js') +var cachedPackageRoot = require('./cached-package-root.js') +var chownr = require('chownr') +var inflight = require('inflight') +var once = require('once') +var writeStream = require('fs-write-stream-atomic') +var tempFilename = require('../utils/temp-filename.js') +var rimraf = require('rimraf') module.exports = addLocalTarball function addLocalTarball (p, pkgData, shasum, cb) { - assert(typeof p === "string", "must have path") - assert(typeof cb === "function", "must have callback") + assert(typeof p === 'string', 'must have path') + assert(typeof cb === 'function', 'must have callback') if (!pkgData) pkgData = {} @@ -29,16 +29,16 @@ function addLocalTarball (p, pkgData, shasum, cb) { if (!shasum) { return sha.get(p, function (er, shasum) { if (er) return cb(er) - log.silly("addLocalTarball", "shasum (computed)", shasum) + log.silly('addLocalTarball', 'shasum (computed)', shasum) addLocalTarball(p, pkgData, shasum, cb) }) } if (pathIsInside(p, npm.cache)) { - if (path.basename(p) !== "package.tgz") { - return cb(new Error("Not a valid cache tarball name: "+p)) + if (path.basename(p) !== 'package.tgz') { + return cb(new Error('Not a valid cache tarball name: ' + p)) } - log.verbose("addLocalTarball", "adding from inside cache", p) + log.verbose('addLocalTarball', 'adding from inside cache', p) return addPlacedTarball(p, pkgData, shasum, cb) } @@ -52,8 +52,8 @@ function addLocalTarball (p, pkgData, shasum, cb) { } function addPlacedTarball (p, pkgData, shasum, cb) { - assert(pkgData, "should have package data by now") - assert(typeof cb === "function", "cb function required") + assert(pkgData, 'should have package data by now') + assert(typeof cb === 'function', 'cb function required') getCacheStat(function (er, cs) { if (er) return cb(er) @@ -62,7 +62,7 @@ function addPlacedTarball (p, pkgData, shasum, cb) { } function addPlacedTarball_ (p, pkgData, uid, gid, resolvedSum, cb) { - var folder = path.join(cachedPackageRoot(pkgData), "package") + var folder = path.join(cachedPackageRoot(pkgData), 'package') // First, make sure we have the shasum, if we don't already. if (!resolvedSum) { @@ -75,7 +75,7 @@ function addPlacedTarball_ (p, pkgData, uid, gid, resolvedSum, cb) { mkdir(folder, function (er) { if (er) return cb(er) - var pj = path.join(folder, "package.json") + var pj = path.join(folder, 'package.json') var json = JSON.stringify(pkgData, null, 2) writeFileAtomic(pj, json, function (er) { cb(er, pkgData) @@ -84,19 +84,19 @@ function addPlacedTarball_ (p, pkgData, uid, gid, resolvedSum, cb) { } function addTmpTarball (tgz, pkgData, shasum, cb) { - assert(typeof cb === "function", "must have callback function") - assert(shasum, "must have shasum by now") + assert(typeof cb === 'function', 'must have callback function') + assert(shasum, 'must have shasum by now') - cb = inflight("addTmpTarball:" + tgz, cb) - if (!cb) return log.verbose("addTmpTarball", tgz, "already in flight; not adding") - log.verbose("addTmpTarball", tgz, "not in flight; adding") + cb = inflight('addTmpTarball:' + tgz, cb) + if (!cb) return log.verbose('addTmpTarball', tgz, 'already in flight; not adding') + log.verbose('addTmpTarball', tgz, 'not in flight; adding') // we already have the package info, so just move into place if (pkgData && pkgData.name && pkgData.version) { log.verbose( - "addTmpTarball", - "already have metadata; skipping unpack for", - pkgData.name + "@" + pkgData.version + 'addTmpTarball', + 'already have metadata; skipping unpack for', + pkgData.name + '@' + pkgData.version ) return addTmpTarball_(tgz, pkgData, shasum, cb) } @@ -108,31 +108,29 @@ function addTmpTarball (tgz, pkgData, shasum, cb) { // NOTE: we might not have any clue what we think it is, for example if the // user just did `npm install ./foo.tgz` - var target = tempFilename("unpack") + var target = tempFilename('unpack') getCacheStat(function (er, cs) { if (er) return cb(er) - log.verbose("addTmpTarball", "validating metadata from", tgz) + log.verbose('addTmpTarball', 'validating metadata from', tgz) tar.unpack(tgz, target, null, null, cs.uid, cs.gid, function (unpackEr, data) { // cleanup the extracted package and move on with the metadata rimraf(target, function () { if (unpackEr) return cb(unpackEr) // check that this is what we expected. if (!data.name) { - return cb(new Error("No name provided")) - } - else if (pkgData.name && data.name !== pkgData.name) { - return cb(new Error("Invalid Package: expected " + pkgData.name + - " but found " + data.name)) + return cb(new Error('No name provided')) + } else if (pkgData.name && data.name !== pkgData.name) { + return cb(new Error('Invalid Package: expected ' + pkgData.name + + ' but found ' + data.name)) } if (!data.version) { - return cb(new Error("No version provided")) - } - else if (pkgData.version && data.version !== pkgData.version) { - return cb(new Error("Invalid Package: expected " + - pkgData.name + "@" + pkgData.version + - " but found " + data.name + "@" + data.version)) + return cb(new Error('No version provided')) + } else if (pkgData.version && data.version !== pkgData.version) { + return cb(new Error('Invalid Package: expected ' + + pkgData.name + '@' + pkgData.version + + ' but found ' + data.name + '@' + data.version)) } addTmpTarball_(tgz, data, shasum, cb) @@ -142,15 +140,15 @@ function addTmpTarball (tgz, pkgData, shasum, cb) { } function addTmpTarball_ (tgz, data, shasum, cb) { - assert(typeof cb === "function", "must have callback function") + assert(typeof cb === 'function', 'must have callback function') cb = once(cb) - assert(data.name, "should have package name by now") - assert(data.version, "should have package version by now") + assert(data.name, 'should have package name by now') + assert(data.version, 'should have package version by now') var root = cachedPackageRoot(data) - var pkg = path.resolve(root, "package") - var target = path.resolve(root, "package.tgz") + var pkg = path.resolve(root, 'package') + var target = path.resolve(root, 'package.tgz') getCacheStat(function (er, cs) { if (er) return cb(er) mkdir(pkg, function (er, created) { @@ -166,12 +164,12 @@ function addTmpTarball_ (tgz, data, shasum, cb) { var read = fs.createReadStream(tgz) var write = writeStream(target, { mode: npm.modes.file }) var fin = cs.uid && cs.gid ? chown : done - read.on("error", cb).pipe(write).on("error", cb).on("close", fin) + read.on('error', cb).pipe(write).on('error', cb).on('close', fin) }) }) - function done() { + function done () { data._shasum = data._shasum || shasum cb(null, data) } diff --git a/lib/cache/add-local.js b/lib/cache/add-local.js index dbe55ddc2..075ca5fc6 100644 --- a/lib/cache/add-local.js +++ b/lib/cache/add-local.js @@ -1,46 +1,45 @@ -var assert = require("assert") - , path = require("path") - , mkdir = require("mkdirp") - , chownr = require("chownr") - , pathIsInside = require("path-is-inside") - , readJson = require("read-package-json") - , log = require("npmlog") - , npm = require("../npm.js") - , tar = require("../utils/tar.js") - , deprCheck = require("../utils/depr-check.js") - , getCacheStat = require("./get-stat.js") - , cachedPackageRoot = require("./cached-package-root.js") - , addLocalTarball = require("./add-local-tarball.js") - , sha = require("sha") - , inflight = require("inflight") - , lifecycle = require("../utils/lifecycle.js") - , iferr = require("iferr") +var assert = require('assert') +var path = require('path') +var mkdir = require('mkdirp') +var chownr = require('chownr') +var pathIsInside = require('path-is-inside') +var readJson = require('read-package-json') +var log = require('npmlog') +var npm = require('../npm.js') +var tar = require('../utils/tar.js') +var deprCheck = require('../utils/depr-check.js') +var getCacheStat = require('./get-stat.js') +var cachedPackageRoot = require('./cached-package-root.js') +var addLocalTarball = require('./add-local-tarball.js') +var sha = require('sha') +var inflight = require('inflight') +var lifecycle = require('../utils/lifecycle.js') +var iferr = require('iferr') module.exports = addLocal function addLocal (p, pkgData, cb_) { - assert(typeof p === "object", "must have spec info") - assert(typeof cb === "function", "must have callback") + assert(typeof p === 'object', 'must have spec info') + assert(typeof cb === 'function', 'must have callback') pkgData = pkgData || {} function cb (er, data) { if (er) { - log.error("addLocal", "Could not install %s", p.spec) + log.error('addLocal', 'Could not install %s', p.spec) return cb_(er) } if (data && !data._fromHosted) { - data._from = path.relative(npm.prefix, p.spec) || "." + data._from = path.relative(npm.prefix, p.spec) || '.' var resolved = path.relative(npm.prefix, p.spec) - if (resolved) data._resolved = "file:"+resolved + if (resolved) data._resolved = 'file:' + resolved } return cb_(er, data) } - if (p.type === "directory") { + if (p.type === 'directory') { addLocalDirectory(p.spec, pkgData, null, cb) - } - else { + } else { addLocalTarball(p.spec, pkgData, null, cb) } } @@ -48,33 +47,34 @@ function addLocal (p, pkgData, cb_) { // At this point, if shasum is set, it's something that we've already // read and checked. Just stashing it in the data at this point. function addLocalDirectory (p, pkgData, shasum, cb) { - assert(pkgData, "must pass package data") - assert(typeof cb === "function", "must have callback") + assert(pkgData, 'must pass package data') + assert(typeof cb === 'function', 'must have callback') // if it's a folder, then read the package.json, // tar it to the proper place, and add the cache tar - if (pathIsInside(p, npm.cache)) return cb(new Error( - "Adding a cache directory to the cache will make the world implode.")) + if (pathIsInside(p, npm.cache)) { + return cb(new Error( + 'Adding a cache directory to the cache will make the world implode.' + )) + } - readJson(path.join(p, "package.json"), false, function (er, data) { + readJson(path.join(p, 'package.json'), false, function (er, data) { if (er) return cb(er) if (!data.name) { - return cb(new Error("No name provided in package.json")) - } - else if (pkgData.name && pkgData.name !== data.name) { + return cb(new Error('No name provided in package.json')) + } else if (pkgData.name && pkgData.name !== data.name) { return cb(new Error( - "Invalid package: expected " + pkgData.name + " but found " + data.name + 'Invalid package: expected ' + pkgData.name + ' but found ' + data.name )) } if (!data.version) { - return cb(new Error("No version provided in package.json")) - } - else if (pkgData.version && pkgData.version !== data.version) { + return cb(new Error('No version provided in package.json')) + } else if (pkgData.version && pkgData.version !== data.version) { return cb(new Error( - "Invalid package: expected " + pkgData.name + "@" + pkgData.version + - " but found " + data.name + "@" + data.version + 'Invalid package: expected ' + pkgData.name + '@' + pkgData.version + + ' but found ' + data.name + '@' + data.version )) } @@ -82,27 +82,26 @@ function addLocalDirectory (p, pkgData, shasum, cb) { // pack to {cache}/name/ver/package.tgz var root = cachedPackageRoot(data) - var tgz = path.resolve(root, "package.tgz") - var pj = path.resolve(root, "package/package.json") + var tgz = path.resolve(root, 'package.tgz') + var pj = path.resolve(root, 'package/package.json') var wrapped = inflight(tgz, next) - if (!wrapped) return log.verbose("addLocalDirectory", tgz, "already in flight; waiting") - log.verbose("addLocalDirectory", tgz, "not in flight; packing") + if (!wrapped) return log.verbose('addLocalDirectory', tgz, 'already in flight; waiting') + log.verbose('addLocalDirectory', tgz, 'not in flight; packing') getCacheStat(function (er, cs) { mkdir(path.dirname(pj), function (er, made) { if (er) return cb(er) var doPrePublish = !pathIsInside(p, npm.tmp) if (doPrePublish) { - lifecycle(data, "prepublish", p, iferr(cb, thenPack)) - } - else { + lifecycle(data, 'prepublish', p, iferr(cb, thenPack)) + } else { thenPack() } function thenPack () { tar.pack(tgz, p, data, function (er) { if (er) { - log.error("addLocalDirectory", "Could not pack", p, "to", tgz) + log.error('addLocalDirectory', 'Could not pack', p, 'to', tgz) return cb(er) } diff --git a/lib/cache/add-named.js b/lib/cache/add-named.js index b5eb2e06f..1acaf28fa 100644 --- a/lib/cache/add-named.js +++ b/lib/cache/add-named.js @@ -1,24 +1,23 @@ -var path = require("path") - , assert = require("assert") - , fs = require("graceful-fs") - , http = require("http") - , log = require("npmlog") - , semver = require("semver") - , readJson = require("read-package-json") - , url = require("url") - , npm = require("../npm.js") - , deprCheck = require("../utils/depr-check.js") - , inflight = require("inflight") - , addRemoteTarball = require("./add-remote-tarball.js") - , cachedPackageRoot = require("./cached-package-root.js") - , mapToRegistry = require("../utils/map-to-registry.js") - , pulseTillDone = require("../utils/pulse-till-done.js") - +var path = require('path') +var assert = require('assert') +var fs = require('graceful-fs') +var http = require('http') +var log = require('npmlog') +var semver = require('semver') +var readJson = require('read-package-json') +var url = require('url') +var npm = require('../npm.js') +var deprCheck = require('../utils/depr-check.js') +var inflight = require('inflight') +var addRemoteTarball = require('./add-remote-tarball.js') +var cachedPackageRoot = require('./cached-package-root.js') +var mapToRegistry = require('../utils/map-to-registry.js') +var pulseTillDone = require('../utils/pulse-till-done.js') module.exports = addNamed function getOnceFromRegistry (name, from, next, done) { - function fixName(err, data, json, resp) { + function fixName (err, data, json, resp) { // this is only necessary until npm/npm-registry-client#80 is fixed if (err && err.pkgid && err.pkgid !== name) { err.message = err.message.replace( @@ -33,21 +32,21 @@ function getOnceFromRegistry (name, from, next, done) { mapToRegistry(name, npm.config, function (er, uri, auth) { if (er) return done(er) - var key = "registry:" + uri + var key = 'registry:' + uri next = inflight(key, next) - if (!next) return log.verbose(from, key, "already in flight; waiting") - else log.verbose(from, key, "not in flight; fetching") + if (!next) return log.verbose(from, key, 'already in flight; waiting') + else log.verbose(from, key, 'not in flight; fetching') - npm.registry.get(uri, { auth : auth }, pulseTillDone("fetchRegistry", fixName)) + npm.registry.get(uri, { auth: auth }, pulseTillDone('fetchRegistry', fixName)) }) } function addNamed (name, version, data, cb_) { - assert(typeof name === "string", "must have module name") - assert(typeof cb_ === "function", "must have callback") + assert(typeof name === 'string', 'must have module name') + assert(typeof cb_ === 'function', 'must have callback') - var key = name + "@" + version - log.silly("addNamed", key) + var key = name + '@' + version + log.silly('addNamed', key) function cb (er, data) { if (data && !data._fromHosted) data._from = key @@ -67,29 +66,29 @@ function addNamed (name, version, data, cb_) { } function addNameTag (name, tag, data, cb) { - log.info("addNameTag", [name, tag]) + log.info('addNameTag', [name, tag]) var explicit = true if (!tag) { explicit = false - tag = npm.config.get("tag") + tag = npm.config.get('tag') } - getOnceFromRegistry(name, "addNameTag", next, cb) + getOnceFromRegistry(name, 'addNameTag', next, cb) function next (er, data, json, resp) { if (!er) er = errorResponse(name, resp) if (er) return cb(er) - log.silly("addNameTag", "next cb for", name, "with tag", tag) + log.silly('addNameTag', 'next cb for', name, 'with tag', tag) engineFilter(data) - if (data["dist-tags"] && data["dist-tags"][tag] - && data.versions[data["dist-tags"][tag]]) { - var ver = data["dist-tags"][tag] + if (data['dist-tags'] && data['dist-tags'][tag] && + data.versions[data['dist-tags'][tag]]) { + var ver = data['dist-tags'][tag] return addNamed(name, ver, data.versions[ver], cb) } if (!explicit && Object.keys(data.versions).length) { - return addNamed(name, "*", data, cb) + return addNamed(name, '*', data, cb) } er = installTargetsError(tag, data) @@ -99,17 +98,17 @@ function addNameTag (name, tag, data, cb) { function engineFilter (data) { var npmv = npm.version - , nodev = npm.config.get("node-version") - , strict = npm.config.get("engine-strict") + var nodev = npm.config.get('node-version') + var strict = npm.config.get('engine-strict') - if (!nodev || npm.config.get("force")) return data + if (!nodev || npm.config.get('force')) return data Object.keys(data.versions || {}).forEach(function (v) { var eng = data.versions[v].engines if (!eng) return if (!strict && !data.versions[v].engineStrict) return - if (eng.node && !semver.satisfies(nodev, eng.node, true) - || eng.npm && !semver.satisfies(npmv, eng.npm, true)) { + if (eng.node && !semver.satisfies(nodev, eng.node, true) || + eng.npm && !semver.satisfies(npmv, eng.npm, true)) { delete data.versions[v] } }) @@ -117,7 +116,7 @@ function engineFilter (data) { function addNameVersion (name, v, data, cb) { var ver = semver.valid(v, true) - if (!ver) return cb(new Error("Invalid version: "+v)) + if (!ver) return cb(new Error('Invalid version: ' + v)) var response @@ -126,7 +125,7 @@ function addNameVersion (name, v, data, cb) { return next() } - getOnceFromRegistry(name, "addNameVersion", setData, cb) + getOnceFromRegistry(name, 'addNameVersion', setData, cb) function setData (er, d, json, resp) { if (!er) { @@ -135,7 +134,7 @@ function addNameVersion (name, v, data, cb) { if (er) return cb(er) data = d && d.versions[ver] if (!data) { - er = new Error("version not found: "+name+"@"+ver) + er = new Error('version not found: ' + name + '@' + ver) er.package = name er.statusCode = 404 return cb(er) @@ -148,27 +147,30 @@ function addNameVersion (name, v, data, cb) { deprCheck(data) var dist = data.dist - if (!dist) return cb(new Error("No dist in "+data._id+" package")) + if (!dist) return cb(new Error('No dist in ' + data._id + ' package')) - if (!dist.tarball) return cb(new Error( - "No dist.tarball in " + data._id + " package")) + if (!dist.tarball) { + return cb(new Error( + 'No dist.tarball in ' + data._id + ' package' + )) + } - if ((response && response.statusCode !== 304) || npm.config.get("force")) { + if ((response && response.statusCode !== 304) || npm.config.get('force')) { return fetchit() } // we got cached data, so let's see if we have a tarball. - var pkgroot = cachedPackageRoot({name : name, version : ver}) - var pkgtgz = path.join(pkgroot, "package.tgz") - var pkgjson = path.join(pkgroot, "package", "package.json") + var pkgroot = cachedPackageRoot({ name: name, version: ver }) + var pkgtgz = path.join(pkgroot, 'package.tgz') + var pkgjson = path.join(pkgroot, 'package', 'package.json') fs.stat(pkgtgz, function (er) { if (!er) { readJson(pkgjson, function (er, data) { - if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er) + if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er) if (data) { - if (!data.name) return cb(new Error("No name provided")) - if (!data.version) return cb(new Error("No version provided")) + if (!data.name) return cb(new Error('No name provided')) + if (!data.version) return cb(new Error('No version provided')) // check the SHA of the package we have, to ensure it wasn't installed // from somewhere other than the registry (eg, a fork) @@ -200,8 +202,8 @@ function addNameVersion (name, v, data, cb) { // Only add non-shasum'ed packages if --forced. Only ancient things // would lack this for good reasons nowadays. - if (!dist.shasum && !npm.config.get("force")) { - return cb(new Error("package lacks shasum: " + data._id)) + if (!dist.shasum && !npm.config.get('force')) { + return cb(new Error('package lacks shasum: ' + data._id)) } addRemoteTarball(tb, data, dist.shasum, auth, cb) @@ -212,15 +214,17 @@ function addNameVersion (name, v, data, cb) { function addNameRange (name, range, data, cb) { range = semver.validRange(range, true) - if (range === null) return cb(new Error( - "Invalid version range: " + range - )) + if (range === null) { + return cb(new Error( + 'Invalid version range: ' + range + )) + } - log.silly("addNameRange", {name:name, range:range, hasData:!!data}) + log.silly('addNameRange', { name: name, range: range, hasData: !!data }) if (data) return next() - getOnceFromRegistry(name, "addNameRange", setData, cb) + getOnceFromRegistry(name, 'addNameRange', setData, cb) function setData (er, d, json, resp) { if (!er) { @@ -232,18 +236,20 @@ function addNameRange (name, range, data, cb) { } function next () { - log.silly( "addNameRange", "number 2" - , {name:name, range:range, hasData:!!data}) + log.silly( + 'addNameRange', + 'number 2', { name: name, range: range, hasData: !!data } + ) engineFilter(data) - log.silly("addNameRange", "versions" + log.silly('addNameRange', 'versions' , [data.name, Object.keys(data.versions || {})]) // if the tagged version satisfies, then use that. - var tagged = data["dist-tags"][npm.config.get("tag")] - if (tagged - && data.versions[tagged] - && semver.satisfies(tagged, range, true)) { + var tagged = data['dist-tags'][npm.config.get('tag')] + if (tagged && + data.versions[tagged] && + semver.satisfies(tagged, range, true)) { return addNamed(name, tagged, data.versions[tagged], cb) } @@ -261,20 +267,19 @@ function addNameRange (name, range, data, cb) { } function installTargetsError (requested, data) { - var targets = Object.keys(data["dist-tags"]).filter(function (f) { + var targets = Object.keys(data['dist-tags']).filter(function (f) { return (data.versions || {}).hasOwnProperty(f) }).concat(Object.keys(data.versions || {})) - requested = data.name + (requested ? "@'" + requested + "'" : "") + requested = data.name + (requested ? "@'" + requested + "'" : '') targets = targets.length - ? "Valid install targets:\n" + JSON.stringify(targets) + "\n" - : "No valid targets found.\n" - + "Perhaps not compatible with your version of node?" + ? 'Valid install targets:\n' + JSON.stringify(targets) + '\n' + : 'No valid targets found.\n' + + 'Perhaps not compatible with your version of node?' - var er = new Error( "No compatible version found: " - + requested + "\n" + targets) - er.code = "ETARGET" + var er = new Error('No compatible version found: ' + requested + '\n' + targets) + er.code = 'ETARGET' return er } @@ -283,7 +288,7 @@ function errorResponse (name, response) { if (response.statusCode >= 400) { er = new Error(http.STATUS_CODES[response.statusCode]) er.statusCode = response.statusCode - er.code = "E" + er.statusCode + er.code = 'E' + er.statusCode er.pkgid = name } return er diff --git a/lib/cache/add-remote-git.js b/lib/cache/add-remote-git.js index e3863c7f7..d34981b6f 100644 --- a/lib/cache/add-remote-git.js +++ b/lib/cache/add-remote-git.js @@ -118,7 +118,7 @@ function tryClone (from, combinedURL, silent, cb) { // ensure that similarly-named remotes don't collide var cachedRemote = uniqueFilename(remotes, combinedURL.replace(/[^a-zA-Z0-9]+/g, '-'), cloneURL) var repoID = path.relative(remotes, cachedRemote) - var cachedRemote = path.join(remotes, repoID) + cachedRemote = path.join(remotes, repoID) cb = inflight(repoID, cb) if (!cb) { diff --git a/lib/cache/add-remote-tarball.js b/lib/cache/add-remote-tarball.js index dd49b5f54..aaa768acb 100644 --- a/lib/cache/add-remote-tarball.js +++ b/lib/cache/add-remote-tarball.js @@ -1,22 +1,22 @@ -var mkdir = require("mkdirp") - , assert = require("assert") - , log = require("npmlog") - , path = require("path") - , sha = require("sha") - , retry = require("retry") - , createWriteStream = require("fs-write-stream-atomic") - , npm = require("../npm.js") - , inflight = require("inflight") - , addLocalTarball = require("./add-local-tarball.js") - , cacheFile = require("npm-cache-filename") - , rimraf = require("rimraf") - , pulseTillDone = require("../utils/pulse-till-done.js") +var mkdir = require('mkdirp') +var assert = require('assert') +var log = require('npmlog') +var path = require('path') +var sha = require('sha') +var retry = require('retry') +var createWriteStream = require('fs-write-stream-atomic') +var npm = require('../npm.js') +var inflight = require('inflight') +var addLocalTarball = require('./add-local-tarball.js') +var cacheFile = require('npm-cache-filename') +var rimraf = require('rimraf') +var pulseTillDone = require('../utils/pulse-till-done.js') module.exports = addRemoteTarball function addRemoteTarball (u, pkgData, shasum, auth, cb_) { - assert(typeof u === "string", "must have module URL") - assert(typeof cb_ === "function", "must have callback") + assert(typeof u === 'string', 'must have module URL') + assert(typeof cb_ === 'function', 'must have callback') function cb (er, data) { if (data) { @@ -28,8 +28,8 @@ function addRemoteTarball (u, pkgData, shasum, auth, cb_) { } cb_ = inflight(u, cb_) - if (!cb_) return log.verbose("addRemoteTarball", u, "already in flight; waiting") - log.verbose("addRemoteTarball", u, "not in flight; adding") + if (!cb_) return log.verbose('addRemoteTarball', u, 'already in flight; waiting') + log.verbose('addRemoteTarball', u, 'not in flight; adding') // XXX Fetch direct to cache location, store tarballs under // ${cache}/registry.npmjs.org/pkg/-/pkg-1.2.3.tgz @@ -46,7 +46,7 @@ function addRemoteTarball (u, pkgData, shasum, auth, cb_) { }) } - log.verbose("addRemoteTarball", [u, shasum]) + log.verbose('addRemoteTarball', [u, shasum]) mkdir(path.dirname(tmp), function (er) { if (er) return cb(er) addRemoteTarball_(u, tmp, shasum, auth, next) @@ -57,21 +57,24 @@ function addRemoteTarball_ (u, tmp, shasum, auth, cb) { // Tuned to spread 3 attempts over about a minute. // See formula at <https://github.com/tim-kos/node-retry>. var operation = retry.operation({ - retries: npm.config.get("fetch-retries") - , factor: npm.config.get("fetch-retry-factor") - , minTimeout: npm.config.get("fetch-retry-mintimeout") - , maxTimeout: npm.config.get("fetch-retry-maxtimeout") + retries: npm.config.get('fetch-retries'), + factor: npm.config.get('fetch-retry-factor'), + minTimeout: npm.config.get('fetch-retry-mintimeout'), + maxTimeout: npm.config.get('fetch-retry-maxtimeout') }) operation.attempt(function (currentAttempt) { - log.info("retry", "fetch attempt " + currentAttempt - + " at " + (new Date()).toLocaleTimeString()) + log.info( + 'retry', + 'fetch attempt', currentAttempt, + 'at', (new Date()).toLocaleTimeString() + ) fetchAndShaCheck(u, tmp, shasum, auth, function (er, response, shasum) { // Only retry on 408, 5xx or no `response`. var sc = response && response.statusCode var statusRetry = !sc || (sc === 408 || sc >= 500) if (er && statusRetry && operation.retry(er)) { - log.warn("retry", "will retry, error on last attempt: " + er) + log.warn('retry', 'will retry, error on last attempt: ' + er) return } cb(er, response, shasum) @@ -80,35 +83,35 @@ function addRemoteTarball_ (u, tmp, shasum, auth, cb) { } function fetchAndShaCheck (u, tmp, shasum, auth, cb) { - cb = pulseTillDone("fetchTarball", cb) - npm.registry.fetch(u, { auth : auth }, function (er, response) { + cb = pulseTillDone('fetchTarball', cb) + npm.registry.fetch(u, { auth: auth }, function (er, response) { if (er) { - log.error("fetch failed", u) + log.error('fetch failed', u) return cb(er, response) } - var tarball = createWriteStream(tmp, { mode : npm.modes.file }) - tarball.on("error", function (er) { + var tarball = createWriteStream(tmp, { mode: npm.modes.file }) + tarball.on('error', function (er) { cb(er) tarball.destroy() }) - tarball.on("finish", function () { + tarball.on('finish', function () { if (!shasum) { // Well, we weren't given a shasum, so at least sha what we have // in case we want to compare it to something else later return sha.get(tmp, function (er, shasum) { - log.silly("fetchAndShaCheck", "shasum", shasum) + log.silly('fetchAndShaCheck', 'shasum', shasum) cb(er, response, shasum) }) } // validate that the url we just downloaded matches the expected shasum. - log.silly("fetchAndShaCheck", "shasum", shasum) + log.silly('fetchAndShaCheck', 'shasum', shasum) sha.check(tmp, shasum, function (er) { if (er && er.message) { // add original filename for better debuggability - er.message = er.message + "\n" + "From: " + u + er.message = er.message + '\n' + 'From: ' + u } return cb(er, response, shasum) }) diff --git a/lib/cache/cached-package-root.js b/lib/cache/cached-package-root.js index 7163314a8..b47fac6c9 100644 --- a/lib/cache/cached-package-root.js +++ b/lib/cache/cached-package-root.js @@ -1,14 +1,14 @@ -var assert = require("assert") -var resolve = require("path").resolve +var assert = require('assert') +var resolve = require('path').resolve -var npm = require("../npm.js") +var npm = require('../npm.js') module.exports = getCacheRoot function getCacheRoot (data) { - assert(data, "must pass package metadata") - assert(data.name, "package metadata must include name") - assert(data.version, "package metadata must include version") + assert(data, 'must pass package metadata') + assert(data.name, 'package metadata must include name') + assert(data.version, 'package metadata must include version') return resolve(npm.cache, data.name, data.version) } diff --git a/lib/cache/caching-client.js b/lib/cache/caching-client.js index ec8eb8e16..faf748b1f 100644 --- a/lib/cache/caching-client.js +++ b/lib/cache/caching-client.js @@ -1,30 +1,30 @@ module.exports = CachingRegistryClient -var path = require("path") - , fs = require("graceful-fs") - , url = require("url") - , assert = require("assert") - , inherits = require("util").inherits - -var RegistryClient = require("npm-registry-client") - , npm = require("../npm.js") - , log = require("npmlog") - , getCacheStat = require("./get-stat.js") - , cacheFile = require("npm-cache-filename") - , mkdirp = require("mkdirp") - , rimraf = require("rimraf") - , chownr = require("chownr") - , writeFile = require("write-file-atomic") +var path = require('path') +var fs = require('graceful-fs') +var url = require('url') +var assert = require('assert') +var inherits = require('util').inherits + +var RegistryClient = require('npm-registry-client') +var npm = require('../npm.js') +var log = require('npmlog') +var getCacheStat = require('./get-stat.js') +var cacheFile = require('npm-cache-filename') +var mkdirp = require('mkdirp') +var rimraf = require('rimraf') +var chownr = require('chownr') +var writeFile = require('write-file-atomic') function CachingRegistryClient (config) { RegistryClient.call(this, adaptConfig(config)) - this._mapToCache = cacheFile(config.get("cache")) + this._mapToCache = cacheFile(config.get('cache')) // swizzle in our custom cache invalidation logic this._request = this.request - this.request = this._invalidatingRequest - this.get = get + this.request = this._invalidatingRequest + this.get = get } inherits(CachingRegistryClient, RegistryClient) @@ -34,7 +34,7 @@ CachingRegistryClient.prototype._invalidatingRequest = function (uri, params, cb var args = arguments var method = params.method - if (method !== "HEAD" && method !== "GET") { + if (method !== 'HEAD' && method !== 'GET') { var invalidated = client._mapToCache(uri) // invalidate cache // @@ -43,7 +43,7 @@ CachingRegistryClient.prototype._invalidatingRequest = function (uri, params, cb // thinking that it didn't work when it did. // Note that failure is an acceptable option here, since the only // result will be a stale cache for some helper commands. - log.verbose("request", "invalidating", invalidated, "on", method) + log.verbose('request', 'invalidating', invalidated, 'on', method) return rimraf(invalidated, function () { cb.apply(undefined, args) }) @@ -54,23 +54,23 @@ CachingRegistryClient.prototype._invalidatingRequest = function (uri, params, cb } function get (uri, params, cb) { - assert(typeof uri === "string", "must pass registry URI to get") - assert(params && typeof params === "object", "must pass params to get") - assert(typeof cb === "function", "must pass callback to get") + assert(typeof uri === 'string', 'must pass registry URI to get') + assert(params && typeof params === 'object', 'must pass params to get') + assert(typeof cb === 'function', 'must pass callback to get') var parsed = url.parse(uri) assert( - parsed.protocol === "http:" || parsed.protocol === "https:", - "must have a URL that starts with http: or https:" + parsed.protocol === 'http:' || parsed.protocol === 'https:', + 'must have a URL that starts with http: or https:' ) - var cacheBase = cacheFile(npm.config.get("cache"))(uri) - var cachePath = path.join(cacheBase, ".cache.json") + var cacheBase = cacheFile(npm.config.get('cache'))(uri) + var cachePath = path.join(cacheBase, '.cache.json') // If the GET is part of a write operation (PUT or DELETE), then // skip past the cache entirely, but still save the results. if (uri.match(/\?write=true$/)) { - log.verbose("get", "GET as part of write; not caching result") + log.verbose('get', 'GET as part of write; not caching result') return get_.call(this, uri, cachePath, params, cb) } @@ -80,8 +80,7 @@ function get (uri, params, cb) { fs.readFile(cachePath, function (er, data) { try { data = JSON.parse(data) - } - catch (ex) { + } catch (ex) { data = null } @@ -90,8 +89,7 @@ function get (uri, params, cb) { get_.call(client, uri, cachePath, params, cb) }) - } - else { + } else { get_.call(client, uri, cachePath, params, cb) } }) @@ -99,16 +97,16 @@ function get (uri, params, cb) { function get_ (uri, cachePath, params, cb) { var staleOk = params.staleOk === undefined ? false : params.staleOk - , timeout = params.timeout === undefined ? -1 : params.timeout - , data = params.data - , stat = params.stat - , etag - , lastModified - - timeout = Math.min(timeout, npm.config.get("cache-max") || 0) - timeout = Math.max(timeout, npm.config.get("cache-min") || -Infinity) + var timeout = params.timeout === undefined ? -1 : params.timeout + var data = params.data + var stat = params.stat + var etag + var lastModified + + timeout = Math.min(timeout, npm.config.get('cache-max') || 0) + timeout = Math.max(timeout, npm.config.get('cache-min') || -Infinity) if (process.env.COMP_CWORD !== undefined && - process.env.COMP_LINE !== undefined && + process.env.COMP_LINE !== undefined && process.env.COMP_POINT !== undefined) { timeout = Math.max(timeout, 60000) } @@ -118,19 +116,19 @@ function get_ (uri, cachePath, params, cb) { if (data._lastModified) lastModified = data._lastModified if (stat && timeout && timeout > 0) { - if ((Date.now() - stat.mtime.getTime())/1000 < timeout) { - log.verbose("get", uri, "not expired, no request") + if ((Date.now() - stat.mtime.getTime()) / 1000 < timeout) { + log.verbose('get', uri, 'not expired, no request') delete data._etag delete data._lastModified - return cb(null, data, JSON.stringify(data), { statusCode : 304 }) + return cb(null, data, JSON.stringify(data), { statusCode: 304 }) } if (staleOk) { - log.verbose("get", uri, "staleOk, background update") + log.verbose('get', uri, 'staleOk, background update') delete data._etag delete data._lastModified process.nextTick( - cb.bind(null, null, data, JSON.stringify(data), { statusCode : 304 } ) + cb.bind(null, null, data, JSON.stringify(data), { statusCode: 304 }) ) cb = function () {} } @@ -138,10 +136,10 @@ function get_ (uri, cachePath, params, cb) { } var options = { - etag : etag, - lastModified : lastModified, - follow : params.follow, - auth : params.auth + etag: etag, + lastModified: lastModified, + follow: params.follow, + auth: params.auth } this.request(uri, options, function (er, remoteData, raw, response) { // if we get an error talking to the registry, but we have it @@ -152,15 +150,15 @@ function get_ (uri, cachePath, params, cb) { } if (response) { - log.silly("get", "cb", [response.statusCode, response.headers]) + log.silly('get', 'cb', [response.statusCode, response.headers]) if (response.statusCode === 304 && (etag || lastModified)) { remoteData = data - log.verbose(etag ? "etag" : "lastModified", uri+" from cache") + log.verbose(etag ? 'etag' : 'lastModified', uri + ' from cache') } } data = remoteData - if (!data) er = er || new Error("failed to fetch from registry: " + uri) + if (!data) er = er || new Error('failed to fetch from registry: ' + uri) if (er) return cb(er, data, raw, response) @@ -174,7 +172,7 @@ function get_ (uri, cachePath, params, cb) { } function saveToCache (cachePath, data, saved) { - log.verbose("get", "saving", data.name, "to", cachePath) + log.verbose('get', 'saving', data.name, 'to', cachePath) getCacheStat(function (er, st) { mkdirp(path.dirname(cachePath), function (er, made) { if (er) return saved() @@ -192,26 +190,26 @@ function get_ (uri, cachePath, params, cb) { function adaptConfig (config) { return { - proxy : { - http : config.get("proxy"), - https : config.get("https-proxy"), - localAddress : config.get("local-address") + proxy: { + http: config.get('proxy'), + https: config.get('https-proxy'), + localAddress: config.get('local-address') }, - ssl : { - certificate : config.get("cert"), - key : config.get("key"), - ca : config.get("ca"), - strict : config.get("strict-ssl") + ssl: { + certificate: config.get('cert'), + key: config.get('key'), + ca: config.get('ca'), + strict: config.get('strict-ssl') }, - retry : { - retries : config.get("fetch-retries"), - factor : config.get("fetch-retry-factor"), - minTimeout : config.get("fetch-retry-mintimeout"), - maxTimeout : config.get("fetch-retry-maxtimeout") + retry: { + retries: config.get('fetch-retries'), + factor: config.get('fetch-retry-factor'), + minTimeout: config.get('fetch-retry-mintimeout'), + maxTimeout: config.get('fetch-retry-maxtimeout') }, - userAgent : config.get("user-agent"), - log : log, - defaultTag : config.get("tag"), - couchToken : config.get("_token") + userAgent: config.get('user-agent'), + log: log, + defaultTag: config.get('tag'), + couchToken: config.get('_token') } } |