Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
switch API to use URLs instead of names
Browse files Browse the repository at this point in the history
  • Loading branch information
othiym23 committed Jun 6, 2014
1 parent 283fc4f commit c96fc63
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 138 deletions.
14 changes: 8 additions & 6 deletions lib/adduser.js
@@ -1,6 +1,12 @@
module.exports = adduser module.exports = adduser


function adduser (username, password, email, cb) { var url = require("url")

function adduser (base, username, password, email, cb) {
if (!base) return cb(new Error("Required base URI not supplied"))

username = ("" + (username || "")).trim()
if (!username) return cb(new Error("No username supplied."))


password = ("" + (password || "")).trim() password = ("" + (password || "")).trim()
if (!password) return cb(new Error("No password supplied.")) if (!password) return cb(new Error("No password supplied."))
Expand Down Expand Up @@ -48,11 +54,7 @@ function adduser (username, password, email, cb) {


this.log.verbose("adduser", "before first PUT", logObj) this.log.verbose("adduser", "before first PUT", logObj)


var uri = this.request.toRegistryURL( var uri = url.resolve(base, '/-/user/org.couchdb.user:' + encodeURIComponent(username))
this.conf.get('registry'),
'/-/user/org.couchdb.user:' + encodeURIComponent(username)
)

this.request('PUT' this.request('PUT'
, uri , uri
, { body : userobj } , { body : userobj }
Expand Down
12 changes: 4 additions & 8 deletions lib/deprecate.js
@@ -1,9 +1,9 @@

module.exports = deprecate module.exports = deprecate


var url = require("url")
var semver = require("semver") var semver = require("semver")


function deprecate (name, ver, message, cb) { function deprecate (uri, ver, message, cb) {
if (!this.conf.get('username')) { if (!this.conf.get('username')) {
return cb(new Error("Must be logged in to deprecate a package")) return cb(new Error("Must be logged in to deprecate a package"))
} }
Expand All @@ -12,7 +12,7 @@ function deprecate (name, ver, message, cb) {
return cb(new Error("invalid version range: "+ver)) return cb(new Error("invalid version range: "+ver))
} }


this.get(name + '?write=true', function (er, data) { this.get(uri + '?write=true', function (er, data) {
if (er) return cb(er) if (er) return cb(er)
// filter all the versions that match // filter all the versions that match
Object.keys(data.versions).filter(function (v) { Object.keys(data.versions).filter(function (v) {
Expand All @@ -21,10 +21,6 @@ function deprecate (name, ver, message, cb) {
data.versions[v].deprecated = message data.versions[v].deprecated = message
}) })
// now update the doc on the registry // now update the doc on the registry
var fixed = this.request.toRegistryURL( this.request('PUT', url.resolve(uri, data._id), { body : data }, cb)
this.conf.get('registry'),
data._id
)
this.request('PUT', fixed, { body : data }, cb)
}.bind(this)) }.bind(this))
} }
14 changes: 4 additions & 10 deletions lib/get.js
Expand Up @@ -5,6 +5,7 @@ var fs = require("graceful-fs")
, path = require("path") , path = require("path")
, mkdir = require("mkdirp") , mkdir = require("mkdirp")
, chownr = require("chownr") , chownr = require("chownr")
, url = require("url")


function get (uri, timeout, nofollow, staleOk, cb) { function get (uri, timeout, nofollow, staleOk, cb) {
if (typeof cb !== "function") cb = staleOk, staleOk = false if (typeof cb !== "function") cb = staleOk, staleOk = false
Expand All @@ -14,19 +15,17 @@ function get (uri, timeout, nofollow, staleOk, cb) {
timeout = Math.min(timeout, this.conf.get('cache-max') || 0) timeout = Math.min(timeout, this.conf.get('cache-max') || 0)
timeout = Math.max(timeout, this.conf.get('cache-min') || -Infinity) timeout = Math.max(timeout, this.conf.get('cache-min') || -Infinity)


if (!this.conf.get('registry')) timeout = Infinity

if ( process.env.COMP_CWORD !== undefined if ( process.env.COMP_CWORD !== undefined
&& process.env.COMP_LINE !== undefined && process.env.COMP_LINE !== undefined
&& process.env.COMP_POINT !== undefined && process.env.COMP_POINT !== undefined
) timeout = Math.max(timeout, 60000) ) timeout = Math.max(timeout, 60000)


var cache = this.cacheFile(this.registry + uri) + "/.cache.json" var cache = this.cacheFile(uri) + "/.cache.json"


// /-/all is special. // /-/all is special.
// It uses timestamp-based caching and partial updates, // It uses timestamp-based caching and partial updates,
// because it is a monster. // because it is a monster.
if (uri === "/-/all") { if (url.parse(uri).pathname === "/-/all") {
return requestAll.call(this, cache, cb) return requestAll.call(this, cache, cb)
} }


Expand Down Expand Up @@ -113,12 +112,7 @@ function get_ (uri, timeout, cache, stat, data, nofollow, staleOk, cb) {
} }
} }


var fixed = this.request.toRegistryURL( this.request('GET', uri, { etag : etag, follow : !nofollow }, function (er, remoteData, raw, response) {
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 // if we get an error talking to the registry, but we have it
// from the cache, then just pretend we got it. // from the cache, then just pretend we got it.
if (er && cache && data && !data.error) { if (er && cache && data && !data.error) {
Expand Down
29 changes: 8 additions & 21 deletions lib/publish.js
Expand Up @@ -6,7 +6,7 @@ var url = require("url")
, crypto = require("crypto") , crypto = require("crypto")
, fs = require("fs") , fs = require("fs")


function publish (data, tarball, cb) { function publish (uri, data, tarball, cb) {
var email = this.conf.get('email') var email = this.conf.get('email')
var auth = this.conf.get('_auth') var auth = this.conf.get('_auth')
var username = this.conf.get('username') var username = this.conf.get('username')
Expand All @@ -30,12 +30,12 @@ function publish (data, tarball, cb) {
if (er) return cb(er) if (er) return cb(er)
fs.readFile(tarball, function(er, tarbuffer) { fs.readFile(tarball, function(er, tarbuffer) {
if (er) return cb(er) if (er) return cb(er)
putFirst.call(self, data, tarbuffer, s, username, email, cb) putFirst.call(self, uri, data, tarbuffer, s, username, email, cb)
}) })
}) })
} }


function putFirst (data, tarbuffer, stat, username, email, cb) { function putFirst (registry, data, tarbuffer, stat, username, email, cb) {
// optimistically try to PUT all in one single atomic thing. // optimistically try to PUT all in one single atomic thing.
// If 409, then GET and merge, try again. // If 409, then GET and merge, try again.
// If other error, then fail. // If other error, then fail.
Expand All @@ -59,7 +59,6 @@ function putFirst (data, tarbuffer, stat, username, email, cb) {
var tag = data.tag || this.conf.get('tag') || "latest" var tag = data.tag || this.conf.get('tag') || "latest"
root["dist-tags"][tag] = data.version root["dist-tags"][tag] = data.version


var registry = this.conf.get("registry")
var tbName = data.name + "-" + data.version + ".tgz" var tbName = data.name + "-" + data.version + ".tgz"
, tbURI = data.name + "/-/" + tbName , tbURI = data.name + "/-/" + tbName


Expand All @@ -76,11 +75,7 @@ function putFirst (data, tarbuffer, stat, username, email, cb) {
length: stat.size length: stat.size
}; };


var fixed = this.request.toRegistryURL( var fixed = url.resolve(registry, data.name)
this.conf.get('registry'),
data.name
)

this.request("PUT", fixed, { body : root }, function (er, parsed, json, res) { this.request("PUT", fixed, { body : root }, function (er, parsed, json, res) {
var r409 = "must supply latest _rev to update existing package" var r409 = "must supply latest _rev to update existing package"
var r409b = "Document update conflict." var r409b = "Document update conflict."
Expand All @@ -99,20 +94,16 @@ function putFirst (data, tarbuffer, stat, username, email, cb) {
return cb(er, parsed, json, res) return cb(er, parsed, json, res)


// let's see what versions are already published. // let's see what versions are already published.
var getUrl = this.request.toRegistryURL( var getUrl = url.resolve(registry, data.name + "?write=true")
this.conf.get('registry'),
data.name + "?write=true"
)

this.request("GET", getUrl, null, function (er, current) { this.request("GET", getUrl, null, function (er, current) {
if (er) return cb(er) if (er) return cb(er)


putNext.call(this, data.version, root, current, cb) putNext.call(this, registry, data.version, root, current, cb)
}.bind(this)) }.bind(this))
}.bind(this)) }.bind(this))
} }


function putNext(newVersion, root, current, cb) { function putNext(registry, newVersion, root, current, cb) {
// already have the tardata on the root object // already have the tardata on the root object
// just merge in existing stuff // just merge in existing stuff
var curVers = Object.keys(current.versions || {}).map(function (v) { var curVers = Object.keys(current.versions || {}).map(function (v) {
Expand Down Expand Up @@ -152,11 +143,7 @@ function putNext(newVersion, root, current, cb) {
var maint = JSON.parse(JSON.stringify(root.maintainers)) var maint = JSON.parse(JSON.stringify(root.maintainers))
root.versions[newVersion].maintainers = maint root.versions[newVersion].maintainers = maint


var putUrl = this.request.toRegistryURL( this.request("PUT", url.resolve(registry, root.name), { body : current }, cb)
this.conf.get('registry'),
root.name
)
this.request("PUT", putUrl, { body : current }, cb)
} }


function conflictError (pkgid, version) { function conflictError (pkgid, version) {
Expand Down
14 changes: 3 additions & 11 deletions lib/star.js
@@ -1,15 +1,11 @@


module.exports = star module.exports = star


function star (package, starred, cb) { function star (uri, starred, cb) {
if (!this.conf.get('username')) return cb(new Error( if (!this.conf.get('username')) return cb(new Error(
"Must be logged in to star/unstar packages")) "Must be logged in to star/unstar packages"))


var fixed = this.request.toRegistryURL( this.request("GET", uri+"?write=true", null, function (er, fullData) {
this.conf.get('registry'),
package + '?write=true'
)
this.request("GET", fixed, null, function (er, fullData) {
if (er) return cb(er) if (er) return cb(er)


fullData = { _id: fullData._id fullData = { _id: fullData._id
Expand All @@ -26,10 +22,6 @@ function star (package, starred, cb) {
this.log.verbose("unstarring", fullData) this.log.verbose("unstarring", fullData)
} }


var fixed = this.request.toRegistryURL( return this.request("PUT", uri, { body : fullData }, cb)
this.conf.get('registry'),
package
)
return this.request("PUT", fixed, { body : fullData }, cb)
}.bind(this)) }.bind(this))
} }
9 changes: 2 additions & 7 deletions lib/tag.js
@@ -1,10 +1,5 @@

module.exports = tag module.exports = tag


function tag (project, version, tagName, cb) { function tag (uri, version, tagName, cb) {
var fixed = this.request.toRegistryURL( this.request("PUT", uri+"/"+tagName, { body : JSON.stringify(version) }, cb)
this.conf.get('registry'),
project + "/" + tagName
)
this.request("PUT", fixed, { body : JSON.stringify(version) }, cb)
} }
42 changes: 15 additions & 27 deletions lib/unpublish.js
Expand Up @@ -11,43 +11,36 @@ var semver = require("semver")
, url = require("url") , url = require("url")
, chain = require("slide").chain , chain = require("slide").chain


function unpublish (name, ver, cb) { function unpublish (uri, ver, cb) {
if (typeof cb !== "function") cb = ver, ver = null if (typeof cb !== "function") cb = ver, ver = null


var u = name + '?write=true' this.get(uri + "?write=true", null, -1, true, function (er, data) {
this.get(u, null, -1, true, function (er, data) {
if (er) { if (er) {
this.log.info("unpublish", name+" not published") this.log.info("unpublish", uri+" not published")
return cb() return cb()
} }
// remove all if no version specified // remove all if no version specified
if (!ver) { if (!ver) {
this.log.info("unpublish", "No version specified, removing all") this.log.info("unpublish", "No version specified, removing all")
return this.request("DELETE", this.conf.get("registry"), name+'/-rev/'+data._rev, cb) return this.request("DELETE", uri+'/-rev/'+data._rev, cb)
} }


var versions = data.versions || {} var versions = data.versions || {}
, versionPublic = versions.hasOwnProperty(ver) , versionPublic = versions.hasOwnProperty(ver)


var dist var dist
if (!versionPublic) { if (!versionPublic) {
this.log.info("unpublish", name+"@"+ver+" not published") this.log.info("unpublish", uri+"@"+ver+" not published")
} else { } else {
dist = versions[ver].dist dist = versions[ver].dist
this.log.verbose("unpublish", "removing attachments", dist) this.log.verbose("unpublish", "removing attachments", dist)
} }


delete versions[ver] delete versions[ver]
// if it was the only version, then delete the whole package. // if it was the only version, then delete the whole package.
var fixed
if (!Object.keys(versions).length) { if (!Object.keys(versions).length) {
this.log.info("unpublish", "No versions remain, removing entire package") this.log.info("unpublish", "No versions remain, removing entire package")
fixed = this.request.toRegistryURL( return this.request("DELETE", uri + "/-rev/" + data._rev, null, cb)
this.conf.get('registry'),
name + "/-rev/" + data._rev
)

return this.request("DELETE", fixed, null, cb)
} }


if (!versionPublic) return cb() if (!versionPublic) return cb()
Expand All @@ -65,13 +58,9 @@ function unpublish (name, ver, cb) {
var rev = data._rev var rev = data._rev
delete data._revisions delete data._revisions
delete data._attachments delete data._attachments
var cb_ = detacher.call(this, data, dist, cb) var cb_ = detacher.call(this, uri, data, dist, cb)
fixed = this.request.toRegistryURL(
this.conf.get('registry'),
name + "/-rev/" + rev
)


this.request("PUT", fixed, { body : data }, function (er) { this.request("PUT", uri + "/-rev/" + rev, { body : data }, function (er) {
if (er) { if (er) {
this.log.error("unpublish", "Failed to update data") this.log.error("unpublish", "Failed to update data")
} }
Expand All @@ -80,35 +69,34 @@ function unpublish (name, ver, cb) {
}.bind(this)) }.bind(this))
} }


function detacher (data, dist, cb) { function detacher (uri, data, dist, cb) {
return function (er) { return function (er) {
if (er) return cb(er) if (er) return cb(er)
this.get(data.name, function (er, data) { this.get(url.resolve(uri, data.name), function (er, data) {
if (er) return cb(er) if (er) return cb(er)


var tb = url.parse(dist.tarball) var tb = url.parse(dist.tarball)


detach.call(this, data, tb.pathname, data._rev, function (er) { detach.call(this, uri, data, tb.pathname, data._rev, function (er) {
if (er || !dist.bin) return cb(er) if (er || !dist.bin) return cb(er)
chain(Object.keys(dist.bin).map(function (bt) { chain(Object.keys(dist.bin).map(function (bt) {
return function (cb) { return function (cb) {
var d = dist.bin[bt] var d = dist.bin[bt]
detach.call(this, data, url.parse(d.tarball).pathname, null, cb) detach.call(this, uri, data, url.parse(d.tarball).pathname, null, cb)
}.bind(this) }.bind(this)
}, this), cb) }, this), cb)
}.bind(this)) }.bind(this))
}.bind(this)) }.bind(this))
}.bind(this) }.bind(this)
} }


function detach (data, path, rev, cb) { function detach (uri, data, path, rev, cb) {
if (rev) { if (rev) {
path += "/-rev/" + rev path += "/-rev/" + rev
this.log.info("detach", path) this.log.info("detach", path)
var fixed = this.request.toRegistryURL(this.conf.get('registry'), path) return this.request("DELETE", url.resolve(uri, path), null, cb)
return this.request("DELETE", fixed, null, cb)
} }
this.get(data.name, function (er, data) { this.get(url.resolve(uri, data.name), function (er, data) {
rev = data._rev rev = data._rev
if (!rev) return cb(new Error( if (!rev) return cb(new Error(
"No _rev found in "+data._id)) "No _rev found in "+data._id))
Expand Down
11 changes: 3 additions & 8 deletions lib/upload.js
Expand Up @@ -3,17 +3,12 @@ module.exports = upload
var fs = require('fs') var fs = require('fs')
, Stream = require("stream").Stream , Stream = require("stream").Stream


function upload (where, file, etag, nofollow, cb) { function upload (uri, file, etag, nofollow, cb) {
if (typeof nofollow === "function") cb = nofollow, nofollow = false if (typeof nofollow === "function") cb = nofollow, nofollow = false
if (typeof etag === "function") cb = etag, etag = null if (typeof etag === "function") cb = etag, etag = null


var fixed = this.request.toRegistryURL(
this.conf.get('registry'),
where
)

if (file instanceof Stream) { if (file instanceof Stream) {
return this.request("PUT", fixed, { body : file, etag : etag, follow : !nofollow }, cb) return this.request("PUT", uri, { body : file, etag : etag, follow : !nofollow }, cb)
} }


fs.stat(file, function (er, stat) { fs.stat(file, function (er, stat) {
Expand All @@ -22,6 +17,6 @@ function upload (where, file, etag, nofollow, cb) {
s.size = stat.size s.size = stat.size
s.on("error", cb) s.on("error", cb)


this.request("PUT", fixed, { body : s, etag : etag, follow : !nofollow }, cb) this.request("PUT", uri, { body : s, etag : etag, follow : !nofollow }, cb)
}.bind(this)) }.bind(this))
} }
13 changes: 7 additions & 6 deletions test/adduser-new.js
Expand Up @@ -4,15 +4,16 @@ var server = require("./lib/server.js")
var common = require("./lib/common.js") var common = require("./lib/common.js")
var client = common.freshClient() var client = common.freshClient()


var userdata = var password = "password"
{ name: "username", , username = "username"
email: "i@izs.me", , email = "i@izs.me"
, userdata = {
name: username,
email: email,
_id: "org.couchdb.user:username", _id: "org.couchdb.user:username",
type: "user", type: "user",
roles: [], roles: [],
date: "2012-06-07T04:11:21.591Z" } date: "2012-06-07T04:11:21.591Z" }
, password = "password"
, username = "username"
, SD = require("string_decoder").StringDecoder , SD = require("string_decoder").StringDecoder
, decoder = new SD() , decoder = new SD()


Expand All @@ -35,7 +36,7 @@ tap.test("create new user account", function (t) {
}) })
}) })


client.adduser(username, password, "i@izs.me", function (er, data) { client.adduser("http://localhost:1337/", username, password, email, function (er, data) {
if (er) throw er if (er) throw er
t.deepEqual(data, { created: true }) t.deepEqual(data, { created: true })
t.end() t.end()
Expand Down

0 comments on commit c96fc63

Please sign in to comment.