Files
codeql-action/node_modules/eslint-module-utils/ModuleCache.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-07-27 16:54:26 +00:00
'use strict';
2023-08-01 03:35:02 -07:00
2021-07-27 16:54:26 +00:00
exports.__esModule = true;
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
const log = require('debug')('eslint-module-utils:ModuleCache');
2020-09-14 10:42:37 +01:00
2024-08-26 17:13:37 +00:00
/** @type {import('./ModuleCache').ModuleCache} */
2020-09-14 10:42:37 +01:00
class ModuleCache {
2024-08-26 17:13:37 +00:00
/** @param {typeof import('./ModuleCache').ModuleCache.prototype.map} map */
2020-09-14 10:42:37 +01:00
constructor(map) {
2024-08-26 17:13:37 +00:00
this.map = map || /** @type {{typeof import('./ModuleCache').ModuleCache.prototype.map} */ new Map();
2020-09-14 10:42:37 +01:00
}
2024-08-26 17:13:37 +00:00
/** @type {typeof import('./ModuleCache').ModuleCache.prototype.set} */
2020-09-14 10:42:37 +01:00
set(cacheKey, result) {
2021-07-27 16:54:26 +00:00
this.map.set(cacheKey, { result, lastSeen: process.hrtime() });
log('setting entry for', cacheKey);
return result;
2020-09-14 10:42:37 +01:00
}
2024-08-26 17:13:37 +00:00
/** @type {typeof import('./ModuleCache').ModuleCache.prototype.get} */
2020-09-14 10:42:37 +01:00
get(cacheKey, settings) {
if (this.map.has(cacheKey)) {
2021-07-27 16:54:26 +00:00
const f = this.map.get(cacheKey);
2020-09-14 10:42:37 +01:00
// check freshness
2024-08-26 17:13:37 +00:00
// @ts-expect-error TS can't narrow properly from `has` and `get`
2023-08-01 03:35:02 -07:00
if (process.hrtime(f.lastSeen)[0] < settings.lifetime) { return f.result; }
} else {
log('cache miss for', cacheKey);
}
2020-09-14 10:42:37 +01:00
// cache miss
2021-07-27 16:54:26 +00:00
return undefined;
2020-09-14 10:42:37 +01:00
}
2024-08-26 17:13:37 +00:00
/** @type {typeof import('./ModuleCache').ModuleCache.getSettings} */
static getSettings(settings) {
/** @type {ReturnType<typeof ModuleCache.getSettings>} */
const cacheSettings = Object.assign({
lifetime: 30, // seconds
}, settings['import/cache']);
// parse infinity
// @ts-expect-error the lack of type overlap is because we're abusing `cacheSettings` as a temporary object
if (cacheSettings.lifetime === '∞' || cacheSettings.lifetime === 'Infinity') {
cacheSettings.lifetime = Infinity;
}
2020-09-14 10:42:37 +01:00
2024-08-26 17:13:37 +00:00
return cacheSettings;
2020-09-14 10:42:37 +01:00
}
2024-08-26 17:13:37 +00:00
}
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
exports.default = ModuleCache;