Lint files

This commit is contained in:
Andras Schmelczer 2024-12-20 16:14:46 +00:00
parent 2f7cad602a
commit ff5af8aea5
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
11 changed files with 184 additions and 276 deletions

View file

@ -1,16 +1,17 @@
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
import createClient, { Client } from "openapi-fetch";
import type { components, paths } from "./types.js"; // generated by openapi-typescript
import { Logger } from "src/logger";
import { Database } from "src/database/database";
import { SyncSettings } from "src/database/sync-settings";
import {
VaultUpdateId,
RelativePath,
import type { Client } from "openapi-fetch";
import createClient from "openapi-fetch";
import type { components, paths } from "./types.js"; // Generated by openapi-typescript
import type { Database } from "src/database/database";
import type { SyncSettings } from "src/database/sync-settings";
import type {
DocumentId,
RelativePath,
VaultUpdateId,
} from "src/database/document-metadata";
import PQueue from "p-queue";
import { Logger } from "src/tracing/logger.js";
export interface RequestCountStatus {
waiting: number;
@ -21,16 +22,17 @@ export interface RequestCountStatus {
export class SyncService {
private client: Client<paths>;
private promiseQueue: PQueue;
private requestCountListeners: Array<(status: RequestCountStatus) => void> =
[];
private status: RequestCountStatus = {
private readonly promiseQueue: PQueue;
private readonly requestCountListeners: ((
status: RequestCountStatus
) => void)[] = [];
private readonly status: RequestCountStatus = {
waiting: 0,
success: 0,
failure: 0,
};
public constructor(private database: Database) {
public constructor(private readonly database: Database) {
this.createClient(database.getSettings());
this.promiseQueue = new PQueue({
concurrency: database.getSettings().uploadConcurrency,
@ -64,36 +66,21 @@ export class SyncService {
listener({ ...this.status });
}
private emitRequestCountChange(): void {
this.requestCountListeners.forEach((listener) =>
listener({ ...this.status })
);
}
private createClient(settings: SyncSettings) {
this.client = createClient<paths>({
baseUrl: settings.remoteUri,
});
}
private enqueue<T>(fn: () => Promise<T>): Promise<T> {
return this.promiseQueue.add(fn) as Promise<T>;
}
public async ping(): Promise<components["schemas"]["PingResponse"]> {
const response = await this.enqueue(() =>
const response = await this.enqueue(async () =>
this.client.GET("/ping", {
params: {
header: {
authorization:
"Bearer " + this.database.getSettings().token,
authorization: `Bearer ${
this.database.getSettings().token
}`,
},
},
})
);
Logger.getInstance().debug(
"Ping response: " + JSON.stringify(response.data)
`Ping response: ${JSON.stringify(response.data)}`
);
if (!response.data) {
@ -112,15 +99,16 @@ export class SyncService {
contentBytes: Uint8Array;
createdDate: Date;
}): Promise<components["schemas"]["DocumentVersion"]> {
const response = await this.enqueue(() =>
const response = await this.enqueue(async () =>
this.client.POST("/vaults/{vault_id}/documents", {
params: {
path: {
vault_id: this.database.getSettings().vaultName,
},
header: {
authorization:
"Bearer " + this.database.getSettings().token,
authorization: `Bearer ${
this.database.getSettings().token
}`,
},
},
body: {
@ -136,7 +124,7 @@ export class SyncService {
}
Logger.getInstance().debug(
"Created document " + JSON.stringify(response.data)
`Created document ${JSON.stringify(response.data)}`
);
return response.data;
@ -155,7 +143,7 @@ export class SyncService {
contentBytes: Uint8Array;
createdDate: Date;
}): Promise<components["schemas"]["DocumentVersion"]> {
const response = await this.enqueue(() =>
const response = await this.enqueue(async () =>
this.client.PUT("/vaults/{vault_id}/documents/{document_id}", {
params: {
path: {
@ -163,8 +151,9 @@ export class SyncService {
document_id: documentId,
},
header: {
authorization:
"Bearer " + this.database.getSettings().token,
authorization: `Bearer ${
this.database.getSettings().token
}`,
},
},
body: {
@ -181,7 +170,7 @@ export class SyncService {
}
Logger.getInstance().debug(
"Updated document " + JSON.stringify(response.data)
`Updated document ${JSON.stringify(response.data)}`
);
return response.data;
@ -196,7 +185,7 @@ export class SyncService {
relativePath: RelativePath;
createdDate: Date;
}): Promise<void> {
const response = await this.enqueue(() =>
const response = await this.enqueue(async () =>
this.client.DELETE("/vaults/{vault_id}/documents/{document_id}", {
params: {
path: {
@ -204,8 +193,9 @@ export class SyncService {
document_id: documentId,
},
header: {
authorization:
"Bearer " + this.database.getSettings().token,
authorization: `Bearer ${
this.database.getSettings().token
}`,
},
},
body: {
@ -220,7 +210,7 @@ export class SyncService {
}
Logger.getInstance().debug(
"Updated document " + JSON.stringify(response.data)
`Updated document ${JSON.stringify(response.data)}`
);
return response.data;
@ -231,7 +221,7 @@ export class SyncService {
}: {
documentId: DocumentId;
}): Promise<components["schemas"]["DocumentVersion"]> {
const response = await this.enqueue(() =>
const response = await this.enqueue(async () =>
this.client.GET("/vaults/{vault_id}/documents/{document_id}", {
params: {
path: {
@ -239,8 +229,9 @@ export class SyncService {
document_id: documentId,
},
header: {
authorization:
"Bearer " + this.database.getSettings().token,
authorization: `Bearer ${
this.database.getSettings().token
}`,
},
},
})
@ -251,7 +242,7 @@ export class SyncService {
}
Logger.getInstance().debug(
"Get document " + JSON.stringify(response.data)
`Get document ${JSON.stringify(response.data)}`
);
return response.data;
@ -260,15 +251,16 @@ export class SyncService {
public async getAll(
since?: VaultUpdateId
): Promise<components["schemas"]["FetchLatestDocumentsResponse"]> {
const response = await this.enqueue(() =>
const response = await this.enqueue(async () =>
this.client.GET("/vaults/{vault_id}/documents", {
params: {
path: {
vault_id: this.database.getSettings().vaultName,
},
header: {
authorization:
"Bearer " + this.database.getSettings().token,
authorization: `Bearer ${
this.database.getSettings().token
}`,
},
query: {
since_update_id: since,
@ -277,14 +269,32 @@ export class SyncService {
})
);
if (!response.data) {
throw new Error(`Failed to get documents: ${response.error}`);
const { error } = response;
if (error) {
throw new Error(`Failed to get documents: ${error}`);
}
Logger.getInstance().debug(
"Get document " + JSON.stringify(response.data)
`Get document ${JSON.stringify(response.data)}`
);
return response.data;
}
private emitRequestCountChange(): void {
this.requestCountListeners.forEach((listener) => {
listener({ ...this.status });
});
}
private createClient(settings: SyncSettings): void {
this.client = createClient<paths>({
baseUrl: settings.remoteUri,
});
}
private async enqueue<T>(fn: () => Promise<T>): Promise<T> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
return this.promiseQueue.add(fn) as Promise<T>;
}
}

View file

@ -23,9 +23,7 @@ export interface paths {
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
headers: Record<string, unknown>;
content: {
"application/json": components["schemas"]["PingResponse"];
};
@ -63,9 +61,7 @@ export interface paths {
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
headers: Record<string, unknown>;
content: {
"application/json": components["schemas"]["FetchLatestDocumentsResponse"];
};
@ -91,9 +87,7 @@ export interface paths {
};
responses: {
200: {
headers: {
[name: string]: unknown;
};
headers: Record<string, unknown>;
content: {
"application/json": components["schemas"]["DocumentVersion"];
};
@ -128,9 +122,7 @@ export interface paths {
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
headers: Record<string, unknown>;
content: {
"application/json": components["schemas"]["DocumentVersion"];
};
@ -156,9 +148,7 @@ export interface paths {
};
responses: {
200: {
headers: {
[name: string]: unknown;
};
headers: Record<string, unknown>;
content: {
"application/json": components["schemas"]["DocumentVersion"];
};
@ -186,9 +176,7 @@ export interface paths {
responses: {
/** @description no content */
200: {
headers: {
[name: string]: unknown;
};
headers: Record<string, unknown>;
content?: never;
};
};