"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/cache/update-index.js" (8 Mar 2017, 3345 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 module.exports = updateIndex
    2 
    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')
   13 var pulseTillDone = require('../utils/pulse-till-done.js')
   14 var parseJSON = require('../utils/parse-json.js')
   15 
   16 /* /-/all is special.
   17  * It uses timestamp-based caching and partial updates,
   18  * because it is a monster.
   19  */
   20 function updateIndex (staleness, cb) {
   21   assert(typeof cb === 'function', 'must pass callback to updateIndex')
   22 
   23   mapToRegistry('-/all', npm.config, function (er, uri, auth) {
   24     if (er) return cb(er)
   25 
   26     var params = {
   27       timeout: staleness,
   28       follow: true,
   29       staleOk: true,
   30       auth: auth
   31     }
   32     var cacheBase = cacheFile(npm.config.get('cache'))(uri)
   33     var cachePath = path.join(cacheBase, '.cache.json')
   34     log.info('updateIndex', cachePath)
   35 
   36     getCacheStat(function (er, st) {
   37       if (er) return cb(er)
   38 
   39       mkdir(cacheBase, function (er, made) {
   40         if (er) return cb(er)
   41 
   42         fs.readFile(cachePath, function (er, data) {
   43           if (er) {
   44             log.warn('', 'Building the local index for the first time, please be patient')
   45             return updateIndex_(uri, params, {}, cachePath, cb)
   46           }
   47 
   48           chownr(made || cachePath, st.uid, st.gid, function (er) {
   49             if (er) return cb(er)
   50 
   51             data = parseJSON.noExceptions(data)
   52             if (!data) {
   53               fs.writeFile(cachePath, '{}', function (er) {
   54                 if (er) return cb(new Error('Broken cache.'))
   55 
   56                 log.warn('', 'Building the local index for the first time, please be patient')
   57                 return updateIndex_(uri, params, {}, cachePath, cb)
   58               })
   59             }
   60 
   61             var t = +data._updated || 0
   62             // use the cache and update in the background if it's not too old
   63             if (Date.now() - t < 60000) {
   64               cb(null, data)
   65               cb = function () {}
   66             }
   67 
   68             if (t === 0) {
   69               log.warn('', 'Building the local index for the first time, please be patient')
   70             } else {
   71               log.verbose('updateIndex', 'Cached search data present with timestamp', t)
   72               uri += '/since?stale=update_after&startkey=' + t
   73             }
   74             updateIndex_(uri, params, data, cachePath, cb)
   75           })
   76         })
   77       })
   78     })
   79   })
   80 }
   81 
   82 function updateIndex_ (all, params, data, cachePath, cb) {
   83   log.silly('update-index', 'fetching', all)
   84   npm.registry.request(all, params, pulseTillDone('updateIndex', function (er, updates, _, res) {
   85     if (er) return cb(er, data)
   86 
   87     var headers = res.headers
   88     var updated = updates._updated || Date.parse(headers.date)
   89 
   90     Object.keys(updates).forEach(function (p) { data[p] = updates[p] })
   91 
   92     data._updated = updated
   93     getCacheStat(function (er, st) {
   94       if (er) return cb(er)
   95 
   96       fs.writeFile(cachePath, JSON.stringify(data), function (er) {
   97         delete data._updated
   98         if (er) return cb(er)
   99         chownr(cachePath, st.uid, st.gid, function (er) {
  100           cb(er, data)
  101         })
  102       })
  103     })
  104   }))
  105 }