UNPKG

10.3 kBJavaScriptView Raw
1// XXX lib/utils/tar.js and this file need to be rewritten.
2
3// URL-to-cache folder mapping:
4// : -> !
5// @ -> _
6// http://registry.npmjs.org/foo/version -> cache/http!/...
7//
8
9/*
10fetching a URL:
111. Check for URL in inflight URLs. If present, add cb, and return.
122. Acquire lock at {cache}/{sha(url)}.lock
13 retries = {cache-lock-retries, def=3}
14 stale = {cache-lock-stale, def=30000}
15 wait = {cache-lock-wait, def=100}
163. if lock can't be acquired, then fail
174. fetch url, clear lock, call cbs
18
19cache folders:
201. urls: http!/server.com/path/to/thing
212. c:\path\to\thing: file!/c!/path/to/thing
223. /path/to/thing: file!/path/to/thing
234. git@ private: git_github.com!npm/npm
245. git://public: git!/github.com/npm/npm
256. git+blah:// git-blah!/server.com/foo/bar
26
27adding a folder:
281. tar into tmp/random/package.tgz
292. untar into tmp/random/contents/package, stripping one dir piece
303. tar tmp/random/contents/package to cache/n/v/package.tgz
314. untar cache/n/v/package.tgz into cache/n/v/package
325. rm tmp/random
33
34Adding a url:
351. fetch to tmp/random/package.tgz
362. goto folder(2)
37
38adding a name@version:
391. registry.get(name/version)
402. if response isn't 304, add url(dist.tarball)
41
42adding a name@range:
431. registry.get(name)
442. Find a version that satisfies
453. add name@version
46
47adding a local tarball:
481. untar to tmp/random/{blah}
492. goto folder(2)
50
51adding a namespaced package:
521. lookup registry for @namespace
532. namespace_registry.get('name')
543. add url(namespace/latest.tarball)
55*/
56
57exports = module.exports = cache
58
59cache.unpack = unpack
60cache.clean = clean
61cache.read = read
62
63var 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 , maybeGithub = require("./cache/maybe-github.js")
80 , inflight = require("inflight")
81 , realizePackageSpecifier = require("realize-package-specifier")
82 , npa = require("npm-package-arg")
83 , getStat = require("./cache/get-stat.js")
84 , cachedPackageRoot = require("./cache/cached-package-root.js")
85
86cache.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>]]"
93
94cache.completion = function (opts, cb) {
95
96 var argv = opts.conf.argv.remain
97 if (argv.length === 2) {
98 return cb(null, ["add", "ls", "clean"])
99 }
100
101 switch (argv[2]) {
102 case "clean":
103 case "ls":
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 = ""
109 return ls_(p, 2, cb)
110 case "add":
111 // Same semantics as install and publish.
112 return npm.commands.install.completion(opts, cb)
113 }
114}
115
116function cache (args, cb) {
117 var cmd = args.shift()
118 switch (cmd) {
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)
123 }
124}
125
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.
129function 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")
132
133 if (forceBypass === undefined || forceBypass === null) forceBypass = true
134
135 var root = cachedPackageRoot({name : name, version : ver})
136 function c (er, data) {
137 log.silly("cache", "addNamed cb", name+"@"+ver)
138 if (er) log.verbose("cache", "addNamed error for", name+"@"+ver, er)
139
140 if (data) deprCheck(data)
141
142 return cb(er, data)
143 }
144
145 if (forceBypass && npm.config.get("force")) {
146 log.verbose("using force", "skipping cache")
147 return addNamed(name, ver, null, c)
148 }
149
150 readJson(path.join(root, "package", "package.json"), function (er, data) {
151 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
152
153 if (data) {
154 if (!data.name) return cb(new Error("No name provided"))
155 if (!data.version) return cb(new Error("No version provided"))
156 }
157
158 if (er) return addNamed(name, ver, null, c)
159 else c(er, data)
160 })
161}
162
163function normalize (args) {
164 var normalized = ""
165 if (args.length > 0) {
166 var a = npa(args[0])
167 if (a.name) normalized = a.name
168 if (a.rawSpec) normalized = [normalized, a.rawSpec].join("/")
169 if (args.length > 1) normalized = [normalized].concat(args.slice(1)).join("/")
170 }
171
172 if (normalized.substr(-1) === "/") {
173 normalized = normalized.substr(0, normalized.length - 1)
174 }
175 log.silly("ls", "normalized", normalized)
176
177 return normalized
178}
179
180// npm cache ls [<path>]
181function ls (args, cb) {
182 var prefix = npm.config.get("cache")
183 if (prefix.indexOf(process.env.HOME) === 0) {
184 prefix = "~" + prefix.substr(process.env.HOME.length)
185 }
186 ls_(normalize(args), npm.config.get("depth"), function (er, files) {
187 console.log(files.map(function (f) {
188 return path.join(prefix, f)
189 }).join("\n").trim())
190 cb(er, files)
191 })
192}
193
194// Calls cb with list of cached pkgs matching show.
195function ls_ (req, depth, cb) {
196 return fileCompletion(npm.cache, req, depth, cb)
197}
198
199// npm cache clean [<path>]
200function clean (args, cb) {
201 assert(typeof cb === "function", "must include callback")
202
203 if (!args) args = []
204
205 var f = path.join(npm.cache, path.normalize(normalize(args)))
206 if (f === npm.cache) {
207 fs.readdir(npm.cache, function (er, files) {
208 if (er) return cb()
209 asyncMap( files.filter(function (f) {
210 return npm.config.get("force") || f !== "-"
211 }).map(function (f) {
212 return path.join(npm.cache, f)
213 })
214 , rm, cb )
215 })
216 } else rm(path.join(npm.cache, path.normalize(normalize(args))), cb)
217}
218
219// npm cache add <tarball-url>
220// npm cache add <pkg> <ver>
221// npm cache add <tarball>
222// npm cache add <folder>
223cache.add = function (pkg, ver, where, scrub, cb) {
224 assert(typeof pkg === "string", "must include name of package to install")
225 assert(typeof cb === "function", "must include callback")
226
227 if (scrub) {
228 return clean([], function (er) {
229 if (er) return cb(er)
230 add([pkg, ver], where, cb)
231 })
232 }
233 return add([pkg, ver], where, cb)
234}
235
236
237var adding = 0
238function add (args, where, cb) {
239 // this is hot code. almost everything passes through here.
240 // the args can be any of:
241 // ["url"]
242 // ["pkg", "version"]
243 // ["pkg@version"]
244 // ["pkg", "url"]
245 // This is tricky, because urls can contain @
246 // Also, in some cases we get [name, null] rather
247 // that just a single argument.
248
249 var usage = "Usage:\n"
250 + " npm cache add <tarball-url>\n"
251 + " npm cache add <pkg>@<ver>\n"
252 + " npm cache add <tarball>\n"
253 + " npm cache add <folder>\n"
254 , spec
255
256 log.silly("cache add", "args", args)
257
258 if (args[1] === undefined) args[1] = null
259
260 // at this point the args length must ==2
261 if (args[1] !== null) {
262 spec = args[0]+"@"+args[1]
263 } else if (args.length === 2) {
264 spec = args[0]
265 }
266
267 log.verbose("cache add", "spec", spec)
268
269 if (!spec) return cb(usage)
270
271 if (adding <= 0) {
272 npm.spinner.start()
273 }
274 adding++
275 cb = afterAdd(cb)
276
277 realizePackageSpecifier(spec, where, function (err, p) {
278 if (err) return cb(err)
279
280 log.silly("cache add", "parsed spec", p)
281
282 switch (p.type) {
283 case "local":
284 case "directory":
285 addLocal(p, null, cb)
286 break
287 case "remote":
288 addRemoteTarball(p.spec, {name : p.name}, null, cb)
289 break
290 case "git":
291 addRemoteGit(p.spec, false, cb)
292 break
293 case "github":
294 maybeGithub(p.spec, cb)
295 break
296 default:
297 if (p.name) return addNamed(p.name, p.spec, null, cb)
298
299 cb(new Error("couldn't figure out how to install " + spec))
300 }
301 })
302}
303
304function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb) {
305 if (typeof cb !== "function") cb = gid, gid = null
306 if (typeof cb !== "function") cb = uid, uid = null
307 if (typeof cb !== "function") cb = fMode, fMode = null
308 if (typeof cb !== "function") cb = dMode, dMode = null
309
310 read(pkg, ver, false, function (er) {
311 if (er) {
312 log.error("unpack", "Could not read data for %s", pkg + "@" + ver)
313 return cb(er)
314 }
315 npm.commands.unbuild([unpackTarget], true, function (er) {
316 if (er) return cb(er)
317 tar.unpack( path.join(cachedPackageRoot({name : pkg, version : ver}), "package.tgz")
318 , unpackTarget
319 , dMode, fMode
320 , uid, gid
321 , cb )
322 })
323 })
324}
325
326function afterAdd (cb) { return function (er, data) {
327 adding--
328 if (adding <= 0) npm.spinner.stop()
329
330 if (er || !data || !data.name || !data.version) return cb(er, data)
331 log.silly("cache", "afterAdd", data.name+"@"+data.version)
332
333 // Save the resolved, shasum, etc. into the data so that the next
334 // time we load from this cached data, we have all the same info.
335 var pj = path.join(cachedPackageRoot(data), "package", "package.json")
336
337 var done = inflight(pj, cb)
338 if (!done) return log.verbose("afterAdd", pj, "already in flight; not writing")
339 log.verbose("afterAdd", pj, "not in flight; writing")
340
341 getStat(function (er, cs) {
342 if (er) return done(er)
343 writeFileAtomic(pj, JSON.stringify(data), {chown : cs}, function (er) {
344 if (!er) log.verbose("afterAdd", pj, "written")
345 return done(er, data)
346 })
347 })
348}}