molly-db 1.3.7 → 1.3.9
Sign up to get free protection for your applications and to get access to all the features.
- package/main.js +6 -9
- package/module/init_handler.js +24 -26
- package/module/utils.js +4 -4
- package/module/worker_handler.js +4 -5
- package/package.json +1 -1
- package/module/import_handler.js +0 -3
package/main.js
CHANGED
@@ -18,17 +18,14 @@ function config( _config ) {
|
|
18
18
|
|
19
19
|
class molly_db{
|
20
20
|
constructor( opt ){
|
21
|
-
if(opt.pass) this.pass = opt.pass; this.port = opt.port || 27017;
|
22
21
|
const dir = path.join(__dirname,'/module/worker_handler.js');
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
this.
|
29
|
-
this.
|
30
|
-
this.time = opt.saveTime || .1;
|
31
|
-
this.import = opt.import || '';
|
23
|
+
this.host = opt?.host || '127.0.0.1';
|
24
|
+
this.protocol = opt?.protocol || 'http';
|
25
|
+
this.port = opt?.port || 27017;
|
26
|
+
this.path = opt?.path || null;
|
27
|
+
this.pass = opt?.pass || null;
|
28
|
+
this.time = opt?.saveTime || .1;
|
32
29
|
|
33
30
|
return require(dir)(this);
|
34
31
|
}
|
package/module/init_handler.js
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
const download = require('./import_handler');
|
2
1
|
const crypto = require('./crypto_handler');
|
3
2
|
const fetch = require('molly-fetch');
|
4
3
|
const readline = require('readline');
|
@@ -11,23 +10,19 @@ const db = new Object();
|
|
11
10
|
function fillDB( _db, _table, _path, _pass ){
|
12
11
|
return new Promise(async(response,reject)=>{
|
13
12
|
|
14
|
-
let
|
13
|
+
let stream;
|
15
14
|
|
16
|
-
if(
|
17
|
-
const stream = await fetch(_path,{responseType:'stream'});
|
18
|
-
_itr = readline.createInterface({ input: stream.data });
|
19
|
-
} catch(e) { return response(`error reading ${_path}`); }}
|
15
|
+
if( fs.existsSync(_path) ) stream = fs.createReadStream(_path);
|
20
16
|
|
21
|
-
else if(
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
}); _itr.on('close',()=>{ response() });
|
17
|
+
else if( (/^http/).test(_path) )
|
18
|
+
stream = (await fetch(_path,{responseType:'stream'})).data;
|
19
|
+
|
20
|
+
try {
|
21
|
+
const _itr = readline.createInterface({ input: stream });
|
22
|
+
_itr.on('close',()=>{ response() }); _itr.on('line',(line)=>{
|
23
|
+
db[_db][_table].push(crypto.decrypt( line,_pass ));
|
24
|
+
});
|
25
|
+
} catch(e) { return response(`error: reading ${_path}`) }
|
31
26
|
|
32
27
|
});
|
33
28
|
}
|
@@ -38,19 +33,21 @@ module.exports = function(args){
|
|
38
33
|
return new Promise(async(response,reject)=>{
|
39
34
|
try {
|
40
35
|
|
41
|
-
const dir =
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
36
|
+
const dir = `${args.path}/_init_.json`;
|
37
|
+
|
38
|
+
if( !(/^http/).test(args.path) )
|
39
|
+
db._init_ = JSON.parse(fs.readFileSync(dir));
|
40
|
+
else
|
41
|
+
db._init_ = (await fetch(dir)).data;
|
42
|
+
db._path_ = args.path;
|
46
43
|
|
47
|
-
for(
|
44
|
+
for( let i in db._init_.DB ){
|
48
45
|
|
49
46
|
const DB = db._init_.DB[i]; const name = DB.name;
|
50
47
|
delete db[name]; db[name] = new Object();
|
51
48
|
|
52
|
-
for(
|
53
|
-
const table = DB.tables[j]; const dir =
|
49
|
+
for( let j in DB.tables ){
|
50
|
+
const table = DB.tables[j]; const dir = `${db._path_}/${table}.json`;
|
54
51
|
db[name][table] = new Array(); await fillDB( name,table,dir,args.pass );
|
55
52
|
}
|
56
53
|
|
@@ -58,8 +55,9 @@ module.exports = function(args){
|
|
58
55
|
|
59
56
|
} catch(e) {
|
60
57
|
db._init_ = {DB:[]}; db._path_ = args.path;
|
61
|
-
const dir =
|
62
|
-
|
58
|
+
const dir = `${args.path}/_init_.json`; try {
|
59
|
+
fs.writeFileSync( dir,JSON.stringify(db._init_) );
|
60
|
+
} catch(e) { }
|
63
61
|
} response(db);
|
64
62
|
});
|
65
63
|
}
|
package/module/utils.js
CHANGED
@@ -61,8 +61,8 @@ const encryptDB = function( param, db, _db, _table, _path ){
|
|
61
61
|
const modifyDB = async function( data, db, _name, _table ){
|
62
62
|
try{
|
63
63
|
|
64
|
-
const
|
65
|
-
const
|
64
|
+
const init = `${db._path_}/_init_.json`;
|
65
|
+
const dir = `${db._path_}/${_table}.json`;
|
66
66
|
|
67
67
|
fs.writeFileSync( init,JSON.stringify(db._init_) );
|
68
68
|
|
@@ -241,7 +241,7 @@ output.removeDB = function(data,db){
|
|
241
241
|
for( var i in db._init_.DB ){
|
242
242
|
if( db._init_.DB[i].name == data.db ){
|
243
243
|
db._init_.DB[i].tables.map(x=>{
|
244
|
-
const dir =
|
244
|
+
const dir = `${data.path}/${x}.json`;
|
245
245
|
if( fs.existsSync(dir) ) fs.unlinkSync(dir);
|
246
246
|
});
|
247
247
|
const arr = db._init_.DB; arr.splice(i,1);
|
@@ -301,7 +301,7 @@ output.removeTable = function(data,db){
|
|
301
301
|
|
302
302
|
for( var i in db._init_.DB ){
|
303
303
|
if( db._init_.DB[i].name == data.db ){
|
304
|
-
const dir =
|
304
|
+
const dir = `${data.path}/${data.table}.json`;
|
305
305
|
const j = db._init_.DB[i].tables.indexOf(data.table);
|
306
306
|
if( fs.existsSync(dir) ) fs.unlinkSync(dir);
|
307
307
|
const arr = db._init_.DB[i].tables; arr.splice(j,1);
|
package/module/worker_handler.js
CHANGED
@@ -7,7 +7,6 @@ const app = require('./api_handler');
|
|
7
7
|
const {Buffer} = require('buffer');
|
8
8
|
const utils = require('./utils');
|
9
9
|
const http = require('http');
|
10
|
-
const path = require('path');
|
11
10
|
|
12
11
|
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
|
13
12
|
|
@@ -16,8 +15,8 @@ function Exit(code){ console.log(`worker exit code: ${code}`); }
|
|
16
15
|
|
17
16
|
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
|
18
17
|
|
19
|
-
function
|
20
|
-
|
18
|
+
function saveInterval(data,db){
|
19
|
+
setInterval(() => {
|
21
20
|
if( db._update_ ){
|
22
21
|
db._update_ = false;
|
23
22
|
utils.saveAll(data,db);
|
@@ -36,9 +35,9 @@ module.exports = (arg)=>{ init( arg ).then((db)=>{
|
|
36
35
|
name: 'molly-db', protocol: 'HTTP',
|
37
36
|
port: arg.port, host: arg.host,
|
38
37
|
}));
|
39
|
-
});
|
38
|
+
}); saveInterval( arg, db );
|
40
39
|
|
41
40
|
}).catch((e)=>{ console.log(e) });
|
42
41
|
}
|
43
42
|
|
44
|
-
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
|
43
|
+
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
|
package/package.json
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
"license": "MIT",
|
11
11
|
"main": "main.js",
|
12
12
|
"name": "molly-db",
|
13
|
-
"version": "1.3.
|
13
|
+
"version": "1.3.9",
|
14
14
|
"author": "bececrazy",
|
15
15
|
"description": "Molly-db is a free and open source library for nodejs that allow you create a lightweight encrypted database using Json files",
|
16
16
|
"keywords": [
|
package/module/import_handler.js
DELETED