@vrtmrz/livesync-commonlib 0.3.2 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/dist/common/models/fileaccess.definition.d.ts +35 -0
- package/dist/common/models/fileaccess.definition.js +11 -0
- package/dist/common/typeUtils.d.ts +27 -4
- package/dist/interfaces/FileHandler.d.ts +1 -1
- package/dist/managers/EntryManager/EntryManagerImpls.js +1 -1
- package/dist/managers/StorageEventManager/admission.d.ts +50 -0
- package/dist/managers/StorageEventManager/admission.js +129 -0
- package/dist/managers/StorageEventManager/batching.d.ts +93 -0
- package/dist/managers/StorageEventManager/batching.js +97 -0
- package/dist/managers/StorageEventManager/operations.d.ts +142 -0
- package/dist/managers/StorageEventManager/operations.js +69 -0
- package/dist/managers/StorageEventManager/processing.d.ts +63 -0
- package/dist/managers/StorageEventManager/processing.js +94 -0
- package/dist/managers/StorageEventManager/queue.d.ts +73 -0
- package/dist/managers/StorageEventManager/queue.js +113 -0
- package/dist/managers/StorageEventManager/retry.d.ts +33 -0
- package/dist/managers/StorageEventManager/retry.js +31 -0
- package/dist/managers/StorageEventManager/snapshot.d.ts +46 -0
- package/dist/managers/StorageEventManager/snapshot.js +53 -0
- package/dist/managers/StorageEventManager/state.d.ts +155 -0
- package/dist/managers/StorageEventManager/state.js +105 -0
- package/dist/managers/StorageEventManager/watching.d.ts +93 -0
- package/dist/managers/StorageEventManager/watching.js +36 -0
- package/dist/managers/StorageEventManager.d.ts +303 -83
- package/dist/managers/StorageEventManager.js +457 -547
- package/dist/p2p/P2PService.d.ts +10 -1
- package/dist/p2p/P2PService.js +25 -2
- package/dist/p2p/index.d.ts +4 -1
- package/dist/p2p/index.js +2 -0
- package/dist/p2p/p2pDiagnostics.d.ts +35 -0
- package/dist/p2p/p2pDiagnostics.js +76 -0
- package/dist/pouchdb/ReplicatorShim.batch.d.ts +30 -0
- package/dist/pouchdb/ReplicatorShim.batch.js +62 -0
- package/dist/pouchdb/ReplicatorShim.d.ts +14 -1
- package/dist/pouchdb/ReplicatorShim.js +64 -80
- package/dist/replication/trystero/P2PLogCollector.d.ts +12 -0
- package/dist/replication/trystero/P2PLogCollector.js +107 -72
- package/dist/replication/trystero/P2PRoomSessionOwner.d.ts +4 -0
- package/dist/replication/trystero/P2PRoomSessionOwner.js +39 -2
- package/dist/replication/trystero/TrysteroReplicator.d.ts +21 -24
- package/dist/replication/trystero/TrysteroReplicator.js +191 -69
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.js +11 -0
- package/dist/replication/trystero/p2pReplicationDiagnostics.d.ts +9 -0
- package/dist/replication/trystero/p2pReplicationDiagnostics.js +51 -0
- package/dist/replication/trystero/p2pReplicationProgress.d.ts +15 -0
- package/dist/replication/trystero/p2pReplicationProgress.js +37 -0
- package/dist/replication/trystero/types.d.ts +3 -0
- package/dist/serviceModules/ServiceFileHandlerBase/chunks.d.ts +10 -0
- package/dist/serviceModules/ServiceFileHandlerBase/chunks.js +51 -0
- package/dist/serviceModules/ServiceFileHandlerBase/context.d.ts +168 -0
- package/dist/serviceModules/ServiceFileHandlerBase/context.js +15 -0
- package/dist/serviceModules/ServiceFileHandlerBase/deletion.d.ts +40 -0
- package/dist/serviceModules/ServiceFileHandlerBase/deletion.js +142 -0
- package/dist/serviceModules/ServiceFileHandlerBase/events.d.ts +20 -0
- package/dist/serviceModules/ServiceFileHandlerBase/events.js +205 -0
- package/dist/serviceModules/ServiceFileHandlerBase/locking.d.ts +23 -0
- package/dist/serviceModules/ServiceFileHandlerBase/locking.js +51 -0
- package/dist/serviceModules/ServiceFileHandlerBase/protection.d.ts +26 -0
- package/dist/serviceModules/ServiceFileHandlerBase/protection.js +67 -0
- package/dist/serviceModules/ServiceFileHandlerBase/provenance.d.ts +174 -0
- package/dist/serviceModules/ServiceFileHandlerBase/provenance.js +182 -0
- package/dist/serviceModules/ServiceFileHandlerBase/reflection.d.ts +48 -0
- package/dist/serviceModules/ServiceFileHandlerBase/reflection.js +344 -0
- package/dist/serviceModules/ServiceFileHandlerBase/renaming.d.ts +23 -0
- package/dist/serviceModules/ServiceFileHandlerBase/renaming.js +77 -0
- package/dist/serviceModules/ServiceFileHandlerBase/saving.d.ts +40 -0
- package/dist/serviceModules/ServiceFileHandlerBase/saving.js +306 -0
- package/dist/serviceModules/ServiceFileHandlerBase/scanning.d.ts +22 -0
- package/dist/serviceModules/ServiceFileHandlerBase/scanning.js +93 -0
- package/dist/serviceModules/ServiceFileHandlerBase/selection.d.ts +58 -0
- package/dist/serviceModules/ServiceFileHandlerBase/selection.js +94 -0
- package/dist/serviceModules/ServiceFileHandlerBase/storage.d.ts +55 -0
- package/dist/serviceModules/ServiceFileHandlerBase/storage.js +37 -0
- package/dist/serviceModules/ServiceFileHandlerBase.d.ts +119 -182
- package/dist/serviceModules/ServiceFileHandlerBase.js +182 -1402
- package/dist/services/base/APIService.d.ts +10 -0
- package/dist/services/base/APIService.js +13 -0
- package/dist/services/base/IService.d.ts +2 -0
- package/docs/conflict-resolution.md +3 -1
- package/docs/development.md +36 -0
- package/docs/journal-receive-recovery.md +7 -4
- package/docs/offline-scanner.md +5 -1
- package/docs/p2p-transport-lifecycle.md +125 -11
- package/docs/releasing.md +6 -0
- package/docs/storage-events-and-reflection.md +203 -19
- package/package.json +2 -2
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { AnyEntry, FilePath, FilePathWithPrefix, UXFileInfo, UXFileInfoStub, UXInternalFileInfoStub } from "../../common/types.js";
|
|
2
|
+
import type { LiveSyncEventHub } from "../../hub/hub.js";
|
|
3
|
+
import type { StoreFileOptions } from "../../interfaces/FileHandler.js";
|
|
4
|
+
import type { APIService } from "../../services/base/APIService.js";
|
|
5
|
+
import type { DatabaseFileAccess } from "../../interfaces/DatabaseFileAccess.js";
|
|
6
|
+
import type { StorageAccess } from "../../interfaces/StorageAccess.js";
|
|
7
|
+
import type { FileProcessingService } from "../../services/base/FileProcessingService.js";
|
|
8
|
+
import type { ReplicationService } from "../../services/base/ReplicationService.js";
|
|
9
|
+
import type { ConflictService } from "../../services/base/ConflictService.js";
|
|
10
|
+
import type { PathService } from "../../services/base/PathService.js";
|
|
11
|
+
import type { SettingService } from "../../services/base/SettingService.js";
|
|
12
|
+
import type { VaultService } from "../../services/base/VaultService.js";
|
|
13
|
+
import type { FileReflectionProvenance, FileReflectionProvenanceRecord } from "../../interfaces/FileReflectionProvenance.js";
|
|
14
|
+
import type { LogFunction } from "../../services/lib/logUtils.js";
|
|
15
|
+
/** Services captured when the host constructs its file-handler facade. */
|
|
16
|
+
export interface ServiceFileHandlerDependencies {
|
|
17
|
+
/** Host event hub used for conflict-cancellation notifications. */
|
|
18
|
+
events: LiveSyncEventHub;
|
|
19
|
+
/** Host logging service used by the inherited module logger. */
|
|
20
|
+
API: APIService;
|
|
21
|
+
/** Database access captured by the facade at construction. */
|
|
22
|
+
databaseFileAccess: DatabaseFileAccess;
|
|
23
|
+
/** Storage access captured by the facade at construction. */
|
|
24
|
+
storageAccess: StorageAccess;
|
|
25
|
+
/** Storage-event handler registration owned by the host. */
|
|
26
|
+
fileProcessing: FileProcessingService;
|
|
27
|
+
/** Replicated-document handler registration owned by the host. */
|
|
28
|
+
replication: ReplicationService;
|
|
29
|
+
/** Conflict review hooks delivered outside document locks. */
|
|
30
|
+
conflict: ConflictService;
|
|
31
|
+
/** Path identity and freshness service captured at construction. */
|
|
32
|
+
path: PathService;
|
|
33
|
+
/** Live settings service captured at construction. */
|
|
34
|
+
setting: SettingService;
|
|
35
|
+
/** Current vault selection and size policy service. */
|
|
36
|
+
vault: VaultService;
|
|
37
|
+
/**
|
|
38
|
+
* Device-local record of the exact database revision reflected in storage.
|
|
39
|
+
*
|
|
40
|
+
* This is optional for compatibility hosts. Maintained hosts should provide
|
|
41
|
+
* it so edits made while a document is conflicted extend the displayed
|
|
42
|
+
* branch instead of whichever branch PouchDB currently selects as winner.
|
|
43
|
+
* The host must finish opening its backing store before it dispatches file
|
|
44
|
+
* or replication events; provenance does not hide lifecycle violations by
|
|
45
|
+
* waiting for readiness.
|
|
46
|
+
*/
|
|
47
|
+
fileReflectionProvenance?: FileReflectionProvenance;
|
|
48
|
+
}
|
|
49
|
+
/** Exact storage bytes and the provenance observed alongside them. */
|
|
50
|
+
export type StorageSnapshot = {
|
|
51
|
+
/** Immutable body and raw file statistics captured from storage. */
|
|
52
|
+
file: UXFileInfo;
|
|
53
|
+
/** Copied device-local revision evidence captured with those bytes. */
|
|
54
|
+
revision: FileReflectionProvenanceRecord | undefined;
|
|
55
|
+
};
|
|
56
|
+
/** Result controlling whether an incoming effect must stop or revalidate a candidate. */
|
|
57
|
+
export interface PreservationResult {
|
|
58
|
+
/** Stop the incoming write, including when a preservation branch write was refused. */
|
|
59
|
+
preserved: boolean;
|
|
60
|
+
/** Candidate bytes/provenance requiring final revalidation; absence can mean unstable or unproven safety. */
|
|
61
|
+
snapshot?: StorageSnapshot;
|
|
62
|
+
}
|
|
63
|
+
/** A conflict review retained until its document lock is released. */
|
|
64
|
+
export type DeferredConflictCheck = {
|
|
65
|
+
/** Storage path to review after unlocking. */
|
|
66
|
+
path: FilePathWithPrefix;
|
|
67
|
+
/** Whether only an existing open review should be refreshed. */
|
|
68
|
+
ifOpen: boolean;
|
|
69
|
+
};
|
|
70
|
+
/** Remaining effect of a saved operation after inspecting current storage. */
|
|
71
|
+
export type RestoredFileEventAction = {
|
|
72
|
+
kind: "none";
|
|
73
|
+
} | {
|
|
74
|
+
kind: "unresolved";
|
|
75
|
+
} | {
|
|
76
|
+
kind: "store";
|
|
77
|
+
file: UXFileInfoStub;
|
|
78
|
+
complete?: boolean;
|
|
79
|
+
} | {
|
|
80
|
+
kind: "delete";
|
|
81
|
+
path: FilePath;
|
|
82
|
+
} | {
|
|
83
|
+
kind: "rename";
|
|
84
|
+
file: UXFileInfoStub;
|
|
85
|
+
oldPath: FilePathWithPrefix;
|
|
86
|
+
};
|
|
87
|
+
/** Dynamic public seams retained for host subclasses and runtime replacements. */
|
|
88
|
+
export interface FileHandlerHost {
|
|
89
|
+
/**
|
|
90
|
+
* Resolve the current virtual database getter.
|
|
91
|
+
* @returns Current database access.
|
|
92
|
+
* @remarks Getter overrides and later host replacements remain visible at each access site.
|
|
93
|
+
*/
|
|
94
|
+
db(): DatabaseFileAccess;
|
|
95
|
+
/**
|
|
96
|
+
* Resolve the current virtual storage getter.
|
|
97
|
+
* @returns Current storage access.
|
|
98
|
+
* @remarks Getter overrides and later host replacements remain visible at each access site.
|
|
99
|
+
*/
|
|
100
|
+
storage(): StorageAccess;
|
|
101
|
+
/** Route diagnostics through the current public logger. */
|
|
102
|
+
log: LogFunction;
|
|
103
|
+
/**
|
|
104
|
+
* Resolve metadata through the current virtual path method.
|
|
105
|
+
* @param entry - Metadata to resolve.
|
|
106
|
+
* @returns Its host path.
|
|
107
|
+
* @remarks This seam does not acquire a document lock.
|
|
108
|
+
*/
|
|
109
|
+
getPath(entry: AnyEntry): FilePathWithPrefix;
|
|
110
|
+
/**
|
|
111
|
+
* Resolve a storage path through the current virtual method.
|
|
112
|
+
* @param entry - Metadata to resolve.
|
|
113
|
+
* @returns Its unprefixed host path.
|
|
114
|
+
* @remarks This seam remains independent of the virtual metadata-path method.
|
|
115
|
+
*/
|
|
116
|
+
getPathWithoutPrefix(entry: AnyEntry): FilePathWithPrefix;
|
|
117
|
+
/**
|
|
118
|
+
* Read through the current virtual reader.
|
|
119
|
+
* @param file - The supplied observation.
|
|
120
|
+
* @returns Its supplied or current body.
|
|
121
|
+
* @remarks Callers retain their surrounding document lock and original read count.
|
|
122
|
+
*/
|
|
123
|
+
readFileFromStub(file: UXFileInfoStub | UXFileInfo): Promise<UXFileInfo>;
|
|
124
|
+
/**
|
|
125
|
+
* Invoke the virtual save boundary used by chunk repair.
|
|
126
|
+
* @param info - File observation or path to save.
|
|
127
|
+
* @param force - Existing force policy.
|
|
128
|
+
* @param onlyChunks - Whether only missing chunks should be created.
|
|
129
|
+
* @param options - Existing recovery revalidation option.
|
|
130
|
+
* @returns Whether the requested save completed or safely skipped.
|
|
131
|
+
* @remarks The public save method owns document locking.
|
|
132
|
+
*/
|
|
133
|
+
storeFileToDB(info: UXFileInfoStub | UXFileInfo | UXInternalFileInfoStub | FilePathWithPrefix, force?: boolean, onlyChunks?: boolean, options?: StoreFileOptions): Promise<boolean>;
|
|
134
|
+
}
|
|
135
|
+
/** Mutable state belonging to one facade instance, never shared across hosts. */
|
|
136
|
+
export interface FileHandlerState {
|
|
137
|
+
/** Hashes reused only for the same immutable Blob object. */
|
|
138
|
+
readonly contentHashes: WeakMap<Blob, Promise<string>>;
|
|
139
|
+
/** Pending post-lock hooks keyed by normalised document identity. */
|
|
140
|
+
readonly deferredConflictChecks: Map<string, DeferredConflictCheck[]>;
|
|
141
|
+
}
|
|
142
|
+
/** Captured service values, per-instance state, and the small virtual host boundary. */
|
|
143
|
+
export interface FileHandlerContext {
|
|
144
|
+
/** Existing host event hub. */
|
|
145
|
+
readonly events: LiveSyncEventHub;
|
|
146
|
+
/** Existing conflict-review hooks. */
|
|
147
|
+
readonly conflict: ConflictService;
|
|
148
|
+
/** Existing path identity and freshness policy. */
|
|
149
|
+
readonly path: PathService;
|
|
150
|
+
/** Existing settings service; settings remain live when its getter is called. */
|
|
151
|
+
readonly setting: SettingService;
|
|
152
|
+
/** Existing vault selection and size policy. */
|
|
153
|
+
readonly vault: VaultService;
|
|
154
|
+
/** Optional device-local record store captured at construction. */
|
|
155
|
+
readonly provenance?: FileReflectionProvenance;
|
|
156
|
+
/** Per-instance mutable hashes and deferred hooks. */
|
|
157
|
+
readonly state: FileHandlerState;
|
|
158
|
+
/** Public methods and getters resolved dynamically at use time. */
|
|
159
|
+
readonly host: FileHandlerHost;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Capture service values and allocate the facade's existing per-instance state.
|
|
163
|
+
* @param services - Services supplied at construction; later property replacement does not change this context.
|
|
164
|
+
* @param host - Dynamic public seams; internal helpers are called directly rather than mirrored here.
|
|
165
|
+
* @returns The isolated context used by responsibility functions.
|
|
166
|
+
* @remarks No I/O, document locking, handler registration, or provenance access occurs here.
|
|
167
|
+
*/
|
|
168
|
+
export declare function createFileHandlerContext(services: ServiceFileHandlerDependencies, host: FileHandlerHost): FileHandlerContext;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function createFileHandlerContext(services, host) {
|
|
2
|
+
return {
|
|
3
|
+
events: services.events,
|
|
4
|
+
conflict: services.conflict,
|
|
5
|
+
path: services.path,
|
|
6
|
+
setting: services.setting,
|
|
7
|
+
vault: services.vault,
|
|
8
|
+
provenance: services.fileReflectionProvenance,
|
|
9
|
+
host,
|
|
10
|
+
state: { contentHashes: /* @__PURE__ */ new WeakMap(), deferredConflictChecks: /* @__PURE__ */ new Map() }
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
createFileHandlerContext
|
|
15
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { FilePath, FilePathWithPrefix, UXFileInfoStub, UXInternalFileInfoStub } from "../../common/types.js";
|
|
2
|
+
import type { FileHandlerContext } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Record a storage deletion under the corresponding document lock.
|
|
5
|
+
*
|
|
6
|
+
* @param ctx - The captured services, per-instance state, and dynamic host seams.
|
|
7
|
+
* @param info - The supplied file observation or path.
|
|
8
|
+
* @returns True after deletion or conservative conflict preservation; false for absent/deleted records or a refused write.
|
|
9
|
+
* @remarks Core operations require source and destination document locks as applicable. Unknown conflict ancestry is preserved for review rather than guessed.
|
|
10
|
+
*/
|
|
11
|
+
export declare function deleteFileFromDB(ctx: FileHandlerContext, info: UXFileInfoStub | UXInternalFileInfoStub | FilePath): Promise<boolean>;
|
|
12
|
+
/**
|
|
13
|
+
* Accept an already represented deletion before applying any remaining database effect.
|
|
14
|
+
*
|
|
15
|
+
* @param ctx - The captured services, per-instance state, and dynamic host seams.
|
|
16
|
+
* @param info - The supplied file observation or path.
|
|
17
|
+
* @returns True if deletion is already represented or the remaining effect is accepted; false on refusal.
|
|
18
|
+
* @remarks Core operations require source and destination document locks as applicable. Unknown conflict ancestry is preserved for review rather than guessed.
|
|
19
|
+
*/
|
|
20
|
+
export declare function deleteFileFromDBIfPresent(ctx: FileHandlerContext, info: UXFileInfoStub | UXInternalFileInfoStub | FilePath): Promise<boolean>;
|
|
21
|
+
/**
|
|
22
|
+
* Delete a selected revision under its document lock.
|
|
23
|
+
*
|
|
24
|
+
* @param ctx - The captured services, per-instance state, and dynamic host seams.
|
|
25
|
+
* @param info - The supplied file observation or path.
|
|
26
|
+
* @param rev - The exact selected database revision.
|
|
27
|
+
* @returns The database deletion result; matching provenance is cleared only after success.
|
|
28
|
+
* @remarks Core operations require source and destination document locks as applicable. Unknown conflict ancestry is preserved for review rather than guessed.
|
|
29
|
+
*/
|
|
30
|
+
export declare function deleteRevisionFromDB(ctx: FileHandlerContext, info: UXFileInfoStub | FilePath | FilePathWithPrefix, rev: string): Promise<boolean | undefined>;
|
|
31
|
+
/**
|
|
32
|
+
* Run the legacy delete-and-reflect resolution under one document lock.
|
|
33
|
+
*
|
|
34
|
+
* @param ctx - The captured services, per-instance state, and dynamic host seams.
|
|
35
|
+
* @param info - The supplied file observation or path.
|
|
36
|
+
* @param rev - The exact selected database revision.
|
|
37
|
+
* @returns False on refusal; the existing undefined result on successful legacy completion.
|
|
38
|
+
* @remarks Core operations require source and destination document locks as applicable. Unknown conflict ancestry is preserved for review rather than guessed.
|
|
39
|
+
*/
|
|
40
|
+
export declare function resolveConflictedByDeletingRevision(ctx: FileHandlerContext, info: UXFileInfoStub | FilePath, rev: string): Promise<boolean | undefined>;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { withFileEventPaths, queueConflictCheckFor } from "./locking.js";
|
|
2
|
+
import { infoToStub } from "./storage.js";
|
|
3
|
+
import { deleteProvenance, getProvenance, findUniqueContentRevision } from "./provenance.js";
|
|
4
|
+
import { applyDatabaseEntryToStorage } from "./reflection.js";
|
|
5
|
+
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
|
|
6
|
+
import { getStoragePathFromUXFileInfo, getDatabasePathFromUXFileInfo } from "../../common/typeUtils.js";
|
|
7
|
+
import { tryGetFilePath } from "../../common/utils.doc.js";
|
|
8
|
+
async function deleteFileFromDB(ctx, info) {
|
|
9
|
+
return await withFileEventPaths(ctx, [getStoragePathFromUXFileInfo(info)], () => deleteFileFromDBCore(ctx, info));
|
|
10
|
+
}
|
|
11
|
+
async function deleteFileFromDBIfPresent(ctx, info) {
|
|
12
|
+
const target = typeof info !== "string" && info.isInternal ? getDatabasePathFromUXFileInfo(info) : info;
|
|
13
|
+
const entry = await ctx.host.db().fetchEntryMeta(target, void 0, true);
|
|
14
|
+
if (!entry || entry.deleted || entry._deleted) return true;
|
|
15
|
+
return await deleteFileFromDBCore(ctx, info);
|
|
16
|
+
}
|
|
17
|
+
async function deleteFileFromDBCore(ctx, info) {
|
|
18
|
+
const file = await infoToStub(ctx, info);
|
|
19
|
+
const path = typeof info === "string" ? info : tryGetFilePath(info);
|
|
20
|
+
if (file == null) return await deleteMissingStorageFile(ctx, info, path);
|
|
21
|
+
if (file.isInternal) {
|
|
22
|
+
ctx.host.log(
|
|
23
|
+
`Internal file ${file.path} is not allowed to be processed on processFileEvent`,
|
|
24
|
+
LOG_LEVEL_VERBOSE
|
|
25
|
+
);
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
const entry = await ctx.host.db().fetchEntry(file, void 0, true, true);
|
|
29
|
+
if (!entry || entry.deleted || entry._deleted) {
|
|
30
|
+
ctx.host.log(`File ${file.path} is not exist or already deleted on the database`, LOG_LEVEL_VERBOSE);
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const conflictedRevs = await ctx.host.db().getConflictedRevs(file);
|
|
34
|
+
if (conflictedRevs.length === 0) {
|
|
35
|
+
const deleted = await ctx.host.db().delete(file);
|
|
36
|
+
if (deleted) await deleteProvenance(ctx, file.path);
|
|
37
|
+
return deleted;
|
|
38
|
+
}
|
|
39
|
+
let baseRevision = (await getProvenance(ctx, file.path))?.revision;
|
|
40
|
+
if (!baseRevision) {
|
|
41
|
+
try {
|
|
42
|
+
const readFile = await ctx.host.readFileFromStub(file);
|
|
43
|
+
baseRevision = await findUniqueContentRevision(ctx, readFile);
|
|
44
|
+
} catch {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!baseRevision) {
|
|
48
|
+
ctx.host.log(
|
|
49
|
+
`The deleted storage file ${file.path} has conflicts, but its displayed revision is unknown; preserving every database branch`,
|
|
50
|
+
LOG_LEVEL_NOTICE
|
|
51
|
+
);
|
|
52
|
+
await queueConflictCheckFor(ctx, file.path);
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
const storedRevision = await ctx.host.db().storeDeletionWithBaseRevision(file.path, baseRevision);
|
|
56
|
+
if (storedRevision === false) return false;
|
|
57
|
+
await deleteProvenance(ctx, file.path);
|
|
58
|
+
await queueConflictCheckFor(ctx, file.path);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
async function deleteRevisionFromDB(ctx, info, rev) {
|
|
62
|
+
return await withFileEventPaths(
|
|
63
|
+
ctx,
|
|
64
|
+
[getStoragePathFromUXFileInfo(info)],
|
|
65
|
+
() => deleteRevisionFromDBCore(ctx, info, rev)
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
async function deleteRevisionFromDBCore(ctx, info, rev) {
|
|
69
|
+
const path = getStoragePathFromUXFileInfo(info);
|
|
70
|
+
const provenance = await getProvenance(ctx, path);
|
|
71
|
+
const deleted = await ctx.host.db().delete(info, rev);
|
|
72
|
+
if (deleted && provenance?.revision === rev) {
|
|
73
|
+
await deleteProvenance(ctx, path);
|
|
74
|
+
}
|
|
75
|
+
return deleted;
|
|
76
|
+
}
|
|
77
|
+
async function resolveConflictedByDeletingRevision(ctx, info, rev) {
|
|
78
|
+
return await withFileEventPaths(
|
|
79
|
+
ctx,
|
|
80
|
+
[getStoragePathFromUXFileInfo(info)],
|
|
81
|
+
() => resolveConflictedByDeletingRevisionCore(ctx, info, rev)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
async function resolveConflictedByDeletingRevisionCore(ctx, info, rev) {
|
|
85
|
+
const path = getStoragePathFromUXFileInfo(info);
|
|
86
|
+
const file = await infoToStub(ctx, info);
|
|
87
|
+
const docEntry = await ctx.host.db().fetchEntryMeta(file ?? info, rev, true);
|
|
88
|
+
if (!docEntry) {
|
|
89
|
+
ctx.host.log(`Failed to read the conflicted revision ${rev} of ${path}`, LOG_LEVEL_VERBOSE);
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (!await deleteRevisionFromDBCore(ctx, info, rev)) {
|
|
93
|
+
ctx.host.log(`Failed to delete the conflicted revision ${rev} of ${path}`, LOG_LEVEL_VERBOSE);
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (!await applyDatabaseEntryToStorage(ctx, docEntry, file, true)) {
|
|
97
|
+
ctx.host.log(`Failed to apply the resolved revision ${rev} of ${path} to the storage`, LOG_LEVEL_VERBOSE);
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
await deleteProvenance(ctx, path);
|
|
101
|
+
}
|
|
102
|
+
async function deleteMissingStorageFile(ctx, info, path) {
|
|
103
|
+
if (path === void 0) {
|
|
104
|
+
ctx.host.log(`File ${tryGetFilePath(info)} is not exist on the storage`, LOG_LEVEL_VERBOSE);
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
const entryByPath = await ctx.host.db().fetchEntry(path, void 0, true, true);
|
|
108
|
+
if (!entryByPath || entryByPath.deleted || entryByPath._deleted) {
|
|
109
|
+
ctx.host.log(
|
|
110
|
+
`File ${path} is not exist on the storage nor the database (or already deleted)`,
|
|
111
|
+
LOG_LEVEL_VERBOSE
|
|
112
|
+
);
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
const conflictedRevs = await ctx.host.db().getConflictedRevs(path);
|
|
116
|
+
if (conflictedRevs.length > 0) {
|
|
117
|
+
const provenance = await getProvenance(ctx, path);
|
|
118
|
+
if (!provenance) {
|
|
119
|
+
ctx.host.log(
|
|
120
|
+
`The deleted storage file ${path} has conflicts, but its displayed revision is unknown; preserving every database branch`,
|
|
121
|
+
LOG_LEVEL_NOTICE
|
|
122
|
+
);
|
|
123
|
+
await queueConflictCheckFor(ctx, path);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
const storedRevision = await ctx.host.db().storeDeletionWithBaseRevision(path, provenance.revision);
|
|
127
|
+
if (storedRevision === false) return false;
|
|
128
|
+
await deleteProvenance(ctx, path);
|
|
129
|
+
await queueConflictCheckFor(ctx, path);
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
ctx.host.log(`File ${path} is missing on storage; deleting from the database by path`, LOG_LEVEL_INFO);
|
|
133
|
+
const deleted = await ctx.host.db().delete(path);
|
|
134
|
+
if (deleted) await deleteProvenance(ctx, path);
|
|
135
|
+
return deleted;
|
|
136
|
+
}
|
|
137
|
+
export {
|
|
138
|
+
deleteFileFromDB,
|
|
139
|
+
deleteFileFromDBIfPresent,
|
|
140
|
+
deleteRevisionFromDB,
|
|
141
|
+
resolveConflictedByDeletingRevision
|
|
142
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FileEventItem, MetaEntry } from "../../common/types.js";
|
|
2
|
+
import type { FileHandlerContext } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reconcile an ordinary or recovery storage event under every related document lock.
|
|
5
|
+
*
|
|
6
|
+
* @param ctx - The captured services, per-instance state, and dynamic host seams.
|
|
7
|
+
* @param item - The admitted operation intent and related paths.
|
|
8
|
+
* @returns Whether the event completed or safely skipped; unsupported or unresolved work returns false and inspection errors retain their original propagation.
|
|
9
|
+
* @remarks Operation intent is revalidated under related document locks. Policy skips, retained failures, and conservative conflict handling retain their original meanings.
|
|
10
|
+
*/
|
|
11
|
+
export declare function processFileEvent(ctx: FileHandlerContext, item: FileEventItem): Promise<boolean>;
|
|
12
|
+
/**
|
|
13
|
+
* Select and reflect a replicated document while retaining host override dispatch.
|
|
14
|
+
*
|
|
15
|
+
* @param ctx - The captured services, per-instance state, and dynamic host seams.
|
|
16
|
+
* @param entry - The database metadata being processed.
|
|
17
|
+
* @returns Whether replication reflection completed; excluded or unsafe documents retain a false result.
|
|
18
|
+
* @remarks Operation intent is revalidated under related document locks. Policy skips, retained failures, and conservative conflict handling retain their original meanings.
|
|
19
|
+
*/
|
|
20
|
+
export declare function processReplicatedDocument(ctx: FileHandlerContext, entry: MetaEntry): Promise<boolean>;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { withFileEventPaths } from "./locking.js";
|
|
2
|
+
import {
|
|
3
|
+
isCurrentPathSelected,
|
|
4
|
+
getExactCurrentFile,
|
|
5
|
+
canRecordStorageDeletion,
|
|
6
|
+
logRestoredEventValidationFailure,
|
|
7
|
+
isCurrentFileSelected,
|
|
8
|
+
canApplyRestoredDeletion
|
|
9
|
+
} from "./selection.js";
|
|
10
|
+
import { storeFileToDBFromRevision } from "./saving.js";
|
|
11
|
+
import { deleteFileFromDBIfPresent } from "./deletion.js";
|
|
12
|
+
import { renameFileInDBCore } from "./renaming.js";
|
|
13
|
+
import { isFolderInfo } from "./storage.js";
|
|
14
|
+
import { dbToStorageCore } from "./reflection.js";
|
|
15
|
+
import { LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
|
|
16
|
+
import { isReconcileEvent } from "../../common/models/fileaccess.definition.js";
|
|
17
|
+
import { EVENT_CONFLICT_CANCELLED } from "../../events/coreEvents.js";
|
|
18
|
+
import { shouldBeIgnored } from "../../string_and_binary/path.js";
|
|
19
|
+
async function processFileEvent(ctx, item) {
|
|
20
|
+
if (item.restoredFromPreviousRuntime || isReconcileEvent(item)) {
|
|
21
|
+
return await processRestoredFileEvent(ctx, item);
|
|
22
|
+
}
|
|
23
|
+
const eventItem = item.args;
|
|
24
|
+
const type = item.type;
|
|
25
|
+
const path = eventItem.file.path;
|
|
26
|
+
if (type === "RENAME" && !eventItem.oldPath) {
|
|
27
|
+
ctx.host.log(`Rename event for ${path} has no source path`, LOG_LEVEL_VERBOSE);
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const relatedPath = type === "RENAME" ? eventItem.oldPath : type === "DELETE" ? eventItem.renameTarget : void 0;
|
|
31
|
+
const eventPaths = relatedPath === void 0 ? [path] : [path, relatedPath];
|
|
32
|
+
return await withFileEventPaths(ctx, eventPaths, async () => {
|
|
33
|
+
if (!await isCurrentPathSelected(ctx, path)) {
|
|
34
|
+
return shouldBeIgnored(path) || !await ctx.vault.isTargetFile(path, { skipCaseCollisionCheck: true });
|
|
35
|
+
}
|
|
36
|
+
switch (type) {
|
|
37
|
+
case "CREATE":
|
|
38
|
+
case "CHANGED": {
|
|
39
|
+
const current = getExactCurrentFile(ctx, await ctx.host.storage().getStub(path), path);
|
|
40
|
+
if (!current || ctx.vault.isFileSizeTooLarge(current.stat.size)) return true;
|
|
41
|
+
return await storeFileToDBFromRevision(ctx, current);
|
|
42
|
+
}
|
|
43
|
+
case "DELETE":
|
|
44
|
+
try {
|
|
45
|
+
if (!await canRecordStorageDeletion(ctx, item)) return true;
|
|
46
|
+
} catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return await deleteFileFromDBIfPresent(ctx, item.args.file);
|
|
50
|
+
case "RENAME":
|
|
51
|
+
return await renameFileInDBCore(
|
|
52
|
+
ctx,
|
|
53
|
+
item.args.file,
|
|
54
|
+
item.args.oldPath
|
|
55
|
+
);
|
|
56
|
+
case "INTERNAL":
|
|
57
|
+
return false;
|
|
58
|
+
default:
|
|
59
|
+
ctx.host.log(`Unsupported event type: ${type}`, LOG_LEVEL_VERBOSE);
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async function processRestoredFileEvent(ctx, item) {
|
|
65
|
+
const eventItem = item.args;
|
|
66
|
+
const type = item.type;
|
|
67
|
+
const path = eventItem.file.path;
|
|
68
|
+
if (type === "RENAME" && !eventItem.oldPath) {
|
|
69
|
+
ctx.host.log(`Rename event for ${path} has no source path`, LOG_LEVEL_VERBOSE);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
const relatedPath = type === "RENAME" ? eventItem.oldPath : type === "DELETE" ? eventItem.renameTarget : void 0;
|
|
73
|
+
const eventPaths = relatedPath === void 0 ? [path] : [path, relatedPath];
|
|
74
|
+
return await withFileEventPaths(ctx, eventPaths, async (isSameDocument) => {
|
|
75
|
+
let action;
|
|
76
|
+
try {
|
|
77
|
+
action = await planRestoredFileEvent(ctx, item, isSameDocument);
|
|
78
|
+
} catch (ex) {
|
|
79
|
+
logRestoredEventValidationFailure(ctx, path, ex);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
switch (action.kind) {
|
|
83
|
+
case "none":
|
|
84
|
+
return true;
|
|
85
|
+
case "unresolved":
|
|
86
|
+
return false;
|
|
87
|
+
case "store": {
|
|
88
|
+
const stored = await storeFileToDBFromRevision(ctx, action.file, false, false, void 0, true);
|
|
89
|
+
return stored && action.complete !== false;
|
|
90
|
+
}
|
|
91
|
+
case "delete":
|
|
92
|
+
return await deleteFileFromDBIfPresent(ctx, action.path);
|
|
93
|
+
case "rename":
|
|
94
|
+
return await renameFileInDBCore(ctx, action.file, action.oldPath, true);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async function planRestoredFileEvent(ctx, item, isSameDocument) {
|
|
99
|
+
const path = item.args.file.path;
|
|
100
|
+
switch (item.type) {
|
|
101
|
+
case "CREATE":
|
|
102
|
+
case "CHANGED": {
|
|
103
|
+
const current = getExactCurrentFile(ctx, await ctx.host.storage().getStub(path), path);
|
|
104
|
+
return current && await isCurrentFileSelected(ctx, current) ? { kind: "store", file: current } : { kind: "none" };
|
|
105
|
+
}
|
|
106
|
+
case "DELETE": {
|
|
107
|
+
if (item.args.renameTarget !== void 0) {
|
|
108
|
+
return await canRecordStorageDeletion(ctx, item) && await canApplyRestoredDeletion(ctx, path) ? { kind: "delete", path } : { kind: "none" };
|
|
109
|
+
}
|
|
110
|
+
const current = await ctx.host.storage().getStub(path);
|
|
111
|
+
return current === null && await canApplyRestoredDeletion(ctx, path) ? { kind: "delete", path } : { kind: "none" };
|
|
112
|
+
}
|
|
113
|
+
case "RENAME":
|
|
114
|
+
return await planRestoredRename(ctx, path, item.args.oldPath, isSameDocument);
|
|
115
|
+
case "INTERNAL":
|
|
116
|
+
return { kind: "none" };
|
|
117
|
+
default:
|
|
118
|
+
ctx.host.log(`Unsupported event type: ${item.type}`, LOG_LEVEL_VERBOSE);
|
|
119
|
+
return { kind: "none" };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function planRestoredRename(ctx, newPath, oldPath, isSameDocument) {
|
|
123
|
+
if (isSameDocument) return await planSameDocumentRename(ctx, newPath, oldPath);
|
|
124
|
+
return await planCrossDocumentRename(ctx, newPath, oldPath);
|
|
125
|
+
}
|
|
126
|
+
async function processReplicatedDocument(ctx, entry) {
|
|
127
|
+
return await withFileEventPaths(ctx, [ctx.host.getPath(entry)], async () => {
|
|
128
|
+
if (!await ctx.vault.isTargetFile(entry.path)) {
|
|
129
|
+
ctx.host.log(`File ${entry.path} is not the target file`, LOG_LEVEL_VERBOSE);
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
if (ctx.vault.isFileSizeTooLarge(entry.size)) {
|
|
133
|
+
ctx.host.log(`File ${entry.path} is too large (on database) to be processed`, LOG_LEVEL_VERBOSE);
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
if (shouldBeIgnored(entry.path)) {
|
|
137
|
+
ctx.host.log(`File ${entry.path} should be ignored`, LOG_LEVEL_VERBOSE);
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
const path = ctx.host.getPath(entry);
|
|
141
|
+
const targetFile = await ctx.host.storage().getStub(ctx.host.getPathWithoutPrefix(entry));
|
|
142
|
+
if (isFolderInfo(targetFile)) {
|
|
143
|
+
ctx.host.log(`${path} is already exist as the folder`);
|
|
144
|
+
return true;
|
|
145
|
+
} else {
|
|
146
|
+
if (targetFile && ctx.vault.isFileSizeTooLarge(targetFile.stat.size)) {
|
|
147
|
+
ctx.host.log(`File ${targetFile.path} is too large (on storage) to be processed`, LOG_LEVEL_VERBOSE);
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
ctx.host.log(
|
|
151
|
+
`Processing ${path} (${entry._id.substring(0, 8)} :${entry._rev?.substring(0, 5)}) : Started...`,
|
|
152
|
+
LOG_LEVEL_VERBOSE
|
|
153
|
+
);
|
|
154
|
+
ctx.events.emitEvent(EVENT_CONFLICT_CANCELLED, path);
|
|
155
|
+
const ret = await dbToStorageCore(ctx, entry, targetFile);
|
|
156
|
+
ctx.host.log(`Processing ${path} (${entry._id.substring(0, 8)} :${entry._rev?.substring(0, 5)}) : Done`);
|
|
157
|
+
return ret;
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
async function planSameDocumentRename(ctx, newPath, oldPath) {
|
|
162
|
+
const currentTarget = getExactCurrentFile(ctx, await ctx.host.storage().getStub(newPath), newPath);
|
|
163
|
+
if (!currentTarget || !await isCurrentFileSelected(ctx, currentTarget)) {
|
|
164
|
+
return { kind: "none" };
|
|
165
|
+
}
|
|
166
|
+
const entry = await ctx.host.db().fetchEntryMeta(newPath, void 0, true);
|
|
167
|
+
if (entry && !entry.deleted && !entry._deleted && ctx.host.storage().normalisePath(ctx.host.getPath(entry)) === ctx.host.storage().normalisePath(newPath)) {
|
|
168
|
+
return { kind: "store", file: currentTarget };
|
|
169
|
+
}
|
|
170
|
+
return { kind: "rename", file: currentTarget, oldPath };
|
|
171
|
+
}
|
|
172
|
+
async function planCrossDocumentRename(ctx, newPath, oldPath) {
|
|
173
|
+
const currentTargetItem = await ctx.host.storage().getStub(newPath);
|
|
174
|
+
let currentSourceItem = null;
|
|
175
|
+
let sourceWasInspected = true;
|
|
176
|
+
try {
|
|
177
|
+
currentSourceItem = await ctx.host.storage().getStub(oldPath);
|
|
178
|
+
} catch (ex) {
|
|
179
|
+
sourceWasInspected = false;
|
|
180
|
+
logRestoredEventValidationFailure(ctx, oldPath, ex);
|
|
181
|
+
}
|
|
182
|
+
const currentTarget = getExactCurrentFile(ctx, currentTargetItem, newPath);
|
|
183
|
+
if (currentTargetItem !== null) {
|
|
184
|
+
if (!currentTarget || !await isCurrentFileSelected(ctx, currentTarget)) {
|
|
185
|
+
return { kind: "none" };
|
|
186
|
+
}
|
|
187
|
+
if (!sourceWasInspected || currentSourceItem !== null || !await canApplyRestoredDeletion(ctx, oldPath)) {
|
|
188
|
+
return { kind: "store", file: currentTarget, complete: sourceWasInspected };
|
|
189
|
+
}
|
|
190
|
+
const sourceEntry = await ctx.host.db().fetchEntryMeta(oldPath, void 0, true);
|
|
191
|
+
if (!sourceEntry || sourceEntry.deleted || sourceEntry._deleted) {
|
|
192
|
+
return { kind: "store", file: currentTarget };
|
|
193
|
+
}
|
|
194
|
+
return { kind: "rename", file: currentTarget, oldPath };
|
|
195
|
+
}
|
|
196
|
+
if (!sourceWasInspected) return { kind: "unresolved" };
|
|
197
|
+
if (currentSourceItem !== null || !await canApplyRestoredDeletion(ctx, oldPath)) {
|
|
198
|
+
return { kind: "none" };
|
|
199
|
+
}
|
|
200
|
+
return { kind: "delete", path: oldPath };
|
|
201
|
+
}
|
|
202
|
+
export {
|
|
203
|
+
processFileEvent,
|
|
204
|
+
processReplicatedDocument
|
|
205
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { FilePathWithPrefix } from "../../common/types.js";
|
|
2
|
+
import type { FileHandlerContext } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Serialise related document identities and deliver deferred conflict hooks after unlocking.
|
|
5
|
+
*
|
|
6
|
+
* @param ctx - The captured services, per-instance state, and dynamic host seams.
|
|
7
|
+
* @param eventPaths - All storage paths whose document identities must be locked.
|
|
8
|
+
* @param callback - Work performed under the deduplicated, sorted document locks.
|
|
9
|
+
* @typeParam T - The callback result returned after locks are released and deferred hooks have run.
|
|
10
|
+
* @returns The callback result, unless its error or a later conflict-hook error must propagate.
|
|
11
|
+
* @remarks Document IDs are deduplicated and sorted. Conflict hooks run after every lock is released; a callback failure retains precedence over hook failure.
|
|
12
|
+
*/
|
|
13
|
+
export declare function withFileEventPaths<T>(ctx: FileHandlerContext, eventPaths: readonly FilePathWithPrefix[], callback: (isSameDocument: boolean) => Promise<T>): Promise<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Defer conflict hooks within a document lock or deliver them immediately outside it.
|
|
16
|
+
*
|
|
17
|
+
* @param ctx - The captured services, per-instance state, and dynamic host seams.
|
|
18
|
+
* @param path - The affected file or provenance path.
|
|
19
|
+
* @param ifOpen - Whether to request only an already-open conflict review.
|
|
20
|
+
* @returns Completion after queueing or immediate hook delivery; immediate lookup or hook failures propagate.
|
|
21
|
+
* @remarks This helper neither sorts identities nor acquires a lock. It appends to an existing per-document deferred queue, or invokes the selected hook immediately when no queue is active.
|
|
22
|
+
*/
|
|
23
|
+
export declare function queueConflictCheckFor(ctx: FileHandlerContext, path: FilePathWithPrefix, ifOpen?: boolean): Promise<void>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { withLocks } from "octagonal-wheels/concurrency/lock";
|
|
2
|
+
async function withFileEventPaths(ctx, eventPaths, callback) {
|
|
3
|
+
const documentIds = await Promise.all(eventPaths.map((eventPath) => ctx.path.path2id(eventPath)));
|
|
4
|
+
const uniqueIds = [...new Set(documentIds)].sort();
|
|
5
|
+
const lockKeys = uniqueIds.map((documentId) => `processFileEvent-${documentId}`);
|
|
6
|
+
const pending = [];
|
|
7
|
+
let result;
|
|
8
|
+
let failed = false;
|
|
9
|
+
let failure;
|
|
10
|
+
try {
|
|
11
|
+
result = await withLocks(lockKeys, async () => {
|
|
12
|
+
for (const id of uniqueIds) ctx.state.deferredConflictChecks.set(id, pending);
|
|
13
|
+
try {
|
|
14
|
+
return await callback(documentIds.length === 2 && documentIds[0] === documentIds[1]);
|
|
15
|
+
} finally {
|
|
16
|
+
for (const id of uniqueIds) ctx.state.deferredConflictChecks.delete(id);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
} catch (ex) {
|
|
20
|
+
failed = true;
|
|
21
|
+
failure = ex;
|
|
22
|
+
}
|
|
23
|
+
for (const check of pending) {
|
|
24
|
+
try {
|
|
25
|
+
if (check.ifOpen) await ctx.conflict.queueCheckForIfOpen(check.path);
|
|
26
|
+
else await ctx.conflict.queueCheckFor(check.path);
|
|
27
|
+
} catch (ex) {
|
|
28
|
+
if (!failed) {
|
|
29
|
+
failed = true;
|
|
30
|
+
failure = ex;
|
|
31
|
+
}
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (failed) throw failure;
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
async function queueConflictCheckFor(ctx, path, ifOpen = false) {
|
|
39
|
+
const documentId = await ctx.path.path2id(path);
|
|
40
|
+
const pending = ctx.state.deferredConflictChecks.get(documentId);
|
|
41
|
+
if (pending) {
|
|
42
|
+
pending.push({ path, ifOpen });
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (ifOpen) await ctx.conflict.queueCheckForIfOpen(path);
|
|
46
|
+
else await ctx.conflict.queueCheckFor(path);
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
queueConflictCheckFor,
|
|
50
|
+
withFileEventPaths
|
|
51
|
+
};
|