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
|
module.exports = get
var fs = require("graceful-fs")
, assert = require("assert")
, path = require("path")
, mkdir = require("mkdirp")
, chownr = require("chownr")
, url = require("url")
/**
* options:
*
* timeout: request timeouts
* follow: follow redirects
* staleOk: stale results are OK
* stat: results of checking for cached metadata
* data: the cached metadata
*/
function get (uri, options, cb) {
assert(uri, "must have URL to fetch")
assert(cb, "must have callback")
if (!options) options = {}
var parsed = url.parse(uri)
assert(parsed.protocol, "must have a URL that starts with npm:, http:, or https:")
var cache = this.cacheFile(uri) + "/.cache.json"
// /-/all is special.
// It uses timestamp-based caching and partial updates,
// because it is a monster.
if (parsed.pathname === "/-/all") {
return requestAll.call(this, uri, cache, cb)
}
// 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$/)) {
return get_.call(this, uri, cache, options, cb)
}
fs.stat(cache, function (er, stat) {
if (!er) fs.readFile(cache, function (er, data) {
try { data = JSON.parse(data) }
catch (ex) { data = null }
options.stat = stat
options.data = data
get_.call(this, uri, cache, options, cb)
}.bind(this))
else {
get_.call(this, uri, cache, options, cb)
}
}.bind(this))
}
function requestAll (uri, cache, cb) {
this.log.info("get", cache)
mkdir(path.dirname(cache), function (er) {
if (er) return cb(er)
fs.readFile(cache, function (er, data) {
if (er) return requestAll_.call(this, uri, 0, {}, cache, cb)
try {
data = JSON.parse(data)
} catch (ex) {
fs.writeFile(cache, "{}", function (er) {
if (er) return cb(new Error("Broken cache."))
return requestAll_.call(this, uri, 0, {}, cache, cb)
}.bind(this))
}
var t = +data._updated || 0
requestAll_.call(this, uri, t, data, cache, cb)
}.bind(this))
}.bind(this))
}
function requestAll_ (uri, c, data, cache, cb) {
// use the cache and update in the background if it's not too old
if (Date.now() - c < 60000) {
cb(null, data)
cb = function () {}
}
if (c === 0) {
this.log.warn("", "Building the local index for the first time, please be patient")
uri = url.resolve(uri, "/-/all")
}
else {
uri = url.resolve(uri, "/-/all/since?stale=update_after&startkey=" + c)
}
this.request('GET', uri, null, function (er, updates, _, res) {
if (er) return cb(er, data)
var headers = res.headers
, updated = updates._updated || Date.parse(headers.date)
Object.keys(updates).forEach(function (p) {
data[p] = updates[p]
})
data._updated = updated
fs.writeFile( cache, JSON.stringify(data)
, function (er) {
delete data._updated
return cb(er, data)
})
})
}
function get_ (uri, cache, options, cb) {
var staleOk = options.staleOk === undefined ? false : options.staleOk
, follow = options.follow
, data = options.data
, stat = options.stat
, etag
var timeout = options.timeout === undefined ? -1 : options.timeout
timeout = Math.min(timeout, this.conf.get('cache-max') || 0)
timeout = Math.max(timeout, this.conf.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 && data._etag) etag = data._etag
if (timeout && timeout > 0 && options.stat && options.data) {
if ((Date.now() - stat.mtime.getTime())/1000 < timeout) {
this.log.verbose("registry.get", uri, "not expired, no request")
delete data._etag
return cb(null, data, JSON.stringify(data), {statusCode:304})
}
if (staleOk) {
this.log.verbose("registry.get", uri, "staleOk, background update")
delete data._etag
process.nextTick(cb.bind( null, null, data, JSON.stringify(data)
, {statusCode: 304} ))
cb = function () {}
}
}
this.request('GET', uri, { etag : etag, follow : follow }, 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 && cache && data && !data.error) {
er = null
response = {statusCode: 304}
}
if (response) {
this.log.silly("registry.get", "cb", [response.statusCode, response.headers])
if (response.statusCode === 304 && etag) {
remoteData = data
this.log.verbose("etag", 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)
// just give the write the old college try. if it fails, whatever.
function saved () {
delete data._etag
cb(er, data, raw, response)
}
saveToCache.call(this, cache, data, saved)
}.bind(this))
}
function saveToCache (cache, data, saved) {
if (this._cacheStat) {
var cs = this._cacheStat
return saveToCache_.call(this, cache, data, cs.uid, cs.gid, saved)
}
fs.stat(this.conf.get('cache'), function (er, st) {
if (er) {
return fs.stat(process.env.HOME || "", function (er, st) {
// if this fails, oh well.
if (er) return saved()
this._cacheStat = st
return saveToCache.call(this, cache, data, saved)
}.bind(this))
}
this._cacheStat = st || { uid: null, gid: null }
return saveToCache.call(this, cache, data, saved)
}.bind(this))
}
function saveToCache_ (cache, data, uid, gid, saved) {
mkdir(path.dirname(cache), function (er, made) {
if (er) return saved()
fs.writeFile(cache, JSON.stringify(data), function (er) {
if (er || uid === null || gid === null) {
return saved()
}
chownr(made || cache, uid, gid, saved)
})
})
}
|