import type { FileSystem } from '../models/file-system'; import type { FileChange, SyncApiFactory } from '../remote-api'; import { isOrgNoteConfigPath } from './config-path'; export type SyncApi = ReturnType; export type SyncStatus = | 'synced' | 'dirty' | 'uploading' | 'downloading' | 'error' | 'pending' | 'conflict'; export enum SyncOperationType { Upload = 'upload', Download = 'download', DeleteLocal = 'deleteLocal', DeleteRemote = 'deleteRemote', } export interface SyncedFile { mtime: number; size: number; contentHash?: string; version?: number; status: SyncStatus; syncedAt?: string; conflictPath?: string; errorMessage?: string; } export interface SyncStateData { files: Record; } export interface SyncState { get(): Promise; getFile(path: string): Promise; setFile(path: string, file: SyncedFile): Promise; setSyncedAt(paths: readonly string[], syncedAt: string): Promise; removeFile(path: string): Promise; clear(): Promise; } export interface LocalFile { path: string; mtime: number; size: number; contentHash?: string; } export type RemoteFile = Pick< FileChange, 'path' | 'version' | 'deleted' | 'updatedAt' | 'contentHash' >; export type UploadResult = | { status: 'ok'; version: number } | { status: 'conflict'; serverVersion: number }; export type DeleteResult = | { status: 'ok' } | { status: 'conflict'; serverVersion: number }; export interface SyncExecutor { upload: (file: LocalFile, expectedVersion?: number) => Promise; download: (file: RemoteFile) => Promise; fetchContent: (file: RemoteFile) => Promise; deleteLocal: (path: string) => Promise; deleteRemote: (path: string, expectedVersion: number) => Promise; } export interface SyncPlan { toUpload: LocalFile[]; toDownload: RemoteFile[]; toDeleteLocal: string[]; toDeleteRemote: string[]; unchangedPaths: string[]; serverTime: string; } export interface SyncTask { path: string; operation: SyncOperationType; file?: LocalFile | RemoteFile; } export interface CreateSyncPlanParams { fs: FileSystem; api: SyncApi; state: SyncState; rootPath: string; ignorePatterns?: string[]; enableContentHashCheck?: boolean; } export interface SyncContext { executor: SyncExecutor; state: SyncState; fs: FileSystem; serverTime: string; deviceName?: string; isDirtyFile?: (path: string) => Promise | boolean; baseStore?: BaseContentStore; } export enum MergeOutcome { Merged = 'merged', Ambiguous = 'ambiguous', } export interface MergeResult { outcome: MergeOutcome; mergedContent?: Uint8Array; } export interface BaseContentEntry { path: string; version: number; contentHash: string; content: Uint8Array; updatedAt: string; } export interface BaseContentStore { get(path: string): Promise; set(path: string, entry: BaseContentEntry): Promise; remove(path: string): Promise; } export interface MergeInputs { base: Uint8Array; local: Uint8Array; remote: Uint8Array; } const MERGEABLE_EXTENSIONS = new Set(['.org', '.md']); const MAX_MERGEABLE_SIZE_BYTES = 512 * 1024; export const isMergeableFile = (path: string, size: number): boolean => { if (size > MAX_MERGEABLE_SIZE_BYTES) return false; if (isOrgNoteConfigPath(path)) return true; const lastDot = path.lastIndexOf('.'); const extension = lastDot >= 0 ? path.substring(lastDot) : ''; return MERGEABLE_EXTENSIONS.has(extension); };