-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
102 lines (84 loc) · 2.21 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
'use strict'
const cote = require('cote')
const u = require('elife-utils')
/* understand/
* This is the main entry point where we start.
*
* outcome/
* Start our microservice and register with the communication manager.
*/
function main() {
startMicroservice()
registerWithCommMgr()
}
const commMgrClient = new cote.Requester({
name: 'Calculator -> CommMgr',
key: 'everlife-communication-svc',
})
function sendReply(msg, req) {
req.type = 'reply'
req.msg = String(msg)
commMgrClient.send(req, (err) => {
if(err) u.showErr(err)
})
}
let msKey = 'everlife-calculator-demo-svc'
/* outcome/
* Register ourselves as a message handler with the communication
* manager so we can handle requests for simple calculations.
*/
function registerWithCommMgr() {
commMgrClient.send({
type: 'register-msg-handler',
mskey: msKey,
mstype: 'msg',
mshelp: [ { cmd: '/calc', txt: 'do some math!' } ],
}, (err) => {
if(err) u.showErr(err)
})
}
function startMicroservice() {
/* understand/
* The calculator microservice (partitioned by key to prevent
* conflicting with other services.
*/
const calcSvc = new cote.Responder({
name: 'Everlife Calculator Service Demo',
key: msKey,
})
/* outcome/
* Respond to user messages asking us to calculate things by
* evaluating them as an expression and returning the result if
* found.
*/
calcSvc.on('msg', (req, cb) => {
if(!req.msg) return cb()
let txt = req.msg
if(!txt.startsWith('/calc ')) return cb()
cb(null, true)
txt = txt.substr('/calc '.length)
calc(txt, (v) => {
if(v) {
sendReply(v, req)
} else {
sendReply(`Could not calculate ${txt}`, req)
}
})
})
calcSvc.on('calc', (req, cb) => {
calc(req.expr, cb)
})
}
function calc(txt, cb) {
try {
txt = txt.replace(/[^-()\d/*+.]/g, '')
// TODO: Is there a better way instead
// of `eval()`?
let v = eval(txt)
if(isNaN(v)) cb()
else cb(v)
} catch(e) {
cb()
}
}
main()