summaryrefslogtreecommitdiff
path: root/deps/npm/lib/cache/update-index.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/lib/cache/update-index.js')
-rw-r--r--deps/npm/lib/cache/update-index.js124
1 files changed, 64 insertions, 60 deletions
diff --git a/deps/npm/lib/cache/update-index.js b/deps/npm/lib/cache/update-index.js
index 2955f6a148..ab1ede120b 100644
--- a/deps/npm/lib/cache/update-index.js
+++ b/deps/npm/lib/cache/update-index.js
@@ -1,82 +1,86 @@
module.exports = updateIndex
-var fs = require("graceful-fs")
- , assert = require("assert")
- , path = require("path")
- , mkdir = require("mkdirp")
- , chownr = require("chownr")
- , url = require("url")
- , npm = require("../npm.js")
- , log = require("npmlog")
- , cacheFile = require("npm-cache-filename")
- , getCacheStat = require("./get-stat.js")
+var fs = require('graceful-fs')
+var assert = require('assert')
+var path = require('path')
+var mkdir = require('mkdirp')
+var chownr = require('chownr')
+var npm = require('../npm.js')
+var log = require('npmlog')
+var cacheFile = require('npm-cache-filename')
+var getCacheStat = require('./get-stat.js')
+var mapToRegistry = require('../utils/map-to-registry.js')
/* /-/all is special.
* It uses timestamp-based caching and partial updates,
* because it is a monster.
*/
-function updateIndex (uri, params, cb) {
- assert(typeof uri === "string", "must pass registry URI to updateIndex")
- assert(params && typeof params === "object", "must pass params to updateIndex")
- assert(typeof cb === "function", "must pass callback to updateIndex")
-
- var parsed = url.parse(uri)
- assert(
- parsed.protocol === "http:" || parsed.protocol === "https:",
- "must have a URL that starts with http: or https:"
- )
-
- var cacheBase = cacheFile(npm.config.get("cache"))(uri)
- var cachePath = path.join(cacheBase, ".cache.json")
- log.info("updateIndex", cachePath)
-
- getCacheStat(function (er, st) {
+function updateIndex (staleness, cb) {
+ assert(typeof cb === 'function', 'must pass callback to updateIndex')
+
+ mapToRegistry('-/all', npm.config, function (er, uri, auth) {
if (er) return cb(er)
- mkdir(cacheBase, function (er, made) {
- if (er) return cb(er)
+ var params = {
+ timeout: staleness,
+ follow: true,
+ staleOk: true,
+ auth: auth
+ }
+ var cacheBase = cacheFile(npm.config.get('cache'))(uri)
+ var cachePath = path.join(cacheBase, '.cache.json')
+ log.info('updateIndex', cachePath)
- fs.readFile(cachePath, function (er, data) {
- if (er) return updateIndex_(uri, params, 0, {}, cachePath, cb)
+ getCacheStat(function (er, st) {
+ if (er) return cb(er)
- try {
- data = JSON.parse(data)
- }
- catch (ex) {
- fs.writeFile(cachePath, "{}", function (er) {
- if (er) return cb(new Error("Broken cache."))
+ mkdir(cacheBase, function (er, made) {
+ if (er) return cb(er)
- return updateIndex_(uri, params, 0, {}, cachePath, cb)
+ fs.readFile(cachePath, function (er, data) {
+ if (er) {
+ log.warn('', 'Building the local index for the first time, please be patient')
+ return updateIndex_(uri, params, {}, cachePath, cb)
+ }
+
+ chownr(made || cachePath, st.uid, st.gid, function (er) {
+ if (er) return cb(er)
+
+ try {
+ data = JSON.parse(data)
+ } catch (ex) {
+ fs.writeFile(cachePath, '{}', function (er) {
+ if (er) return cb(new Error('Broken cache.'))
+
+ log.warn('', 'Building the local index for the first time, please be patient')
+ return updateIndex_(uri, params, {}, cachePath, cb)
+ })
+ }
+
+ var t = +data._updated || 0
+ // use the cache and update in the background if it's not too old
+ if (Date.now() - t < 60000) {
+ cb(null, data)
+ cb = function () {}
+ }
+
+ if (t === 0) {
+ log.warn('', 'Building the local index for the first time, please be patient')
+ } else {
+ log.verbose('updateIndex', 'Cached search data present with timestamp', t)
+ uri += '/since?stale=update_after&startkey=' + t
+ }
+ updateIndex_(uri, params, data, cachePath, cb)
})
- }
- var t = +data._updated || 0
- chownr(made || cachePath, st.uid, st.gid, function (er) {
- if (er) return cb(er)
-
- updateIndex_(uri, params, t, data, cachePath, cb)
})
})
})
})
}
-function updateIndex_ (uri, params, t, data, cachePath, cb) {
- // use the cache and update in the background if it's not too old
- if (Date.now() - t < 60000) {
- cb(null, data)
- cb = function () {}
- }
-
- var full
- if (t === 0) {
- log.warn("", "Building the local index for the first time, please be patient")
- full = url.resolve(uri, "/-/all")
- }
- else {
- full = url.resolve(uri, "/-/all/since?stale=update_after&startkey=" + t)
- }
-
- npm.registry.request(full, params, function (er, updates, _, res) {
+function updateIndex_ (all, params, data, cachePath, cb) {
+ log.silly('update-index', 'fetching', all)
+ npm.registry.request(all, params, function (er, updates, _, res) {
if (er) return cb(er, data)
var headers = res.headers