-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
254 lines (224 loc) · 8.67 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
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
/* jshint esversion: 6 */
const http = require('http');
const nullBodyError = 'Body is null';
const defaultConfig = {
host: '127.0.0.1',
port: 9676
};
function setConfig(config) {
if (!config) {
return;
}
defaultConfig.host = (config.host) ? config.host : defaultConfig.host;
defaultConfig.port = (config.port) ? config.port : defaultConfig.port;
}
var localStringResources = {};
function getStringResources(callback) {
const options = {
host: defaultConfig.host,
port: defaultConfig.port,
path: '/api/StringResources',
method: 'GET'
};
http.request(options, function (response) {
response.on('data', (data) => {
const jsonData = JSON.parse(data.toString('utf8'));
if (!jsonData.Message) {
callback(null, jsonData);
} else {
callback(new Error("Error on " + options.host + ":" + options.port), jsonData);
}
});
})
.on('error', callback)
.end();
}
function getTask(componentName, stateMachineName, callback) {
const getOptions = (componentName, stateMachineName) => {
return {
host: defaultConfig.host,
port: defaultConfig.port,
path: '/api/Functions?componentName=' + escape(componentName) + '&stateMachineName=' + escape(stateMachineName),
method: 'GET'
};
};
http
.request(getOptions(componentName, stateMachineName), function (response) {
let body = '';
response.on('data', (data) => {
body += data;
});
response.on('end', () => {
if (!body) {
callback(nullBodyError, null);
return;
}
try {
callback(null, JSON.parse(body));
} catch (e) {
callback(e, null);
}
});
response.on('error', callback);
})
.on('error', callback)
.end();
}
function postObject(object, endpoint, callback) {
const postOptions = {
host: defaultConfig.host,
port: defaultConfig.port,
path: '/api/' + endpoint,
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
const request = http.request(postOptions, function (response) {
if (response.statusCode != 204) {
if (callback) callback(response.statusCode);
} else {
if (callback) callback(null, response.statusCode);
}
});
request.on('error', function (error) {
if (callback) callback(error);
});
request.write(JSON.stringify(object));
request.end();
}
function postConfiguration(configurationObject, callback) {
postObject(configurationObject, 'Configuration', callback);
}
function postResult(responseObject, callback) {
postObject(responseObject, 'Functions', callback);
}
const triggeredMethods = {};
exports.registerTriggeredMethod = (componentName, stateMachineName, triggeredMethodName, triggeredMethodFunction) => {
if (!(componentName in triggeredMethods)) {
triggeredMethods[componentName] = {};
}
if (!(stateMachineName in triggeredMethods[componentName])) {
triggeredMethods[componentName][stateMachineName] = {};
}
triggeredMethods[componentName][stateMachineName][triggeredMethodName] = triggeredMethodFunction;
};
exports.registerTriggeredMethods = (componentName, stateMachineName, triggeredMethods) => {
for (const triggeredMethodName in triggeredMethods) {
exports.registerTriggeredMethod(componentName, stateMachineName, triggeredMethodName, triggeredMethods[triggeredMethodName]);
}
};
function setConfig(configuration) {
defaultConfig.host = (configuration.host) ? configuration.host : defaultConfig.host;
defaultConfig.port = (configuration.port) ? configuration.port : defaultConfig.port;
}
function updateConfiguration(configuration, callback) {
if (configuration) {
setConfig(configuration);
const postConfigurationAction = () => postConfiguration(configuration, configurationCallback);
const configurationCallback = (error) => {
if (error) {
console.error('Error updating configuration: ', error);
callback(error, null);
return;
}
console.log('Configuraton successfuly updated!');
callback(null, true);
};
postConfigurationAction();
} else {
callback(null, true);
}
}
function installEventQueue(callback) {
setInterval(eventQueue);
callback(null, true);
console.log('Waiting for tasks...');
}
function addStringResource(component, key, value) {
if (!localStringResources[component]) {
localStringResources[component] = {};
}
localStringResources[component][key] = value;
}
exports.startEventQueue = (configuration, callback) => {
updateConfiguration(configuration, (error, success) => {
if (error) {
console.error(error);
return callback && callback(error, success);
}
getStringResources((error, stringResources) => {
if (error) {
console.error(error);
return callback && callback(error, null);
}
stringResources.forEach((keypair) => addStringResource(keypair.ComponentName, keypair.Key, keypair.Value));
installEventQueue((error, success) => callback && callback(error, success));
});
});
};
function senderHandler(target, name) {
return (parameter, useContext) => {
target.Senders.push({
SenderName: name,
SenderParameter: parameter || {},
UseContext: useContext || false
});
};
}
function eventQueue() {
for (const componentName in triggeredMethods) {
for (const stateMachineName in triggeredMethods[componentName]) {
getTask(componentName, stateMachineName, (error, task) => {
if (error) {
if (error !== nullBodyError && !(error.code && error.code === 'ECONNREFUSED')) {
console.error(error);
}
return;
}
console.log('Received task: ', task);
const triggeredMethod = triggeredMethods[componentName][stateMachineName][task.FunctionName];
const isTriggeredMethodAsync = triggeredMethod && triggeredMethod.length > 6;
const sendersList = [];
const sender = new Proxy({ Senders: sendersList }, { get: senderHandler });
const stringResources = localStringResources[componentName] || {};
const done = (err) => {
if (!err) {
postResult({
// jshint ignore:start
Senders: sendersList,
PublicMember: task.PublicMember,
InternalMember: task.InternalMember,
ComponentName: task.ComponentName,
StateMachineName: task.StateMachineName,
RequestId: task.RequestId
// jshint ignore:end
});
} else {
postResult({
// jshint ignore:start
IsError: true,
ErrorMessage: "" + err
// jshint ignore:end
});
}
};
try {
if (!triggeredMethod) {
error = new Error('Received task for unregistered function', task.FunctionName);
done(error);
} else if (isTriggeredMethodAsync) {
triggeredMethod(task.Event, task.PublicMember, task.InternalMember, task.Context, sender, stringResources, done);
} else {
triggeredMethod(task.Event, task.PublicMember, task.InternalMember, task.Context, sender, stringResources);
done();
}
} catch (e) {
console.error("Caught exception", e);
error = e;
done(e);
}
});
}
}
}