UNPKG

3.47 kBJavaScriptView Raw
1'use strict';
2
3var debug = require('debug')('npmjs')
4 , mana = require('mana');
5
6/**
7 * A simple npm registry interface for data retrieval.
8 *
9 * The following options are accepted:
10 *
11 * - registry: Registry URL we want to connect to.
12 * - user: Name of the account.
13 * - password: Password of the account.
14 * - mirrors: Alternate mirrors we should use when we receive an error.
15 * - factor: Backoff factor.
16 * - mindelay: Minimum backoff delay.
17 * - maxdelay: Maximum backoff delay.
18 * - retries: Maximum amount of retries.
19 *
20 * @constructor
21 * @param {Object} options Configuration
22 * @api public
23 */
24var Registry = mana.extend({
25 /**
26 * Initialise the module.
27 *
28 * @param {Object} options npmjs configuration.
29 * @api private
30 */
31 initialise: function initialise(options) {
32 options = options || {};
33
34 //
35 // Get an array of active npm mirrors.
36 //
37 var mirrors = Object.keys(Registry.mirrors).map(function map(mirror) {
38 return Registry.mirrors[mirror];
39 });
40
41 options.registry = 'registry' in options ? options.registry : Registry.mirrors.nodejitsu;
42 options.stats = 'stats' in options ? options.stats : 'https://api.npmjs.org/';
43 options.mirrors = 'mirrors' in options ? options.mirrors : mirrors;
44 options.maxdelay = 'maxdelay' in options ? options.maxdelay : 60000;
45 options.mindelay = 'mindelay' in options ? options.mindelay : 100;
46 options.githulk = 'githulk' in options ? options.githulk : null;
47 options.retries = 'retries' in options ? options.retries : 3;
48 options.factor = 'factor' in options ? options.factor : 2;
49
50 //
51 // Make sure that the given registry is a string as we can only connect to
52 // URL's. If it isn't an string we assume it's an object with an `url`
53 // parameter.
54 //
55 if ('string' !== typeof options.registry) options.registry = options.registry.url;
56
57 this.authorization = options.authorization;
58 this.mirrors = options.mirrors;
59 this.mindelay = options.mindelay;
60 this.maxdelay = options.maxdelay;
61 this.statservice = options.stats;
62 this.githulk = options.githulk;
63 this.retries = options.retries;
64 this.factor = options.factor;
65 this.api = options.registry;
66
67 //
68 // Pre-compile the basic authorization so we can do updates and deletes
69 // against the registries.
70 //
71 if (!this.authorization && options.user && options.password) {
72 debug('received authorization information for %s', options.user);
73 this.authorization = new Buffer(
74 options.user +':'+ options.password
75 ).toString('base64');
76 }
77
78 if (Buffer.isBuffer(this.authorization)) {
79 this.authorization = 'Basic '+ this.authorization;
80 }
81 },
82
83 /**
84 * Common map operations that can be shared between the different endpoints.
85 *
86 * @type {Object}
87 * @private
88 */
89 map: {
90 simple: function simple(data) {
91 return data ? {
92 name: data[1],
93 description: data[2]
94 } : undefined;
95 }
96 }
97});
98
99//
100// Expose list of public endpoints that people can use to connect.
101//
102Registry.mirrors = {
103 nodejitsu: 'http://registry.nodejitsu.com/',
104 strongloop: 'http://npm.strongloop.com/',
105 npmjsau: 'http://registry.npmjs.org.au/',
106 npmjseu: 'http://registry.npmjs.eu/',
107 npmjspt: 'http://registry.npmjs.pt/',
108 npmjs: 'http://registry.npmjs.org/',
109 cnpmjs: 'http://registry.cnpmjs.org/'
110};
111
112//
113// Drink our own potion.
114//
115Registry.drink(module);