UNPKG

realm-object-server

Version:

Realm Object Server

143 lines 6.74 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; 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 Server_1 = require("../Server"); const ConfigurationRealm_1 = require("../realms/ConfigurationRealm"); const util_1 = require("../shared/util"); const decorators_1 = require("../decorators"); const RealmProblem_1 = require("../errors/RealmProblem"); const deepmerge = require("deepmerge"); class ConfigurableServiceBase { constructor(staticConfig) { this.staticConfig = staticConfig; } start(server) { return __awaiter(this, void 0, void 0, function* () { this.server = server; this.configurationRealm = yield server.openRealm(ConfigurationRealm_1.ConfigurationRealm); const name = this.constructor.name; const runtimeConfigs = this.configurationRealm.objects("ServiceConfig").filtered("serviceName = $0", name); let runtimeConfig = runtimeConfigs[0]; if (!runtimeConfig || runtimeConfig.version < this.staticConfig.version) { yield util_1.writeAsync(this.configurationRealm, () => { runtimeConfig = this.configurationRealm.create("ServiceConfig", { serviceName: name, config: JSON.stringify({}), version: 0, }, true); }); } this.setConfig(runtimeConfig); yield this.startCore(server); this.hasStarted = true; runtimeConfigs.addListener((collection, changes) => { const containsChanges = changes.modifications.length || changes.insertions.length; const newConfig = collection[0]; if (containsChanges && newConfig) { this.server.logger.detail(`Update service ${newConfig.serviceName} due to config change. OldConfig: ${JSON.stringify(this.staticConfig)}. NewConfig: ${newConfig.config}`); try { this.setConfig(newConfig); } catch (err) { this.server.logger.error(`Failed to start service ${newConfig.serviceName} with config ${newConfig.config}. Error: ${err.stack}`); } } }); }); } stop() { return __awaiter(this, void 0, void 0, function* () { if (this.configurationRealm) { this.configurationRealm.close(); delete this.configurationRealm; } yield this.stopCore(); }); } updateServiceConfig(req, config) { return __awaiter(this, void 0, void 0, function* () { if (!this.server.tokenValidator.isAdminToken(req.authToken)) { throw new RealmProblem_1.AccessDenied(); } yield util_1.writeAsync(this.configurationRealm, () => { this.configurationRealm.create("ServiceConfig", { serviceName: this.constructor.name, config: config ? JSON.stringify(config) : null, }, true); }); return {}; }); } getServiceConfig(req) { return __awaiter(this, void 0, void 0, function* () { if (!this.server.tokenValidator.isAdminToken(req.authToken)) { throw new RealmProblem_1.AccessDenied(); } const runtimeConfig = this.configurationRealm.objectForPrimaryKey("ServiceConfig", this.constructor.name); if (runtimeConfig) { return JSON.parse(runtimeConfig.config); } return {}; }); } setConfig(config) { let mergedConfig; if (config) { const runtimeConfig = JSON.parse(config.config); mergedConfig = deepmerge(this.staticConfig, runtimeConfig); } else { mergedConfig = this.staticConfig; } this.setConfigCore(mergedConfig); } } __decorate([ decorators_1.Start(), __metadata("design:type", Function), __metadata("design:paramtypes", [Server_1.Server]), __metadata("design:returntype", Promise) ], ConfigurableServiceBase.prototype, "start", null); __decorate([ decorators_1.Stop(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], ConfigurableServiceBase.prototype, "stop", null); __decorate([ decorators_1.Put("/serviceConfig"), __param(0, decorators_1.Request()), __param(1, decorators_1.Body("config")), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], ConfigurableServiceBase.prototype, "updateServiceConfig", null); __decorate([ decorators_1.Get("/serviceConfig"), __param(0, decorators_1.Request()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], ConfigurableServiceBase.prototype, "getServiceConfig", null); exports.ConfigurableServiceBase = ConfigurableServiceBase; //# sourceMappingURL=ConfigurableServiceBase.js.map