npm: Upgrade to 1.3.19
[platform/upstream/nodejs.git] / deps / npm / node_modules / npm-registry-client / index.js
1 // utilities for working with the js-registry site.
2
3 module.exports = RegClient
4
5 var fs = require('fs')
6 , url = require('url')
7 , path = require('path')
8 , CouchLogin = require('couch-login')
9 , npmlog
10
11 try {
12   npmlog = require("npmlog")
13 } catch (er) {
14   npmlog = { error: noop, warn: noop, info: noop,
15              verbose: noop, silly: noop, http: noop,
16              pause: noop, resume: noop }
17 }
18
19 function noop () {}
20
21 function RegClient (conf) {
22   // accept either a plain-jane object, or a npmconf object
23   // with a "get" method.
24   if (typeof conf.get !== 'function') {
25     var data = conf
26     conf = { get: function (k) { return data[k] }
27            , set: function (k, v) { data[k] = v }
28            , del: function (k) { delete data[k] } }
29   }
30
31   this.conf = conf
32
33   // if provided, then the registry needs to be a url.
34   // if it's not provided, then we're just using the cache only.
35   var registry = conf.get('registry')
36   if (registry) {
37     registry = url.parse(registry)
38     if (!registry.protocol) throw new Error(
39       'Invalid registry: ' + registry.url)
40     registry = registry.href
41     if (registry.slice(-1) !== '/') {
42       registry += '/'
43     }
44     this.conf.set('registry', registry)
45   } else {
46     registry = null
47   }
48
49   if (!conf.get('cache')) throw new Error("Cache dir is required")
50
51   var auth = this.conf.get('_auth')
52   var alwaysAuth = this.conf.get('always-auth')
53   if (auth && !alwaysAuth && registry) {
54     // if we're always authing, then we just send the
55     // user/pass on every thing.  otherwise, create a
56     // session, and use that.
57     var token = this.conf.get('_token')
58     this.couchLogin = new CouchLogin(registry, token)
59     this.couchLogin.proxy = this.conf.get('proxy')
60     this.couchLogin.strictSSL = this.conf.get('strict-ssl')
61     this.couchLogin.ca = this.conf.get('ca')
62     this.couchLogin.cert = this.conf.get('cert')
63     this.couchLogin.key = this.conf.get('key')
64   }
65
66   this.log = conf.log || conf.get('log') || npmlog
67 }
68
69 require('fs').readdirSync(__dirname + "/lib").forEach(function (f) {
70   if (!f.match(/\.js$/)) return
71   RegClient.prototype[f.replace(/\.js$/, '')] = require('./lib/' + f)
72 })