-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
294 lines (234 loc) · 8.98 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
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
'use strict';
var scxml = require('scxml'),
uuid = require('uuid'),
request = require('request');
//TODO: fix this ugly, non-standards-compiant interface
scxml.scion.ioProcessorTypes.smaas = { location: 'http://scxml.io/httpLifecycle' };
var instanceSubscriptions = {};
module.exports = function (db, model, modelName) {
var server = {};
var timeoutMap = {};
function completeInstantly () {
//Call last argument
arguments[arguments.length -1]();
}
function react (instanceId, snapshot, event, sendOptions, eventUuid, done) {
//Check if chartname.scxml folder exists
//If it does
//Use scxml.pathToModel
//else
//Query db for statechart content
//Use documentStringToModel
//Get model
//Create instance
//Add listeners
//Start instance with or without snapshot
//If event exists
//Send the event
//Return config
var wait = false;
var instance = new scxml.scion.Statechart(model, {
snapshot: snapshot,
sessionid: instanceId,
customSend : function(event, options) {
//TODO: set event.origin based on _ioprocessors and send type
//TODO: inject _ioprocessors
//For now, set the origin to uri - assumes BasicHTTPEventProcessor
//FIXME: if token is in the path, then it will probably show up in the origin. Don't want to allow token to leak out via origin.
event.origin = sendOptions.uri;
var n;
console.log('customSend event',event);
var requestUrl;
switch(event.type) {
case 'http://scxml.io/httpLifecycle':
var targetMatch = event.target.match(targetRegex);
if(!targetMatch && targetMatch.length) return done(new Error('Received malformed target url'));
var target = targetMatch[1];
console.log('event.name',event.name);
if(event.name === 'response'){
respond(target, null, event.data);
} else if(event.name === 'wait'){
wait = true;
}
break;
case 'http://www.w3.org/TR/scxml/#SCXMLEventProcessor':
//See http://www.w3.org/TR/scxml/#SCXMLEventProcessor
var scxmlSessionMatch = event.target && event.target.match(scxmlSessionRegex);
//normalize to event for BasicHTTPEventProcessor
if(!event.target){
targetUrl = sendOptions.uri;
}else if(event.target === INTERNAL_TARGET){
//TODO: expose this.raise to send()
//this.raise(event);
throw new Error('Send target=#_internal not yet implemented');
break;
}else if(scxmlSessionMatch){
var targetSessionid = scxmlSessionMatch[1];
var baseUri = extractBaseUri(targetSessionid);
var targetUrl = baseUri + '/' + targetSessionid;
}else if(event.target.match(httpSessionRegex)){
var targetUrl = event.target;
}
case 'http://www.w3.org/TR/scxml/#BasicHTTPEventProcessor':
if(!targetUrl) targetUrl = event.target;
//See http://www.w3.org/TR/scxml/#SCXMLEventProcessor
if(!event.target) {
n = function () {
sendEventToSelf(sendOptions, event);
};
} else {
n = function(){
var options = {
method : 'POST',
json : event,
url : targetUrl
};
debug('sending event', options);
request(options,function(error, response, body ) {
//ignore the response for now
//TODO: if error results, enqueue error.communication to external queue (event though SCXML spec says inner queue)
});
};
}
//FIXME: make it possible to support send/@dely on other send types
var timeoutId = setTimeout(n, options.delay || 0);
if (options.sendid) timeoutMap[options.sendid] = timeoutId;
break;
default:
console.log('wrong processor', event.type);
break;
}
},
customCancel : function(sendid) {
var timeoutId = timeoutMap[sendid];
if(timeoutId) {
clearTimeout(timeoutId);
delete timeoutMap[sendid];
}
}
});
instance.registerListener({
onEntry: publishChanges.bind(this,'onEntry'),
onExit: publishChanges.bind(this,'onExit')
});
//Don't start the instance from the beginning if there no snapshot
if(!snapshot) instance.start();
//Process the event
if(event){
event.uuid = eventUuid; //tag event with the uuid for <respond>
instance.gen(event);
}
//Get final configuration
var conf = instance.getSnapshot();
if(!wait){
respond(eventUuid, conf);
}
done(null, conf);
//TODO: refactor to go through redis. otherwise, this won't be stateless/scalable.
function publishChanges (eventName, stateId) {
var subscriptions = instanceSubscriptions[instanceId];
if(!subscriptions) return;
subscriptions.forEach(function (response) {
response.write('event: ' + eventName +'\n');
response.write('data: ' + stateId + '\n\n');
});
}
}
server.createInstance = function (id, done) {
var instanceId = id || uuid.v1();
done(null, instanceId);
};
server.startInstance = function (id, sendOptions, eventUuid, done) {
react(id, null, null, sendOptions, eventUuid, done);
};
server.sendEvent = function (id, event, sendOptions, eventUuid, done) {
if(event.name === 'system.start') {
server.startInstance(id, sendOptions, eventUuid, done);
} else {
db.getInstance(modelName, id, function (err, snapshot) {
react(id, snapshot, event, sendOptions, eventUuid, done);
});
}
};
server.registerListener = function (id, response, done) {
instanceSubscriptions[id] = instanceSubscriptions[id] || [];
instanceSubscriptions[id].push(response);
done();
};
//This is a much needed interface on instance deletion
server.unregisterAllListeners = function (id, done) {
var subscriptions = instanceSubscriptions[id];
if(!subscriptions) return done();
subscriptions.forEach(function (response) {
response.end();
});
delete instanceSubscriptions[id];
if(done) done();
};
server.unregisterListener = function (id, response, done) {
//instanceSubscriptions
var subscriptions = instanceSubscriptions[id];
if(!subscriptions) return done();
//TODO: somehow remove using response object?
//Any unique identifier in response?
//http://stackoverflow.com/a/26707009/1744033
instanceSubscriptions[id] = subscriptions.filter(function (subResponse) {
if(response.uniqueId === subResponse.uniqueId) {
response.end();
return false;
}
return true;
});
if(done) done();
};
server.getInstanceSnapshot = completeInstantly;
server.deleteInstance = completeInstantly;
return server;
};
// send action
var redis = require('redis'),
urlModule = require('url'),
debug = require('debug')('SCXMLD-stateless-simulation-provider');
if (process.env.REDIS_URL) {
var rtg = urlModule.parse(process.env.REDIS_URL);
var redisPublish = redis.createClient(rtg.port, rtg.hostname);
if(rtg.auth) redisPublish.auth(rtg.auth.split(':')[1]);
} else {
redisPublish = redis.createClient();
}
var timeoutMap = {};
function sendEventToSelf(serverSendOptions, event) {
if(serverSendOptions.cookies && Object.keys(serverSendOptions.cookies).length > 0) {
//Extra support for ExpressJs and request cookies
var jar = request.jar();
Object.keys(serverSendOptions.cookies).forEach(function (cookieName) {
jar.setCookie(request.cookie(cookieName + '=' + serverSendOptions.cookies[cookieName]), serverSendOptions.uri);
});
delete serverSendOptions.cookies;
serverSendOptions.jar = jar;
}
serverSendOptions.json = event;
debug('sending event to self', serverSendOptions);
request(serverSendOptions, function(error, response, body){
//debug('response',err,response.statusCode,body);
if(error) console.error('error sending event to server', error || response.body);
});
}
function respond(eventUuid, snapshot, customData){
console.log('Responding',eventUuid, snapshot, customData);
redisPublish.publish('/response/' + eventUuid, JSON.stringify({snapshot:snapshot, customData:customData}));
}
var targetRegex = /^scxml:\/\/response\/(.*)$/;
var scxmlSessionRegex = /^#_scxml_(.*)$/
var INTERNAL_TARGET = '#_internal';
var httpSessionRegex = /^http(s?):\/\//
function extractBaseUri(uri){
var url = urlModule.parse(uri);
var arr = pathModule.parse(url.pathname);
var pathComponents = url.pathname.split('/');
url.pathname = pathComponents.slice(0,-1).join('/');
delete url.path;
return url.format(url.path);
}
//TODO: store delaye messages in a more robust message queue
//TODO: send along sendid