| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- "use strict";
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const fs = require("fs");
- class Storage {
- constructor(id) {
- try {
- fs.mkdirSync("data");
- }
- catch (e) {
- // ignore
- }
- const safeId = id.replace(/[^A-Za-z0-9]/g, "");
- if (safeId === '') {
- throw new Error("must have non-empty id");
- }
- this.prefix = safeId;
- }
- getValues() {
- return __awaiter(this, void 0, void 0, function* () {
- return Object.values(this.getMap());
- });
- }
- get(key) {
- return __awaiter(this, void 0, void 0, function* () {
- return this.getMap()[key];
- });
- }
- put(key, value) {
- return __awaiter(this, void 0, void 0, function* () {
- const filename = "data/storage_" + this.prefix;
- let map;
- try {
- const contents = fs.readFileSync(filename, { encoding: 'utf-8' });
- map = JSON.parse(contents);
- }
- catch (e) {
- map = {};
- }
- map[key] = value;
- fs.writeFileSync(filename, JSON.stringify(map));
- });
- }
- set(key, value) {
- return __awaiter(this, void 0, void 0, function* () {
- return yield this.put(key, value);
- });
- }
- getMap() {
- try {
- const filename = "data/storage_" + this.prefix;
- const contents = fs.readFileSync(filename, { encoding: 'utf-8' });
- return JSON.parse(contents);
- }
- catch (e) {
- return {};
- }
- }
- }
- exports.Storage = Storage;
- //# sourceMappingURL=storage.js.map
|