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/bundles.js ADDED
@@ -0,0 +1,447 @@
1
+ // Total.js Bundles
2
+ // The MIT License
3
+ // Copyright 2018-2023 (c) Peter Širka <petersirka@gmail.com>
4
+
5
+ require('./index');
6
+
7
+ 'use strict';
8
+
9
+ const CONSOLE = process.argv.indexOf('--restart') === -1;
10
+ const INTERNAL = { '/sitemap': 1, '/versions': 1, '/workflows': 1, '/dependencies': 1, '/config': 1, '/config-release': 1, '/config-debug': 1 };
11
+ const REG_APPEND = /\/--[a-z0-9]+/i;
12
+ const REG_APPENDREPLACE = /\/--/g;
13
+ const REG_BK = /(-|_)bk\.bundle$/i;
14
+
15
+ var META = {};
16
+
17
+ META.version = 1;
18
+ META.created = new Date();
19
+ META.total = 'v' + F.version_header;
20
+ META.node = F.version_node;
21
+ META.files = [];
22
+ META.skip = false;
23
+ META.directories = [];
24
+ META.ignore = () => true;
25
+
26
+ function extract(callback) {
27
+
28
+ if (!callback)
29
+ return new Promise(resolve => extract(resolve));
30
+
31
+ var path = F.path.root();
32
+ var ignore = {};
33
+
34
+ if (CONSOLE) {
35
+ console.log('--------------------- BUNDLING ---------------------');
36
+ console.time('Done');
37
+ }
38
+
39
+ var isignore = false;
40
+
41
+ try {
42
+ META.ignore = makeignore(F.Fs.readFileSync(F.Path.join(path, '.bundleignore')).toString('utf8').split('\n'));
43
+ isignore = true;
44
+ } catch (e) {}
45
+
46
+ if (!isignore) {
47
+ try {
48
+ META.ignore = makeignore(F.Fs.readFileSync(F.Path.join(path, '.bundlesignore')).toString('utf8').split('\n'));
49
+ } catch (e) {}
50
+ }
51
+
52
+ ignore['/tmp/'] = 1;
53
+ ignore['/bundles/'] = 1;
54
+ ignore['/.src'] = 1;
55
+ ignore['/logs/'] = 1;
56
+ ignore['/node_modules/'] = 1;
57
+ ignore['/bundles.debug'] = 1;
58
+ ignore['/debug.pid'] = 1;
59
+ ignore['/debug.js.json'] = 1;
60
+ ignore['/release.js.json'] = 1;
61
+ ignore['/release.pid'] = 1;
62
+ ignore['/index.pid'] = 1;
63
+ ignore['/index.js.json'] = 1;
64
+ ignore['/package-lock.json'] = 1;
65
+ ignore['/superadmin.pid'] = 1;
66
+ ignore['/superadmin.socket'] = 1;
67
+
68
+ var Files = [];
69
+ var Dirs = [];
70
+ var Merge = [];
71
+ var Length = path.length;
72
+ var async = [];
73
+
74
+ async.push(cleanFiles);
75
+
76
+ async.push(function(next) {
77
+ META.skip && (async.length = 0);
78
+ next();
79
+ });
80
+
81
+ async.push(function(next) {
82
+ var target = F.path.root('/.src/');
83
+ F.TUtils.ls(F.path.root('/bundles/'), function(files) {
84
+ var dirs = {};
85
+ files.wait(function(filename, resume) {
86
+
87
+ if (!filename.endsWith('.bundle') || REG_BK.test(filename))
88
+ return resume();
89
+
90
+ if (CONSOLE)
91
+ console.log('-----', F.TUtils.getName(filename));
92
+
93
+ var dbpath = '/databases';
94
+ var pathupdate = '/updates/';
95
+ var pathstartup = '/startup';
96
+
97
+ F.restore(filename, target, resume, function(p, dir) {
98
+
99
+ if (dir) {
100
+ if (!p.startsWith(dbpath) && META.directories.indexOf(p) === -1)
101
+ META.directories.push(p);
102
+ } else {
103
+
104
+ var dirname = p.substring(0, p.length - F.TUtils.getName(p).length);
105
+ if (dirname && dirname !== '/')
106
+ dirs[dirname] = true;
107
+
108
+ // handle files in bundle to merge
109
+ var mergeme = 0;
110
+
111
+ if (REG_APPEND.test(p)) {
112
+ mergeme = 3;
113
+ p = p.replace(REG_APPENDREPLACE, '/');
114
+ }
115
+
116
+ var exists = null;
117
+ try {
118
+ exists = F.Fs.statSync(F.Path.join(target, p));
119
+ } catch (e) {}
120
+
121
+ if ((dirname === pathupdate || dirname === pathstartup) && !exists) {
122
+ try {
123
+ exists = F.Fs.statSync(F.Path.join(target, p + '_bk')) != null;
124
+ } catch (e) {}
125
+ }
126
+
127
+ // A specific file like DB file or startup file or update script
128
+ if (exists && (p.startsWith(dbpath) || p.startsWith(pathupdate) || p.startsWith(pathstartup)))
129
+ return false;
130
+
131
+ if (INTERNAL[p] || F.TUtils.getExtension(p) === 'resource' || mergeme) {
132
+ var hash = p.hash(true).toString(36);
133
+ Merge.push({ name: p, filename: F.Path.join(target, p + hash), type: mergeme });
134
+ META.files.push(p + hash);
135
+ return p + hash;
136
+ }
137
+
138
+ if (META.files.indexOf(p) === -1)
139
+ META.files.push(p);
140
+ }
141
+
142
+ return true;
143
+ });
144
+ }, function() {
145
+ dirs = Object.keys(dirs);
146
+ dirs.length && Dirs.push.apply(Dirs, dirs);
147
+ next();
148
+ });
149
+ });
150
+ });
151
+
152
+ async.push(function(next) {
153
+ if (Merge.length) {
154
+ copyFiles(Merge, function() {
155
+ for (var i = 0; i < Merge.length; i++) {
156
+ try {
157
+ F.Fs.unlinkSync(Merge[i].filename);
158
+ } catch(e) {}
159
+ }
160
+ next();
161
+ });
162
+ } else
163
+ next();
164
+ });
165
+
166
+ async.push(function(next) {
167
+ F.TUtils.ls(path, function(files, dirs) {
168
+
169
+ for (var i = 0, length = dirs.length; i < length; i++)
170
+ Dirs.push(normalize(dirs[i].substring(Length)));
171
+
172
+ for (var i = 0, length = files.length; i < length; i++) {
173
+ var file = files[i].substring(Length);
174
+ var type = 0;
175
+
176
+ if (file.startsWith(F.config.directory_databases) || file.startsWith('/flow/') || file.startsWith('/dashboard/'))
177
+ type = 1;
178
+ else if (REG_APPEND.test(file)) {
179
+ file = file.replace(REG_APPENDREPLACE, '/');
180
+ type = 3;
181
+ } else if (file.startsWith(F.config.directory_public))
182
+ type = 2;
183
+
184
+ Files.push({ name: file, filename: files[i], type: type });
185
+ }
186
+
187
+ next();
188
+ }, function(p) {
189
+ p = normalize(p.substring(Length));
190
+ return ignore[p] == null && p.substring(0, 2) !== '/.';
191
+ });
192
+ });
193
+
194
+ async.push(function(next) {
195
+ createDirectories(Dirs, () => copyFiles(Files, next));
196
+ });
197
+
198
+ async.push(function(next) {
199
+ F.Fs.writeFileSync(F.Path.join(F.path.root('/.src/'), 'bundle.json'), JSON.stringify(META, null, '\t'));
200
+ next();
201
+ });
202
+
203
+ async.async(function() {
204
+ CONSOLE && console.timeEnd('Done');
205
+ callback();
206
+ });
207
+
208
+ }
209
+
210
+ function makeignore(arr) {
211
+
212
+ var ext;
213
+ var code = ['var path=P.substring(0,P.lastIndexOf(\'/\')+1);', 'var ext=F.TUtils.getExtension(P);', 'var name=F.TUtils.getName(P).replace(\'.\'+ ext,\'\');'];
214
+
215
+ for (var i = 0; i < arr.length; i++) {
216
+ var item = arr[i];
217
+ var index = item.lastIndexOf('*.');
218
+
219
+ if (index !== -1) {
220
+ // only extensions on this path
221
+ ext = item.substring(index + 2);
222
+ item = item.substring(0, index);
223
+ code.push('tmp=\'{0}\';'.format(item));
224
+ code.push('if((!tmp||path===tmp)&&ext===\'{0}\')return;'.format(ext));
225
+ continue;
226
+ }
227
+
228
+ ext = F.TUtils.getExtension(item);
229
+ if (ext) {
230
+ // only filename
231
+ index = item.lastIndexOf('/');
232
+ code.push('tmp=\'{0}\';'.format(item.substring(0, index + 1)));
233
+ code.push('if(path===tmp&&F.TUtils.getName(\'{0}\').replace(\'.{1}\', \'\')===name&&ext===\'{1}\')return;'.format(item.substring(index + 1), ext));
234
+ continue;
235
+ }
236
+
237
+ // all nested path
238
+ code.push('if(path.startsWith(\'{0}\'))return;'.format(item.replace('*', '')));
239
+ }
240
+
241
+ code.push('return true');
242
+ return new Function('P', code.join(''));
243
+ }
244
+
245
+ function normalize(path) {
246
+ return F.isWindows ? path.replace(/\\/g, '/') : path;
247
+ }
248
+
249
+ function cleanFiles(callback) {
250
+
251
+ var path = F.path.root('/.src/');
252
+ var ignore = {};
253
+
254
+ ignore[F.config.directory_public] = 1;
255
+ ignore[F.config.directory_private] = 1;
256
+ ignore[F.config.directory_databases] = 1;
257
+
258
+ var meta;
259
+
260
+ try {
261
+
262
+ meta = F.Fs.readFileSync(F.Path.join(path, 'bundle.json')).toString('utf8').parseJSON(true) || {};
263
+
264
+ if (F.config.bundling === 'shallow') {
265
+ META.skip = true;
266
+ callback();
267
+ return;
268
+ }
269
+
270
+ } catch (e) {
271
+ meta = {};
272
+ }
273
+
274
+ if (meta.files && meta.files.length) {
275
+ for (var i = 0; i < meta.files.length; i++) {
276
+ var filename = meta.files[i];
277
+ var dir = filename.substring(0, filename.indexOf('/', 1) + 1);
278
+ if (!ignore[dir]) {
279
+ try {
280
+ F.Fs.unlinkSync(F.Path.join(path, filename));
281
+ } catch (e) {}
282
+ }
283
+ }
284
+ }
285
+
286
+ if (meta.directories && meta.directories.length) {
287
+ meta.directories.quicksort('length_desc');
288
+ for (var i = 0; i < meta.directories.length; i++) {
289
+ try {
290
+
291
+ var p = F.Path.join(path, meta.directories[i]);
292
+
293
+ if (ignore[meta.directories[i]])
294
+ continue;
295
+
296
+ while (true) {
297
+
298
+ var files = F.Fs.readdirSync(p);
299
+ if (files.length)
300
+ break;
301
+
302
+ try {
303
+ F.Fs.rmdirSync(p);
304
+ } catch (e) {
305
+ break;
306
+ }
307
+
308
+ p = F.Path.join(path, '..');
309
+
310
+ if (p.length < path || p === path)
311
+ break;
312
+ }
313
+
314
+ } catch (e) {}
315
+ }
316
+ }
317
+
318
+ callback();
319
+ }
320
+
321
+ function createDirectories(dirs, callback) {
322
+
323
+ var path = F.path.root('/.src/');
324
+
325
+ try {
326
+ F.Fs.mkdirSync(path);
327
+ } catch(e) {}
328
+
329
+ for (var i = 0, length = dirs.length; i < length; i++) {
330
+ var p = normalize(dirs[i]);
331
+ if (META.directories.indexOf(p) === -1)
332
+ META.directories.push(p);
333
+ try {
334
+ F.Fs.mkdirSync(F.Path.join(path, dirs[i]));
335
+ } catch (e) {}
336
+ }
337
+
338
+ callback();
339
+ }
340
+
341
+ function copyFiles(files, callback) {
342
+ var path = F.path.root('/.src/');
343
+ files.wait(function(file, next) {
344
+
345
+ if (!META.ignore(file.name) || (/\.(socket|pid)$/).test(file.name))
346
+ return next();
347
+
348
+ var filename = F.Path.join(path, file.name);
349
+ var ext = F.TUtils.getExtension(file.name);
350
+ var append = file.type === 3;
351
+ var exists = null;
352
+
353
+ try {
354
+ exists = F.Fs.statSync(filename);
355
+ } catch (e) {}
356
+
357
+ if (exists && (!exists.isFile() | exists.isSocket())) {
358
+ next();
359
+ return;
360
+ }
361
+
362
+ // DB file
363
+ if (file.type === 1 && exists) {
364
+ next();
365
+ return;
366
+ }
367
+
368
+ var p = normalize(file.name);
369
+
370
+ if (file.type !== 1 && META.files.indexOf(p) === -1)
371
+ META.files.push(p);
372
+
373
+ if (exists && (ext === 'resource' || (!ext && file.name.substring(1, 7) === 'config') || INTERNAL[file.name]))
374
+ append = true;
375
+
376
+ if (append) {
377
+ F.Fs.appendFile(filename, '\n' + F.Fs.readFileSync(file.filename).toString('utf8'), next);
378
+ } else
379
+ copyFile(file.filename, filename, next);
380
+
381
+ }, callback);
382
+ }
383
+
384
+ function copyFile(oldname, newname, callback) {
385
+ var writer = F.Fs.createWriteStream(newname);
386
+ writer.on('finish', callback);
387
+ F.Fs.createReadStream(oldname).pipe(writer);
388
+ }
389
+
390
+ exports.extract = function(callback, skip) {
391
+
392
+ if (!callback)
393
+ return new Promise(resolve => exports.extract(resolve, skip));
394
+
395
+ if (skip) {
396
+ callback();
397
+ return;
398
+ }
399
+
400
+ try {
401
+
402
+ if (F.Fs.readFileSync('bundles.debug')) {
403
+ F.isBundle = true;
404
+ F.dir(F.path.root('/.src/'));
405
+ callback();
406
+ return;
407
+ }
408
+
409
+ } catch (e) {}
410
+
411
+ var bundles = F.path.root('/bundles/');
412
+ var extractbundles = function() {
413
+
414
+ var arr = F.Fs.readdirSync(bundles);
415
+ var url = [];
416
+
417
+ for (var i = 0; i < arr.length; i++) {
418
+ if (arr[i].endsWith('.url'))
419
+ url.push(arr[i]);
420
+ }
421
+
422
+ url.wait(function(item, next) {
423
+ var filename = F.path.root('/bundles/') + item.replace('.url', '.bundle');
424
+ var url = F.Fs.readFileSync(F.path.root('/bundles/') + item).toString('utf8').trim();
425
+ F.download(url, filename, function(err) {
426
+ err && F.error(err, 'Bundle: ' + url);
427
+ next();
428
+ });
429
+ }, function() {
430
+ extract(function() {
431
+ F.isBundle = true;
432
+ F.dir(F.path.root('/.src/'));
433
+ callback();
434
+ });
435
+ });
436
+ };
437
+
438
+ try {
439
+ var files = F.Fs.readdirSync(bundles);
440
+ if (files.length)
441
+ extractbundles();
442
+ else
443
+ callback();
444
+ } catch(e) {
445
+ callback();
446
+ }
447
+ };
package/cache.js ADDED
@@ -0,0 +1,58 @@
1
+ // Total.js Cache (in-memory)
2
+ // The MIT License
3
+ // Copyright 2012-2023 (c) Peter Širka <petersirka@gmail.com>
4
+
5
+ 'use strict';
6
+
7
+ exports.set = function(key, value, expire = '5 minutes') {
8
+ global.NOW = new Date();
9
+ F.temporary.cache[key] = { value: value, expire: global.NOW.add(expire) };
10
+ };
11
+
12
+ exports.read = exports.get = function(key, def) {
13
+ var item = F.temporary.cache[key];
14
+ global.NOW = new Date();
15
+ if (item && item.expire < global.NOW)
16
+ return item.value;
17
+ else
18
+ return def;
19
+ };
20
+
21
+ exports.remove = function(key) {
22
+ if (F.temporary.cache[key])
23
+ delete F.temporary.cache[key];
24
+ };
25
+
26
+ exports.reset = function(search) {
27
+
28
+ var isreg = search instanceof RegExp;
29
+ var count = 0;
30
+
31
+ if (search) {
32
+ for (let key in F.temporary.cache) {
33
+ if (isreg) {
34
+ if (search.test(key)) {
35
+ count++;
36
+ delete F.temporary.cache[key];
37
+ }
38
+ } else if (search) {
39
+ if (key.indexOf(search) !== -1) {
40
+ count++;
41
+ delete F.temporary.cache[key];
42
+ }
43
+ }
44
+ }
45
+ } else
46
+ F.temporary.cache = {};
47
+
48
+ return count;
49
+
50
+ };
51
+
52
+ exports.refresh = function() {
53
+ for (let key in F.temporary.cache) {
54
+ let item = F.temporary.cache[key];
55
+ if (item.expire < NOW)
56
+ delete F.temporary.cache[key];
57
+ }
58
+ };
package/changelog.txt ADDED
@@ -0,0 +1,5 @@
1
+ ========================
2
+ 0.0.2
3
+ ========================
4
+
5
+ - first release