forked from mafintosh/polo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (93 loc) · 2.68 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
var common = require('common');
var repository = require('./repository');
var polo = function(options) {
var that = common.createEmitter();
var ups = common.createEmitter();
var repo = repository(options || {});
var next = function(name) {
var list = that.all(name);
return list[Math.floor(Math.random() * list.length)];
};
var parse = function(name) {
var result = {
name: name
};
name.replace(/\{([^\}]+)\}/g, function(_, app) {
result.name = app;
result.format = name;
});
return result;
};
var formatURL = function(url) {
if (url.substring(0, 7) === 'http://') return url.replace(':80', '');
if (url.substring(0, 8) === 'https://') return url.replace(':443', '');
return url;
};
var format = function(parsed) {
var app = next(parsed.name);
if (!app) return null;
return parsed.format ? formatURL(parsed.format.replace('{' + parsed.name + '}', app.address)) : app;
};
ups.setMaxListeners(0);
repo.on('pop', function(name, service) {
that.emit('down', name, service);
that.emit(name + '/down', service);
});
repo.on('push', function(name, service) {
that.emit('up', name, service);
that.emit(name + '/up', service);
ups.emit(name);
});
repo.on('error', function(error) {
that.emit('error', error);
});
that.put = function(service, port) {
// passed a http server as second argument
if (port && typeof port.address === 'function') port = port.address().port;
// passed host:port as second argument
if (typeof service === 'string' && typeof port === 'string') {
return that.put({
name: service,
host: port
});
}
// passed port as second argument
if (typeof service === 'string' && typeof port === 'number') {
return that.put({
name: service,
port: port
});
}
// name is required
if (!service.name) throw new Error('invalid arguments - name required');
service.host = service.host || repo.address;
if (!service.port) {
var parts = service.host.split(':');
if (parts.length !== 2) throw new Error('invalid arguments - port required');
service.host = parts[0];
service.port = parseInt(parts[1], 10);
}
service.address = service.address || service.host + ':' + service.port;
repo.push(service.name, service);
return service;
};
that.get = function(name, onup) {
var parsed = parse(name);
onup = typeof onup === 'function' && onup;
if (onup && !repo.get(parsed.name).length) {
ups.once(parsed.name, function() {
onup(format(parsed));
});
return;
}
return onup ? onup(format(parsed)) : format(parsed);
};
that.all = function(name) {
return name ? repo.get(name) : repo.all();
};
that.stop = function() {
repo.close();
};
return that;
};
module.exports = polo;