Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2014-02-17 04:18:44 +0400
committerisaacs <i@izs.me>2014-02-17 04:18:44 +0400
commit5e5e89f4b96205ec1ac054edcca76d197923450f (patch)
tree3891a4f7bbd9d37da18c1d910463c57a7f4bbb9b /lib
parentb20f9bf921a3842fe6392ed94c676b6d06ec253b (diff)
outdated: optimize out an extra HTTP request
While the request to foo/latest will be less data over the wire, it's also much less likely to be cached by the registry CDN. This, it's more likely to be a trip to the origin server, which is much slower than serving out of the CDN. Additionally, we can then use that data object to check the versions hash here in outdated.js, rather than doing a full tgz download and unpack for 'cache.add'.
Diffstat (limited to 'lib')
-rw-r--r--lib/outdated.js33
1 files changed, 29 insertions, 4 deletions
diff --git a/lib/outdated.js b/lib/outdated.js
index 5eb49737f..416253252 100644
--- a/lib/outdated.js
+++ b/lib/outdated.js
@@ -29,6 +29,7 @@ var path = require("path")
, color = require("ansicolors")
, styles = require("ansistyles")
, table = require("text-table")
+ , semver = require("semver")
function outdated (args, silent, cb) {
if (typeof cb !== "function") cb = silent, silent = false
@@ -222,10 +223,33 @@ function shouldUpdate (args, dir, dep, has, req, depth, cb) {
var registry = npm.registry
// search for the latest package
- registry.get(dep + "/latest", function (er, l) {
+ registry.get(dep, function (er, d) {
if (er) return cb()
- // so, we can conceivably update this. find out if we need to.
- cache.add(dep, req, function (er, d) {
+ if (!d || !d['dist-tags'] || !d.versions) return cb()
+ var l = d.versions[d['dist-tags'].latest]
+ if (!l) return cb()
+
+ // set to true if found in doc
+ var found = false
+
+ var r = req
+ if (d['dist-tags'][req])
+ r = d['dist-tags'][req]
+
+ if (semver.validRange(r, true)) {
+ // some kind of semver range.
+ // see if it's in the doc.
+ var vers = Object.keys(d.versions)
+ var v = semver.maxSatisfying(vers, r, true)
+ if (v) {
+ return onCacheAdd(null, d.versions[v])
+ }
+ }
+
+ // We didn't find the version in the doc. See if cache can find it.
+ cache.add(dep, req, onCacheAdd)
+
+ function onCacheAdd(er, d) {
// if this fails, then it means we can't update this thing.
// it's probably a thing that isn't published.
if (er) {
@@ -247,6 +271,7 @@ function shouldUpdate (args, dir, dep, has, req, depth, cb) {
doIt(d.version, l.version)
else
skip()
- })
+ }
+
})
}