Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use a npmconf object instead of a plain data object

The better to publishConfig you with.
  • Loading branch information...
commit e9538a00ced884a2f0766a474dea6c8f3313ed54 1 parent 311832e
@isaacs isaacs authored
View
82 index.js
@@ -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
36 lib/adduser.js
@@ -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
24 lib/get.js
@@ -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
14 lib/publish.js
@@ -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.
View
51 lib/request.js
@@ -13,7 +13,8 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
if (typeof cb_ !== "function") cb_ = etag, etag = null
if (typeof cb_ !== "function") cb_ = what, what = null
- if (!this.registry) return cb(new Error(
+ var registry = this.conf.get('registry')
+ if (!registry) return cb(new Error(
"No registry url provided: " + method + " " + where))
// Since there are multiple places where an error could occur,
@@ -29,13 +30,11 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
return cb(new Error("favicon.ico isn't a package, it's a picture."))
}
- var registry = this.registry
-
var adduserChange = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)\/-rev/
, adduserNew = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)/
, nu = where.match(adduserNew)
, uc = where.match(adduserChange)
- , isUpload = what || this.alwaysAuth
+ , isUpload = what || this.conf.get('always-auth')
, isDel = method === "DELETE"
, authRequired = isUpload && !nu || uc || isDel
@@ -62,15 +61,15 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
}
var remote = url.parse(where)
- , auth = this.auth
+ , auth = this.conf.get('_auth')
- if (authRequired && !this.alwaysAuth) {
+ if (authRequired && !this.conf.get('always-auth')) {
var couch = this.couchLogin
- , token = couch && (this.token || couch.token)
+ , token = couch && (this.conf.get('_token') || couch.token)
, validToken = token && couch.valid(token)
if (!validToken) token = null
- else this.token = token
+ else this.conf.set('_token', token)
if (couch && !token) {
// login to get a valid token
@@ -81,8 +80,10 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
er = er || new Error('login error')
return cb(er, cr, data)
}
- this.token = this.couchLogin.token
- return regRequest.call(this, method, where, what, etag, nofollow, cb_)
+ this.conf.set('_token', this.couchLogin.token)
+ return regRequest.call(this,
+ method, where, what,
+ etag, nofollow, cb_)
}.bind(this))
}
}
@@ -101,11 +102,12 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
// Tuned to spread 3 attempts over about a minute.
// See formula at <https://github.com/tim-kos/node-retry>.
var operation = retry.operation({
- retries: this.retries,
- factor: this.retryFactor,
- minTimeout: this.retryMinTimeout,
- maxTimeout: this.retryMaxTimeout
+ retries: this.conf.get('fetch-retries') || 2,
+ factor: this.conf.get('fetch-retry-factor'),
+ minTimeout: this.conf.get('fetch-retry-mintimeout') || 10000,
+ maxTimeout: this.conf.get('fetch-retry-maxtimeout') || 60000
})
+
var self = this
operation.attempt(function (currentAttempt) {
self.log.info("retry", "registry request attempt " + currentAttempt
@@ -118,8 +120,8 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
var timeout = statusCode === 408
var serverError = statusCode >= 500
var statusRetry = !statusCode || timeout || serverError
- if (reauth && this.auth && this.token) {
- this.token = null
+ if (reauth && this.conf.get('_auth') && this.conf.get('_token')) {
+ this.conf.del('_token')
this.couchLogin.token = null
return regRequest.call(this, method, where, what, etag, nofollow, cb_)
}
@@ -140,10 +142,12 @@ function makeRequest (method, remote, where, what, etag, nofollow, tok, cb_) {
cb_.apply(null, arguments)
}
+ var strict = this.conf.get('strict-ssl')
+ if (strict === undefined) strict = true
var opts = { url: remote
, method: method
- , ca: this.ca
- , strictSSL: this.strictSSL }
+ , ca: this.conf.get('ca')
+ , strictSSL: strict }
, headers = opts.headers = {}
if (etag) {
this.log.verbose("etag", etag)
@@ -156,10 +160,11 @@ function makeRequest (method, remote, where, what, etag, nofollow, tok, cb_) {
headers.accept = "application/json"
- headers["user-agent"] = this.userAgent
+ headers["user-agent"] = this.conf.get('user-agent')
- opts.proxy = remote.protocol === "https:"
- ? this.httpsProxy : this.proxy
+ var p = this.conf.get('proxy')
+ var sp = this.conf.get('https-proxy') || p
+ opts.proxy = remote.protocol === "https:" ? sp : p
// figure out wth 'what' is
if (what) {
@@ -259,7 +264,7 @@ function requestDone (method, where, cb) {
, caches = p.map(function (part) {
return _ = path.join(_, part)
}).map(function (cache) {
- return path.join(this.cache, cache, ".cache.json")
+ return path.join(this.conf.get('cache'), cache, ".cache.json")
}, this)
// if the method is DELETE, then also remove the thing itself.
@@ -267,7 +272,7 @@ function requestDone (method, where, cb) {
// That's what you get for deleting stuff. Don't do that.
if (method === "DELETE") {
p = p.slice(0, p.indexOf("-rev"))
- caches.push(path.join(this.cache, p.join("/")))
+ caches.push(path.join(this.conf.get('cache'), p.join("/")))
}
asyncMap(caches, rm, function () {})
View
6 test/retries.js
@@ -5,9 +5,9 @@ var pkg = { _id: 'some-package@1.2.3',
name: 'some-package',
version: '1.2.3' }
var client = new RC({
- retries: 6
- , retryMinTimeout: 10
- , retryMaxTimeout: 100
+ 'fetch-retries': 6
+ , 'fetch-retry-mintimeout': 10
+ , 'fetch-retry-maxtimeout': 100
, cache: __dirname + '/fixtures/cache'
, registry: 'http://localhost:' + server.port })
Please sign in to comment.
Something went wrong with that request. Please try again.