summaryrefslogtreecommitdiff
path: root/deps/npm/lib/cache/caching-client.js
blob: a6bcee373dafc87fd99b5e8a56d4150d5bccb617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
module.exports = CachingRegistryClient

var path = require('path')
var fs = require('graceful-fs')
var url = require('url')
var assert = require('assert')
var inherits = require('util').inherits

var RegistryClient = require('npm-registry-client')
var npm = require('../npm.js')
var log = require('npmlog')
var getCacheStat = require('./get-stat.js')
var cacheFile = require('npm-cache-filename')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var chownr = require('chownr')
var writeFile = require('write-file-atomic')
var parseJSON = require('../utils/parse-json')

function CachingRegistryClient (config) {
  RegistryClient.call(this, adaptConfig(config))

  this._mapToCache = cacheFile(config.get('cache'))

  // swizzle in our custom cache invalidation logic
  this._request = this.request
  this.request = this._invalidatingRequest
  this.get = get
}
inherits(CachingRegistryClient, RegistryClient)

CachingRegistryClient.prototype._invalidatingRequest = function (uri, params, cb) {
  var client = this
  this._request(uri, params, function () {
    var args = arguments

    var method = params.method
    if (method !== 'HEAD' && method !== 'GET') {
      var invalidated = client._mapToCache(uri)
      // invalidate cache
      //
      // This is irrelevant for commands that do etag / last-modified caching,
      // but ls and view also have a timed cache, so this keeps the user from
      // thinking that it didn't work when it did.
      // Note that failure is an acceptable option here, since the only
      // result will be a stale cache for some helper commands.
      log.verbose('request', 'invalidating', invalidated, 'on', method)
      return rimraf(invalidated, function () {
        cb.apply(undefined, args)
      })
    }

    cb.apply(undefined, args)
  })
}

function get (uri, params, cb) {
  assert(typeof uri === 'string', 'must pass registry URI to get')
  assert(params && typeof params === 'object', 'must pass params to get')
  assert(typeof cb === 'function', 'must pass callback to get')

  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')

  // 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$/)) {
    log.verbose('get', 'GET as part of write; not caching result')
    return get_.call(this, uri, cachePath, params, cb)
  }

  if (params.skipCache) {
    return get_.call(this, uri, cachePath, params, cb)
  }

  var client = this
  fs.stat(cachePath, function (er, stat) {
    if (!er) {
      fs.readFile(cachePath, function (er, data) {
        data = parseJSON.noExceptions(data)

        params.stat = stat
        params.data = data

        get_.call(client, uri, cachePath, params, cb)
      })
    } else {
      get_.call(client, uri, cachePath, params, cb)
    }
  })
}

function get_ (uri, cachePath, params, cb) {
  var staleOk = params.staleOk === undefined ? false : params.staleOk
  var timeout = params.timeout === undefined ? -1 : params.timeout
  var data = params.data
  var stat = params.stat
  var etag
  var lastModified

  timeout = Math.min(timeout, npm.config.get('cache-max') || 0)
  timeout = Math.max(timeout, npm.config.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) {
    if (data._etag) etag = data._etag
    if (data._lastModified) lastModified = data._lastModified

    data._cached = true

    if (stat && timeout && timeout > 0) {
      if ((Date.now() - stat.mtime.getTime()) / 1000 < timeout) {
        log.verbose('get', uri, 'not expired, no request')
        delete data._etag
        delete data._lastModified
        return cb(null, data, JSON.stringify(data), { statusCode: 304 })
      }

      if (staleOk) {
        log.verbose('get', uri, 'staleOk, background update')
        delete data._etag
        delete data._lastModified
        process.nextTick(
          cb.bind(null, null, data, JSON.stringify(data), { statusCode: 304 })
        )
        cb = function () {}
      }
    }
  }

  var options = {
    etag: etag,
    lastModified: lastModified,
    follow: params.follow,
    auth: params.auth
  }
  this.request(uri, options, 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 && cachePath && data && !data.error) {
      er = null
      response = { statusCode: 304 }
    }

    if (response) {
      log.silly('get', 'cb', [response.statusCode, response.headers])
      if (response.statusCode === 304 && (etag || lastModified)) {
        remoteData = data
        log.verbose(etag ? 'etag' : 'lastModified', uri + ' from cache')
      }
    }

    data = remoteData
    if (!data) er = er || new Error('failed to fetch from registry: ' + uri)

    if (er) return cb(er, data, raw, response)

    saveToCache(cachePath, data, saved)

    // just give the write the old college try.  if it fails, whatever.
    function saved () {
      delete data._etag
      delete data._lastModified
      cb(er, data, raw, response)
    }

    function saveToCache (cachePath, data, saved) {
      log.verbose('get', 'saving', data.name, 'to', cachePath)
      getCacheStat(function (er, st) {
        mkdirp(path.dirname(cachePath), function (er, made) {
          if (er) return saved()

          writeFile(cachePath, JSON.stringify(data), function (er) {
            if (er) return saved()

            chownr(made || cachePath, st.uid, st.gid, saved)
          })
        })
      })
    }
  })
}

function adaptConfig (config) {
  return {
    proxy: {
      http: config.get('proxy'),
      https: config.get('https-proxy'),
      localAddress: config.get('local-address')
    },
    ssl: {
      certificate: config.get('cert'),
      key: config.get('key'),
      ca: config.get('ca'),
      strict: config.get('strict-ssl')
    },
    retry: {
      retries: config.get('fetch-retries'),
      factor: config.get('fetch-retry-factor'),
      minTimeout: config.get('fetch-retry-mintimeout'),
      maxTimeout: config.get('fetch-retry-maxtimeout')
    },
    userAgent: config.get('user-agent'),
    log: log,
    defaultTag: config.get('tag'),
    couchToken: config.get('_token'),
    maxSockets: config.get('maxsockets')
  }
}