From 9cebf53707de066c44eb75cc3c31dc5a7c4a1081 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Tue, 25 Feb 2025 22:52:47 +0000 Subject: [PATCH] Allow overriding fetch implementation --- frontend/sync-client/src/services/sync-service.ts | 10 ++++++++-- frontend/sync-client/src/sync-client.ts | 4 ++++ frontend/sync-client/src/utils/retried-fetch.ts | 9 +++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/frontend/sync-client/src/services/sync-service.ts b/frontend/sync-client/src/services/sync-service.ts index e60fcaf..56e8a69 100644 --- a/frontend/sync-client/src/services/sync-service.ts +++ b/frontend/sync-client/src/services/sync-service.ts @@ -17,12 +17,13 @@ export interface CheckConnectionResult { export class SyncService { private client!: Client; private clientWithoutRetries!: Client; + private _fetchImplementation: typeof globalThis.fetch = globalThis.fetch; public constructor( private readonly settings: Settings, private readonly logger: Logger ) { - this.createClient(settings.getSettings().remoteUri); + this.createClient(this.settings.getSettings().remoteUri); settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => { if (newSettings.remoteUri === oldSettings.remoteUri) { @@ -33,6 +34,11 @@ export class SyncService { }); } + public set fetchImplementation(fetch: typeof globalThis.fetch) { + this._fetchImplementation = fetch; + this.createClient(this.settings.getSettings().remoteUri); + } + private static formatError( error: components["schemas"]["SerializedError"] ): string { @@ -289,7 +295,7 @@ export class SyncService { private createClient(remoteUri: string): void { this.client = createClient({ baseUrl: remoteUri, - fetch: retriedFetchFactory(this.logger) + fetch: retriedFetchFactory(this.logger, this._fetchImplementation) }); this.clientWithoutRetries = createClient({ diff --git a/frontend/sync-client/src/sync-client.ts b/frontend/sync-client/src/sync-client.ts index ba9f9d7..302daf3 100644 --- a/frontend/sync-client/src/sync-client.ts +++ b/frontend/sync-client/src/sync-client.ts @@ -45,6 +45,10 @@ export class SyncClient { return this._database.getDocuments().size; } + public set fetchImplementation(fetch: typeof globalThis.fetch) { + this._syncService.fetchImplementation = fetch; + } + public static async create( fs: FileSystemOperations, persistence: PersistenceProvider< diff --git a/frontend/sync-client/src/utils/retried-fetch.ts b/frontend/sync-client/src/utils/retried-fetch.ts index d3efcd2..e4c47f0 100644 --- a/frontend/sync-client/src/utils/retried-fetch.ts +++ b/frontend/sync-client/src/utils/retried-fetch.ts @@ -2,8 +2,6 @@ import * as fetchRetryFactory from "fetch-retry"; import type { RequestInitRetryParams } from "fetch-retry"; import type { Logger } from "src/tracing/logger"; -const fetchWithRetry = fetchRetryFactory.default(fetch); - function getUrlFromInput(input: RequestInfo | URL): string { if (input instanceof URL) { return input.href; @@ -14,12 +12,15 @@ function getUrlFromInput(input: RequestInfo | URL): string { return input.url; } -export function retriedFetchFactory(logger: Logger) { +export function retriedFetchFactory( + logger: Logger, + fetch: typeof globalThis.fetch = globalThis.fetch +) { return async ( input: RequestInfo | URL, init: RequestInitRetryParams = {} ): Promise => { - return fetchWithRetry(input, { + return fetchRetryFactory.default(fetch)(input, { retryOn: function (attempt, error, response) { if (error !== null || !response || response.status >= 500) { logger.warn(