40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
export class S3ExpressIdentityCache {
|
|
data;
|
|
lastPurgeTime = Date.now();
|
|
static EXPIRED_CREDENTIAL_PURGE_INTERVAL_MS = 30000;
|
|
constructor(data = {}) {
|
|
this.data = data;
|
|
}
|
|
get(key) {
|
|
const entry = this.data[key];
|
|
if (!entry) {
|
|
return;
|
|
}
|
|
return entry;
|
|
}
|
|
set(key, entry) {
|
|
this.data[key] = entry;
|
|
return entry;
|
|
}
|
|
delete(key) {
|
|
delete this.data[key];
|
|
}
|
|
async purgeExpired() {
|
|
const now = Date.now();
|
|
if (this.lastPurgeTime + S3ExpressIdentityCache.EXPIRED_CREDENTIAL_PURGE_INTERVAL_MS > now) {
|
|
return;
|
|
}
|
|
for (const key in this.data) {
|
|
const entry = this.data[key];
|
|
if (!entry.isRefreshing) {
|
|
const credential = await entry.identity;
|
|
if (credential.expiration) {
|
|
if (credential.expiration.getTime() < now) {
|
|
delete this.data[key];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|