Permalink
Browse files

pull registry config a level out (WIP)

  • Loading branch information...
1 parent fc241ac commit 16b17484d6bc4115912eebfe2b9e20297e77c868 @othiym23 othiym23 committed May 21, 2014
Showing with 134 additions and 55 deletions.
  1. +12 −11 lib/adduser.js
  2. +5 −1 lib/deprecate.js
  3. +7 −3 lib/get.js
  4. +19 −6 lib/publish.js
  5. +31 −8 lib/request.js
  6. +10 −2 lib/star.js
  7. +5 −1 lib/tag.js
  8. +15 −3 lib/unpublish.js
  9. +7 −2 lib/upload.js
  10. +2 −7 test/adduser-new.js
  11. +2 −9 test/adduser-update.js
  12. +3 −2 test/basic.js
  13. +16 −0 test/lib/common.js
View
@@ -48,10 +48,14 @@ function adduser (username, password, email, cb) {
this.log.verbose("adduser", "before first PUT", logObj)
+ var uri = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ '/-/user/org.couchdb.user:' + encodeURIComponent(username)
+ )
+
this.request('PUT'
- , this.conf.get('registry')
- , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
- , userobj
+ , uri
+ , { body : userobj }
, function (error, data, json, response) {
// if it worked, then we just created a new user, and all is well.
// but if we're updating a current record, then it'll 409 first
@@ -70,9 +74,8 @@ function adduser (username, password, email, cb) {
this.log.verbose("adduser", "update existing user")
return this.request('GET'
- , this.conf.get('registry')
- , '/-/user/org.couchdb.user:'+encodeURIComponent(username) +
- '?write=true'
+ , uri + '?write=true'
+ , null
, function (er, data, json, response) {
if (er || data.error) {
return cb(er, data, json, response)
@@ -84,11 +87,9 @@ function adduser (username, password, email, cb) {
})
this.log.verbose("adduser", "userobj", logObj)
this.request('PUT'
- , this.conf.get('registry')
- , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
- + "/-rev/" + userobj._rev
- , userobj
- , cb )
+ , uri + "/-rev/" + userobj._rev
+ , { body : userobj }
+ , cb)
}.bind(this))
}.bind(this))
}
View
@@ -21,6 +21,10 @@ function deprecate (name, ver, message, cb) {
data.versions[v].deprecated = message
})
// now update the doc on the registry
- this.request('PUT', this.conf.get('registry'), data._id, data, cb)
+ var fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ data._id
+ )
+ this.request('PUT', fixed, { body : data }, cb)
}.bind(this))
}
View
@@ -3,7 +3,6 @@ module.exports = get
var fs = require("graceful-fs")
, path = require("path")
- , url = require("url")
, mkdir = require("mkdirp")
, chownr = require("chownr")
@@ -80,7 +79,7 @@ function requestAll_ (c, data, cache, cb) {
uri = "/-/all"
}
- this.request('GET', uri, function (er, updates, _, res) {
+ 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)
@@ -114,7 +113,12 @@ function get_ (uri, timeout, cache, stat, data, nofollow, staleOk, cb) {
}
}
- this.request('GET', this.conf.get('registry'), uri, null, etag, nofollow, function (er, remoteData, raw, response) {
+ var fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ uri
+ )
+
+ this.request('GET', fixed, { etag : etag, follow : !nofollow }, 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) {
View
@@ -76,7 +76,12 @@ function putFirst (data, tarbuffer, stat, username, email, cb) {
length: stat.size
};
- this.request("PUT", this.conf.get("registry"), data.name, root, function (er, parsed, json, res) {
+ var fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ data.name
+ )
+
+ this.request("PUT", fixed, { body : root }, function (er, parsed, json, res) {
var r409 = "must supply latest _rev to update existing package"
var r409b = "Document update conflict."
var conflict = res && res.statusCode === 409
@@ -94,10 +99,14 @@ function putFirst (data, tarbuffer, stat, username, email, cb) {
return cb(er, parsed, json, res)
// let's see what versions are already published.
- var getUrl = data.name + "?write=true"
- this.request("GET", this.conf.get("registry"), getUrl, function (er, current) {
- if (er)
- return cb(er)
+ var getUrl = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ data.name + "?write=true"
+ )
+
+ this.request("GET", getUrl, null, function (er, current) {
+ if (er) return cb(er)
+
putNext.call(this, data.version, root, current, cb)
}.bind(this))
}.bind(this))
@@ -143,7 +152,11 @@ function putNext(newVersion, root, current, cb) {
var maint = JSON.parse(JSON.stringify(root.maintainers))
root.versions[newVersion].maintainers = maint
- this.request("PUT", this.conf.get("registry"), root.name, current, cb)
+ var putUrl = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ root.name
+ )
+ this.request("PUT", putUrl, { body : current }, cb)
}
function conflictError (pkgid, version) {
View
@@ -1,28 +1,50 @@
module.exports = regRequest
+module.exports.toRegistryURL = toRegistryURL
var url = require("url")
, zlib = require("zlib")
+ , assert = require("assert")
, rm = require("rimraf")
- , asyncMap = require("slide").asyncMap
, Stream = require("stream").Stream
, request = require("request")
, retry = require("retry")
, crypto = require("crypto")
, pkg = require("../package.json")
-function regRequest (method, registry, where, what, etag, nofollow, cb_) {
- if (typeof cb_ !== "function") cb_ = nofollow, nofollow = false
- if (typeof cb_ !== "function") cb_ = etag, etag = null
- if (typeof cb_ !== "function") cb_ = what, what = null
+
+function toRegistryURL(registry, path) {
+ return url.resolve(registry, path)
+}
+
+// npm: means
+// 1. https
+// 2. send authorization
+// 3. content-type is 'application/json' -- metadata
+function regRequest (method, uri, options, cb_) {
+ assert(uri, "must pass resource to load")
+ options = options || {}
+ var nofollow = !options.follow
+ var etag = options.etag
+ var what = options.body
+
+ var parsed = url.parse(uri)
+
+ var where = parsed.pathname
+ if (parsed.search) {
+ where = where + parsed.search
+ parsed.search = ""
+ }
+ parsed.pathname = "/"
+ this.log.verbose("request", "where is", where)
+
+ var registry = url.format(parsed)
+ this.log.verbose("request", "registry is", registry)
if (!this.sessionToken) {
this.sessionToken = crypto.randomBytes(8).toString("hex")
this.log.verbose("request id", this.sessionToken)
}
- if (!registry) return cb(new Error(
- "No registry url provided: " + method + " " + where))
-
// Since there are multiple places where an error could occur,
// don't let the cb be called more than once.
var errState = null
@@ -66,6 +88,7 @@ function regRequest (method, registry, where, what, etag, nofollow, cb_) {
where = url.resolve(registry, where)
this.log.verbose("url resolved", where)
}
+ this.log.verbose("request", "where is", where)
var remote = url.parse(where)
, auth = this.conf.get('_auth')
View
@@ -5,7 +5,11 @@ function star (package, starred, cb) {
if (!this.conf.get('username')) return cb(new Error(
"Must be logged in to star/unstar packages"))
- this.request("GET", this.conf.get("registry"), package + '?write=true', function (er, fullData) {
+ var fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ package + '?write=true'
+ )
+ this.request("GET", fixed, null, function (er, fullData) {
if (er) return cb(er)
fullData = { _id: fullData._id
@@ -22,6 +26,10 @@ function star (package, starred, cb) {
this.log.verbose("unstarring", fullData)
}
- return this.request("PUT", this.conf.get("registry"), package, fullData, cb)
+ var fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ package
+ )
+ return this.request("PUT", fixed, { body : fullData }, cb)
}.bind(this))
}
View
@@ -2,5 +2,9 @@
module.exports = tag
function tag (project, version, tagName, cb) {
- this.request("PUT", this.conf.get("registry"), project+"/"+tagName, JSON.stringify(version), cb)
+ var fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ project + "/" + tagName
+ )
+ this.request("PUT", fixed, { body : JSON.stringify(version) }, cb)
}
View
@@ -39,9 +39,15 @@ function unpublish (name, ver, cb) {
delete versions[ver]
// if it was the only version, then delete the whole package.
+ var fixed
if (!Object.keys(versions).length) {
this.log.info("unpublish", "No versions remain, removing entire package")
- return this.request("DELETE", this.conf.get("registry"), name+"/-rev/"+data._rev, cb)
+ fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ name + "/-rev/" + data._rev
+ )
+
+ return this.request("DELETE", fixed, null, cb)
}
if (!versionPublic) return cb()
@@ -60,7 +66,12 @@ function unpublish (name, ver, cb) {
delete data._revisions
delete data._attachments
var cb_ = detacher.call(this, data, dist, cb)
- this.request("PUT", this.conf.get("registry"), name+"/-rev/"+rev, data, function (er) {
+ fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ name + "/-rev/" + rev
+ )
+
+ this.request("PUT", fixed, { body : data }, function (er) {
if (er) {
this.log.error("unpublish", "Failed to update data")
}
@@ -94,7 +105,8 @@ function detach (data, path, rev, cb) {
if (rev) {
path += "/-rev/" + rev
this.log.info("detach", path)
- return this.request("DELETE", this.conf.get("registry"), path, cb)
+ var fixed = this.request.toRegistryURL(this.conf.get('registry'), path)
+ return this.request("DELETE", fixed, null, cb)
}
this.get(data.name, function (er, data) {
rev = data._rev
View
@@ -7,8 +7,13 @@ function upload (where, file, etag, nofollow, cb) {
if (typeof nofollow === "function") cb = nofollow, nofollow = false
if (typeof etag === "function") cb = etag, etag = null
+ var fixed = this.request.toRegistryURL(
+ this.conf.get('registry'),
+ where
+ )
+
if (file instanceof Stream) {
- return this.request("PUT", this.conf.get("registry"), where, file, etag, nofollow, cb)
+ return this.request("PUT", fixed, { body : file, etag : etag, follow : !nofollow }, cb)
}
fs.stat(file, function (er, stat) {
@@ -17,6 +22,6 @@ function upload (where, file, etag, nofollow, cb) {
s.size = stat.size
s.on("error", cb)
- this.request("PUT", this.conf.get("registry"), where, s, etag, nofollow, cb)
+ this.request("PUT", fixed, { body : s, etag : etag, follow : !nofollow }, cb)
}.bind(this))
}
View
@@ -12,13 +12,8 @@ var userdata =
date: '2012-06-07T04:11:21.591Z' }
, password = "password"
, username = "username"
-, crypto = require("crypto")
, SD = require('string_decoder').StringDecoder
-, decoder = new SD
-
-function sha (s) {
- return crypto.createHash("sha1").update(s).digest("hex")
-}
+, decoder = new SD()
tap.test("create new user account", function (t) {
server.expect("/-/user/org.couchdb.user:username", function (req, res) {
@@ -39,7 +34,7 @@ tap.test("create new user account", function (t) {
})
})
- client.adduser(username, password, "i@izs.me", function (er, data, raw, res) {
+ client.adduser(username, password, "i@izs.me", function (er, data) {
if (er) throw er
t.deepEqual(data, { created: true })
t.end()
View
@@ -14,15 +14,10 @@ var userdata =
, password = "password"
, username = "username"
-, crypto = require("crypto")
, SD = require('string_decoder').StringDecoder
-, decoder = new SD
+, decoder = new SD()
-function sha (s) {
- return crypto.createHash("sha1").update(s).digest("hex")
-}
-
tap.test("update a user acct", function (t) {
server.expect("PUT", "/-/user/org.couchdb.user:username", function (req, res) {
t.equal(req.method, "PUT")
@@ -54,9 +49,7 @@ tap.test("update a user acct", function (t) {
})
})
-
-
- client.adduser(username, password, "i@izs.me", function (er, data, raw, res) {
+ client.adduser(username, password, "i@izs.me", function (er, data) {
if (er) throw er
t.deepEqual(data, { created: true })
t.end()
View
@@ -4,6 +4,7 @@ var server = require('./fixtures/server.js')
var client = common.freshClient()
var us = require('./fixtures/underscore/1.3.3/cache.json')
var usroot = require("./fixtures/underscore/cache.json")
+client.log.level = "verbose"
tap.test("basic request", function (t) {
server.expect("/underscore/1.3.3", function (req, res) {
@@ -17,12 +18,12 @@ tap.test("basic request", function (t) {
})
t.plan(2)
- client.get("/underscore/1.3.3", function (er, data, raw, res) {
+ client.get("/underscore/1.3.3", function (er, data) {
console.error("got response")
t.deepEqual(data, us)
})
- client.get("/underscore", function (er, data, raw, res) {
+ client.get("/underscore", function (er, data) {
console.error("got response")
t.deepEqual(data, usroot)
})
View
@@ -0,0 +1,16 @@
+var resolve = require("path").resolve
+var server = require('../fixtures/server.js')
+var RC = require('../../')
+
+module.exports = {
+ freshClient : function freshClient(config) {
+ config = config || {}
+ config.cache = resolve(__dirname, '..', '/fixtures/cache')
+ config.registry = 'http://localhost:' + server.port
+
+ var client = new RC(config)
+ client.log.level = 'error'
+
+ return client
+ }
+}

0 comments on commit 16b1748

Please sign in to comment.