UNPKG

rxdb-server

Version:
172 lines (170 loc) 6.9 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); var _exportNames = { RxServerReplicationState: true, replicateServer: true }; exports.RxServerReplicationState = void 0; exports.replicateServer = replicateServer; var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _core = require("rxdb/plugins/core"); var _leaderElection = require("rxdb/plugins/leader-election"); var _replication = require("rxdb/plugins/replication"); var _rxjs = require("rxjs"); var _helpers = require("./helpers.js"); var _eventsource = require("eventsource"); var _utils = require("../../utils.js"); var _types = require("./types.js"); Object.keys(_types).forEach(function (key) { if (key === "default" || key === "__esModule") return; if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; if (key in exports && exports[key] === _types[key]) return; Object.defineProperty(exports, key, { enumerable: true, get: function () { return _types[key]; } }); }); var RxServerReplicationState = exports.RxServerReplicationState = /*#__PURE__*/function (_RxReplicationState) { function RxServerReplicationState(replicationIdentifier, collection, pull, push, live = true, retryTime = 1000 * 5, autoStart = true, headers = {}) { var _this; _this = _RxReplicationState.call(this, replicationIdentifier, collection, '_deleted', pull, push, live, retryTime, autoStart) || this; _this.outdatedClient$ = new _rxjs.Subject(); _this.unauthorized$ = new _rxjs.Subject(); _this.forbidden$ = new _rxjs.Subject(); _this.replicationIdentifier = replicationIdentifier; _this.collection = collection; _this.pull = pull; _this.push = push; _this.live = live; _this.retryTime = retryTime; _this.autoStart = autoStart; _this.headers = headers; _this.onCancel.push(() => { _this.outdatedClient$.complete(); _this.unauthorized$.complete(); _this.forbidden$.complete(); }); return _this; } (0, _inheritsLoose2.default)(RxServerReplicationState, _RxReplicationState); var _proto = RxServerReplicationState.prototype; _proto.setHeaders = function setHeaders(headers) { this.headers = (0, _core.flatClone)(headers); }; return RxServerReplicationState; }(_replication.RxReplicationState); function replicateServer(options) { if (!options.pull && !options.push) { throw (0, _core.newRxError)('UT3', { collection: options.collection.name, args: { replicationIdentifier: options.replicationIdentifier } }); } options.live = typeof options.live === 'undefined' ? true : options.live; options.waitForLeadership = typeof options.waitForLeadership === 'undefined' ? true : options.waitForLeadership; var collection = options.collection; (0, _core.addRxPlugin)(_leaderElection.RxDBLeaderElectionPlugin); var pullStream$ = new _rxjs.Subject(); var replicationPrimitivesPull; if (options.pull) { replicationPrimitivesPull = { async handler(checkpointOrNull, batchSize) { var lwt = checkpointOrNull && checkpointOrNull.lwt ? checkpointOrNull.lwt : 0; var id = checkpointOrNull && checkpointOrNull.id ? checkpointOrNull.id : ''; var url = options.url + ("/pull?lwt=" + lwt + "&id=" + id + "&limit=" + batchSize); var response = await fetch(url, { method: 'GET', credentials: 'include', headers: Object.assign({ 'Accept': 'application/json', 'Content-Type': 'application/json' }, replicationState.headers) }); var data = await (0, _helpers.parseResponse)(replicationState, response); return { documents: data.documents, checkpoint: data.checkpoint }; }, batchSize: (0, _core.ensureNotFalsy)(options.pull).batchSize, modifier: (0, _core.ensureNotFalsy)(options.pull).modifier, stream$: pullStream$.asObservable() }; } var replicationPrimitivesPush; if (options.push) { replicationPrimitivesPush = { async handler(changeRows) { var response = await fetch(options.url + '/push', { method: 'POST', credentials: 'include', headers: Object.assign({ 'Accept': 'application/json', 'Content-Type': 'application/json' }, replicationState.headers), body: JSON.stringify(changeRows) }); var conflictsArray = await (0, _helpers.parseResponse)(replicationState, response); return conflictsArray; }, batchSize: options.push.batchSize, modifier: options.push.modifier }; } var replicationState = new RxServerReplicationState(options.replicationIdentifier, collection, replicationPrimitivesPull, replicationPrimitivesPush, options.live, options.retryTime, options.autoStart, options.headers); /** * Use long polling to get live changes for the pull.stream$ */ if (options.live && options.pull) { var startBefore = replicationState.start.bind(replicationState); replicationState.start = async () => { var useEventSource = options.eventSource ? options.eventSource : _eventsource.EventSource; var eventSource; var refreshEventSource = () => { eventSource = new useEventSource(options.url + '/pullStream', { withCredentials: true, /** * Sending headers is not supported by the Browser EventSource API, * only by the npm module we use. In react-native you might have * to set another EventSource implementation. * @link https://www.npmjs.com/package/eventsource */ fetch: (0, _utils.customFetchWithFixedHeaders)(replicationState.headers) }); // TODO check for 426 errors and handle them eventSource.onerror = err => { if (err.code === 401) { replicationState.unauthorized$.next(); eventSource.close(); (0, _core.promiseWait)(replicationState.retryTime).then(() => refreshEventSource()); } else { pullStream$.next('RESYNC'); } }; eventSource.onopen = x => { pullStream$.next('RESYNC'); }; eventSource.onmessage = event => { var eventData = JSON.parse(event.data); pullStream$.next({ documents: eventData.documents, checkpoint: eventData.checkpoint }); }; }; refreshEventSource(); replicationState.onCancel.push(() => eventSource && eventSource.close()); return startBefore(); }; } (0, _replication.startReplicationOnLeaderShip)(options.waitForLeadership, replicationState); return replicationState; } //# sourceMappingURL=index.js.map