UNPKG

@herbertgao/surgio

Version:

Generating rules for Surge, Clash, Quantumult like a PRO

102 lines 3.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createTmpFactory = exports.TmpRedis = exports.TmpFile = void 0; const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const logger_1 = require("@surgio/logger"); const fs_extra_1 = __importDefault(require("fs-extra")); const constant_1 = require("../constant"); const redis_1 = __importDefault(require("../redis")); const logger = (0, logger_1.createLogger)({ service: 'surgio:utils:tmp-helper' }); const tmpDir = path_1.default.join(os_1.default.tmpdir(), constant_1.TMP_FOLDER_NAME); class TmpHelper { cacheKey; maxAge; constructor(cacheKey, maxAge) { this.cacheKey = cacheKey; this.maxAge = maxAge; } } class TmpFile { cacheKey; maxAge; constructor(cacheKey, maxAge) { this.cacheKey = cacheKey; this.maxAge = maxAge; fs_extra_1.default.accessSync(path_1.default.dirname(this.cacheKey), fs_extra_1.default.constants.W_OK); } async setContent(content) { await fs_extra_1.default.writeJson(this.cacheKey, { content, maxAge: this.maxAge, lastEditTime: new Date().getTime(), }); } async getContent() { const tmpContent = await this.validateContent(); if (tmpContent) { return tmpContent.content; } return undefined; } async validateContent() { if (!fs_extra_1.default.existsSync(this.cacheKey)) { return undefined; } const tmpContent = await fs_extra_1.default.readJson(this.cacheKey); const now = Date.now(); if (!tmpContent.maxAge) { return tmpContent; } else if (this.maxAge && now - tmpContent.lastEditTime < this.maxAge) { return tmpContent; } else if (!this.maxAge && tmpContent.maxAge) { this.maxAge = tmpContent.maxAge; } return undefined; } } exports.TmpFile = TmpFile; class TmpRedis { cacheKey; maxAge; redisClient; constructor(cacheKey, maxAge) { this.cacheKey = cacheKey; this.maxAge = maxAge; this.redisClient = redis_1.default.getRedis(); } async getContent() { const value = await this.redisClient.get(this.cacheKey); return value ? value : undefined; } async setContent(content) { if (this.maxAge) { await this.redisClient.set(this.cacheKey, content, 'PX', this.maxAge); } else { await this.redisClient.set(this.cacheKey, content); } } } exports.TmpRedis = TmpRedis; const createTmpFactory = (baseDir, cacheType = 'default') => { logger.debug('tmpDir: %s', baseDir); logger.debug('tmpDir cache type: %s', cacheType); if (cacheType === 'default') { const fullTmpDir = path_1.default.join(tmpDir, baseDir); if (!fs_extra_1.default.existsSync(fullTmpDir)) { fs_extra_1.default.mkdirpSync(fullTmpDir); } return (fileCacheKey, maxAge) => new TmpFile(path_1.default.join(fullTmpDir, fileCacheKey), maxAge); } else { return (fileCacheKey, maxAge) => new TmpRedis(`${baseDir}:${fileCacheKey}`, maxAge); } }; exports.createTmpFactory = createTmpFactory; //# sourceMappingURL=tmp-helper.js.map