/
ttlConfig.ts
66 lines (57 loc) · 2.27 KB
/
ttlConfig.ts
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
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Duration } from '@salesforce/kit';
import { JsonMap, Nullable } from '@salesforce/ts-types';
import { ConfigFile } from './configFile';
/**
* A Time To Live configuration file where each entry is timestamped and removed once the TTL has expired.
*
* @example
* ```
* import { Duration } from '@salesforce/kit';
* const config = await TTLConfig.create({
* isGlobal: false,
* ttl: Duration.days(1)
* });
* ```
*/
export class TTLConfig<T extends TTLConfig.Options, P extends JsonMap> extends ConfigFile<T, TTLConfig.Contents<P>> {
public set(key: string, value: Partial<TTLConfig.Entry<P>>): void {
super.set(key, this.timestamp(value));
}
public getLatestEntry(): Nullable<[string, TTLConfig.Entry<P>]> {
const entries = this.entries() as Array<[string, TTLConfig.Entry<P>]>;
const sorted = entries.sort(
([, valueA], [, valueB]) => new Date(valueB.timestamp).getTime() - new Date(valueA.timestamp).getTime()
);
return sorted.length > 0 ? sorted[0] : null;
}
public getLatestKey(): Nullable<string> {
const [key] = this.getLatestEntry() ?? [null];
return key;
}
public isExpired(dateTime: number, value: P & { timestamp: string }): boolean {
return dateTime - new Date(value.timestamp).getTime() > this.options.ttl.milliseconds;
}
protected async init(): Promise<void> {
const contents = await this.read(this.options.throwOnNotFound);
const date = new Date().getTime();
// delete all the expired entries
Object.entries(contents)
.filter(([, value]) => this.isExpired(date, value))
.map(([key]) => this.unset(key));
}
// eslint-disable-next-line class-methods-use-this
private timestamp(value: Partial<TTLConfig.Entry<P>>): TTLConfig.Entry<P> {
return { ...value, timestamp: new Date().toISOString() } as TTLConfig.Entry<P>;
}
}
export namespace TTLConfig {
export type Options = ConfigFile.Options & { ttl: Duration };
export type Entry<T extends JsonMap> = T & { timestamp: string };
export type Contents<T extends JsonMap> = Record<string, Entry<T>>;
}