molly-db 1.0.1
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/LICENSE +21 -0
- package/README.md +199 -0
- package/main.js +8 -0
- package/module/localDB.js +437 -0
- package/module/mongoDB.js +224 -0
- package/module/streamDB.js +221 -0
- package/module/webDB.js +395 -0
- package/package.json +12 -0
package/module/webDB.js
ADDED
@@ -0,0 +1,395 @@
|
|
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
|
+
on( _event, _callback ){ this.events[_event] = _callback; }
|
154
|
+
loop( _table ){
|
155
|
+
return new Promise( (res,rej)=>{ try{
|
156
|
+
|
157
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
158
|
+
if( this.events.start ) this.events.start( _itr );
|
159
|
+
|
160
|
+
_itr.every( ( encryptedLine )=>{ try{
|
161
|
+
const line = this.decrypt( encryptedLine );
|
162
|
+
if( this.events.data ) this.events.data( line,_i,_itr );
|
163
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
164
|
+
_i++; });
|
165
|
+
|
166
|
+
if( this.events.close ) this.events.close( _itr );
|
167
|
+
|
168
|
+
} catch(e) { rej( e ); } });
|
169
|
+
}
|
170
|
+
|
171
|
+
find( _table, _target, _logic='AND', _config ){
|
172
|
+
return new Promise( (res,rej)=>{ try{
|
173
|
+
|
174
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,_config,this );
|
175
|
+
|
176
|
+
_itr.every( ( encryptedLine )=>{ try{
|
177
|
+
|
178
|
+
const line = this.decrypt( encryptedLine );
|
179
|
+
const keys = Object.keys( _target );
|
180
|
+
const data = JSON.parse( line );
|
181
|
+
|
182
|
+
const regex = ( x )=>{
|
183
|
+
const target = slugify(_target[x].toString());
|
184
|
+
const data = slugify(data[x].toString());
|
185
|
+
const regex = new RegExp(target,'gi');
|
186
|
+
return regex.test(data);
|
187
|
+
}
|
188
|
+
|
189
|
+
const every = keys.every( (x)=>{return regex(x)} );
|
190
|
+
const some = keys.some( (x)=>{return regex(x)} );
|
191
|
+
|
192
|
+
if( ( (/AND/gi).test(_logic) && every ) || ( (/OR/gi).test(_logic) && some ) ){
|
193
|
+
const cns = _lineConstrain_(_i, _cfg );
|
194
|
+
if( cns == 0 ) _res.push( data );
|
195
|
+
else if( cns == 1 ) return false;
|
196
|
+
_i++;}
|
197
|
+
|
198
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
199
|
+
|
200
|
+
return true; }); res({data:_res,table:_table});
|
201
|
+
|
202
|
+
} catch(e) { rej( e ); } });
|
203
|
+
}
|
204
|
+
|
205
|
+
match( _table, _match, _config ){
|
206
|
+
return new Promise( (res,rej)=>{ try{
|
207
|
+
|
208
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,_config,this );
|
209
|
+
|
210
|
+
_itr.every( ( encryptedLine )=>{ try{
|
211
|
+
|
212
|
+
const line = this.decrypt( encryptedLine );
|
213
|
+
if( slugify(line).search( slugify(_match) ) >= 0 ){
|
214
|
+
const cns = _lineConstrain_(_i, _cfg );
|
215
|
+
const data = JSON.parse( line );
|
216
|
+
if( cns == 0 ) _res.push( data );
|
217
|
+
else if( cns == 1 ) return false;
|
218
|
+
i++;}
|
219
|
+
|
220
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
221
|
+
return true; }); res({data:_res,table:_table});
|
222
|
+
|
223
|
+
} catch(e) { rej( e ); } });
|
224
|
+
}
|
225
|
+
|
226
|
+
findByHash( _table, _hash ){
|
227
|
+
return new Promise( (res,rej)=>{ try{
|
228
|
+
|
229
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
230
|
+
|
231
|
+
_itr.every( ( encryptedLine )=>{ try{
|
232
|
+
|
233
|
+
const line = this.decrypt( encryptedLine );
|
234
|
+
const data = JSON.parse( line );
|
235
|
+
|
236
|
+
if( data.hash == _hash ){
|
237
|
+
_res.push( data );
|
238
|
+
return false;
|
239
|
+
}
|
240
|
+
|
241
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
242
|
+
_i++; return true; }); res({data:_res,table:_table});
|
243
|
+
|
244
|
+
} catch(e) { rej( e ); } });
|
245
|
+
}
|
246
|
+
|
247
|
+
// TODO: Saving functions //
|
248
|
+
push( _table, ..._object ){
|
249
|
+
return new Promise( (res,rej)=>{ try{
|
250
|
+
|
251
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
252
|
+
|
253
|
+
_object.flat().forEach( item=>{
|
254
|
+
item = _createNewHash_( item );
|
255
|
+
const data = JSON.stringify(item);
|
256
|
+
_itr.push( this.encrypt( data ) );
|
257
|
+
}); store.set( _table,_itr.join('\n') ); res({table:_table});
|
258
|
+
|
259
|
+
} catch(e) { rej( e ); } });
|
260
|
+
}
|
261
|
+
|
262
|
+
unshift( _table, ..._object ){
|
263
|
+
return new Promise( (res,rej)=>{ try{
|
264
|
+
|
265
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
266
|
+
|
267
|
+
_object.flat().forEach( item=>{
|
268
|
+
item = _createNewHash_( item );
|
269
|
+
const data = JSON.stringify(item);
|
270
|
+
_itr.unshift( this.encrypt(data) );
|
271
|
+
}); store.set( _table,_itr.join('\n') ); res({table:_table});
|
272
|
+
|
273
|
+
} catch(e) { rej( e ); } });
|
274
|
+
}
|
275
|
+
|
276
|
+
place( _table, _line, ..._object ){
|
277
|
+
return new Promise( (res,rej)=>{ try{
|
278
|
+
|
279
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
280
|
+
|
281
|
+
_object.flat().forEach( item=>{
|
282
|
+
item = _createNewHash_( item );
|
283
|
+
const data = JSON.stringify(item);
|
284
|
+
_itr.splice( _line,0,this.encrypt(data) );
|
285
|
+
}); store.set( _table,_itr.join('\n') ); res({table:_table});
|
286
|
+
|
287
|
+
} catch(e) { rej( e ); } });
|
288
|
+
}
|
289
|
+
|
290
|
+
update( _table, _hash, ..._object ){
|
291
|
+
return new Promise( (res,rej)=>{ try{
|
292
|
+
|
293
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
294
|
+
|
295
|
+
_itr.every( ( encryptedLine )=>{ try{
|
296
|
+
|
297
|
+
const line = this.decrypt( encryptedLine );
|
298
|
+
const data = JSON.parse( line );
|
299
|
+
|
300
|
+
if( data.hash == _hash ){
|
301
|
+
|
302
|
+
_object.flat().forEach( item=>{
|
303
|
+
item = _createNewHash_( item );
|
304
|
+
const data = JSON.stringify(item);
|
305
|
+
_itr.splice( _i,1,this.encrypt(data) );
|
306
|
+
}); return false;
|
307
|
+
}
|
308
|
+
|
309
|
+
_i++;} catch(e) { rej(`the db can be decripted ${e}`) }
|
310
|
+
return true; }); store.set( _table,_itr.join('\n') ); res({table:_table});
|
311
|
+
|
312
|
+
} catch(e) { rej( e ); } });
|
313
|
+
}
|
314
|
+
|
315
|
+
// TODO: Removing functions //
|
316
|
+
remove( _table, _hash ){
|
317
|
+
return new Promise( (res,rej)=>{ try{
|
318
|
+
|
319
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
320
|
+
|
321
|
+
_itr.every( ( encryptedLine )=>{ try{
|
322
|
+
|
323
|
+
const line = this.decrypt( encryptedLine );
|
324
|
+
const data = JSON.parse( line );
|
325
|
+
|
326
|
+
if( data.hash == _hash ){
|
327
|
+
_object = _createNewHash_( _object );
|
328
|
+
_itr.splice( _i,0 );
|
329
|
+
return false;
|
330
|
+
}
|
331
|
+
|
332
|
+
_i++;} catch(e) { rej(`the db can be decripted ${e}`) }
|
333
|
+
return true; }); store.set( _table,_itr.join('\n') ); res({table:_table});
|
334
|
+
|
335
|
+
} catch(e) { rej( e ); } });
|
336
|
+
}
|
337
|
+
|
338
|
+
shif( _table ){
|
339
|
+
return new Promise( (res,rej)=>{ try{
|
340
|
+
const _itr = _createNewTable_( _table );
|
341
|
+
_itr.shif(); store.set( _table,_itr.join('\n') ); res({table:_table});
|
342
|
+
} catch(e) { rej( e ); } });
|
343
|
+
}
|
344
|
+
|
345
|
+
pop( _table ){
|
346
|
+
return new Promise( (res,rej)=>{ try{
|
347
|
+
const _itr = _createNewTable_( _table );
|
348
|
+
_itr.pop(); store.set( _table,_itr.join('\n') ); res({table:_table});
|
349
|
+
} catch(e) { rej( e ); } });
|
350
|
+
}
|
351
|
+
|
352
|
+
// TODO: Saving functions //
|
353
|
+
encryptTable( _table,_password ){
|
354
|
+
return new Promise( (res,rej)=>{ try{
|
355
|
+
|
356
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
357
|
+
const writable = fs.createWriteStream( _tmp );
|
358
|
+
let db = new String();
|
359
|
+
|
360
|
+
_itr.every( ( encryptedLine )=>{
|
361
|
+
try{
|
362
|
+
const line = this.decrypt( encryptedLine );
|
363
|
+
const encryptedData = this.encrypt( line,_password,true );
|
364
|
+
db += `${encryptedData}\n`;
|
365
|
+
} catch(e) { rej(`the db can be decripted ${e}`) }
|
366
|
+
});
|
367
|
+
|
368
|
+
store.set(_table,db); res({ table:_table });
|
369
|
+
|
370
|
+
} catch(e) { rej( e ); } });
|
371
|
+
}
|
372
|
+
|
373
|
+
decryptTable( _table ){
|
374
|
+
return new Promise( (res,rej)=>{ try{
|
375
|
+
|
376
|
+
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
377
|
+
const writable = fs.createWriteStream( _tmp );
|
378
|
+
let db = new String();
|
379
|
+
|
380
|
+
_itr.on( 'line',( encryptedLine )=>{
|
381
|
+
try{
|
382
|
+
const line = this.decrypt( encryptedLine );
|
383
|
+
db += `${line}\n`;
|
384
|
+
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
385
|
+
});
|
386
|
+
|
387
|
+
store.set(_table,db); res({ table:_table });
|
388
|
+
|
389
|
+
} catch(e) { rej( e ); } });
|
390
|
+
}
|
391
|
+
|
392
|
+
}
|
393
|
+
|
394
|
+
// TODO: export --------------------------------------------------------------------------------------//
|
395
|
+
module.exports = createWebDB;
|
package/package.json
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
{
|
2
|
+
"license": "MIT",
|
3
|
+
"main": "main.js",
|
4
|
+
"version": "1.0.1",
|
5
|
+
"name": "molly-db",
|
6
|
+
"author": "bececrazy",
|
7
|
+
"scripts": { "start": "node main" },
|
8
|
+
"repository": "https://github.com/EDBC-REPO-NPM/Molly-db",
|
9
|
+
"dependencies": { "axios": "^0.27.2", "readline": "^1.3.0", "crypto-js": "^4.1.1", "fs": "^0.0.1-security" },
|
10
|
+
"description": "molly-db is a free and open source library for nodejs that allow you create a lightweight encrypted database using Json files",
|
11
|
+
"keywords": [ "json", "storage", "database", "datastorage", "web-database", "json-database", "encrypted-data", "web-local-storage", "encrypted-database", "web-encrypted-database" ]
|
12
|
+
}
|