1 // XXX lib/utils/tar.js and this file need to be rewritten.
3 // URL-to-cache folder mapping:
6 // http://registry.npmjs.org/foo/version -> cache/http!/...
11 1. Check for URL in inflight URLs. If present, add cb, and return.
12 2. Acquire lock at {cache}/{sha(url)}.lock
13 retries = {cache-lock-retries, def=10}
14 stale = {cache-lock-stale, def=60000}
15 wait = {cache-lock-wait, def=10000}
16 3. if lock can't be acquired, then fail
17 4. fetch url, clear lock, call cbs
20 1. urls: http!/server.com/path/to/thing
21 2. c:\path\to\thing: file!/c!/path/to/thing
22 3. /path/to/thing: file!/path/to/thing
23 4. git@ private: git_github.com!npm/npm
24 5. git://public: git!/github.com/npm/npm
25 6. git+blah:// git-blah!/server.com/foo/bar
28 1. tar into tmp/random/package.tgz
29 2. untar into tmp/random/contents/package, stripping one dir piece
30 3. tar tmp/random/contents/package to cache/n/v/package.tgz
31 4. untar cache/n/v/package.tgz into cache/n/v/package
35 1. fetch to tmp/random/package.tgz
38 adding a name@version:
39 1. registry.get(name/version)
40 2. if response isn't 304, add url(dist.tarball)
44 2. Find a version that satisfies
47 adding a local tarball:
48 1. untar to tmp/random/{blah}
51 adding a namespaced package:
52 1. lookup registry for @namespace
53 2. namespace_registry.get('name')
54 3. add url(namespace/latest.tarball)
57 exports = module.exports = cache
63 var npm = require("./npm.js")
64 , fs = require("graceful-fs")
65 , writeFileAtomic = require("write-file-atomic")
66 , assert = require("assert")
67 , rm = require("./utils/gently-rm.js")
68 , readJson = require("read-package-json")
69 , log = require("npmlog")
70 , path = require("path")
71 , asyncMap = require("slide").asyncMap
72 , tar = require("./utils/tar.js")
73 , fileCompletion = require("./utils/completion/file-completion.js")
74 , deprCheck = require("./utils/depr-check.js")
75 , addNamed = require("./cache/add-named.js")
76 , addLocal = require("./cache/add-local.js")
77 , addRemoteTarball = require("./cache/add-remote-tarball.js")
78 , addRemoteGit = require("./cache/add-remote-git.js")
79 , inflight = require("inflight")
80 , realizePackageSpecifier = require("realize-package-specifier")
81 , npa = require("npm-package-arg")
82 , getStat = require("./cache/get-stat.js")
83 , cachedPackageRoot = require("./cache/cached-package-root.js")
84 , mapToRegistry = require("./utils/map-to-registry.js")
86 cache.usage = "npm cache add <tarball file>"
87 + "\nnpm cache add <folder>"
88 + "\nnpm cache add <tarball url>"
89 + "\nnpm cache add <git url>"
90 + "\nnpm cache add <name>@<version>"
91 + "\nnpm cache ls [<path>]"
92 + "\nnpm cache clean [<pkg>[@<version>]]"
94 cache.completion = function (opts, cb) {
96 var argv = opts.conf.argv.remain
97 if (argv.length === 2) {
98 return cb(null, ["add", "ls", "clean"])
104 // cache and ls are easy, because the completion is
105 // what ls_ returns anyway.
106 // just get the partial words, minus the last path part
107 var p = path.dirname(opts.partialWords.slice(3).join("/"))
108 if (p === ".") p = ""
111 // Same semantics as install and publish.
112 return npm.commands.install.completion(opts, cb)
116 function cache (args, cb) {
117 var cmd = args.shift()
119 case "rm": case "clear": case "clean": return clean(args, cb)
120 case "list": case "sl": case "ls": return ls(args, cb)
121 case "add": return add(args, npm.prefix, cb)
122 default: return cb("Usage: "+cache.usage)
126 // if the pkg and ver are in the cache, then
127 // just do a readJson and return.
128 // if they're not, then fetch them from the registry.
129 function read (name, ver, forceBypass, cb) {
130 assert(typeof name === "string", "must include name of module to install")
131 assert(typeof cb === "function", "must include callback")
133 if (forceBypass === undefined || forceBypass === null) forceBypass = true
135 var root = cachedPackageRoot({name : name, version : ver})
136 function c (er, data) {
137 if (er) log.verbose("cache", "addNamed error for", name+"@"+ver, er)
138 if (data) deprCheck(data)
143 if (forceBypass && npm.config.get("force")) {
144 log.verbose("using force", "skipping cache")
145 return addNamed(name, ver, null, c)
148 readJson(path.join(root, "package", "package.json"), function (er, data) {
149 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
152 if (!data.name) return cb(new Error("No name provided"))
153 if (!data.version) return cb(new Error("No version provided"))
156 if (er) return addNamed(name, ver, null, c)
161 function normalize (args) {
163 if (args.length > 0) {
165 if (a.name) normalized = a.name
166 if (a.rawSpec) normalized = [normalized, a.rawSpec].join("/")
167 if (args.length > 1) normalized = [normalized].concat(args.slice(1)).join("/")
170 if (normalized.substr(-1) === "/") {
171 normalized = normalized.substr(0, normalized.length - 1)
173 normalized = path.normalize(normalized)
174 log.silly("ls", "normalized", normalized)
179 // npm cache ls [<path>]
180 function ls (args, cb) {
181 var prefix = npm.config.get("cache")
182 if (prefix.indexOf(process.env.HOME) === 0) {
183 prefix = "~" + prefix.substr(process.env.HOME.length)
185 ls_(normalize(args), npm.config.get("depth"), function (er, files) {
186 console.log(files.map(function (f) {
187 return path.join(prefix, f)
188 }).join("\n").trim())
193 // Calls cb with list of cached pkgs matching show.
194 function ls_ (req, depth, cb) {
195 return fileCompletion(npm.cache, req, depth, cb)
198 // npm cache clean [<path>]
199 function clean (args, cb) {
200 assert(typeof cb === "function", "must include callback")
204 var f = path.join(npm.cache, normalize(args))
205 if (f === npm.cache) {
206 fs.readdir(npm.cache, function (er, files) {
208 asyncMap( files.filter(function (f) {
209 return npm.config.get("force") || f !== "-"
210 }).map(function (f) {
211 return path.join(npm.cache, f)
221 // npm cache add <tarball-url>
222 // npm cache add <pkg> <ver>
223 // npm cache add <tarball>
224 // npm cache add <folder>
225 cache.add = function (pkg, ver, where, scrub, cb) {
226 assert(typeof pkg === "string", "must include name of package to install")
227 assert(typeof cb === "function", "must include callback")
230 return clean([], function (er) {
231 if (er) return cb(er)
232 add([pkg, ver], where, cb)
235 return add([pkg, ver], where, cb)
240 function add (args, where, cb) {
241 // this is hot code. almost everything passes through here.
242 // the args can be any of:
244 // ["pkg", "version"]
247 // This is tricky, because urls can contain @
248 // Also, in some cases we get [name, null] rather
249 // that just a single argument.
251 var usage = "Usage:\n"
252 + " npm cache add <tarball-url>\n"
253 + " npm cache add <pkg>@<ver>\n"
254 + " npm cache add <tarball>\n"
255 + " npm cache add <folder>\n"
258 log.silly("cache add", "args", args)
260 if (args[1] === undefined) args[1] = null
262 // at this point the args length must ==2
263 if (args[1] !== null) {
264 spec = args[0]+"@"+args[1]
265 } else if (args.length === 2) {
269 log.verbose("cache add", "spec", spec)
271 if (!spec) return cb(usage)
279 realizePackageSpecifier(spec, where, function (err, p) {
280 if (err) return cb(err)
282 log.silly("cache add", "parsed spec", p)
287 addLocal(p, null, cb)
290 // get auth, if possible
291 mapToRegistry(p.raw, npm.config, function (err, uri, auth) {
292 if (err) return cb(err)
294 addRemoteTarball(p.spec, {name : p.name}, null, auth, cb)
299 addRemoteGit(p.rawSpec, cb)
302 if (p.name) return addNamed(p.name, p.spec, null, cb)
304 cb(new Error("couldn't figure out how to install " + spec))
309 function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb) {
310 if (typeof cb !== "function") cb = gid, gid = null
311 if (typeof cb !== "function") cb = uid, uid = null
312 if (typeof cb !== "function") cb = fMode, fMode = null
313 if (typeof cb !== "function") cb = dMode, dMode = null
315 read(pkg, ver, false, function (er) {
317 log.error("unpack", "Could not read data for %s", pkg + "@" + ver)
320 npm.commands.unbuild([unpackTarget], true, function (er) {
321 if (er) return cb(er)
322 tar.unpack( path.join(cachedPackageRoot({name : pkg, version : ver}), "package.tgz")
331 function afterAdd (cb) { return function (er, data) {
333 if (adding <= 0) npm.spinner.stop()
335 if (er || !data || !data.name || !data.version) return cb(er, data)
336 log.silly("cache", "afterAdd", data.name+"@"+data.version)
338 // Save the resolved, shasum, etc. into the data so that the next
339 // time we load from this cached data, we have all the same info.
340 // Ignore if it fails.
341 var pj = path.join(cachedPackageRoot(data), "package", "package.json")
343 var done = inflight(pj, cb)
344 if (!done) return log.verbose("afterAdd", pj, "already in flight; not writing")
345 log.verbose("afterAdd", pj, "not in flight; writing")
347 getStat(function (er, cs) {
348 if (er) return done(er)
349 writeFileAtomic(pj, JSON.stringify(data), {chown : cs}, function (er) {
350 if (!er) log.verbose("afterAdd", pj, "written")
351 return done(null, data)