total5 0.0.1 → 0.0.2

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/global.js ADDED
@@ -0,0 +1,274 @@
1
+ // Total.js Globals
2
+ // The MIT License
3
+ // Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
4
+
5
+ 'use strict';
6
+
7
+ global.ON = (name, fn) => F.on(name, fn);
8
+ global.ONCE = (name, fn) => F.once(name, fn);
9
+ global.EMIT = (name, a, b, c, d, e, f, g) => F.emit(name, a, b, c, d, e, f, g);
10
+ global.OFF = (name, fn) => F.off(name, fn);
11
+ global.ROUTE = F.TRouting.route;
12
+ global.PROXY = F.TRouting.proxy;
13
+ global.print = console.log;
14
+ global.LOAD = F.load;
15
+ global.LOADCONFIG = F.loadconfig;
16
+ global.LOADRESOURCE = F.loadresource;
17
+ global.SHELL = F.shell;
18
+ global.NPMINSTALL = F.npminstall;
19
+ global.COMPONENTATOR = F.componentator;
20
+ global.MERGE = F.merge;
21
+ global.TOUCH = F.touch;
22
+ global.LOCALIZE = F.localize;
23
+ global.AUTH = F.auth;
24
+ global.CLEANUP = F.cleanup;
25
+ global.NEWDB = F.newdb;
26
+ global.IMPORT = F.import;
27
+ global.REQUIRE = F.require;
28
+ global.CRON = F.cron;
29
+ global.UID = F.uid;
30
+ global.SUCCESS = value => DEF.onSuccess(value);
31
+ global.MEMORIZE = F.memorize;
32
+ global.AUDIT = F.audit;
33
+ global.TRANSLATE = F.translate;
34
+ global.TRANSFORM = F.transform;
35
+ global.NEWTRANSFORM = F.newtransform;
36
+ global.MIDDLEWARE = F.middleware;
37
+ global.DATA = new F.TQueryBuilder.Controller(true);
38
+ global.DB = () => new F.TQueryBuilder.Controller();
39
+ global.CACHE = F.cache;
40
+ global.NEWACTION = F.TBuilders.newaction;
41
+ global.NEWSCHEMA = F.TBuilders.newschema;
42
+ global.ACTION = global.EXEC = F.TBuilders.action;
43
+ global.TEMPLATE = F.template;
44
+ global.FILESTORAGE = F.filestorage;
45
+ global.WEBSOCKETCLIENT = F.websocketclient;
46
+ global.PAUSESERVER = F.pauseserver;
47
+ global.MODS = F.modules;
48
+ global.PLUGINS = F.plugins;
49
+ global.DECRYPT = F.decrypt;
50
+ global.ENCRYPT = F.encrypt;
51
+ global.DECRYPTREQ = F.decryptreq;
52
+ global.ENCRYPTREQ = F.encryptreq;
53
+ global.PATH = F.path;
54
+ global.UNAUTHORIZED = F.unauthorized;
55
+ global.LOGMAIL = F.logmail;
56
+ global.HTMLMAIL = F.htmlmail;
57
+ global.MAIL = F.mail;
58
+ global.Mail = F.TMail.Mailer;
59
+ global.RESTBuilder = F.TBuilders.RESTBuilder;
60
+ global.ErrorBuilder = F.TBuilders.ErrorBuilder;
61
+ global.DOWNLOAD = F.download;
62
+ global.OPENCLIENT = (url, id) => require('./openclient').create(url, id);
63
+ global.NEWMACRO = (str, nocompile, isasync) => require('./macros').compile(str, nocompile, isasync);
64
+
65
+ global.BLOCKED = function($, limit, expire) {
66
+
67
+ var key = $.ip;
68
+
69
+ if (limit === -1 || limit === null) {
70
+ delete F.temporary.bans[key];
71
+ return;
72
+ }
73
+
74
+ if (!limit)
75
+ limit = 5;
76
+
77
+ var item = F.temporary.bans[key];
78
+ if (item) {
79
+ if (item.count > limit)
80
+ return true;
81
+ item.count++;
82
+ } else {
83
+ item = F.temporary.bans[key] = {};
84
+ item.expire = NOW.add(expire || '15 minutes');
85
+ item.count = 1;
86
+ }
87
+ };
88
+
89
+ global.ERROR = function(name) {
90
+ return name == null ? F.errorcallback : function(err) {
91
+ err && F.error(err, name);
92
+ };
93
+ };
94
+
95
+ global.LDAP = function(opt, callback) {
96
+ if (!opt.ldap.port)
97
+ opt.ldap.port = 389;
98
+ var Ldap = require('./ldap');
99
+ if (callback)
100
+ Ldap.load(opt, callback);
101
+ else
102
+ return new Promise((resolve, reject) => Ldap.load(opt, (err, res) => err ? reject(err) : resolve(res)));
103
+ };
104
+
105
+ global.CORS = function(origin) {
106
+ if (origin && origin[0] === '+') {
107
+ if (F.config.$cors !== '*')
108
+ F.config.$cors = (F.config.$cors ? ',' : '') + origin.substring(1);
109
+ } else
110
+ F.config.$cors = origin || '*';
111
+ F.emit('$cors');
112
+ };
113
+
114
+ global.AJAX = function(url, data, callback) {
115
+
116
+ if (typeof(data) === 'function') {
117
+ callback = data;
118
+ data = null;
119
+ }
120
+
121
+ if (!callback)
122
+ return new Promise((resolve, reject) => global.AJAX(url, data, (err, response) => err ? reject(err) : resolve(response)));
123
+
124
+ var index = url.indexOf(' ');
125
+ var opt = {};
126
+
127
+ if (index !== -1) {
128
+ opt.method = url.substring(0, index);
129
+ opt.url = url.substring(index + 1);
130
+ } else {
131
+ opt.method = 'GET';
132
+ opt.url = url;
133
+ }
134
+
135
+ if (data) {
136
+ opt.type = 'json';
137
+ opt.body = JSON.stringify(data);
138
+ }
139
+
140
+ opt.headers = { 'X-Requested-With': 'XMLHttpRequest' };
141
+
142
+ opt.callback = function(err, response) {
143
+
144
+ if (err) {
145
+ callback && callback(err, null, response);
146
+ return;
147
+ }
148
+
149
+ var type = err ? '' : response.headers['content-type'] || '';
150
+ if (type) {
151
+ var index = type.lastIndexOf(';');
152
+ if (index !== -1)
153
+ type = type.substring(0, index).trim();
154
+ }
155
+
156
+ var value = null;
157
+
158
+ switch (type.toLowerCase()) {
159
+ case 'text/xml':
160
+ case 'application/xml':
161
+ value = response.body ? response.body.parseXML(self.$replace ? true : false) : {};
162
+ break;
163
+ case 'application/x-www-form-urlencoded':
164
+ value = response.body ? DEF.parsers.urlencoded(response.body) : {};
165
+ break;
166
+ case 'application/json':
167
+ case 'text/json':
168
+ value = response.body ? response.body.parseJSON(true) : null;
169
+ break;
170
+ default:
171
+ value = response.body;
172
+ break;
173
+ }
174
+
175
+ if (response.status >= 400) {
176
+ err = value;
177
+ value = null;
178
+ }
179
+
180
+ delete response.body;
181
+ callback && callback(err, value, response);
182
+ };
183
+ F.TUtils.request(opt);
184
+ };
185
+
186
+ // Utils
187
+ global.U = F.TUtils;
188
+ global.GUID = F.TUtils.guid;
189
+ global.NOOP = F.TUtils.noop;
190
+ global.REQUEST = F.TUtils.request;
191
+ global.HASH = (val, type) => val.hash(type ? type : true);
192
+ global.DIFFARR = F.TUtils.diffarr;
193
+ global.CLONE = F.TUtils.clone;
194
+ global.COPY = F.TUtils.copy;
195
+ global.QUERIFY = F.TUtils.querify;
196
+
197
+ // TMS
198
+ global.SUBSCRIBE = F.TTMS.subscribe;
199
+ global.UNSUBSCRIBE = F.TTMS.unsubscribe;
200
+ global.PUBLISH = F.TTMS.publish;
201
+ global.NEWPUBLISH = F.TTMS.newpublish;
202
+ global.NEWSUBSCRIBE = F.TTMS.newsubscribe;
203
+ global.NEWCALL = F.TTMS.newcall;
204
+ global.TMSCLIENT = F.TTMS.client;
205
+
206
+ // API
207
+ global.API = (name, schema, data, $) => F.TApi.exec(name, schema, data, $);
208
+ global.NEWAPI = (name, callback) => F.TApi.newapi(name, callback);
209
+
210
+ // NoSQL
211
+ global.NOSQL = F.TNoSQL.nosql;
212
+
213
+ // Workers
214
+ global.NEWFORK = F.TWorkers.createfork;
215
+ global.NEWTHREAD = F.TWorkers.createthread;
216
+ global.NEWTHREADPOOL = F.TWorkers.createpool;
217
+
218
+ // Custom global functionality
219
+ function timeout2(key, a, b, c, d, e) {
220
+ let tmp = F.temporary.internal[key];
221
+ if (tmp) {
222
+ tmp.callback(a, b, c, d, e);
223
+ delete F.temporary.internal[key];
224
+ }
225
+ }
226
+
227
+ global.setTimeout2 = function(id, callback, timeout, limit, a, b, c, d, e) {
228
+
229
+ let key = 'timeout2' + id;
230
+ let internal = F.temporary.internal;
231
+ let cache = internal[key];
232
+
233
+ if (limit > 0) {
234
+
235
+ if (cache && cache.count >= limit) {
236
+ clearTimeout(cache.timer);
237
+ delete internal[key];
238
+ callback();
239
+ return;
240
+ }
241
+
242
+ if (cache) {
243
+ clearTimeout(cache.timer);
244
+ cache.count++;
245
+ } else
246
+ cache = internal[key] = {};
247
+
248
+ cache.callback = callback;
249
+ cache.timer = setTimeout(timeout2, timeout, key, a, b, c, d, e);
250
+
251
+ } else {
252
+
253
+ if (cache)
254
+ clearTimeout(cache.timer);
255
+ else
256
+ cache = internal[key] = {};
257
+
258
+ cache.callback = callback;
259
+ cache.timer = setTimeout(timeout2, timeout, key, a, b, c, d, e);
260
+ }
261
+ };
262
+
263
+ global.clearTimeout2 = function(id) {
264
+
265
+ let key = 'timeout2' + id;
266
+ let tmp = F.temporary.internal[key];
267
+
268
+ if (tmp) {
269
+ clearTimeout(tmp.timer);
270
+ delete F.temporary.internal[key];
271
+ }
272
+
273
+ return !!tmp;
274
+ };