-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
81 lines (79 loc) · 1.64 KB
/
Copy pathutils.js
File metadata and controls
81 lines (79 loc) · 1.64 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
export class TTLCache {
options;
cache = new Map();
checkPeriod;
cleanupTimer;
constructor(options) {
this.options = options;
this.checkPeriod = options.checkPeriod || options.ttl;
this.startCleanup();
}
set(key, value, ttl) {
const expiresAt = Date.now() + (ttl || this.options.ttl);
this.cache.set(key, { value, expiresAt });
}
get(key) {
const entry = this.cache.get(key);
if (!entry) {
return undefined;
}
if (Date.now() > entry.expiresAt) {
this.cache.delete(key);
return undefined;
}
return entry.value;
}
has(key) {
return this.get(key) !== undefined;
}
delete(key) {
return this.cache.delete(key);
}
clear() {
this.cache.clear();
}
size() {
this.cleanup();
return this.cache.size;
}
cleanup() {
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now > entry.expiresAt) {
this.cache.delete(key);
}
}
}
startCleanup() {
this.cleanupTimer = setInterval(() => {
this.cleanup();
}, this.checkPeriod);
if (this.cleanupTimer.unref) {
this.cleanupTimer.unref();
}
}
destroy() {
if (this.cleanupTimer) {
clearInterval(this.cleanupTimer);
}
this.cache.clear();
}
}
export class MessageDeduplicator {
seen = new TTLCache({ ttl: 300000 }); // 5 minutes
isDuplicate(msgId) {
if (this.seen.has(msgId)) {
return true;
}
this.seen.set(msgId, true);
return false;
}
markAsSeen(msgId) {
this.seen.set(msgId, true);
}
}
export const CONSTANTS = {
AES_BLOCK_SIZE: 32,
AES_KEY_LENGTH: 43,
WECOM_TEXT_LIMIT: 2048,
};