UNPKG

cs2-cdn

Version:

Retrieves the Steam CDN Image URLs for CS2/CS:GO Items

63 lines (62 loc) 3.15 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _Store_instances, _Store_save, _Store_readIn; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); class Store { constructor(path) { _Store_instances.add(this); if (!fs_1.default.existsSync(path)) { fs_1.default.writeFileSync(path, '{}'); } this.path = path; this.data = JSON.parse(fs_1.default.readFileSync(path).toString()); if (process.platform === 'win32' || process.platform === 'linux' || process.platform === 'darwin') { fs_1.default.watch(path, () => __awaiter(this, void 0, void 0, function* () { return this.data = yield __classPrivateFieldGet(this, _Store_instances, "m", _Store_readIn).call(this); })); } else { fs_1.default.watchFile(path, () => __awaiter(this, void 0, void 0, function* () { return this.data = yield __classPrivateFieldGet(this, _Store_instances, "m", _Store_readIn).call(this); })); } } getValue(key) { return this.data[key]; } setValue(key, value) { this.data[key] = value; __classPrivateFieldGet(this, _Store_instances, "m", _Store_save).call(this); } } _Store_instances = new WeakSet(), _Store_save = function _Store_save() { fs_1.default.writeFileSync(this.path, JSON.stringify(this.data)); }, _Store_readIn = function _Store_readIn() { return new Promise((resolve, reject) => { // ReadFileSync is too unstable in this situation fs_1.default.readFile(this.path, (err, data) => { if (err) { return console.log(err); } const file = data.toString(); if (!file) { return; } resolve(JSON.parse(file)); }); }); }; exports.default = Store;