-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsec-mem.js
More file actions
105 lines (90 loc) · 3.19 KB
/
Copy pathsec-mem.js
File metadata and controls
105 lines (90 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const crypto = require('crypto');
class LocalMemoryStore {
constructor(secretKey) {
this.data_store = {};
this.expiration_times = {};
this.store_event_data_handlers = {};
this.secretKey = secretKey;
}
async operate(id, method, data) {
try {
switch (method) {
case 'new':
await this.clear_data(id);
break;
case 'set':
await this.memorize_data(id, data);
break;
case 'get':
return await this.get_data(id);
default:
throw new Error('Invalid method');
}
} catch (error) {
console.error('Error:', error.message);
throw error;
}
}
async clear_data(id) {
delete this.data_store[id];
delete this.expiration_times[id];
this.emit_event('clear', id);
return { success: true, message: 'Data deleted' };
}
async memorize_data(id, data, expire_time = null) {
const encryptedData = this.encryptData(data);
this.data_store[id] = encryptedData;
if (expire_time) {
this.expiration_times[id] = Date.now() + expire_time;
}
this.emit_event('set', id);
return { success: true, message: 'Data memorized' };
}
async get_data(id) {
const encryptedData = this.data_store[id];
const expiration_time = this.expiration_times[id];
if (!encryptedData || (expiration_time && Date.now() > expiration_time)) {
await this.clear_data(id);
return null;
}
return this.decryptData(encryptedData) || null;
}
encryptData(data) {
const cipher = crypto.createCipher('aes-256-cbc', this.secretKey);
let encryptedData = cipher.update(JSON.stringify(data), 'utf-8', 'hex');
encryptedData += cipher.final('hex');
return encryptedData;
}
decryptData(encryptedData) {
const decipher = crypto.createDecipher('aes-256-cbc', this.secretKey);
let decryptedData = decipher.update(encryptedData, 'hex', 'utf-8');
decryptedData += decipher.final('utf-8');
return JSON.parse(decryptedData);
}
async handle_operation(operation, id, ...args) {
try {
return await this[operation](id, ...args);
} catch (error) {
console.error('Error:', error.message);
throw error;
}
}
async schedule_expiration(id, expire_time) {
setTimeout(async () => {
await this.clear_data(id);
}, expire_time);
}
emit_event(event, id) {
const data_handlers = this.store_event_data_handlers[event] || [];
data_handlers.forEach(data_handler => data_handler(id));
}
on(event, data_handler) {
if (!this.store_event_data_handlers[event]) {
this.store_event_data_handlers[event] = [];
}
this.store_event_data_handlers[event].push(data_handler);
}
}
const secretKey = 'your_secret_key_here'; // Replace with your actual secret key
const localMemory = new LocalMemoryStore(secretKey);
export default localMemory;