molly-db 1.3.5 → 1.3.6

Sign up to get free protection for your applications and to get access to all the features.
package/main.js CHANGED
@@ -25,11 +25,10 @@ class molly_db{
25
25
  this.path = path.join(process.cwd(),opt.path);
26
26
  else this.path = opt.path;
27
27
 
28
- this.host = opt.host || 'localhost';
28
+ this.host = opt.host || '127.0.0.1';
29
29
  this.protocol = opt.protocol || 'http';
30
30
  this.time = opt.saveTime || .1;
31
31
  this.import = opt.import || '';
32
- this.threads = opt.thread || 1;
33
32
 
34
33
  return require(dir)(this);
35
34
  }
@@ -1,45 +1,44 @@
1
+
2
+ const memory = require('./memory_handler');
1
3
  const cluster = require('cluster');
2
4
  const {Buffer} = require('buffer');
3
5
  const output = new Object();
4
6
 
5
7
  /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
6
8
 
7
- output.http = function(req,res){
9
+ function send( db,arg,msg ){
10
+ const empty = '{ "status":"404", "message":"empty data" }';
11
+ const error = '{ "status":"404", "message":"error data" }';
12
+ return new Promise((response,reject)=>{
13
+ try {memory(arg,msg,db)
14
+ .then(x=> response(JSON.stringify(x)||empty) )
15
+ .catch(e=>response(JSON.stringify(e)||empty) )
16
+ } catch(e) { response(error) }
17
+ })
18
+ }
19
+
20
+ /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
21
+
22
+ module.exports = function(db,req,res,arg){
8
23
 
9
24
  if( req.method != 'POST' ){
10
25
  res.writeHead(200,{'content-type': 'text/plain'});
11
26
  return res.end('Only Post Method Avalilable');
12
27
  } const raw = new Array();
13
28
 
14
- req.on('close',()=>{ process.send(Buffer.concat(raw)) });
15
29
  req.on('data',(chunk)=>{ raw.push(chunk) });
16
-
17
- cluster.worker.once('message',(msg)=>{
18
- const status = msg.match(/\d+/i)[0];
19
- res.writeHead(status,{'content-type': 'text/plain'});
20
- res.write(msg); res.end();
21
- })
30
+ req.on('close',()=>{
31
+ const data = JSON.parse(Buffer.concat(raw));
32
+ send(db,arg,data).then(msg=>{
33
+ const status = msg.match(/\d+/i)[0];
34
+ res.writeHead(status,{'content-type': 'text/plain'});
35
+ res.write(msg); res.end();
36
+ }).catch(e=>{
37
+ res.writeHead(404,{'content-type': 'text/plain'});
38
+ res.write(e); res.end();
39
+ })
40
+ });
22
41
 
23
42
  }
24
43
 
25
- /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
26
-
27
- output.WebSocket = function( message,client ){
28
- cluster.worker.once('message',(msg)=>{
29
- client.send(msg);
30
- }); process.send(message);
31
- }
32
-
33
- /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
34
-
35
- output.Socket = function( message,client ){
36
- cluster.worker.once('message',(msg)=>{
37
- client.write(msg);
38
- }); process.send(message);
39
- }
40
-
41
- /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
42
-
43
- module.exports = output;
44
-
45
44
  /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
@@ -3,8 +3,10 @@
3
3
  const memory = require('./memory_handler');
4
4
  const worker = require('worker_threads');
5
5
  const init = require('./init_handler');
6
+ const app = require('./api_handler');
6
7
  const {Buffer} = require('buffer');
7
8
  const utils = require('./utils');
9
+ const http = require('http');
8
10
  const path = require('path');
9
11
 
10
12
  /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
@@ -25,23 +27,17 @@ function saveTimeout(data,db){
25
27
 
26
28
  /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
27
29
 
28
- module.exports = (args)=>{
29
- process.env.MOLLY_DB_ARGS = JSON.stringify(args);
30
- init( args ).then((db)=>{ saveTimeout( args, db );
31
- const dir = path.join(__dirname,'server_worker.js');
32
- const srv = new worker.Worker(dir,{ workerData: args });
33
- srv.on('error',Error); srv.on('exit',Exit); srv.on('message',(msg)=>{
34
- const empty = '{ "status":"404", "message":"empty data" }';
35
- const error = '{ "status":"404", "message":"error data" }';
36
- try {
37
- const raw = Buffer.from(msg).toString();
38
- memory(args,JSON.parse(raw),db).then(x=>{
39
- const out = JSON.stringify(x); srv.postMessage(out||empty);
40
- }).catch(e=>{
41
- const out = JSON.stringify(e); srv.postMessage(out||empty);
42
- })
43
- } catch(e) { console.log(e); srv.postMessage(error) }
44
- });
30
+ module.exports = (arg)=>{ init( arg ).then((db)=>{
31
+
32
+ process.env.MOLLY_DB_arg = JSON.stringify(arg);
33
+ const server = http.createServer((req,res)=>app(db,req,res,arg));
34
+ server.listen( arg.port, arg.host, ()=>{
35
+ console.log(JSON.stringify({
36
+ name: 'molly-file', protocol: 'HTTP',
37
+ port: arg.port, host: arg.host,
38
+ }));
39
+ }); saveTimeout( arg, db );
40
+
45
41
  }).catch((e)=>{ console.log(e) });
46
42
  }
47
43
 
package/package.json CHANGED
@@ -5,13 +5,12 @@
5
5
  "repository": "https://github.com/EDBC-REPO-NPM/Molly-db",
6
6
  "dependencies": {
7
7
  "crypto-js": "^4.1.1",
8
- "molly-fetch": "^1.1.2",
9
- "ws": "^8.11.0"
8
+ "molly-fetch": "^1.1.2"
10
9
  },
11
10
  "license": "MIT",
12
11
  "main": "main.js",
13
12
  "name": "molly-db",
14
- "version": "1.3.5",
13
+ "version": "1.3.6",
15
14
  "author": "bececrazy",
16
15
  "description": "Molly-db is a free and open source library for nodejs that allow you create a lightweight encrypted database using Json files",
17
16
  "keywords": [
@@ -1,71 +0,0 @@
1
- const worker = require('worker_threads');
2
- const app = require('./api_handler');
3
- const cluster = require('cluster');
4
- const http = require('http');
5
- const net = require('net');
6
- const ws = require('ws');
7
-
8
- /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
9
-
10
- function createHTTPServer(){
11
- http.createServer( app.http ).listen( process.env.port, process.env.host, ()=>{
12
- console.log({
13
- protocol: 'HTTP', status: 'started',
14
- wrkID: process.pid, port: process.env.port,
15
- host: process.env.host
16
- });
17
- });
18
- }
19
-
20
- function createWebSocketServer(){
21
- const srv = new ws.WebSocketServer({ port: process.env.port, host: process.env.host });
22
- srv.on('connection',(client)=>{
23
- client.on('message',(msg)=>app.WebSocket(msg,client));
24
- }); console.log ({
25
- protocol: 'WebSocket', status: 'started',
26
- wrkID: process.pid, port: process.env.port,
27
- host: process.env.host
28
- });
29
- }
30
-
31
- function createSocketServer(){
32
- const srv = net.createServer({ port: process.env.port, host: process.env.host });
33
- srv.on('connection',(client)=>{
34
- client.on('message',(msg)=>app.Socket(msg,client));
35
- }); console.log ({
36
- protocol: 'Socket', status: 'started',
37
- wrkID: process.pid, port: process.env.port,
38
- host: process.env.host
39
- });
40
- }
41
-
42
- /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
43
-
44
- (()=>{
45
-
46
- if( cluster.isPrimary ) {
47
- const env = worker.workerData;
48
- const data = worker.workerData;
49
- for ( let i=data.threads; i--; ){
50
-
51
- const wrk = cluster.fork(data,env);
52
-
53
- cluster.on('exit', (wrk, code, signal) => { cluster.fork(data,env);
54
- console.log(`wrk ${wrk.process.pid} died`);
55
- }); wrk.on('exit', (msg)=>{ console.log(msg); });
56
-
57
- wrk.on('message', (msg)=>{ worker.parentPort.postMessage(msg) });
58
- worker.parentPort.on('message',(msg)=>{ wrk.send(msg) });
59
-
60
- }
61
- } else {
62
- switch( process.env.protocol ){
63
- case 'WebSocket': createWebSocketServer(); break;
64
- case 'Socket': createSocketServer(); break;
65
- default: createHTTPServer(); break;
66
- }
67
- }
68
-
69
- })();
70
-
71
- /*--────────────────────────────────────────────────────────────────────────────────────────────--*/