total5 0.0.1-2 → 0.0.1-21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/flow.js CHANGED
@@ -1,4 +1,6 @@
1
- // Internal module
1
+ // Total.js Flow module
2
+ // The MIT License
3
+ // Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
2
4
 
3
5
  'use strict';
4
6
 
@@ -8,8 +10,7 @@ const REG_BK = /-bk|_bk/i;
8
10
  var FS = exports;
9
11
 
10
12
  FS.module = require('./flow-flowstream');
11
- FS.version = 1;
12
- FS.proxies = {};
13
+ FS.version = 40;
13
14
  FS.db = {};
14
15
  FS.worker = false;
15
16
  FS.instances = {};
@@ -53,26 +54,39 @@ FS.onsave = function(data) {
53
54
  FS.$events.save && FS.emit('save', data);
54
55
  };
55
56
 
56
- FS.reload = function(flow, restart) {
57
+ FS.remove = function(id) {
57
58
 
58
- var prev = FS.db[flow.id];
59
- if (!prev)
60
- return;
59
+ var tmp = FS.db[id];
61
60
 
62
- if (prev.worker) {
63
- if (prev.proxypath !== flow.proxypath) {
64
- if (FS.proxies[prev.proxypath]) {
65
- FS.proxies[prev.proxypath].remove();
66
- delete FS.proxies[prev.proxypath];
67
- }
68
- }
61
+ if (FS.instances[id]) {
62
+ FS.instances[id].destroy();
63
+ delete FS.instances[id];
69
64
  }
70
65
 
71
- if (flow.worker && prev.proxypath !== flow.proxypath)
72
- FS.proxies[flow.proxypath] = F.proxy(flow.proxypath, flow.unixsocket);
66
+ if (tmp)
67
+ delete FS.db[id];
68
+
69
+ FS.$events.remove && FS.emit('remove', tmp);
70
+ };
71
+
72
+ FS.reload = function(flow, restart = false) {
73
+
74
+ var prev = FS.instances[flow.id];
75
+ if (!prev)
76
+ return false;
73
77
 
74
78
  FS.db[flow.id] = flow;
75
- FS.instance[flow.id].restart(flow, restart);
79
+
80
+ var instance = FS.instances[flow.id];
81
+ instance.workertype = flow.worker;
82
+ instance.proxypath = flow.proxypath;
83
+
84
+ if (restart)
85
+ instance.restart();
86
+ else
87
+ instance.reload(flow);
88
+
89
+ return true;
76
90
  };
77
91
 
78
92
  FS.init = function(directory, callback) {
@@ -105,9 +119,7 @@ FS.init = function(directory, callback) {
105
119
  if (response) {
106
120
  response = response.parseJSON();
107
121
  response.directory = directory;
108
- FS.load(response, function() {
109
- next();
110
- });
122
+ FS.load(response, () => next());
111
123
  } else
112
124
  next();
113
125
  });
@@ -126,27 +138,18 @@ FS.load = function(flow, callback) {
126
138
  // flow.memory {Number}
127
139
  // flow.proxypath {String}
128
140
 
141
+ if (FS.instances[flow.id]) {
142
+ FS.reload(flow);
143
+ callback && setImmediate(callback, null, FS.instances[flow.id]);
144
+ return;
145
+ }
146
+
129
147
  var id = flow.id;
130
148
  flow.directory = flow.directory || F.path.root('/flowstream/');
131
149
  FS.db[id] = flow;
132
150
 
133
151
  FS.module.init(flow, flow.worker, function(err, instance) {
134
152
 
135
- FS.$events.load && FS.emit('load', instance, flow);
136
-
137
- if (flow.worker && flow.proxypath) {
138
-
139
- // Removes old
140
- if (FS.proxies[flow.proxypath])
141
- FS.proxies[flow.proxypath].remove();
142
-
143
- // Registers new
144
- FS.proxies[flow.proxypath] = F.proxy(flow.proxypath, flow.unixsocket);
145
-
146
- }
147
-
148
- // instance.httprouting();
149
- instance.ondone = callback;
150
153
  instance.onerror = FS.onerror;
151
154
 
152
155
  instance.onsave = function(data) {
@@ -156,6 +159,12 @@ FS.load = function(flow, callback) {
156
159
  };
157
160
 
158
161
  FS.instances[id] = instance;
162
+
163
+ // instance.httprouting();
164
+ instance.ondone = function(err) {
165
+ FS.$events.load && FS.emit('load', instance, flow);
166
+ callback && callback(err, err ? null : instance);
167
+ };
159
168
  });
160
169
 
161
170
  };
@@ -168,7 +177,7 @@ FS.notify = function(controller, id) {
168
177
  if (instance) {
169
178
  var obj = {};
170
179
  obj.id = arr[1];
171
- obj.method = $.req.method;
180
+ obj.method = $.method;
172
181
  obj.headers = $.headers;
173
182
  obj.query = $.query;
174
183
  obj.body = $.body;
@@ -191,6 +200,23 @@ FS.notify = function(controller, id) {
191
200
  $.success();
192
201
  };
193
202
 
203
+ FS.restart = function(id) {
204
+ let item = FS.instances[id];
205
+ if (item) {
206
+ if (item.flow) {
207
+ if (item.flow.terminate)
208
+ item.flow.terminate();
209
+ else
210
+ item.flow.kill(9);
211
+ return true;
212
+ }
213
+ }
214
+ };
215
+
216
+ FS.save = function(data) {
217
+ FS.onsave(data);
218
+ };
219
+
194
220
  FS.ping = function() {
195
221
  // ping all services
196
222
  for (let key in FS.instances) {
package/flowstream.js CHANGED
@@ -277,7 +277,7 @@ function variables(str, data, encoding) {
277
277
  if (typeof(str) !== 'string' || str.indexOf('{') === -1)
278
278
  return str;
279
279
 
280
- var main = this.main;
280
+ var main = this.main ? this.main : this;
281
281
 
282
282
  if (data == null || data == true)
283
283
  data = this;
@@ -754,7 +754,7 @@ FP.destroy = function() {
754
754
  self.emit('destroy');
755
755
  self.meta = null;
756
756
  self.$events = null;
757
- delete F.flows[self.name];
757
+ delete F.flowstreams[self.name];
758
758
  });
759
759
 
760
760
  };
@@ -896,8 +896,8 @@ FP.unregister = function(name, callback) {
896
896
  callback && callback();
897
897
  self.clean();
898
898
  });
899
- } else
900
- callback && callback();
899
+ } else if (callback)
900
+ callback();
901
901
 
902
902
  return self;
903
903
  };
@@ -1218,27 +1218,43 @@ FP.unload = function(callback) {
1218
1218
  return self;
1219
1219
  };
1220
1220
 
1221
- FP.load = function(components, design, callback, asfile) {
1221
+ FP.loadvariables = function(variables, type = 'variables') {
1222
+
1223
+ // @type {String} variables (default), variables2, secrets
1224
+
1225
+ var self = this;
1226
+ if (JSON.stringify(self[type]) !== JSON.stringify(variables)) {
1227
+ self[type] = variables;
1228
+ for (let key in self.meta.flow) {
1229
+ let instance = self.meta.flow[key];
1230
+ instance[type] && instance[type](self[type]);
1231
+ instance.vary && instance.vary(type);
1232
+ }
1233
+ }
1234
+ return self;
1235
+ };
1236
+
1237
+ FP.load = function(data, callback) {
1222
1238
 
1223
1239
  var self = this;
1224
1240
  if (self.loading) {
1225
- setTimeout(() => self.load(components, design, callback), 200);
1241
+ setTimeout(() => self.load(data, callback), 200);
1226
1242
  return self;
1227
1243
  }
1228
1244
 
1229
- self.loading = 10000;
1245
+ self.loading = 100000;
1230
1246
  self.unload(function() {
1231
1247
 
1232
- var keys = Object.keys(components);
1248
+ var keys = Object.keys(data.components);
1233
1249
  var error = new F.ErrorBuilder();
1234
1250
 
1235
1251
  keys.wait(function(key, next) {
1236
- var body = components[key];
1252
+ var body = data.components[key];
1237
1253
  if (typeof(body) === 'string' && body.indexOf('<script ') !== -1) {
1238
1254
  self.add(key, body, function(err) {
1239
1255
  err && error.push(err);
1240
1256
  next();
1241
- }, asfile);
1257
+ }, data.asfiles);
1242
1258
  } else {
1243
1259
  error.push('Invalid component: ' + key);
1244
1260
  next();
@@ -1247,11 +1263,11 @@ FP.load = function(components, design, callback, asfile) {
1247
1263
 
1248
1264
  // Loads design
1249
1265
  self.inc(0);
1250
- self.use(design, function(err) {
1266
+ self.use(data.design, function(err) {
1251
1267
  self.inc(0);
1252
1268
  err && error.push(err);
1253
- callback && callback(err);
1254
1269
  self.clean();
1270
+ callback && callback(err);
1255
1271
  });
1256
1272
 
1257
1273
  });
@@ -1260,6 +1276,59 @@ FP.load = function(components, design, callback, asfile) {
1260
1276
  return self;
1261
1277
  };
1262
1278
 
1279
+ FP.replace = variables;
1280
+ FP.rewrite = function(data, callback) {
1281
+
1282
+ var self = this;
1283
+ if (self.loading) {
1284
+ setTimeout(() => self.replace(data, callback), 200);
1285
+ return self;
1286
+ }
1287
+
1288
+ self.loading = 100000;
1289
+
1290
+ var keys = Object.keys(data.components);
1291
+ var error = new F.ErrorBuilder();
1292
+ var processed = {};
1293
+
1294
+ keys.wait(function(key, next) {
1295
+ var body = data.components[key];
1296
+ processed[key] = 1;
1297
+ if (typeof(body) === 'string' && body.indexOf('<script ') !== -1) {
1298
+ self.add(key, body, function(err) {
1299
+ err && error.push(err);
1300
+ next();
1301
+ }, data.asfiles);
1302
+ } else {
1303
+ error.push('Invalid component: ' + key);
1304
+ next();
1305
+ }
1306
+ }, function() {
1307
+ // Removed non-exist components
1308
+ Object.keys(self.meta.components).wait(function(key, next) {
1309
+ if (processed[key])
1310
+ next();
1311
+ else
1312
+ self.unregister(key, next);
1313
+ }, function() {
1314
+ // Loads design
1315
+ self.inc(0);
1316
+ self.use(data.design, function(err) {
1317
+
1318
+ if (data.variables)
1319
+ self.loadvariables(F.TUtils.clone(data.variables));
1320
+
1321
+ self.inc(0);
1322
+ err && error.push(err);
1323
+ self.clean();
1324
+ callback && callback(err);
1325
+ });
1326
+ });
1327
+ });
1328
+
1329
+ return self;
1330
+ };
1331
+
1263
1332
  FP.insert = function(schema, callback) {
1264
1333
  var self = this;
1265
1334
  if (callback)
@@ -1984,8 +2053,8 @@ FP.components = function(prepare_export) {
1984
2053
  return arr;
1985
2054
  };
1986
2055
 
1987
- exports.create = function(name, errorhandler) {
1988
- let flowstream = new FlowStream(name, errorhandler);
1989
- F.flowstreams[name] =flowstream;
2056
+ exports.create = function(id, errorhandler) {
2057
+ let flowstream = new FlowStream(id, errorhandler);
2058
+ F.flowstreams[id] = flowstream;
1990
2059
  return flowstream;
1991
2060
  };
package/global.js CHANGED
@@ -17,21 +17,26 @@ global.NPMINSTALL = F.npminstall;
17
17
  global.COMPONENTATOR = F.componentator;
18
18
  global.MERGE = F.merge;
19
19
  global.TOUCH = F.touch;
20
+ global.LOCALIZE = F.localize;
20
21
  global.AUTH = F.auth;
21
22
  global.CLEANUP = F.cleanup;
22
23
  global.NEWDB = F.newdb;
23
24
  global.REQUIRE = F.require;
24
25
  global.CRON = F.cron;
25
26
  global.UID = F.uid;
27
+ global.SUCCESS = value => DEF.onSuccess(value);
26
28
  global.MEMORIZE = F.memorize;
27
29
  global.AUDIT = F.audit;
28
30
  global.TRANSLATE = F.translate;
31
+ global.TRANSFORM = F.transform;
32
+ global.NEWTRANSFORM = F.newtransform;
33
+ global.MIDDLEWARE = F.middleware;
29
34
  global.DATA = new F.TQueryBuilder.Controller(true);
30
35
  global.DB = () => new F.TQueryBuilder.Controller();
31
36
  global.CACHE = F.cache;
32
37
  global.NEWACTION = F.TBuilders.newaction;
33
38
  global.NEWSCHEMA = F.TBuilders.newschema;
34
- global.ACTION = F.TBuilders.action;
39
+ global.ACTION = global.EXEC = F.TBuilders.action;
35
40
  global.TEMPLATE = F.template;
36
41
  global.FILESTORAGE = F.filestorage;
37
42
  global.WEBSOCKETCLIENT = F.websocketclient;
@@ -50,6 +55,7 @@ global.MAIL = F.mail;
50
55
  global.Mail = F.TMail.Mailer;
51
56
  global.RESTBuilder = F.TBuilders.RESTBuilder;
52
57
  global.ErrorBuilder = F.TBuilders.ErrorBuilder;
58
+ global.DOWNLOAD = F.download;
53
59
 
54
60
  global.BLOCKED = function($, limit, expire) {
55
61
 
@@ -92,7 +98,82 @@ global.LDAP = function(opt, callback) {
92
98
  };
93
99
 
94
100
  global.CORS = function(origin) {
95
- CONF.$cors = origin || '*';
101
+ if (origin && origin[0] === '+') {
102
+ if (CONF.$cors !== '*')
103
+ CONF.$cors = (CONF.$cors ? ',' : '') + origin.substring(1);
104
+ } else
105
+ CONF.$cors = origin || '*';
106
+ F.emit('$cors');
107
+ };
108
+
109
+ global.AJAX = function(url, data, callback) {
110
+
111
+ if (typeof(data) === 'function') {
112
+ callback = data;
113
+ data = null;
114
+ }
115
+
116
+ if (!callback)
117
+ return new Promise((resolve, reject) => global.AJAX(url, data, (err, response) => err ? reject(err) : resolve(response)));
118
+
119
+ var index = url.indexOf(' ');
120
+ var opt = {};
121
+
122
+ if (index !== -1) {
123
+ opt.method = url.substring(0, index);
124
+ opt.url = url.substring(index + 1);
125
+ } else {
126
+ opt.method = 'GET';
127
+ opt.url = url;
128
+ }
129
+
130
+ if (data) {
131
+ opt.type = 'json';
132
+ opt.body = JSON.stringify(data);
133
+ }
134
+
135
+ opt.callback = function(err, response) {
136
+
137
+ if (err) {
138
+ callback && callback(err, null, response);
139
+ return;
140
+ }
141
+
142
+ var type = err ? '' : response.headers['content-type'] || '';
143
+ if (type) {
144
+ var index = type.lastIndexOf(';');
145
+ if (index !== -1)
146
+ type = type.substring(0, index).trim();
147
+ }
148
+
149
+ var value = null;
150
+
151
+ switch (type.toLowerCase()) {
152
+ case 'text/xml':
153
+ case 'application/xml':
154
+ value = response.body ? response.body.parseXML(self.$replace ? true : false) : {};
155
+ break;
156
+ case 'application/x-www-form-urlencoded':
157
+ value = response.body ? DEF.parsers.urlencoded(response.body) : {};
158
+ break;
159
+ case 'application/json':
160
+ case 'text/json':
161
+ value = response.body ? response.body.parseJSON(true) : null;
162
+ break;
163
+ default:
164
+ value = response.body;
165
+ break;
166
+ }
167
+
168
+ if (response.status >= 400) {
169
+ err = value;
170
+ value = null;
171
+ }
172
+
173
+ delete response.body;
174
+ callback && callback(err, value, response);
175
+ };
176
+ F.TUtils.request(opt);
96
177
  };
97
178
 
98
179
  // Utils
@@ -103,6 +184,8 @@ global.REQUEST = F.TUtils.request;
103
184
  global.HASH = (val, type) => val.hash(type ? type : true);
104
185
  global.DIFFARR = F.TUtils.diffarr;
105
186
  global.CLONE = F.TUtils.clone;
187
+ global.COPY = F.TUtils.copy;
188
+ global.QUERIFY = F.TUtils.querify;
106
189
 
107
190
  // TMS
108
191
  global.SUBSCRIBE = F.TTMS.subscribe;
@@ -123,4 +206,62 @@ global.NOSQL = F.TNoSQL.nosql;
123
206
  // Workers
124
207
  global.NEWFORK = F.TWorkers.createfork;
125
208
  global.NEWTHREAD = F.TWorkers.createthread;
126
- global.NEWTHREADPOOL = F.TWorkers.createpool;
209
+ global.NEWTHREADPOOL = F.TWorkers.createpool;
210
+
211
+ // Custom global functionality
212
+ function timeout2(key, a, b, c, d, e) {
213
+ let tmp = F.temporary.internal[key];
214
+ if (tmp) {
215
+ tmp.callback(a, b, c, d, e);
216
+ delete F.temporary.internal[key];
217
+ }
218
+ }
219
+
220
+ global.setTimeout2 = function(id, callback, timeout, limit, a, b, c, d, e) {
221
+
222
+ let key = 'timeout2' + id;
223
+ let internal = F.temporary.internal;
224
+ let cache = internal[key];
225
+
226
+ if (limit > 0) {
227
+
228
+ if (cache && cache.count >= limit) {
229
+ clearTimeout(cache.timer);
230
+ delete internal[key];
231
+ callback();
232
+ return;
233
+ }
234
+
235
+ if (cache) {
236
+ clearTimeout(cache.timer);
237
+ cache.count++;
238
+ } else
239
+ cache = internal[key] = {};
240
+
241
+ cache.callback = callback;
242
+ cache.timer = setTimeout(timeout2, timeout, key, a, b, c, d, e);
243
+
244
+ } else {
245
+
246
+ if (cache)
247
+ clearTimeout(cache.timer);
248
+ else
249
+ cache = internal[key] = {};
250
+
251
+ cache.callback = callback;
252
+ cache.timer = setTimeout(timeout2, timeout, key, a, b, c, d, e);
253
+ }
254
+ };
255
+
256
+ global.clearTimeout2 = function(id) {
257
+
258
+ let key = 'timeout2' + id;
259
+ let tmp = F.temporary.internal[key];
260
+
261
+ if (tmp) {
262
+ clearTimeout(tmp.timer);
263
+ delete F.temporary.internal[key];
264
+ }
265
+
266
+ return !!tmp;
267
+ };
package/http.js CHANGED
@@ -63,7 +63,7 @@ exports.listen = function(req, res) {
63
63
  // Pending requests
64
64
  F.temporary.pending.push(ctrl);
65
65
 
66
- if (!ctrl.uri.file && (F.def.onCORS || F.config.$cors)) {
66
+ if (ctrl.headers.origin && (F.def.onCORS || F.config.$cors)) {
67
67
  if (F.TRouting.lookupcors(ctrl))
68
68
  ctrl.$route();
69
69
  } else
package/image.js CHANGED
@@ -1,3 +1,7 @@
1
+ // Total.js Images
2
+ // The MIT License
3
+ // Copyright 2016-2023 (c) Peter Širka <petersirka@gmail.com>
4
+
1
5
  'use strict';
2
6
 
3
7
  const D = F.iswindows ? '"' : '\'';
package/images.js CHANGED
@@ -196,7 +196,7 @@ ImageProto.measure = function(callback) {
196
196
 
197
197
  F.stats.performance.open++;
198
198
  var extension = self.filename.substring(index).toLowerCase();
199
- var stream = require('fs').createReadStream(self.filename, { start: 0, end: (extension === '.jpg' || extension === '.webp') ? 40000 : 24 });
199
+ var stream = F.Fs.createReadStream(self.filename, { start: 0, end: (extension === '.jpg' || extension === '.webp') ? 40000 : 24 });
200
200
 
201
201
  stream.on('data', function(buffer) {
202
202