This commit is contained in:
Andras Schmelczer 2025-03-15 14:12:04 +00:00
parent b5260e97e9
commit 9ad54eff7a
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
21 changed files with 76 additions and 75 deletions

View file

@ -1,6 +1,9 @@
import type { Logger } from "../tracing/logger";
import type { RelativePath } from "../persistence/database";
// Manages locks on documents to prevent concurrent modifications
// allowing the client's FileOperations implementation to be simpler.
// Locks are granted in a first-in-first-out order.
export class DocumentLocks {
private readonly locked = new Set<RelativePath>();
private readonly waiters = new Map<RelativePath, (() => void)[]>();

View file

@ -1,4 +1,3 @@
import type { FileSystemOperations } from "sync-client";
import type {
Database,
DocumentRecord,
@ -7,6 +6,7 @@ import type {
import { FileOperations } from "./file-operations";
import { Logger } from "../tracing/logger";
import { assertSetContainsExactly } from "../utils/assert-set-contains-exactly";
import type { FileSystemOperations } from "./filesystem-operations";
describe("File operations", () => {
class MockDatabase {

View file

@ -1,6 +1,6 @@
import type { Logger } from "src/tracing/logger";
import type { Logger } from "../tracing/logger";
import type { FileSystemOperations } from "./filesystem-operations";
import type { Database, RelativePath } from "src/persistence/database";
import type { Database, RelativePath } from "../persistence/database";
import { isBinary, isFileTypeMergable, mergeText } from "sync_lib";
import {
FileNotFoundError,

View file

@ -1,4 +1,4 @@
import type { RelativePath } from "src/persistence/database";
import type { RelativePath } from "../persistence/database";
export interface FileSystemOperations {
listAllFiles: () => Promise<RelativePath[]>;

View file

@ -12,7 +12,8 @@ export class FileNotFoundError extends Error {
// Decorate FileSystemOperations replacing errors with FileNotFoundError
// if the accessed file doesn't exist. It also ensures that there's only
// ever a single request in-flight for any one file.
// ever a single request in-flight for any one file through the use of
// DocumentLocks.
export class SafeFileSystemOperations implements FileSystemOperations {
private readonly locks: DocumentLocks;
@ -24,7 +25,6 @@ export class SafeFileSystemOperations implements FileSystemOperations {
}
public async listAllFiles(): Promise<RelativePath[]> {
this.logger.debug("Listing all files");
return this.fs.listAllFiles();
}

View file

@ -216,7 +216,7 @@ export class Database {
relativePath: RelativePath,
promise: Promise<void>
): Promise<void> {
let entry = this.getLatestDocumentByRelativePath(relativePath);
const entry = this.getLatestDocumentByRelativePath(relativePath);
if (entry === undefined) {
throw new Error(
@ -238,7 +238,7 @@ export class Database {
relativePath: RelativePath,
promise: Promise<void>
): void {
let previousEntry = this.getLatestDocumentByRelativePath(relativePath);
const previousEntry = this.getLatestDocumentByRelativePath(relativePath);
const entry = {
relativePath,
@ -300,7 +300,7 @@ export class Database {
({ identity }) => identity !== oldDocument.identity
);
let newDocument = this.getLatestDocumentByRelativePath(newRelativePath);
const newDocument = this.getLatestDocumentByRelativePath(newRelativePath);
if (newDocument !== undefined && !newDocument.isDeleted) {
throw new Error(
`Document already exists at new location: ${newRelativePath}`

View file

@ -1,5 +1,5 @@
import type { Logger } from "src/tracing/logger";
import { LogLevel } from "src/tracing/logger";
import type { Logger } from "../tracing/logger";
import { LogLevel } from "../tracing/logger";
export interface SyncSettings {
remoteUri: string;

View file

@ -1,5 +1,5 @@
import { Settings } from "../persistence/settings";
import { Logger } from "../tracing/logger";
import type { Settings } from "../persistence/settings";
import type { Logger } from "../tracing/logger";
import { createPromise } from "../utils/create-promise";
import { retriedFetchFactory } from "../utils/retried-fetch";

View file

@ -8,7 +8,7 @@ import type {
} from "../persistence/database";
import type { Logger } from "../tracing/logger";
import type { Settings } from "../persistence/settings";
import { ConnectedState } from "./connected-state";
import type { ConnectedState } from "./connected-state";
export interface CheckConnectionResult {
isSuccessful: boolean;

View file

@ -1,17 +1,17 @@
import type { Database, RelativePath } from "../persistence/database";
import type { SyncService } from "src/services/sync-service";
import type { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history";
import type { SyncService } from "../services/sync-service";
import type { Logger } from "../tracing/logger";
import type { SyncHistory } from "../tracing/sync-history";
import PQueue from "p-queue";
import { v4 as uuidv4 } from "uuid";
import { hash } from "src/utils/hash";
import type { components } from "src/services/types";
import type { Settings } from "src/persistence/settings";
import type { FileOperations } from "src/file-operations/file-operations";
import { findMatchingFile } from "src/utils/find-matching-file";
import { hash } from "../utils/hash";
import type { components } from "../services/types";
import type { Settings } from "../persistence/settings";
import type { FileOperations } from "../file-operations/file-operations";
import { findMatchingFile } from "../utils/find-matching-file";
import { UnrestrictedSyncer } from "./unrestricted-syncer";
import { FileNotFoundError } from "src/file-operations/safe-filesystem-operations";
import { createPromise } from "src/utils/create-promise";
import { FileNotFoundError } from "../file-operations/safe-filesystem-operations";
import { createPromise } from "../utils/create-promise";
export class Syncer {
private readonly remainingOperationsListeners: ((
@ -45,9 +45,9 @@ export class Syncer {
});
this.syncQueue.on("active", () =>
this.remainingOperationsListeners.forEach((listener) =>
listener(this.syncQueue.size)
)
{ this.remainingOperationsListeners.forEach((listener) =>
{ listener(this.syncQueue.size); }
); }
);
this.internalSyncer = new UnrestrictedSyncer(
@ -107,7 +107,7 @@ export class Syncer {
);
try {
await this.syncQueue.add(() =>
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncLocallyCreatedFile(
proposedDocumentId,
() => this.database.getDocumentByUpdatePromise(promise)
@ -261,7 +261,7 @@ export class Syncer {
public async reset(): Promise<void> {
this.syncQueue.clear();
await this.syncQueue.onEmpty();
this.remainingOperationsListeners.forEach((listener) => listener(0));
this.remainingOperationsListeners.forEach((listener) => { listener(0); });
this.internalSyncer.reset();
}
@ -297,7 +297,7 @@ export class Syncer {
private async syncRemotelyUpdatedFile(
remoteVersion: components["schemas"]["DocumentVersionWithoutContent"]
): Promise<void> {
let document = this.database.getDocumentByDocumentId(
const document = this.database.getDocumentByDocumentId(
remoteVersion.documentId
);

View file

@ -5,18 +5,18 @@ import type {
RelativePath
} from "../persistence/database";
import type { SyncService } from "src/services/sync-service";
import { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history";
import { SyncSource, SyncStatus, SyncType } from "src/tracing/sync-history";
import { EMPTY_HASH, hash } from "src/utils/hash";
import type { components } from "src/services/types";
import { deserialize } from "src/utils/deserialize";
import type { Settings } from "src/persistence/settings";
import type { FileOperations } from "src/file-operations/file-operations";
import { FileNotFoundError } from "src/file-operations/safe-filesystem-operations";
import type { SyncService } from "../services/sync-service";
import type { Logger } from "../tracing/logger";
import type { SyncHistory } from "../tracing/sync-history";
import { SyncSource, SyncStatus, SyncType } from "../tracing/sync-history";
import { EMPTY_HASH, hash } from "../utils/hash";
import type { components } from "../services/types";
import { deserialize } from "../utils/deserialize";
import type { Settings } from "../persistence/settings";
import type { FileOperations } from "../file-operations/file-operations";
import { FileNotFoundError } from "../file-operations/safe-filesystem-operations";
import { DocumentLocks } from "../file-operations/document-locks";
import { createPromise } from "src/utils/create-promise";
import { createPromise } from "../utils/create-promise";
export class UnrestrictedSyncer {
private readonly locks: DocumentLocks;
@ -289,7 +289,6 @@ export class UnrestrictedSyncer {
let localMetadata = getLatestDocument();
if (
localMetadata !== undefined &&
localMetadata?.metadata !== undefined
) {
// If the file exists locally, let's pretend the user has updated it
@ -352,11 +351,11 @@ export class UnrestrictedSyncer {
remoteVersion.relativePath,
contentBytes,
() =>
this.database.getNewResolvedDocumentByRelativePath(
{ this.database.getNewResolvedDocumentByRelativePath(
remoteVersion.documentId,
remoteVersion.relativePath,
promise
)
); }
);
const document =

View file

@ -1,4 +1,4 @@
import type { RelativePath } from "src/persistence/database";
import type { RelativePath } from "../persistence/database";
import type { Logger } from "./logger";
export interface CommonHistoryEntry {

View file

@ -1,6 +1,6 @@
import * as fetchRetryFactory from "fetch-retry";
import type { RequestInitRetryParams } from "fetch-retry";
import type { Logger } from "src/tracing/logger";
import type { Logger } from "../tracing/logger";
function getUrlFromInput(input: RequestInfo | URL): string {
if (input instanceof URL) {