1 module.exports = regRequest
3 var url = require("url")
4 , fs = require("graceful-fs")
5 , rm = require("rimraf")
6 , asyncMap = require("slide").asyncMap
7 , Stream = require("stream").Stream
8 , request = require("request")
9 , retry = require("retry")
11 function regRequest (method, where, what, etag, nofollow, reauthed, cb_) {
12 if (typeof cb_ !== "function") cb_ = reauthed, reauthed = false
13 if (typeof cb_ !== "function") cb_ = nofollow, nofollow = false
14 if (typeof cb_ !== "function") cb_ = etag, etag = null
15 if (typeof cb_ !== "function") cb_ = what, what = null
17 var registry = this.conf.get('registry')
18 if (!registry) return cb(new Error(
19 "No registry url provided: " + method + " " + where))
21 // Since there are multiple places where an error could occur,
22 // don't let the cb be called more than once.
27 cb_.apply(null, arguments)
30 if (where.match(/^\/?favicon.ico/)) {
31 return cb(new Error("favicon.ico isn't a package, it's a picture."))
34 var adduserChange = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)\/-rev/
35 , adduserNew = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)/
36 , nu = where.match(adduserNew)
37 , uc = where.match(adduserChange)
38 , isUpload = what || this.conf.get('always-auth')
39 , isDel = method === "DELETE"
40 , authRequired = isUpload && !nu || uc || isDel
42 // resolve to a full url on the registry
43 if (!where.match(/^https?:\/\//)) {
44 this.log.verbose("url raw", where)
46 var q = where.split("?")
50 if (where.charAt(0) !== "/") where = "/" + where
51 where = "." + where.split("/").map(function (p) {
53 if (p.match(/^org.couchdb.user/)) {
54 return p.replace(/\//g, encodeURIComponent("/"))
56 return encodeURIComponent(p)
58 if (q) where += "?" + q
59 this.log.verbose("url resolving", [registry, where])
60 where = url.resolve(registry, where)
61 this.log.verbose("url resolved", where)
64 var remote = url.parse(where)
65 , auth = this.conf.get('_auth')
67 if (authRequired && !this.conf.get('always-auth')) {
68 var couch = this.couchLogin
69 , token = couch && (this.conf.get('_token') || couch.token)
70 , validToken = token && couch.valid(token)
72 if (!validToken) token = null
73 else this.conf.set('_token', token)
75 if (couch && !token) {
76 // login to get a valid token
77 var a = { name: this.conf.get('username'),
78 password: this.conf.get('_password') }
80 return this.couchLogin.login(a, function (er, cr, data) {
81 if (er || !couch.valid(couch.token)) {
82 er = er || new Error('login error')
83 return cb(er, cr, data)
85 this.conf.set('_token', this.couchLogin.token)
86 return regRequest.call(this,
88 etag, nofollow, reauthed, cb_)
93 // now we either have a valid token, or an auth.
95 if (authRequired && !auth && !token) {
97 "Cannot insert data into the registry without auth"))
100 if (auth && !token) {
101 remote.auth = new Buffer(auth, "base64").toString("utf8")
104 // Tuned to spread 3 attempts over about a minute.
105 // See formula at <https://github.com/tim-kos/node-retry>.
106 var operation = retry.operation({
107 retries: this.conf.get('fetch-retries') || 2,
108 factor: this.conf.get('fetch-retry-factor'),
109 minTimeout: this.conf.get('fetch-retry-mintimeout') || 10000,
110 maxTimeout: this.conf.get('fetch-retry-maxtimeout') || 60000
114 operation.attempt(function (currentAttempt) {
115 self.log.info("trying", "registry request attempt " + currentAttempt
116 + " at " + (new Date()).toLocaleTimeString())
117 makeRequest.call(self, method, remote, where, what, etag, nofollow, token
118 , function (er, parsed, raw, response) {
119 if (!er || er.message.match(/^SSL Error/)) {
122 return cb(er, parsed, raw, response)
125 // Only retry on 408, 5xx or no `response`.
126 var statusCode = response && response.statusCode
128 var reauth = !reauthed &&
129 ( statusCode === 401 ||
130 statusCode === 400 ||
135 var timeout = statusCode === 408
136 var serverError = statusCode >= 500
137 var statusRetry = !statusCode || timeout || serverError
138 if (reauth && this.conf.get('_auth') && this.conf.get('_token')) {
139 this.conf.del('_token')
140 this.couchLogin.token = null
141 return regRequest.call(this, method, where, what,
142 etag, nofollow, reauthed, cb_)
144 if (er && statusRetry && operation.retry(er)) {
145 self.log.info("retry", "will retry, error on last attempt: " + er)
148 cb.apply(null, arguments)
153 function makeRequest (method, remote, where, what, etag, nofollow, tok, cb_) {
158 cb_.apply(null, arguments)
161 var strict = this.conf.get('strict-ssl')
162 if (strict === undefined) strict = true
163 var opts = { url: remote
165 , ca: this.conf.get('ca')
166 , strictSSL: strict }
167 , headers = opts.headers = {}
169 this.log.verbose("etag", etag)
170 headers[method === "GET" ? "if-none-match" : "if-match"] = etag
174 headers.cookie = 'AuthSession=' + tok.AuthSession
177 headers.accept = "application/json"
179 headers["user-agent"] = this.conf.get('user-agent') ||
180 'node/' + process.version
182 var p = this.conf.get('proxy')
183 var sp = this.conf.get('https-proxy') || p
184 opts.proxy = remote.protocol === "https:" ? sp : p
186 // figure out wth 'what' is
188 if (Buffer.isBuffer(what) || typeof what === "string") {
190 headers["content-type"] = "application/json"
191 headers["content-length"] = Buffer.byteLength(what)
192 } else if (what instanceof Stream) {
193 headers["content-type"] = "application/octet-stream"
194 if (what.size) headers["content-length"] = what.size
202 opts.followRedirect = false
205 this.log.http(method, remote.href || "/")
207 var done = requestDone.call(this, method, where, cb)
208 var req = request(opts, done)
211 req.on("socket", function (s) {
215 if (what && (what instanceof Stream)) {
220 // cb(er, parsed, raw, response)
221 function requestDone (method, where, cb) {
222 return function (er, response, data) {
223 if (er) return cb(er)
225 var urlObj = url.parse(where)
228 this.log.http(response.statusCode, url.format(urlObj))
232 if (Buffer.isBuffer(data)) {
233 data = data.toString()
236 if (data && typeof data === "string" && response.statusCode !== 304) {
238 parsed = JSON.parse(data)
240 ex.message += "\n" + data
241 this.log.verbose("bad json", data)
242 this.log.error("registry", "error parsing json")
243 return cb(ex, null, data, response)
247 data = JSON.stringify(parsed)
250 // expect data with any error codes
251 if (!data && response.statusCode >= 400) {
252 return cb( response.statusCode + " "
253 + require("http").STATUS_CODES[response.statusCode]
254 , null, data, response )
258 if (parsed && response.headers.etag) {
259 parsed._etag = response.headers.etag
262 if (parsed && parsed.error && response.statusCode >= 400) {
263 var w = url.parse(where).pathname.substr(1)
265 if (!w.match(/^-/) && parsed.error === "not_found") {
267 name = w[w.indexOf("_rewrite") + 1]
268 er = new Error("404 Not Found: "+name)
273 parsed.error + " " + (parsed.reason || "") + ": " + w)
275 } else if (method !== "HEAD" && method !== "GET") {
277 // This is irrelevant for commands that do etag caching, but
278 // ls and view also have a timed cache, so this keeps the user
279 // from thinking that it didn't work when it did.
280 // Note that failure is an acceptable option here, since the
281 // only result will be a stale cache for some helper commands.
282 var path = require("path")
283 , p = url.parse(where).pathname.split("/")
285 , caches = p.map(function (part) {
286 part = part.replace(/:/g, "_")
287 return _ = path.join(_, part)
288 }).map(function (cache) {
289 return path.join(this.conf.get('cache'), cache, ".cache.json")
292 // if the method is DELETE, then also remove the thing itself.
293 // Note that the search index is probably invalid. Whatever.
294 // That's what you get for deleting stuff. Don't do that.
295 if (method === "DELETE") {
296 p = p.slice(0, p.indexOf("-rev"))
297 p = p.join("/").replace(/:/g, "_")
298 caches.push(path.join(this.conf.get('cache'), p))
301 asyncMap(caches, rm, function () {})
303 return cb(er, parsed, data, response)