Skip to content

Commit

Permalink
outdated: optimize out an extra HTTP request
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
isaacs committed Feb 17, 2014
1 parent b20f9bf commit 5e5e89f
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -247,6 +271,7 @@ function shouldUpdate (args, dir, dep, has, req, depth, cb) {
doIt(d.version, l.version)
else
skip()
})
}

})
}

0 comments on commit 5e5e89f

Please sign in to comment.