molly-db 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
package/module/_init_.js DELETED
@@ -1,69 +0,0 @@
1
-
2
- function fillDB( _db, _table, _path ){
3
- return new Promise(async(response,reject)=>{
4
-
5
- let _itr = undefined;
6
-
7
- if( (/^http/).test(_path) ){ try{
8
- const stream = await fetch(_path,{responseType:'stream'});
9
- _itr = readline.createInterface({ input: stream.data });
10
- } catch(e) { console.log(`error reading ${_path}`); return response(); }}
11
-
12
- else if( fs.existsSync(_path) )
13
- _itr = readline.createInterface({
14
- input: fs.createReadStream(_path)
15
- });
16
-
17
- else { console.log(`error reading ${_path}`); return response(); }
18
-
19
- _itr.on('line',(line)=>{
20
- db[_db][_table].push(crypto.decrypt( line,process.mollyDB.pass ));
21
- }); _itr.on('close',()=>{ response() });
22
-
23
- });
24
- }
25
-
26
- /* --------------------------------------------------------------------------------------- */
27
-
28
- (()=>{ return new Promise(async(response,reject)=>{
29
- try {
30
-
31
- const path = `${process.mollyDB.path}/_init_.json`;
32
- db._update_ = true;
33
-
34
- if( (/^http/).test(process.mollyDB.path) ){
35
- const stream = await fetch(path);
36
- db._init_ = stream.data;
37
- } else{
38
- db._buff_ = fs.readFileSync( path );
39
- db._init_ = JSON.parse( db._buff_ );
40
- }
41
-
42
- db._path_ = process.mollyDB.path;
43
- for( var i in db._init_.DB ){
44
-
45
- const DB = db._init_.DB[i];
46
- const name = DB.name;
47
- delete db[name];
48
-
49
- db[name] = new Object();
50
-
51
- for( var j in DB.tables ){
52
- const table = DB.tables[j];
53
- const path = `${db._path_}/${table}.json`;
54
- db[name][table] = new Array(); await fillDB( name,table,path );
55
- }
56
-
57
- }
58
-
59
- } catch(e) {
60
-
61
- db._init_ = { DB:[] };
62
- db._path_ = process.mollyDB.path;
63
- const path = `${process.mollyDB.path}/_init_.json`;
64
- fs.writeFileSync( path,JSON.stringify(db._init_) );
65
-
66
- } response();
67
-
68
-
69
- }); })();
@@ -1,59 +0,0 @@
1
- const worker = require('worker_threads');
2
- const readline = require('readline');
3
- const crypto = require('./_crypto_');
4
- const { Buffer } = require('buffer');
5
- const fetch = require('molly-fetch');
6
- const cluster = require('cluster');
7
- const http = require('http');
8
- const url = require('url');
9
- const fs = require('fs');
10
-
11
- /* --------------------------------------------------------------------------------------- */
12
-
13
- const db = new Object();
14
- const api_script = fs.readFileSync(`${__dirname}/_api_.js`).toString();
15
- const app_script = fs.readFileSync(`${__dirname}/_app_.js`).toString();
16
- const init_script = fs.readFileSync(`${__dirname}/_init_.js`).toString();
17
-
18
- /* --------------------------------------------------------------------------------------- */
19
-
20
- async function _init_(){ await eval(`try{ ${init_script} }catch(e){console.log(e)}`) }
21
- async function app(req,res){ await eval(`try{ ${api_script} \n ${app_script} }catch(e){console.log(e)}`) }
22
-
23
- /* --------------------------------------------------------------------------------------- */
24
- function saveTimeout(){
25
- time = process.mollyDB.time * 3600000;
26
- setTimeout(() => {
27
- if( db._update_ ){db._update_ = false;
28
- const port = process.mollyDB.port;
29
- const api = `http://127.0.0.1:${port}/saveAll`;
30
- fetch(api).then(()=>{}).catch(()=>{});
31
- }
32
- }, time);
33
- }
34
- /* --------------------------------------------------------------------------------------- */
35
-
36
- module.exports = (args)=>{ process.mollyDB = args;
37
- return new Promise((response,reject)=>{
38
-
39
- if( cluster.isPrimary )
40
- for ( let i=args.threads; i--; ){ const worker = cluster.fork();
41
- worker.on('message', (msg)=>{ console.log(msg); response(); });
42
- cluster.on('exit', (worker, code, signal) => { cluster.fork();
43
- console.log(`worker ${worker.process.pid} died`);
44
- }); worker.on('exit', (msg)=>{ reject(msg); });
45
- }
46
-
47
- else
48
- http.createServer( app ).listen( process.mollyDB.port,()=>{
49
- _init_().then(()=>{ process.send({
50
- protocol: 'HTTP', status: 'started',
51
- workerID: process.pid, port: process.mollyDB.port,
52
- }); saveTimeout();
53
- }).catch(e=>{ console.log(e) });
54
- });
55
-
56
- });
57
- }
58
-
59
- /* --------------------------------------------------------------------------------------- */