@vrtmrz/livesync-commonlib 0.1.8 → 0.1.10

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.
@@ -46,8 +46,10 @@ export type FetchChangesForInitialSyncProgress = {
46
46
  * Fetches initial data from CouchDB as a stream and writes it into PouchDB.
47
47
  * @param downloadToDB PouchDB instance.
48
48
  * @param remoteDbUrl CouchDB database URL (for example: 'https://xxx.com/mydb').
49
+ * @param authHeader Value of the `Authorization` header for CouchDB.
49
50
  * @param decryptFunction Function to decrypt each document.
50
51
  * @param since Sequence ID to start fetching changes from (default is '0').
52
+ * @param customHeaders Additional request headers required by the CouchDB endpoint or its reverse proxy.
51
53
  */
52
- export declare function fetchChangesForInitialSync(downloadToDB: PouchDB.Database, remoteDbUrl: string, authHeader: string, decryptFunction: (doc: EntryDoc) => Promise<AnyEntry | EntryLeaf>, since?: number | string, onProgress?: (progress: FetchChangesForInitialSyncProgress) => void, onCheckpoint?: (sequence: DBSequence) => void | Promise<void>): Promise<void>;
54
+ export declare function fetchChangesForInitialSync(downloadToDB: PouchDB.Database, remoteDbUrl: string, authHeader: string, decryptFunction: (doc: EntryDoc) => Promise<AnyEntry | EntryLeaf>, since?: number | string, onProgress?: (progress: FetchChangesForInitialSyncProgress) => void, onCheckpoint?: (sequence: DBSequence) => void | Promise<void>, customHeaders?: Record<string, string>): Promise<void>;
53
55
  export {};
@@ -227,7 +227,7 @@ function parseChangesFeedLine(line) {
227
227
  }
228
228
  return { type: "change", change };
229
229
  }
230
- async function fetchChangesForInitialSync(downloadToDB, remoteDbUrl, authHeader, decryptFunction, since = "0", onProgress, onCheckpoint) {
230
+ async function fetchChangesForInitialSync(downloadToDB, remoteDbUrl, authHeader, decryptFunction, since = "0", onProgress, onCheckpoint, customHeaders) {
231
231
  let totalFetched = 0;
232
232
  let totalValidFetched = 0;
233
233
  let totalBytes = 0;
@@ -239,10 +239,9 @@ async function fetchChangesForInitialSync(downloadToDB, remoteDbUrl, authHeader,
239
239
  revs: "true",
240
240
  since: since.toString()
241
241
  };
242
- const fetchHeaders = {
243
- Accept: "application/json",
244
- Authorization: authHeader
245
- };
242
+ const fetchHeaders = new Headers(customHeaders);
243
+ fetchHeaders.set("Accept", "application/json");
244
+ fetchHeaders.set("Authorization", authHeader);
246
245
  const targetURL = setParamsToURL(new URL(`${remoteDbUrl}/_changes`), {
247
246
  ...changesBaseParams,
248
247
  feed: "normal",
@@ -8,6 +8,7 @@ import { ServiceModuleBase } from "./ServiceModuleBase.js";
8
8
  import { fetchChangesForInitialSync, isRetryableStreamingFetchFailure } from "../pouchdb/StreamingFetch.js";
9
9
  import { getConfiguredFunctionsForEncryption } from "../pouchdb/encryption.js";
10
10
  import { AuthorizationHeaderGenerator, generateCredentialObject } from "../replication/httplib.js";
11
+ import { parseHeaderValues } from "../common/utils.js";
11
12
  import { sizeToHumanReadable } from "octagonal-wheels/number";
12
13
  const FAST_FETCH_CHECKPOINT_KEY = "fast-fetch-checkpoint";
13
14
  const FAST_FETCH_RETRY_DELAYS = [2e3, 5e3, 1e4, 2e4];
@@ -261,6 +262,7 @@ Are you sure you wish to proceed?`;
261
262
  await this.suspendReflectingDatabase(!autoResume);
262
263
  await this.control.applySettings();
263
264
  await this.resetLocalDatabase();
265
+ this.clearFastFetchCheckpoint();
264
266
  await delay(1e3);
265
267
  await this.database.openDatabase({
266
268
  databaseEvents: this.databaseEvents,
@@ -299,6 +301,14 @@ Are you sure you wish to proceed?`;
299
301
  await this.fetchLocal(false, true, autoResume);
300
302
  return;
301
303
  }
304
+ if (settings.useRequestAPI) {
305
+ this._log(
306
+ "Fast database fetch is unavailable while 'Use Internal API' is enabled. Falling back to Standard Fetch.",
307
+ LOG_LEVEL_NOTICE
308
+ );
309
+ await this.fetchLocal(false, true, autoResume);
310
+ return;
311
+ }
302
312
  await this.replicator.runBoundedRemoteActivity(() => this.performFetchLocalDBFast(settings, autoResume), {
303
313
  label: "fast-fetch"
304
314
  });
@@ -356,6 +366,7 @@ Are you sure you wish to proceed?`;
356
366
  const authHeader = await new AuthorizationHeaderGenerator().getAuthorizationHeader(
357
367
  generateCredentialObject(settings)
358
368
  );
369
+ const customHeaders = parseHeaderValues(settings.couchDB_CustomHeaders);
359
370
  for (let attempt = 0; ; attempt++) {
360
371
  try {
361
372
  await fetchChangesForInitialSync(
@@ -372,7 +383,8 @@ Total bytes fetched: ${sizeToHumanReadable(progress.totalBytes)}`,
372
383
  "fetch-init-progress"
373
384
  );
374
385
  },
375
- (sequence) => this.saveFastFetchCheckpoint(remote, sequence)
386
+ (sequence) => this.saveFastFetchCheckpoint(remote, sequence),
387
+ customHeaders
376
388
  );
377
389
  break;
378
390
  } catch (ex) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vrtmrz/livesync-commonlib",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Shared data, storage, and replication primitives for Self-hosted LiveSync clients.",
5
5
  "type": "module",
6
6
  "license": "MIT",