Skip to content

Commit 05f9986

Browse files
author
alanm
committed
host protocol scheme
1 parent 9a72331 commit 05f9986

3 files changed

Lines changed: 186 additions & 1 deletion

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dataparty/api",
33
"private": false,
4-
"version": "1.2.16",
4+
"version": "1.2.17",
55
"main": "dist/dataparty.js",
66
"frontend": "dist/dataparty-browser.js",
77
"backend": "dist/dataparty.js",
@@ -76,6 +76,7 @@
7676
"express": "^4.17.1",
7777
"express-list-routes": "^0.1.4",
7878
"git-repo-info": "^2.1.1",
79+
"joi-objectid": "^4.0.2",
7980
"jshashes": "^1.0.8",
8081
"jsonpath-plus": "^0.20.1",
8182
"last-eventemitter": "^1.1.1",
@@ -86,6 +87,7 @@
8687
"morgan": "^1.10.0",
8788
"multer": "^1.4.5-lts.1",
8889
"nconf": "^0.10.0",
90+
"node-mocks-http": "^1.12.1",
8991
"node-persist": "^3.0.1",
9092
"origin-router": "^1.6.4",
9193
"parse-url": "^5.0.1",

src/comms/host-op.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const Hoek = require('@hapi/hoek')
2+
const debug = require('debug')('roshub.op')
3+
const moment = require('moment')
4+
const EventEmitter = require('eventemitter3')
5+
6+
const OP_STATUS_LEVEL = {
7+
Status_Undefined: 'Status_Undefined',
8+
Status_None: 'Status_None',
9+
Status_Error: 'Status_Error',
10+
Status_Warning: 'Status_Warning',
11+
Status_Info: 'Status_Info',
12+
Status_Debug: 'Status_Debug'
13+
}
14+
15+
16+
const OP_STATES = {
17+
Pending: 'Pending',
18+
Starting: 'Starting',
19+
Running: 'Running',
20+
Finished_Success: 'Finished_Success',
21+
Finished_Fail: 'Finished_Fail',
22+
Finished_Permission_Denied: 'Finished_Permission_Denied',
23+
Finished_Invalid_Op: 'Finished_Invalid_Op',
24+
Finished_Invalid_Id: 'Finished_Invalid_Id',
25+
Finished_Buffer_Full: 'Finished_Buffer_Full'
26+
}
27+
28+
class Op extends EventEmitter {
29+
constructor({msg, input}){
30+
super()
31+
this.msg = msg //! original encrypted message
32+
this.input = input //! parsed and validated input
33+
this.start = Date.now()
34+
this.end = undefined
35+
this.state = Op.STATES.Pending
36+
this.level = Op.STATUS_LEVELS.Status_Info
37+
}
38+
39+
get op(){
40+
return Hoek.reach(this.input, 'op')
41+
}
42+
43+
get id(){
44+
return Hoek.reach(this.input, 'id')
45+
}
46+
47+
48+
static get STATES(){
49+
return OP_STATES
50+
}
51+
52+
static get STATUS_LEVELS(){
53+
return OP_STATUS_LEVEL
54+
}
55+
56+
setState(state, level, message){
57+
58+
this.state = state
59+
60+
const status = {
61+
op: 'status',
62+
id: this.id,
63+
state: this.state,
64+
level: level || Op.STATUS_LEVELS.Status_Info,
65+
msg: message || undefined,
66+
timestamp: Date.now(),
67+
stats: {
68+
start: this.start,
69+
end: this.end,
70+
duration_ms: this.end - this.start
71+
}
72+
}
73+
74+
if(this.state == Op.STATES.Finished_Success ||
75+
this.state == Op.STATES.Finished_Fail ||
76+
this.state == Op.STATES.Finished_Invalid_Op ||
77+
this.state == Op.STATES.Finished_Invalid_Id ||
78+
this.state == Op.STATES.Finished_Permission_Denied ||
79+
this.state == Op.STATES.Finished_Buffer_Full)
80+
{
81+
debug('set end time')
82+
this.end = Date.now()
83+
this.emit('finished', true)
84+
}
85+
else{
86+
this.emit('finished', false)
87+
this.emit('status', status)
88+
}
89+
}
90+
91+
setOpStatusLevel(level){
92+
this.level = level
93+
}
94+
95+
setStatus(level, message){
96+
this.emit('status', )
97+
}
98+
99+
}
100+
101+
module.exports = Op

src/comms/protocol-scheme.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const Joi = require('@hapi/joi')
2+
Joi.objectId = require('joi-objectid')(Joi)
3+
4+
const ID_SCHEME = Joi.alternatives().try(Joi.string(), Joi.number())
5+
6+
const OP_HEADER = Joi.object().keys({
7+
id: ID_SCHEME.required(),
8+
op: Joi.string().valid([
9+
'auth',
10+
'advertise',
11+
'subscribe',
12+
'unsubscribe',
13+
'publish'
14+
]).required()
15+
})
16+
17+
18+
const AUTH_OP = Joi.object().keys({
19+
id: ID_SCHEME.required(),
20+
op: Joi.string().valid('auth').required(),
21+
session: Joi.objectId().required()
22+
})
23+
24+
const ADVERTISE_OP = Joi.object().keys({
25+
id: ID_SCHEME.required(),
26+
op: Joi.string().valid('advertise').required(),
27+
type: Joi.string().required(),
28+
topic: Joi.string().required(),
29+
queue_size: Joi.number(),
30+
latch: Joi.boolean()
31+
})
32+
33+
const UNADVERTISE_OP = Joi.object().keys({
34+
id: ID_SCHEME.required(),
35+
op: Joi.string().valid('unadvertise').required(),
36+
topic: Joi.string().required()
37+
})
38+
39+
40+
const SUBSCRIBE_OP = Joi.object().keys({
41+
id: ID_SCHEME.required(),
42+
op: Joi.string().valid('subscribe').required(),
43+
type: Joi.string().required(),
44+
topic: Joi.string().required(),
45+
compression: Joi.string(),
46+
queue_length: Joi.number(),
47+
throttle_rate: Joi.number()
48+
})
49+
50+
const UNSUBSCRIBE_OP = Joi.object().keys({
51+
id: ID_SCHEME.required(),
52+
op: Joi.string().valid('unsubscribe').required(),
53+
topic: Joi.string().required()
54+
})
55+
56+
const PUBLISH_OP = Joi.object().keys({
57+
id: ID_SCHEME.required(),
58+
op: Joi.string().valid('publish').required(),
59+
topic: Joi.string().required(),
60+
latch: Joi.boolean(),
61+
msg: Joi.any()
62+
})
63+
64+
65+
const ANY_OP = Joi.alternatives().try(
66+
ADVERTISE_OP,
67+
UNADVERTISE_OP,
68+
SUBSCRIBE_OP,
69+
UNSUBSCRIBE_OP,
70+
PUBLISH_OP
71+
)
72+
73+
module.exports = {
74+
'OP_HEADER': OP_HEADER,
75+
'AUTH_OP': AUTH_OP,
76+
'ADVERTISE_OP': ADVERTISE_OP,
77+
'UNADVERTISE_OP': UNADVERTISE_OP,
78+
'SUBSCRIBE_OP': SUBSCRIBE_OP,
79+
'UNSUBSCRIBE_OP': UNSUBSCRIBE_OP,
80+
'PUBLISH_OP': PUBLISH_OP,
81+
'ANY_OP': ANY_OP
82+
}

0 commit comments

Comments
 (0)