@vrtmrz/livesync-commonlib 0.6.0 → 0.7.0

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.
Files changed (83) hide show
  1. package/README.md +29 -19
  2. package/dist/common/models/shared.definition.d.ts +10 -1
  3. package/dist/common/models/shared.definition.js +4 -3
  4. package/dist/events/coreEvents.d.ts +3 -0
  5. package/dist/events/coreEvents.js +2 -0
  6. package/dist/managers/EntryManager/EntryManager.d.ts +1 -1
  7. package/dist/managers/EntryManager/EntryManager.js +2 -2
  8. package/dist/managers/EntryManager/EntryManagerImpls.d.ts +1 -1
  9. package/dist/managers/EntryManager/EntryManagerImpls.js +4 -4
  10. package/dist/managers/StorageEventManager/admission.d.ts +1 -0
  11. package/dist/managers/StorageEventManager/admission.js +5 -1
  12. package/dist/managers/StorageEventManager.d.ts +2 -0
  13. package/dist/managers/StorageEventManager.js +1 -0
  14. package/dist/pouchdb/LiveSyncLocalDB.d.ts +1 -1
  15. package/dist/pouchdb/LiveSyncLocalDB.js +2 -2
  16. package/dist/pouchdb/encryption.policy.js +2 -1
  17. package/dist/replication/LiveSyncAbstractReplicator.d.ts +7 -0
  18. package/dist/replication/LiveSyncAbstractReplicator.js +5 -2
  19. package/dist/replication/ReplicatorInstance.d.ts +3 -0
  20. package/dist/replication/ReplicatorProvider.d.ts +23 -2
  21. package/dist/replication/ReplicatorProvider.js +10 -0
  22. package/dist/replication/SyncParamsHandler.js +26 -25
  23. package/dist/replication/UnsupportedEncryptedDocumentFormatError.d.ts +13 -0
  24. package/dist/replication/UnsupportedEncryptedDocumentFormatError.js +28 -0
  25. package/dist/replication/couchdb/LiveSyncReplicator.d.ts +17 -3
  26. package/dist/replication/couchdb/LiveSyncReplicator.js +265 -132
  27. package/dist/replication/index.d.ts +5 -2
  28. package/dist/replication/index.js +8 -0
  29. package/dist/replication/remoteTransportFailure.d.ts +24 -0
  30. package/dist/replication/remoteTransportFailure.js +80 -0
  31. package/dist/replication/trystero/TrysteroReplicatorP2PServer.d.ts +1 -1
  32. package/dist/replication/trystero/TrysteroReplicatorP2PServer.js +2 -2
  33. package/dist/replication/trystero/TrysteroRpcRoom.d.ts +14 -0
  34. package/dist/replication/trystero/TrysteroRpcRoom.js +168 -0
  35. package/dist/replication/trystero/rpcCompat.d.ts +1 -0
  36. package/dist/replication/trystero/rpcCompat.js +15 -0
  37. package/dist/rpc/RpcRoom.d.ts +1 -0
  38. package/dist/rpc/RpcRoom.js +21 -9
  39. package/dist/rpc/RpcSession.d.ts +8 -13
  40. package/dist/rpc/RpcSession.js +14 -35
  41. package/dist/rpc/commonlibRpcRoom.d.ts +1 -0
  42. package/dist/rpc/commonlibRpcRoom.js +4 -0
  43. package/dist/rpc/errors.d.ts +9 -3
  44. package/dist/rpc/errors.js +13 -16
  45. package/dist/rpc/index.d.ts +10 -1
  46. package/dist/rpc/index.js +2 -1
  47. package/dist/rpc/pouchdb/RpcPouchDBProxy.d.ts +1 -1
  48. package/dist/rpc/pouchdb/RpcPouchDBServer.d.ts +1 -1
  49. package/dist/serviceFeatures/targetFilter.d.ts +3 -1
  50. package/dist/serviceFeatures/targetFilter.js +2 -2
  51. package/dist/services/base/IService.d.ts +9 -6
  52. package/dist/services/base/ReadinessEvaluator.d.ts +7 -6
  53. package/dist/services/base/ReadinessEvaluator.js +4 -2
  54. package/dist/services/base/RemoteConnection.d.ts +4 -0
  55. package/dist/services/base/RemoteService.d.ts +5 -3
  56. package/dist/services/base/RemoteService.js +85 -33
  57. package/dist/services/base/ReplicationService.d.ts +11 -2
  58. package/dist/services/base/ReplicationService.js +18 -7
  59. package/dist/services/base/ReplicationService.readiness.d.ts +14 -4
  60. package/dist/services/base/ReplicationService.readiness.js +29 -2
  61. package/dist/services/base/ReplicationService.typedReplication.d.ts +5 -2
  62. package/dist/services/base/ReplicationService.typedReplication.js +67 -6
  63. package/dist/services/base/ReplicationStaticsOwner.d.ts +18 -0
  64. package/dist/services/base/ReplicationStaticsOwner.js +33 -0
  65. package/dist/services/base/ReplicatorService.d.ts +2 -10
  66. package/dist/services/base/ReplicatorService.js +6 -3
  67. package/dist/services/base/VaultService.d.ts +1 -0
  68. package/dist/string_and_binary/chunks.d.ts +1 -1
  69. package/dist/string_and_binary/chunks.js +63 -50
  70. package/dist/worker/bg.worker.js +3 -1
  71. package/dist/worker/bg.worker.splitting.d.ts +1 -0
  72. package/dist/worker/bg.worker.splitting.js +42 -6
  73. package/dist/worker/bgWorker.d.ts +4 -0
  74. package/dist/worker/bgWorker.js +87 -57
  75. package/dist/worker/bgWorker.splitting.d.ts +2 -3
  76. package/dist/worker/bgWorker.splitting.js +48 -52
  77. package/dist/worker/universalTypes.d.ts +13 -1
  78. package/docs/development.md +69 -32
  79. package/docs/offline-scanner.md +14 -6
  80. package/docs/p2p-transport-lifecycle.md +2 -3
  81. package/docs/releasing.md +6 -5
  82. package/docs/storage-events-and-reflection.md +10 -3
  83. package/package.json +3 -2
@@ -3,6 +3,7 @@ import {
3
3
  getUnencryptedContentRejection,
4
4
  isUnencryptedContentRejectedError
5
5
  } from "../UnencryptedContentRejectedError.js";
6
+ import { UnsupportedEncryptedDocumentFormatError } from "../UnsupportedEncryptedDocumentFormatError.js";
6
7
  import { observeReplicationProgress } from "./replicationProgress.js";
7
8
  import {
8
9
  VER,
@@ -39,6 +40,7 @@ import {
39
40
  LiveSyncAbstractReplicator
40
41
  } from "../LiveSyncAbstractReplicator.js";
41
42
  import { shareRunningResult } from "octagonal-wheels/concurrency/lock";
43
+ import { promiseWithResolvers } from "octagonal-wheels/promises";
42
44
  import { Semaphore } from "octagonal-wheels/concurrency/semaphore";
43
45
  import { Trench } from "octagonal-wheels/memory/memutil";
44
46
  import { StreamInbox } from "octagonal-wheels/bureau/StreamInbox";
@@ -62,6 +64,10 @@ import {
62
64
  replicationBlocked,
63
65
  replicationFailed
64
66
  } from "../ReplicatorProvider.js";
67
+ import {
68
+ getReplicationNetworkFailure,
69
+ OneShotConnectivityPreflightTimeoutError
70
+ } from "../remoteTransportFailure.js";
65
71
  import {
66
72
  forgetJoinedDevice as forgetJoinedDeviceFromStore,
67
73
  projectJoinedDevices
@@ -74,11 +80,12 @@ const currentVersionRange = {
74
80
  const selectorOnDemandPull = { selector: { type: { $ne: "leaf" } } };
75
81
  const DEFAULT_ONE_SHOT_CONNECTIVITY_TIMEOUT_MS = 6e4;
76
82
  const ONE_SHOT_REPLICATION_ALREADY_RUNNING = /* @__PURE__ */ Symbol("one-shot-replication-already-running");
77
- class OneShotConnectivityPreflightTimeoutError extends Error {
78
- constructor(timeoutMs) {
79
- super(`Remote connectivity preflight exceeded ${timeoutMs} ms.`);
80
- this.name = "OneShotConnectivityPreflightTimeoutError";
81
- }
83
+ const COUCHDB_REPLICATION_FAILED = { status: "failed" };
84
+ function couchDBReplicationFailure(networkFailure) {
85
+ return networkFailure ? { status: "failed", networkFailure } : COUCHDB_REPLICATION_FAILED;
86
+ }
87
+ function couchDBReplicationFailureFromError(error) {
88
+ return couchDBReplicationFailure(getReplicationNetworkFailure(error));
82
89
  }
83
90
  async function* genReplication(s, signal, log) {
84
91
  const inbox = new StreamInbox({ capacity: 1e4 });
@@ -101,7 +108,8 @@ async function* genReplication(s, signal, log) {
101
108
  const requestSource = s.pull ?? s;
102
109
  let policyFailureReported = false;
103
110
  const onRequestError = (error) => {
104
- if (policyFailureReported || !isUnencryptedContentRejectedError(error)) return;
111
+ if (policyFailureReported) return;
112
+ if (!isUnencryptedContentRejectedError(error) && !UnsupportedEncryptedDocumentFormatError.findIn(error)) return;
105
113
  policyFailureReported = true;
106
114
  push(["error", error]);
107
115
  };
@@ -135,6 +143,7 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
135
143
  * controller, so a stop request cannot be lost during catch-up.
136
144
  */
137
145
  continuousTask;
146
+ continuousStart;
138
147
  continuousStopWaiter;
139
148
  continuousStopRequested = false;
140
149
  continuousPhase;
@@ -282,6 +291,7 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
282
291
  const task = this.continuousTask;
283
292
  if (task) {
284
293
  this.continuousStopRequested = true;
294
+ this.continuousStart?.resolve({ status: "cancelled" });
285
295
  }
286
296
  this.abortController();
287
297
  this.finishReplicationProgress();
@@ -301,6 +311,10 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
301
311
  return this.openOneShotReplication(setting, showResult, false, "sync", ignoreCleanLock);
302
312
  }
303
313
  }
314
+ async startContinuousReplication(setting, onStartSettled) {
315
+ if (!this.nodeid) return false;
316
+ void this.openContinuousReplication(setting, false, false, onStartSettled);
317
+ }
304
318
  /**
305
319
  * Run one finite CouchDB attempt and retain only its own compatibility result.
306
320
  *
@@ -328,7 +342,10 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
328
342
  if (result === ONE_SHOT_REPLICATION_ALREADY_RUNNING) {
329
343
  return replicationBlocked("replication-in-progress");
330
344
  }
331
- return outcomeFromFiniteOpenReplication(result, centralCompatibilityRecoveryHint(decision));
345
+ if (result instanceof UnsupportedEncryptedDocumentFormatError) {
346
+ return replicationFailed(result);
347
+ }
348
+ return outcomeFromFiniteOpenReplication(result === true, centralCompatibilityRecoveryHint(decision));
332
349
  } catch (error) {
333
350
  return replicationFailed(error, centralCompatibilityRecoveryHint(decision));
334
351
  }
@@ -380,7 +397,7 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
380
397
  }
381
398
  /** Ask the current owner to inspect ambiguous managed-operation failures without joining its drain. */
382
399
  requestCurrentDatabaseInspection(error, database) {
383
- if (error instanceof RemoteRequestError || error instanceof ContentReadError && !error.failure.resources.includes("local-database")) {
400
+ if (error instanceof RemoteRequestError || getReplicationNetworkFailure(error) || error instanceof ContentReadError && !error.failure.resources.includes("local-database")) {
384
401
  return;
385
402
  }
386
403
  const service = this.env.services.database;
@@ -420,7 +437,9 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
420
437
  }
421
438
  return;
422
439
  }
423
- this.requestCurrentDatabaseInspection(e, database);
440
+ if (!(e instanceof UnsupportedEncryptedDocumentFormatError)) {
441
+ this.requestCurrentDatabaseInspection(e, database);
442
+ }
424
443
  this.log("Replication error", LOG_LEVEL_NOTICE, "sync");
425
444
  this.log(e, LOG_LEVEL_VERBOSE);
426
445
  }
@@ -430,6 +449,25 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
430
449
  this.log("Replication paused", LOG_LEVEL_VERBOSE, "sync");
431
450
  }
432
451
  async processSync(syncHandler, showResult, docSentOnStart, docArrivedOnStart, syncMode, retrying, reportCancelledAsDone = true) {
452
+ const outcome = await this.runSyncTransfer(
453
+ syncHandler,
454
+ showResult,
455
+ docSentOnStart,
456
+ docArrivedOnStart,
457
+ syncMode,
458
+ retrying,
459
+ reportCancelledAsDone
460
+ );
461
+ return outcome instanceof UnsupportedEncryptedDocumentFormatError ? outcome : outcome.action;
462
+ }
463
+ async processSyncWithOutcome(...args) {
464
+ if (this.processSync !== LiveSyncCouchDBReplicator.prototype.processSync) {
465
+ const outcome = await this.processSync(...args);
466
+ return outcome instanceof UnsupportedEncryptedDocumentFormatError ? outcome : { action: outcome };
467
+ }
468
+ return await this.runSyncTransfer(...args);
469
+ }
470
+ async runSyncTransfer(syncHandler, showResult, docSentOnStart, docArrivedOnStart, syncMode, retrying, reportCancelledAsDone = true) {
433
471
  const databaseOwner = this.env.services.database?.localDatabaseDirect;
434
472
  const controller = new AbortController();
435
473
  if (this.controller) {
@@ -453,7 +491,7 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
453
491
  const releaser = await globalConcurrencyController.tryAcquire(1, REPLICATION_BUSY_TIMEOUT);
454
492
  if (releaser === false) {
455
493
  this.log("Replication stopped for busy.", showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO, "sync");
456
- return "FAILED";
494
+ return { action: "FAILED" };
457
495
  }
458
496
  releaser();
459
497
  if (controller.signal.aborted || this.controller !== controller) break;
@@ -488,22 +526,25 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
488
526
  }
489
527
  if (retrying) {
490
528
  if (this.docSent - docSentOnStart + (this.docArrived - docArrivedOnStart) > this.originalSetting.batch_size * 2) {
491
- return "NEED_RESURRECT";
529
+ return { action: "NEED_RESURRECT" };
492
530
  }
493
531
  }
494
532
  break;
495
533
  case "complete":
496
534
  this.replicationCompleted(showResult);
497
- return "DONE";
535
+ return { action: "DONE" };
498
536
  case "active":
499
537
  this.replicationActivated(showResult);
500
538
  break;
501
539
  case "denied":
502
540
  this.replicationDenied(e);
503
- return "FAILED";
504
- case "error":
505
- this.replicationErrored(e, databaseOwner);
506
- if (isUnencryptedContentRejectedError(e)) return "FAILED";
541
+ return { action: "FAILED" };
542
+ case "error": {
543
+ const unsupportedFormat = UnsupportedEncryptedDocumentFormatError.findIn(e);
544
+ this.replicationErrored(unsupportedFormat ?? e, databaseOwner);
545
+ if (unsupportedFormat) return unsupportedFormat;
546
+ if (isUnencryptedContentRejectedError(e)) return { action: "FAILED" };
547
+ const networkFailure = getReplicationNetworkFailure(e);
507
548
  this.log("Replication stopped.", showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO, "sync");
508
549
  if (this.env.services.remote.hadLastPostFailedBySize) {
509
550
  if (e && e.status == 413) {
@@ -511,14 +552,15 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
511
552
  `Something went wrong during synchronisation. Please check the log!`,
512
553
  LOG_LEVEL_NOTICE
513
554
  );
514
- return "FAILED";
555
+ return { action: "FAILED", networkFailure };
515
556
  }
516
- return "NEED_RETRY";
557
+ return { action: "NEED_RETRY", networkFailure };
517
558
  } else {
518
559
  this.log("Replication error", LOG_LEVEL_NOTICE, "sync");
519
560
  this.log(e, LOG_LEVEL_VERBOSE);
520
561
  }
521
- return "FAILED";
562
+ return { action: "FAILED", networkFailure };
563
+ }
522
564
  case "paused":
523
565
  this.replicationPaused();
524
566
  break;
@@ -529,18 +571,19 @@ class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicator {
529
571
  }
530
572
  }
531
573
  if (reportCancelledAsDone) {
532
- return "DONE";
574
+ return { action: "DONE" };
533
575
  }
534
- return "CANCELLED";
576
+ return { action: "CANCELLED" };
535
577
  } catch (ex) {
536
- if (isUnencryptedContentRejectedError(ex)) {
537
- this.replicationErrored(ex, databaseOwner);
538
- return "FAILED";
578
+ const unsupportedFormat = UnsupportedEncryptedDocumentFormatError.findIn(ex);
579
+ if (unsupportedFormat || isUnencryptedContentRejectedError(ex)) {
580
+ this.replicationErrored(unsupportedFormat ?? ex, databaseOwner);
581
+ return unsupportedFormat ?? { action: "FAILED" };
539
582
  }
540
583
  this.requestCurrentDatabaseInspection(ex, databaseOwner);
541
584
  this.log(`Unexpected synchronization exception`);
542
585
  this.log(ex, LOG_LEVEL_VERBOSE);
543
- return "FAILED";
586
+ return { action: "FAILED", networkFailure: getReplicationNetworkFailure(ex) };
544
587
  } finally {
545
588
  progress.dispose();
546
589
  controller.abort();
@@ -766,7 +809,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
766
809
  recordCompatibilityDecision,
767
810
  cancellationRequested
768
811
  );
769
- return result === ONE_SHOT_REPLICATION_ALREADY_RUNNING ? false : result;
812
+ return result === true;
770
813
  }
771
814
  /**
772
815
  * Run one finite CouchDB transfer and close its owned remote connection.
@@ -782,7 +825,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
782
825
  */
783
826
  async runOneShotReplication(setting, showResult, retrying, syncMode, ignoreCleanLock = false, recordCompatibilityDecision, cancellationRequested = () => false) {
784
827
  if (cancellationRequested()) {
785
- return false;
828
+ return COUCHDB_REPLICATION_FAILED;
786
829
  }
787
830
  let ownsSharedAttempt = false;
788
831
  let next;
@@ -790,10 +833,14 @@ ${sendingDocs}/(${sentDocsCount} done)`,
790
833
  next = await shareRunningResult("oneShotReplication", async () => {
791
834
  ownsSharedAttempt = true;
792
835
  if (cancellationRequested()) {
793
- return false;
836
+ return COUCHDB_REPLICATION_FAILED;
837
+ }
838
+ const seed = await this.prepareOneShotSecuritySeed(setting, showResult);
839
+ if (cancellationRequested()) {
840
+ return COUCHDB_REPLICATION_FAILED;
794
841
  }
795
- if (await this.ensurePBKDF2Salt(setting, showResult, false) === false || cancellationRequested()) {
796
- return false;
842
+ if (seed.ready === false) {
843
+ return couchDBReplicationFailureFromError(seed.error);
797
844
  }
798
845
  if (this.controller) {
799
846
  this.log(
@@ -801,7 +848,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
801
848
  showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO,
802
849
  "sync"
803
850
  );
804
- return false;
851
+ return COUCHDB_REPLICATION_FAILED;
805
852
  }
806
853
  const localOwner = this.env.services.database.localDatabase;
807
854
  const localDB = localOwner.localDatabase;
@@ -813,7 +860,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
813
860
  ignoreCleanLock,
814
861
  recordCompatibilityDecision
815
862
  );
816
- if (ret === false) {
863
+ if ("status" in ret) {
817
864
  if (!cancellationRequested()) {
818
865
  this.log(
819
866
  this.translate("liveSyncReplicator.couldNotConnectToServer"),
@@ -821,12 +868,12 @@ ${sendingDocs}/(${sentDocsCount} done)`,
821
868
  "sync"
822
869
  );
823
870
  }
824
- return false;
871
+ return ret;
825
872
  }
826
873
  const { db, syncOptionBase } = ret;
827
874
  try {
828
875
  if (cancellationRequested()) {
829
- return false;
876
+ return COUCHDB_REPLICATION_FAILED;
830
877
  }
831
878
  this.maxPullSeq = Number(`${ret.info.update_seq}`.split("-")[0]);
832
879
  this.maxPushSeq = Number(
@@ -835,7 +882,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
835
882
  )[0]
836
883
  );
837
884
  if (cancellationRequested()) {
838
- return false;
885
+ return COUCHDB_REPLICATION_FAILED;
839
886
  }
840
887
  if (showResult) {
841
888
  this.log(this.translate("liveSyncReplicator.checkingLastSyncPoint"), LOG_LEVEL_NOTICE, "sync");
@@ -849,13 +896,13 @@ ${sendingDocs}/(${sentDocsCount} done)`,
849
896
  }
850
897
  this.abortController();
851
898
  if (cancellationRequested()) {
852
- return false;
899
+ return COUCHDB_REPLICATION_FAILED;
853
900
  }
854
901
  const syncHandler = syncMode == "sync" ? localDB.sync(db, { ...syncOptionBase }) : syncMode == "pullOnly" ? localDB.replicate.from(db, {
855
902
  ...syncOptionBase,
856
903
  ...setting.readChunksOnline ? selectorOnDemandPull : {}
857
904
  }) : syncMode == "pushOnly" ? localDB.replicate.to(db, { ...syncOptionBase }) : void 0;
858
- const syncResult = await this.processSync(
905
+ const syncResult = await this.processSyncWithOutcome(
859
906
  syncHandler,
860
907
  showResult,
861
908
  docSentOnStart,
@@ -865,21 +912,24 @@ ${sendingDocs}/(${sentDocsCount} done)`,
865
912
  false
866
913
  );
867
914
  if (cancellationRequested()) {
868
- return false;
915
+ return COUCHDB_REPLICATION_FAILED;
869
916
  }
870
- if (syncResult == "DONE") {
917
+ if (syncResult instanceof UnsupportedEncryptedDocumentFormatError) {
918
+ return syncResult;
919
+ }
920
+ if (syncResult.action === "DONE") {
871
921
  if (syncMode !== "pushOnly") {
872
922
  await this.notifyFiniteReplicationCompleted();
873
923
  }
874
924
  return true;
875
925
  }
876
- if (syncResult == "CANCELLED") {
877
- return false;
926
+ if (syncResult.action === "CANCELLED") {
927
+ return COUCHDB_REPLICATION_FAILED;
878
928
  }
879
- if (syncResult == "FAILED") {
880
- return false;
929
+ if (syncResult.action === "FAILED") {
930
+ return couchDBReplicationFailure(syncResult.networkFailure);
881
931
  }
882
- if (syncResult == "NEED_RESURRECT") {
932
+ if (syncResult.action === "NEED_RESURRECT") {
883
933
  this.abortController();
884
934
  return async () => await this.runOneShotReplication(
885
935
  this.originalSetting,
@@ -891,7 +941,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
891
941
  cancellationRequested
892
942
  );
893
943
  }
894
- if (syncResult == "NEED_RETRY") {
944
+ if (syncResult.action === "NEED_RETRY") {
895
945
  const tempSetting = JSON.parse(JSON.stringify(setting));
896
946
  tempSetting.batch_size = Math.ceil(tempSetting.batch_size / 2) + 2;
897
947
  tempSetting.batches_limit = Math.ceil(tempSetting.batches_limit / 2) + 2;
@@ -900,7 +950,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
900
950
  this.translate("liveSyncReplicator.cantReplicateLowerValue"),
901
951
  showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
902
952
  );
903
- return false;
953
+ return couchDBReplicationFailure(syncResult.networkFailure);
904
954
  } else {
905
955
  this.log(
906
956
  this.translate("liveSyncReplicator.retryLowerBatchSize", {
@@ -920,7 +970,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
920
970
  );
921
971
  }
922
972
  }
923
- return false;
973
+ return COUCHDB_REPLICATION_FAILED;
924
974
  } finally {
925
975
  await this.closeRemoteConnection(ret);
926
976
  }
@@ -934,17 +984,27 @@ ${sendingDocs}/(${sentDocsCount} done)`,
934
984
  if (!ownsSharedAttempt) {
935
985
  return ONE_SHOT_REPLICATION_ALREADY_RUNNING;
936
986
  }
937
- if (typeof next === "boolean") {
938
- return next;
987
+ return typeof next === "function" ? await next() : next;
988
+ }
989
+ async prepareOneShotSecuritySeed(setting, showResult) {
990
+ if (this.ensurePBKDF2Salt !== LiveSyncAbstractReplicator.prototype.ensurePBKDF2Salt) {
991
+ const ready = await this.ensurePBKDF2Salt(setting, showResult, false);
992
+ return ready ? { ready } : { ready, error: void 0 };
939
993
  }
940
- return await next();
994
+ return await this.prepareSecuritySeed(setting, showResult, false);
941
995
  }
942
996
  async checkOneShotReplicationConnectivity(setting, skipCheck, showResult, ignoreCleanLock, recordCompatibilityDecision) {
943
997
  if (setting.useRequestAPI) {
944
998
  if (!recordCompatibilityDecision) {
945
- return await this.checkReplicationConnectivity(setting, false, skipCheck, showResult, ignoreCleanLock);
999
+ return await this.checkReplicationConnectivityWithOutcome(
1000
+ setting,
1001
+ false,
1002
+ skipCheck,
1003
+ showResult,
1004
+ ignoreCleanLock
1005
+ );
946
1006
  }
947
- return await this.checkReplicationConnectivity(
1007
+ return await this.checkReplicationConnectivityWithOutcome(
948
1008
  setting,
949
1009
  false,
950
1010
  skipCheck,
@@ -961,7 +1021,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
961
1021
  timeoutMs
962
1022
  );
963
1023
  try {
964
- return await this.checkReplicationConnectivity(
1024
+ return await this.checkReplicationConnectivityWithOutcome(
965
1025
  setting,
966
1026
  false,
967
1027
  skipCheck,
@@ -1027,19 +1087,38 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1027
1087
  return true;
1028
1088
  }
1029
1089
  async checkReplicationConnectivity(setting, keepAlive, skipCheck, showResult, ignoreCleanLock = false, connectionOptions, recordCompatibilityDecision) {
1090
+ const opened = await this.openReplicationConnection(
1091
+ setting,
1092
+ keepAlive,
1093
+ skipCheck,
1094
+ showResult,
1095
+ ignoreCleanLock,
1096
+ connectionOptions,
1097
+ recordCompatibilityDecision
1098
+ );
1099
+ return "status" in opened ? false : opened;
1100
+ }
1101
+ async checkReplicationConnectivityWithOutcome(...args) {
1102
+ if (this.checkReplicationConnectivity !== LiveSyncCouchDBReplicator.prototype.checkReplicationConnectivity) {
1103
+ const connection = await this.checkReplicationConnectivity(...args);
1104
+ return connection === false ? COUCHDB_REPLICATION_FAILED : connection;
1105
+ }
1106
+ return await this.openReplicationConnection(...args);
1107
+ }
1108
+ async openReplicationConnection(setting, keepAlive, skipCheck, showResult, ignoreCleanLock = false, connectionOptions, recordCompatibilityDecision) {
1030
1109
  recordCompatibilityDecision?.(CENTRAL_COMPATIBILITY_NOT_ASSESSED);
1031
1110
  if (setting.versionUpFlash != "") {
1032
1111
  this.log(this.translate("Replicator.Message.VersionUpFlash"), LOG_LEVEL_NOTICE);
1033
- return false;
1112
+ return COUCHDB_REPLICATION_FAILED;
1034
1113
  }
1035
1114
  const uri = setting.couchDB_URI.replace(/\/+$/, "") + (setting.couchDB_DBNAME == "" ? "" : "/" + setting.couchDB_DBNAME);
1036
1115
  if (this.controller) {
1037
1116
  this.log("Another replication running.");
1038
- return false;
1117
+ return COUCHDB_REPLICATION_FAILED;
1039
1118
  }
1040
1119
  let dbRet;
1041
1120
  try {
1042
- dbRet = await this.connectRemoteCouchDBWithSetting(
1121
+ dbRet = await this.connectRemoteCouchDBWithOutcome(
1043
1122
  setting,
1044
1123
  this.isMobile(),
1045
1124
  true,
@@ -1048,19 +1127,18 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1048
1127
  );
1049
1128
  } catch (ex) {
1050
1129
  if (this.reportConnectivityPreflightTimeout(connectionOptions, showResult, ex)) {
1051
- return false;
1130
+ return couchDBReplicationFailureFromError(ex);
1052
1131
  }
1053
1132
  throw ex;
1054
1133
  }
1055
- if (typeof dbRet === "string") {
1056
- if (this.reportConnectivityPreflightTimeout(connectionOptions, showResult)) {
1057
- return false;
1134
+ if ("error" in dbRet) {
1135
+ if (!this.reportConnectivityPreflightTimeout(connectionOptions, showResult)) {
1136
+ this.log(
1137
+ this.translate("liveSyncReplicator.couldNotConnectToURI", { uri, dbRet: dbRet.error.message }),
1138
+ showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
1139
+ );
1058
1140
  }
1059
- this.log(
1060
- this.translate("liveSyncReplicator.couldNotConnectToURI", { uri, dbRet }),
1061
- showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
1062
- );
1063
- return false;
1141
+ return couchDBReplicationFailureFromError(dbRet.error);
1064
1142
  }
1065
1143
  let ownershipTransferred = false;
1066
1144
  try {
@@ -1078,7 +1156,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1078
1156
  centralCompatibilityRejected(CENTRAL_COMPATIBILITY_REJECTION_REASONS.INCOMPATIBLE_VERSION)
1079
1157
  );
1080
1158
  this.log(error, LOG_LEVEL_NOTICE);
1081
- return false;
1159
+ return couchDBReplicationFailureFromError(error);
1082
1160
  }
1083
1161
  if (!skipCheck) {
1084
1162
  this.remoteCleaned = false;
@@ -1096,7 +1174,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1096
1174
  centralCompatibilityRejected(CENTRAL_COMPATIBILITY_REJECTION_REASONS.ID_DERIVATION_MISMATCH)
1097
1175
  );
1098
1176
  this.log("The remote document IDs do not match the configured ID key.", LOG_LEVEL_NOTICE);
1099
- return false;
1177
+ return COUCHDB_REPLICATION_FAILED;
1100
1178
  }
1101
1179
  if (idCompatibility === "unverified" && setting.idDerivationVersion === 1 && setting.usePathObfuscation) {
1102
1180
  this.log("No remote document was available to verify the configured ID key.", LOG_LEVEL_INFO);
@@ -1106,7 +1184,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1106
1184
  centralCompatibilityRejected(CENTRAL_COMPATIBILITY_REJECTION_REASONS.INCOMPATIBLE_VERSION)
1107
1185
  );
1108
1186
  this.log(this.translate("liveSyncReplicator.remoteDbCorrupted"), LOG_LEVEL_NOTICE);
1109
- return false;
1187
+ return COUCHDB_REPLICATION_FAILED;
1110
1188
  }
1111
1189
  const progress = `${dbRet.info.update_seq}`;
1112
1190
  const info = {
@@ -1136,7 +1214,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1136
1214
  "The remote database has no compatibility with the running version. Please upgrade the plugin.",
1137
1215
  LOG_LEVEL_NOTICE
1138
1216
  );
1139
- return false;
1217
+ return COUCHDB_REPLICATION_FAILED;
1140
1218
  } else if (ensure == "NODE_LOCKED") {
1141
1219
  recordCompatibilityDecision?.(
1142
1220
  centralCompatibilityRejected(CENTRAL_COMPATIBILITY_REJECTION_REASONS.NODE_LOCKED)
@@ -1147,7 +1225,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1147
1225
  );
1148
1226
  this.remoteLockedAndDeviceNotAccepted = true;
1149
1227
  this.remoteLocked = true;
1150
- return false;
1228
+ return COUCHDB_REPLICATION_FAILED;
1151
1229
  } else if (ensure == "LOCKED") {
1152
1230
  this.remoteLocked = true;
1153
1231
  } else if (ensure == "NODE_CLEANED") {
@@ -1164,13 +1242,13 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1164
1242
  this.remoteLockedAndDeviceNotAccepted = true;
1165
1243
  this.remoteLocked = true;
1166
1244
  this.remoteCleaned = true;
1167
- return false;
1245
+ return COUCHDB_REPLICATION_FAILED;
1168
1246
  }
1169
1247
  } else if (ensure == "ID_KEY_MISMATCH") {
1170
1248
  recordCompatibilityDecision?.(
1171
1249
  centralCompatibilityRejected(CENTRAL_COMPATIBILITY_REJECTION_REASONS.ID_DERIVATION_MISMATCH)
1172
1250
  );
1173
- return false;
1251
+ return COUCHDB_REPLICATION_FAILED;
1174
1252
  } else if (ensure == "OK") {
1175
1253
  } else if (ensure[0] == "MISMATCHED") {
1176
1254
  recordCompatibilityDecision?.(
@@ -1183,14 +1261,14 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1183
1261
  this.log(this.translate("liveSyncReplicator.mismatchedTweakDetected"), LOG_LEVEL_NOTICE);
1184
1262
  this.tweakSettingsMismatched = true;
1185
1263
  this.preferredTweakValue = ensure[1];
1186
- return false;
1264
+ return COUCHDB_REPLICATION_FAILED;
1187
1265
  }
1188
1266
  const requiredFeatures = requiredRemoteFeatures(setting);
1189
1267
  if (requiredFeatures.length > 0 && !await declareRemoteFeatures(dbRet.db, requiredFeatures)) {
1190
1268
  recordCompatibilityDecision?.(
1191
1269
  centralCompatibilityRejected(CENTRAL_COMPATIBILITY_REJECTION_REASONS.INCOMPATIBLE_VERSION)
1192
1270
  );
1193
- return false;
1271
+ return COUCHDB_REPLICATION_FAILED;
1194
1272
  }
1195
1273
  recordCompatibilityDecision?.(CENTRAL_COMPATIBILITY_ACCEPTED);
1196
1274
  }
@@ -1203,7 +1281,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1203
1281
  syncOptionBase.pull = { ...selectorOnDemandPull };
1204
1282
  }
1205
1283
  if (this.reportConnectivityPreflightTimeout(connectionOptions, showResult)) {
1206
- return false;
1284
+ return COUCHDB_REPLICATION_FAILED;
1207
1285
  }
1208
1286
  const syncOption = keepAlive ? { live: true, retry: true, heartbeat: setting.useTimeouts ? false : 3e4, ...syncOptionBase } : { ...syncOptionBase };
1209
1287
  const connection = {
@@ -1215,7 +1293,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1215
1293
  return connection;
1216
1294
  } catch (ex) {
1217
1295
  if (this.reportConnectivityPreflightTimeout(connectionOptions, showResult, ex)) {
1218
- return false;
1296
+ return couchDBReplicationFailureFromError(ex);
1219
1297
  }
1220
1298
  throw ex;
1221
1299
  } finally {
@@ -1224,25 +1302,45 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1224
1302
  }
1225
1303
  }
1226
1304
  }
1227
- openContinuousReplication(setting, showResult, retrying) {
1228
- if (this.continuousTask) {
1305
+ openContinuousReplication(setting, showResult, retrying, onStartSettled) {
1306
+ if (this.continuousTask && this.continuousStart) {
1307
+ const start2 = this.continuousStopRequested ? Promise.resolve({ status: "cancelled" }) : this.continuousStart.promise;
1308
+ this.observeContinuousStart(start2, onStartSettled);
1229
1309
  return this.continuousTask;
1230
1310
  }
1231
1311
  this.continuousStopRequested = false;
1232
1312
  this.continuousStopWaiter = void 0;
1233
- let resolveTask;
1234
- let rejectTask;
1235
- const task = new Promise((resolve, reject) => {
1236
- resolveTask = resolve;
1237
- rejectTask = reject;
1238
- });
1239
- this.continuousTask = task;
1240
- void task.then(
1241
- () => this.clearContinuousTask(task),
1242
- () => this.clearContinuousTask(task)
1313
+ const start = promiseWithResolvers();
1314
+ const task = promiseWithResolvers();
1315
+ this.continuousTask = task.promise;
1316
+ this.continuousStart = start;
1317
+ this.observeContinuousStart(start.promise, onStartSettled);
1318
+ void task.promise.then(
1319
+ () => this.clearContinuousTask(task.promise),
1320
+ () => this.clearContinuousTask(task.promise)
1243
1321
  );
1244
- void this.runContinuousReplication(setting, showResult, retrying).then(resolveTask, rejectTask);
1245
- return task;
1322
+ const reportIncompleteStart = (failure) => {
1323
+ start.resolve(this.continuousStopRequested ? { status: "cancelled" } : failure);
1324
+ };
1325
+ void this.runContinuousReplication(setting, showResult, retrying, start.resolve).then(
1326
+ (result) => {
1327
+ task.resolve(result === true);
1328
+ if (result !== true) {
1329
+ reportIncompleteStart(result);
1330
+ }
1331
+ },
1332
+ (error) => {
1333
+ task.reject(error);
1334
+ reportIncompleteStart(couchDBReplicationFailureFromError(error));
1335
+ }
1336
+ );
1337
+ return task.promise;
1338
+ }
1339
+ observeContinuousStart(start, onStartSettled) {
1340
+ if (!onStartSettled) return;
1341
+ void start.then(onStartSettled).catch((error) => {
1342
+ this.log(error, LOG_LEVEL_VERBOSE, "sync");
1343
+ });
1246
1344
  }
1247
1345
  clearContinuousTask(task) {
1248
1346
  if (this.continuousTask !== task) {
@@ -1251,57 +1349,65 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1251
1349
  this.finishReplicationProgress();
1252
1350
  this.continuousPhase = void 0;
1253
1351
  this.continuousTask = void 0;
1352
+ this.continuousStart = void 0;
1254
1353
  this.continuousStopWaiter = void 0;
1255
1354
  this.continuousStopRequested = false;
1256
1355
  }
1257
- async runContinuousReplication(setting, showResult, retrying) {
1258
- const next = await shareRunningResult("continuousReplication", async () => {
1356
+ async runContinuousCatchUp(setting, showResult) {
1357
+ const args = [
1358
+ setting,
1359
+ showResult,
1360
+ false,
1361
+ "pullOnly",
1362
+ false,
1363
+ void 0,
1364
+ () => this.continuousStopRequested
1365
+ ];
1366
+ if (this.openOneShotReplication !== LiveSyncCouchDBReplicator.prototype.openOneShotReplication) {
1367
+ const caughtUp = await this.openOneShotReplication(...args);
1368
+ return caughtUp ? true : COUCHDB_REPLICATION_FAILED;
1369
+ }
1370
+ return await this.runOneShotReplication(...args);
1371
+ }
1372
+ async runContinuousReplication(setting, showResult, retrying, settleStart) {
1373
+ let ownsSharedAttempt = false;
1374
+ const shared = shareRunningResult("continuousReplication", async () => {
1375
+ ownsSharedAttempt = true;
1259
1376
  if (this.continuousStopRequested || this.controller) {
1260
1377
  this.log(
1261
1378
  this.translate("liveSyncReplicator.replicationInProgress"),
1262
1379
  showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
1263
1380
  );
1264
- return false;
1381
+ return COUCHDB_REPLICATION_FAILED;
1265
1382
  }
1266
1383
  this.publishPreparationPhase("catch-up");
1267
1384
  const localOwner = this.env.services.database.localDatabase;
1268
1385
  const localDB = localOwner.localDatabase;
1269
1386
  this.log(this.translate("liveSyncReplicator.beforeLiveSync"));
1270
1387
  const caughtUp = await this.env.services.replicator.runFiniteReplicationActivity(
1271
- () => this.openOneShotReplication(
1272
- setting,
1273
- showResult,
1274
- false,
1275
- "pullOnly",
1276
- false,
1277
- void 0,
1278
- () => this.continuousStopRequested
1279
- ),
1388
+ () => this.runContinuousCatchUp(setting, showResult),
1280
1389
  { label: "replication" }
1281
1390
  );
1282
- if (caughtUp && !this.continuousStopRequested) {
1391
+ if (caughtUp === true && !this.continuousStopRequested) {
1283
1392
  this.publishPreparationPhase("continuous");
1284
1393
  this.log(this.translate("liveSyncReplicator.liveSyncBegin"));
1285
- const ret = await this.checkReplicationConnectivity(setting, true, false, showResult);
1286
- if (ret === false || this.continuousStopRequested) {
1287
- if (ret !== false) {
1288
- await this.closeRemoteConnection(ret);
1289
- }
1290
- if (ret === false && !this.continuousStopRequested) {
1394
+ const ret = await this.checkReplicationConnectivityWithOutcome(setting, true, false, showResult);
1395
+ if ("status" in ret) {
1396
+ if (!this.continuousStopRequested) {
1291
1397
  this.log(
1292
1398
  this.translate("liveSyncReplicator.couldNotConnectToServer"),
1293
1399
  showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
1294
1400
  );
1295
1401
  }
1296
- return false;
1297
- }
1298
- if (showResult) {
1299
- this.log(this.translate("liveSyncReplicator.checkingLastSyncPoint"), LOG_LEVEL_NOTICE, "sync");
1402
+ return ret;
1300
1403
  }
1301
1404
  const { db, syncOption } = ret;
1302
1405
  try {
1303
1406
  if (this.continuousStopRequested) {
1304
- return false;
1407
+ return COUCHDB_REPLICATION_FAILED;
1408
+ }
1409
+ if (showResult) {
1410
+ this.log(this.translate("liveSyncReplicator.checkingLastSyncPoint"), LOG_LEVEL_NOTICE, "sync");
1305
1411
  }
1306
1412
  this.syncStatus = "STARTED";
1307
1413
  this.maxPullSeq = Number(`${ret.info.update_seq}`.split("-")[0]);
@@ -1311,7 +1417,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1311
1417
  )[0]
1312
1418
  );
1313
1419
  if (this.continuousStopRequested) {
1314
- return false;
1420
+ return COUCHDB_REPLICATION_FAILED;
1315
1421
  }
1316
1422
  this.updateInfo();
1317
1423
  const docArrivedOnStart = this.docArrived;
@@ -1321,11 +1427,12 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1321
1427
  }
1322
1428
  this.abortController();
1323
1429
  if (this.continuousStopRequested) {
1324
- return false;
1430
+ return COUCHDB_REPLICATION_FAILED;
1325
1431
  }
1326
1432
  const syncHandler = localDB.sync(db, {
1327
1433
  ...syncOption
1328
1434
  });
1435
+ settleStart?.({ status: "started" });
1329
1436
  const syncMode = "sync";
1330
1437
  const syncResult = await this.processSync(
1331
1438
  syncHandler,
@@ -1336,19 +1443,19 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1336
1443
  retrying
1337
1444
  );
1338
1445
  if (this.continuousStopRequested) {
1339
- return false;
1446
+ return COUCHDB_REPLICATION_FAILED;
1340
1447
  }
1341
1448
  if (syncResult == "DONE") {
1342
1449
  return true;
1343
1450
  }
1344
1451
  if (syncResult == "FAILED") {
1345
- return false;
1452
+ return COUCHDB_REPLICATION_FAILED;
1346
1453
  }
1347
1454
  if (syncResult == "NEED_RESURRECT") {
1348
1455
  this.abortController();
1349
1456
  return async () => {
1350
1457
  if (this.continuousStopRequested) {
1351
- return false;
1458
+ return COUCHDB_REPLICATION_FAILED;
1352
1459
  }
1353
1460
  return await this.runContinuousReplication(this.originalSetting, showResult, false);
1354
1461
  };
@@ -1362,7 +1469,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1362
1469
  this.translate("liveSyncReplicator.cantReplicateLowerValue"),
1363
1470
  showResult ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
1364
1471
  );
1365
- return false;
1472
+ return COUCHDB_REPLICATION_FAILED;
1366
1473
  } else {
1367
1474
  this.log(
1368
1475
  this.translate("liveSyncReplicator.retryLowerBatchSize", {
@@ -1373,7 +1480,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1373
1480
  );
1374
1481
  return async () => {
1375
1482
  if (this.continuousStopRequested) {
1376
- return false;
1483
+ return COUCHDB_REPLICATION_FAILED;
1377
1484
  }
1378
1485
  return await this.runContinuousReplication(tempSetting, showResult, true);
1379
1486
  };
@@ -1383,12 +1490,13 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1383
1490
  await this.closeRemoteConnection(ret);
1384
1491
  }
1385
1492
  }
1386
- return false;
1493
+ return caughtUp === true || caughtUp === ONE_SHOT_REPLICATION_ALREADY_RUNNING || caughtUp instanceof UnsupportedEncryptedDocumentFormatError ? COUCHDB_REPLICATION_FAILED : caughtUp;
1387
1494
  });
1388
- if (typeof next === "boolean") {
1389
- return next;
1495
+ if (!ownsSharedAttempt) {
1496
+ return COUCHDB_REPLICATION_FAILED;
1390
1497
  }
1391
- return await next();
1498
+ const next = await shared;
1499
+ return typeof next === "function" ? await next() : next;
1392
1500
  }
1393
1501
  closeReplication() {
1394
1502
  const hadController = !!this.controller;
@@ -1513,8 +1621,25 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1513
1621
  });
1514
1622
  }
1515
1623
  async connectRemoteCouchDBWithSetting(settings, isMobile, performSetup = false, skipInfo = false, connectionOptions) {
1516
- if (settings.encrypt && settings.passphrase == "" && !settings.permitEmptyPassphrase) {
1517
- return "Empty passphrases cannot be used without explicit permission";
1624
+ const opened = await this.openRemoteCouchDBWithSetting(
1625
+ settings,
1626
+ isMobile,
1627
+ performSetup,
1628
+ skipInfo,
1629
+ connectionOptions
1630
+ );
1631
+ return "error" in opened ? opened.error.message : opened;
1632
+ }
1633
+ async connectRemoteCouchDBWithOutcome(...args) {
1634
+ if (this.connectRemoteCouchDBWithSetting !== LiveSyncCouchDBReplicator.prototype.connectRemoteCouchDBWithSetting) {
1635
+ const opened = await this.connectRemoteCouchDBWithSetting(...args);
1636
+ return typeof opened === "string" ? { error: new Error(opened) } : opened;
1637
+ }
1638
+ return await this.openRemoteCouchDBWithSetting(...args);
1639
+ }
1640
+ async openRemoteCouchDBWithSetting(settings, isMobile, performSetup = false, skipInfo = false, connectionOptions) {
1641
+ if (settings.encrypt && settings.passphrase === "" && !settings.permitEmptyPassphrase) {
1642
+ return { error: new Error("Empty passphrases cannot be used without explicit permission") };
1518
1643
  }
1519
1644
  const customHeaders = parseHeaderValues(settings.couchDB_CustomHeaders);
1520
1645
  const auth = settings.useJWT ? {
@@ -1529,7 +1654,7 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1529
1654
  password: settings.couchDB_PASSWORD,
1530
1655
  type: "basic"
1531
1656
  };
1532
- return this.env.services.remote.connect(
1657
+ const args = [
1533
1658
  settings.couchDB_URI.replace(/\/+$/, "") + (settings.couchDB_DBNAME == "" ? "" : "/" + settings.couchDB_DBNAME),
1534
1659
  auth,
1535
1660
  settings.disableRequestURI || isMobile,
@@ -1548,12 +1673,20 @@ ${sendingDocs}/(${sentDocsCount} done)`,
1548
1673
  acceptUnencryptedContent: settings.acceptUnencryptedContent === true,
1549
1674
  useCouchDBCacheControlHeadersOnly: settings.useCouchDBCacheControlHeadersOnly === true
1550
1675
  }
1551
- );
1676
+ ];
1677
+ const remote = this.env.services.remote;
1678
+ if (remote.openCouchDBConnection) {
1679
+ return await remote.openCouchDBConnection(...args);
1680
+ }
1681
+ const opened = await remote.connect(...args);
1682
+ return typeof opened === "string" ? { error: new Error(opened) } : opened;
1552
1683
  }
1553
1684
  async openOwnedConnection(settings, performSetup = false) {
1554
- const ret = await this.connectRemoteCouchDBWithSetting(settings, this.isMobile(), performSetup, true);
1555
- if (typeof ret === "string") {
1556
- throw new Error(`${this.translate("liveSyncReplicator.couldNotConnectToServer")}:${ret}`);
1685
+ const ret = await this.connectRemoteCouchDBWithOutcome(settings, this.isMobile(), performSetup, true);
1686
+ if ("error" in ret) {
1687
+ throw new Error(`${this.translate("liveSyncReplicator.couldNotConnectToServer")}:${ret.error.message}`, {
1688
+ cause: ret.error
1689
+ });
1557
1690
  }
1558
1691
  return ret;
1559
1692
  }