-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.js
186 lines (172 loc) · 4.74 KB
/
websocket.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
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
/**
* Profile Stats MicroService.
*/
'use strict';
const WSCluster = require('./includes/cluster.js');
const framework = '@microservice-framework';
const MicroserviceRouterRegister = require(framework + '/microservice-router-register').register;
const clientViaRouter = require(framework + '/microservice-router-register').clientViaRouter;
const Validator = require('jsonschema').Validator;
const fs = require('fs');
const debugF = require('debug');
var debug = {
log: debugF('ws:log'),
debug: debugF('ws:debug')
};
require('dotenv').config();
var mControlCluster = new WSCluster({
pid: process.env.PIDFILE,
count: process.env.WORKERS,
ws: {
port: process.env.PORT,
hostname: process.env.HOSTNAME
},
callbacks: {
init: websocketInit,
receivedMessage: websocketReceivedMessage,
preSendMessage: websocketPreSendMessage
}
});
/**
* Init Handler.
*/
function websocketInit(cluster, worker, address) {
if (worker.id == 1 && address.addressType == 4) {
var mserviceRegister = new MicroserviceRouterRegister({
server: {
url: process.env.ROUTER_URL,
secureKey: process.env.ROUTER_SECRET,
period: process.env.ROUTER_PERIOD,
},
route: {
path: [process.env.SELF_PATH],
url: process.env.SELF_URL,
secureKey: process.env.SECURE_KEY,
scope: 'ws',
methods: {
post: 'data',
put: 'data',
delete: 'data',
search: 'meta',
get: 'meta',
}
},
cluster: cluster
});
}
}
function websocketPreSendMessage(jsonData, auth, callback) {
debug.debug('Received pre Send Message %O %O', auth, jsonData);
jsonData.method.toLowerCase();
// jsonData.
callback(null, jsonData);
}
/**
* WS Message handler.
* Process message from Client.
*/
function websocketReceivedMessage(jsonData, auth, callback) {
debug.debug('Received Client Message %O', jsonData);
var errors = websocketValidateJson(jsonData);
if (true !== errors) {
var error = new Error;
error.message = errors.join();
debug.debug('REQUEST:validateJson %O', error);
return callback(error);
}
if (auth.internal) {
return clientViaRouter(jsonData.EndPoint, function(err, apiServer) {
if (err) {
return callback(err);
}
processRequest(jsonData, apiServer, callback);
});
}
clientViaRouter(jsonData.EndPoint, auth.accessToken, function(err, apiServer) {
if (err) {
return callback(err);
}
processRequest(jsonData, apiServer, callback);
});
}
/**
* Process request.
*/
function processRequest(jsonData, apiServer, callback) {
switch (jsonData.method){
case 'POST': {
apiServer.post(jsonData.Request, callback);
break;
}
case 'GET': {
if (jsonData.RecordToken) {
return apiServer.get(jsonData.RecordID, jsonData.RecordToken, callback);
}
apiServer.get(jsonData.RecordID, callback);
break;
}
case 'PUT': {
if (jsonData.RecordToken) {
return apiServer.put(jsonData.RecordID, jsonData.RecordToken, jsonData.Request, callback);
}
apiServer.put(jsonData.RecordID, jsonData.Request, callback);
break;
}
case 'DELETE': {
if (jsonData.RecordToken) {
return apiServer.delete(jsonData.RecordID, jsonData.RecordToken, callback);
}
apiServer.delete(jsonData.RecordID, callback);
break;
}
case 'SEARCH': {
apiServer.search(jsonData.Request, callback);
break;
}
case 'OPTIONS': {
apiServer.options(jsonData.Request, callback);
break;
}
default: {
return callback(new Error('Unsupported method'));
}
}
}
/**
* Validate message.
*/
function websocketValidateJson(jsonData) {
var v = new Validator();
var errors = [];
try {
var schemaTask = JSON.parse(fs.readFileSync('schema/' + process.env.SCHEMA));
} catch (e) {
debug.debug('validateJson:Validator %O', e);
errors.push(new Error('Internal error: schema syntax error.'));
return errors;
}
var result = v.validate(jsonData, schemaTask);
if (['GET', 'PUT', 'DELETE'].indexOf(jsonData.method) != -1) {
if (!jsonData.RecordID) {
result.errors.push({
property: 'RecordID',
message: 'RecordID required if method is GET, PUT or DELETE'
});
}
}
if (['POST', 'PUT', 'SEARCH', 'OPTIONS'].indexOf(jsonData.method) != -1) {
if (!jsonData.Request) {
result.errors.push({
property: 'Request',
message: 'Request required if method is POST, PUT, SEARCH or OPTIONS'
});
}
}
if (result.errors.length > 0) {
for (var errorNum in result.errors) {
errors.push(result.errors[ errorNum ].property + ' ' + result.errors[ errorNum ].message);
}
return errors;
}
return true;
}