@vrtmrz/livesync-commonlib 0.1.7 → 0.1.9

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 {};
@@ -1,6 +1,7 @@
1
1
  import { _fetch } from "../common/coreEnvFunctions.js";
2
2
  import { LOG_LEVEL_VERBOSE, Logger } from "octagonal-wheels/common/logger";
3
3
  const FAST_FETCH_CHANGES_PAGE_LIMIT = 1e4;
4
+ const FAST_FETCH_CHANGES_PAGE_TIMEOUT_MS = 1e3;
4
5
  class StreamingFetchFailure extends Error {
5
6
  constructor(stage, message, retryable, options) {
6
7
  super(message, options?.cause === void 0 ? void 0 : { cause: options.cause });
@@ -226,7 +227,7 @@ function parseChangesFeedLine(line) {
226
227
  }
227
228
  return { type: "change", change };
228
229
  }
229
- async function fetchChangesForInitialSync(downloadToDB, remoteDbUrl, authHeader, decryptFunction, since = "0", onProgress, onCheckpoint) {
230
+ async function fetchChangesForInitialSync(downloadToDB, remoteDbUrl, authHeader, decryptFunction, since = "0", onProgress, onCheckpoint, customHeaders) {
230
231
  let totalFetched = 0;
231
232
  let totalValidFetched = 0;
232
233
  let totalBytes = 0;
@@ -236,13 +237,11 @@ async function fetchChangesForInitialSync(downloadToDB, remoteDbUrl, authHeader,
236
237
  style: "all_docs",
237
238
  conflicts: "true",
238
239
  revs: "true",
239
- since: since.toString(),
240
- heartbeat: "30000"
241
- };
242
- const fetchHeaders = {
243
- Accept: "application/json",
244
- Authorization: authHeader
240
+ since: since.toString()
245
241
  };
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",
@@ -362,7 +361,8 @@ async function fetchChangesForInitialSync(downloadToDB, remoteDbUrl, authHeader,
362
361
  const changesURL = setParamsToURL(new URL(`${remoteDbUrl}/_changes`), {
363
362
  ...changesBaseParams,
364
363
  since: pageSince.toString(),
365
- limit: pageLimit.toString()
364
+ limit: pageLimit.toString(),
365
+ timeout: FAST_FETCH_CHANGES_PAGE_TIMEOUT_MS.toString()
366
366
  });
367
367
  const response = await fetchResponse(
368
368
  changesURL.toString(),
@@ -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];
@@ -356,6 +357,7 @@ Are you sure you wish to proceed?`;
356
357
  const authHeader = await new AuthorizationHeaderGenerator().getAuthorizationHeader(
357
358
  generateCredentialObject(settings)
358
359
  );
360
+ const customHeaders = parseHeaderValues(settings.couchDB_CustomHeaders);
359
361
  for (let attempt = 0; ; attempt++) {
360
362
  try {
361
363
  await fetchChangesForInitialSync(
@@ -372,7 +374,8 @@ Total bytes fetched: ${sizeToHumanReadable(progress.totalBytes)}`,
372
374
  "fetch-init-progress"
373
375
  );
374
376
  },
375
- (sequence) => this.saveFastFetchCheckpoint(remote, sequence)
377
+ (sequence) => this.saveFastFetchCheckpoint(remote, sequence),
378
+ customHeaders
376
379
  );
377
380
  break;
378
381
  } catch (ex) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vrtmrz/livesync-commonlib",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Shared data, storage, and replication primitives for Self-hosted LiveSync clients.",
5
5
  "type": "module",
6
6
  "license": "MIT",