UNPKG

wechaty-puppet-official-account

Version:
77 lines 3.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PayloadStore = void 0; const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); const fs_1 = __importDefault(require("fs")); const wechaty_puppet_1 = require("wechaty-puppet"); const flash_store_1 = require("flash-store"); const lru_cache_1 = __importDefault(require("lru-cache")); const config_js_1 = require("../config.js"); const semver_1 = __importDefault(require("semver")); const { major, minor } = semver_1.default; class PayloadStore { appId; cacheOAContactPayload; cacheOAMessagePayload; constructor(appId) { this.appId = appId; wechaty_puppet_1.log.verbose('PayloadStore', 'constructor(%s)', appId); } async start() { wechaty_puppet_1.log.verbose('PayloadStore', 'start()'); if (this.cacheOAMessagePayload) { throw new Error('PayloadStore should be stop() before start() again.'); } /** * FlashStore */ const baseDir = path_1.default.join(os_1.default.homedir(), '.wechaty', 'wechaty-puppet-official-account', `v${major(config_js_1.VERSION)}.${minor(config_js_1.VERSION)}`, this.appId); if (!fs_1.default.existsSync(baseDir)) { fs_1.default.mkdirSync(baseDir, { recursive: true }); } this.cacheOAContactPayload = new flash_store_1.FlashStore(path_1.default.join(baseDir, 'oa-contact-raw-payload')); /** * LRU */ const lruOptions = { dispose(key, val) { wechaty_puppet_1.log.silly('PayloadStore', `constructor() lruOptions.dispose(${key}, ${JSON.stringify(val)})`); }, max: 1000, maxAge: 1000 * 60 * 60, }; this.cacheOAMessagePayload = new lru_cache_1.default(lruOptions); } async stop() { wechaty_puppet_1.log.verbose('PayloadStore', 'stop()'); if (this.cacheOAMessagePayload) { this.cacheOAMessagePayload = undefined; } if (this.cacheOAContactPayload) { await this.cacheOAContactPayload.close(); this.cacheOAContactPayload = undefined; } } async getMessagePayload(id) { wechaty_puppet_1.log.verbose('PayloadStore', 'getMessagePayload(%s)', id); return this.cacheOAMessagePayload?.get(id); } async setMessagePayload(id, payload) { wechaty_puppet_1.log.verbose('PayloadStore', 'setMessagePayload(%s, %s)', id, JSON.stringify(payload)); await this.cacheOAMessagePayload?.set(id, payload); } async getContactPayload(id) { wechaty_puppet_1.log.verbose('PayloadStore', 'getContactPayload(%s)', id); return this.cacheOAContactPayload?.get(id); } async setContactPayload(id, payload) { wechaty_puppet_1.log.verbose('PayloadStore', 'setContactPayload(%s, %s)', id, JSON.stringify(payload)); await this.cacheOAContactPayload?.set(id, payload); } } exports.PayloadStore = PayloadStore; //# sourceMappingURL=payload-store.js.map