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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
var path = require("path")
, assert = require("assert")
, fs = require("graceful-fs")
, http = require("http")
, log = require("npmlog")
, semver = require("semver")
, readJson = require("read-package-json")
, url = require("url")
, npm = require("../npm.js")
, deprCheck = require("../utils/depr-check.js")
, inflight = require("inflight")
, addRemoteTarball = require("./add-remote-tarball.js")
, cachedPackageRoot = require("./cached-package-root.js")
, mapToRegistry = require("../utils/map-to-registry.js")
module.exports = addNamed
function getOnceFromRegistry (name, from, next, done) {
mapToRegistry(name, npm.config, function (er, uri, auth) {
if (er) return done(er)
var key = "registry:" + uri
next = inflight(key, next)
if (!next) return log.verbose(from, key, "already in flight; waiting")
else log.verbose(from, key, "not in flight; fetching")
npm.registry.get(uri, { auth : auth }, next)
})
}
function addNamed (name, version, data, cb_) {
assert(typeof name === "string", "must have module name")
assert(typeof cb_ === "function", "must have callback")
var key = name + "@" + version
log.verbose("addNamed", key)
function cb (er, data) {
if (data && !data._fromGithub) data._from = key
cb_(er, data)
}
log.silly("addNamed", "semver.valid", semver.valid(version))
log.silly("addNamed", "semver.validRange", semver.validRange(version))
var fn = ( semver.valid(version, true) ? addNameVersion
: semver.validRange(version, true) ? addNameRange
: addNameTag
)
fn(name, version, data, cb)
}
function addNameTag (name, tag, data, cb) {
log.info("addNameTag", [name, tag])
var explicit = true
if (!tag) {
explicit = false
tag = npm.config.get("tag")
}
getOnceFromRegistry(name, "addNameTag", next, cb)
function next (er, data, json, resp) {
if (!er) er = errorResponse(name, resp)
if (er) return cb(er)
log.silly("addNameTag", "next cb for", name, "with tag", tag)
engineFilter(data)
if (data["dist-tags"] && data["dist-tags"][tag]
&& data.versions[data["dist-tags"][tag]]) {
var ver = data["dist-tags"][tag]
return addNamed(name, ver, data.versions[ver], cb)
}
if (!explicit && Object.keys(data.versions).length) {
return addNamed(name, "*", data, cb)
}
er = installTargetsError(tag, data)
return cb(er)
}
}
function engineFilter (data) {
var npmv = npm.version
, nodev = npm.config.get("node-version")
, strict = npm.config.get("engine-strict")
if (!nodev || npm.config.get("force")) return data
Object.keys(data.versions || {}).forEach(function (v) {
var eng = data.versions[v].engines
if (!eng) return
if (!strict && !data.versions[v].engineStrict) return
if (eng.node && !semver.satisfies(nodev, eng.node, true)
|| eng.npm && !semver.satisfies(npmv, eng.npm, true)) {
delete data.versions[v]
}
})
}
function addNameVersion (name, v, data, cb) {
var ver = semver.valid(v, true)
if (!ver) return cb(new Error("Invalid version: "+v))
var response
if (data) {
response = null
return next()
}
getOnceFromRegistry(name, "addNameVersion", setData, cb)
function setData (er, d, json, resp) {
if (!er) {
er = errorResponse(name, resp)
}
if (er) return cb(er)
data = d && d.versions[ver]
if (!data) {
er = new Error("version not found: "+name+"@"+ver)
er.package = name
er.statusCode = 404
return cb(er)
}
response = resp
next()
}
function next () {
deprCheck(data)
var dist = data.dist
if (!dist) return cb(new Error("No dist in "+data._id+" package"))
if (!dist.tarball) return cb(new Error(
"No dist.tarball in " + data._id + " package"))
if ((response && response.statusCode !== 304) || npm.config.get("force")) {
return fetchit()
}
// we got cached data, so let's see if we have a tarball.
var pkgroot = cachedPackageRoot({name : name, version : ver})
var pkgtgz = path.join(pkgroot, "package.tgz")
var pkgjson = path.join(pkgroot, "package", "package.json")
fs.stat(pkgtgz, function (er) {
if (!er) {
readJson(pkgjson, function (er, data) {
if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
if (data) {
if (!data.name) return cb(new Error("No name provided"))
if (!data.version) return cb(new Error("No version provided"))
// check the SHA of the package we have, to ensure it wasn't installed
// from somewhere other than the registry (eg, a fork)
if (data._shasum && dist.shasum && data._shasum !== dist.shasum) {
return fetchit()
}
}
if (er) return fetchit()
else return cb(null, data)
})
} else return fetchit()
})
function fetchit () {
mapToRegistry(name, npm.config, function (er, _, auth, ruri) {
if (er) return cb(er)
// Use the same protocol as the registry. https registry --> https
// tarballs, but only if they're the same hostname, or else detached
// tarballs may not work.
var tb = url.parse(dist.tarball)
var rp = url.parse(ruri)
if (tb.hostname === rp.hostname && tb.protocol !== rp.protocol) {
tb.protocol = rp.protocol
delete tb.href
}
tb = url.format(tb)
// Only add non-shasum'ed packages if --forced. Only ancient things
// would lack this for good reasons nowadays.
if (!dist.shasum && !npm.config.get("force")) {
return cb(new Error("package lacks shasum: " + data._id))
}
addRemoteTarball(tb, data, dist.shasum, auth, cb)
})
}
}
}
function addNameRange (name, range, data, cb) {
range = semver.validRange(range, true)
if (range === null) return cb(new Error(
"Invalid version range: " + range
))
log.silly("addNameRange", {name:name, range:range, hasData:!!data})
if (data) return next()
getOnceFromRegistry(name, "addNameRange", setData, cb)
function setData (er, d, json, resp) {
if (!er) {
er = errorResponse(name, resp)
}
if (er) return cb(er)
data = d
next()
}
function next () {
log.silly( "addNameRange", "number 2"
, {name:name, range:range, hasData:!!data})
engineFilter(data)
log.silly("addNameRange", "versions"
, [data.name, Object.keys(data.versions || {})])
// if the tagged version satisfies, then use that.
var tagged = data["dist-tags"][npm.config.get("tag")]
if (tagged
&& data.versions[tagged]
&& semver.satisfies(tagged, range, true)) {
return addNamed(name, tagged, data.versions[tagged], cb)
}
// find the max satisfying version.
var versions = Object.keys(data.versions || {})
var ms = semver.maxSatisfying(versions, range, true)
if (!ms) {
return cb(installTargetsError(range, data))
}
// if we don't have a registry connection, try to see if
// there's a cached copy that will be ok.
addNamed(name, ms, data.versions[ms], cb)
}
}
function installTargetsError (requested, data) {
var targets = Object.keys(data["dist-tags"]).filter(function (f) {
return (data.versions || {}).hasOwnProperty(f)
}).concat(Object.keys(data.versions || {}))
requested = data.name + (requested ? "@'" + requested + "'" : "")
targets = targets.length
? "Valid install targets:\n" + JSON.stringify(targets) + "\n"
: "No valid targets found.\n"
+ "Perhaps not compatible with your version of node?"
var er = new Error( "No compatible version found: "
+ requested + "\n" + targets)
er.code = "ETARGET"
return er
}
function errorResponse (name, response) {
var er
if (response.statusCode >= 400) {
er = new Error(http.STATUS_CODES[response.statusCode])
er.statusCode = response.statusCode
er.code = "E" + er.statusCode
er.pkgid = name
}
return er
}
|