molly-db 1.0.13 → 1.0.14
Sign up to get free protection for your applications and to get access to all the features.
- package/main.js +22 -19
- package/module/_init_.js +5 -5
- package/module/_server_.js +37 -58
- package/module/_worker_.js +11 -15
- package/package.json +1 -1
package/main.js
CHANGED
@@ -15,28 +15,31 @@ 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
|
+
|
18
34
|
class molly_db{
|
19
35
|
|
20
36
|
constructor( opt ){
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
if( this.worker ) return console.log(`server is running`);
|
28
|
-
|
29
|
-
this.worker = new worker.Worker(
|
30
|
-
`${__dirname}/module/_worker_.js`,{
|
31
|
-
env: worker.SHARE_ENV,
|
32
|
-
workerData: this
|
33
|
-
}
|
34
|
-
);
|
35
|
-
|
36
|
-
this.worker.on('exit',(err)=>{ console.log(err); reject(err) });
|
37
|
-
this.worker.on('error',(err)=>{ console.log(err); reject(err) });
|
38
|
-
this.worker.on('message',(msg)=>{ console.log(msg); response(msg) });
|
39
|
-
});
|
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 );
|
40
43
|
}
|
41
44
|
|
42
45
|
}
|
package/module/_init_.js
CHANGED
@@ -4,19 +4,19 @@ function fillDB( _db, _table, _path ){
|
|
4
4
|
|
5
5
|
let _itr = undefined;
|
6
6
|
|
7
|
-
if( (/^http/).test(_path) ){
|
7
|
+
if( (/^http/).test(_path) ){ try{
|
8
8
|
const stream = await fetch.get(_path,{responseType:'stream'});
|
9
9
|
_itr = readline.createInterface({
|
10
10
|
input: stream.data
|
11
|
-
})
|
12
|
-
}
|
13
|
-
|
11
|
+
})
|
12
|
+
} catch(e) { console.log(`error reading ${_path}`); return response(); }}
|
13
|
+
|
14
14
|
else if( fs.existsSync(_path) )
|
15
15
|
_itr = readline.createInterface({
|
16
16
|
input: fs.createReadStream(_path)
|
17
17
|
});
|
18
18
|
|
19
|
-
else return response();
|
19
|
+
else { console.log(`error reading ${_path}`); return response(); }
|
20
20
|
|
21
21
|
_itr.on('line',(line)=>{
|
22
22
|
db[_db][_table].push(crypto.decrypt( line,query.pass ));
|
package/module/_server_.js
CHANGED
@@ -21,20 +21,13 @@ function json( _data ){
|
|
21
21
|
|
22
22
|
function bodyParser( _data ){
|
23
23
|
|
24
|
-
const date = Date.now();
|
25
|
-
const result =
|
24
|
+
const date = Date.now(); _data = JSON.parse(_data);
|
25
|
+
const result = typeof _data != 'String' ? _data : [ _data ];
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
result.push(JSON.stringify(_data[i]));
|
32
|
-
}
|
33
|
-
} else {
|
34
|
-
if(!_data?.hash)
|
35
|
-
_data.hash = crypto.hash( date,0 );
|
36
|
-
result.push(JSON.stringify(_data));
|
37
|
-
} return result;
|
27
|
+
return result.map(x=>{ if( !x?.hash )
|
28
|
+
x.hash = crypto.hash( date,Math.random() );
|
29
|
+
return JSON.stringify(x);
|
30
|
+
})
|
38
31
|
|
39
32
|
}
|
40
33
|
|
@@ -45,9 +38,8 @@ function getBody(){
|
|
45
38
|
const data = new Array();
|
46
39
|
req.on('data',(chunk)=>{ data.push(chunk); });
|
47
40
|
req.on('close',()=>{ try{
|
48
|
-
const buff = Buffer.concat(data);
|
49
|
-
|
50
|
-
response( bodyParser(json) );
|
41
|
+
const buff = Buffer.concat(data);
|
42
|
+
response( bodyParser(buff) );
|
51
43
|
} catch(e) { response(false) } });
|
52
44
|
} else { response(true) }
|
53
45
|
} catch(e) { response(false) }
|
@@ -141,12 +133,12 @@ function hash( _params ){
|
|
141
133
|
|
142
134
|
/* --------------------------------------------------------------------------------------- */
|
143
135
|
|
144
|
-
function shift( _params ){
|
145
|
-
const result = db[_params.db][_params.table].shift(); save( _params ); return result;
|
136
|
+
async function shift( _params ){
|
137
|
+
const result = db[_params.db][_params.table].shift(); await save( _params ); return result;
|
146
138
|
}
|
147
139
|
|
148
|
-
function pop( _params ){
|
149
|
-
const result = db[_params.db][_params.table].pop(); save( _params ); return result;
|
140
|
+
async function pop( _params ){
|
141
|
+
const result = db[_params.db][_params.table].pop(); await save( _params ); return result;
|
150
142
|
}
|
151
143
|
|
152
144
|
/* --------------------------------------------------------------------------------------- */
|
@@ -161,40 +153,26 @@ function indexOf( _params ){
|
|
161
153
|
|
162
154
|
/* --------------------------------------------------------------------------------------- */
|
163
155
|
|
164
|
-
async function
|
156
|
+
async function unshift( _params ){
|
165
157
|
|
166
|
-
db[_params.db][_params.table].
|
158
|
+
db[_params.db][_params.table].unshift( ...body );
|
167
159
|
|
168
|
-
save( _params ); return [{
|
160
|
+
await save( _params ); return [{
|
169
161
|
database: _params.db,
|
170
162
|
table: _params.table,
|
171
|
-
status: '
|
163
|
+
status: 'unshifted'
|
172
164
|
}];
|
173
|
-
|
174
165
|
}
|
175
166
|
|
176
|
-
async function
|
177
|
-
|
178
|
-
db[_params.db][_params.table].splice(
|
179
|
-
_params.offset,_params.length,...body
|
180
|
-
);
|
167
|
+
async function push( _params ){
|
181
168
|
|
182
|
-
|
169
|
+
db[_params.db][_params.table].push( ...body );
|
170
|
+
await save( _params ); return [{
|
183
171
|
database: _params.db,
|
184
172
|
table: _params.table,
|
185
|
-
status: '
|
173
|
+
status: 'pushed'
|
186
174
|
}];
|
187
|
-
}
|
188
|
-
|
189
|
-
async function unshift( _params ){
|
190
|
-
|
191
|
-
db[_params.db][_params.table].unshift( ...body );
|
192
175
|
|
193
|
-
save( _params ); return [{
|
194
|
-
database: _params.db,
|
195
|
-
table: _params.table,
|
196
|
-
status: 'unshifted'
|
197
|
-
}];
|
198
176
|
}
|
199
177
|
|
200
178
|
/* --------------------------------------------------------------------------------------- */
|
@@ -209,7 +187,7 @@ async function update( _params ){
|
|
209
187
|
if( !(index<0) )
|
210
188
|
db[_params.db][_params.table].splice( index,1,...body );
|
211
189
|
|
212
|
-
save( _params ); return [{
|
190
|
+
await save( _params ); return [{
|
213
191
|
database: _params.db,
|
214
192
|
table: _params.table,
|
215
193
|
status: 'udated'
|
@@ -226,7 +204,7 @@ async function remove( _params ){
|
|
226
204
|
if( !(index<0) )
|
227
205
|
db[_params.db][_params.table].splice( index,1 );
|
228
206
|
|
229
|
-
save( _params ); return [{
|
207
|
+
await save( _params ); return [{
|
230
208
|
database: _params.db,
|
231
209
|
table: _params.table,
|
232
210
|
status: 'removed'
|
@@ -235,7 +213,7 @@ async function remove( _params ){
|
|
235
213
|
|
236
214
|
/* --------------------------------------------------------------------------------------- */
|
237
215
|
|
238
|
-
function addDB( _params ){
|
216
|
+
async function addDB( _params ){
|
239
217
|
try{
|
240
218
|
|
241
219
|
db._init_.DB.push({
|
@@ -243,7 +221,7 @@ function addDB( _params ){
|
|
243
221
|
name: _params.db,
|
244
222
|
}); db[_params.db] = new Array();
|
245
223
|
|
246
|
-
save( _params ); return [{
|
224
|
+
await save( _params ); return [{
|
247
225
|
database: _params.db,
|
248
226
|
status: 'DB added'
|
249
227
|
}];
|
@@ -251,7 +229,7 @@ function addDB( _params ){
|
|
251
229
|
} catch(e) { }
|
252
230
|
}
|
253
231
|
|
254
|
-
function removeDB( _params ){
|
232
|
+
async function removeDB( _params ){
|
255
233
|
try{
|
256
234
|
|
257
235
|
const i = db._init_.DB.findIndex(x=>{
|
@@ -264,7 +242,7 @@ function removeDB( _params ){
|
|
264
242
|
}); db._init_.DB.splice(i,1);
|
265
243
|
db[_params.db] = new Array();
|
266
244
|
|
267
|
-
save( _params ); return [{
|
245
|
+
await save( _params ); return [{
|
268
246
|
database: _params.db,
|
269
247
|
table: _params.table,
|
270
248
|
status: 'DB deleted'
|
@@ -273,18 +251,19 @@ function removeDB( _params ){
|
|
273
251
|
} catch(e) { }
|
274
252
|
}
|
275
253
|
|
276
|
-
function modifyDB( _name, _table ){
|
254
|
+
async function modifyDB( _name, _table ){
|
277
255
|
try{
|
278
256
|
|
279
257
|
const init = `${query.path}/_init_.json`;
|
280
258
|
const path = `${query.path}/${_table}.json`;
|
281
|
-
|
282
259
|
fs.writeFileSync( init,JSON.stringify(db._init_) );
|
283
260
|
|
284
261
|
try {
|
285
262
|
const length = db[_name][_table].length;
|
286
|
-
if( length>0 )
|
287
|
-
|
263
|
+
if( length>0 )
|
264
|
+
await encryptDB( _name, _table, path );
|
265
|
+
else
|
266
|
+
fs.writeFileSync( path,'' );
|
288
267
|
} catch(e) {
|
289
268
|
fs.unlinkSync( path );
|
290
269
|
}
|
@@ -294,7 +273,7 @@ function modifyDB( _name, _table ){
|
|
294
273
|
|
295
274
|
/* --------------------------------------------------------------------------------------- */
|
296
275
|
|
297
|
-
function addTable( _params ){
|
276
|
+
async function addTable( _params ){
|
298
277
|
|
299
278
|
if( db[_params.db][_params.table] )
|
300
279
|
return {
|
@@ -309,7 +288,7 @@ function addTable( _params ){
|
|
309
288
|
|
310
289
|
db[_params.db][_params.table] = new Array();
|
311
290
|
|
312
|
-
save( _params ); return [{
|
291
|
+
await save( _params ); return [{
|
313
292
|
database: _params.db,
|
314
293
|
table: _params.table,
|
315
294
|
status: 'table added'
|
@@ -317,7 +296,7 @@ function addTable( _params ){
|
|
317
296
|
|
318
297
|
}
|
319
298
|
|
320
|
-
function removeTable( _params ){
|
299
|
+
async function removeTable( _params ){
|
321
300
|
|
322
301
|
const i = db._init_.DB.findIndex(x=>{
|
323
302
|
return x.name == _params.db;
|
@@ -330,7 +309,7 @@ function removeTable( _params ){
|
|
330
309
|
db._init_.DB[i].tables.splice(j,1);
|
331
310
|
delete db[_params.db][_params.table];
|
332
311
|
|
333
|
-
save( _params ); return [{
|
312
|
+
await save( _params ); return [{
|
334
313
|
database: _params.db,
|
335
314
|
table: _params.table,
|
336
315
|
status: 'table removed'
|
@@ -347,8 +326,8 @@ function refresh( _params ){
|
|
347
326
|
});
|
348
327
|
}
|
349
328
|
|
350
|
-
function save( _params ){
|
351
|
-
modifyDB( _params.db,_params.table );
|
329
|
+
async function save( _params ){
|
330
|
+
await modifyDB( _params.db,_params.table );
|
352
331
|
return [{
|
353
332
|
database: _params.db,
|
354
333
|
table: _params.table,
|
package/module/_worker_.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
const worker = require('worker_threads');
|
2
2
|
const readline = require('readline');
|
3
3
|
const crypto = require('./_crypto_');
|
4
|
-
const {Buffer} = require('buffer');
|
4
|
+
const { Buffer } = require('buffer');
|
5
5
|
const fetch = require('axios');
|
6
6
|
const http = require('http');
|
7
7
|
const url = require('url');
|
@@ -13,8 +13,7 @@ const db = new Object();
|
|
13
13
|
|
14
14
|
function _init_(){
|
15
15
|
return new Promise((response,reject)=>{
|
16
|
-
try{
|
17
|
-
eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
|
16
|
+
try{ eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
|
18
17
|
} catch(e){ console.log(e); }
|
19
18
|
});
|
20
19
|
}
|
@@ -22,24 +21,21 @@ function _init_(){
|
|
22
21
|
/* --------------------------------------------------------------------------------------- */
|
23
22
|
|
24
23
|
function app(req,res){
|
25
|
-
try{
|
26
|
-
eval( fs.readFileSync(`${__dirname}/_server_.js`).toString() );
|
24
|
+
try{ eval( fs.readFileSync(`${__dirname}/_server_.js`).toString() );
|
27
25
|
} catch(e) { console.log(e) }
|
28
26
|
}
|
29
27
|
|
30
28
|
/* --------------------------------------------------------------------------------------- */
|
31
29
|
|
32
30
|
(()=>{
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
});
|
42
|
-
} catch(e) {}
|
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); });
|
38
|
+
});
|
43
39
|
})();
|
44
40
|
|
45
41
|
/* --------------------------------------------------------------------------------------- */
|