Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
101 lines (82 loc) · 3.08 KB
/
Copy pathindex.ts
File metadata and controls
101 lines (82 loc) · 3.08 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
import type { KeyValueAdapter } from "adminforth";
// setTimeout fires right away when the delay doesn't fit into 32 bits (~24.8 days)
const MAX_TIMEOUT_MS = 2 ** 31 - 1;
export default class RAMKeyValueAdapter implements KeyValueAdapter {
private data: Map<string, any>;
private expirationTimers = new Map<string, NodeJS.Timeout>();
constructor() {
this.data = new Map();
}
validate() {
}
protected getActualKey(key: string, collection?: string): string {
if (collection) {
return `${collection}:${key}`;
}
return key;
}
async get(key: string, collection?: string): Promise<string> {
const actualKey = this.getActualKey(key, collection);
return this.data.get(actualKey);
}
async set(key: string, value: any, expiresInSeconds?: number, collection?: string): Promise<void> {
const actualKey = this.getActualKey(key, collection);
this.data.set(actualKey, value);
// the key may have been set before with another expiration
this.clearExpiration(actualKey);
if (expiresInSeconds) {
this.expireAfter(actualKey, expiresInSeconds * 1000);
}
}
async delete(key: string, collection?: string): Promise<void> {
const actualKey = this.getActualKey(key, collection);
this.data.delete(actualKey);
this.clearExpiration(actualKey);
}
/**
* Deletes the key after the delay, a delay longer than setTimeout accepts is waited out in steps.
*/
private expireAfter(actualKey: string, delayMs: number) {
const stepMs = Math.min(delayMs, MAX_TIMEOUT_MS);
this.expirationTimers.set(actualKey, setTimeout(() => {
if (delayMs > stepMs) {
this.expireAfter(actualKey, delayMs - stepMs);
return;
}
this.data.delete(actualKey);
this.expirationTimers.delete(actualKey);
}, stepMs));
}
private clearExpiration(actualKey: string) {
clearTimeout(this.expirationTimers.get(actualKey));
this.expirationTimers.delete(actualKey);
}
async listByPrefix(prefix: string, limit?: number, collection?: string): Promise<Record<string, string>[]> {
const actualPrefix = this.getActualKey(prefix, collection);
const matchedKeys = Array.from(this.data.keys())
.filter((key) => key.startsWith(actualPrefix))
.sort();
const limitedKeys = typeof limit === 'number' ? matchedKeys.slice(0, limit) : matchedKeys;
const keyValuePairs: Record<string, string>[] = limitedKeys.map((key) => ({ [key]: this.data.get(key) }));
const keyValuePairsFiltered = keyValuePairs.filter((pair) => {
const key = Object.keys(pair)[0];
if (collection) {
return key.startsWith(`${collection}:`);
}
return true;
});
keyValuePairsFiltered.forEach((pair) => {
const key = Object.keys(pair)[0];
if (collection) {
const keyWithoutCollection = key.replace(`${collection}:`, '');
pair[keyWithoutCollection] = pair[key];
delete pair[key];
}
});
const result = keyValuePairsFiltered.filter((pair) => {
const key = Object.keys(pair)[0];
return key.startsWith(`${prefix}`);
});
return result;
}
}