@vrtmrz/livesync-commonlib 0.6.1 → 0.8.0
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 +29 -19
- package/dist/common/models/shared.definition.d.ts +10 -1
- package/dist/common/models/shared.definition.js +4 -3
- package/dist/events/coreEvents.d.ts +3 -0
- package/dist/events/coreEvents.js +2 -0
- package/dist/interfaces/FileReflectionProvenance.js +1 -0
- package/dist/interfaces/KeyValueDatabase.d.ts +1 -1
- package/dist/managers/EntryManager/EntryManager.d.ts +1 -1
- package/dist/managers/EntryManager/EntryManager.js +2 -2
- package/dist/managers/EntryManager/EntryManagerImpls.d.ts +1 -1
- package/dist/managers/EntryManager/EntryManagerImpls.js +80 -8
- package/dist/managers/StorageEventManager/admission.d.ts +16 -3
- package/dist/managers/StorageEventManager/admission.js +18 -4
- package/dist/managers/StorageEventManager/processing.d.ts +2 -2
- package/dist/managers/StorageEventManager/processing.js +3 -3
- package/dist/managers/StorageEventManager/queue.d.ts +23 -14
- package/dist/managers/StorageEventManager/queue.js +70 -30
- package/dist/managers/StorageEventManager/snapshot.d.ts +12 -1
- package/dist/managers/StorageEventManager/snapshot.js +74 -0
- package/dist/managers/StorageEventManager/state.d.ts +27 -5
- package/dist/managers/StorageEventManager/state.js +9 -7
- package/dist/managers/StorageEventManager.d.ts +8 -0
- package/dist/managers/StorageEventManager.js +136 -18
- package/dist/managers/adapters/IStorageEventManagerAdapter.d.ts +3 -1
- package/dist/managers/adapters/IStorageEventPersistenceAdapter.d.ts +16 -0
- package/dist/managers/adapters/index.d.ts +1 -1
- package/dist/pouchdb/LiveSyncLocalDB.d.ts +1 -1
- package/dist/pouchdb/LiveSyncLocalDB.js +2 -2
- package/dist/replication/LiveSyncAbstractReplicator.d.ts +7 -0
- package/dist/replication/LiveSyncAbstractReplicator.js +5 -2
- package/dist/replication/ReplicatorInstance.d.ts +3 -0
- package/dist/replication/ReplicatorProvider.d.ts +23 -2
- package/dist/replication/ReplicatorProvider.js +10 -0
- package/dist/replication/SyncParamsHandler.js +26 -25
- package/dist/replication/couchdb/LiveSyncReplicator.d.ts +14 -2
- package/dist/replication/couchdb/LiveSyncReplicator.js +247 -122
- package/dist/replication/index.d.ts +4 -2
- package/dist/replication/index.js +6 -0
- package/dist/replication/remoteTransportFailure.d.ts +24 -0
- package/dist/replication/remoteTransportFailure.js +80 -0
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.d.ts +1 -1
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.js +2 -2
- package/dist/replication/trystero/TrysteroRpcRoom.d.ts +14 -0
- package/dist/replication/trystero/TrysteroRpcRoom.js +168 -0
- package/dist/replication/trystero/rpcCompat.d.ts +1 -0
- package/dist/replication/trystero/rpcCompat.js +15 -0
- package/dist/rpc/RpcRoom.d.ts +1 -0
- package/dist/rpc/RpcRoom.js +21 -9
- package/dist/rpc/RpcSession.d.ts +8 -13
- package/dist/rpc/RpcSession.js +14 -35
- package/dist/rpc/commonlibRpcRoom.d.ts +1 -0
- package/dist/rpc/commonlibRpcRoom.js +4 -0
- package/dist/rpc/errors.d.ts +9 -3
- package/dist/rpc/errors.js +13 -16
- package/dist/rpc/index.d.ts +10 -1
- package/dist/rpc/index.js +2 -1
- package/dist/rpc/pouchdb/RpcPouchDBProxy.d.ts +1 -1
- package/dist/rpc/pouchdb/RpcPouchDBServer.d.ts +1 -1
- package/dist/serviceFeatures/targetFilter.d.ts +3 -1
- package/dist/serviceFeatures/targetFilter.js +2 -2
- package/dist/serviceModules/ServiceFileHandlerBase/protection.d.ts +5 -2
- package/dist/serviceModules/ServiceFileHandlerBase/protection.js +24 -4
- package/dist/serviceModules/ServiceFileHandlerBase/provenance.d.ts +28 -0
- package/dist/serviceModules/ServiceFileHandlerBase/provenance.js +53 -0
- package/dist/serviceModules/ServiceFileHandlerBase/reflection.js +59 -3
- package/dist/services/base/IService.d.ts +9 -6
- package/dist/services/base/KeyValueDBService.d.ts +6 -6
- package/dist/services/base/ReadinessEvaluator.d.ts +7 -6
- package/dist/services/base/ReadinessEvaluator.js +4 -2
- package/dist/services/base/RemoteConnection.d.ts +4 -0
- package/dist/services/base/RemoteService.d.ts +5 -3
- package/dist/services/base/RemoteService.js +85 -33
- package/dist/services/base/ReplicationService.d.ts +10 -2
- package/dist/services/base/ReplicationService.js +13 -7
- package/dist/services/base/ReplicationService.readiness.d.ts +14 -4
- package/dist/services/base/ReplicationService.readiness.js +29 -2
- package/dist/services/base/ReplicationService.typedReplication.d.ts +5 -2
- package/dist/services/base/ReplicationService.typedReplication.js +67 -6
- package/dist/services/base/ReplicationStaticsOwner.d.ts +18 -0
- package/dist/services/base/ReplicationStaticsOwner.js +33 -0
- package/dist/services/base/ReplicatorService.d.ts +2 -10
- package/dist/services/base/ReplicatorService.js +6 -3
- package/dist/services/base/VaultService.d.ts +1 -0
- package/dist/string_and_binary/chunks.d.ts +1 -1
- package/dist/string_and_binary/chunks.js +63 -50
- package/dist/worker/WorkerPushStreamProducer.d.ts +45 -0
- package/dist/worker/WorkerPushStreamProducer.js +299 -0
- package/dist/worker/WorkerPushStreamReader.d.ts +28 -0
- package/dist/worker/WorkerPushStreamReader.js +179 -0
- package/dist/worker/backgroundWorkerRpcContract.d.ts +18 -0
- package/dist/worker/backgroundWorkerRpcContract.js +24 -0
- package/dist/worker/bg.worker.js +2 -14
- package/dist/worker/bg.worker.splitting.d.ts +3 -6
- package/dist/worker/bg.worker.splitting.js +65 -30
- package/dist/worker/bgWorker.d.ts +9 -11
- package/dist/worker/bgWorker.direct.d.ts +5 -28
- package/dist/worker/bgWorker.direct.js +32 -2
- package/dist/worker/bgWorker.errors.d.ts +1 -0
- package/dist/worker/bgWorker.errors.js +12 -0
- package/dist/worker/bgWorker.js +718 -268
- package/dist/worker/bgWorker.splitting.d.ts +12 -27
- package/dist/worker/bgWorker.splitting.js +114 -110
- package/dist/worker/outOfBandCancelIterator.d.ts +2 -0
- package/dist/worker/outOfBandCancelIterator.js +8 -0
- package/dist/worker/serveBackgroundWorker.d.ts +3 -0
- package/dist/worker/serveBackgroundWorker.js +35 -0
- package/dist/worker/splitConnectionTracker.d.ts +25 -0
- package/dist/worker/splitConnectionTracker.js +73 -0
- package/dist/worker/splitGate.d.ts +19 -0
- package/dist/worker/splitGate.js +71 -0
- package/dist/worker/universalTypes.d.ts +1 -19
- package/dist/worker/universalTypes.js +0 -4
- package/dist/worker/workerPushStreamProtocol.d.ts +68 -0
- package/dist/worker/workerPushStreamProtocol.js +143 -0
- package/docs/development.md +75 -32
- package/docs/p2p-transport-lifecycle.md +2 -3
- package/docs/storage-events-and-reflection.md +40 -5
- package/package.json +3 -2
- package/dist/worker/bg.common.d.ts +0 -2
- package/dist/worker/bg.common.js +0 -6
- package/dist/worker/bg.worker.encryption.d.ts +0 -7
- package/dist/worker/bg.worker.encryption.js +0 -30
- package/dist/worker/bgWorker.encryption.d.ts +0 -22
- package/dist/worker/bgWorker.encryption.js +0 -40
|
@@ -11,15 +11,15 @@ import {
|
|
|
11
11
|
retainFileEvent
|
|
12
12
|
} from "./state.js";
|
|
13
13
|
function enqueueStorageEvent(state, event, effects) {
|
|
14
|
-
|
|
15
|
-
if (
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
effects.
|
|
14
|
+
const events = event.type === "DELETE" || event.type === "RENAME" ? [{ type: TYPE_SENTINEL_FLUSH }, event] : [event];
|
|
15
|
+
if (state.ordered) {
|
|
16
|
+
for (const item of events) retainFileEvent(state, item, false);
|
|
17
|
+
effects.updateStatus();
|
|
18
|
+
fireAndForget(effects.takeSnapshot());
|
|
19
|
+
return;
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
-
operation
|
|
22
|
-
effects.buffer.append(event);
|
|
21
|
+
publishBufferedEvents(state, events, effects.buffer);
|
|
22
|
+
const operation = state.operations.get(event);
|
|
23
23
|
const attempt = operation.attempt;
|
|
24
24
|
effects.updateStatus();
|
|
25
25
|
fireAndForget(async () => {
|
|
@@ -34,6 +34,16 @@ function enqueueStorageEvent(state, event, effects) {
|
|
|
34
34
|
}
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
|
+
function publishBufferedEvents(state, events, buffer) {
|
|
38
|
+
retainBufferedFileEvents(state, events);
|
|
39
|
+
for (const event of events) buffer.append(event);
|
|
40
|
+
}
|
|
41
|
+
function publishCommittedStorageEvents(state, events, effects) {
|
|
42
|
+
publishBufferedEvents(state, events, effects.buffer);
|
|
43
|
+
fireAndForget(
|
|
44
|
+
Promise.resolve().then(() => effects.runQueuedEvents()).catch(effects.reportFailure)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
37
47
|
function mergeAdmittedAttempts(boundary, admissions) {
|
|
38
48
|
for (const admission of admissions) {
|
|
39
49
|
for (const [operation, attempt] of admission.operations) {
|
|
@@ -41,13 +51,19 @@ function mergeAdmittedAttempts(boundary, admissions) {
|
|
|
41
51
|
}
|
|
42
52
|
}
|
|
43
53
|
}
|
|
44
|
-
async function flushStorageEvents(state, effects) {
|
|
54
|
+
async function flushStorageEvents(state, effects, loading) {
|
|
45
55
|
const admissions = [...state.admissions];
|
|
46
56
|
for (const admission of admissions) admission.flushRequested = true;
|
|
47
|
-
retainBufferedFileEvents(state, effects.buffer.read());
|
|
57
|
+
if (!state.ordered) retainBufferedFileEvents(state, effects.buffer.read());
|
|
48
58
|
const operations = pendingFileOperations(state);
|
|
49
59
|
effects.release(operations);
|
|
50
60
|
const boundary = effects.capture(operations, state.nextFailureSequence);
|
|
61
|
+
if (loading) {
|
|
62
|
+
for (const operation of await loading) {
|
|
63
|
+
if (!boundary.has(operation)) boundary.set(operation, operation.attempt);
|
|
64
|
+
}
|
|
65
|
+
effects.release([...boundary.keys()]);
|
|
66
|
+
}
|
|
51
67
|
const admitted = await Promise.allSettled(admissions.map((admission) => admission.completion));
|
|
52
68
|
mergeAdmittedAttempts(boundary, admissions);
|
|
53
69
|
effects.release([...boundary.keys()]);
|
|
@@ -58,15 +74,21 @@ async function flushStorageEvents(state, effects) {
|
|
|
58
74
|
assertSuccessfulFileEvents(results);
|
|
59
75
|
}
|
|
60
76
|
async function dispatchBufferedEvents(state, effects) {
|
|
61
|
-
while (
|
|
62
|
-
const event = effects.buffer.
|
|
77
|
+
while (true) {
|
|
78
|
+
const event = effects.buffer.peek();
|
|
79
|
+
if (event === void 0) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
63
82
|
const entry = retainFileEvent(state, event);
|
|
83
|
+
if (!effects.buffer.takeHead(event)) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
64
86
|
if (event.type === TYPE_SENTINEL_FLUSH) {
|
|
65
87
|
entry.dispatched = true;
|
|
66
88
|
const results = await effects.wait(effects.capture(pendingFileOperations(state, entry.sequence)));
|
|
67
|
-
if (results.every((result) => result.ok)) {
|
|
89
|
+
if (effects.acknowledgeSentinel || results.every((result) => result.ok)) {
|
|
68
90
|
try {
|
|
69
|
-
await effects.takeSnapshot();
|
|
91
|
+
await (effects.acknowledgeSentinel?.(entry) ?? effects.takeSnapshot());
|
|
70
92
|
} catch (error) {
|
|
71
93
|
effects.reportFailure(error);
|
|
72
94
|
}
|
|
@@ -78,16 +100,14 @@ async function dispatchBufferedEvents(state, effects) {
|
|
|
78
100
|
}
|
|
79
101
|
}
|
|
80
102
|
async function runBufferedStorageEvents(state, effects, options = {}) {
|
|
81
|
-
retainBufferedFileEvents(state, effects.buffer.read());
|
|
82
|
-
const accepted = pendingFileOperations(state);
|
|
103
|
+
if (!state.ordered) retainBufferedFileEvents(state, effects.buffer.read());
|
|
104
|
+
const accepted = options.waitForProcessing ? pendingFileOperations(state) : void 0;
|
|
83
105
|
await shareRunningResult(state.dispatchKey, () => dispatchBufferedEvents(state, effects));
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}
|
|
106
|
+
if (!accepted) return;
|
|
107
|
+
const results = await effects.wait(effects.capture(accepted));
|
|
108
|
+
if (state.ordered) return;
|
|
109
|
+
for (const result of results) {
|
|
110
|
+
if (result.ok === false) effects.reportFailure(result.error);
|
|
91
111
|
}
|
|
92
112
|
}
|
|
93
113
|
async function restoreStorageEvents(effects) {
|
|
@@ -96,18 +116,38 @@ async function restoreStorageEvents(effects) {
|
|
|
96
116
|
effects.log("No snapshot to restore", LOG_LEVEL_VERBOSE);
|
|
97
117
|
return;
|
|
98
118
|
}
|
|
99
|
-
effects
|
|
100
|
-
effects.buffer.replace(
|
|
101
|
-
snapshot.map(
|
|
102
|
-
(event) => event.type === TYPE_SENTINEL_FLUSH ? event : { ...event, restoredFromPreviousRuntime: true, skipBatchWait: true }
|
|
103
|
-
)
|
|
104
|
-
);
|
|
105
|
-
effects.updateStatus();
|
|
119
|
+
replayStorageEvents(effects, snapshot.map(markRestoredEvent));
|
|
106
120
|
await effects.runQueuedEvents({ waitForProcessing: true });
|
|
107
121
|
}
|
|
122
|
+
function restoreOrderedStorageEvents(state, effects, buffers) {
|
|
123
|
+
const events = [];
|
|
124
|
+
const operations = [];
|
|
125
|
+
for (const { key, events: saved } of buffers) {
|
|
126
|
+
const buffer = { key, remaining: saved.length };
|
|
127
|
+
for (const event of saved) {
|
|
128
|
+
const restored = event.type === TYPE_SENTINEL_FLUSH ? { ...event } : markRestoredEvent(event);
|
|
129
|
+
const entry = retainFileEvent(state, restored, true, buffer);
|
|
130
|
+
events.push(restored);
|
|
131
|
+
if (entry.operation) operations.push(entry.operation);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
retainBufferedFileEvents(state, events);
|
|
135
|
+
replayStorageEvents(effects, events);
|
|
136
|
+
return operations;
|
|
137
|
+
}
|
|
138
|
+
function markRestoredEvent(event) {
|
|
139
|
+
return event.type === TYPE_SENTINEL_FLUSH ? event : { ...event, restoredFromPreviousRuntime: true, skipBatchWait: true };
|
|
140
|
+
}
|
|
141
|
+
function replayStorageEvents(effects, events) {
|
|
142
|
+
effects.log(`Restoring storage operation snapshot: ${events.length} items`, LOG_LEVEL_VERBOSE);
|
|
143
|
+
effects.buffer.replace(events);
|
|
144
|
+
effects.updateStatus();
|
|
145
|
+
}
|
|
108
146
|
export {
|
|
109
147
|
enqueueStorageEvent,
|
|
110
148
|
flushStorageEvents,
|
|
149
|
+
publishCommittedStorageEvents,
|
|
150
|
+
restoreOrderedStorageEvents,
|
|
111
151
|
restoreStorageEvents,
|
|
112
152
|
runBufferedStorageEvents
|
|
113
153
|
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* The snapshot tail keeps provisional marking, save, rollback, and in-memory acknowledgement in one ordered step.
|
|
4
4
|
*/
|
|
5
5
|
import type { FileEventItem } from "../../common/types.js";
|
|
6
|
-
import { type PendingSnapshotEntry, type QueuedFileEvent, type SnapshotCompletion, type StorageEventBuffer, type StorageEventState } from "./state.js";
|
|
6
|
+
import { type OrderedStorageEventState, type PendingSnapshotEntry, type QueuedFileEvent, type SnapshotCompletion, type StorageEventBuffer, type StorageEventState } from "./state.js";
|
|
7
7
|
/** Snapshot persistence and status callbacks supplied by the owning manager. */
|
|
8
8
|
export interface StorageEventSnapshotIO {
|
|
9
9
|
/** Save the currently pending event intents using the existing snapshot format. */
|
|
@@ -44,3 +44,14 @@ export declare function planFileEventSnapshot(entries: readonly PendingSnapshotE
|
|
|
44
44
|
* this caller; the caught tail allows later saves to proceed.
|
|
45
45
|
*/
|
|
46
46
|
export declare function persistFileEventSnapshot(state: StorageEventState, buffer: StorageEventBuffer, io: StorageEventSnapshotIO, completion?: SnapshotCompletion): Promise<void>;
|
|
47
|
+
/** Publish committed events and report an array that could not be queued. */
|
|
48
|
+
export interface OrderedStorageEventAppendEffects {
|
|
49
|
+
publish(events: readonly QueuedFileEvent[]): void;
|
|
50
|
+
reportAppendFailure(error: unknown): void;
|
|
51
|
+
}
|
|
52
|
+
/** Save the next buffer, coalescing requests until the writer detaches it. */
|
|
53
|
+
export declare function appendPendingEventBuffer(state: StorageEventState, ordered: OrderedStorageEventState, effects: OrderedStorageEventAppendEffects): Promise<void>;
|
|
54
|
+
/** Acknowledge one entry in memory, deleting its buffer only when the last entry completes. */
|
|
55
|
+
export declare function acknowledgeOrderedEntry(state: StorageEventState, ordered: OrderedStorageEventState, entry: PendingSnapshotEntry, updateStatus: () => void, completion?: SnapshotCompletion): Promise<void>;
|
|
56
|
+
/** Retire a saved buffer with no effects to replay. */
|
|
57
|
+
export declare function acknowledgeEmptyOrderedBuffer(state: StorageEventState, ordered: OrderedStorageEventState, key: string): Promise<void>;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { delay } from "../../common/utils.js";
|
|
1
2
|
import { assertFileEventAdmission } from "./operations.js";
|
|
2
3
|
import {
|
|
4
|
+
failFileEventAttempt,
|
|
3
5
|
retainBufferedFileEvents,
|
|
4
6
|
retainFileEvent
|
|
5
7
|
} from "./state.js";
|
|
@@ -47,7 +49,79 @@ async function persistFileEventSnapshot(state, buffer, io, completion) {
|
|
|
47
49
|
});
|
|
48
50
|
await save;
|
|
49
51
|
}
|
|
52
|
+
const APPEND_YIELD_MS = 50;
|
|
53
|
+
function writeOrderedStore(state, write) {
|
|
54
|
+
const done = state.snapshotTail.then(write);
|
|
55
|
+
state.snapshotTail = done.catch(() => {
|
|
56
|
+
});
|
|
57
|
+
return done;
|
|
58
|
+
}
|
|
59
|
+
function discardStagedEntries(state, ordered, entries, error) {
|
|
60
|
+
for (const { event, operation } of entries) {
|
|
61
|
+
state.pendingSnapshot.delete(event);
|
|
62
|
+
if (operation) {
|
|
63
|
+
state.operations.delete(operation.event);
|
|
64
|
+
ordered.pendingOperations--;
|
|
65
|
+
failFileEventAttempt(state, operation.attempt, error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function appendPendingEventBuffer(state, ordered, effects) {
|
|
70
|
+
if (ordered.nextWrite) return ordered.nextWrite;
|
|
71
|
+
const previous = ordered.appendTail;
|
|
72
|
+
if (ordered.nextBuffer.length === 0) return previous ?? Promise.resolve();
|
|
73
|
+
const write = (previous?.catch(() => {
|
|
74
|
+
}) ?? Promise.resolve()).then(async () => {
|
|
75
|
+
if (ordered.appendFinishedAt !== void 0) {
|
|
76
|
+
const elapsed = Math.max(0, Date.now() - ordered.appendFinishedAt);
|
|
77
|
+
const remainingYield = APPEND_YIELD_MS - elapsed;
|
|
78
|
+
if (remainingYield > 0) await delay(remainingYield);
|
|
79
|
+
}
|
|
80
|
+
await writeOrderedStore(state, async () => {
|
|
81
|
+
const entries = ordered.nextBuffer;
|
|
82
|
+
ordered.nextBuffer = [];
|
|
83
|
+
ordered.nextWrite = void 0;
|
|
84
|
+
const events = entries.map(({ event }) => event);
|
|
85
|
+
let key;
|
|
86
|
+
try {
|
|
87
|
+
key = await ordered.store.append(events);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
discardStagedEntries(state, ordered, entries, error);
|
|
90
|
+
effects.reportAppendFailure(error);
|
|
91
|
+
throw error;
|
|
92
|
+
} finally {
|
|
93
|
+
if (ordered.appendTail === write) ordered.appendTail = void 0;
|
|
94
|
+
ordered.appendFinishedAt = Date.now();
|
|
95
|
+
}
|
|
96
|
+
const buffer = { key, remaining: entries.length };
|
|
97
|
+
for (const entry of entries) entry.savedBuffer = buffer;
|
|
98
|
+
effects.publish(events);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
ordered.nextWrite = write;
|
|
102
|
+
ordered.appendTail = write;
|
|
103
|
+
return write;
|
|
104
|
+
}
|
|
105
|
+
function acknowledgeOrderedEntry(state, ordered, entry, updateStatus, completion) {
|
|
106
|
+
return writeOrderedStore(state, async () => {
|
|
107
|
+
if (completion) assertFileEventAdmission(completion.attempt);
|
|
108
|
+
if (state.pendingSnapshot.get(entry.event) !== entry) return;
|
|
109
|
+
if (completion && entry.operation?.attempt !== completion.attempt) return;
|
|
110
|
+
const buffer = entry.savedBuffer;
|
|
111
|
+
if (buffer.remaining === 1) await ordered.store.remove(buffer.key);
|
|
112
|
+
buffer.remaining--;
|
|
113
|
+
if (entry.operation) ordered.pendingOperations--;
|
|
114
|
+
state.pendingSnapshot.delete(entry.event);
|
|
115
|
+
updateStatus();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function acknowledgeEmptyOrderedBuffer(state, ordered, key) {
|
|
119
|
+
return writeOrderedStore(state, () => ordered.store.remove(key));
|
|
120
|
+
}
|
|
50
121
|
export {
|
|
122
|
+
acknowledgeEmptyOrderedBuffer,
|
|
123
|
+
acknowledgeOrderedEntry,
|
|
124
|
+
appendPendingEventBuffer,
|
|
51
125
|
persistFileEventSnapshot,
|
|
52
126
|
planFileEventSnapshot
|
|
53
127
|
};
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Snapshot writes are delegated to the snapshot module; these state transitions do not commit them.
|
|
4
4
|
*/
|
|
5
5
|
import type { FileEventItem, FilePath } from "../../common/types.js";
|
|
6
|
+
import type { PendingStorageEventStore } from "../adapters/index.js";
|
|
6
7
|
import { type FileEventAttempt, type PendingFileEvent } from "./operations.js";
|
|
7
8
|
/** Queue marker which closes the ordered boundary before a delete or rename. */
|
|
8
9
|
export declare const TYPE_SENTINEL_FLUSH = "SENTINEL_FLUSH";
|
|
@@ -25,12 +26,19 @@ export type EventAdmission = {
|
|
|
25
26
|
/** Whether a flush has captured this admission, including operations it registers later. */
|
|
26
27
|
flushRequested: boolean;
|
|
27
28
|
};
|
|
29
|
+
/** Completion ownership shared by entries from one durable buffer. */
|
|
30
|
+
export type SavedEventBuffer = {
|
|
31
|
+
key: string;
|
|
32
|
+
remaining: number;
|
|
33
|
+
};
|
|
28
34
|
/** Queue and persistence view of one event, optionally linked to its durable operation. */
|
|
29
35
|
export type PendingSnapshotEntry = {
|
|
30
36
|
/** Event intent written to or restored from the existing snapshot format. */
|
|
31
37
|
event: QueuedFileEvent;
|
|
32
38
|
/** Stable insertion order within this manager's pending snapshot. */
|
|
33
39
|
sequence: number;
|
|
40
|
+
/** Buffer that durably holds this entry, once its append has committed. */
|
|
41
|
+
savedBuffer?: SavedEventBuffer;
|
|
34
42
|
/** Provisional completion within a serial snapshot step; a failed save rolls it back. */
|
|
35
43
|
completed: boolean;
|
|
36
44
|
/** Durable operation for file events; sentinels have no operation. */
|
|
@@ -47,6 +55,15 @@ export type SnapshotCompletion = {
|
|
|
47
55
|
export type RunQueuedEventsOptions = {
|
|
48
56
|
waitForProcessing?: boolean;
|
|
49
57
|
};
|
|
58
|
+
/** State for the ordered buffer store's append writer. */
|
|
59
|
+
export interface OrderedStorageEventState {
|
|
60
|
+
readonly store: PendingStorageEventStore;
|
|
61
|
+
nextBuffer: PendingSnapshotEntry[];
|
|
62
|
+
nextWrite?: Promise<void>;
|
|
63
|
+
appendTail?: Promise<void>;
|
|
64
|
+
appendFinishedAt?: number;
|
|
65
|
+
pendingOperations: number;
|
|
66
|
+
}
|
|
50
67
|
/** Mutable ownership, admission, and persistence state for one manager. */
|
|
51
68
|
export interface StorageEventState {
|
|
52
69
|
/** Ordered intents still requiring durable acknowledgement. */
|
|
@@ -63,7 +80,9 @@ export interface StorageEventState {
|
|
|
63
80
|
acceptingEvents: boolean;
|
|
64
81
|
/** Admissions in progress which a finite flush must account for. */
|
|
65
82
|
admissions: Set<EventAdmission>;
|
|
66
|
-
/**
|
|
83
|
+
/** Ordered buffer persistence, when supplied by this host. */
|
|
84
|
+
readonly ordered?: OrderedStorageEventState;
|
|
85
|
+
/** Serial tail for persistence mutations and completion bookkeeping. */
|
|
67
86
|
snapshotTail: Promise<void>;
|
|
68
87
|
/** Stable key so concurrent dispatch calls for this manager share one run. */
|
|
69
88
|
readonly dispatchKey: symbol;
|
|
@@ -76,8 +95,10 @@ export interface StorageEventBuffer {
|
|
|
76
95
|
replace(events: QueuedFileEvent[]): void;
|
|
77
96
|
/** Append one event or sentinel at the current tail. */
|
|
78
97
|
append(event: QueuedFileEvent): void;
|
|
79
|
-
/**
|
|
80
|
-
|
|
98
|
+
/** Return the current head, if any, without removing it. */
|
|
99
|
+
peek(): QueuedFileEvent | undefined;
|
|
100
|
+
/** Remove the expected item only if it is still the head. */
|
|
101
|
+
takeHead(expected: QueuedFileEvent): boolean;
|
|
81
102
|
}
|
|
82
103
|
/** Host notifications kept outside the state owner. */
|
|
83
104
|
export interface StorageEventNotifications {
|
|
@@ -91,19 +112,20 @@ export interface StorageEventNotifications {
|
|
|
91
112
|
* @returns Fresh maps, counters, admission tracking, and a resolved initial snapshot tail.
|
|
92
113
|
* @remarks The state is isolated to one manager; no host callbacks or persistence are invoked here.
|
|
93
114
|
*/
|
|
94
|
-
export declare function createStorageEventState(): StorageEventState;
|
|
115
|
+
export declare function createStorageEventState(store?: PendingStorageEventStore): StorageEventState;
|
|
95
116
|
/**
|
|
96
117
|
* Retain an event once and reuse its durable operation when the same event is encountered again.
|
|
97
118
|
* @param state - The manager state which owns the snapshot entry and event-to-operation identity map.
|
|
98
119
|
* @param event - A file event or ordering sentinel to retain.
|
|
99
120
|
* @param register - Whether a file operation should also enter the per-path ordering index.
|
|
121
|
+
* @param savedBuffer - The existing durable buffer when this event was restored.
|
|
100
122
|
* @returns The existing entry for this event object, or the newly created entry.
|
|
101
123
|
* @remarks Snapshot collection passes register as false for events surfaced by batch gates, so persistence
|
|
102
124
|
* discovery cannot become their first path-indexing moment and change path ownership or ordering. The registered
|
|
103
125
|
* flag independently prevents later duplicate indexing. Retention does not write a snapshot or start a handler;
|
|
104
126
|
* a new operation captures the current failure sequence as its boundary.
|
|
105
127
|
*/
|
|
106
|
-
export declare function retainFileEvent(state: StorageEventState, event: QueuedFileEvent, register?: boolean): PendingSnapshotEntry;
|
|
128
|
+
export declare function retainFileEvent(state: StorageEventState, event: QueuedFileEvent, register?: boolean, savedBuffer?: SavedEventBuffer): PendingSnapshotEntry;
|
|
107
129
|
/**
|
|
108
130
|
* Retain buffered events in order and attach an adjacent sentinel to its delete or rename.
|
|
109
131
|
* @param state - The manager state receiving entries and ordering barriers.
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createFileEventAttempt,
|
|
3
|
-
getFileEventPaths
|
|
4
|
-
} from "./operations.js";
|
|
1
|
+
import { createFileEventAttempt, getFileEventPaths } from "./operations.js";
|
|
5
2
|
const TYPE_SENTINEL_FLUSH = "SENTINEL_FLUSH";
|
|
6
|
-
function createStorageEventState() {
|
|
3
|
+
function createStorageEventState(store) {
|
|
7
4
|
return {
|
|
8
5
|
pendingSnapshot: /* @__PURE__ */ new Map(),
|
|
6
|
+
ordered: store ? { store, nextBuffer: [], pendingOperations: 0 } : void 0,
|
|
9
7
|
operations: /* @__PURE__ */ new WeakMap(),
|
|
10
8
|
latestByPath: /* @__PURE__ */ new Map(),
|
|
11
9
|
nextSequence: 0,
|
|
@@ -25,13 +23,13 @@ function registerOperation(state, operation) {
|
|
|
25
23
|
state.latestByPath.set(path, operation);
|
|
26
24
|
}
|
|
27
25
|
}
|
|
28
|
-
function retainFileEvent(state, event, register = true) {
|
|
26
|
+
function retainFileEvent(state, event, register = true, savedBuffer) {
|
|
29
27
|
const existing = state.pendingSnapshot.get(event);
|
|
30
28
|
if (existing) {
|
|
31
29
|
if (register && existing.operation) registerOperation(state, existing.operation);
|
|
32
30
|
return existing;
|
|
33
31
|
}
|
|
34
|
-
const entry = { event, sequence: ++state.nextSequence, completed: false };
|
|
32
|
+
const entry = { event, sequence: ++state.nextSequence, completed: false, savedBuffer };
|
|
35
33
|
if (event.type !== TYPE_SENTINEL_FLUSH) {
|
|
36
34
|
const operation = state.operations.get(event) ?? {
|
|
37
35
|
event,
|
|
@@ -50,6 +48,10 @@ function retainFileEvent(state, event, register = true) {
|
|
|
50
48
|
state.operations.set(event, operation);
|
|
51
49
|
}
|
|
52
50
|
state.pendingSnapshot.set(event, entry);
|
|
51
|
+
if (state.ordered) {
|
|
52
|
+
if (entry.operation) state.ordered.pendingOperations++;
|
|
53
|
+
if (!savedBuffer) state.ordered.nextBuffer.push(entry);
|
|
54
|
+
}
|
|
53
55
|
return entry;
|
|
54
56
|
}
|
|
55
57
|
function retainBufferedFileEvents(state, events) {
|
|
@@ -18,6 +18,8 @@ export type { FileEventItemSentinel } from "./StorageEventManager/state.js";
|
|
|
18
18
|
* @remarks These dependencies keep vault access, file processing, settings, and diagnostics at the adapter boundary.
|
|
19
19
|
*/
|
|
20
20
|
export interface StorageEventManagerBaseDependencies {
|
|
21
|
+
/** Apply the host's admission checks to optional-file events before queuing them. */
|
|
22
|
+
isTargetInternalFile?: (path: FilePath) => Promise<boolean>;
|
|
21
23
|
setting: SettingService;
|
|
22
24
|
vaultService: IVaultService;
|
|
23
25
|
fileProcessing: FileProcessingService;
|
|
@@ -57,6 +59,8 @@ export declare abstract class StorageEventManagerBase<TAdapter extends IStorageE
|
|
|
57
59
|
protected _waitingMap: Map<string, WaitInfo>;
|
|
58
60
|
protected processingCount: number;
|
|
59
61
|
private readonly eventState;
|
|
62
|
+
private orderedStoreLoaded;
|
|
63
|
+
private orderedStoreLoad?;
|
|
60
64
|
private dispatchPaused;
|
|
61
65
|
private dispatchResumeSignal;
|
|
62
66
|
private readonly activeDispatches;
|
|
@@ -67,6 +71,7 @@ export declare abstract class StorageEventManagerBase<TAdapter extends IStorageE
|
|
|
67
71
|
private readonly retry;
|
|
68
72
|
private readonly snapshotIO;
|
|
69
73
|
private readonly queue;
|
|
74
|
+
private readonly orderedAppend;
|
|
70
75
|
private readonly admission;
|
|
71
76
|
private readonly attempts;
|
|
72
77
|
private readonly handler;
|
|
@@ -151,6 +156,7 @@ export declare abstract class StorageEventManagerBase<TAdapter extends IStorageE
|
|
|
151
156
|
* Add an already selected event to the buffer and start background snapshotting and dispatch.
|
|
152
157
|
* @param event - The event which has already passed host admission checks.
|
|
153
158
|
* @returns Nothing; processing and persistence failures are reported through manager effects.
|
|
159
|
+
* @remarks Ordered stores must have completed their initial load. Append watcher events through `appendQueue`.
|
|
154
160
|
*/
|
|
155
161
|
enqueue(event: FileEventItem): void;
|
|
156
162
|
/**
|
|
@@ -291,6 +297,8 @@ export declare abstract class StorageEventManagerBase<TAdapter extends IStorageE
|
|
|
291
297
|
* @returns A promise for restoration; storage or dispatch errors reject it, while failed attempt results are reported and retained.
|
|
292
298
|
*/
|
|
293
299
|
_restoreFromSnapshot(): Promise<void>;
|
|
300
|
+
/** Join the initial load without waiting for restored handlers. */
|
|
301
|
+
private loadOrderedEvents;
|
|
294
302
|
/**
|
|
295
303
|
* Dispatch events in the buffer and optionally wait for captured processing attempts.
|
|
296
304
|
* @param options - Queue-run options, including whether to wait for processing.
|