cozy-pouch-link 60.15.2 → 60.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -96,10 +96,26 @@ var MAX_DEBOUNCE_DELAY = 600 * 1000;
96
96
  var addBasicAuth = function addBasicAuth(url, basicAuth) {
97
97
  return url.replace('//', "//".concat(basicAuth));
98
98
  };
99
+ /**
100
+ * Constructs the replication URL for a given doctype and replication options.
101
+ *
102
+ * @param {string} uri - The base URI of the Cozy instance.
103
+ * @param {Object} token - The authentication token object, must have a toBasicAuth() method.
104
+ * @param {string} doctype - The doctype for which to construct the replication URL.
105
+ * @param {Object} [replicationOptions] - Additional replication options.
106
+ * @param {string} [replicationOptions.driveId] - If present, indicates replication is for a shared drive and which one.
107
+ * @returns {string} The fully constructed replication URL.
108
+ */
99
109
 
100
- var _getReplicationURL = function getReplicationURL(uri, token, doctype) {
110
+
111
+ var _getReplicationURL = function getReplicationURL(uri, token, doctype, replicationOptions) {
101
112
  var basicAuth = token.toBasicAuth();
102
113
  var authenticatedURL = addBasicAuth(uri, basicAuth);
114
+
115
+ if (replicationOptions !== null && replicationOptions !== void 0 && replicationOptions.driveId) {
116
+ return "".concat(authenticatedURL, "/sharings/drives/").concat(replicationOptions === null || replicationOptions === void 0 ? void 0 : replicationOptions.driveId);
117
+ }
118
+
103
119
  return "".concat(authenticatedURL, "/data/").concat(doctype);
104
120
  };
105
121
 
@@ -196,7 +212,16 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
196
212
 
197
213
  (0, _createClass2.default)(PouchLink, [{
198
214
  key: "getReplicationURL",
199
- value: function getReplicationURL(doctype) {
215
+
216
+ /**
217
+ * Get the authenticated replication URL for a specific doctype
218
+ *
219
+ * @param {string} doctype - The document type to replicate (e.g., 'io.cozy.files')
220
+ * @param {object} [replicationOptions={}] - Replication options
221
+ * @param {string} [replicationOptions.driveId] - The ID of the shared drive to replicate (for shared drives)
222
+ * @returns {string} The authenticated replication URL
223
+ */
224
+ value: function getReplicationURL(doctype, replicationOptions) {
200
225
  var url = this.client && this.client.stackClient.uri;
201
226
  var token = this.client && this.client.stackClient.token;
202
227
 
@@ -208,7 +233,7 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
208
233
  throw new Error("Can't get replication URL since the stack client doesn't have a URI");
209
234
  }
210
235
 
211
- return _getReplicationURL(url, token, doctype);
236
+ return _getReplicationURL(url, token, doctype, replicationOptions);
212
237
  }
213
238
  }, {
214
239
  key: "registerClient",
@@ -1656,6 +1681,101 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1656
1681
 
1657
1682
  return syncImmediately;
1658
1683
  }()
1684
+ /**
1685
+ * Adds a new doctype to the list of managed doctypes, sets its replication options,
1686
+ * adds it to the pouches, and starts replication.
1687
+ *
1688
+ * @param {string} doctype - The name of the doctype to add.
1689
+ * @param {Object} replicationOptions - The replication options for the doctype.
1690
+ * @param {Object} options - The replication options for the doctype.
1691
+ * @param {boolean} [options.shouldStartReplication=true] - Whether the replication should be started.
1692
+ */
1693
+
1694
+ }, {
1695
+ key: "addDoctype",
1696
+ value: function () {
1697
+ var _addDoctype = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee23(doctype, replicationOptions, options) {
1698
+ return _regenerator.default.wrap(function _callee23$(_context23) {
1699
+ while (1) {
1700
+ switch (_context23.prev = _context23.next) {
1701
+ case 0:
1702
+ this.doctypes.push(doctype);
1703
+
1704
+ if (!this.doctypesReplicationOptions) {
1705
+ this.doctypesReplicationOptions = {};
1706
+ }
1707
+
1708
+ this.doctypesReplicationOptions[doctype] = replicationOptions;
1709
+ this.pouches.doctypes.push(doctype);
1710
+ _context23.next = 6;
1711
+ return this.pouches.addDoctype(doctype, replicationOptions);
1712
+
1713
+ case 6:
1714
+ if ((options === null || options === void 0 ? void 0 : options.shouldStartReplication) === true) {
1715
+ this.startReplicationWithDebounce();
1716
+ }
1717
+
1718
+ case 7:
1719
+ case "end":
1720
+ return _context23.stop();
1721
+ }
1722
+ }
1723
+ }, _callee23, this);
1724
+ }));
1725
+
1726
+ function addDoctype(_x26, _x27, _x28) {
1727
+ return _addDoctype.apply(this, arguments);
1728
+ }
1729
+
1730
+ return addDoctype;
1731
+ }()
1732
+ /**
1733
+ * Removes a doctype from the list of managed doctypes, deletes its replication options,
1734
+ * and removes it from the pouches.
1735
+ *
1736
+ * @param {string} doctype - The name of the doctype to remove.
1737
+ */
1738
+
1739
+ }, {
1740
+ key: "removeDoctype",
1741
+ value: function () {
1742
+ var _removeDoctype = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee24(doctype) {
1743
+ return _regenerator.default.wrap(function _callee24$(_context24) {
1744
+ while (1) {
1745
+ switch (_context24.prev = _context24.next) {
1746
+ case 0:
1747
+ this.doctypes = this.doctypes.filter(function (d) {
1748
+ return d !== doctype;
1749
+ });
1750
+ delete this.doctypesReplicationOptions[doctype];
1751
+ _context24.next = 4;
1752
+ return this.pouches.removeDoctype(doctype);
1753
+
1754
+ case 4:
1755
+ case "end":
1756
+ return _context24.stop();
1757
+ }
1758
+ }
1759
+ }, _callee24, this);
1760
+ }));
1761
+
1762
+ function removeDoctype(_x29) {
1763
+ return _removeDoctype.apply(this, arguments);
1764
+ }
1765
+
1766
+ return removeDoctype;
1767
+ }()
1768
+ }, {
1769
+ key: "getSharedDriveDoctypes",
1770
+ value: function getSharedDriveDoctypes() {
1771
+ var _this4 = this;
1772
+
1773
+ return this.doctypes.filter(function (doctype) {
1774
+ var _this4$doctypesReplic;
1775
+
1776
+ return (_this4$doctypesReplic = _this4.doctypesReplicationOptions[doctype]) === null || _this4$doctypesReplic === void 0 ? void 0 : _this4$doctypesReplic.driveId;
1777
+ });
1778
+ }
1659
1779
  }]);
1660
1780
  return PouchLink;
1661
1781
  }(_cozyClient.CozyLink);
@@ -9,6 +9,10 @@ jest.mock('./helpers', () => ({
9
9
  withoutDesignDocuments: jest.fn(),
10
10
  isAdapterBugged: jest.fn()
11
11
  }))
12
+ jest.mock('./remote', () => ({
13
+ fetchRemoteInstance: jest.fn()
14
+ }))
15
+ import { fetchRemoteInstance } from './remote'
12
16
 
13
17
  import CozyPouchLink from '.'
14
18
  import { SCHEMA, TODO_1, TODO_2, TODO_3, TODO_4 } from './__tests__/fixtures'
@@ -53,6 +57,7 @@ async function setup(linkOpts = {}) {
53
57
  await link.onLogin()
54
58
 
55
59
  client.setData = jest.fn()
60
+ fetchRemoteInstance.mockResolvedValue({ rows: [] })
56
61
  }
57
62
 
58
63
  async function clean() {
@@ -15,12 +15,8 @@ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/cl
15
15
 
16
16
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
17
17
 
18
- var _fromPairs = _interopRequireDefault(require("lodash/fromPairs"));
19
-
20
18
  var _forEach = _interopRequireDefault(require("lodash/forEach"));
21
19
 
22
- var _get = _interopRequireDefault(require("lodash/get"));
23
-
24
20
  var _cozyDeviceHelper = require("cozy-device-helper");
25
21
 
26
22
  var _localStorage = require("./localStorage");
@@ -39,6 +35,12 @@ var _pouchdb = require("./migrations/pouchdb");
39
35
 
40
36
  var _pouchdb2 = _interopRequireDefault(require("./db/pouchdb/pouchdb"));
41
37
 
38
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
39
+
40
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
41
+
42
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
43
+
42
44
  var DEFAULT_DELAY = 30 * 1000; // See view_update_changes_batch_size in https://pouchdb.com/api.html#create_database
43
45
  // PouchDB default is 50, which badly hurt performances for large databases
44
46
 
@@ -80,15 +82,22 @@ var PouchManager = /*#__PURE__*/function () {
80
82
  key: "init",
81
83
  value: function () {
82
84
  var _init = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
83
- var _this = this;
85
+ var _this$options$pouch$p,
86
+ _this$options,
87
+ _this$options$pouch,
88
+ _this$options$pouch$o,
89
+ _this$options2,
90
+ _this$options2$pouch,
91
+ _this = this;
92
+
93
+ var pouchPlugins, pouchOptions, _iterator, _step, doctype;
84
94
 
85
- var pouchPlugins, pouchOptions, dbNames, pouchDbNames;
86
95
  return _regenerator.default.wrap(function _callee$(_context) {
87
96
  while (1) {
88
97
  switch (_context.prev = _context.next) {
89
98
  case 0:
90
- pouchPlugins = (0, _get.default)(this.options, 'pouch.plugins', []);
91
- pouchOptions = (0, _get.default)(this.options, 'pouch.options', {});
99
+ pouchPlugins = (_this$options$pouch$p = (_this$options = this.options) === null || _this$options === void 0 ? void 0 : (_this$options$pouch = _this$options.pouch) === null || _this$options$pouch === void 0 ? void 0 : _this$options$pouch.plugins) !== null && _this$options$pouch$p !== void 0 ? _this$options$pouch$p : [];
100
+ pouchOptions = (_this$options$pouch$o = (_this$options2 = this.options) === null || _this$options2 === void 0 ? void 0 : (_this$options2$pouch = _this$options2.pouch) === null || _this$options2$pouch === void 0 ? void 0 : _this$options2$pouch.options) !== null && _this$options$pouch$o !== void 0 ? _this$options$pouch$o : {};
92
101
 
93
102
  if (!pouchOptions.view_update_changes_batch_size) {
94
103
  pouchOptions.view_update_changes_batch_size = DEFAULT_VIEW_UPDATE_BATCH;
@@ -97,26 +106,23 @@ var PouchManager = /*#__PURE__*/function () {
97
106
  (0, _forEach.default)(pouchPlugins, function (plugin) {
98
107
  return _this.PouchDB.plugin(plugin);
99
108
  });
100
- this.pouches = (0, _fromPairs.default)(this.doctypes.map(function (doctype) {
101
- var dbName = (0, _utils.getDatabaseName)(_this.options.prefix, doctype);
102
- var pouch = new _this.PouchDB((0, _utils.getDatabaseName)(_this.options.prefix, doctype), pouchOptions);
103
- return [dbName, pouch];
104
- }));
105
- dbNames = Object.keys(this.pouches);
106
- dbNames.forEach(function (dbName) {
107
- // Set query engine for all databases
108
- var doctype = (0, _utils.getDoctypeFromDatabaseName)(dbName);
109
-
110
- _this.setQueryEngine(dbName, doctype);
111
- }); // Persist db names for old browsers not supporting indexeddb.databases()
112
- // This is useful for cleanup.
113
- // Note PouchDB adds itself the _pouch_ prefix
109
+ this.pouches = {};
110
+ this.doctypesReplicationOptions = this.options.doctypesReplicationOptions || {};
111
+ _iterator = _createForOfIteratorHelper(this.doctypes);
112
+
113
+ try {
114
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
115
+ doctype = _step.value;
116
+ this.addDoctype(doctype, this.doctypesReplicationOptions[doctype]);
117
+ }
118
+ } catch (err) {
119
+ _iterator.e(err);
120
+ } finally {
121
+ _iterator.f();
122
+ }
114
123
 
115
- pouchDbNames = dbNames.map(function (dbName) {
116
- return "_pouch_".concat(dbName);
117
- });
118
124
  _context.next = 10;
119
- return this.storage.persistDatabasesNames(pouchDbNames);
125
+ return this.persistDatabasesNames();
120
126
 
121
127
  case 10:
122
128
  _context.next = 12;
@@ -130,7 +136,6 @@ var PouchManager = /*#__PURE__*/function () {
130
136
  case 15:
131
137
  this.warmedUpQueries = _context.sent;
132
138
  this.getReplicationURL = this.options.getReplicationURL;
133
- this.doctypesReplicationOptions = this.options.doctypesReplicationOptions || {};
134
139
  this.listenerLaunched = false; // We must ensure databases exist on the remote before
135
140
  // starting replications
136
141
 
@@ -144,7 +149,7 @@ var PouchManager = /*#__PURE__*/function () {
144
149
  this.replications = undefined;
145
150
  (0, _pouchdb.destroyOldDatabases)();
146
151
 
147
- case 26:
152
+ case 25:
148
153
  case "end":
149
154
  return _context.stop();
150
155
  }
@@ -704,6 +709,141 @@ var PouchManager = /*#__PURE__*/function () {
704
709
 
705
710
  return clearWarmedUpQueries;
706
711
  }()
712
+ /**
713
+ * Adds a new doctype to the list of managed doctypes, sets its replication options,
714
+ * creates a new PouchDB instance for it, and sets up the query engine.
715
+ *
716
+ * @param {string} doctype - The name of the doctype to add.
717
+ * @param {Object} replicationOptions - The replication options for the doctype.
718
+ */
719
+
720
+ }, {
721
+ key: "addDoctype",
722
+ value: function () {
723
+ var _addDoctype = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee12(doctype, replicationOptions) {
724
+ var _this$options3, _this$options$pouch$o2, _this$options4, _this$options4$pouch;
725
+
726
+ var pouchOptions, dbName;
727
+ return _regenerator.default.wrap(function _callee12$(_context12) {
728
+ while (1) {
729
+ switch (_context12.prev = _context12.next) {
730
+ case 0:
731
+ if (!((_this$options3 = this.options) !== null && _this$options3 !== void 0 && _this$options3.doctypesReplicationOptions)) {
732
+ this.options.doctypesReplicationOptions = {};
733
+ }
734
+
735
+ this.options.doctypesReplicationOptions[doctype] = replicationOptions;
736
+ pouchOptions = (_this$options$pouch$o2 = (_this$options4 = this.options) === null || _this$options4 === void 0 ? void 0 : (_this$options4$pouch = _this$options4.pouch) === null || _this$options4$pouch === void 0 ? void 0 : _this$options4$pouch.options) !== null && _this$options$pouch$o2 !== void 0 ? _this$options$pouch$o2 : {};
737
+
738
+ if (!pouchOptions.view_update_changes_batch_size) {
739
+ pouchOptions.view_update_changes_batch_size = DEFAULT_VIEW_UPDATE_BATCH;
740
+ }
741
+
742
+ dbName = (0, _utils.getDatabaseName)(this.options.prefix, doctype);
743
+ this.pouches[dbName] = new this.PouchDB((0, _utils.getDatabaseName)(this.options.prefix, doctype), pouchOptions);
744
+ _context12.next = 8;
745
+ return this.persistDatabasesNames();
746
+
747
+ case 8:
748
+ this.setQueryEngine(dbName, (0, _utils.getDoctypeFromDatabaseName)(dbName));
749
+
750
+ case 9:
751
+ case "end":
752
+ return _context12.stop();
753
+ }
754
+ }
755
+ }, _callee12, this);
756
+ }));
757
+
758
+ function addDoctype(_x7, _x8) {
759
+ return _addDoctype.apply(this, arguments);
760
+ }
761
+
762
+ return addDoctype;
763
+ }()
764
+ /**
765
+ * Removes a doctype from the list of managed doctypes, deletes its replication options,
766
+ * destroys its PouchDB instance, and removes it from the pouches.
767
+ *
768
+ * @param {string} doctype - The name of the doctype to remove.
769
+ */
770
+
771
+ }, {
772
+ key: "removeDoctype",
773
+ value: function () {
774
+ var _removeDoctype = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee13(doctype) {
775
+ var _this$options5, _this$options5$doctyp;
776
+
777
+ var dbName;
778
+ return _regenerator.default.wrap(function _callee13$(_context13) {
779
+ while (1) {
780
+ switch (_context13.prev = _context13.next) {
781
+ case 0:
782
+ this.doctypes = this.doctypes.filter(function (d) {
783
+ return d !== doctype;
784
+ });
785
+ (_this$options5 = this.options) === null || _this$options5 === void 0 ? true : (_this$options5$doctyp = _this$options5.doctypesReplicationOptions) === null || _this$options5$doctyp === void 0 ? true : delete _this$options5$doctyp[doctype];
786
+ dbName = (0, _utils.getDatabaseName)(this.options.prefix, doctype);
787
+ this.pouches[dbName].destroy();
788
+ delete this.pouches[dbName];
789
+ _context13.next = 7;
790
+ return this.persistDatabasesNames();
791
+
792
+ case 7:
793
+ case "end":
794
+ return _context13.stop();
795
+ }
796
+ }
797
+ }, _callee13, this);
798
+ }));
799
+
800
+ function removeDoctype(_x9) {
801
+ return _removeDoctype.apply(this, arguments);
802
+ }
803
+
804
+ return removeDoctype;
805
+ }()
806
+ /**
807
+ * Persists the names of the PouchDB databases.
808
+ *
809
+ * This method is primarily used to ensure that database names are saved for
810
+ * old browsers that do not support `indexeddb.databases()`. This persistence
811
+ * facilitates cleanup processes. Note that PouchDB automatically adds the
812
+ * `_pouch_` prefix to database names.
813
+ *
814
+ * @returns {Promise<void>}
815
+ */
816
+
817
+ }, {
818
+ key: "persistDatabasesNames",
819
+ value: function () {
820
+ var _persistDatabasesNames = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee14() {
821
+ var dbNames, pouchDbNames;
822
+ return _regenerator.default.wrap(function _callee14$(_context14) {
823
+ while (1) {
824
+ switch (_context14.prev = _context14.next) {
825
+ case 0:
826
+ dbNames = Object.keys(this.pouches);
827
+ pouchDbNames = dbNames.map(function (dbName) {
828
+ return "_pouch_".concat(dbName);
829
+ });
830
+ _context14.next = 4;
831
+ return this.storage.persistDatabasesNames(pouchDbNames);
832
+
833
+ case 4:
834
+ case "end":
835
+ return _context14.stop();
836
+ }
837
+ }
838
+ }, _callee14, this);
839
+ }));
840
+
841
+ function persistDatabasesNames() {
842
+ return _persistDatabasesNames.apply(this, arguments);
843
+ }
844
+
845
+ return persistDatabasesNames;
846
+ }()
707
847
  }]);
708
848
  return PouchManager;
709
849
  }();
@@ -22,7 +22,7 @@ import PouchDB from 'pouchdb-browser'
22
22
  import { LOCALSTORAGE_STORAGE_KEYS, PouchLocalStorage } from './localStorage'
23
23
  import { platformWeb } from './platformWeb'
24
24
 
25
- import { fetchRemoteLastSequence, fetchRemoteInstance } from './remote'
25
+ import { fetchRemoteInstance, fetchRemoteLastSequence } from './remote'
26
26
 
27
27
  const ls = new PouchLocalStorage(platformWeb.storage)
28
28
 
@@ -105,7 +105,6 @@ describe('remote', () => {
105
105
  )
106
106
  })
107
107
  })
108
-
109
108
  describe('fetchRemoteLastSequence', () => {
110
109
  it('Should return data when found', async () => {
111
110
  const remoteUrl =
@@ -165,7 +164,6 @@ const mockDatabaseNotFoundOn = url => {
165
164
  status: 404
166
165
  })
167
166
  }
168
-
169
167
  const mockDatabaseReservedDoctypeOn = url => {
170
168
  fetch.mockOnceIf(
171
169
  url,
@@ -13,8 +13,6 @@ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/
13
13
 
14
14
  var _fromPairs = _interopRequireDefault(require("lodash/fromPairs"));
15
15
 
16
- var _get = _interopRequireDefault(require("lodash/get"));
17
-
18
16
  var _map = _interopRequireDefault(require("lodash/map"));
19
17
 
20
18
  var _startsWith = _interopRequireDefault(require("lodash/startsWith"));
@@ -68,17 +66,21 @@ var replicateOnce = /*#__PURE__*/function () {
68
66
 
69
67
  pouchManager.replications = (0, _map.default)(pouchManager.pouches, /*#__PURE__*/function () {
70
68
  var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(pouch, dbName) {
71
- var doctype, getReplicationURL, initialReplication, replicationFilter, seq, lastSeq, replicationOptions, res;
69
+ var _pouchManager$doctype, _pouchManager$doctype2;
70
+
71
+ var doctype, replicationOptions, getReplicationURL, initialReplication, replicationFilter, isSharedDrive, seq, lastSeq, res;
72
72
  return _regenerator.default.wrap(function _callee$(_context) {
73
73
  while (1) {
74
74
  switch (_context.prev = _context.next) {
75
75
  case 0:
76
- doctype = (0, _utils.getDoctypeFromDatabaseName)(dbName);
76
+ doctype = (0, _utils.getDoctypeFromDatabaseName)(dbName); // Use optional chaining and nullish coalescing instead of get
77
+
78
+ replicationOptions = (_pouchManager$doctype = (_pouchManager$doctype2 = pouchManager.doctypesReplicationOptions) === null || _pouchManager$doctype2 === void 0 ? void 0 : _pouchManager$doctype2[doctype]) !== null && _pouchManager$doctype !== void 0 ? _pouchManager$doctype : {};
77
79
 
78
80
  _logger.default.info('PouchManager: Starting replication for ' + doctype);
79
81
 
80
82
  getReplicationURL = function getReplicationURL() {
81
- return pouchManager.getReplicationURL(doctype);
83
+ return pouchManager.getReplicationURL(doctype, replicationOptions);
82
84
  };
83
85
 
84
86
  initialReplication = pouchManager.getSyncStatus(doctype) !== 'synced';
@@ -87,34 +89,44 @@ var replicateOnce = /*#__PURE__*/function () {
87
89
  return !(0, _startsWith.default)(doc._id, '_design');
88
90
  };
89
91
 
92
+ isSharedDrive = Boolean(replicationOptions.driveId);
90
93
  seq = '';
91
94
 
92
- if (!initialReplication) {
93
- _context.next = 14;
95
+ if (!(initialReplication && !isSharedDrive)) {
96
+ _context.next = 16;
94
97
  break;
95
98
  }
96
99
 
97
- _context.next = 9;
100
+ _context.next = 11;
98
101
  return (0, _remote.fetchRemoteLastSequence)(getReplicationURL());
99
102
 
100
- case 9:
103
+ case 11:
101
104
  lastSeq = _context.sent;
102
- _context.next = 12;
105
+ _context.next = 14;
103
106
  return pouchManager.storage.persistDoctypeLastSequence(doctype, lastSeq);
104
107
 
105
- case 12:
106
- _context.next = 17;
108
+ case 14:
109
+ _context.next = 22;
107
110
  break;
108
111
 
109
- case 14:
110
- _context.next = 16;
112
+ case 16:
113
+ _context.next = 18;
111
114
  return pouchManager.storage.getDoctypeLastSequence(doctype);
112
115
 
113
- case 16:
114
- seq = _context.sent;
116
+ case 18:
117
+ _context.t0 = _context.sent;
118
+
119
+ if (_context.t0) {
120
+ _context.next = 21;
121
+ break;
122
+ }
123
+
124
+ _context.t0 = '';
125
+
126
+ case 21:
127
+ seq = _context.t0;
115
128
 
116
- case 17:
117
- replicationOptions = (0, _get.default)(pouchManager.doctypesReplicationOptions, doctype, {});
129
+ case 22:
118
130
  replicationOptions.initialReplication = initialReplication;
119
131
  replicationOptions.filter = replicationFilter;
120
132
  replicationOptions.since = seq;
@@ -124,25 +136,25 @@ var replicateOnce = /*#__PURE__*/function () {
124
136
  pouchManager.options.onDoctypeSyncStart(doctype);
125
137
  }
126
138
 
127
- _context.next = 25;
128
- return (0, _startReplication.startReplication)(pouch, replicationOptions, getReplicationURL, pouchManager.storage);
139
+ _context.next = 29;
140
+ return (0, _startReplication.startReplication)(pouch, replicationOptions, getReplicationURL, pouchManager.storage, pouchManager.client);
129
141
 
130
- case 25:
142
+ case 29:
131
143
  res = _context.sent;
132
144
 
133
- if (!seq) {
134
- _context.next = 29;
145
+ if (!(seq && !isSharedDrive)) {
146
+ _context.next = 33;
135
147
  break;
136
148
  }
137
149
 
138
- _context.next = 29;
150
+ _context.next = 33;
139
151
  return pouchManager.storage.destroyDoctypeLastSequence(doctype);
140
152
 
141
- case 29:
142
- _context.next = 31;
153
+ case 33:
154
+ _context.next = 35;
143
155
  return pouchManager.updateSyncInfo(doctype);
144
156
 
145
- case 31:
157
+ case 35:
146
158
  pouchManager.checkToWarmupDoctype(doctype, replicationOptions);
147
159
 
148
160
  if (pouchManager.options.onDoctypeSyncEnd) {
@@ -151,7 +163,7 @@ var replicateOnce = /*#__PURE__*/function () {
151
163
 
152
164
  return _context.abrupt("return", res);
153
165
 
154
- case 34:
166
+ case 38:
155
167
  case "end":
156
168
  return _context.stop();
157
169
  }