molly-db 1.0.3 → 1.0.4
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 +1 -0
- package/module/localDB.js +232 -58
- package/module/streamDB.js +168 -48
- package/module/webDB.js +377 -0
- package/package.json +1 -1
package/main.js
CHANGED
@@ -2,5 +2,6 @@ const output = new Object();
|
|
2
2
|
|
3
3
|
try{ output.streamDB = require('./module/streamDB'); } catch(e) { /*console.log(e)*/ }
|
4
4
|
try{ output.localDB = require('./module/localDB'); } catch(e) { /*console.log(e)*/ }
|
5
|
+
try{ output.webDB = require('./module/webDB'); } catch(e) { /*console.log(e)*/ }
|
5
6
|
|
6
7
|
module.exports = output;
|
package/module/localDB.js
CHANGED
@@ -1,22 +1,65 @@
|
|
1
|
-
const readline = require('
|
1
|
+
const readline = require('readline');
|
2
|
+
const crypto = require('crypto-js');
|
2
3
|
const fs = require('fs');
|
3
4
|
|
5
|
+
//TODO: Function ---------------------------------------------------------------------------------//
|
6
|
+
function slugify(str){
|
7
|
+
const map = {
|
8
|
+
'o' : 'ó|ò|ô|õ|ö|Ó|Ò|Ô|Õ|Ö',
|
9
|
+
'a' : 'á|à|ã|â|ä|À|Á|Ã|Â|Ä',
|
10
|
+
'e' : 'é|è|ê|ë|É|È|Ê|Ë',
|
11
|
+
'i' : 'í|ì|î|ï|Í|Ì|Î|Ï',
|
12
|
+
'u' : 'ú|ù|û|ü|Ú|Ù|Û|Ü',
|
13
|
+
'c' : 'ç|Ç','n':'ñ|Ñ',
|
14
|
+
'' : /\s+|\W+/,
|
15
|
+
};
|
16
|
+
for (var pattern in map) {
|
17
|
+
str=str.replace( new RegExp(map[pattern],'gi' ), pattern);
|
18
|
+
} return str.toLowerCase();
|
19
|
+
}
|
20
|
+
|
21
|
+
const JsonFormatter = {
|
22
|
+
'stringify': function(cipherParams) {
|
23
|
+
var jsonObj = { ct: cipherParams.ciphertext.toString(crypto.enc.Base64) };
|
24
|
+
if (cipherParams.salt) jsonObj.s = cipherParams.salt.toString();
|
25
|
+
if (cipherParams.iv) jsonObj.iv = cipherParams.iv.toString();
|
26
|
+
return new Buffer(JSON.stringify(jsonObj)).toString('base64');
|
27
|
+
},
|
28
|
+
|
29
|
+
'parse': function(jsonStr) {
|
30
|
+
var jsonObj = JSON.parse( new Buffer(jsonStr,'base64').toString('UTF-8'));
|
31
|
+
var cipherParams = crypto.lib.CipherParams.create({
|
32
|
+
ciphertext: crypto.enc.Base64.parse(jsonObj.ct)
|
33
|
+
});
|
34
|
+
if (jsonObj.iv) cipherParams.iv = crypto.enc.Hex.parse(jsonObj.iv);
|
35
|
+
if (jsonObj.s) cipherParams.salt = crypto.enc.Hex.parse(jsonObj.s);
|
36
|
+
return cipherParams;
|
37
|
+
}
|
38
|
+
};
|
39
|
+
|
4
40
|
//TODO: Optimization FUnctions -------------------------------------------------------------------//
|
5
41
|
|
6
|
-
const _default = { offset: 0, length: 100 };
|
7
42
|
const init = function( _table,_config,_self ){
|
8
43
|
return {
|
9
|
-
|
44
|
+
_itr: readline.createInterface( createNewTable( `${_self.path}/${_table}.json` ) ),
|
45
|
+
_cfg: !_config ? _self.default : _config,
|
10
46
|
_tmp: `${_self.path}/${_table}_tmp.json`,
|
11
47
|
_path: `${_self.path}/${_table}.json`,
|
12
|
-
|
48
|
+
_res: new Array(),
|
49
|
+
_i: 0,
|
13
50
|
}
|
14
51
|
};
|
15
52
|
|
53
|
+
const lineConstrain = function( _i,_config ){
|
54
|
+
if( _i >= parseInt(_config.length)+parseInt(_config.offset) ) return 1;
|
55
|
+
else if ( _i < parseInt(_config.offset) ) return -1
|
56
|
+
else return 0;
|
57
|
+
}
|
58
|
+
|
16
59
|
const createNewTable = function( _path ){
|
17
60
|
if( !fs.existsSync( _path ) ) fs.writeFileSync( _path,"" );
|
18
61
|
const table = fs.createReadStream( _path );
|
19
|
-
return table;
|
62
|
+
return { debug: false, input: table };
|
20
63
|
}
|
21
64
|
|
22
65
|
const createNewHash = function( _object ){
|
@@ -27,64 +70,195 @@ const createNewHash = function( _object ){
|
|
27
70
|
return _object;
|
28
71
|
}
|
29
72
|
|
30
|
-
function config(_config){
|
31
|
-
Object.keys(_config).map((x)=>{
|
32
|
-
_default[x] = _config[x];
|
33
|
-
}); return _default;
|
34
|
-
}
|
35
|
-
|
36
73
|
//TODO: localDB Class ----------------------------------------------------------------------------//
|
37
74
|
|
38
|
-
class localDB
|
75
|
+
class localDB{
|
39
76
|
|
77
|
+
encrypted = false
|
78
|
+
events = new Object()
|
40
79
|
default = { offset: 0, length: 100 }
|
41
80
|
|
42
|
-
constructor(
|
43
|
-
if(
|
44
|
-
this.password =
|
45
|
-
|
81
|
+
constructor( object ){
|
82
|
+
if( object.pass ){
|
83
|
+
this.password = object.pass;
|
84
|
+
this.encrypted = true;
|
85
|
+
} this.path = object.path;
|
46
86
|
}
|
47
87
|
|
48
|
-
// TODO:
|
88
|
+
// TODO: Encription & Decription DATA //
|
89
|
+
encrypt( _message,_password=this.password,_encrypted=this.encrypted ){
|
90
|
+
try{
|
91
|
+
if( _encrypted )
|
92
|
+
return crypto.AES.encrypt( _message,_password,{
|
93
|
+
format: JsonFormatter
|
94
|
+
}).toString(); return _message;
|
95
|
+
} catch(e) { return _message; }
|
96
|
+
}
|
97
|
+
|
98
|
+
decrypt( _message,_password=this.password,_encrypted=this.encrypted ){
|
99
|
+
try{
|
100
|
+
if( _encrypted )
|
101
|
+
return crypto.AES.decrypt( _message,_password,{
|
102
|
+
format: JsonFormatter
|
103
|
+
}).toString( crypto.enc.Utf8 );
|
104
|
+
return _message;
|
105
|
+
} catch(e) { return _message; }
|
106
|
+
}
|
107
|
+
|
108
|
+
// TODO: Searching functions //
|
49
109
|
list( _table, _config ){
|
50
|
-
return new Promise(
|
51
|
-
|
52
|
-
let
|
53
|
-
|
54
|
-
|
55
|
-
|
110
|
+
return new Promise( (res,rej)=>{ try{
|
111
|
+
|
112
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,_config,this );
|
113
|
+
|
114
|
+
_itr.on( 'line',( encryptedLine )=>{ try{
|
115
|
+
const line = this.decrypt( encryptedLine );
|
116
|
+
const cns = lineConstrain( _i, _cfg );
|
117
|
+
if( cns == 0 ) { _res.push( JSON.parse( line ) );
|
118
|
+
} else if( cns == 1 ) _itr.close();
|
119
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
120
|
+
_i++; });
|
121
|
+
|
122
|
+
_itr.on( 'close',()=>{ res({
|
123
|
+
table:_table,
|
124
|
+
data:_res,
|
125
|
+
}); });
|
126
|
+
|
127
|
+
} catch(e) { rej( e ); } });
|
56
128
|
}
|
57
129
|
|
58
130
|
find( _table, _target, _config ){
|
59
|
-
return new Promise( async(
|
60
|
-
|
61
|
-
let
|
62
|
-
|
63
|
-
|
64
|
-
|
131
|
+
return new Promise( async(res,rej)=>{ try{
|
132
|
+
|
133
|
+
let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
|
134
|
+
|
135
|
+
_itr.on( 'line',( encryptedLine )=>{
|
136
|
+
try{
|
137
|
+
const line = this.decrypt( encryptedLine );
|
138
|
+
const keys = Object.keys( _target );
|
139
|
+
const data = JSON.parse( line );
|
140
|
+
|
141
|
+
const regex = ( x )=>{
|
142
|
+
const target = slugify(_target[x].toString());
|
143
|
+
const info = slugify(data[x].toString());
|
144
|
+
const regex = new RegExp(target,'gi');
|
145
|
+
return regex.test(info);
|
146
|
+
}
|
147
|
+
|
148
|
+
const every = keys.every( (x)=>{return regex(x)} );
|
149
|
+
if( ( every ) ){
|
150
|
+
const cns = lineConstrain( _i, _cfg );
|
151
|
+
if( cns == 0 ) _res.push( data );
|
152
|
+
else if( cns == 1 ) _itr.close();
|
153
|
+
_i++;}
|
154
|
+
|
155
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
156
|
+
});
|
157
|
+
|
158
|
+
_itr.on( 'close',()=>{ res({
|
159
|
+
table:_table,
|
160
|
+
data:_res,
|
161
|
+
}); });
|
162
|
+
|
163
|
+
} catch(e) { rej( e ); } });
|
65
164
|
}
|
66
165
|
|
67
166
|
match( _table,_match,_config ){
|
68
|
-
return new Promise( async(
|
69
|
-
|
70
|
-
let
|
71
|
-
|
72
|
-
|
73
|
-
|
167
|
+
return new Promise( async(res,rej)=>{ try{
|
168
|
+
|
169
|
+
let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
|
170
|
+
|
171
|
+
_itr.on( 'line',( encryptedLine )=>{ try{
|
172
|
+
const regex = new RegExp(slugify(_match),'gi');
|
173
|
+
const line = this.decrypt( encryptedLine );
|
174
|
+
if( regex.test(slugify(line)) ){
|
175
|
+
const cns = lineConstrain( _i, _cfg );
|
176
|
+
if( cns == 0 ) _res.push( JSON.parse( line ) );
|
177
|
+
else if( cns == 1 ) _itr.close();
|
178
|
+
_i++;}
|
179
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
180
|
+
});
|
181
|
+
|
182
|
+
_itr.on( 'close',()=>{ res({
|
183
|
+
table:_table,
|
184
|
+
data:_res,
|
185
|
+
}); });
|
186
|
+
|
187
|
+
} catch(e) { rej( e ); } });
|
74
188
|
}
|
75
189
|
|
76
190
|
hash( _table, _hash ){
|
77
|
-
return new Promise(
|
78
|
-
|
79
|
-
let
|
80
|
-
|
81
|
-
|
82
|
-
|
191
|
+
return new Promise( (res,rej)=>{ try{
|
192
|
+
|
193
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
194
|
+
|
195
|
+
_itr.on( 'line',( encryptedLine )=>{ try{
|
196
|
+
const line = this.decrypt( encryptedLine );
|
197
|
+
const data = JSON.parse( line );
|
198
|
+
if( data.hash == _hash ){
|
199
|
+
_res.push( data );
|
200
|
+
_itr.close();
|
201
|
+
}
|
202
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
203
|
+
});
|
204
|
+
|
205
|
+
_itr.on( 'close',()=>{ res({
|
206
|
+
table:_table,
|
207
|
+
data:_res,
|
208
|
+
}); });
|
209
|
+
|
210
|
+
} catch(e) { rej( e ); } });
|
211
|
+
}
|
212
|
+
|
213
|
+
// TODO: Saving functions //
|
214
|
+
encryptTable( _table,_password ){
|
215
|
+
return new Promise( (res,rej)=>{ try{
|
216
|
+
|
217
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
218
|
+
const writable = fs.createWriteStream( _tmp );
|
219
|
+
|
220
|
+
writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res(); });
|
221
|
+
|
222
|
+
_itr.on( 'line',( encryptedLine )=>{
|
223
|
+
try{
|
224
|
+
const line = this.decrypt( encryptedLine );
|
225
|
+
const encryptedData = this.encrypt( line,_password,true );
|
226
|
+
writable.write( `${encryptedData}\n` );
|
227
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
228
|
+
});
|
229
|
+
|
230
|
+
_itr.on( 'close',()=>{ writable.end(); res({
|
231
|
+
table: _table,
|
232
|
+
}); });
|
233
|
+
|
234
|
+
} catch(e) { rej( e ); } });
|
235
|
+
}
|
236
|
+
|
237
|
+
decryptTable( _table ){
|
238
|
+
return new Promise( (res,rej)=>{ try{
|
239
|
+
|
240
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
241
|
+
const writable = fs.createWriteStream( _tmp );
|
242
|
+
|
243
|
+
writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res(); });
|
244
|
+
_itr.on( 'line',( encryptedLine )=>{
|
245
|
+
try{
|
246
|
+
const line = this.decrypt( encryptedLine );
|
247
|
+
writable.write( `${line}\n` );
|
248
|
+
|
249
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
250
|
+
});
|
251
|
+
|
252
|
+
_itr.on( 'close',()=>{ writable.end(); res({
|
253
|
+
table: _table,
|
254
|
+
}); });
|
255
|
+
|
256
|
+
} catch(e) { rej( e ); } });
|
83
257
|
}
|
84
258
|
|
85
|
-
// TODO: Saving functions
|
259
|
+
// TODO: Saving functions //
|
86
260
|
push( _table, ..._object ){
|
87
|
-
return new Promise( (
|
261
|
+
return new Promise( (res,rej)=>{ try{
|
88
262
|
|
89
263
|
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
90
264
|
const writable = fs.createWriteStream( _tmp );
|
@@ -103,11 +277,11 @@ class localDB {
|
|
103
277
|
table: _table,
|
104
278
|
}); });
|
105
279
|
|
106
|
-
} catch(e) {
|
280
|
+
} catch(e) { rej(e); }
|
107
281
|
}); }
|
108
282
|
|
109
283
|
unshift( _table, ..._object ){
|
110
|
-
return new Promise( (
|
284
|
+
return new Promise( (res,rej)=>{ try{
|
111
285
|
|
112
286
|
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
113
287
|
const writable = fs.createWriteStream( _tmp );
|
@@ -128,11 +302,11 @@ class localDB {
|
|
128
302
|
table: _table,
|
129
303
|
}); });
|
130
304
|
|
131
|
-
} catch(e) {
|
305
|
+
} catch(e) { rej(e) }
|
132
306
|
}); }
|
133
307
|
|
134
308
|
place( _table, _line, ..._object ){
|
135
|
-
return new Promise( (
|
309
|
+
return new Promise( (res,rej)=>{ try{
|
136
310
|
|
137
311
|
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
138
312
|
const writable = fs.createWriteStream( _tmp );
|
@@ -153,11 +327,11 @@ class localDB {
|
|
153
327
|
table: _table,
|
154
328
|
}); });
|
155
329
|
|
156
|
-
} catch(e) {
|
330
|
+
} catch(e) { rej(e) }
|
157
331
|
}); }
|
158
332
|
|
159
333
|
update( _table, _hash, ..._object ){
|
160
|
-
return new Promise( (
|
334
|
+
return new Promise( (res,rej)=>{ try{
|
161
335
|
|
162
336
|
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
163
337
|
const writable = fs.createWriteStream( _tmp );
|
@@ -173,7 +347,7 @@ class localDB {
|
|
173
347
|
});
|
174
348
|
} else
|
175
349
|
writable.write( `${encryptedLine}\n` );
|
176
|
-
} catch(e) {
|
350
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
177
351
|
});
|
178
352
|
|
179
353
|
_itr.on( 'close',()=>{ writable.end(); });
|
@@ -182,12 +356,12 @@ class localDB {
|
|
182
356
|
table: _table,
|
183
357
|
}); });
|
184
358
|
|
185
|
-
} catch(e) {
|
359
|
+
} catch(e) { rej(e) }
|
186
360
|
}); }
|
187
361
|
|
188
362
|
// TODO: Removing functions //
|
189
363
|
remove( _table, _hash ){
|
190
|
-
return new Promise( (
|
364
|
+
return new Promise( (res,rej)=>{ try{
|
191
365
|
|
192
366
|
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
193
367
|
const writable = fs.createWriteStream( _tmp );
|
@@ -205,21 +379,21 @@ class localDB {
|
|
205
379
|
table: _table,
|
206
380
|
}); });
|
207
381
|
|
208
|
-
} catch(e) {
|
382
|
+
} catch(e) { rej(e) }
|
209
383
|
}); }
|
210
384
|
|
211
385
|
removeTable( _table ){
|
212
|
-
return new Promise( (
|
386
|
+
return new Promise( (res,rej)=>{
|
213
387
|
const _path = `${this.path}/${_table}`;
|
214
388
|
fs.unlink( _path,( err,data )=>{
|
215
|
-
if(err)
|
389
|
+
if(err) rej( err );
|
216
390
|
res({ table: _table });
|
217
391
|
});
|
218
392
|
})
|
219
393
|
}
|
220
394
|
|
221
395
|
shift( _table ){
|
222
|
-
return new Promise( (
|
396
|
+
return new Promise( (res,rej)=>{ try{
|
223
397
|
|
224
398
|
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
225
399
|
const writable = fs.createWriteStream( _tmp );
|
@@ -232,10 +406,10 @@ class localDB {
|
|
232
406
|
table: _table,
|
233
407
|
}); });
|
234
408
|
|
235
|
-
} catch(e) {
|
409
|
+
} catch(e) { rej(e) }
|
236
410
|
}); }
|
237
411
|
|
238
412
|
}
|
239
413
|
|
240
414
|
//TODO: localDB Class ----------------------------------------------------------------------------//
|
241
|
-
module.exports = localDB;
|
415
|
+
module.exports = localDB;
|
package/module/streamDB.js
CHANGED
@@ -1,77 +1,197 @@
|
|
1
|
-
|
2
|
-
const
|
1
|
+
|
2
|
+
const readline = require('readline');
|
3
|
+
const crypto = require('crypto-js');
|
4
|
+
const axios = require('axios');
|
3
5
|
const fs = require('fs');
|
4
6
|
|
5
|
-
//TODO:
|
7
|
+
//TODO: Function ---------------------------------------------------------------------------------//
|
8
|
+
function slugify(str) {
|
9
|
+
const map = {
|
10
|
+
'o' : 'ó|ò|ô|õ|ö|Ó|Ò|Ô|Õ|Ö',
|
11
|
+
'a' : 'á|à|ã|â|ä|À|Á|Ã|Â|Ä',
|
12
|
+
'e' : 'é|è|ê|ë|É|È|Ê|Ë',
|
13
|
+
'i' : 'í|ì|î|ï|Í|Ì|Î|Ï',
|
14
|
+
'u' : 'ú|ù|û|ü|Ú|Ù|Û|Ü',
|
15
|
+
'c' : 'ç|Ç','n':'ñ|Ñ',
|
16
|
+
'' : /\s+|\W+/,
|
17
|
+
};
|
18
|
+
for (var pattern in map) {
|
19
|
+
str=str.replace( new RegExp(map[pattern],'gi' ), pattern);
|
20
|
+
} return str.toLowerCase();
|
21
|
+
}
|
6
22
|
|
7
|
-
const
|
23
|
+
const JsonFormatter = {
|
24
|
+
'stringify': function(cipherParams) {
|
25
|
+
var jsonObj = { ct: cipherParams.ciphertext.toString(crypto.enc.Base64) };
|
26
|
+
if (cipherParams.salt) jsonObj.s = cipherParams.salt.toString();
|
27
|
+
if (cipherParams.iv) jsonObj.iv = cipherParams.iv.toString();
|
28
|
+
return new Buffer(JSON.stringify(jsonObj)).toString('base64');
|
29
|
+
},
|
30
|
+
|
31
|
+
'parse': function(jsonStr) {
|
32
|
+
var jsonObj = JSON.parse( new Buffer(jsonStr,'base64').toString('UTF-8'));
|
33
|
+
var cipherParams = crypto.lib.CipherParams.create({
|
34
|
+
ciphertext: crypto.enc.Base64.parse(jsonObj.ct)
|
35
|
+
});
|
36
|
+
if (jsonObj.iv) cipherParams.iv = crypto.enc.Hex.parse(jsonObj.iv);
|
37
|
+
if (jsonObj.s) cipherParams.salt = crypto.enc.Hex.parse(jsonObj.s);
|
38
|
+
return cipherParams;
|
39
|
+
}
|
40
|
+
};
|
41
|
+
|
42
|
+
//TODO: Optimization FUnctions -------------------------------------------------------------------//
|
8
43
|
|
9
44
|
const init = function( _table,_config,_self ){
|
10
|
-
return new Promise( (
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
45
|
+
return new Promise( (res,rej)=>{
|
46
|
+
axios.get(`${_self.path}/${_table}.json`,{responseType:'stream'})
|
47
|
+
.then( ({data})=>{
|
48
|
+
res({
|
49
|
+
_itr: readline.createInterface({ input: data }),
|
50
|
+
_cfg: !_config ? _self.default : _config,
|
51
|
+
_res: new Array(),
|
52
|
+
_i: 0,
|
18
53
|
});
|
19
|
-
});
|
54
|
+
}) .catch( e=>rej() );
|
20
55
|
});
|
21
56
|
};
|
22
57
|
|
23
|
-
function
|
24
|
-
if(
|
25
|
-
|
26
|
-
|
27
|
-
}); return _default;
|
58
|
+
const lineConstrain = function( _i,_config ){
|
59
|
+
if( _i >= parseInt(_config.length)+parseInt(_config.offset) ) return 1;
|
60
|
+
else if ( _i < parseInt(_config.offset) ) return -1
|
61
|
+
else return 0;
|
28
62
|
}
|
29
63
|
|
30
64
|
//TODO: localDB Class ----------------------------------------------------------------------------//
|
31
65
|
|
32
66
|
class streamDB{
|
33
67
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
68
|
+
encrypted = false
|
69
|
+
events = new Object()
|
70
|
+
default = { offset: 0, length: 100 }
|
71
|
+
|
72
|
+
constructor( object ){
|
73
|
+
if( object.pass ){
|
74
|
+
this.password = object.pass;
|
75
|
+
this.encrypted = true;
|
76
|
+
} this.path = object.path;
|
38
77
|
}
|
39
78
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
} catch(e) {
|
79
|
+
decrypt( _message,_password=this.password,_encrypted=this.encrypted ){
|
80
|
+
try{
|
81
|
+
if( _encrypted )
|
82
|
+
return crypto.AES.decrypt( _message,_password,{
|
83
|
+
format: JsonFormatter
|
84
|
+
}).toString( crypto.enc.Utf8 );
|
85
|
+
return _message;
|
86
|
+
} catch(e) {
|
87
|
+
return _message;
|
88
|
+
}
|
48
89
|
}
|
49
90
|
|
91
|
+
// TODO: Searching functions //
|
92
|
+
list( _table, _config ){
|
93
|
+
return new Promise( async(res,rej)=>{ try{
|
94
|
+
|
95
|
+
let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
|
96
|
+
|
97
|
+
_itr.on( 'line',( encryptedLine )=>{ try{
|
98
|
+
const line = this.decrypt( encryptedLine );
|
99
|
+
const cns = lineConstrain( _i, _cfg );
|
100
|
+
if( cns == 0 ) { _res.push( JSON.parse( line ) );
|
101
|
+
} else if( cns == 1 ) _itr.close();
|
102
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
103
|
+
_i++; });
|
104
|
+
|
105
|
+
_itr.on( 'close',()=>{ res({
|
106
|
+
table:_table,
|
107
|
+
data:_res,
|
108
|
+
}); });
|
109
|
+
|
110
|
+
} catch(e) { rej( e ); } });
|
111
|
+
}
|
112
|
+
|
50
113
|
find( _table, _target, _config ){
|
51
|
-
return new Promise( async(
|
52
|
-
|
53
|
-
let
|
54
|
-
|
55
|
-
|
56
|
-
|
114
|
+
return new Promise( async(res,rej)=>{ try{
|
115
|
+
|
116
|
+
let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
|
117
|
+
|
118
|
+
_itr.on( 'line',( encryptedLine )=>{
|
119
|
+
try{
|
120
|
+
const line = this.decrypt( encryptedLine );
|
121
|
+
const keys = Object.keys( _target );
|
122
|
+
const data = JSON.parse( line );
|
123
|
+
|
124
|
+
const regex = ( x )=>{
|
125
|
+
const target = slugify(_target[x].toString());
|
126
|
+
const info = slugify(data[x].toString());
|
127
|
+
const regex = new RegExp(target,'gi');
|
128
|
+
return regex.test(info);
|
129
|
+
}
|
130
|
+
|
131
|
+
const every = keys.every( (x)=>{return regex(x)} );
|
132
|
+
|
133
|
+
if( every ){
|
134
|
+
const cns = lineConstrain( _i, _cfg );
|
135
|
+
if( cns == 0 ) _res.push( data );
|
136
|
+
else if( cns == 1 ) _itr.close();
|
137
|
+
_i++;}
|
138
|
+
|
139
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
140
|
+
});
|
141
|
+
|
142
|
+
_itr.on( 'close',()=>{ res({
|
143
|
+
table:_table,
|
144
|
+
data:_res,
|
145
|
+
}); });
|
146
|
+
|
147
|
+
} catch(e) { rej( e ); } });
|
57
148
|
}
|
58
149
|
|
59
150
|
match( _table,_match,_config ){
|
60
|
-
return new Promise( async(
|
61
|
-
|
62
|
-
let
|
63
|
-
|
64
|
-
|
65
|
-
|
151
|
+
return new Promise( async(res,rej)=>{ try{
|
152
|
+
|
153
|
+
let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
|
154
|
+
|
155
|
+
_itr.on( 'line',( encryptedLine )=>{ try{
|
156
|
+
const regex = new RegExp(slugify(_match),'gi');
|
157
|
+
const line = this.decrypt( encryptedLine );
|
158
|
+
if( regex.test(slugify(line)) ){
|
159
|
+
const cns = lineConstrain( _i, _cfg );
|
160
|
+
if( cns == 0 ) _res.push( JSON.parse( line ) );
|
161
|
+
else if( cns == 1 ) _itr.close();
|
162
|
+
_i++;}
|
163
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
164
|
+
});
|
165
|
+
|
166
|
+
_itr.on( 'close',()=>{ res({
|
167
|
+
table:_table,
|
168
|
+
data:_res,
|
169
|
+
}); });
|
170
|
+
|
171
|
+
} catch(e) { rej( e ); } });
|
66
172
|
}
|
67
173
|
|
68
174
|
hash( _table, _hash ){
|
69
|
-
return new Promise( async(
|
70
|
-
|
71
|
-
let
|
72
|
-
|
73
|
-
|
74
|
-
|
175
|
+
return new Promise( async(res,rej)=>{ try{
|
176
|
+
|
177
|
+
let { _i,_cfg,_itr,_res } = await init( _table,null,this );
|
178
|
+
|
179
|
+
_itr.on( 'line',( encryptedLine )=>{ try{
|
180
|
+
const line = this.decrypt( encryptedLine );
|
181
|
+
const data = JSON.parse( line );
|
182
|
+
if( data.hash == _hash ){
|
183
|
+
_res.push( data );
|
184
|
+
_itr.close();
|
185
|
+
}
|
186
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
187
|
+
});
|
188
|
+
|
189
|
+
_itr.on( 'close',()=>{ res({
|
190
|
+
table:_table,
|
191
|
+
data:_res,
|
192
|
+
}); });
|
193
|
+
|
194
|
+
} catch(e) { rej( e ); } });
|
75
195
|
}
|
76
196
|
|
77
197
|
}
|
package/module/webDB.js
ADDED
@@ -0,0 +1,377 @@
|
|
1
|
+
const crypto = require('crypto-js');
|
2
|
+
|
3
|
+
//TODO: Function ---------------------------------------------------------------------------------//
|
4
|
+
|
5
|
+
const store = {
|
6
|
+
set: function(key,data){ localStorage.setItem(key,data); },
|
7
|
+
key: function(index){ return localStorage.key(index); },
|
8
|
+
get: function(key){ return localStorage.getItem(key); },
|
9
|
+
clear: function(){ localStorage.clear(); },
|
10
|
+
length: localStorage.length,
|
11
|
+
}
|
12
|
+
|
13
|
+
function slugify(str) {
|
14
|
+
const map = {
|
15
|
+
'o' : 'ó|ò|ô|õ|ö|Ó|Ò|Ô|Õ|Ö',
|
16
|
+
'a' : 'á|à|ã|â|ä|À|Á|Ã|Â|Ä',
|
17
|
+
'e' : 'é|è|ê|ë|É|È|Ê|Ë',
|
18
|
+
'i' : 'í|ì|î|ï|Í|Ì|Î|Ï',
|
19
|
+
'u' : 'ú|ù|û|ü|Ú|Ù|Û|Ü',
|
20
|
+
'c' : 'ç|Ç','n':'ñ|Ñ',
|
21
|
+
'' : /\s+|\W+/,
|
22
|
+
};
|
23
|
+
for (var pattern in map) {
|
24
|
+
str=str.replace( new RegExp(map[pattern],'gi' ), pattern);
|
25
|
+
} return str.toLowerCase();
|
26
|
+
}
|
27
|
+
|
28
|
+
//TODO: Function ---------------------------------------------------------------------------------//
|
29
|
+
|
30
|
+
const JsonFormatter = {
|
31
|
+
'stringify': function(cipherParams) {
|
32
|
+
var jsonObj = { ct: cipherParams.ciphertext.toString(crypto.enc.Base64) };
|
33
|
+
if (cipherParams.salt) jsonObj.s = cipherParams.salt.toString();
|
34
|
+
if (cipherParams.iv) jsonObj.iv = cipherParams.iv.toString();
|
35
|
+
return btoa( JSON.stringify(jsonObj) );
|
36
|
+
},
|
37
|
+
|
38
|
+
'parse': function(jsonStr) {
|
39
|
+
var jsonObj = JSON.parse( atob(jsonStr) );
|
40
|
+
var cipherParams = crypto.lib.CipherParams.create({
|
41
|
+
ciphertext: crypto.enc.Base64.parse(jsonObj.ct)
|
42
|
+
});
|
43
|
+
if (jsonObj.iv) cipherParams.iv = crypto.enc.Hex.parse(jsonObj.iv);
|
44
|
+
if (jsonObj.s) cipherParams.salt = crypto.enc.Hex.parse(jsonObj.s);
|
45
|
+
return cipherParams;
|
46
|
+
}
|
47
|
+
};
|
48
|
+
|
49
|
+
//TODO: Function ---------------------------------------------------------------------------------//
|
50
|
+
|
51
|
+
const init = function( _table,_config,_self ){
|
52
|
+
return {
|
53
|
+
_cfg: !_config ? _self.default : _config,
|
54
|
+
_itr: _createNewTable_( _table ),
|
55
|
+
_res: new Array(),
|
56
|
+
_i: 0,
|
57
|
+
}
|
58
|
+
};
|
59
|
+
|
60
|
+
const _lineConstrain_ = function( _i,_config ){
|
61
|
+
if( _i >= parseInt(_config.length)+parseInt(_config.offset) ) return 1;
|
62
|
+
else if ( _i < parseInt(_config.offset) ) return -1
|
63
|
+
else return 0;
|
64
|
+
}
|
65
|
+
|
66
|
+
const _createNewTable_ = function( _path ){
|
67
|
+
const content = store.get( _path )
|
68
|
+
if( content == null ) return new Array();
|
69
|
+
return content.split('\n');
|
70
|
+
}
|
71
|
+
|
72
|
+
const _createNewHash_ = function( _object ){
|
73
|
+
_object['_stamp'] = Date.now();
|
74
|
+
const _base = JSON.stringify( _object );
|
75
|
+
if( !_object.hash )
|
76
|
+
_object.hash = crypto.SHA256( _base ).toString();
|
77
|
+
return _object;
|
78
|
+
}
|
79
|
+
|
80
|
+
|
81
|
+
//TODO: localDB Class ----------------------------------------------------------------------------//
|
82
|
+
|
83
|
+
class createWebDB{
|
84
|
+
|
85
|
+
constructor( _password ){
|
86
|
+
this.encrypted = false
|
87
|
+
this.events = new Object()
|
88
|
+
this.default = { offset: 0, length: 100 }
|
89
|
+
if( !crypto ) return console.log(' please import libs.crypto ');
|
90
|
+
if( _password ){
|
91
|
+
this.password = _password;
|
92
|
+
this.encrypted = true;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
//TODO: clear all LocalStorage //
|
97
|
+
removeTable( _table ){
|
98
|
+
store.set( _table, null );
|
99
|
+
delete localStorage[ _table ];
|
100
|
+
}
|
101
|
+
|
102
|
+
//TODO: clear all LocalStorage //
|
103
|
+
removeTable( _table ){
|
104
|
+
store.set( _table, null );
|
105
|
+
delete localStorage[ _table ];
|
106
|
+
}
|
107
|
+
|
108
|
+
clearDB(){ store.clear(); }
|
109
|
+
|
110
|
+
// TODO: Encription & Decription DATA //
|
111
|
+
encrypt( _message,_password=this.password,_encrypted=this.encrypted ){
|
112
|
+
try{
|
113
|
+
if( this.encrypted )
|
114
|
+
return crypto.AES.encrypt( _message,this.password,{
|
115
|
+
format: JsonFormatter
|
116
|
+
}).toString( crypto.enc.Utf8 );
|
117
|
+
return _message;
|
118
|
+
} catch(e) { return _message; }
|
119
|
+
}
|
120
|
+
|
121
|
+
decrypt( _message,_password=this.password,_encrypted=this.encrypted ){
|
122
|
+
try{
|
123
|
+
if( this.encrypted )
|
124
|
+
return crypto.AES.decrypt( _message,this.password,{
|
125
|
+
format: JsonFormatter
|
126
|
+
}).toString( crypto.enc.Utf8 );
|
127
|
+
return _message;
|
128
|
+
} catch(e) { return _message; }
|
129
|
+
}
|
130
|
+
|
131
|
+
// TODO: Searching functions //
|
132
|
+
list( _table, _config ){
|
133
|
+
return new Promise( (res,rej)=>{ try{
|
134
|
+
|
135
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,_config,this );
|
136
|
+
|
137
|
+
_itr.every( ( encryptedLine )=>{ try{
|
138
|
+
|
139
|
+
const line = this.decrypt( encryptedLine );
|
140
|
+
const cns = _lineConstrain_( _i, _cfg );
|
141
|
+
const data = JSON.parse( line );
|
142
|
+
|
143
|
+
if( cns == 0 ) _res.push( data );
|
144
|
+
else if( cns == 1 ) return false;
|
145
|
+
|
146
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
147
|
+
|
148
|
+
_i++; return true; }); res({data:_res,table:_table});
|
149
|
+
|
150
|
+
} catch(e) { rej( e ); } });
|
151
|
+
}
|
152
|
+
|
153
|
+
find( _table, _target, _logic='AND', _config ){
|
154
|
+
return new Promise( (res,rej)=>{ try{
|
155
|
+
|
156
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,_config,this );
|
157
|
+
|
158
|
+
_itr.every( ( encryptedLine )=>{ try{
|
159
|
+
|
160
|
+
const line = this.decrypt( encryptedLine );
|
161
|
+
const keys = Object.keys( _target );
|
162
|
+
const data = JSON.parse( line );
|
163
|
+
|
164
|
+
const regex = ( x )=>{
|
165
|
+
const target = slugify(_target[x].toString());
|
166
|
+
const data = slugify(data[x].toString());
|
167
|
+
const regex = new RegExp(target,'gi');
|
168
|
+
return regex.test(data);
|
169
|
+
}
|
170
|
+
|
171
|
+
const every = keys.every( (x)=>{return regex(x)} );
|
172
|
+
const some = keys.some( (x)=>{return regex(x)} );
|
173
|
+
|
174
|
+
if( ( (/AND/gi).test(_logic) && every ) || ( (/OR/gi).test(_logic) && some ) ){
|
175
|
+
const cns = _lineConstrain_(_i, _cfg );
|
176
|
+
if( cns == 0 ) _res.push( data );
|
177
|
+
else if( cns == 1 ) return false;
|
178
|
+
_i++;}
|
179
|
+
|
180
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
181
|
+
|
182
|
+
return true; }); res({data:_res,table:_table});
|
183
|
+
|
184
|
+
} catch(e) { rej( e ); } });
|
185
|
+
}
|
186
|
+
|
187
|
+
match( _table, _match, _config ){
|
188
|
+
return new Promise( (res,rej)=>{ try{
|
189
|
+
|
190
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,_config,this );
|
191
|
+
|
192
|
+
_itr.every( ( encryptedLine )=>{ try{
|
193
|
+
|
194
|
+
const line = this.decrypt( encryptedLine );
|
195
|
+
if( slugify(line).search( slugify(_match) ) >= 0 ){
|
196
|
+
const cns = _lineConstrain_(_i, _cfg );
|
197
|
+
const data = JSON.parse( line );
|
198
|
+
if( cns == 0 ) _res.push( data );
|
199
|
+
else if( cns == 1 ) return false;
|
200
|
+
i++;}
|
201
|
+
|
202
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
203
|
+
return true; }); res({data:_res,table:_table});
|
204
|
+
|
205
|
+
} catch(e) { rej( e ); } });
|
206
|
+
}
|
207
|
+
|
208
|
+
findByHash( _table, _hash ){
|
209
|
+
return new Promise( (res,rej)=>{ try{
|
210
|
+
|
211
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
212
|
+
|
213
|
+
_itr.every( ( encryptedLine )=>{ try{
|
214
|
+
|
215
|
+
const line = this.decrypt( encryptedLine );
|
216
|
+
const data = JSON.parse( line );
|
217
|
+
|
218
|
+
if( data.hash == _hash ){
|
219
|
+
_res.push( data );
|
220
|
+
return false;
|
221
|
+
}
|
222
|
+
|
223
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
224
|
+
_i++; return true; }); res({data:_res,table:_table});
|
225
|
+
|
226
|
+
} catch(e) { rej( e ); } });
|
227
|
+
}
|
228
|
+
|
229
|
+
// TODO: Saving functions //
|
230
|
+
push( _table, ..._object ){
|
231
|
+
return new Promise( (res,rej)=>{ try{
|
232
|
+
|
233
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
234
|
+
|
235
|
+
_object.flat().forEach( item=>{
|
236
|
+
item = _createNewHash_( item );
|
237
|
+
const data = JSON.stringify(item);
|
238
|
+
_itr.push( this.encrypt( data ) );
|
239
|
+
}); store.set( _table,_itr.join('\n') ); res({table:_table});
|
240
|
+
|
241
|
+
} catch(e) { rej( e ); } });
|
242
|
+
}
|
243
|
+
|
244
|
+
unshift( _table, ..._object ){
|
245
|
+
return new Promise( (res,rej)=>{ try{
|
246
|
+
|
247
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
248
|
+
|
249
|
+
_object.flat().forEach( item=>{
|
250
|
+
item = _createNewHash_( item );
|
251
|
+
const data = JSON.stringify(item);
|
252
|
+
_itr.unshift( this.encrypt(data) );
|
253
|
+
}); store.set( _table,_itr.join('\n') ); res({table:_table});
|
254
|
+
|
255
|
+
} catch(e) { rej( e ); } });
|
256
|
+
}
|
257
|
+
|
258
|
+
place( _table, _line, ..._object ){
|
259
|
+
return new Promise( (res,rej)=>{ try{
|
260
|
+
|
261
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
262
|
+
|
263
|
+
_object.flat().forEach( item=>{
|
264
|
+
item = _createNewHash_( item );
|
265
|
+
const data = JSON.stringify(item);
|
266
|
+
_itr.splice( _line,0,this.encrypt(data) );
|
267
|
+
}); store.set( _table,_itr.join('\n') ); res({table:_table});
|
268
|
+
|
269
|
+
} catch(e) { rej( e ); } });
|
270
|
+
}
|
271
|
+
|
272
|
+
update( _table, _hash, ..._object ){
|
273
|
+
return new Promise( (res,rej)=>{ try{
|
274
|
+
|
275
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
276
|
+
|
277
|
+
_itr.every( ( encryptedLine )=>{ try{
|
278
|
+
|
279
|
+
const line = this.decrypt( encryptedLine );
|
280
|
+
const data = JSON.parse( line );
|
281
|
+
|
282
|
+
if( data.hash == _hash ){
|
283
|
+
|
284
|
+
_object.flat().forEach( item=>{
|
285
|
+
item = _createNewHash_( item );
|
286
|
+
const data = JSON.stringify(item);
|
287
|
+
_itr.splice( _i,1,this.encrypt(data) );
|
288
|
+
}); return false;
|
289
|
+
}
|
290
|
+
|
291
|
+
_i++;} catch(e) { rej(`the db can be decripted ${e}`) }
|
292
|
+
return true; }); store.set( _table,_itr.join('\n') ); res({table:_table});
|
293
|
+
|
294
|
+
} catch(e) { rej( e ); } });
|
295
|
+
}
|
296
|
+
|
297
|
+
// TODO: Removing functions //
|
298
|
+
remove( _table, _hash ){
|
299
|
+
return new Promise( (res,rej)=>{ try{
|
300
|
+
|
301
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
302
|
+
|
303
|
+
_itr.every( ( encryptedLine )=>{ try{
|
304
|
+
|
305
|
+
const line = this.decrypt( encryptedLine );
|
306
|
+
const data = JSON.parse( line );
|
307
|
+
|
308
|
+
if( data.hash == _hash ){
|
309
|
+
_object = _createNewHash_( _object );
|
310
|
+
_itr.splice( _i,0 );
|
311
|
+
return false;
|
312
|
+
}
|
313
|
+
|
314
|
+
_i++;} catch(e) { rej(`the db can be decripted ${e}`) }
|
315
|
+
return true; }); store.set( _table,_itr.join('\n') ); res({table:_table});
|
316
|
+
|
317
|
+
} catch(e) { rej( e ); } });
|
318
|
+
}
|
319
|
+
|
320
|
+
shif( _table ){
|
321
|
+
return new Promise( (res,rej)=>{ try{
|
322
|
+
const _itr = _createNewTable_( _table );
|
323
|
+
_itr.shif(); store.set( _table,_itr.join('\n') ); res({table:_table});
|
324
|
+
} catch(e) { rej( e ); } });
|
325
|
+
}
|
326
|
+
|
327
|
+
pop( _table ){
|
328
|
+
return new Promise( (res,rej)=>{ try{
|
329
|
+
const _itr = _createNewTable_( _table );
|
330
|
+
_itr.pop(); store.set( _table,_itr.join('\n') ); res({table:_table});
|
331
|
+
} catch(e) { rej( e ); } });
|
332
|
+
}
|
333
|
+
|
334
|
+
// TODO: Saving functions //
|
335
|
+
encryptTable( _table,_password ){
|
336
|
+
return new Promise( (res,rej)=>{ try{
|
337
|
+
|
338
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
339
|
+
const writable = fs.createWriteStream( _tmp );
|
340
|
+
let db = new String();
|
341
|
+
|
342
|
+
_itr.every( ( encryptedLine )=>{
|
343
|
+
try{
|
344
|
+
const line = this.decrypt( encryptedLine );
|
345
|
+
const encryptedData = this.encrypt( line,_password,true );
|
346
|
+
db += `${encryptedData}\n`;
|
347
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
348
|
+
});
|
349
|
+
|
350
|
+
store.set(_table,db); res({ table:_table });
|
351
|
+
|
352
|
+
} catch(e) { rej( e ); } });
|
353
|
+
}
|
354
|
+
|
355
|
+
decryptTable( _table ){
|
356
|
+
return new Promise( (res,rej)=>{ try{
|
357
|
+
|
358
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
359
|
+
const writable = fs.createWriteStream( _tmp );
|
360
|
+
let db = new String();
|
361
|
+
|
362
|
+
_itr.on( 'line',( encryptedLine )=>{
|
363
|
+
try{
|
364
|
+
const line = this.decrypt( encryptedLine );
|
365
|
+
db += `${line}\n`;
|
366
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
367
|
+
});
|
368
|
+
|
369
|
+
store.set(_table,db); res({ table:_table });
|
370
|
+
|
371
|
+
} catch(e) { rej( e ); } });
|
372
|
+
}
|
373
|
+
|
374
|
+
}
|
375
|
+
|
376
|
+
// TODO: export --------------------------------------------------------------------------------------//
|
377
|
+
module.exports = createWebDB;
|