molly-db 1.0.14 → 1.0.17

Sign up to get free protection for your applications and to get access to all the features.
package/main.js CHANGED
@@ -15,31 +15,12 @@ function config( _config ) {
15
15
 
16
16
  /* --------------------------------------------------------------------------------------- */
17
17
 
18
- function startWorker(_path,_self){
19
- return new Promise((response,reject)=>{
20
- const wrk = new worker.Worker(
21
- _path,{ workerData:_self,
22
- env: worker.SHARE_ENV,
23
- }); wrk.on('message',(msg)=>{
24
- console.log(msg)
25
- response(msg)
26
- }); wrk.on('exit',(err)=>{ console.log(err);
27
- response(startWorker(_path,_self));
28
- });
29
- });
30
- }
31
-
32
- /* --------------------------------------------------------------------------------------- */
33
-
34
18
  class molly_db{
35
19
 
36
- constructor( opt ){
37
- if( opt.pass )
38
- this.pass = opt.pass;
39
- this.port = opt.port || 27017;
40
- this.path = opt.path.replace(/^\./,process.cwd());
41
- this.workerPath = `${__dirname}/module/_worker_.js`;
42
- return startWorker( this.workerPath,this );
20
+ constructor( opt ){ if( opt.pass )
21
+ this.pass = opt.pass; this.port = opt.port || 27017;
22
+ this.path = opt.path.replace( /^\./,process.cwd() );
23
+ return require(`${__dirname}/module/_worker_.js`)(this);
43
24
  }
44
25
 
45
26
  }
package/module/_init_.js CHANGED
@@ -27,7 +27,7 @@ function fillDB( _db, _table, _path ){
27
27
 
28
28
  /* --------------------------------------------------------------------------------------- */
29
29
 
30
- (async()=>{
30
+ (()=>{ return new Promise(async(response,reject)=>{
31
31
  try {
32
32
 
33
33
  const path = `${query.path}/_init_.json`;
@@ -65,4 +65,6 @@ function fillDB( _db, _table, _path ){
65
65
  fs.writeFileSync( path,JSON.stringify(db._init_) );
66
66
 
67
67
  } response();
68
- })();
68
+
69
+
70
+ }); })();
@@ -22,12 +22,12 @@ function json( _data ){
22
22
  function bodyParser( _data ){
23
23
 
24
24
  const date = Date.now(); _data = JSON.parse(_data);
25
- const result = typeof _data != 'String' ? _data : [ _data ];
25
+ const result = _data.length ? _data : [_data];
26
26
 
27
27
  return result.map(x=>{ if( !x?.hash )
28
28
  x.hash = crypto.hash( date,Math.random() );
29
29
  return JSON.stringify(x);
30
- })
30
+ });
31
31
 
32
32
  }
33
33
 
@@ -379,4 +379,4 @@ async function save( _params ){
379
379
  } catch(e) { error(e?.message||e); }
380
380
  })();
381
381
 
382
- /* --------------------------------------------------------------------------------------- */
382
+ /* --------------------------------------------------------------------------------------- */
@@ -2,20 +2,22 @@ const worker = require('worker_threads');
2
2
  const readline = require('readline');
3
3
  const crypto = require('./_crypto_');
4
4
  const { Buffer } = require('buffer');
5
+ const cluster = require('cluster');
5
6
  const fetch = require('axios');
6
7
  const http = require('http');
7
8
  const url = require('url');
8
9
  const fs = require('fs');
9
10
 
10
- const query = worker.workerData;
11
+ /* --------------------------------------------------------------------------------------- */
12
+
13
+ let query;
11
14
  const db = new Object();
15
+
12
16
  /* --------------------------------------------------------------------------------------- */
13
17
 
14
18
  function _init_(){
15
- return new Promise((response,reject)=>{
16
- try{ eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
17
- } catch(e){ console.log(e); }
18
- });
19
+ try{ return eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
20
+ } catch(e){ console.log(e); }
19
21
  }
20
22
 
21
23
  /* --------------------------------------------------------------------------------------- */
@@ -27,15 +29,22 @@ function app(req,res){
27
29
 
28
30
  /* --------------------------------------------------------------------------------------- */
29
31
 
30
- (()=>{
31
- http.createServer( app ).listen( query.port,()=>{
32
- _init_().then(()=>{
33
- worker.parentPort.postMessage({
34
- protocol: 'HTTP', status: 'started',
35
- workerID: process.pid, port: query.port,
36
- });
37
- }).catch(e=>{ console.log(e); process.exit(1); });
32
+ module.exports = (args)=>{ query = args;
33
+ return new Promise((response,reject)=>{
34
+ if (cluster.isPrimary) { const worker = cluster.fork();
35
+ cluster.on('exit', (worker, code, signal) => { cluster.fork();
36
+ console.log(`worker ${worker.process.pid} died`);
37
+ }); worker.on('message', (msg)=>{ console.log(msg); response(); });
38
+ worker.on('exit', (msg)=>{ reject(msg); });
39
+ } else {
40
+ http.createServer( app ).listen( query.port,()=>{
41
+ _init_().then(()=>{ process.send({
42
+ workerID: process.pid, port: query.port,
43
+ protocol: 'HTTP', status: 'started',
44
+ })}).catch(e=>{ console.log(e); });
45
+ });
46
+ }
38
47
  });
39
- })();
48
+ }
40
49
 
41
50
  /* --------------------------------------------------------------------------------------- */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "license": "MIT",
3
3
  "main": "main.js",
4
- "version": "1.0.14",
4
+ "version": "1.0.17",
5
5
  "name": "molly-db",
6
6
  "author": "bececrazy",
7
7
  "scripts": {