diff options
Diffstat (limited to 'lib/cache/caching-client.js')
-rw-r--r-- | lib/cache/caching-client.js | 142 |
1 files changed, 70 insertions, 72 deletions
diff --git a/lib/cache/caching-client.js b/lib/cache/caching-client.js index ec8eb8e16..faf748b1f 100644 --- a/lib/cache/caching-client.js +++ b/lib/cache/caching-client.js @@ -1,30 +1,30 @@ module.exports = CachingRegistryClient -var path = require("path") - , fs = require("graceful-fs") - , url = require("url") - , assert = require("assert") - , inherits = require("util").inherits - -var RegistryClient = require("npm-registry-client") - , npm = require("../npm.js") - , log = require("npmlog") - , getCacheStat = require("./get-stat.js") - , cacheFile = require("npm-cache-filename") - , mkdirp = require("mkdirp") - , rimraf = require("rimraf") - , chownr = require("chownr") - , writeFile = require("write-file-atomic") +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') function CachingRegistryClient (config) { RegistryClient.call(this, adaptConfig(config)) - this._mapToCache = cacheFile(config.get("cache")) + this._mapToCache = cacheFile(config.get('cache')) // swizzle in our custom cache invalidation logic this._request = this.request - this.request = this._invalidatingRequest - this.get = get + this.request = this._invalidatingRequest + this.get = get } inherits(CachingRegistryClient, RegistryClient) @@ -34,7 +34,7 @@ CachingRegistryClient.prototype._invalidatingRequest = function (uri, params, cb var args = arguments var method = params.method - if (method !== "HEAD" && method !== "GET") { + if (method !== 'HEAD' && method !== 'GET') { var invalidated = client._mapToCache(uri) // invalidate cache // @@ -43,7 +43,7 @@ CachingRegistryClient.prototype._invalidatingRequest = function (uri, params, cb // 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) + log.verbose('request', 'invalidating', invalidated, 'on', method) return rimraf(invalidated, function () { cb.apply(undefined, args) }) @@ -54,23 +54,23 @@ CachingRegistryClient.prototype._invalidatingRequest = function (uri, params, cb } 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") + 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:" + 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") + 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") + log.verbose('get', 'GET as part of write; not caching result') return get_.call(this, uri, cachePath, params, cb) } @@ -80,8 +80,7 @@ function get (uri, params, cb) { fs.readFile(cachePath, function (er, data) { try { data = JSON.parse(data) - } - catch (ex) { + } catch (ex) { data = null } @@ -90,8 +89,7 @@ function get (uri, params, cb) { get_.call(client, uri, cachePath, params, cb) }) - } - else { + } else { get_.call(client, uri, cachePath, params, cb) } }) @@ -99,16 +97,16 @@ function get (uri, params, cb) { function get_ (uri, cachePath, params, cb) { var staleOk = params.staleOk === undefined ? false : params.staleOk - , timeout = params.timeout === undefined ? -1 : params.timeout - , data = params.data - , stat = params.stat - , etag - , lastModified - - timeout = Math.min(timeout, npm.config.get("cache-max") || 0) - timeout = Math.max(timeout, npm.config.get("cache-min") || -Infinity) + 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_LINE !== undefined && process.env.COMP_POINT !== undefined) { timeout = Math.max(timeout, 60000) } @@ -118,19 +116,19 @@ function get_ (uri, cachePath, params, cb) { if (data._lastModified) lastModified = data._lastModified if (stat && timeout && timeout > 0) { - if ((Date.now() - stat.mtime.getTime())/1000 < timeout) { - log.verbose("get", uri, "not expired, no request") + 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 }) + return cb(null, data, JSON.stringify(data), { statusCode: 304 }) } if (staleOk) { - log.verbose("get", uri, "staleOk, background update") + 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.bind(null, null, data, JSON.stringify(data), { statusCode: 304 }) ) cb = function () {} } @@ -138,10 +136,10 @@ function get_ (uri, cachePath, params, cb) { } var options = { - etag : etag, - lastModified : lastModified, - follow : params.follow, - auth : params.auth + 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 @@ -152,15 +150,15 @@ function get_ (uri, cachePath, params, cb) { } if (response) { - log.silly("get", "cb", [response.statusCode, response.headers]) + log.silly('get', 'cb', [response.statusCode, response.headers]) if (response.statusCode === 304 && (etag || lastModified)) { remoteData = data - log.verbose(etag ? "etag" : "lastModified", uri+" from cache") + log.verbose(etag ? 'etag' : 'lastModified', uri + ' from cache') } } data = remoteData - if (!data) er = er || new Error("failed to fetch from registry: " + uri) + if (!data) er = er || new Error('failed to fetch from registry: ' + uri) if (er) return cb(er, data, raw, response) @@ -174,7 +172,7 @@ function get_ (uri, cachePath, params, cb) { } function saveToCache (cachePath, data, saved) { - log.verbose("get", "saving", data.name, "to", cachePath) + log.verbose('get', 'saving', data.name, 'to', cachePath) getCacheStat(function (er, st) { mkdirp(path.dirname(cachePath), function (er, made) { if (er) return saved() @@ -192,26 +190,26 @@ function get_ (uri, cachePath, params, cb) { function adaptConfig (config) { return { - proxy : { - http : config.get("proxy"), - https : config.get("https-proxy"), - localAddress : config.get("local-address") + 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") + 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") + 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") + userAgent: config.get('user-agent'), + log: log, + defaultTag: config.get('tag'), + couchToken: config.get('_token') } } |