"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 plugins = require("./smartuniverse.plugins"); const smartexpress_1 = require("smartexpress"); const smartuniverse_classes_manager_1 = require("./smartuniverse.classes.manager"); const smartuniverse_classes_universestore_1 = require("./smartuniverse.classes.universestore"); const paths = require("./smartuniverse.paths"); class Universe { get universeVersion() { if (this.universeVersionStore) { return this.universeVersionStore; } else { const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson); this.universeVersionStore = packageJson.version; return this.universeVersionStore; } } constructor(optionsArg) { this.options = optionsArg; this.universeStore = new smartuniverse_classes_universestore_1.UniverseStore(this.options.messageExpiryInMilliseconds); this.universeManager = new smartuniverse_classes_manager_1.UniverseManager(); } /** * initiates a server */ initServer(portArg) { return __awaiter(this, void 0, void 0, function* () { this.smartexpressServer = new plugins.smartexpress.Server({ cors: true, defaultAnswer: `smartuniverse server ${this.universeVersion}`, forceSsl: false, port: portArg }); // route handling const addMessageHandler = new smartexpress_1.Handler('PUT', request => { const requestBody = request.body; this.universeStore.addMessage(requestBody.message, requestBody.payload); return true; }); const readMessageHandler = new smartexpress_1.Handler('GET', request => { const requestBody = request.body; this.universeStore.readMessagesYoungerThan(requestBody.since); }); const messageRoute = new smartexpress_1.Route(this.smartexpressServer, 'message'); messageRoute.addHandler(addMessageHandler); messageRoute.addHandler(readMessageHandler); yield this.smartexpressServer.start(); }); } stopServer() { return __awaiter(this, void 0, void 0, function* () { yield this.smartexpressServer.stop(); }); } } exports.Universe = Universe; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWx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