molly-db 1.3.4 → 1.3.6
Sign up to get free protection for your applications and to get access to all the features.
- package/main.js +3 -3
- package/module/api_handler.js +27 -28
- package/module/worker_handler.js +13 -17
- package/package.json +5 -4
- package/module/server_worker.js +0 -68
package/main.js
CHANGED
@@ -25,10 +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 || '127.0.0.1';
|
28
29
|
this.protocol = opt.protocol || 'http';
|
29
|
-
this.time = opt.saveTime
|
30
|
-
this.import = opt.import
|
31
|
-
this.threads = opt.thread|| 1;
|
30
|
+
this.time = opt.saveTime || .1;
|
31
|
+
this.import = opt.import || '';
|
32
32
|
|
33
33
|
return require(dir)(this);
|
34
34
|
}
|
package/module/api_handler.js
CHANGED
@@ -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
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
|
package/module/worker_handler.js
CHANGED
@@ -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 = (
|
29
|
-
|
30
|
-
|
31
|
-
const
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
@@ -1,15 +1,16 @@
|
|
1
1
|
{
|
2
|
-
"scripts": {
|
2
|
+
"scripts": {
|
3
|
+
"start": "node main"
|
4
|
+
},
|
3
5
|
"repository": "https://github.com/EDBC-REPO-NPM/Molly-db",
|
4
6
|
"dependencies": {
|
5
|
-
"molly-fetch": "^1.0.16",
|
6
7
|
"crypto-js": "^4.1.1",
|
7
|
-
"
|
8
|
+
"molly-fetch": "^1.1.2"
|
8
9
|
},
|
9
10
|
"license": "MIT",
|
10
11
|
"main": "main.js",
|
11
12
|
"name": "molly-db",
|
12
|
-
"version": "1.3.
|
13
|
+
"version": "1.3.6",
|
13
14
|
"author": "bececrazy",
|
14
15
|
"description": "Molly-db is a free and open source library for nodejs that allow you create a lightweight encrypted database using Json files",
|
15
16
|
"keywords": [
|
package/module/server_worker.js
DELETED
@@ -1,68 +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,()=>{
|
12
|
-
console.log({
|
13
|
-
protocol: 'HTTP', status: 'started',
|
14
|
-
wrkID: process.pid, port: process.env.port,
|
15
|
-
});
|
16
|
-
});
|
17
|
-
}
|
18
|
-
|
19
|
-
function createWebSocketServer(){
|
20
|
-
const srv = new ws.WebSocketServer({ port: process.env.port });
|
21
|
-
srv.on('connection',(client)=>{
|
22
|
-
client.on('message',(msg)=>app.WebSocket(msg,client));
|
23
|
-
}); console.log ({
|
24
|
-
protocol: 'WebSocket', status: 'started',
|
25
|
-
wrkID: process.pid, port: process.env.port,
|
26
|
-
});
|
27
|
-
}
|
28
|
-
|
29
|
-
function createSocketServer(){
|
30
|
-
const srv = net.createServer({ port: process.env.port });
|
31
|
-
srv.on('connection',(client)=>{
|
32
|
-
client.on('message',(msg)=>app.Socket(msg,client));
|
33
|
-
}); console.log ({
|
34
|
-
protocol: 'Socket', status: 'started',
|
35
|
-
wrkID: process.pid, port: process.env.port,
|
36
|
-
});
|
37
|
-
}
|
38
|
-
|
39
|
-
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
|
40
|
-
|
41
|
-
(()=>{
|
42
|
-
|
43
|
-
if( cluster.isPrimary ) {
|
44
|
-
const env = worker.workerData;
|
45
|
-
const data = worker.workerData;
|
46
|
-
for ( let i=data.threads; i--; ){
|
47
|
-
|
48
|
-
const wrk = cluster.fork(data,env);
|
49
|
-
|
50
|
-
cluster.on('exit', (wrk, code, signal) => { cluster.fork(data,env);
|
51
|
-
console.log(`wrk ${wrk.process.pid} died`);
|
52
|
-
}); wrk.on('exit', (msg)=>{ console.log(msg); });
|
53
|
-
|
54
|
-
wrk.on('message', (msg)=>{ worker.parentPort.postMessage(msg) });
|
55
|
-
worker.parentPort.on('message',(msg)=>{ wrk.send(msg) });
|
56
|
-
|
57
|
-
}
|
58
|
-
} else {
|
59
|
-
switch( process.env.protocol ){
|
60
|
-
case 'WebSocket': createWebSocketServer(); break;
|
61
|
-
case 'Socket': createSocketServer(); break;
|
62
|
-
default: createHTTPServer(); break;
|
63
|
-
}
|
64
|
-
}
|
65
|
-
|
66
|
-
})();
|
67
|
-
|
68
|
-
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
|