66 lines
5.3 KiB
JavaScript
66 lines
5.3 KiB
JavaScript
"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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnR1bml2ZXJzZS5jbGFzc2VzLnNtYXJ0dW5pdmVyc2UuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHVuaXZlcnNlLmNsYXNzZXMuc21hcnR1bml2ZXJzZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsbURBQW1EO0FBRW5ELCtDQUFzRDtBQUV0RCxtRkFBa0U7QUFFbEUsK0ZBQXNFO0FBQ3RFLCtDQUErQztBQWUvQztJQVVFLElBQVksZUFBZTtRQUN6QixFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsb0JBQW9CLENBQUMsQ0FBQyxDQUFDO1lBQzlCLE1BQU0sQ0FBQyxJQUFJLENBQUMsb0JBQW9CLENBQUM7UUFDbkMsQ0FBQztRQUFDLElBQUksQ0FBQyxDQUFDO1lBQ04sTUFBTSxXQUFXLEdBQUcsT0FBTyxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsWUFBWSxDQUFDLEtBQUssQ0FBQyxXQUFXLENBQUMsQ0FBQztZQUN6RSxJQUFJLENBQUMsb0JBQW9CLEdBQUcsV0FBVyxDQUFDLE9BQU8sQ0FBQztZQUNoRCxNQUFNLENBQUMsSUFBSSxDQUFDLG9CQUFvQixDQU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