Permalink
Browse files

Use a npmconf object instead of a plain data object

The better to publishConfig you with.
  • Loading branch information...
1 parent 311832e commit e9538a00ced884a2f0766a474dea6c8f3313ed54 @isaacs isaacs committed Aug 17, 2012
Showing with 96 additions and 117 deletions.
  1. +26 −56 index.js
  2. +18 −18 lib/adduser.js
  3. +12 −12 lib/get.js
  4. +9 −5 lib/publish.js
  5. +28 −23 lib/request.js
  6. +3 −3 test/retries.js
View
@@ -19,78 +19,48 @@ try {
function noop () {}
-function RegClient (options) {
+function RegClient (conf) {
+ // accept either a plain-jane object, or a npmconf object
+ // with a "get" method.
+ if (typeof conf.get !== 'function') {
+ var data = conf
+ conf = { get: function (k) { return data[k] }
+ , set: function (k, v) { data[k] = v }
+ , del: function (k) { delete data[k] } }
+ }
+
+ this.conf = conf
+
// if provided, then the registry needs to be a url.
// if it's not provided, then we're just using the cache only.
- var registry = options.registry
+ var registry = conf.get('registry')
if (registry) {
registry = url.parse(registry)
if (!registry.protocol) throw new Error(
'Invalid registry: ' + registry.url)
- this.registry = registry.href
- if (this.registry.slice(-1) !== '/') {
- this.registry += '/'
+ registry = registry.href
+ if (registry.slice(-1) !== '/') {
+ registry += '/'
}
+ this.conf.set('registry', registry)
} else {
- this.registry = null
+ registry = null
}
- this.retries = options.retries || 2
- this.retryFactor = options.retryFactor || 10
- this.retryMinTimeout = options.retryMinTimeout || 10000
- this.retryMaxTimeout = options.retryMaxTimeout || 60000
-
- this.cache = options.cache
- if (!this.cache) throw new Error("Cache dir is required")
-
- this.alwaysAuth = options.alwaysAuth || false
+ if (!conf.get('cache')) throw new Error("Cache dir is required")
- this.auth = options.auth || null
- if (this.auth) {
- var a = new Buffer(this.auth, "base64").toString()
- a = a.split(":")
- this.username = a.shift()
- this.password = a.join(":")
- } else {
- this.username = options.username
- this.password = options.password
-
- // if username and password are set, but auth isn't, use them.
- if (this.username && this.password) {
- var a = this.username + ":" + this.password
- this.auth = new Buffer(a, "utf8").toString("base64")
- }
- }
-
- if (this.auth && !this.alwaysAuth && this.registry) {
+ var auth = this.conf.get('_auth')
+ var alwaysAuth = this.conf.get('always-auth')
+ if (auth && !alwaysAuth && registry) {
// if we're always authing, then we just send the
// user/pass on every thing. otherwise, create a
// session, and use that.
- this.token = options.token
- this.couchLogin = new CouchLogin(this.registry, this.token)
- this.couchLogin.proxy = this.proxy
- }
-
- this.email = options.email || null
- this.defaultTag = options.tag || "latest"
-
- this.ca = options.ca || null
-
- this.strictSSL = options.strictSSL
- if (this.strictSSL === undefined) this.strictSSL = true
-
- this.userAgent = options.userAgent
- if (this.userAgent === undefined) {
- this.userAgent = 'node/' + process.version
+ var token = this.conf.get('_token')
+ this.couchLogin = new CouchLogin(registry, token)
+ this.couchLogin.proxy = this.conf.get('proxy')
}
- this.cacheMin = options.cacheMin || 0
- this.cacheMax = options.cacheMax || Infinity
-
- this.proxy = options.proxy
- this.httpsProxy = options.httpsProxy || options.proxy
-
- this.log = options.log || npmlog
+ this.log = conf.log || conf.get('log') || npmlog
}
require('fs').readdirSync(__dirname + "/lib").forEach(function (f) {
View
@@ -41,18 +41,18 @@ function adduser (username, password, email, cb) {
// pluck off any other username/password/token. it needs to be the
// same as the user we're becoming now. replace them on error.
- var pre = { username: this.username
- , password: this.password
- , auth: this.auth
- , token: this.token }
-
- this.token = null
+ var pre = { username: this.conf.get('username')
+ , password: this.conf.get('_password')
+ , auth: this.conf.get('_auth')
+ , token: this.conf.get('_token') }
+
+ this.conf.del('_token')
+ this.conf.del('username')
+ this.conf.del('_auth')
+ this.conf.del('_password')
if (this.couchLogin) {
this.couchLogin.token = null
}
- this.username = null
- this.password = null
- this.auth = null
cb = done.call(this, cb, pre)
@@ -72,13 +72,13 @@ function adduser (username, password, email, cb) {
, 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
- if (error && !this.auth) {
+ if (error && !this.conf.get('_auth')) {
// must be trying to re-auth on a new machine.
// use this info as auth
var b = new Buffer(username + ":" + password)
- this.auth = b.toString("base64")
- this.username = username
- this.password = password
+ this.conf.set('_auth', b.toString("base64"))
+ this.conf.set('username', username)
+ this.conf.set('_password', password)
}
if (!error || !response || response.statusCode !== 409) {
@@ -114,16 +114,16 @@ function done (cb, pre) {
}
// there was some kind of error, re-instate previous auth/token/etc.
- this.token = pre.token
+ this.conf.set('_token', pre.token)
if (this.couchLogin) {
- this.couchLogin.token = this.token
+ this.couchLogin.token = pre.token
if (this.couchLogin.tokenSet) {
this.couchLogin.tokenSet(pre.token)
}
}
- this.username = pre.username
- this.password = pre.password
- this.auth = pre.auth
+ this.conf.set('username', pre.username)
+ this.conf.set('_password', pre.password)
+ this.conf.set('_auth', pre.auth)
this.log.verbose("adduser", "back", [error, data, json])
if (!error) {
View
@@ -12,10 +12,10 @@ function get (uri, timeout, nofollow, staleOk, cb) {
if (typeof cb !== "function") cb = timeout, timeout = -1
if (typeof cb !== "function") cb = version, version = null
- timeout = Math.min(timeout, this.cacheMax)
- timeout = Math.max(timeout, this.cacheMin)
+ timeout = Math.min(timeout, this.conf.get('cache-max') || 0)
+ timeout = Math.max(timeout, this.conf.get('cache-min') || Infinity)
- if (!this.registry) timeout = Infinity
+ if (!this.conf.get('registry')) timeout = Infinity
if ( process.env.COMP_CWORD !== undefined
&& process.env.COMP_LINE !== undefined
@@ -29,7 +29,7 @@ function get (uri, timeout, nofollow, staleOk, cb) {
return requestAll.call(this, cb)
}
- var cache = path.join(this.cache, uri, ".cache.json")
+ var cache = path.join(this.conf.get('cache'), uri, ".cache.json")
fs.stat(cache, function (er, stat) {
if (!er) fs.readFile(cache, function (er, data) {
try { data = JSON.parse(data) }
@@ -41,9 +41,9 @@ function get (uri, timeout, nofollow, staleOk, cb) {
}
function requestAll (cb) {
- var cache = path.join(this.cache, "/-/all", ".cache.json")
+ var cache = path.join(this.conf.get('cache'), "/-/all", ".cache.json")
- mkdir(path.join(this.cache, "-", "all"), function (er) {
+ mkdir(path.join(this.conf.get('cache'), "-", "all"), function (er) {
fs.readFile(cache, function (er, data) {
if (er) return requestAll_.call(this, 0, {}, cb)
try {
@@ -74,7 +74,7 @@ function requestAll_ (c, data, cb) {
uri = "/-/all"
}
- var cache = path.join(this.cache, "-/all", ".cache.json")
+ var cache = path.join(this.conf.get('cache'), "-/all", ".cache.json")
this.request('GET', uri, function (er, updates, _, res) {
if (er) return cb(er, data)
var headers = res.headers
@@ -143,20 +143,20 @@ function get_ (uri, timeout, cache, stat, data, nofollow, staleOk, cb) {
}
function saveToCache (cache, data, saved) {
- if (this.cacheStat) {
- var cs = this.cacheStat
+ if (this._cacheStat) {
+ var cs = this._cacheStat
return saveToCache_.call(this, cache, data, cs.uid, cs.gid, saved)
}
- fs.stat(this.cache, function (er, st) {
+ fs.stat(this.conf.get('cache'), function (er, st) {
if (er) {
return fs.stat(process.env.HOME || "", function (er, st) {
// if this fails, oh well.
if (er) return saved()
- this.cacheStat = st
+ this._cacheStat = st
return saveToCache.call(this, cache, data, saved)
}.bind(this))
}
- this.cacheStat = st || { uid: null, gid: null }
+ this._cacheStat = st || { uid: null, gid: null }
return saveToCache.call(this, cache, data, saved)
}.bind(this))
}
View
@@ -6,7 +6,11 @@ var path = require("path")
function publish (data, tarball, cb) {
- if (!this.email || !this.auth || !this.username) {
+ var email = this.conf.get('email')
+ var auth = this.conf.get('_auth')
+ var username = this.conf.get('username')
+
+ if (!email || !auth || !username) {
return cb(new Error("auth and email required for publishing"))
}
@@ -15,7 +19,7 @@ function publish (data, tarball, cb) {
// if the {version} is already there, then fail.
// then:
// PUT the data to {config.registry}/{data.name}/{data.version}
- var registry = this.registry
+ var registry = this.conf.get('registry')
var fullData =
{ _id : data.name
@@ -25,8 +29,8 @@ function publish (data, tarball, cb) {
, versions : {}
, readme: data.readme || ""
, maintainers :
- [ { name : this.username
- , email : this.email
+ [ { name : username
+ , email : email
}
]
}
@@ -58,7 +62,7 @@ function publish (data, tarball, cb) {
var dataURI = encodeURIComponent(data.name)
+ "/" + encodeURIComponent(data.version)
- var tag = data.tag || this.defaultTag || "latest"
+ var tag = data.tag || this.conf.get('tag') || "latest"
dataURI += "/-tag/" + tag
// let's see what versions are already published.
Oops, something went wrong.

0 comments on commit e9538a0

Please sign in to comment.