diff options
Diffstat (limited to 'node_modules/npm-registry-client/lib/get.js')
-rw-r--r-- | node_modules/npm-registry-client/lib/get.js | 100 |
1 files changed, 63 insertions, 37 deletions
diff --git a/node_modules/npm-registry-client/lib/get.js b/node_modules/npm-registry-client/lib/get.js index 9aab7d7b0..d7b58092b 100644 --- a/node_modules/npm-registry-client/lib/get.js +++ b/node_modules/npm-registry-client/lib/get.js @@ -2,83 +2,94 @@ module.exports = get var fs = require("graceful-fs") + , assert = require("assert") , path = require("path") , mkdir = require("mkdirp") , chownr = require("chownr") - -function get (uri, timeout, nofollow, staleOk, cb) { - if (typeof cb !== "function") cb = staleOk, staleOk = false - if (typeof cb !== "function") cb = nofollow, nofollow = false - if (typeof cb !== "function") cb = timeout, timeout = -1 - - timeout = Math.min(timeout, this.conf.get('cache-max') || 0) - timeout = Math.max(timeout, this.conf.get('cache-min') || -Infinity) - - if (!this.conf.get('registry')) timeout = Infinity - - if ( process.env.COMP_CWORD !== undefined - && process.env.COMP_LINE !== undefined - && process.env.COMP_POINT !== undefined - ) timeout = Math.max(timeout, 60000) - - var cache = this.cacheFile(this.registry + uri) + "/.cache.json" + , url = require("url") + +/** + * options: + * + * timeout: request timeouts + * follow: follow redirects + * staleOk: stale results are OK + * stat: results of checking for cached metadata + * data: the cached metadata + */ +function get (uri, options, cb) { + assert(uri, "must have URL to fetch") + assert(cb, "must have callback") + if (!options) options = {} + + var parsed = url.parse(uri) + assert(parsed.protocol, "must have a URL that starts with npm:, http:, or https:") + + var cache = this.cacheFile(uri) + "/.cache.json" // /-/all is special. // It uses timestamp-based caching and partial updates, // because it is a monster. - if (uri === "/-/all") { - return requestAll.call(this, cache, cb) + if (parsed.pathname === "/-/all") { + return requestAll.call(this, uri, cache, cb) } // If the GET is part of a write operation (PUT or DELETE), then // skip past the cache entirely, but still save the results. - if (uri.match(/\?write=true$/)) - return get_.call(this, uri, timeout, cache, null, null, nofollow, staleOk, cb) - + if (uri.match(/\?write=true$/)) { + return get_.call(this, uri, cache, options, cb) + } fs.stat(cache, function (er, stat) { if (!er) fs.readFile(cache, function (er, data) { try { data = JSON.parse(data) } catch (ex) { data = null } - get_.call(this, uri, timeout, cache, stat, data, nofollow, staleOk, cb) + options.stat = stat + options.data = data + get_.call(this, uri, cache, options, cb) }.bind(this)) - else get_.call(this, uri, timeout, cache, null, null, nofollow, staleOk, cb) + else { + get_.call(this, uri, cache, options, cb) + } }.bind(this)) } -function requestAll (cache, cb) { +function requestAll (uri, cache, cb) { + this.log.info("get", cache) mkdir(path.dirname(cache), function (er) { + if (er) return cb(er) fs.readFile(cache, function (er, data) { - if (er) return requestAll_.call(this, 0, {}, cache, cb) + if (er) return requestAll_.call(this, uri, 0, {}, cache, cb) try { data = JSON.parse(data) } catch (ex) { fs.writeFile(cache, "{}", function (er) { if (er) return cb(new Error("Broken cache.")) - return requestAll_.call(this, 0, {}, cache, cb) + return requestAll_.call(this, uri, 0, {}, cache, cb) }.bind(this)) } var t = +data._updated || 0 - requestAll_.call(this, t, data, cache, cb) + requestAll_.call(this, uri, t, data, cache, cb) }.bind(this)) }.bind(this)) } -function requestAll_ (c, data, cache, cb) { +function requestAll_ (uri, c, data, cache, cb) { // use the cache and update in the background if it's not too old if (Date.now() - c < 60000) { cb(null, data) cb = function () {} } - var uri = "/-/all/since?stale=update_after&startkey=" + c - if (c === 0) { this.log.warn("", "Building the local index for the first time, please be patient") - uri = "/-/all" + uri = url.resolve(uri, "/-/all") + } + else { + uri = url.resolve(uri, "/-/all/since?stale=update_after&startkey=" + c) } - this.request('GET', uri, function (er, updates, _, res) { + this.request('GET', uri, null, function (er, updates, _, res) { if (er) return cb(er, data) var headers = res.headers , updated = updates._updated || Date.parse(headers.date) @@ -94,10 +105,25 @@ function requestAll_ (c, data, cache, cb) { }) } -function get_ (uri, timeout, cache, stat, data, nofollow, staleOk, cb) { - var etag +function get_ (uri, cache, options, cb) { + var staleOk = options.staleOk === undefined ? false : options.staleOk + , follow = options.follow + , data = options.data + , stat = options.stat + , etag + + var timeout = options.timeout === undefined ? -1 : options.timeout + timeout = Math.min(timeout, this.conf.get('cache-max') || 0) + timeout = Math.max(timeout, this.conf.get('cache-min') || -Infinity) + if (process.env.COMP_CWORD !== undefined && + process.env.COMP_LINE !== undefined && + process.env.COMP_POINT !== undefined) { + timeout = Math.max(timeout, 60000) + } + if (data && data._etag) etag = data._etag - if (timeout && timeout > 0 && stat && data) { + + if (timeout && timeout > 0 && options.stat && options.data) { if ((Date.now() - stat.mtime.getTime())/1000 < timeout) { this.log.verbose("registry.get", uri, "not expired, no request") delete data._etag @@ -112,7 +138,7 @@ function get_ (uri, timeout, cache, stat, data, nofollow, staleOk, cb) { } } - this.request('GET', uri, null, etag, nofollow, function (er, remoteData, raw, response) { + this.request('GET', uri, { etag : etag, follow : follow }, function (er, remoteData, raw, response) { // if we get an error talking to the registry, but we have it // from the cache, then just pretend we got it. if (er && cache && data && !data.error) { |