-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
167 lines (154 loc) · 4.04 KB
/
index.js
File metadata and controls
167 lines (154 loc) · 4.04 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const superagent = require('superagent');
const defaults = {
host: 'https://api.flowhub.io',
};
Object.defineProperty(exports, 'PING_INTERVAL', {
value: 10 * 60 * 1000,
enumerable: true,
});
class Runtime {
constructor(runtime, options) {
if (typeof runtime !== 'object') {
throw new Error('Runtime options expected');
}
if (!runtime.id) {
throw new Error('Runtime requires an UUID');
}
this.runtime = runtime;
this.options = {};
Object.keys(defaults).forEach((name) => {
this.options[name] = defaults[name];
});
if (options) {
Object.keys(options).forEach((name) => {
this.options[name] = options[name];
});
}
}
register(t, c) {
let token = t;
let callback = c;
if (typeof token === 'function') {
callback = t;
token = null;
}
if (!this.runtime.user) {
callback(new Error('Runtime registration requires a user UUID'));
return;
}
if (!this.runtime.address) {
callback(new Error('Runtime registration requires an address URL'));
return;
}
if (!this.runtime.protocol) {
callback(new Error('Runtime registration requires a protocol'));
return;
}
if (!this.runtime.type) {
callback(new Error('Runtime registration requires a type'));
return;
}
if (token) {
superagent.put(`${this.options.host}/runtimes/${this.runtime.id}`)
.set('Authorization', `Bearer ${token}`)
.send(this.runtime)
.end(callback);
return;
}
superagent.put(`${this.options.host}/runtimes/${this.runtime.id}`)
.send(this.runtime)
.end(callback);
}
ping(callback) {
superagent.post(`${this.options.host}/runtimes/${this.runtime.id}`)
.send({})
.end((err) => {
if (callback) {
callback(err);
}
});
}
get(token, callback) {
if (!token) {
callback(new Error('API token required for fetching'));
return;
}
superagent.get(`${this.options.host}/runtimes/${this.runtime.id}`)
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
if (err) {
callback(err);
return;
}
if (res.status !== 200) {
callback(new Error(`Request returned ${res.status}`));
return;
}
Object.keys(res.body).forEach((name) => {
if (name === 'seen' || name === 'registered') {
this.runtime[name] = new Date(res.body[name]);
return;
}
this.runtime[name] = res.body[name];
});
callback(null, this.runtime);
});
}
del(token, callback) {
if (!token) {
callback(new Error('API token required for deletion'));
return;
}
superagent.del(`${this.options.host}/runtimes/${this.runtime.id}`)
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
if (err && err.status === 404) {
// If the runtime is already gone, that is fine too
callback();
return;
}
callback(err, res);
});
}
}
function list(token, o, c) {
let options = o;
let callback = c;
if (!callback) {
callback = options;
options = {};
}
if (!token) {
callback(new Error('API token required for fetching'));
return;
}
Object.keys(defaults).forEach((name) => {
if (options[name]) {
return;
}
options[name] = defaults[name];
});
superagent.get(`${options.host}/runtimes/`)
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
if (err) {
callback(err);
return;
}
if (res.status !== 200) {
callback(new Error(`Request returned ${res.status}`));
return;
}
const results = [];
res.body.forEach((result) => {
results.push(new Runtime({
...result,
registered: new Date(result.registered),
seen: new Date(result.seen),
}, options));
});
callback(null, results);
});
}
module.exports.Runtime = Runtime;
module.exports.list = list;