-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
flow.js
243 lines (187 loc) · 5.01 KB
/
flow.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
// Total.js Flow module
// The MIT License
// Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
'use strict';
const PING = { TYPE: 'ping' };
const REG_BK = /-bk|_bk/i;
var FS = exports;
FS.module = require('./flow-flowstream');
FS.version = 40;
FS.db = {};
FS.worker = false;
FS.instances = {};
FS.onerror = function(err, source, id, componentid, stack) {
var flow = FS.db[this.id];
var empty = '---';
var output = '';
output += '|------------- FlowStreamError "' + id + '": ' + new Date().format('yyyy-MM-dd HH:mm:ss') + '\n';
output += '| FlowStream: ' + flow.name + '\n';
output += '| Source: ' + (source || empty) + '\n';
output += '| Instance ID: ' + (id || empty) + '\n';
output += '| Component ID: ' + (componentid || empty) + '\n';
output += '| ' + err.toString();
if (stack) {
output += '\n|---- Stack: ----\n';
output += stack;
}
console.error(output);
if (F.$events.flowstream_error) {
var meta = {};
meta.id = flow.id;
meta.name = flow.name;
meta.error = err;
meta.source = source;
meta.instanceid = id;
meta.componentid = componentid;
F.emit('flowstream_error', meta);
}
};
F.TUtils.EventEmitter2(FS);
FS.onsave = function(data) {
// @data {Object} flowstream schema
FS.$events.save && FS.emit('save', data);
};
FS.remove = function(id) {
var tmp = FS.db[id];
if (FS.instances[id]) {
FS.instances[id].destroy();
delete FS.instances[id];
}
if (tmp)
delete FS.db[id];
FS.$events.remove && FS.emit('remove', tmp);
};
FS.reload = function(flow, restart = false) {
var prev = FS.instances[flow.id];
if (!prev)
return false;
FS.db[flow.id] = flow;
var instance = FS.instances[flow.id];
instance.workertype = flow.worker;
instance.proxypath = flow.proxypath;
if (restart)
instance.restart();
else
instance.reload(flow);
return true;
};
FS.init = function(directory, callback) {
if (typeof(directory) === 'function') {
callback = directory;
directory = null;
}
if (!directory)
directory = F.path.root('flowstreams');
F.Fs.readdir(directory, function(err, files) {
if (err) {
callback && callback();
return;
}
var load = [];
for (var m of files) {
var index = m.lastIndexOf('.');
if (index !== -1 && m.substring(index).toLowerCase() === '.flow' && !REG_BK.test(m))
load.push(m);
}
load.wait(function(filename, next) {
F.Fs.readFile(F.path.join(directory, filename), 'utf8', function(err, response) {
if (response) {
response = response.parseJSON();
response.directory = directory;
FS.load(response, () => next());
} else
next();
});
}, callback);
});
};
FS.load = function(flow, callback) {
if (!flow.id)
flow.id = F.TUtils.random_string(10).toLowerCase();
// flow.directory {String}
// flow.asfiles {Boolean}
// flow.worker {String/Boolean}
// flow.memory {Number}
// flow.proxypath {String}
if (FS.instances[flow.id]) {
FS.reload(flow);
callback && setImmediate(callback, null, FS.instances[flow.id]);
return;
}
var id = flow.id;
flow.directory = flow.directory || F.path.root('/flowstream/');
FS.db[id] = flow;
FS.module.init(flow, flow.worker, function(err, instance) {
instance.onerror = FS.onerror;
instance.onsave = function(data) {
data.unixsocket = flow.unixsocket;
FS.db[id] = data;
FS.onsave(data);
};
FS.instances[id] = instance;
// instance.httprouting();
instance.ondone = function(err) {
FS.$events.load && FS.emit('load', instance, flow);
callback && callback(err, err ? null : instance);
};
});
};
FS.notify = function(controller, id) {
var $ = controller;
var arr = id.split('-');
var instance = FS.instances[arr[0]];
if (instance) {
var obj = {};
obj.id = arr[1];
obj.method = $.method;
obj.headers = $.headers;
obj.query = $.query;
obj.body = $.body;
obj.url = $.url;
obj.ip = $.ip;
obj.params = arr.length > 2 ? arr.slice(2) : EMPTYOBJECT;
arr[1] && instance.notify(arr[1], obj);
instance.flow && instance.flow.$socket && instance.flow.$socket.send({ TYPE: 'flow/notify', data: obj });
}
if ($.query.REDIRECT) {
$.redirect($.query.REDIRECT);
return;
}
var accept = $.headers.accept;
if (accept && accept.indexOf('html') !== -1)
$.html('<html><body style="font-family:Arial;font-size:11px;color:#777;background-color:#FFF">Close the window<script>window.close();</script></body></html>');
else
$.success();
};
FS.restart = function(id) {
let item = FS.instances[id];
if (item) {
if (item.flow) {
if (item.flow.terminate)
item.flow.terminate();
else
item.flow.kill(9);
return true;
}
}
};
FS.save = function(data) {
FS.onsave(data);
};
FS.ping = function() {
// ping all services
for (let key in FS.instances) {
let fs = FS.instances[key];
if (fs.isworkerthread && fs.flow && fs.flow.postMessage2)
fs.flow.postMessage2(PING);
}
};
FS.test = function(name, source, config, init) {
require('./flow-test.js').load(name, source, config, init);
};
global.Flow = FS;
global.FlowStream = exports;
setImmediate(function() {
FS.socket = FS.module.socket;
FS.client = FS.module.client;
});