diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 803cddb2..0e437cbd 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -49,7 +49,7 @@ jobs: cargo run config-e2e.yml --color never & cd .. - scripts/e2e.sh 16 + scripts/e2e.sh 8 - name: Cleanup if: always() diff --git a/frontend/sync-client/src/file-operations/file-operations.test.ts b/frontend/sync-client/src/file-operations/file-operations.test.ts index 35595e6e..998e47ec 100644 --- a/frontend/sync-client/src/file-operations/file-operations.test.ts +++ b/frontend/sync-client/src/file-operations/file-operations.test.ts @@ -12,7 +12,7 @@ import type { TextWithCursors } from "reconcile-text"; import type { ServerConfig, ServerConfigData } from "../services/server-config"; class MockServerConfig implements Pick { - public getConfig(): ServerConfigData { + public async getConfig(): Promise { return { mergeableFileExtensions: ["md", "txt"], supportedApiVersion: 1, diff --git a/frontend/sync-client/src/file-operations/file-operations.ts b/frontend/sync-client/src/file-operations/file-operations.ts index 4d3e517d..2864bd20 100644 --- a/frontend/sync-client/src/file-operations/file-operations.ts +++ b/frontend/sync-client/src/file-operations/file-operations.ts @@ -97,7 +97,7 @@ export class FileOperations { if ( !isFileTypeMergable( path, - this.serverConfig.getConfig().mergeableFileExtensions + (await this.serverConfig.getConfig()).mergeableFileExtensions ) || isBinary(expectedContent) || isBinary(newContent) diff --git a/frontend/sync-client/src/services/server-config.ts b/frontend/sync-client/src/services/server-config.ts index 3d40f182..309c637c 100644 --- a/frontend/sync-client/src/services/server-config.ts +++ b/frontend/sync-client/src/services/server-config.ts @@ -16,41 +16,40 @@ export class ServerConfig { public constructor(private readonly syncService: SyncService) {} - public async initialize(): Promise { - this.response = this.syncService.ping(); - this.config = await this.response; - - if (this.config.supportedApiVersion !== SUPPORTED_API_VERSION) { + private static validateConfig(config: ServerConfigData): void { + if (config.supportedApiVersion !== SUPPORTED_API_VERSION) { const shouldUpgradeClient = - this.config.supportedApiVersion > SUPPORTED_API_VERSION; + config.supportedApiVersion > SUPPORTED_API_VERSION; throw new ServerVersionMismatchError( - `Unsupported API version: ${this.config.supportedApiVersion}. Consider upgrading the ${ + `Unsupported API version: ${config.supportedApiVersion}. Consider upgrading the ${ shouldUpgradeClient ? "client" : "sync-server" - } to ensure compatibility.` + } to ensure compatibility` ); } - if (!this.config.isAuthenticated) { + if (!config.isAuthenticated) { throw new AuthenticationError( - "Failed to authenticate with the sync-server." + "Failed to authenticate with the sync-server" ); } } + // warm the cache + public async initialize(): Promise { + await this.getConfig(); + } + public async checkConnection(forceUpdate = false): Promise<{ isSuccessful: boolean; message: string; }> { try { let { response } = this; - if (!response && !forceUpdate) { - throw new Error("ServerConfig not initialized"); - } else if (forceUpdate) { + if (!response || forceUpdate) { response = this.response = this.syncService.ping(); } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const result: PingResponse = (await response)!; // it must be defined, otherwise we would have thrown above + const result: PingResponse = await response; // it must be defined, otherwise we would have thrown above this.config = result; if (result.isAuthenticated) { @@ -72,11 +71,14 @@ export class ServerConfig { } } - public getConfig(): ServerConfigData { + public async getConfig(): Promise { if (!this.config) { - throw new Error("ServerConfig not initialized"); + this.response ??= this.syncService.ping(); + this.config = await this.response; } + ServerConfig.validateConfig(this.config); + return this.config; } diff --git a/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts b/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts index 0bef47d4..e3964d30 100644 --- a/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts +++ b/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts @@ -108,7 +108,7 @@ export class UnrestrictedSyncer { ); this.database.addSeenUpdateId(response.vaultUpdateId); - this.updateCache( + await this.updateCache( response.vaultUpdateId, contentBytes, response.relativePath @@ -206,7 +206,8 @@ export class UnrestrictedSyncer { !isBinary(contentBytes) && isFileTypeMergable( document.relativePath, - this.serverConfig.getConfig().mergeableFileExtensions + (await this.serverConfig.getConfig()) + .mergeableFileExtensions ); const cachedVersion = this.contentCache.get( document.metadata.parentVersionId @@ -300,7 +301,7 @@ export class UnrestrictedSyncer { contentBytes, responseBytes ); - this.updateCache( + await this.updateCache( response.vaultUpdateId, responseBytes, actualPath @@ -322,7 +323,7 @@ export class UnrestrictedSyncer { }, document ); - this.updateCache( + await this.updateCache( response.vaultUpdateId, contentBytes, actualPath @@ -451,7 +452,7 @@ export class UnrestrictedSyncer { remoteVersion.relativePath, contentBytes ); - this.updateCache( + await this.updateCache( remoteVersion.vaultUpdateId, contentBytes, remoteVersion.relativePath @@ -547,15 +548,15 @@ export class UnrestrictedSyncer { } } - private updateCache( + private async updateCache( updateId: number, contentBytes: Uint8Array, filePath: RelativePath - ): void { + ): Promise { if ( isFileTypeMergable( filePath, - this.serverConfig.getConfig().mergeableFileExtensions + (await this.serverConfig.getConfig()).mergeableFileExtensions ) && !isBinary(contentBytes) ) { diff --git a/scripts/e2e.sh b/scripts/e2e.sh index a5b5cf3b..49f320a0 100755 --- a/scripts/e2e.sh +++ b/scripts/e2e.sh @@ -50,8 +50,8 @@ for i in $(seq 1 $process_count); do pids+=($pid) echo "Started process $i with PID: $pid" - # Read from pipe, prefix with PID, and write to log file - (sed "s/^/[PID $pid] /" < "$pipe" > "../logs/log_${i}.log"; rm "$pipe") & + # Read from pipe, prefix with PID + (sed "s/^/[PID $pid] /" < "$pipe" | tee "../logs/log_${i}.log"; rm "$pipe") & done cd ..