-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
135 lines (103 loc) · 3.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var EE = require('events').EventEmitter,
util = require('util'),
redis = require('redis'),
cradle = require('cradle'),
Pagelet = require('packages-pagelet'),
Registry = require('npm-registry'),
Dynamis = require('dynamis'),
Githulk = require('githulk');
module.exports = PreHeat;
util.inherits(PreHeat, EE);
function PreHeat (options) {
options = options || {};
if (!options.couchdb || !options.redis || !options.tokens) {
throw new Error('This requires redis, couchdb, and tokens option');
}
options.registry = options.registry || 'https://registry.nodejitsu.com';
this.options = options;
this.couchdb = new (cradle).Connection(options.couchdb);
// Is this a redis instance?
var isInstance = typeof options.redis.auth === 'function';
this.redis = isInstance
? options.redis
: redis.createClient(options.redis.port, options.redis.host,
{ auth_pass: options.redis.auth });
this.githulk = new Githulk({
cache: new Dynamis('cradle', this.couchdb, options.couchdb),
tokens: options.tokens
});
if (!isInstance) this.redis.on('error', this.emit.bind(this, 'error'));
this.registry = new Registry({
githulk: this.githulk,
registry: options.registry
});
this.prefix = options.private
? options.registry + ':private:'
: '';
this.pagelet = new (Pagelet.extend({
cache: new Dynamis('redis', this.redis, options.redis),
registry: this.registry,
githulk: this.githulk,
key: this.key.bind(this)
}).optimize());
}
PreHeat.prototype.key = function (name, version) {
return this.prefix + Pagelet.prototype.key.call(this, name, version);
};
//
// Take the package name, resolve it and its dependencies and cache it.
// Allow a registry host to also be passed in
//
PreHeat.prototype.cache = function (package, callback) {
this._callback = callback && typeof callback === 'function'
? callback
: undefined;
this.latest(package, this._onLatest.bind(this, package));
};
PreHeat.prototype.latest = function (name, cb) {
var key = this.key(name, 'latest');
this.registry.packages.get(name + '/latest', function (err, data) {
if (err) return cb(err);
if (Array.isArray(data)) data = data[0];
this.pagelet.fireforget('get', key, function (err, version) {
if (err) { return cb(err) }
// If we have the same version already just skip as we may have crashed
// or something
if (version === data.version) { return cb(null, null) };
this.pagelet.fireforget('set', key, data.version, 0);
cb(null, data.version);
}.bind(this));
}.bind(this));
};
PreHeat.prototype._onLatest = function (package, err, version) {
if (err) { return this.onError(err); }
if (!version) return this.done();
this.resolve(package, version, this._onResolved.bind(this));
};
PreHeat.prototype.resolve = function (package, version, fn) {
var key = this.key(package, version);
this.pagelet.resolve(package, {
registry: this.registry,
githulk: this.githulk
}, fn.bind(this, key));
};
PreHeat.prototype._onResolved = function (key, err, data) {
if (err) { return this.onError(err) }
this.pagelet.fireforget('set', key, data,
this.answer.bind(this));
};
PreHeat.prototype.answer = function (err) {
return err
? this.onError(err)
: this.done();
};
PreHeat.prototype.done = function () {
return this._callback
? this._callback()
: this.emit('done');
};
PreHeat.prototype.onError = function (err) {
return this._callback
? this._callback(err)
: this.emit('error', err);
};