Extract library from plugin
This commit is contained in:
parent
8374c971ee
commit
ae3acb9e1e
37 changed files with 61 additions and 77 deletions
3
sync-client/jest.config.js
Normal file
3
sync-client/jest.config.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module.exports = {
|
||||
preset: "ts-jest/presets/js-with-babel-esm"
|
||||
};
|
||||
186
sync-client/src/database/database.ts
Normal file
186
sync-client/src/database/database.ts
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
import type { SyncSettings } from "./sync-settings";
|
||||
import { DEFAULT_SETTINGS } from "./sync-settings";
|
||||
import type {
|
||||
DocumentId,
|
||||
DocumentMetadata,
|
||||
RelativePath,
|
||||
VaultUpdateId
|
||||
} from "./document-metadata";
|
||||
import { Logger } from "src/tracing/logger";
|
||||
|
||||
interface StoredDatabase {
|
||||
documents: Map<RelativePath, DocumentMetadata>;
|
||||
settings: SyncSettings;
|
||||
lastSeenUpdateId: VaultUpdateId | undefined;
|
||||
}
|
||||
|
||||
// Todo: split it into settings and documents
|
||||
export class Database {
|
||||
private _documents = new Map<RelativePath, DocumentMetadata>();
|
||||
private _settings: SyncSettings;
|
||||
private _lastSeenUpdateId: VaultUpdateId | undefined;
|
||||
|
||||
private readonly onSettingsChangeHandlers: ((
|
||||
newSettings: SyncSettings,
|
||||
oldSettings: SyncSettings
|
||||
) => void)[] = [];
|
||||
|
||||
public constructor(
|
||||
initialState: Partial<StoredDatabase> | undefined,
|
||||
private readonly saveData: (data: unknown) => Promise<void>
|
||||
) {
|
||||
initialState ??= {};
|
||||
if (
|
||||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
||||
Object.prototype.hasOwnProperty.call(initialState, "documents") &&
|
||||
initialState.documents
|
||||
) {
|
||||
for (const [relativePath, metadata] of Object.entries(
|
||||
initialState.documents
|
||||
)) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
||||
this._documents.set(relativePath, metadata as DocumentMetadata);
|
||||
}
|
||||
}
|
||||
|
||||
Logger.getInstance().debug(`Loaded ${this._documents.size} documents`);
|
||||
|
||||
this._settings = {
|
||||
...DEFAULT_SETTINGS,
|
||||
...(initialState.settings ?? {})
|
||||
};
|
||||
|
||||
Logger.getInstance().debug(
|
||||
`Loaded settings: ${JSON.stringify(this._settings, null, 2)}`
|
||||
);
|
||||
|
||||
this._lastSeenUpdateId = initialState.lastSeenUpdateId;
|
||||
|
||||
Logger.getInstance().debug(
|
||||
`Loaded last seen update id: ${this._lastSeenUpdateId}`
|
||||
);
|
||||
}
|
||||
|
||||
public getDocuments(): Map<RelativePath, DocumentMetadata> {
|
||||
return this._documents;
|
||||
}
|
||||
|
||||
public getSettings(): SyncSettings {
|
||||
return this._settings;
|
||||
}
|
||||
|
||||
public async setSettings(value: SyncSettings): Promise<void> {
|
||||
const oldSettings = this._settings;
|
||||
this._settings = value;
|
||||
this.onSettingsChangeHandlers.forEach((handler) => {
|
||||
handler(value, oldSettings);
|
||||
});
|
||||
await this.save();
|
||||
}
|
||||
|
||||
public addOnSettingsChangeHandlers(
|
||||
handler: (settings: SyncSettings, oldSettings: SyncSettings) => void
|
||||
): void {
|
||||
this.onSettingsChangeHandlers.push(handler);
|
||||
}
|
||||
|
||||
public async setSetting<T extends keyof SyncSettings>(
|
||||
key: T,
|
||||
value: SyncSettings[T]
|
||||
): Promise<void> {
|
||||
const newSettings = { ...this._settings, [key]: value };
|
||||
Logger.getInstance().debug(
|
||||
`Setting ${key} to ${value}, new settings: ${JSON.stringify(
|
||||
newSettings,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
);
|
||||
await this.setSettings(newSettings);
|
||||
}
|
||||
|
||||
public getLastSeenUpdateId(): VaultUpdateId | undefined {
|
||||
return this._lastSeenUpdateId;
|
||||
}
|
||||
|
||||
public async setLastSeenUpdateId(
|
||||
value: VaultUpdateId | undefined
|
||||
): Promise<void> {
|
||||
this._lastSeenUpdateId = value;
|
||||
await this.save();
|
||||
}
|
||||
|
||||
public async resetSyncState(): Promise<void> {
|
||||
this._documents = new Map();
|
||||
this._lastSeenUpdateId = 0;
|
||||
await this.save();
|
||||
}
|
||||
|
||||
public getDocumentByDocumentId(
|
||||
documentId: DocumentId
|
||||
): [RelativePath, DocumentMetadata] | undefined {
|
||||
return [...this._documents.entries()].find(
|
||||
([_, metadata]) => metadata.documentId === documentId
|
||||
);
|
||||
}
|
||||
|
||||
public async setDocument({
|
||||
documentId,
|
||||
relativePath,
|
||||
parentVersionId,
|
||||
hash
|
||||
}: {
|
||||
documentId: DocumentId;
|
||||
relativePath: RelativePath;
|
||||
parentVersionId: VaultUpdateId;
|
||||
hash: string;
|
||||
}): Promise<void> {
|
||||
this._documents.set(relativePath, {
|
||||
documentId,
|
||||
parentVersionId,
|
||||
hash
|
||||
});
|
||||
await this.save();
|
||||
}
|
||||
|
||||
public async moveDocument({
|
||||
documentId,
|
||||
oldRelativePath,
|
||||
relativePath,
|
||||
parentVersionId,
|
||||
hash
|
||||
}: {
|
||||
documentId: DocumentId;
|
||||
oldRelativePath: RelativePath;
|
||||
relativePath: RelativePath;
|
||||
parentVersionId: VaultUpdateId;
|
||||
hash: string;
|
||||
}): Promise<void> {
|
||||
this._documents.delete(oldRelativePath);
|
||||
this._documents.set(relativePath, {
|
||||
documentId,
|
||||
parentVersionId,
|
||||
hash
|
||||
});
|
||||
await this.save();
|
||||
}
|
||||
|
||||
public async removeDocument(relativePath: RelativePath): Promise<void> {
|
||||
this._documents.delete(relativePath);
|
||||
await this.save();
|
||||
}
|
||||
|
||||
public getDocument(
|
||||
relativePath: RelativePath
|
||||
): DocumentMetadata | undefined {
|
||||
return this._documents.get(relativePath);
|
||||
}
|
||||
|
||||
private async save(): Promise<void> {
|
||||
await this.saveData({
|
||||
documents: Object.fromEntries(this._documents.entries()),
|
||||
settings: this._settings,
|
||||
lastSeenUpdateId: this._lastSeenUpdateId
|
||||
});
|
||||
}
|
||||
}
|
||||
9
sync-client/src/database/document-metadata.ts
Normal file
9
sync-client/src/database/document-metadata.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
export type VaultUpdateId = number;
|
||||
export type DocumentId = string;
|
||||
export type RelativePath = string;
|
||||
|
||||
export interface DocumentMetadata {
|
||||
parentVersionId: VaultUpdateId;
|
||||
documentId: DocumentId;
|
||||
hash: string;
|
||||
}
|
||||
25
sync-client/src/database/sync-settings.ts
Normal file
25
sync-client/src/database/sync-settings.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { LogLevel } from "src/tracing/logger";
|
||||
|
||||
export interface SyncSettings {
|
||||
remoteUri: string;
|
||||
token: string;
|
||||
vaultName: string;
|
||||
fetchChangesUpdateIntervalMs: number;
|
||||
syncConcurrency: number;
|
||||
isSyncEnabled: boolean;
|
||||
displayNoopSyncEvents: boolean;
|
||||
minimumLogLevel: LogLevel;
|
||||
maxFileSizeMB: number;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: SyncSettings = {
|
||||
remoteUri: "",
|
||||
token: "",
|
||||
vaultName: "default",
|
||||
fetchChangesUpdateIntervalMs: 1000,
|
||||
syncConcurrency: 1,
|
||||
isSyncEnabled: false,
|
||||
displayNoopSyncEvents: false,
|
||||
minimumLogLevel: LogLevel.INFO,
|
||||
maxFileSizeMB: 10
|
||||
};
|
||||
32
sync-client/src/file-operations.ts
Normal file
32
sync-client/src/file-operations.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import type { RelativePath } from "src/database/document-metadata";
|
||||
|
||||
export interface FileOperations {
|
||||
listAllFiles: () => Promise<RelativePath[]>;
|
||||
|
||||
read: (path: RelativePath) => Promise<Uint8Array>;
|
||||
|
||||
getFileSize: (path: RelativePath) => Promise<number>;
|
||||
|
||||
exists: (path: RelativePath) => Promise<boolean>;
|
||||
|
||||
getModificationTime: (path: RelativePath) => Promise<Date>;
|
||||
|
||||
// Create and write the file if it doesn't exist. Otherwise, it has the same behavior as write.
|
||||
// All parent directories are created if they don't exist.
|
||||
create: (path: RelativePath, newContent: Uint8Array) => Promise<void>;
|
||||
|
||||
// Update the file at the given path.
|
||||
// If the file's content is different from `expectedContent`, the a 3-way merge is performed before writing.
|
||||
// If the file no longer exists, the file is not recreated and an empty array is returned.
|
||||
write: (
|
||||
path: RelativePath,
|
||||
expectedContent: Uint8Array,
|
||||
newContent: Uint8Array
|
||||
) => Promise<Uint8Array>;
|
||||
|
||||
remove: (path: RelativePath) => Promise<void>;
|
||||
|
||||
move: (oldPath: RelativePath, newPath: RelativePath) => Promise<void>;
|
||||
|
||||
isFileEligibleForSync: (path: RelativePath) => boolean;
|
||||
}
|
||||
295
sync-client/src/services/sync-service.ts
Normal file
295
sync-client/src/services/sync-service.ts
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
import type { Client } from "openapi-fetch";
|
||||
import createClient from "openapi-fetch";
|
||||
import type { components, paths } from "./types"; // Generated by openapi-typescript
|
||||
import type { Database } from "../database/database";
|
||||
import type { SyncSettings } from "../database/sync-settings";
|
||||
import type {
|
||||
DocumentId,
|
||||
RelativePath,
|
||||
VaultUpdateId
|
||||
} from "src/database/document-metadata";
|
||||
import { Logger } from "src/tracing/logger";
|
||||
import { retriedFetch } from "src/utils/retried-fetch";
|
||||
|
||||
export interface CheckConnectionResult {
|
||||
isSuccessful: boolean;
|
||||
message: string;
|
||||
}
|
||||
export class SyncService {
|
||||
private client: Client<paths>;
|
||||
private clientWithoutRetries: Client<paths>;
|
||||
|
||||
public constructor(private readonly database: Database) {
|
||||
this.createClient(database.getSettings());
|
||||
|
||||
database.addOnSettingsChangeHandlers((s) => {
|
||||
this.createClient(s);
|
||||
});
|
||||
}
|
||||
|
||||
private static formatError(
|
||||
error: components["schemas"]["SerializedError"]
|
||||
): string {
|
||||
let result = error.message;
|
||||
if (error.causes.length > 0) {
|
||||
const causes = error.causes.join(", ");
|
||||
result += ` caused by: ${causes}`;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async ping(): Promise<components["schemas"]["PingResponse"]> {
|
||||
const response = await this.clientWithoutRetries.GET("/ping", {
|
||||
params: {
|
||||
header: {
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Logger.getInstance().debug(
|
||||
`Ping response: ${JSON.stringify(response.data)}`
|
||||
);
|
||||
|
||||
if (!response.data) {
|
||||
throw new Error(
|
||||
`Failed to ping server: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
public async create({
|
||||
relativePath,
|
||||
contentBytes,
|
||||
createdDate
|
||||
}: {
|
||||
relativePath: RelativePath;
|
||||
contentBytes: Uint8Array;
|
||||
createdDate: Date;
|
||||
}): Promise<components["schemas"]["DocumentUpdateResponse"]> {
|
||||
const formData = new FormData();
|
||||
formData.append("relative_path", relativePath);
|
||||
formData.append("created_date", createdDate.toISOString());
|
||||
formData.append("content", new Blob([contentBytes]));
|
||||
|
||||
const response = await this.client.POST(
|
||||
"/vaults/{vault_id}/documents",
|
||||
{
|
||||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
},
|
||||
// eslint-disable-next-line
|
||||
body: formData as any // FormData is not supported by openapi-fetch
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.data) {
|
||||
throw new Error(
|
||||
`Failed to create document: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
Logger.getInstance().debug(
|
||||
`Created document ${JSON.stringify(response.data)} with id ${
|
||||
response.data.documentId
|
||||
}`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
public async put({
|
||||
parentVersionId,
|
||||
documentId,
|
||||
relativePath,
|
||||
contentBytes,
|
||||
createdDate
|
||||
}: {
|
||||
parentVersionId: VaultUpdateId;
|
||||
documentId: DocumentId;
|
||||
relativePath: RelativePath;
|
||||
contentBytes: Uint8Array;
|
||||
createdDate: Date;
|
||||
}): Promise<components["schemas"]["DocumentUpdateResponse"]> {
|
||||
const formData = new FormData();
|
||||
formData.append("parent_version_id", parentVersionId.toString());
|
||||
formData.append("created_date", createdDate.toISOString());
|
||||
formData.append("relative_path", relativePath);
|
||||
formData.append("content", new Blob([contentBytes]));
|
||||
|
||||
const response = await this.client.PUT(
|
||||
"/vaults/{vault_id}/documents/{document_id}",
|
||||
{
|
||||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName,
|
||||
document_id: documentId
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
},
|
||||
// eslint-disable-next-line
|
||||
body: formData as any // FormData is not supported by openapi-fetch
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.data) {
|
||||
throw new Error(
|
||||
`Failed to update document: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
Logger.getInstance().debug(
|
||||
`Updated document ${JSON.stringify(response.data)} with id ${
|
||||
response.data.documentId
|
||||
}`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
public async delete({
|
||||
documentId,
|
||||
relativePath,
|
||||
createdDate
|
||||
}: {
|
||||
documentId: DocumentId;
|
||||
relativePath: RelativePath;
|
||||
createdDate: Date;
|
||||
}): Promise<void> {
|
||||
const response = await this.client.DELETE(
|
||||
"/vaults/{vault_id}/documents/{document_id}",
|
||||
{
|
||||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName,
|
||||
document_id: documentId
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
},
|
||||
body: {
|
||||
createdDate: createdDate.toISOString(),
|
||||
relativePath
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (response.error) {
|
||||
throw new Error(`Failed to delete document`);
|
||||
}
|
||||
|
||||
Logger.getInstance().debug(
|
||||
`Deleted document ${relativePath} with id ${documentId}`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
public async get({
|
||||
documentId
|
||||
}: {
|
||||
documentId: DocumentId;
|
||||
}): Promise<components["schemas"]["DocumentVersion"]> {
|
||||
const response = await this.client.GET(
|
||||
"/vaults/{vault_id}/documents/{document_id}",
|
||||
{
|
||||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName,
|
||||
document_id: documentId
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.data) {
|
||||
throw new Error(
|
||||
`Failed to get document: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
Logger.getInstance().debug(
|
||||
`Get document ${response.data.relativePath} with id ${response.data.documentId}`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
public async getAll(
|
||||
since?: VaultUpdateId
|
||||
): Promise<components["schemas"]["FetchLatestDocumentsResponse"]> {
|
||||
const response = await this.client.GET("/vaults/{vault_id}/documents", {
|
||||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
},
|
||||
query: {
|
||||
since_update_id: since
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const { error } = response;
|
||||
if (error) {
|
||||
throw new Error(
|
||||
`Failed to get documents: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
Logger.getInstance().debug(
|
||||
`Got ${response.data.latestDocuments.length} document metadata`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
public async checkConnection(): Promise<CheckConnectionResult> {
|
||||
try {
|
||||
const result = await this.ping();
|
||||
if (result.isAuthenticated) {
|
||||
return {
|
||||
isSuccessful: true,
|
||||
message: `Successfully connected to server (version: ${result.serverVersion}) and authenticated.`
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
isSuccessful: false,
|
||||
message: `Successfully connected to server (version: ${result.serverVersion}) but failed to authenticate.`
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
isSuccessful: false,
|
||||
message: `Failed to connect to server: ${e}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private createClient(settings: SyncSettings): void {
|
||||
this.client = createClient<paths>({
|
||||
baseUrl: settings.remoteUri,
|
||||
fetch: retriedFetch
|
||||
});
|
||||
|
||||
this.clientWithoutRetries = createClient<paths>({
|
||||
baseUrl: settings.remoteUri
|
||||
});
|
||||
}
|
||||
}
|
||||
612
sync-client/src/services/types.ts
Normal file
612
sync-client/src/services/types.ts
Normal file
|
|
@ -0,0 +1,612 @@
|
|||
/**
|
||||
* This file was auto-generated by openapi-typescript.
|
||||
* Do not make direct changes to the file.
|
||||
*/
|
||||
|
||||
export interface paths {
|
||||
"/ping": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
get: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: {
|
||||
authorization?: string;
|
||||
};
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["PingResponse"];
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
put?: never;
|
||||
post?: never;
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/vaults/{vault_id}/documents": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
get: {
|
||||
parameters: {
|
||||
query?: {
|
||||
since_update_id?: number | null;
|
||||
};
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
vault_id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["FetchLatestDocumentsResponse"];
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
put?: never;
|
||||
post: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
vault_id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"multipart/form-data": components["schemas"]["CreateDocumentVersionMultipart"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["DocumentUpdateResponse"];
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/vaults/{vault_id}/documents/json": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
get?: never;
|
||||
put?: never;
|
||||
post: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
vault_id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["CreateDocumentVersion"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["DocumentUpdateResponse"];
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/vaults/{vault_id}/documents/{document_id}": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
get: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["DocumentVersion"];
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
put: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"multipart/form-data": components["schemas"]["UpdateDocumentVersionMultipart"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["DocumentUpdateResponse"];
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
post?: never;
|
||||
delete: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["DeleteDocumentVersion"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description no content */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content?: never;
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/vaults/{vault_id}/documents/{document_id}/json": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
get?: never;
|
||||
put: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["UpdateDocumentVersion"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["DocumentUpdateResponse"];
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
post?: never;
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/vaults/{vault_id}/documents/{document_id}/versions/{version_id}": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
get?: never;
|
||||
put: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
vault_update_id: number;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["DocumentVersion"];
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
post?: never;
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/vaults/{vault_id}/documents/{document_id}/versions/{version_id}/content": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
get?: never;
|
||||
put: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header: {
|
||||
authorization: string;
|
||||
};
|
||||
path: {
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
vault_update_id: number;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description byte stream */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/octet-stream": unknown;
|
||||
};
|
||||
};
|
||||
default: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SerializedError"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
post?: never;
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
}
|
||||
export type webhooks = Record<string, never>;
|
||||
export interface components {
|
||||
schemas: {
|
||||
Array_of_uint8: number[];
|
||||
CreateDocumentVersion: {
|
||||
contentBase64: string;
|
||||
/** Format: date-time */
|
||||
createdDate: string;
|
||||
relativePath: string;
|
||||
};
|
||||
CreateDocumentVersionMultipart: {
|
||||
content: components["schemas"]["Array_of_uint8"];
|
||||
/** Format: date-time */
|
||||
created_date: string;
|
||||
relative_path: string;
|
||||
};
|
||||
DeleteDocumentVersion: {
|
||||
/** Format: date-time */
|
||||
createdDate: string;
|
||||
relativePath: string;
|
||||
};
|
||||
/** @description Response to a create/update document request. */
|
||||
DocumentUpdateResponse:
|
||||
| {
|
||||
/** Format: date-time */
|
||||
createdDate: string;
|
||||
/** Format: uuid */
|
||||
documentId: string;
|
||||
isDeleted: boolean;
|
||||
relativePath: string;
|
||||
/** @enum {string} */
|
||||
type: "FastForwardUpdate";
|
||||
/** Format: date-time */
|
||||
updatedDate: string;
|
||||
vaultId: string;
|
||||
/** Format: int64 */
|
||||
vaultUpdateId: number;
|
||||
}
|
||||
| {
|
||||
contentBase64: string;
|
||||
/** Format: date-time */
|
||||
createdDate: string;
|
||||
/** Format: uuid */
|
||||
documentId: string;
|
||||
isDeleted: boolean;
|
||||
relativePath: string;
|
||||
/** @enum {string} */
|
||||
type: "MergingUpdate";
|
||||
/** Format: date-time */
|
||||
updatedDate: string;
|
||||
vaultId: string;
|
||||
/** Format: int64 */
|
||||
vaultUpdateId: number;
|
||||
};
|
||||
DocumentVersion: {
|
||||
contentBase64: string;
|
||||
/** Format: date-time */
|
||||
createdDate: string;
|
||||
/** Format: uuid */
|
||||
documentId: string;
|
||||
isDeleted: boolean;
|
||||
relativePath: string;
|
||||
/** Format: date-time */
|
||||
updatedDate: string;
|
||||
vaultId: string;
|
||||
/** Format: int64 */
|
||||
vaultUpdateId: number;
|
||||
};
|
||||
DocumentVersionWithoutContent: {
|
||||
/** Format: date-time */
|
||||
createdDate: string;
|
||||
/** Format: uuid */
|
||||
documentId: string;
|
||||
isDeleted: boolean;
|
||||
relativePath: string;
|
||||
/** Format: date-time */
|
||||
updatedDate: string;
|
||||
vaultId: string;
|
||||
/** Format: int64 */
|
||||
vaultUpdateId: number;
|
||||
};
|
||||
/** @description Response to a fetch latest documents request. */
|
||||
FetchLatestDocumentsResponse: {
|
||||
/**
|
||||
* Format: int64
|
||||
* @description The update ID of the latest document in the response.
|
||||
*/
|
||||
lastUpdateId: number;
|
||||
latestDocuments: components["schemas"]["DocumentVersionWithoutContent"][];
|
||||
};
|
||||
PathParams: {
|
||||
vault_id: string;
|
||||
};
|
||||
PathParams2: {
|
||||
vault_id: string;
|
||||
};
|
||||
PathParams3: {
|
||||
/** Format: uuid */
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
};
|
||||
PathParams4: {
|
||||
/** Format: uuid */
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
};
|
||||
PathParams5: {
|
||||
/** Format: uuid */
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
/** Format: int64 */
|
||||
vault_update_id: number;
|
||||
};
|
||||
PathParams6: {
|
||||
/** Format: uuid */
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
/** Format: int64 */
|
||||
vault_update_id: number;
|
||||
};
|
||||
PathParams7: {
|
||||
/** Format: uuid */
|
||||
document_id: string;
|
||||
vault_id: string;
|
||||
};
|
||||
/** @description Response to a ping request. */
|
||||
PingResponse: {
|
||||
/** @description Whether the client is authenticated based on the sent Authorization header. */
|
||||
isAuthenticated: boolean;
|
||||
/** @description Semantic version of the server. */
|
||||
serverVersion: string;
|
||||
};
|
||||
QueryParams: {
|
||||
/** Format: int64 */
|
||||
since_update_id?: number | null;
|
||||
};
|
||||
SerializedError: {
|
||||
causes: string[];
|
||||
message: string;
|
||||
};
|
||||
UpdateDocumentVersion: {
|
||||
contentBase64: string;
|
||||
/** Format: date-time */
|
||||
createdDate: string;
|
||||
/** Format: int64 */
|
||||
parentVersionId: number;
|
||||
relativePath: string;
|
||||
};
|
||||
UpdateDocumentVersionMultipart: {
|
||||
content: components["schemas"]["Array_of_uint8"];
|
||||
/** Format: date-time */
|
||||
createdDate: string;
|
||||
/** Format: int64 */
|
||||
parentVersionId: number;
|
||||
relativePath: string;
|
||||
};
|
||||
};
|
||||
responses: never;
|
||||
parameters: never;
|
||||
requestBodies: never;
|
||||
headers: never;
|
||||
pathItems: never;
|
||||
}
|
||||
export type $defs = Record<string, never>;
|
||||
export type operations = Record<string, never>;
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
import type { Database } from "../database/database";
|
||||
import type { SyncService } from "src/services/sync-service";
|
||||
import { Logger } from "src/tracing/logger";
|
||||
import type { Syncer } from "./syncer";
|
||||
|
||||
let isRunning = false;
|
||||
|
||||
export async function applyRemoteChangesLocally({
|
||||
database,
|
||||
syncService,
|
||||
syncer
|
||||
}: {
|
||||
database: Database;
|
||||
syncService: SyncService;
|
||||
syncer: Syncer;
|
||||
}): Promise<void> {
|
||||
if (!database.getSettings().isSyncEnabled) {
|
||||
Logger.getInstance().debug(
|
||||
`Syncing is disabled, not fetching remote changes`
|
||||
);
|
||||
return;
|
||||
} else if (isRunning) {
|
||||
Logger.getInstance().debug(
|
||||
"Applying remote changes locally is already in progress, skipping invocation"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
isRunning = true;
|
||||
|
||||
try {
|
||||
const remote = await syncService.getAll(database.getLastSeenUpdateId());
|
||||
|
||||
if (remote.latestDocuments.length === 0) {
|
||||
Logger.getInstance().debug("No remote changes to apply");
|
||||
return;
|
||||
}
|
||||
|
||||
Logger.getInstance().info("Applying remote changes locally");
|
||||
|
||||
await Promise.all(
|
||||
remote.latestDocuments.map(async (remoteDocument) =>
|
||||
syncer.syncRemotelyUpdatedFile(remoteDocument)
|
||||
)
|
||||
);
|
||||
|
||||
const lastSeenUpdateId = database.getLastSeenUpdateId();
|
||||
if (
|
||||
lastSeenUpdateId === undefined ||
|
||||
remote.lastUpdateId > lastSeenUpdateId
|
||||
) {
|
||||
await database.setLastSeenUpdateId(remote.lastUpdateId);
|
||||
}
|
||||
} catch (e) {
|
||||
Logger.getInstance().error(
|
||||
`Failed to apply remote changes locally: ${e}`
|
||||
);
|
||||
} finally {
|
||||
isRunning = false;
|
||||
}
|
||||
}
|
||||
79
sync-client/src/sync-operations/document-lock.test.ts
Normal file
79
sync-client/src/sync-operations/document-lock.test.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import {
|
||||
tryLockDocument,
|
||||
waitForDocumentLock,
|
||||
unlockDocument
|
||||
} from "./document-lock";
|
||||
import type { RelativePath } from "src/database/document-metadata";
|
||||
|
||||
describe("Document Lock Operations", () => {
|
||||
const testPath: RelativePath = "test/document/path";
|
||||
|
||||
beforeEach(() => {
|
||||
// Reset the state before each test
|
||||
(global as any).locked = new Set<RelativePath>();
|
||||
(global as any).waiters = new Map<RelativePath, (() => void)[]>();
|
||||
});
|
||||
|
||||
test("should lock a document successfully", () => {
|
||||
const result = tryLockDocument(testPath);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
test("should not lock a document that is already locked", () => {
|
||||
tryLockDocument(testPath);
|
||||
const result = tryLockDocument(testPath);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
test("should unlock a locked document", () => {
|
||||
tryLockDocument(testPath);
|
||||
unlockDocument(testPath);
|
||||
const result = tryLockDocument(testPath);
|
||||
expect(result).toBe(true);
|
||||
unlockDocument(testPath);
|
||||
});
|
||||
|
||||
test("should throw an error when unlocking a document that is not locked", () => {
|
||||
expect(() => {
|
||||
unlockDocument(testPath);
|
||||
}).toThrow(`Document ${testPath} is not locked, cannot unlock`);
|
||||
});
|
||||
|
||||
test("should wait for a document lock and resolve when unlocked", async () => {
|
||||
tryLockDocument(testPath);
|
||||
|
||||
let resolved = false;
|
||||
const waitPromise = waitForDocumentLock(testPath).then(() => {
|
||||
resolved = true;
|
||||
});
|
||||
|
||||
unlockDocument(testPath);
|
||||
await waitPromise;
|
||||
|
||||
expect(resolved).toBe(true);
|
||||
});
|
||||
|
||||
test("should resolve multiple waiters in FIFO order", async () => {
|
||||
tryLockDocument(testPath);
|
||||
|
||||
let firstResolved = false;
|
||||
let secondResolved = false;
|
||||
|
||||
const firstWaitPromise = waitForDocumentLock(testPath).then(() => {
|
||||
firstResolved = true;
|
||||
});
|
||||
|
||||
const secondWaitPromise = waitForDocumentLock(testPath).then(() => {
|
||||
secondResolved = true;
|
||||
});
|
||||
|
||||
unlockDocument(testPath);
|
||||
await firstWaitPromise;
|
||||
expect(firstResolved).toBe(true);
|
||||
expect(secondResolved).toBe(false);
|
||||
|
||||
unlockDocument(testPath);
|
||||
await secondWaitPromise;
|
||||
expect(secondResolved).toBe(true);
|
||||
});
|
||||
});
|
||||
48
sync-client/src/sync-operations/document-lock.ts
Normal file
48
sync-client/src/sync-operations/document-lock.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import type { RelativePath } from "src/database/document-metadata";
|
||||
|
||||
const locked = new Set<RelativePath>();
|
||||
const waiters = new Map<RelativePath, (() => void)[]>();
|
||||
|
||||
export function tryLockDocument(relativePath: RelativePath): boolean {
|
||||
if (locked.has(relativePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
locked.add(relativePath);
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function waitForDocumentLock(
|
||||
relativePath: RelativePath
|
||||
): Promise<void> {
|
||||
if (tryLockDocument(relativePath)) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let waiting = waiters.get(relativePath);
|
||||
if (!waiting) {
|
||||
waiting = [];
|
||||
waiters.set(relativePath, waiting);
|
||||
}
|
||||
|
||||
waiting.push(resolve);
|
||||
});
|
||||
}
|
||||
|
||||
export function unlockDocument(relativePath: RelativePath): void {
|
||||
if (!locked.has(relativePath)) {
|
||||
throw new Error(
|
||||
`Document ${relativePath} is not locked, cannot unlock`
|
||||
);
|
||||
}
|
||||
|
||||
// Remove the first element to ensure FIFO unblocking order
|
||||
const nextWaiting = waiters.get(relativePath)?.shift();
|
||||
|
||||
if (nextWaiting) {
|
||||
nextWaiting();
|
||||
} else {
|
||||
locked.delete(relativePath);
|
||||
}
|
||||
}
|
||||
98
sync-client/src/tracing/logger.ts
Normal file
98
sync-client/src/tracing/logger.ts
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
export enum LogLevel {
|
||||
DEBUG = "DEBUG",
|
||||
INFO = "INFO",
|
||||
WARNING = "WARNING",
|
||||
ERROR = "ERROR"
|
||||
}
|
||||
|
||||
const LOG_LEVEL_ORDER = {
|
||||
[LogLevel.DEBUG]: 0,
|
||||
[LogLevel.INFO]: 1,
|
||||
[LogLevel.WARNING]: 2,
|
||||
[LogLevel.ERROR]: 3
|
||||
};
|
||||
|
||||
class LogLine {
|
||||
public timestamp = new Date();
|
||||
public constructor(
|
||||
public level: LogLevel,
|
||||
public message: string
|
||||
) {}
|
||||
}
|
||||
|
||||
export class Logger {
|
||||
private static readonly MAX_MESSAGES = 1000;
|
||||
|
||||
private static instance: Logger | null = null;
|
||||
private readonly messages: LogLine[] = [];
|
||||
|
||||
private readonly onMessageListeners: ((
|
||||
status: LogLine | undefined
|
||||
) => void)[] = [];
|
||||
|
||||
private constructor() {} // eslint-disable-line @typescript-eslint/no-empty-function
|
||||
|
||||
public static getInstance(): Logger {
|
||||
if (!Logger.instance) {
|
||||
Logger.instance = new Logger();
|
||||
}
|
||||
return Logger.instance;
|
||||
}
|
||||
|
||||
public debug(message: string): void {
|
||||
console.debug(message);
|
||||
this.pushMessage(message, LogLevel.DEBUG);
|
||||
}
|
||||
|
||||
public info(message: string): void {
|
||||
console.info(message);
|
||||
|
||||
this.pushMessage(message, LogLevel.INFO);
|
||||
}
|
||||
|
||||
public warn(message: string): void {
|
||||
console.warn(message);
|
||||
|
||||
this.pushMessage(message, LogLevel.WARNING);
|
||||
}
|
||||
|
||||
public error(message: string): void {
|
||||
console.error(message);
|
||||
|
||||
this.pushMessage(message, LogLevel.ERROR);
|
||||
}
|
||||
|
||||
public getMessages(mininumSeverity: LogLevel): LogLine[] {
|
||||
return this.messages.filter(
|
||||
(message) =>
|
||||
LOG_LEVEL_ORDER[message.level] >=
|
||||
LOG_LEVEL_ORDER[mininumSeverity]
|
||||
);
|
||||
}
|
||||
|
||||
public addOnMessageListener(
|
||||
listener: (message: LogLine | undefined) => void
|
||||
): void {
|
||||
this.onMessageListeners.push(listener);
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.messages.length = 0;
|
||||
this.onMessageListeners.forEach((listener) => {
|
||||
listener(undefined);
|
||||
});
|
||||
}
|
||||
|
||||
private pushMessage(message: string, level: LogLevel): void {
|
||||
const logLine = new LogLine(level, message);
|
||||
this.messages.push(logLine);
|
||||
|
||||
while (this.messages.length > Logger.MAX_MESSAGES) {
|
||||
this.messages.shift();
|
||||
}
|
||||
|
||||
this.onMessageListeners.forEach((listener) => {
|
||||
listener(logLine);
|
||||
});
|
||||
}
|
||||
}
|
||||
103
sync-client/src/tracing/sync-history.ts
Normal file
103
sync-client/src/tracing/sync-history.ts
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
import type { RelativePath } from "src/database/document-metadata";
|
||||
import { Logger } from "./logger";
|
||||
|
||||
export interface CommonHistoryEntry {
|
||||
status: SyncStatus;
|
||||
relativePath: RelativePath;
|
||||
message: string;
|
||||
type?: SyncType;
|
||||
source?: SyncSource;
|
||||
}
|
||||
|
||||
export enum SyncType {
|
||||
CREATE = "CREATE",
|
||||
UPDATE = "UPDATE",
|
||||
DELETE = "DELETE"
|
||||
}
|
||||
|
||||
export enum SyncSource {
|
||||
PUSH = "PUSH",
|
||||
PULL = "PULL"
|
||||
}
|
||||
|
||||
export enum SyncStatus {
|
||||
NO_OP = "NO_OP",
|
||||
SUCCESS = "SUCCESS",
|
||||
ERROR = "ERROR"
|
||||
}
|
||||
|
||||
export type HistoryEntry = CommonHistoryEntry & { timestamp: Date };
|
||||
|
||||
export interface HistoryStats {
|
||||
success: number;
|
||||
error: number;
|
||||
}
|
||||
|
||||
export class SyncHistory {
|
||||
private static readonly MAX_ENTRIES = 5000;
|
||||
|
||||
private readonly entries: HistoryEntry[] = [];
|
||||
|
||||
private readonly syncHistoryUpdateListeners: ((
|
||||
status: HistoryStats
|
||||
) => void)[] = [];
|
||||
|
||||
private status: HistoryStats = {
|
||||
success: 0,
|
||||
error: 0
|
||||
};
|
||||
|
||||
public getEntries(): HistoryEntry[] {
|
||||
return [...this.entries];
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.entries.length = 0;
|
||||
this.status = {
|
||||
success: 0,
|
||||
error: 0
|
||||
};
|
||||
this.syncHistoryUpdateListeners.forEach((listener) => {
|
||||
listener(this.status);
|
||||
});
|
||||
}
|
||||
|
||||
public addSyncHistoryUpdateListener(
|
||||
listener: (stats: HistoryStats) => void
|
||||
): void {
|
||||
this.syncHistoryUpdateListeners.push(listener);
|
||||
listener({ ...this.status });
|
||||
}
|
||||
|
||||
public addHistoryEntry(entry: CommonHistoryEntry): void {
|
||||
const historyEntry = {
|
||||
...entry,
|
||||
timestamp: new Date()
|
||||
};
|
||||
this.entries.push(historyEntry);
|
||||
|
||||
if (entry.status === SyncStatus.SUCCESS) {
|
||||
this.status.success++;
|
||||
Logger.getInstance().info(
|
||||
`History entry: ${entry.relativePath} - ${entry.message}`
|
||||
);
|
||||
} else if (entry.status === SyncStatus.ERROR) {
|
||||
this.status.error++;
|
||||
Logger.getInstance().error(
|
||||
`Error syncing file: ${entry.relativePath} - ${entry.message}`
|
||||
);
|
||||
} else {
|
||||
Logger.getInstance().debug(
|
||||
`No-op syncing file: ${entry.relativePath} - ${entry.message}`
|
||||
);
|
||||
}
|
||||
|
||||
this.syncHistoryUpdateListeners.forEach((listener) => {
|
||||
listener(this.status);
|
||||
});
|
||||
|
||||
if (this.entries.length > SyncHistory.MAX_ENTRIES) {
|
||||
this.entries.shift();
|
||||
}
|
||||
}
|
||||
}
|
||||
18
sync-client/src/utils/deserialize.test.ts
Normal file
18
sync-client/src/utils/deserialize.test.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import init, { base64ToBytes } from "sync_lib";
|
||||
import fs from "fs";
|
||||
|
||||
describe("deserialize", () => {
|
||||
it("should serialize a Uint8Array to a base64 string", async () => {
|
||||
const wasmBin = fs.readFileSync(
|
||||
"../backend/sync_lib/pkg/sync_lib_bg.wasm"
|
||||
);
|
||||
await init({ module_or_path: wasmBin });
|
||||
|
||||
const base64 = "SGVsbG8=";
|
||||
const jsResult = base64ToBytes(base64);
|
||||
const expected = new Uint8Array([72, 101, 108, 108, 111]);
|
||||
expect(jsResult).toEqual(expected);
|
||||
const rustResult = base64ToBytes(base64);
|
||||
expect(jsResult).toEqual(rustResult);
|
||||
});
|
||||
});
|
||||
5
sync-client/src/utils/deserialize.ts
Normal file
5
sync-client/src/utils/deserialize.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { base64ToBytes } from "byte-base64";
|
||||
|
||||
export function deserialize(data: string): Uint8Array {
|
||||
return base64ToBytes(data);
|
||||
}
|
||||
12
sync-client/src/utils/hash.ts
Normal file
12
sync-client/src/utils/hash.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
|
||||
export function hash(content: Uint8Array): string {
|
||||
let result = 0;
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
||||
for (let i = 0; i < content.length; i++) {
|
||||
result = (result << 5) - result + content[i];
|
||||
result |= 0; // Convert to 32bit integer
|
||||
}
|
||||
return Math.abs(result).toString(16);
|
||||
}
|
||||
|
||||
export const EMPTY_HASH = hash(new Uint8Array(0));
|
||||
27
sync-client/src/utils/is-equal-bytes.test.ts
Normal file
27
sync-client/src/utils/is-equal-bytes.test.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { isEqualBytes } from "./is-equal-bytes";
|
||||
|
||||
describe("isEqualBytes", () => {
|
||||
it("should return true for equal byte arrays", () => {
|
||||
const bytes1 = new Uint8Array([1, 2, 3, 4]);
|
||||
const bytes2 = new Uint8Array([1, 2, 3, 4]);
|
||||
expect(isEqualBytes(bytes1, bytes2)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for byte arrays of different lengths", () => {
|
||||
const bytes1 = new Uint8Array([1, 2, 3, 4]);
|
||||
const bytes2 = new Uint8Array([1, 2, 3]);
|
||||
expect(isEqualBytes(bytes1, bytes2)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return true for empty byte arrays", () => {
|
||||
const bytes1 = new Uint8Array([]);
|
||||
const bytes2 = new Uint8Array([]);
|
||||
expect(isEqualBytes(bytes1, bytes2)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for byte arrays with same length but different content", () => {
|
||||
const bytes1 = new Uint8Array([1, 2, 3, 4]);
|
||||
const bytes2 = new Uint8Array([4, 3, 2, 1]);
|
||||
expect(isEqualBytes(bytes1, bytes2)).toBe(false);
|
||||
});
|
||||
});
|
||||
13
sync-client/src/utils/is-equal-bytes.ts
Normal file
13
sync-client/src/utils/is-equal-bytes.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
export function isEqualBytes(bytes1: Uint8Array, bytes2: Uint8Array): boolean {
|
||||
if (bytes1.length !== bytes2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < bytes1.length; i++) {
|
||||
if (bytes1[i] !== bytes2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
36
sync-client/src/utils/retried-fetch.ts
Normal file
36
sync-client/src/utils/retried-fetch.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import * as fetchRetryFactory from "fetch-retry";
|
||||
import type { RequestInitRetryParams } from "fetch-retry";
|
||||
import { Logger } from "src/tracing/logger";
|
||||
|
||||
const fetchWithRetry = fetchRetryFactory.default(fetch);
|
||||
|
||||
function getUrlFromInput(input: RequestInfo | URL): string {
|
||||
if (input instanceof URL) {
|
||||
return input.href;
|
||||
}
|
||||
if (typeof input === "string") {
|
||||
return input;
|
||||
}
|
||||
return input.url;
|
||||
}
|
||||
|
||||
export async function retriedFetch(
|
||||
input: RequestInfo | URL,
|
||||
init: RequestInitRetryParams<typeof fetch> = {}
|
||||
): Promise<Response> {
|
||||
return fetchWithRetry(input, {
|
||||
retryOn: function (attempt, error, response) {
|
||||
if (error !== null || !response || response.status >= 500) {
|
||||
Logger.getInstance().warn(
|
||||
`Retrying fetch for ${getUrlFromInput(input)}, attempt ${attempt}`
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
retries: 6,
|
||||
retryDelay: (attempt) => Math.pow(1.5, attempt) * 500,
|
||||
...init
|
||||
});
|
||||
}
|
||||
18
sync-client/src/utils/serialize.test.ts
Normal file
18
sync-client/src/utils/serialize.test.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { serialize } from "./serialize";
|
||||
import init, { bytesToBase64 } from "sync_lib";
|
||||
import fs from "fs";
|
||||
|
||||
describe("serialize", () => {
|
||||
it("should serialize a Uint8Array to a base64 string", async () => {
|
||||
const wasmBin = fs.readFileSync(
|
||||
"../backend/sync_lib/pkg/sync_lib_bg.wasm"
|
||||
);
|
||||
await init({ module_or_path: wasmBin });
|
||||
|
||||
const data = new Uint8Array([72, 101, 108, 108, 111]);
|
||||
const jsResult = serialize(data);
|
||||
const rustResult = bytesToBase64(data);
|
||||
expect(rustResult).toBe("SGVsbG8=");
|
||||
expect(jsResult).toBe(rustResult);
|
||||
});
|
||||
});
|
||||
5
sync-client/src/utils/serialize.ts
Normal file
5
sync-client/src/utils/serialize.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { bytesToBase64 } from "byte-base64";
|
||||
|
||||
export function serialize(data: Uint8Array): string {
|
||||
return bytesToBase64(data);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue