Permalink
Browse files

cache: Save shasum, write resolved/etc data to cache

Don't throw away that delicious metadata.
  • Loading branch information...
1 parent 7c49109 commit 120f5a93437bbbea9249801574a2f33e44e81c33 @isaacs isaacs committed Mar 29, 2014
Showing with 42 additions and 3 deletions.
  1. +42 −3 lib/cache.js
View
@@ -241,9 +241,10 @@ function add (args, cb) {
log.verbose("cache add", "name=%j spec=%j args=%j", name, spec, args)
-
if (!name && !spec) return cb(usage)
+ cb = afterAdd([name, spec], cb)
+
// see if the spec is a url
// otherwise, treat as name@version
var p = url.parse(spec) || {}
@@ -265,6 +266,23 @@ function add (args, cb) {
add_(name, spec, p, cb)
}
+function afterAdd (arg, cb) { return function (er, data) {
+ if (er || !data || !data.name || !data.version) {
+ return cb(er, data)
+ }
+
+ // Save the resolved, shasum, etc. into the data so that the next
+ // time we load from this cached data, we have all the same info.
+ var name = data.name
+ var ver = data.version
+ var pj = path.join(npm.cache, name, ver, "package", "package.json")
+ fs.writeFile(pj, JSON.stringify(data), "utf8", function (er) {
+ cb(er, data)
+ })
+}}
+
+
+
function maybeFile (spec, p, cb) {
fs.stat(spec, function (er, stat) {
if (!er) {
@@ -316,7 +334,15 @@ function fetchAndShaCheck (u, tmp, shasum, cb) {
log.error("fetch failed", u)
return cb(er, response)
}
- if (!shasum) return cb(null, response)
+
+ 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) {
+ cb(er, response, shasum)
+ })
+ }
+
// validate that the url we just downloaded matches the expected shasum.
sha.check(tmp, shasum, function (er) {
if (er != null && er.message) {
@@ -344,6 +370,7 @@ function addRemoteTarball (u, shasum, name, version, cb_) {
function cb (er, data) {
if (data) {
data._from = u
+ data._shasum = data._shasum || shasum
data._resolved = u
}
unlock(u, function () {
@@ -931,6 +958,14 @@ function addLocalTarball (p, name, version, shasum, cb_) {
if (typeof cb_ !== "function") cb_ = version, version = ""
if (typeof cb_ !== "function") cb_ = name, name = ""
+ // If we don't have a shasum yet, then get the shasum now.
+ if (!shasum) {
+ return sha.get(p, function (er, shasum) {
+ if (er) return cb_(er)
+ addLocalTarball(p, name, version, shasum, cb_)
+ })
+ }
+
// if it's a tar, and not in place,
// then unzip to .tmp, add the tmp folder, and clean up tmp
if (pathIsInside(p, npm.tmp))
@@ -943,7 +978,10 @@ function addLocalTarball (p, name, version, shasum, cb_) {
}
function cb (er, data) {
- if (data) data._resolved = p
+ if (data) {
+ data._resolved = p
+ data._shasum = data._shasum || shasum
+ }
return cb_(er, data)
}
@@ -1289,6 +1327,7 @@ function addTmpTarball_ (tgz, data, name, version, shasum, cb) {
read.on("error", cb).pipe(write).on("error", cb).on("close", done)
function done() {
+ data._shasum = data._shasum || shasum
cb(null, data)
}
}

0 comments on commit 120f5a9

Please sign in to comment.