UNPKG

silvie

Version:

Typescript Back-end Framework

97 lines (91 loc) 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class BlackList { constructor(filename) { _defineProperty(this, "filename", void 0); _defineProperty(this, "blackList", void 0); this.filename = _path.default.resolve(process.rootPath, `.silvie/${filename}`); this.blackList = this.readBlacklist(); } /** * Read blacklist from the file * @private */ readBlacklist() { try { if (_fs.default.existsSync(this.filename)) { const content = _fs.default.readFileSync(this.filename, { encoding: 'utf-8' }); return JSON.parse(content); } _fs.default.writeFileSync(this.filename, '[]'); return []; } catch (_unused) { return []; } } /** * Write current blacklist array into the file * @private */ writeBlacklist() { try { _fs.default.writeFileSync(this.filename, JSON.stringify(this.blackList)); return true; } catch (_unused2) { return false; } } /** * Add a token to the blacklist * @param token */ add(token) { try { this.blackList.push(token); this.writeBlacklist(); return true; } catch (_unused3) { return false; } } /** * Check if a token is blacklisted or not * @param token */ has(token) { try { return this.blackList.includes(token); } catch (_unused4) { return false; } } /** * Remove a token from the blacklist * @param token */ remove(token) { try { const index = this.blackList.indexOf(token); if (index >= 0) { this.blackList.splice(index, 1); this.writeBlacklist(); return true; } return false; } catch (_unused5) { return false; } } } exports.default = BlackList;