This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
/
request.js
273 lines (254 loc) · 8.77 KB
/
request.js
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
module.exports = request
request.GET = GET
request.PUT = PUT
request.reg = reg
request.upload = upload
var npm = require("../../../npm")
, http = require("http")
, https = require("https")
, url = require("url")
, log = require("../log")
, ini = require("../ini")
, fs = require("../graceful-fs")
, rm = require("../rm-rf")
, asyncMap = require("../async-map")
, proxyify = require("../proxyify")
function request (method, where, what, etag, nofollow, cb_) {
log.verbose(where||"/", method)
if (where.match(/^\/?favicon.ico/)) {
return cb_(new Error("favicon.ico isn't a package, it's a picture."))
}
if (typeof cb_ !== "function") cb_ = nofollow, nofollow = false
if (typeof cb_ !== "function") cb_ = etag, etag = null
if (typeof cb_ !== "function") cb_ = what, what = null
var registry = reg()
if (registry instanceof Error) return cb_(registry)
// Since there are multiple places where an error could occur,
// don't let the cb be called more than once.
var errState = null
function cb (er) {
if (errState) return
if (er) errState = er
cb_.apply(null, arguments)
}
var adduserChange = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)\/-rev/
, adduserNew = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)/
, authRequired = what && !where.match(adduserNew)
|| where.match(adduserChange)
|| method === "DELETE"
if (!where.match(/^https?:\/\//)) {
if (where.charAt(0) !== "/") where = "/" + where
where = "." + where.split("/").map(function (p) {
p = p.trim()
if (p.match(/^org.couchdb.user/)) {
return p.replace(/\//g, encodeURIComponent("/"))
}
return encodeURIComponent(p)
}).join("/")
where = url.resolve(registry, where)
}
var remote = url.parse(where)
, secure = remote.protocol === "https:"
, port = remote.port || (secure ? 443 : 80)
, hostname = remote.hostname
, auth = authRequired && npm.config.get("_auth")
if (authRequired && !auth) {
return cb(new Error(
"Cannot insert data into the registry without authorization\n"
+ "See: npm-adduser(1)"))
}
if (auth && !secure) {
log.warn("Sending authorization over insecure channel.")
}
var headers = { "accept" : "application/json" }
if (auth) headers.authorization = "Basic " + auth
if (what) {
if (what instanceof File) {
log.verbose(what.name, "uploading")
headers["content-type"] = "application/octet-stream"
} else {
delete what._etag
log.silly(what,"writing json")
what = new Buffer(JSON.stringify(what))
headers["content-type"] = "application/json"
}
headers["content-length"] = what.length
} else {
headers["content-length"] = 0
}
if (etag) {
log.verbose(etag, "etag")
headers[method === "GET" ? "if-none-match" : "if-match"] = etag
}
log.silly(headers, "headers")
if (remote.auth) {
headers["authorization"] = (new Buffer(remote.auth).toString("base64"))
delete remote.href
delete remote.host
delete remote.auth
}
var opts = { method: method
, headers: headers
, path: (remote.pathname||"/")
+ (remote.search||"")
+ (remote.hash||"")
, host: remote.hostname
, secure: remote.protocol.toLowerCase() === "https:"
, port: remote.port
}
if (!opts.port) opts.port = opts.secure ? 443 : 80
log.verbose(remote, "about to proxyify")
opts = proxyify(remote, opts)
if (!opts) return cb(new Error("Bad proxy config: "+npm.config.get("proxy")))
var req = (opts.secure ? https : http).request(opts, function (response) {
log.verbose(where, "response")
// if (response.statusCode !== 200) return cb(new Error(
// "Status code " + response.statusCode + " from PUT "+where))
var data = ""
response.on("error", log.er(cb, "response error from "+where))
response.on("data", function (chunk) {
log.silly(chunk+"", "chunk")
data += chunk
})
global.newloctimeout = global.newloctimeout || 0
response.on("end", function () {
if (!nofollow
&& (response.statusCode === 301 || response.statusCode === 302)) {
// relative redirects SHOULD be disallowed, but alas...
var newLoc = response.headers.location
newLoc = newLoc && url.resolve(where, newLoc)
log.verbose(newLoc, "redirect")
if (!newLoc) return cb(new Error(
response.statusCode + " status code with no location"))
//FIXME: wtf? why does this timeout make it work?
return setTimeout(function () {
log.verbose(newLoc, "redirect fer reals")
request(method, newLoc, what, etag,
log.er(cb, "Failed to fetch "+newLoc))
}, 1000*(global.newloctimeout ++))
}
var parsed
if (response.statusCode !== 304) {
try {
parsed = JSON.parse(data)
} catch (ex) {
ex.message += "\n" + data
log.error("error parsing json", "registry")
return cb(ex, null, data, response)
}
}
var er = null
if (parsed && response.headers.etag) {
parsed._etag = response.headers.etag
}
if (parsed && parsed.error) {
var w = url.parse(where).pathname.substr(1)
, name = w.split("/")[0]
if (parsed.error === "not_found") {
er = new Error("404 Not Found: "+name)
er.errno = npm.E404
er.pkgid = name
} else {
er = new Error(
parsed.error + " " + (parsed.reason || "") + ": " + w)
}
} else if (method !== "HEAD" && method !== "GET") {
// invalidate cache
// This is irrelevant for commands that do etag 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.
var path = require("path")
, p = url.parse(where).pathname.split("/")
, _ = "/"
, caches = p.map(function (part) {
return _ = path.join(_, part)
}).map(function (cache) {
return path.join(npm.cache, cache, ".cache.json")
})
asyncMap(caches, rm, function () {})
}
return cb(er, parsed, data, response)
})
}).on("error", cb)
if (what instanceof File) {
var size = Math.min(what.length, 1024*1024*1024)
, remaining = what.length
log.verbose(what.length, "bytes")
;(function W () {
var b = new Buffer(size)
try {
var bytesRead = fs.readSync(what.fd, b, 0, b.length, null)
} catch (er) {
return log.er(cb, "Failure to read tarball")(er)
}
remaining -= bytesRead
if (bytesRead) {
log(bytesRead, "read")
log(remaining, "remain")
return (
req.write(bytesRead === b.length ? b : b.slice(0, bytesRead))
) ? W()
: req.on("drain", function DRAIN () {
log.silly(remaining, "drain")
req.removeListener("drain", DRAIN)
W()
})
}
if (!remaining) {
req.end()
log.verbose(what.name, "written to uploading stream")
log.verbose("Not done yet! If it hangs/quits now, it didn't work.", "upload")
return
}
// wtf!? No bytes read, but also bytes remaining.
return cb(new Error("Some kind of weirdness reading the file"))
})()
return
} else if (typeof what === "string" || Buffer.isBuffer(what)) {
// just a json blob
req.write(what)
}
req.end()
}
function GET (where, etag, nofollow, cb) {
request("GET", where, null, etag, nofollow, cb)
}
function PUT (where, what, etag, nofollow, cb) {
request("PUT", where, what, etag, nofollow, cb)
}
function upload (where, filename, etag, nofollow, cb) {
if (typeof etag === "function") cb = etag, etag = null
new File(filename, function (er, f) {
if (er) return log.er(cb, "Couldn't open "+filename)(er)
PUT(where, f, etag, nofollow, function (er) {
log.info("done with upload", "publish")
cb(er)
})
})
}
function File (name, cb) {
var f = this
f.name = name
if (f.loaded) return cb(null, f)
fs.stat(f.name, function (er, stat) {
if (er) return log.er(cb, "doesn't exist "+f.name)(er)
log.silly(stat, "stat "+name)
f.length = stat.size
fs.open(f.name, "r", 0666, function (er, fd) {
if (er) return log.er(cb, "Error opening "+f.name)(er)
f.fd = fd
cb(null, f)
})
})
}
function reg () {
var r = npm.config.get("registry")
if (!r) {
return new Error("Must define registry URL before accessing registry.")
}
if (r.substr(-1) !== "/") r += "/"
npm.config.set("registry", r)
return r
}