UNPKG

realm-object-server

Version:

Realm Object Server

185 lines 8.85 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 __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 path = require("path"); const fs = require("fs-extra"); const realm_sync_server_1 = require("realm-sync-server"); const decorators_1 = require("../decorators"); const Server_1 = require("../Server"); const stats_1 = require("../stats"); const Logger_1 = require("../shared/Logger"); const ProcessUtil_1 = require("../shared/ProcessUtil"); function createSyncServerConfig({ syncServiceConfig, dataPath, statsEndpoint, realmsEncryptionKey, logger, }) { const config = { dataPath, publicKeyPath: syncServiceConfig.publicKeyPath, listenAddress: syncServiceConfig.listenAddress, listenPort: syncServiceConfig.listenPort, featureToken: syncServiceConfig.featureToken || process.env.SYNC_WORKER_FEATURE_TOKEN, logLevel: syncServiceConfig.logLevel, logCallback: (levelNo, message, timestampMicroseconds) => { const ms = Math.floor(timestampMicroseconds / 1000); const epoch = Math.floor(ms / 1000); const nano = (timestampMicroseconds - (epoch * 1000 * 1000)) * 1000; logger.log(Logger_1.SyncLogLevel[levelNo], message, {}, [epoch, nano]); }, errorCallback: (name, message) => { logger.fatal(`Sync worker crashed with unhandled exception ${name}. Exiting.`, { message }); ProcessUtil_1.ProcessUtil.terminate({ success: false }); }, enableDownloadLogCompaction: syncServiceConfig.enableDownloadLogCompaction, maxDownloadSize: syncServiceConfig.maxDownloadSize, enableLogCompaction: syncServiceConfig.enableLogCompaction, historyCompactionInterval: syncServiceConfig.historyCompactionInterval, historyTtl: syncServiceConfig.historyTtl, enableRealmStateSizeReporting: syncServiceConfig.enableRealmStateSizeReporting, realmsEncryptionKey, maxOpenFiles: syncServiceConfig.maxFilesInCache, maxUploadBacklog: syncServiceConfig.maxUploadBacklog, enableDownloadBootstrapCache: syncServiceConfig.enableDownloadBootstrapCache, disablePartialSyncCompleter: syncServiceConfig.disablePartialSyncCompleter, numAuxPsyncThreads: syncServiceConfig.numAuxPsyncThreads, disableStateRealms: syncServiceConfig.disableStateRealms, historyCompactionIgnoreClients: syncServiceConfig.historyCompactionIgnoreClients, }; if (statsEndpoint) { config.statsEndpoint = statsEndpoint; config.metricsPrefix = "realm.localhost"; } return config; } exports.createSyncServerConfig = createSyncServerConfig; let SyncService = class SyncService { constructor(config = {}) { this.config = config; this.logger = new Logger_1.MuteLogger(); this.config = config; if (!this.config.label) { this.config.label = "default"; } this.tags = ["role=master"]; this.tags.push(`label=${config.label}`); } setLogger(l) { this.logger = l; } start(server) { return __awaiter(this, void 0, void 0, function* () { let dataPath = path.join(server.dataPath, "sync"); if (this.config.dataPath) { dataPath = this.config.dataPath; this.logger.info("Configuring SyncService with a custom dataPath is deprecated and will be removed in a future version and the value will default to 'sync', relative to the server's dataPath."); } yield fs.mkdirp(dataPath); if (!this.config.publicKeyPath) { this.config.publicKeyPath = server.publicKeyPath; } let statsEndpoint; if (this.stats) { const statsLogger = this.logger.withContext({ service: "sync/stats" }); const statsdReceiver = new stats_1.StatsdReceiver({ logger: statsLogger }); this.statsdToStatsSink = new stats_1.StatsdToStatsSink({ statsSink: this.stats, logger: this.logger }, statsdReceiver); this.statsdToMetricsRealm = new stats_1.StatsdToMetricsRealm({ logger: this.logger, openRealm: server.openRealm.bind(server), }, statsdReceiver); this.statsdSocket = yield statsdReceiver.start(); const { address, port } = this.statsdSocket.address(); statsEndpoint = `${address}:${port}`; statsLogger.debug(`Started listening for stats on ${statsEndpoint}`); } const config = createSyncServerConfig({ syncServiceConfig: this.config, dataPath, statsEndpoint, logger: this.logger, realmsEncryptionKey: server["serverConfig"].realmsEncryptionKey, }); if (server["serverConfig"].httpsForInternalComponents) { config.ssl = true; config.sslCertificatePath = server["serverConfig"].httpsCertChainPath; config.sslCertificateKeyPath = server["serverConfig"].httpsKeyPath; let validate = server["serverConfig"].httpsAgentForInternalComponents.options.rejectUnauthorized; if (process.env.NODE_TLS_REJECT_UNAUTHORIZED === "0") { validate = false; } config.masterVerifySslCertificate = validate; } this.syncServer = new realm_sync_server_1.RealmSyncServer(config); yield this.syncServer.start(); }); } stop() { return __awaiter(this, void 0, void 0, function* () { if (this.syncServer) { yield this.syncServer.stop(); } if (this.statsdSocket) { yield new Promise((resolve) => this.statsdSocket.close(resolve)).catch(); delete this.statsdSocket; } if (this.statsdToStatsSink) { this.statsdToStatsSink.stop(); delete this.statsdToStatsSink; } if (this.statsdToMetricsRealm) { this.statsdToMetricsRealm.stop(); delete this.statsdToMetricsRealm; } }); } address() { if (this.syncServer) { return this.syncServer.address(); } } }; __decorate([ decorators_1.Unmute(), __metadata("design:type", Function), __metadata("design:paramtypes", [Logger_1.Logger]), __metadata("design:returntype", void 0) ], SyncService.prototype, "setLogger", null); __decorate([ decorators_1.Start(), __metadata("design:type", Function), __metadata("design:paramtypes", [Server_1.Server]), __metadata("design:returntype", Promise) ], SyncService.prototype, "start", null); __decorate([ decorators_1.Stop(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], SyncService.prototype, "stop", null); __decorate([ decorators_1.Address(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SyncService.prototype, "address", null); SyncService = __decorate([ decorators_1.ServiceName("sync"), __metadata("design:paramtypes", [Object]) ], SyncService); exports.SyncService = SyncService; //# sourceMappingURL=SyncService.js.map