-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
434 lines (346 loc) · 9.95 KB
/
index.js
File metadata and controls
434 lines (346 loc) · 9.95 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// index.js - CNS Dapr
// Copyright 2023 Padi, Inc. All Rights Reserved.
'use strict';
// Imports
const dapr = require('@dapr/dapr');
const env = require('dotenv').config();
const objects = require('./src/objects.js');
const pack = require('./package.json');
// Errors
const E_CONTEXT = 'no context';
const E_TOKEN = 'no token';
const E_READONLY = 'read only';
const E_MISSMATCH = 'type missmatch';
const E_NOTFOUND = 'not found';
// Defaults
const defaults = {
CNS_BROKER: 'padi',
CNS_CONTEXT: '',
CNS_TOKEN: '',
CNS_DAPR: 'cns-dapr',
CNS_DAPR_HOST: 'localhost',
CNS_DAPR_PORT: '3500',
CNS_PUBSUB: 'cns-pubsub',
CNS_SERVER_HOST: 'localhost',
CNS_SERVER_PORT: '3000'
};
// Config
const config = {
CNS_BROKER: process.env.CNS_BROKER || defaults.CNS_BROKER,
CNS_CONTEXT: process.env.CNS_CONTEXT || defaults.CNS_CONTEXT,
CNS_TOKEN: process.env.CNS_TOKEN || defaults.CNS_TOKEN,
CNS_DAPR: process.env.CNS_DAPR || defaults.CNS_DAPR,
CNS_DAPR_HOST: process.env.CNS_DAPR_HOST || defaults.CNS_DAPR_HOST,
CNS_DAPR_PORT: process.env.CNS_DAPR_PORT || defaults.CNS_DAPR_PORT,
CNS_PUBSUB: process.env.CNS_PUBSUB || defaults.CNS_PUBSUB,
CNS_SERVER_HOST: process.env.CNS_SERVER_HOST || defaults.CNS_SERVER_HOST,
CNS_SERVER_PORT: process.env.CNS_SERVER_PORT || defaults.CNS_SERVER_PORT
};
// Dapr server
const server = new dapr.DaprServer({
serverHost: config.CNS_SERVER_HOST,
serverPort: config.CNS_SERVER_PORT,
clientOptions: {
daprHost: config.CNS_DAPR_HOST,
daprPort: config.CNS_DAPR_PORT
},
logger: {
level: dapr.LogLevel.Error
}
});
// Node cache
const cache = {
profiles: {},
node: {
version: pack.version,
broker: config.CNS_BROKER,
status: {
started: new Date().toISOString(),
reads: 0,
writes: 0,
updates: 0,
errors: 0,
connection: 'online'
},
contexts: {}
}
};
// Local data
var broker;
var topic;
// Local functions
// Get profile endpoint
async function getProfile(req) {
try {
// Locate query
const keys = getKeys(req.query);
const loc = await readProfile(keys);
// Success
console.log('APP GET', req.query, 'OK');
return getResponse(req, loc);
} catch(e) {
// Failure
console.error('APP GET', req.query, 'ERROR', e.message);
return getError(e);
}
}
// Get node endpoint
async function getNode(req) {
try {
// Locate query
const keys = getKeys(req.query);
const loc = getLocation(keys);
// Success
console.log('APP GET', req.query, 'OK');
return getResponse(req, loc);
} catch(e) {
// Failure
console.error('APP GET', req.query, 'ERROR', e.message);
return getError(e);
}
}
// Post node endpoint
async function postNode(req) {
try {
// Locate query
const keys = getKeys(req.query);
const loc = postLocation(keys);
// Take snapshot
const context = config.CNS_CONTEXT;
const prev = objects.duplicate(cache.node.contexts[context]);
// Merge into cache
const data1 = loc.obj[loc.key];
const data2 = getRequest(req.body);
const obj1 = objects.isObject(data1);
const obj2 = objects.isObject(data2);
if (obj1 && !obj2)
throw new Error(E_MISSMATCH);
if (obj1 && obj2) {
if (!objects.contains(data1, data2))
throw new Error(E_NOTFOUND);
loc.obj[loc.key] = objects.merge(data1, data2);
} else loc.obj[loc.key] = data2;
// Publish differences
const next = cache.node.contexts[context];
const diff = objects.difference(prev, next);
if (!objects.isEmpty(diff)) {
await broker.postNode(diff, prev, next);
await server.client.pubsub.publish(config.CNS_PUBSUB, topic, diff);
}
// Success
console.log('APP POST', req.query, 'OK');
return postResponse(diff);
} catch(e) {
// Failure
console.error('APP POST', req.query, 'ERROR', e.message);
return getError(e);
}
}
// Put node endpoint
async function putNode(req) {
try {
throw new Error('NYI: ' + req.query);
} catch(e) {
// Failure
console.error('APP PUT', req.query, 'ERROR', e.message);
return getError(e);
}
}
// Delete node endpoint
async function deleteNode(req) {
try {
throw new Error('NYI: ' + req.query);
} catch(e) {
// Failure
console.error('APP DELETE', req.query, 'ERROR', e.message);
return getError(e);
}
}
// Publish node topic
async function publishNode(data) {
try {
// Locate topic
const keys = getKeys(topic);
// Publish to context only
if (keys[0] !== 'node' || keys[1] !== 'contexts' ||
keys[2] !== config.CNS_CONTEXT)
throw new Error(E_NOTFOUND);
//const loc = getLocation(keys);
// Take snapshot
const context = config.CNS_CONTEXT;
const prev = objects.duplicate(cache.node.contexts[context]);
// Merge into cache
const data1 = cache.node.contexts[context];
const data2 = getRequest(data);
const obj1 = objects.isObject(data1);
const obj2 = objects.isObject(data2);
if (!obj1 || !obj2)
throw new Error(E_MISSMATCH);
if (!objects.contains(data1, data2))
throw new Error(E_NOTFOUND);
cache.node.contexts[context] = objects.merge(data1, data2);
// Remove deleted objects
purgeContexts(cache.node.contexts);
purgeCapabilities(prev.capabilities);
// Post differences
const next = cache.node.contexts[context];
const diff = objects.difference(prev, next);
if (!objects.isEmpty(diff))
await broker.postNode(diff, prev, next);
// Success
console.log('APP PUB', topic, 'OK');
} catch(e) {
// Failure
console.error('APP PUB', topic, 'ERROR', e.message);
}
}
// Remove deleted contexts
function purgeContexts(contexts) {
for (const id in contexts) {
if (contexts[id] === null) delete contexts[id];
else purgeCapabilities(contexts[id].capabilities);
}
}
// Remove deleted capabilities
function purgeCapabilities(caps) {
for (const id in caps) {
if (caps[id] === null) delete caps[id];
else purgeConnections(caps[id].connections);
}
}
// Remove deleted connections
function purgeConnections(conns) {
for (const id in conns)
if (conns[id] === null) delete conns[id];
}
// Subscription update
async function updateNode(data) {
// Compute differences
const context = config.CNS_CONTEXT;
const diff = objects.difference(cache.node.contexts[context], data);
if (!objects.isEmpty(diff)) {
// Publish differences
cache.node.contexts[context] = data;
console.log('DAPR PUB', topic);
await server.client.pubsub.publish(config.CNS_PUBSUB, topic, diff);
}
cache.node.status.updates++;
}
// Split query into keys
function getKeys(query) {
const keys = query.split('/');
if (query.startsWith('/')) keys.shift();
if (query.endsWith('/')) keys.pop();
return keys;
}
// Get location from keys
function getLocation(keys) {
const loc = objects.locate(keys, cache);
if (loc === null)
throw new Error('not found');
return loc;
}
// Post location from keys
function postLocation(keys) {
const node = keys[0];
const contexts = keys[1];
const context = keys[2];
// Post to context only
if (node !== 'node' || contexts !== 'contexts' ||
context !== config.CNS_CONTEXT)
throw new Error(E_READONLY);
const capabilities = keys[3];
const profile = keys[4];
const property = keys[5];
// Post to properties or connections only
if (capabilities === 'capabilities' &&
property !== 'properties' && property !== 'connections')
throw new Error(E_READONLY);
return getLocation(keys);
}
// Read profile data from keys
async function readProfile(keys) {
const profiles = keys[0];
const profile = keys[1];
// Profile cached?
var data = cache.profiles[profile];
if (data === undefined) {
try {
// Read profile
data = await broker.getProfile(profile);
} catch(e) {
// Failure
data = null;
}
// Cache result
cache.profiles[profile] = data;
}
// Found anything?
if (data === null)
throw new Error(E_NOTFOUND);
return getLocation(keys);
}
// Get request data
function getRequest(data) {
if (data === '{}') return null;
try {
return JSON.parse(data);
} catch(e) {
// Failure
return data;
}
}
// Get response data
function getResponse(req, loc) {
if (!req.query.startsWith('/node/status'))
cache.node.status.reads++;
return {data: loc.obj[loc.key]};
}
// Post response data
function postResponse(diff) {
cache.node.status.writes++;
return {data: diff};
}
// Get response error
function getError(e) {
cache.node.status.errors++;
return {error: e.message};
}
// Start application
async function start() {
// Output welcome
console.log('CNS Dapr', pack.version);
console.log('CNS Dapr on', config.CNS_DAPR_HOST, 'port', config.CNS_DAPR_PORT);
if (config.context === '')
throw new Error(C_CONTEXT);
if (config.token === '')
throw new Error(C_TOKEN);
console.log('CNS context', config.CNS_CONTEXT);
console.log('CNS broker', config.CNS_BROKER);
// Bind broker
broker = require('./src/brokers/' + config.CNS_BROKER + '.js');
topic = 'node/contexts/' + config.CNS_CONTEXT;
// Start broker
await broker.start({
context: config.CNS_CONTEXT,
token: config.CNS_TOKEN
});
// Ask broker for context
cache.node.contexts[config.CNS_CONTEXT] = await broker.getNode();
await broker.subscribeNode(updateNode);
// Endpoint listeners
await server.invoker.listen('profiles/:profile*', getProfile, {method: dapr.HttpMethod.GET});
await server.invoker.listen('node(/*)?', getNode, {method: dapr.HttpMethod.GET});
await server.invoker.listen('node(/*)?', postNode, {method: dapr.HttpMethod.POST});
await server.invoker.listen('node(/*)?', putNode, {method: dapr.HttpMethod.PUT});
await server.invoker.listen('node(/*)?', deleteNode, {method: dapr.HttpMethod.DELETE});
// Topic listeners
await server.pubsub.subscribe(config.CNS_PUBSUB, topic, publishNode);
// Start Dapr server
await server.start();
}
// Start application
start().catch((e) => {
console.error('Error:', e.message);
process.exit(1);
});