@trapi/metadata
Version:
Generate REST-API metadata scheme from TypeScript Decorators.
98 lines • 3.58 kB
JavaScript
;
/*
* Copyright (c) 2021.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheClient = void 0;
const locter_1 = require("locter");
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const utils_1 = require("./utils");
class CacheClient {
constructor(input) {
this.options = (0, utils_1.buildCacheOptions)(input);
}
// -------------------------------------------------------------------------
async save(data) {
if (!this.options.enabled) {
return undefined;
}
const filePath = this.buildFilePath(undefined, data.sourceFilesSize);
await node_fs_1.default.promises.writeFile(filePath, this.serialize(data));
return filePath;
}
async get(sourceFilesSize) {
if (!this.options.enabled) {
return undefined;
}
await this.clear();
const filePath = this.buildFilePath(undefined, sourceFilesSize);
try {
const content = await node_fs_1.default.promises.readFile(filePath, { encoding: 'utf-8' });
// todo: maybe add shape validation here :)
const cache = JSON.parse(content);
if (!cache || cache.sourceFilesSize !== sourceFilesSize) {
return undefined;
}
return cache;
}
catch (e) {
/* istanbul ignore next */
return undefined;
}
}
// -------------------------------------------------------------------------
/**
* At a 10% chance, clear all cache files :)
*/
/* istanbul ignore next */
async clear() {
if (!this.options.enabled || !this.options.clearAtRandom) {
return;
}
const rand = Math.floor(Math.random() * 100) + 1;
if (rand > 10) {
return;
}
const files = await (0, locter_1.locateMany)(this.buildFileName('**'), {
path: this.options.directoryPath,
});
const unlinkPromises = [];
for (let i = 0; i < files.length; i++) {
unlinkPromises.push(node_fs_1.default.promises.unlink((0, locter_1.buildFilePath)(files[i])));
}
await Promise.all(unlinkPromises);
}
// -------------------------------------------------------------------------
buildFilePath(hash, sourceFilesSize) {
return node_path_1.default.join(this.options.directoryPath, this.buildFileName(hash, sourceFilesSize));
}
buildFileName(hash, sourceFilesSize) {
if (typeof this.options.fileName === 'string') {
return this.options.fileName;
}
return `.swagger-${hash ?? (0, utils_1.generateFileHash)(sourceFilesSize)}.json`;
}
serialize(input) {
let cache = [];
const str = JSON.stringify(input, (key, value) => {
if ((0, locter_1.isObject)(value) || Array.isArray(value)) {
if (cache.indexOf(value) !== -1) {
return undefined;
}
cache.push(value);
}
return value;
});
cache = undefined;
return str;
}
}
exports.CacheClient = CacheClient;
//# sourceMappingURL=client.js.map