UNPKG

@salesforce/core

Version:

Core libraries to interact with SFDX projects, orgs, and APIs.

67 lines 2.41 kB
"use strict"; /* * Copyright 2026, Salesforce, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TTLConfig = void 0; const configFile_1 = require("./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) * }); * ``` */ class TTLConfig extends configFile_1.ConfigFile { set(key, value) { super.set(key, this.timestamp(value)); } getLatestEntry() { const entries = this.entries(); const sorted = entries.sort(([, valueA], [, valueB]) => new Date(valueB.timestamp).getTime() - new Date(valueA.timestamp).getTime()); return sorted.length > 0 ? sorted[0] : null; } getLatestKey() { const [key] = this.getLatestEntry() ?? [null]; return key; } isExpired(dateTime, value) { return dateTime - new Date(value.timestamp).getTime() > this.options.ttl.milliseconds; } async init() { // Normally, this is done in super.init() but we don't call it to prevent // redundant read() calls. if (this.hasEncryption()) { await this.initCrypto(); } 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 timestamp(value) { return { ...value, timestamp: new Date().toISOString() }; } } exports.TTLConfig = TTLConfig; //# sourceMappingURL=ttlConfig.js.map