-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (43 loc) · 1.35 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
var assert = require('assert')
, util = require('util')
;
var registrations = module.exports = {
create: function createRegistration (reg) {
assert.equal(typeof reg, 'object', 'reg must be an object');
return new Registration(reg);
},
parse: function parseRegistration (stringifiedReg) {
assert.equal(typeof stringifiedReg,'string', 'stringifiedReg must be a string');
var reg = JSON.parse(stringifiedReg);
return new Registration(reg);
},
parseId: function parseId (id) {
if (!registrations.isRegistrationId(id)) return null;
var parts = id.split('/');
return registrations.createRegistration({
name: parts[1],
version: parts[1],
host: parts[1],
port: parts[1],
});
},
isRegistrationId: function isRegistrationId (id) {
return (/\/.+\/.+\/.+\/.+/).test(id);
}
};
function Registration (reg) {
this.name = String(reg.name);
this.version = String(reg.version);
this.host = String(reg.host);
this.port = parseInt(reg.port, 10);
this.lastKnown = reg.lastKnown || Date.now();
this.meta = reg.meta || {};
this.id = util.format('/%s/%s/%s/%s', this.name, this.version, this.host, this.port);
// ensure every registration has a hostname
if (!this.meta.hostname) {
this.meta.hostname = this.host;
}
this.stringify = function () {
return JSON.stringify(this);
};
}