UNPKG

3.33 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.mirrors = 'mirrors' in options ? options.mirrors : mirrors;
43 options.maxdelay = 'maxdelay' in options ? options.maxdelay : 60000;
44 options.mindelay = 'mindelay' in options ? options.mindelay : 100;
45 options.githulk = 'githulk' in options ? options.githulk : null;
46 options.retries = 'retries' in options ? options.retries : 3;
47 options.factor = 'factor' in options ? options.factor : 2;
48
49 //
50 // Make sure that the given registry is a string as we can only connect to
51 // URL's. If it isn't an string we assume it's an object with an `url`
52 // parameter.
53 //
54 if ('string' !== typeof options.registry) options.registry = options.registry.url;
55
56 this.authorization = options.authorization;
57 this.mirrors = options.mirrors;
58 this.mindelay = options.mindelay;
59 this.maxdelay = options.maxdelay;
60 this.githulk = options.githulk;
61 this.retries = options.retries;
62 this.factor = options.factor;
63 this.api = options.registry;
64
65 //
66 // Pre-compile the basic authorization so we can do updates and deletes
67 // against the registries.
68 //
69 if (!this.authorization && options.user && options.password) {
70 debug('received authorization information for %s', options.user);
71 this.authorization = new Buffer(
72 options.user +':'+ options.password
73 ).toString('base64');
74 }
75
76 if (Buffer.isBuffer(this.authorization)) {
77 this.authorization = 'Basic '+ this.authorization;
78 }
79 },
80
81 /**
82 * Common map operations that can be shared between the different endpoints.
83 *
84 * @type {Object}
85 * @private
86 */
87 map: {
88 simple: function simple(data) {
89 return {
90 name: data[1],
91 description: data[2]
92 };
93 }
94 }
95});
96
97//
98// Expose list of public endpoints that people can use to connect.
99//
100Registry.mirrors = {
101 nodejitsu: 'http://registry.nodejitsu.com/',
102 strongloop: 'http://npm.strongloop.com/',
103 npmjsau: 'http://registry.npmjs.org.au/',
104 npmjseu: 'http://registry.npmjs.eu/',
105 npmjspt: 'http://registry.npmjs.pt/',
106 npmjs: 'http://registry.npmjs.org/',
107 cnpmjs: 'http://registry.cnpmjs.org/'
108};
109
110//
111// Drink our own potion.
112//
113Registry.drink(module);