UNPKG

rxdb-server

Version:
54 lines (53 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RxServer = void 0; var _endpointReplication = require("./endpoint-replication.js"); var _endpointRest = require("./endpoint-rest.js"); var RxServer = exports.RxServer = /*#__PURE__*/function () { function RxServer(options, authHandler, serverApp, cors = '*') { this.endpoints = []; this.closeFn = (() => this.close()).bind(this); this.options = options; this.authHandler = authHandler; this.serverApp = serverApp; this.cors = cors; this.database = options.database; this.adapter = options.adapter; options.database.onClose.push(this.closeFn); } var _proto = RxServer.prototype; _proto.ensureNotStarted = function ensureNotStarted() { if (this.listenPromise) { throw new Error('This operation cannot be run after the RxServer has been started already'); } }; _proto.addReplicationEndpoint = function addReplicationEndpoint(opts) { this.ensureNotStarted(); var endpoint = new _endpointReplication.RxServerReplicationEndpoint(this, opts.name, opts.collection, opts.queryModifier ? opts.queryModifier : (_a, q) => q, opts.changeValidator ? opts.changeValidator : () => true, opts.serverOnlyFields ? opts.serverOnlyFields : [], opts.cors); this.endpoints.push(endpoint); return endpoint; }; _proto.addRestEndpoint = function addRestEndpoint(opts) { this.ensureNotStarted(); var endpoint = new _endpointRest.RxServerRestEndpoint(this, opts.name, opts.collection, opts.queryModifier ? opts.queryModifier : (_a, q) => q, opts.changeValidator ? opts.changeValidator : () => true, opts.serverOnlyFields ? opts.serverOnlyFields : [], opts.cors); this.endpoints.push(endpoint); return endpoint; }; _proto.start = async function start() { this.ensureNotStarted(); var hostname = this.options.hostname ? this.options.hostname : 'localhost'; this.listenPromise = this.options.adapter.listen(this.serverApp, this.options.port, hostname); return this.listenPromise; }; _proto.close = async function close() { this.database.onClose = this.database.onClose.filter(fn => fn !== this.closeFn); if (this.listenPromise) { await this.listenPromise; await this.options.adapter.close(this.serverApp); } }; return RxServer; }(); //# sourceMappingURL=rx-server.js.map