1 module.exports = updateIndex
3 var fs = require('graceful-fs')
4 var assert = require('assert')
5 var path = require('path')
6 var mkdir = require('mkdirp')
7 var chownr = require('chownr')
8 var npm = require('../npm.js')
9 var log = require('npmlog')
10 var cacheFile = require('npm-cache-filename')
11 var getCacheStat = require('./get-stat.js')
12 var mapToRegistry = require('../utils/map-to-registry.js')
15 * It uses timestamp-based caching and partial updates,
16 * because it is a monster.
18 function updateIndex (staleness, cb) {
19 assert(typeof cb === 'function', 'must pass callback to updateIndex')
21 mapToRegistry('-/all', npm.config, function (er, uri, auth) {
30 var cacheBase = cacheFile(npm.config.get('cache'))(uri)
31 var cachePath = path.join(cacheBase, '.cache.json')
32 log.info('updateIndex', cachePath)
34 getCacheStat(function (er, st) {
37 mkdir(cacheBase, function (er, made) {
40 fs.readFile(cachePath, function (er, data) {
42 log.warn('', 'Building the local index for the first time, please be patient')
43 return updateIndex_(uri, params, {}, cachePath, cb)
46 chownr(made || cachePath, st.uid, st.gid, function (er) {
50 data = JSON.parse(data)
52 fs.writeFile(cachePath, '{}', function (er) {
53 if (er) return cb(new Error('Broken cache.'))
55 log.warn('', 'Building the local index for the first time, please be patient')
56 return updateIndex_(uri, params, {}, cachePath, cb)
60 var t = +data._updated || 0
61 // use the cache and update in the background if it's not too old
62 if (Date.now() - t < 60000) {
68 log.warn('', 'Building the local index for the first time, please be patient')
70 log.verbose('updateIndex', 'Cached search data present with timestamp', t)
71 uri += '/since?stale=update_after&startkey=' + t
73 updateIndex_(uri, params, data, cachePath, cb)
81 function updateIndex_ (all, params, data, cachePath, cb) {
82 log.silly('update-index', 'fetching', all)
83 npm.registry.request(all, params, function (er, updates, _, res) {
84 if (er) return cb(er, data)
86 var headers = res.headers
87 var updated = updates._updated || Date.parse(headers.date)
89 Object.keys(updates).forEach(function (p) { data[p] = updates[p] })
91 data._updated = updated
92 getCacheStat(function (er, st) {
95 fs.writeFile(cachePath, JSON.stringify(data), function (er) {
98 chownr(cachePath, st.uid, st.gid, function (er) {