molly-db 1.0.12 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.js +21 -19
- package/module/_init_.js +13 -10
- package/module/_server_.js +51 -57
- package/module/_worker_.js +10 -20
- package/package.json +1 -1
package/main.js
CHANGED
@@ -15,28 +15,30 @@ 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('exit',(err)=>{ console.log(err);
|
24
|
+
response(startWorker(_path,_self));
|
25
|
+
}); wrk.on('message',(msg)=>{
|
26
|
+
console.log(msg); response(msg)
|
27
|
+
});
|
28
|
+
});
|
29
|
+
}
|
30
|
+
|
31
|
+
/* --------------------------------------------------------------------------------------- */
|
32
|
+
|
18
33
|
class molly_db{
|
19
34
|
|
20
35
|
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
|
-
});
|
36
|
+
if( opt.pass )
|
37
|
+
this.pass = opt.pass;
|
38
|
+
this.port = opt.port || 27017;
|
39
|
+
this.path = opt.path.replace(/^\./,process.cwd());
|
40
|
+
this.workerPath = `${__dirname}/module/_worker_.js`;
|
41
|
+
return startWorker( this.workerPath,this );
|
40
42
|
}
|
41
43
|
|
42
44
|
}
|
package/module/_init_.js
CHANGED
@@ -4,28 +4,30 @@ 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
|
+
else if( fs.existsSync(_path) )
|
14
15
|
_itr = readline.createInterface({
|
15
16
|
input: fs.createReadStream(_path)
|
16
17
|
});
|
18
|
+
|
19
|
+
else { console.log(`error reading ${_path}`); return response(); }
|
17
20
|
|
18
|
-
_itr.on('close',()=>{ response() });
|
19
21
|
_itr.on('line',(line)=>{
|
20
22
|
db[_db][_table].push(crypto.decrypt( line,query.pass ));
|
21
|
-
});
|
23
|
+
}); _itr.on('close',()=>{ response() });
|
22
24
|
|
23
25
|
});
|
24
26
|
}
|
25
27
|
|
26
28
|
/* --------------------------------------------------------------------------------------- */
|
27
29
|
|
28
|
-
(async()=>{
|
30
|
+
(()=>{ return new Promise(async(response,reject)=>{
|
29
31
|
try {
|
30
32
|
|
31
33
|
const path = `${query.path}/_init_.json`;
|
@@ -50,8 +52,7 @@ function fillDB( _db, _table, _path ){
|
|
50
52
|
for( var j in DB.tables ){
|
51
53
|
const table = DB.tables[j];
|
52
54
|
const path = `${db._path_}/${table}.json`;
|
53
|
-
db[name][table] = new Array();
|
54
|
-
await fillDB( name,table,path );
|
55
|
+
db[name][table] = new Array(); await fillDB( name,table,path );
|
55
56
|
}
|
56
57
|
|
57
58
|
}
|
@@ -64,4 +65,6 @@ function fillDB( _db, _table, _path ){
|
|
64
65
|
fs.writeFileSync( path,JSON.stringify(db._init_) );
|
65
66
|
|
66
67
|
} response();
|
67
|
-
|
68
|
+
|
69
|
+
|
70
|
+
}); })();
|
package/module/_server_.js
CHANGED
@@ -21,20 +21,15 @@ 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 = _data.length ? _data : [_data];
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
} else {
|
34
|
-
if(!_data?.hash)
|
35
|
-
_data.hash = crypto.hash( date,0 );
|
36
|
-
result.push(JSON.stringify(_data));
|
37
|
-
} return result;
|
27
|
+
console.log( _data, typeof _data, _data.length, result );
|
28
|
+
|
29
|
+
return result.map(x=>{ if( !x?.hash )
|
30
|
+
x.hash = crypto.hash( date,Math.random() );
|
31
|
+
return JSON.stringify(x);
|
32
|
+
});
|
38
33
|
|
39
34
|
}
|
40
35
|
|
@@ -45,9 +40,8 @@ function getBody(){
|
|
45
40
|
const data = new Array();
|
46
41
|
req.on('data',(chunk)=>{ data.push(chunk); });
|
47
42
|
req.on('close',()=>{ try{
|
48
|
-
const buff = Buffer.concat(data);
|
49
|
-
|
50
|
-
response( bodyParser(json) );
|
43
|
+
const buff = Buffer.concat(data);
|
44
|
+
response( bodyParser(buff) );
|
51
45
|
} catch(e) { response(false) } });
|
52
46
|
} else { response(true) }
|
53
47
|
} catch(e) { response(false) }
|
@@ -141,50 +135,46 @@ function hash( _params ){
|
|
141
135
|
|
142
136
|
/* --------------------------------------------------------------------------------------- */
|
143
137
|
|
144
|
-
function shift( _params ){
|
145
|
-
const result = db[_params.db][_params.table].shift(); save( _params ); return result;
|
138
|
+
async function shift( _params ){
|
139
|
+
const result = db[_params.db][_params.table].shift(); await save( _params ); return result;
|
146
140
|
}
|
147
141
|
|
148
|
-
function pop( _params ){
|
149
|
-
const result = db[_params.db][_params.table].pop(); save( _params ); return result;
|
142
|
+
async function pop( _params ){
|
143
|
+
const result = db[_params.db][_params.table].pop(); await save( _params ); return result;
|
150
144
|
}
|
151
145
|
|
152
146
|
/* --------------------------------------------------------------------------------------- */
|
153
147
|
|
154
|
-
|
155
|
-
|
156
|
-
db[_params.db][_params.table].
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
table: _params.table,
|
161
|
-
status: 'pushed'
|
162
|
-
}];
|
163
|
-
|
148
|
+
function lengthOf( _params ){ return db[_params.db][_params.table].length; }
|
149
|
+
function indexOf( _params ){
|
150
|
+
return db[_params.db][_params.table].indexOf(x=>{
|
151
|
+
const regex = new RegExp(_params.target,'gi');
|
152
|
+
return regex.test(x);
|
153
|
+
});
|
164
154
|
}
|
165
155
|
|
166
|
-
|
156
|
+
/* --------------------------------------------------------------------------------------- */
|
167
157
|
|
168
|
-
|
169
|
-
|
170
|
-
);
|
158
|
+
async function unshift( _params ){
|
159
|
+
|
160
|
+
db[_params.db][_params.table].unshift( ...body );
|
171
161
|
|
172
|
-
save( _params ); return [{
|
162
|
+
await save( _params ); return [{
|
173
163
|
database: _params.db,
|
174
164
|
table: _params.table,
|
175
|
-
status: '
|
165
|
+
status: 'unshifted'
|
176
166
|
}];
|
177
167
|
}
|
178
168
|
|
179
|
-
async function
|
180
|
-
|
181
|
-
db[_params.db][_params.table].unshift( ...body );
|
169
|
+
async function push( _params ){
|
182
170
|
|
183
|
-
|
171
|
+
db[_params.db][_params.table].push( ...body );
|
172
|
+
await save( _params ); return [{
|
184
173
|
database: _params.db,
|
185
174
|
table: _params.table,
|
186
|
-
status: '
|
175
|
+
status: 'pushed'
|
187
176
|
}];
|
177
|
+
|
188
178
|
}
|
189
179
|
|
190
180
|
/* --------------------------------------------------------------------------------------- */
|
@@ -199,7 +189,7 @@ async function update( _params ){
|
|
199
189
|
if( !(index<0) )
|
200
190
|
db[_params.db][_params.table].splice( index,1,...body );
|
201
191
|
|
202
|
-
save( _params ); return [{
|
192
|
+
await save( _params ); return [{
|
203
193
|
database: _params.db,
|
204
194
|
table: _params.table,
|
205
195
|
status: 'udated'
|
@@ -216,7 +206,7 @@ async function remove( _params ){
|
|
216
206
|
if( !(index<0) )
|
217
207
|
db[_params.db][_params.table].splice( index,1 );
|
218
208
|
|
219
|
-
save( _params ); return [{
|
209
|
+
await save( _params ); return [{
|
220
210
|
database: _params.db,
|
221
211
|
table: _params.table,
|
222
212
|
status: 'removed'
|
@@ -225,7 +215,7 @@ async function remove( _params ){
|
|
225
215
|
|
226
216
|
/* --------------------------------------------------------------------------------------- */
|
227
217
|
|
228
|
-
function addDB( _params ){
|
218
|
+
async function addDB( _params ){
|
229
219
|
try{
|
230
220
|
|
231
221
|
db._init_.DB.push({
|
@@ -233,7 +223,7 @@ function addDB( _params ){
|
|
233
223
|
name: _params.db,
|
234
224
|
}); db[_params.db] = new Array();
|
235
225
|
|
236
|
-
save( _params ); return [{
|
226
|
+
await save( _params ); return [{
|
237
227
|
database: _params.db,
|
238
228
|
status: 'DB added'
|
239
229
|
}];
|
@@ -241,7 +231,7 @@ function addDB( _params ){
|
|
241
231
|
} catch(e) { }
|
242
232
|
}
|
243
233
|
|
244
|
-
function removeDB( _params ){
|
234
|
+
async function removeDB( _params ){
|
245
235
|
try{
|
246
236
|
|
247
237
|
const i = db._init_.DB.findIndex(x=>{
|
@@ -254,7 +244,7 @@ function removeDB( _params ){
|
|
254
244
|
}); db._init_.DB.splice(i,1);
|
255
245
|
db[_params.db] = new Array();
|
256
246
|
|
257
|
-
save( _params ); return [{
|
247
|
+
await save( _params ); return [{
|
258
248
|
database: _params.db,
|
259
249
|
table: _params.table,
|
260
250
|
status: 'DB deleted'
|
@@ -263,18 +253,19 @@ function removeDB( _params ){
|
|
263
253
|
} catch(e) { }
|
264
254
|
}
|
265
255
|
|
266
|
-
function modifyDB( _name, _table ){
|
256
|
+
async function modifyDB( _name, _table ){
|
267
257
|
try{
|
268
258
|
|
269
259
|
const init = `${query.path}/_init_.json`;
|
270
260
|
const path = `${query.path}/${_table}.json`;
|
271
|
-
|
272
261
|
fs.writeFileSync( init,JSON.stringify(db._init_) );
|
273
262
|
|
274
263
|
try {
|
275
264
|
const length = db[_name][_table].length;
|
276
|
-
if( length>0 )
|
277
|
-
|
265
|
+
if( length>0 )
|
266
|
+
await encryptDB( _name, _table, path );
|
267
|
+
else
|
268
|
+
fs.writeFileSync( path,'' );
|
278
269
|
} catch(e) {
|
279
270
|
fs.unlinkSync( path );
|
280
271
|
}
|
@@ -284,7 +275,7 @@ function modifyDB( _name, _table ){
|
|
284
275
|
|
285
276
|
/* --------------------------------------------------------------------------------------- */
|
286
277
|
|
287
|
-
function addTable( _params ){
|
278
|
+
async function addTable( _params ){
|
288
279
|
|
289
280
|
if( db[_params.db][_params.table] )
|
290
281
|
return {
|
@@ -299,7 +290,7 @@ function addTable( _params ){
|
|
299
290
|
|
300
291
|
db[_params.db][_params.table] = new Array();
|
301
292
|
|
302
|
-
save( _params ); return [{
|
293
|
+
await save( _params ); return [{
|
303
294
|
database: _params.db,
|
304
295
|
table: _params.table,
|
305
296
|
status: 'table added'
|
@@ -307,7 +298,7 @@ function addTable( _params ){
|
|
307
298
|
|
308
299
|
}
|
309
300
|
|
310
|
-
function removeTable( _params ){
|
301
|
+
async function removeTable( _params ){
|
311
302
|
|
312
303
|
const i = db._init_.DB.findIndex(x=>{
|
313
304
|
return x.name == _params.db;
|
@@ -320,7 +311,7 @@ function removeTable( _params ){
|
|
320
311
|
db._init_.DB[i].tables.splice(j,1);
|
321
312
|
delete db[_params.db][_params.table];
|
322
313
|
|
323
|
-
save( _params ); return [{
|
314
|
+
await save( _params ); return [{
|
324
315
|
database: _params.db,
|
325
316
|
table: _params.table,
|
326
317
|
status: 'table removed'
|
@@ -337,8 +328,8 @@ function refresh( _params ){
|
|
337
328
|
});
|
338
329
|
}
|
339
330
|
|
340
|
-
function save( _params ){
|
341
|
-
modifyDB( _params.db,_params.table );
|
331
|
+
async function save( _params ){
|
332
|
+
await modifyDB( _params.db,_params.table );
|
342
333
|
return [{
|
343
334
|
database: _params.db,
|
344
335
|
table: _params.table,
|
@@ -360,6 +351,9 @@ function save( _params ){
|
|
360
351
|
else if( api.pathname == '/match' ) json( await match(params) )
|
361
352
|
else if( api.pathname == '/update' ) json( await update(params) )
|
362
353
|
|
354
|
+
else if( api.pathname == '/index' ) json( await indexOf(params) )
|
355
|
+
else if( api.pathname == '/lengt' ) json( await lengthOf(params) )
|
356
|
+
|
363
357
|
/* Save Api */
|
364
358
|
else if( api.pathname == '/save' ) json( await save(params) )
|
365
359
|
else if( api.pathname == '/remove' ) json( await remove(params) )
|
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');
|
@@ -12,34 +12,24 @@ const db = new Object();
|
|
12
12
|
/* --------------------------------------------------------------------------------------- */
|
13
13
|
|
14
14
|
function _init_(){
|
15
|
-
return
|
16
|
-
|
17
|
-
eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
|
18
|
-
} catch(e){ console.log(e); }
|
19
|
-
});
|
15
|
+
try{ return eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
|
16
|
+
} catch(e){ console.log(e); }
|
20
17
|
}
|
21
18
|
|
22
19
|
/* --------------------------------------------------------------------------------------- */
|
23
20
|
|
24
21
|
function app(req,res){
|
25
|
-
try{
|
26
|
-
eval( fs.readFileSync(`${__dirname}/_server_.js`).toString() );
|
22
|
+
try{ eval( fs.readFileSync(`${__dirname}/_server_.js`).toString() );
|
27
23
|
} catch(e) { console.log(e) }
|
28
24
|
}
|
29
25
|
|
30
26
|
/* --------------------------------------------------------------------------------------- */
|
31
27
|
|
32
|
-
(()=>{
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
workerID: process.pid, port: query.port,
|
39
|
-
});
|
40
|
-
}).catch(e=>{ process.exit(1); });
|
41
|
-
});
|
42
|
-
} catch(e) {}
|
43
|
-
})();
|
28
|
+
(()=>{ http.createServer( app ).listen( query.port,()=>{
|
29
|
+
_init_().then(()=>{ worker.parentPort.postMessage({
|
30
|
+
workerID: process.pid, port: query.port,
|
31
|
+
protocol: 'HTTP', status: 'started',
|
32
|
+
}); }).catch(e=>{ process.exit(1); });
|
33
|
+
}); })();
|
44
34
|
|
45
35
|
/* --------------------------------------------------------------------------------------- */
|