]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/cache/update-index.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / cache / update-index.js
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
14 /* /-/all is special.
15  * It uses timestamp-based caching and partial updates,
16  * because it is a monster.
17  */
18 function updateIndex (staleness, cb) {
19   assert(typeof cb === 'function', 'must pass callback to updateIndex')
20
21   mapToRegistry('-/all', npm.config, function (er, uri, auth) {
22     if (er) return cb(er)
23
24     var params = {
25       timeout: staleness,
26       follow: true,
27       staleOk: true,
28       auth: auth
29     }
30     var cacheBase = cacheFile(npm.config.get('cache'))(uri)
31     var cachePath = path.join(cacheBase, '.cache.json')
32     log.info('updateIndex', cachePath)
33
34     getCacheStat(function (er, st) {
35       if (er) return cb(er)
36
37       mkdir(cacheBase, function (er, made) {
38         if (er) return cb(er)
39
40         fs.readFile(cachePath, function (er, data) {
41           if (er) {
42             log.warn('', 'Building the local index for the first time, please be patient')
43             return updateIndex_(uri, params, {}, cachePath, cb)
44           }
45
46           chownr(made || cachePath, st.uid, st.gid, function (er) {
47             if (er) return cb(er)
48
49             try {
50               data = JSON.parse(data)
51             } catch (ex) {
52               fs.writeFile(cachePath, '{}', function (er) {
53                 if (er) return cb(new Error('Broken cache.'))
54
55                 log.warn('', 'Building the local index for the first time, please be patient')
56                 return updateIndex_(uri, params, {}, cachePath, cb)
57               })
58             }
59
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) {
63               cb(null, data)
64               cb = function () {}
65             }
66
67             if (t === 0) {
68               log.warn('', 'Building the local index for the first time, please be patient')
69             } else {
70               log.verbose('updateIndex', 'Cached search data present with timestamp', t)
71               uri += '/since?stale=update_after&startkey=' + t
72             }
73             updateIndex_(uri, params, data, cachePath, cb)
74           })
75         })
76       })
77     })
78   })
79 }
80
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)
85
86     var headers = res.headers
87     var updated = updates._updated || Date.parse(headers.date)
88
89     Object.keys(updates).forEach(function (p) { data[p] = updates[p] })
90
91     data._updated = updated
92     getCacheStat(function (er, st) {
93       if (er) return cb(er)
94
95       fs.writeFile(cachePath, JSON.stringify(data), function (er) {
96         delete data._updated
97         if (er) return cb(er)
98         chownr(cachePath, st.uid, st.gid, function (er) {
99           cb(er, data)
100         })
101       })
102     })
103   })
104 }