Compare commits

...
Author SHA1 Message Date
Cursor Agentand陈大猫 4c65072bd2 fix(vault): do not restore a queued log deleted from memory
A queued add or update was written back when the id was already gone from
memory, which let another window keep that row after a later local delete.
Rebase now applies a changed id only while memory still has it.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 17:55:26 +00:00
Cursor Agentand陈大猫 0065c2d91a fix(vault): ignore same-window connection-log echoes
The deferred localStorage notification replays this window's own metadata
write. Applying it replaced connectionLogsRef with an older blob and dropped
logs that were added or finalized while that write was still queued.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 17:41:53 +00:00
Cursor Agentand陈大猫 c9be604297 fix(vault): rebase connection logs after the side-store await
A non-pruned write stored the pre-await snapshot and could replace logs
another window saved during the IndexedDB round trip. Rebase immediately
before that write, shrink the quota fallback until the newest replay fits,
and leave the revision unconsumed when IndexedDB cannot be read.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 17:19:29 +00:00
Cursor Agentand陈大猫 140f90c55b fix(vault): restore only locally changed queued logs
A queued persist was putting back every id from the pre-queue snapshot,
which undid another window's delete of a log this recipe never touched.
Track the ids the recipe added or replaced, and reload IndexedDB during
boot when the revision key moves across the migrate await so that peer
replay is hydrated before this window marks the revision seen.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 17:11:04 +00:00
Cursor Agentand陈大猫 589cdd50eb fix(vault): keep queued connection logs after a remote snapshot
A storage event can replace memory with another window's logs before the
queued write runs. Keep ids from the queued snapshot unless this snapshot
deleted them.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 17:01:55 +00:00
Cursor Agentand陈大猫 4f6c4e22d3 fix(vault): rebase queued connection-log writes onto the latest logs
A persist task can run after another window changes the log list. Write
the rebased list instead of the snapshot captured when the task was queued.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 16:54:22 +00:00
Cursor Agentand陈大猫 9d95d22b44 fix(vault): rebase the IndexedDB fallback onto the latest connection logs
When the side store rejects a close, attach this window's replay to the
connection-log blob currently on disk instead of writing the stale snapshot.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 16:34:07 +00:00
Cursor Agentand陈大猫 ee6867e1d4 fix(vault): migrate legacy replays without clearing newer IndexedDB rows
Insert missing legacy ids in one transaction instead of replaceAll, so a
replay committed after the empty read is not wiped.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 16:29:44 +00:00
Cursor Agentand陈大猫 f67f4a06a6 fix(vault): keep unmigrated connection-log replays until IndexedDB commits
Copy legacy replay ids that are missing from IndexedDB before deleting
the old blob, and do not treat an uncommitted legacy map as the diff
baseline.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 16:24:47 +00:00
Cursor Agentand陈大猫 78bd4b6a1d fix(vault): keep connection-log replay across boot races and IndexedDB failure
Re-read connection-log metadata after the IndexedDB load before pruning,
return the capture promise through TerminalLayer, and write the unpruned
log blob when the side store rejects the commit.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 16:19:20 +00:00
Cursor Agentand陈大猫 afe2218ce4 perf(vault): store connection-log replay blobs in IndexedDB
Move unsaved connection-log terminal replay out of the multi-megabyte
localStorage blob into one IndexedDB row per log id. Persist only the
changed ids, keep the newest 50, and signal other windows with a tiny
revision key.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 15:06:30 +00:00
17 changed files with 1433 additions and 331 deletions

No files matched your search

+5 -2
View File
@@ -1184,7 +1184,7 @@ export function handleConnectToHostImpl(getCtx: AppContextGetter, host: Host, hi
}
}
export function handleTerminalDataCaptureImpl(getCtx: AppContextGetter, sessionId: string, data: string) {
export async function handleTerminalDataCaptureImpl(getCtx: AppContextGetter, sessionId: string, data: string) {
const { IS_DEV, connectionLogs, selectConnectionLogForTerminalDataCapture, sessions, updateConnectionLog } = getCtx();
{
if (IS_DEV) console.log('[handleTerminalDataCapture] Called', { sessionId, dataLength: data.length });
@@ -1200,7 +1200,10 @@ export function handleTerminalDataCaptureImpl(getCtx: AppContextGetter, sessionI
if (IS_DEV) console.log('[handleTerminalDataCapture] Matching log', matchingLog);
if (matchingLog) {
updateConnectionLog(matchingLog.id, {
// The IndexedDB replay write commits asynchronously. Waiting here keeps
// a crash during session teardown from dropping the newest capture any
// more often than the old synchronous localStorage write.
await updateConnectionLog(matchingLog.id, {
endTime: Date.now(),
terminalData: data,
});
@@ -165,8 +165,12 @@ test('vault init cancels the superseded StrictMode effect before publishing read
);
const initAt = vaultSource.indexOf('let cancelled = false;');
assert.notEqual(initAt, -1, 'vault init must track cancellation');
const initSlice = vaultSource.slice(initAt, initAt + 12000);
assert.match(initSlice, /return \(\) => \{\s*\n\s*cancelled = true;\s*\n\s*\};/);
const initSlice = vaultSource.slice(initAt, initAt + 20000);
assert.match(
initSlice,
/return \(\) => \{\s*\n\s*cancelled = true;\s*\n\s*resolveTerminalDataReady\(\);\s*\n\s*\};/,
'StrictMode cleanup cancels init and unblocks the terminal-data ready promise',
);
assert.match(
initSlice,
/if \(!cancelled\) \{\s*\n\s*setIsInitialized\(true\);\s*\n\s*setVaultInitialized\(true\);\s*\n\s*\}/,
@@ -0,0 +1,60 @@
import assert from "node:assert/strict";
import test from "node:test";
import { readFileSync } from "node:fs";
const source = readFileSync(new URL("./useVaultState.ts", import.meta.url), "utf8");
const terminalLayerSource = readFileSync(
new URL("../../components/TerminalLayer.tsx", import.meta.url),
"utf8",
);
test("connection log replay blobs persist through IndexedDB instead of the legacy localStorage blob", () => {
assert.match(source, /await migrateLegacyConnectionLogTerminalData\(/);
assert.match(source, /await writeTerminalDataMapDiff\(/);
assert.match(source, /STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV/);
assert.match(source, /syncConnectionLogTerminalData !== false|syncConnectionLogTerminalData\)/);
assert.doesNotMatch(source, /writeTerminalDataMapWithQuotaRetry/);
assert.doesNotMatch(
source,
/localStorageAdapter\.write\(\s*STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA\s*,/,
);
});
test("close-path connection log updates await the indexedDB persist task", () => {
assert.match(
source,
/const updateConnectionLog = useCallback\([\s\S]*?return commitConnectionLogs\(/,
);
assert.match(source, /rebaseQueuedConnectionLogs\(\s*next,\s*removedIds,\s*changedIds,/);
assert.match(
source,
/if \(!options\?\.pruneMainBlob\) \{[\s\S]*?rebaseQueuedConnectionLogs\(/,
);
assert.match(source, /retainNewestUnsavedReplays\(fallbackLogs, keep\)/);
assert.match(source, /if \(!map\) return;/);
assert.match(
source,
/if \(key === STORAGE_KEY_CONNECTION_LOGS\) \{\s*\/\/ This window already updated connectionLogsRef before setItem\.[\s\S]*?return;\s*\}/,
);
assert.match(
terminalLayerSource,
/const handleTerminalDataCapture = useCallback\(\(sessionId: string, data: string\) => \{\s*return onTerminalDataCapture\?\.\(sessionId, data\);/,
);
});
test("boot re-reads connection logs after indexedDB load and falls back to the main blob", () => {
const revAt = source.indexOf("let revSeen = readTerminalDataRev()");
const bootAt = source.indexOf("const loaded = await migrateLegacyConnectionLogTerminalData");
assert.notEqual(revAt, -1);
assert.ok(revAt < bootAt);
const bootSlice = source.slice(bootAt, bootAt + 2800);
assert.match(bootSlice, /revNow !== revSeen[\s\S]*?await loadConnectionLogTerminalData\(\)/);
assert.match(
bootSlice,
/localStorageAdapter\.read<ConnectionLog\[\]>\(\s*STORAGE_KEY_CONNECTION_LOGS,\s*\)[\s\S]*?pruneTerminalDataMapForStorage\(connectionLogsForBoot/,
);
assert.match(
source,
/!sidePersisted && unsavedTerminalDataPending && mainPersisted\) \{\s*\/\/[\s\S]*?localStorageAdapter\.read<ConnectionLog\[\]>\(\s*STORAGE_KEY_CONNECTION_LOGS\s*\)[\s\S]*?mergeTerminalDataIntoLogs\(freshLogs, buildTerminalDataMapFromLogs\(logs\)\)/,
);
});
+319 -133
View File
@@ -37,6 +37,7 @@ import {
import {
STORAGE_KEY_CONNECTION_LOGS,
STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA,
STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV,
STORAGE_KEY_GROUP_CONFIGS,
STORAGE_KEY_GROUPS,
STORAGE_KEY_HOSTS,
@@ -65,9 +66,17 @@ import {
mergeTerminalDataIntoLogs,
mergeTerminalDataMapsForStorage,
pruneInMemoryUnsavedTerminalData,
writeTerminalDataMapWithQuotaRetry,
rebaseQueuedConnectionLogs,
retainNewestUnsavedReplays,
pruneTerminalDataMapForStorage,
type ConnectionLogTerminalDataMap,
} from "../../domain/connectionLogTerminalData";
import {
tryLoadAll as tryLoadConnectionLogTerminalData,
migrateLegacyConnectionLogTerminalData,
writeTerminalDataMapDiff,
type ConnectionLogTerminalDataRev,
} from "../../infrastructure/persistence/connectionLogTerminalDataStore";
import { getNextVaultOrder, normalizeVaultOrder } from "../../domain/vaultOrder";
import {
deleteSelectedSnippetsFromVault,
@@ -229,6 +238,13 @@ const safeParse = <T,>(value: string | null): T | null => {
}
};
/** `null` means the read failed. `{}` is a successful empty store. Retries once. */
const loadConnectionLogTerminalData = async (): Promise<ConnectionLogTerminalDataMap | null> => {
const first = await tryLoadConnectionLogTerminalData();
if (first) return first;
return tryLoadConnectionLogTerminalData();
};
/**
* Strip the bulky `terminalData` replay buffer from transient (unsaved)
* connection logs before persisting. `terminalData` is the full terminal
@@ -257,9 +273,6 @@ const readLegacyLineTimestampsEnabled = (): boolean => {
return stored?.showLineTimestamps === true;
};
const readConnectionLogTerminalDataMap = (): ConnectionLogTerminalDataMap =>
localStorageAdapter.read<ConnectionLogTerminalDataMap>(STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA) ?? {};
const readPersistedConnectionLogIds = (): Set<string> => {
const stored = localStorageAdapter.read<ConnectionLog[]>(STORAGE_KEY_CONNECTION_LOGS) ?? [];
return new Set(stored.map((log) => log.id));
@@ -278,45 +291,6 @@ const buildPersistedLogIds = (
return ids;
};
/**
* Same result as comparing `JSON.stringify` of both maps (own keys in the
* same order, equal values) without serializing the multi-MB replay data.
*/
const isSameTerminalDataMap = (
a: ConnectionLogTerminalDataMap,
b: ConnectionLogTerminalDataMap,
): boolean => {
if (typeof a !== "object" || a === null || Array.isArray(a)) {
return JSON.stringify(a) === JSON.stringify(b);
}
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) return false;
for (let i = 0; i < aKeys.length; i += 1) {
if (aKeys[i] !== bKeys[i] || a[aKeys[i]] !== b[bKeys[i]]) return false;
}
return true;
};
const writeConnectionLogTerminalDataMap = (
existing: ConnectionLogTerminalDataMap,
pruned: ConnectionLogTerminalDataMap,
logs: ConnectionLog[],
quotaFailedCharsRef: { current: number | null },
): boolean => {
if (isSameTerminalDataMap(existing, pruned)) return true;
// On quota failure keep the newest replay buffers instead of none.
// The hint stays on the hook so the domain helper remains a pure calculation.
const result = writeTerminalDataMapWithQuotaRetry(
logs,
pruned,
(next) => localStorageAdapter.write(STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA, next),
quotaFailedCharsRef.current,
);
quotaFailedCharsRef.current = result.quotaFailedChars;
return result.persisted;
};
export type UseVaultStateOptions = {
/**
* When false, skip loading and cross-window sync of the bulky
@@ -454,42 +428,92 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
const identitiesReadSeq = useRef(0);
const proxyProfilesReadSeq = useRef(0);
const groupConfigsReadSeq = useRef(0);
const connectionLogsRef = useRef<ConnectionLog[]>([]);
const connectionLogTerminalDataRef = useRef<ConnectionLogTerminalDataMap>({});
const connectionLogQuotaFailedCharsRef = useRef<number | null>(null);
const connectionLogTerminalDataPersistedRef = useRef<ConnectionLogTerminalDataMap>({});
const connectionLogTerminalDataRevRawRef = useRef<string | null>(null);
const connectionLogTerminalDataLoadSeq = useRef(0);
const terminalDataQueueRef = useRef<Promise<void>>(Promise.resolve());
const terminalDataReadyRef = useRef<Promise<void>>(Promise.resolve());
const pluginCredentialCatalogVersion = useRef(0);
const syncConnectionLogTerminalDataMap = useCallback((
const enqueueTerminalDataTask = useCallback(<T,>(task: () => Promise<T>): Promise<T> => {
const run = terminalDataQueueRef.current.then(task, task);
terminalDataQueueRef.current = run.then(() => undefined, () => undefined);
return run;
}, []);
const bumpConnectionLogTerminalDataRev = useCallback((ids: readonly string[]) => {
const current = localStorageAdapter.read<ConnectionLogTerminalDataRev>(
STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV,
);
const prev = typeof current?.rev === "number" && Number.isFinite(current.rev) ? current.rev : 0;
const payload: ConnectionLogTerminalDataRev = {
rev: Math.max(prev + 1, Date.now()),
ids: [...ids].sort(),
nonce: crypto.randomUUID(),
};
// Record the exact JSON before setItem so this window ignores its own echo.
connectionLogTerminalDataRevRawRef.current = JSON.stringify(payload);
const written = localStorageAdapter.write(STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV, payload);
if (!written) {
connectionLogTerminalDataRevRawRef.current = localStorageAdapter.readString(
STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV,
);
}
}, []);
const absorbRemoteTerminalDataIfNeeded = useCallback(async () => {
const raw = localStorageAdapter.readString(STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV);
if (raw === connectionLogTerminalDataRevRawRef.current) return;
const map = await loadConnectionLogTerminalData();
if (!map) return;
connectionLogTerminalDataRevRawRef.current = raw;
connectionLogTerminalDataPersistedRef.current = map;
connectionLogTerminalDataRef.current = {
...map,
...connectionLogTerminalDataRef.current,
};
}, []);
const syncConnectionLogTerminalDataMap = useCallback(async (
logs: ConnectionLog[],
options?: { usePersistedLogIdsFromDisk?: boolean },
) => {
const usePersistedLogIdsFromDisk = options?.usePersistedLogIdsFromDisk ?? false;
const localMaps = [
connectionLogTerminalDataRef.current,
buildTerminalDataMapFromLogs(logs),
];
const existing = readConnectionLogTerminalDataMap();
if (!syncConnectionLogTerminalData) return true;
await absorbRemoteTerminalDataIfNeeded();
const baseline = connectionLogTerminalDataPersistedRef.current;
const merged = mergeTerminalDataMapsForStorage(
logs,
existing,
localMaps,
buildPersistedLogIds(logs, usePersistedLogIdsFromDisk),
baseline,
[
connectionLogTerminalDataRef.current,
buildTerminalDataMapFromLogs(logs),
],
buildPersistedLogIds(logs, options?.usePersistedLogIdsFromDisk ?? false),
);
const persisted = writeConnectionLogTerminalDataMap(
existing,
merged,
logs,
connectionLogQuotaFailedCharsRef,
);
// On a failed write (e.g. QuotaExceeded once the side store nears ~50 MB)
// keep the same capped map instead of accumulating every log's replay
// buffer in memory; anything outside it would be pruned on the next write.
const written = await writeTerminalDataMapDiff(baseline, merged);
// Keep the capped map in memory even when IndexedDB rejects the write, so
// the next close retries from the last committed baseline.
connectionLogTerminalDataRef.current = merged;
return { map: connectionLogTerminalDataRef.current, persisted };
}, []);
if (!written.changed) return true;
if (!written.persisted) return false;
connectionLogTerminalDataPersistedRef.current = merged;
bumpConnectionLogTerminalDataRev(Object.keys(merged));
return true;
}, [
absorbRemoteTerminalDataIfNeeded,
bumpConnectionLogTerminalDataRev,
syncConnectionLogTerminalData,
]);
const persistConnectionLogState = useCallback((
const persistConnectionLogState = useCallback(async (
logs: ConnectionLog[],
options?: { pruneMainBlob?: boolean },
options?: {
pruneMainBlob?: boolean;
removedIds?: ReadonlySet<string>;
changedIds?: ReadonlySet<string>;
},
) => {
const unsavedTerminalDataPending = logs.some(
(log) => !log.saved && log.terminalData !== undefined,
@@ -506,13 +530,43 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
let sidePersisted = true;
if (mainPersisted || !options?.pruneMainBlob) {
({ persisted: sidePersisted } = syncConnectionLogTerminalDataMap(logs, {
sidePersisted = await syncConnectionLogTerminalDataMap(logs, {
usePersistedLogIdsFromDisk: Boolean(options?.pruneMainBlob && mainPersisted),
}));
});
}
if (!options?.pruneMainBlob) {
mainPersisted = localStorageAdapter.write(STORAGE_KEY_CONNECTION_LOGS, logs);
// The side-store await can overlap another window's metadata write.
// Rebase this snapshot onto the blob that exists now, immediately before
// replacing it. A failed read keeps the snapshot already rebased at enqueue.
const freshLogs = localStorageAdapter.read<ConnectionLog[]>(STORAGE_KEY_CONNECTION_LOGS);
const toWrite = freshLogs
? rebaseQueuedConnectionLogs(
logs,
options?.removedIds ?? new Set<string>(),
options?.changedIds ?? new Set<string>(),
connectionLogsRef.current,
freshLogs,
)
: logs;
mainPersisted = localStorageAdapter.write(STORAGE_KEY_CONNECTION_LOGS, toWrite);
} else if (!sidePersisted && unsavedTerminalDataPending && mainPersisted) {
// IndexedDB rejected the replay after the pruned metadata write. Put the
// buffers back on the current main blob so a restart still has them,
// without replacing logs another window wrote during the await.
// If that blob exceeds quota, drop older unsaved replays and retry so
// the newest capture still has a durable copy.
const freshLogs = localStorageAdapter.read<ConnectionLog[]>(STORAGE_KEY_CONNECTION_LOGS);
let fallbackLogs = freshLogs
? mergeTerminalDataIntoLogs(freshLogs, buildTerminalDataMapFromLogs(logs))
: logs;
sidePersisted = localStorageAdapter.write(STORAGE_KEY_CONNECTION_LOGS, fallbackLogs);
let keep = fallbackLogs.filter((log) => !log.saved && log.terminalData).length;
while (!sidePersisted && keep > 1) {
keep = Math.max(1, Math.floor(keep / 2));
fallbackLogs = retainNewestUnsavedReplays(fallbackLogs, keep);
sidePersisted = localStorageAdapter.write(STORAGE_KEY_CONNECTION_LOGS, fallbackLogs);
}
}
if (!mainPersisted && unsavedTerminalDataPending) {
@@ -524,8 +578,40 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
"[useVaultState] Failed to persist connection log terminal replay data side store.",
);
}
return mainPersisted && sidePersisted;
}, [syncConnectionLogTerminalDataMap]);
const commitConnectionLogs = useCallback((
recipe: (prev: ConnectionLog[]) => ConnectionLog[],
options?: { pruneMainBlob?: boolean },
) => {
const prev = connectionLogsRef.current;
const next = recipe(prev);
const prevById = new Map(prev.map((log) => [log.id, log]));
const nextIds = new Set<string>();
const changedIds = new Set<string>();
for (const log of next) {
nextIds.add(log.id);
const before = prevById.get(log.id);
if (!before || before !== log) changedIds.add(log.id);
}
const removedIds = new Set(prev.filter((log) => !nextIds.has(log.id)).map((log) => log.id));
connectionLogsRef.current = next;
setConnectionLogs(next);
// The task may run after another window writes. Rebase onto the logs that
// exist then. Restore only ids this recipe added or replaced.
return enqueueTerminalDataTask(() => persistConnectionLogState(
rebaseQueuedConnectionLogs(
next,
removedIds,
changedIds,
connectionLogsRef.current,
localStorageAdapter.read<ConnectionLog[]>(STORAGE_KEY_CONNECTION_LOGS),
),
{ ...options, removedIds, changedIds },
));
}, [enqueueTerminalDataTask, persistConnectionLogState]);
const applyConnectionLogsFromStorage = useCallback((
prev: ConnectionLog[],
storedLogs: ConnectionLog[],
@@ -1326,67 +1412,56 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
...log,
id: crypto.randomUUID(),
};
setConnectionLogs((prev) => {
void commitConnectionLogs((prev) => {
// Keep only the last 500 non-saved entries plus all saved entries
const savedLogs = prev.filter((l) => l.saved);
const unsavedLogs = prev.filter((l) => !l.saved);
const updated = [newLog, ...unsavedLogs].slice(0, 500);
const final = pruneInMemoryUnsavedTerminalData(
return pruneInMemoryUnsavedTerminalData(
[...updated, ...savedLogs].sort(
(a, b) => b.startTime - a.startTime
),
);
persistConnectionLogState(final, { pruneMainBlob: true });
return final;
});
}, { pruneMainBlob: true });
return newLog.id;
},
[persistConnectionLogState],
[commitConnectionLogs],
);
const updateConnectionLog = useCallback(
(id: string, updates: Partial<ConnectionLog>) => {
setConnectionLogs((prev) => {
const updated = pruneInMemoryUnsavedTerminalData(
return commitConnectionLogs((prev) => (
pruneInMemoryUnsavedTerminalData(
prev.map((log) => (log.id === id ? { ...log, ...updates } : log)),
);
persistConnectionLogState(updated, { pruneMainBlob: true });
return updated;
});
)
), { pruneMainBlob: true });
},
[persistConnectionLogState],
[commitConnectionLogs],
);
const toggleConnectionLogSaved = useCallback((id: string) => {
setConnectionLogs((prev) => {
const updated = pruneInMemoryUnsavedTerminalData(
void commitConnectionLogs((prev) => (
pruneInMemoryUnsavedTerminalData(
prev.map((log) =>
log.id === id ? { ...log, saved: !log.saved } : log
),
);
persistConnectionLogState(updated, { pruneMainBlob: false });
return updated;
});
}, [persistConnectionLogState]);
)
), { pruneMainBlob: false });
}, [commitConnectionLogs]);
const deleteConnectionLog = useCallback((id: string) => {
setConnectionLogs((prev) => {
const updated = prev.filter((log) => log.id !== id);
const map = { ...connectionLogTerminalDataRef.current };
delete map[id];
connectionLogTerminalDataRef.current = map;
persistConnectionLogState(updated, { pruneMainBlob: true });
return updated;
});
}, [persistConnectionLogState]);
void commitConnectionLogs(
(prev) => prev.filter((log) => log.id !== id),
{ pruneMainBlob: true },
);
}, [commitConnectionLogs]);
const clearUnsavedConnectionLogs = useCallback(() => {
setConnectionLogs((prev) => {
const saved = prev.filter((log) => log.saved);
persistConnectionLogState(saved, { pruneMainBlob: true });
return saved;
});
}, [persistConnectionLogState]);
void commitConnectionLogs(
(prev) => prev.filter((log) => log.saved),
{ pruneMainBlob: true },
);
}, [commitConnectionLogs]);
// Connection logs for Vault logs section — keep App domain bags off session churn.
useLayoutEffect(() => {
@@ -1453,6 +1528,11 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
useEffect(() => {
let cancelled = false;
let resolveTerminalDataReady = () => {};
const terminalDataReady = new Promise<void>((resolve) => {
resolveTerminalDataReady = resolve;
});
terminalDataReadyRef.current = terminalDataReady;
const init = async () => {
try {
await withVaultImportLock("vault", async () => {
@@ -1662,18 +1742,91 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
publishShellHistorySnapshot(savedShellHistory);
}
// Load connection logs
// Load connection logs. Replay blobs live in IndexedDB, one row per
// log id. Windows that never render replay skip that read
// (Settings / tray / popup).
const savedConnectionLogs = localStorageAdapter.read<ConnectionLog[]>(
STORAGE_KEY_CONNECTION_LOGS,
);
// Windows that never render connection-log replay skip the ~30–50 MB
// side-store parse on boot (Settings / tray / popup).
const terminalDataMap = syncConnectionLogTerminalData
? readConnectionLogTerminalDataMap()
: {};
let connectionLogsForBoot = savedConnectionLogs;
let terminalDataMap: ConnectionLogTerminalDataMap = {};
let durableTerminalData = true;
if (syncConnectionLogTerminalData) {
const readTerminalDataRev = () => localStorageAdapter.readString(
STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV,
);
// A peer can commit replay and bump the rev while this load is in
// flight. Capture the rev first and reload when it moves, so stamping
// the key later does not mark an unread snapshot as consumed.
let revSeen = readTerminalDataRev();
const loaded = await migrateLegacyConnectionLogTerminalData({
read: () => localStorageAdapter.read<ConnectionLogTerminalDataMap>(
STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA,
),
remove: () => {
localStorageAdapter.remove(STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA);
},
});
if (cancelled) return;
terminalDataMap = loaded.map;
let revNow = readTerminalDataRev();
if (revNow !== revSeen) {
const reloaded = await loadConnectionLogTerminalData();
if (cancelled) return;
if (reloaded) {
if (Object.keys(reloaded).length > 0) {
terminalDataMap = { ...terminalDataMap, ...reloaded };
}
revSeen = revNow;
}
}
// Another window can append a log while IndexedDB is opening. Prune
// against that newer metadata so this boot does not delete its replay.
connectionLogsForBoot = localStorageAdapter.read<ConnectionLog[]>(
STORAGE_KEY_CONNECTION_LOGS,
) ?? savedConnectionLogs;
const pruned = connectionLogsForBoot
? pruneTerminalDataMapForStorage(connectionLogsForBoot, terminalDataMap)
: terminalDataMap;
// A legacy map that never reached IndexedDB is not a diff baseline.
// Comparing against it would persist only the changed ids and then
// drop the rest of the legacy blob on the next boot.
const baseline = loaded.durable ? terminalDataMap : {};
const written = await writeTerminalDataMapDiff(baseline, pruned);
if (cancelled) return;
if (written.persisted) {
terminalDataMap = pruned;
if (!loaded.durable) {
localStorageAdapter.remove(STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA);
}
}
durableTerminalData = written.persisted || loaded.durable;
if (loaded.migrated || (written.changed && written.persisted)) {
bumpConnectionLogTerminalDataRev(Object.keys(terminalDataMap));
} else {
revNow = readTerminalDataRev();
if (revNow !== revSeen) {
const reloaded = await loadConnectionLogTerminalData();
if (cancelled) return;
if (reloaded) {
if (Object.keys(reloaded).length > 0) {
terminalDataMap = { ...terminalDataMap, ...reloaded };
}
revSeen = revNow;
connectionLogsForBoot = localStorageAdapter.read<ConnectionLog[]>(
STORAGE_KEY_CONNECTION_LOGS,
) ?? connectionLogsForBoot;
}
}
connectionLogTerminalDataRevRawRef.current = revSeen;
}
}
connectionLogTerminalDataRef.current = terminalDataMap;
if (savedConnectionLogs) {
setConnectionLogs(mergeTerminalDataIntoLogs(savedConnectionLogs, terminalDataMap));
connectionLogTerminalDataPersistedRef.current = durableTerminalData ? terminalDataMap : {};
if (connectionLogsForBoot) {
const hydrated = mergeTerminalDataIntoLogs(connectionLogsForBoot, terminalDataMap);
connectionLogsRef.current = hydrated;
setConnectionLogs(hydrated);
}
// Load managed sources
@@ -1702,6 +1855,7 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
}
}
} finally {
resolveTerminalDataReady();
// StrictMode remount cancels the first init; only the surviving effect
// may publish "vault ready" or terminals can boot against empty keys.
if (!cancelled) {
@@ -1714,8 +1868,15 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
void init();
return () => {
cancelled = true;
resolveTerminalDataReady();
};
}, [commitEncryptedHostsUnderVaultLock, syncConnectionLogTerminalData, updateHosts, updateSnippets]);
}, [
bumpConnectionLogTerminalDataRev,
commitEncryptedHostsUnderVaultLock,
syncConnectionLogTerminalData,
updateHosts,
updateSnippets,
]);
useEffect(() => {
if (!isInitialized) return;
@@ -1917,19 +2078,18 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
if (key === STORAGE_KEY_CONNECTION_LOGS) {
const next = safeParse<ConnectionLog[]>(event.newValue) ?? [];
setConnectionLogs((prev) =>
applyConnectionLogsFromStorage(prev, next, connectionLogTerminalDataRef.current),
const merged = applyConnectionLogsFromStorage(
connectionLogsRef.current,
next,
connectionLogTerminalDataRef.current,
);
connectionLogsRef.current = merged;
setConnectionLogs(merged);
return;
}
if (key === STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA) {
// Other windows still receive the storage event; skip the multi-MB
// JSON.parse when this mount does not show connection-log replay.
if (!syncConnectionLogTerminalData) return;
const next = safeParse<ConnectionLogTerminalDataMap>(event.newValue) ?? {};
connectionLogTerminalDataRef.current = next;
setConnectionLogs((prev) => mergeConnectionLogsFromStorage(prev, prev, next));
if (key === STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV) {
applyTerminalDataRev(event.newValue);
return;
}
@@ -1957,20 +2117,46 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
}
};
const applyTerminalDataRev = (raw: string | null) => {
// Settings, tray, and popup mounts never replay logs and must not
// pull the side store across windows.
if (!syncConnectionLogTerminalData) return;
if (raw === connectionLogTerminalDataRevRawRef.current) return;
const seq = ++connectionLogTerminalDataLoadSeq.current;
void terminalDataReadyRef.current.then(() => enqueueTerminalDataTask(async () => {
if (seq !== connectionLogTerminalDataLoadSeq.current) return;
const map = await loadConnectionLogTerminalData();
if (seq !== connectionLogTerminalDataLoadSeq.current) return;
if (!map) return;
connectionLogTerminalDataRevRawRef.current = localStorageAdapter.readString(
STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV,
);
connectionLogTerminalDataPersistedRef.current = map;
connectionLogTerminalDataRef.current = map;
const merged = mergeConnectionLogsFromStorage(
connectionLogsRef.current,
connectionLogsRef.current,
map,
);
connectionLogsRef.current = merged;
setConnectionLogs(merged);
}));
};
const handleLocalStorageAdapterChanged = (event: Event) => {
const key = (event as CustomEvent<{ key?: string }>).detail?.key;
if (key === STORAGE_KEY_CONNECTION_LOGS) {
const next = localStorageAdapter.read<ConnectionLog[]>(STORAGE_KEY_CONNECTION_LOGS) ?? [];
setConnectionLogs((prev) =>
applyConnectionLogsFromStorage(prev, next, connectionLogTerminalDataRef.current),
);
// This window already updated connectionLogsRef before setItem. The
// adapter notifies on a timer, so this blob can be older than a log
// added or finalized while that write was still queued. Applying it
// would drop that log from the ref, and the follow-up recipe would
// record no changed id. Other windows still arrive through "storage".
return;
}
if (key === STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA) {
if (!syncConnectionLogTerminalData) return;
const next = readConnectionLogTerminalDataMap();
connectionLogTerminalDataRef.current = next;
setConnectionLogs((prev) => mergeConnectionLogsFromStorage(prev, prev, next));
if (key === STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV) {
applyTerminalDataRev(localStorageAdapter.readString(
STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV,
));
}
};
@@ -1980,7 +2166,7 @@ export const useVaultState = (options?: UseVaultStateOptions) => {
window.removeEventListener("storage", handleStorage);
window.removeEventListener(LOCAL_STORAGE_ADAPTER_CHANGED_EVENT, handleLocalStorageAdapterChanged);
};
}, [applyConnectionLogsFromStorage, syncConnectionLogTerminalData]);
}, [applyConnectionLogsFromStorage, enqueueTerminalDataTask, syncConnectionLogTerminalData]);
const updateHostLastConnected = useCallback((hostId: string) => {
setHosts((prev) => {
+4 -2
View File
@@ -1614,7 +1614,7 @@ const TerminalComponent: React.FC<TerminalProps> = ({
const capturedData = options?.finalized
? data
: (finalizeTerminalLogData() || data);
captureHandler(capturedSessionId, capturedData);
return captureHandler(capturedSessionId, capturedData);
}, [finalizeTerminalLogData]);
const attachHomeWebContentsIdRef = useRef<number | null | undefined>(undefined);
@@ -1961,10 +1961,11 @@ const TerminalComponent: React.FC<TerminalProps> = ({
}, [sessionId]);
const forceCloseHibernatedSession = useCallback(() => {
let pendingCapture: void | Promise<void>;
if (!terminalDataCapturedRef.current) {
const hibernatedData = hibernateSnapshotRef.current + hibernatePendingBufferRef.current;
if (hibernatedData) {
handleTerminalDataCaptureOnce(sessionId, hibernatedData);
pendingCapture = handleTerminalDataCaptureOnce(sessionId, hibernatedData);
}
}
disposeDataRef.current?.();
@@ -2007,6 +2008,7 @@ const TerminalComponent: React.FC<TerminalProps> = ({
}
sessionRef.current = null;
clearHibernateRuntimeState();
return pendingCapture;
}, [attachAuthorization, attachExistingSession, clearHibernateRuntimeState, handleTerminalDataCaptureOnce, sessionId, terminalBackend]);
const observeTerminalInputPrompt = useCallback((
+1 -1
View File
@@ -700,7 +700,7 @@ const TerminalLayerInner: React.FC<TerminalLayerProps> = ({
}, [onAddKnownHost]);
const handleTerminalDataCapture = useCallback((sessionId: string, data: string) => {
onTerminalDataCapture?.(sessionId, data);
return onTerminalDataCapture?.(sessionId, data);
}, [onTerminalDataCapture]);
// Terminal backend for broadcast writes
@@ -1965,7 +1965,14 @@ export const createTerminalSessionStarters = (ctx: TerminalSessionStartersContex
sessionId: ctx.sessionId,
dataLength: terminalData.length,
});
ctx.onTerminalDataCapture(ctx.sessionId, terminalData);
const pendingCapture: unknown = ctx.onTerminalDataCapture(ctx.sessionId, terminalData);
if (
pendingCapture !== undefined
&& pendingCapture !== null
&& typeof (pendingCapture as Promise<void>).then === "function"
) {
await (pendingCapture as Promise<void>);
}
} catch (err) {
logger.warn("Failed to serialize terminal data:", err);
}
@@ -245,7 +245,7 @@ export type TerminalSessionStartersContext = {
onSessionAttached?: (sessionId: string) => void;
onRestoreCwdIntentConsumed?: (cwd: string) => void;
onSessionExit?: (sessionId: string, evt: TerminalSessionExitEvent) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void | Promise<void>;
onTerminalLogData?: (data: string) => void;
onProgrammaticCommandLogRewrite?: (rewrite: ProgrammaticCommandLogRewrite) => void;
onTerminalOutput?: (chunk: string, meta?: TerminalSessionDataMeta) => void;
@@ -1000,7 +1000,14 @@ export const attachSessionToTerminal = (
try {
await ctx.prepareKeywordHighlightSerialization?.();
const terminalData = ctx.serializeAddonRef.current.serialize();
ctx.onTerminalDataCapture(ctx.sessionId, terminalData);
const pendingCapture: unknown = ctx.onTerminalDataCapture(ctx.sessionId, terminalData);
if (
pendingCapture !== undefined
&& pendingCapture !== null
&& typeof (pendingCapture as Promise<void>).then === "function"
) {
await (pendingCapture as Promise<void>);
}
} catch (err) {
logger.warn("Failed to serialize terminal data:", err);
}
+1 -1
View File
@@ -198,7 +198,7 @@ export interface TerminalProps {
onTerminalFontSizeChange?: (fontSize: number) => void;
onStatusChange?: (sessionId: string, status: TerminalSession["status"], sftpHost?: Host) => void;
onSessionExit?: (sessionId: string, evt: TerminalSessionExitEvent) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void | Promise<void>;
onOsDetected?: (hostId: string, distro: string) => void;
onCloseSession?: (sessionId: string) => void;
onUpdateHost?: (host: Host) => void;
+19 -6
View File
@@ -917,8 +917,14 @@ export function useTerminalEffects(ctx: TerminalEffectsContext) {
bootAbort.abort();
isBootActiveRef.current = false;
if (hibernatedRef?.current) {
forceCloseHibernatedSession?.();
disposeOwnedRuntime();
const pendingClose = forceCloseHibernatedSession?.() as void | Promise<void>;
if (pendingClose && typeof pendingClose.then === "function") {
void pendingClose.finally(() => {
disposeOwnedRuntime();
});
} else {
disposeOwnedRuntime();
}
return;
}
@@ -946,13 +952,20 @@ export function useTerminalEffects(ctx: TerminalEffectsContext) {
return;
}
const persistCloseCapture = (data: string, source: string, dataLength: number) => {
const persistCloseCapture = async (data: string, source: string, dataLength: number) => {
logger.info("[Terminal] Capturing data on unmount", {
sessionId,
source,
dataLength,
});
handleTerminalDataCaptureOnce(sessionId, data, { finalized: true });
const pendingCapture: unknown = handleTerminalDataCaptureOnce(sessionId, data, { finalized: true });
if (
pendingCapture !== undefined
&& pendingCapture !== null
&& typeof (pendingCapture as Promise<void>).then === "function"
) {
await (pendingCapture as Promise<void>);
}
};
const term = termRef.current;
@@ -991,7 +1004,7 @@ export function useTerminalEffects(ctx: TerminalEffectsContext) {
? resolveConnectionLogCapturePayload(finalizeTerminalLogData)
: null;
if (connectionLogPayload) {
persistCloseCapture(
await persistCloseCapture(
connectionLogPayload.data,
connectionLogPayload.source,
connectionLogPayload.data.length,
@@ -1018,7 +1031,7 @@ export function useTerminalEffects(ctx: TerminalEffectsContext) {
return;
}
if (payload) {
persistCloseCapture(payload.data, payload.source, payload.data.length);
await persistCloseCapture(payload.data, payload.source, payload.data.length);
}
ownedRuntime = null;
scheduleTerminalCloseTeardown(teardown);
@@ -696,7 +696,7 @@ export interface TerminalLayerProps {
onCommandExecuted?: (command: string, hostId: string, hostLabel: string, sessionId: string) => void;
onDeleteShellHistoryEntry?: (entryId: string) => void;
onClearCommandHistory?: () => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void | Promise<void>;
onCreateWorkspaceFromSessions: (baseSessionId: string, joiningSessionId: string, hint: Exclude<SplitHint, null>) => void;
onAddSessionToWorkspace: (workspaceId: string, sessionId: string, hint: Exclude<SplitHint, null>) => void;
onRequestAddToWorkspace?: (workspaceId: string) => void;
@@ -815,7 +815,7 @@ interface TerminalPaneProps {
onCloseSession: (sessionId: string) => void;
onStatusChange: (sessionId: string, status: TerminalSession['status'], sftpHost?: Host) => void;
onSessionExit: (sessionId: string, evt: TerminalSessionExitEvent) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void | Promise<void>;
onOsDetected: (hostId: string, distro: string) => void;
onUpdateHost: (host: Host) => void;
onAddKnownHost?: (knownHost: KnownHost) => void;
@@ -1717,7 +1717,7 @@ interface TerminalPanesHostProps {
onCloseSession: (sessionId: string) => void;
onStatusChange: (sessionId: string, status: TerminalSession['status'], sftpHost?: Host) => void;
onSessionExit: (sessionId: string, evt: TerminalSessionExitEvent) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void;
onTerminalDataCapture?: (sessionId: string, data: string) => void | Promise<void>;
onOsDetected: (hostId: string, distro: string) => void;
onUpdateHost: (host: Host) => void;
onAddKnownHost?: (knownHost: KnownHost) => void;
+160 -122
View File
@@ -5,12 +5,14 @@ import type { ConnectionLog } from "./models.ts";
import { selectConnectionLogForTerminalDataCapture } from "./connectionLog.ts";
import {
MAX_PERSISTED_UNSAVED_TERMINAL_DATA_ENTRIES,
diffTerminalDataMaps,
mergeConnectionLogsFromStorage,
mergeTerminalDataIntoLogs,
mergeTerminalDataMapsForStorage,
pruneInMemoryUnsavedTerminalData,
pruneTerminalDataMapForStorage,
writeTerminalDataMapWithQuotaRetry,
rebaseQueuedConnectionLogs,
retainNewestUnsavedReplays,
} from "./connectionLogTerminalData.ts";
const baseLog: ConnectionLog = {
@@ -234,131 +236,167 @@ test("mergeTerminalDataMapsForStorage drops deleted log replay buffers", () => {
assert.equal(merged.deleted, undefined);
});
const quotaLogs = (count: number): ConnectionLog[] =>
Array.from({ length: count }, (_, index) => ({
test("rebaseQueuedConnectionLogs keeps logs another window wrote while this snapshot waited", () => {
const queued = [{ ...baseLog, id: "local", startTime: 2000, terminalData: "local-replay" }];
const memory = queued;
const disk = [
{ ...baseLog, id: "remote", startTime: 3000 },
{ ...baseLog, id: "local", startTime: 2000 },
];
const rebased = rebaseQueuedConnectionLogs(queued, new Set(), new Set(["local"]), memory, disk);
assert.deepEqual(rebased.map((log) => log.id), ["remote", "local"]);
assert.equal(rebased.find((log) => log.id === "local")?.terminalData, "local-replay");
});
test("rebaseQueuedConnectionLogs does not restore an id deleted from memory after it was queued", () => {
const queued = [{ ...baseLog, id: "local", startTime: 2000, terminalData: "local-replay" }];
const disk = [
{ ...baseLog, id: "remote", startTime: 3000 },
{ ...baseLog, id: "local", startTime: 2000, terminalData: "stale-intermediate" },
];
const rebased = rebaseQueuedConnectionLogs(
queued,
new Set(),
new Set(["local"]),
[{ ...baseLog, id: "remote", startTime: 3000 }],
disk,
);
assert.deepEqual(rebased.map((log) => log.id), ["remote"]);
assert.equal(rebased.find((log) => log.id === "local"), undefined);
const notYetOnDisk = rebaseQueuedConnectionLogs(
queued,
new Set(),
new Set(["local"]),
[{ ...baseLog, id: "remote", startTime: 3000 }],
[{ ...baseLog, id: "remote", startTime: 3000 }],
);
assert.deepEqual(notYetOnDisk.map((log) => log.id), ["remote"]);
});
test("rebaseQueuedConnectionLogs does not restore an unchanged log another window deleted", () => {
const unchanged = { ...baseLog, id: "unchanged", startTime: 1000, terminalData: "old" };
const added = { ...baseLog, id: "local", startTime: 4000, terminalData: "local-replay" };
const queued = [added, unchanged];
const remote = [{ ...baseLog, id: "remote", startTime: 3000 }];
const rebased = rebaseQueuedConnectionLogs(
queued,
new Set(),
new Set(["local"]),
[added, ...remote],
remote,
);
assert.deepEqual(rebased.map((log) => log.id), ["local", "remote"]);
assert.equal(rebased.find((log) => log.id === "unchanged"), undefined);
assert.equal(rebased.find((log) => log.id === "local")?.terminalData, "local-replay");
});
test("rebaseQueuedConnectionLogs applies a local metadata edit onto the latest disk row", () => {
const queued = [{ ...baseLog, id: "local", startTime: 2000, saved: true, terminalData: "replay" }];
const disk = [
{ ...baseLog, id: "remote", startTime: 3000 },
{ ...baseLog, id: "local", startTime: 2000, saved: false },
];
const rebased = rebaseQueuedConnectionLogs(
queued,
new Set(),
new Set(["local"]),
queued,
disk,
);
assert.equal(rebased.find((log) => log.id === "local")?.saved, true);
assert.equal(rebased.find((log) => log.id === "local")?.terminalData, "replay");
assert.ok(rebased.some((log) => log.id === "remote"));
});
test("retainNewestUnsavedReplays drops older unsaved buffers and keeps the newest", () => {
const logs: ConnectionLog[] = [
{ ...baseLog, id: "old", startTime: 1000, terminalData: "old" },
{ ...baseLog, id: "mid", startTime: 2000, terminalData: "mid" },
{ ...baseLog, id: "new", startTime: 3000, terminalData: "new" },
{ ...baseLog, id: "saved", startTime: 500, saved: true, terminalData: "saved" },
];
const kept = retainNewestUnsavedReplays(logs, 1);
assert.equal(kept.find((log) => log.id === "new")?.terminalData, "new");
assert.equal(kept.find((log) => log.id === "saved")?.terminalData, "saved");
assert.equal(kept.find((log) => log.id === "old")?.terminalData, undefined);
assert.equal(kept.find((log) => log.id === "mid")?.terminalData, undefined);
});
test("rebaseQueuedConnectionLogs drops a queued delete and keeps a queued add still in memory", () => {
const queued = [{ ...baseLog, id: "added", startTime: 4000, terminalData: "new" }];
const memory = queued;
const disk = [
{ ...baseLog, id: "deleted", startTime: 1000 },
{ ...baseLog, id: "kept", startTime: 2000 },
];
const rebased = rebaseQueuedConnectionLogs(
queued,
new Set(["deleted"]),
new Set(["added"]),
memory,
disk,
);
assert.deepEqual(rebased.map((log) => log.id), ["added", "kept"]);
assert.equal(rebased.find((log) => log.id === "added")?.terminalData, "new");
});
test("diffTerminalDataMaps puts only changed ids and deletes removed ids", () => {
const diff = diffTerminalDataMaps(
{ same: "keep", edited: "old", removed: "gone" },
{ same: "keep", edited: "new", added: "fresh" },
);
assert.deepEqual(diff.puts, { edited: "new", added: "fresh" });
assert.deepEqual(diff.deleteIds, ["removed"]);
});
test("diffTerminalDataMaps treats key order as unchanged", () => {
const diff = diffTerminalDataMaps(
{ b: "2", a: "1" },
{ a: "1", b: "2" },
);
assert.deepEqual(diff.puts, {});
assert.deepEqual(diff.deleteIds, []);
});
test("prune cap diffs to deleting only replay buffers outside the newest 50", () => {
const logs: ConnectionLog[] = Array.from({ length: 60 }, (_, index) => ({
...baseLog,
id: `log-${index}`,
startTime: index,
saved: false,
}));
logs.push({
...baseLog,
id: "saved-keep",
startTime: 1,
saved: true,
});
const map = Object.fromEntries(logs.map((log) => [log.id, `data-${log.id}`]));
const pruned = pruneTerminalDataMapForStorage(logs, map);
const diff = diffTerminalDataMaps(map, pruned);
const quotaWriter = (limitChars: number) => {
const calls: number[] = [];
const write = (next: Record<string, string>) => {
const size = Object.values(next).reduce((sum, value) => sum + value.length, 0);
calls.push(Object.keys(next).length);
return size <= limitChars;
};
return { calls, write };
};
test("writeTerminalDataMapWithQuotaRetry writes once when it fits", () => {
const logs = quotaLogs(5);
const map = Object.fromEntries(logs.map((log) => [log.id, "x".repeat(10)]));
const { calls, write } = quotaWriter(1000);
const result = writeTerminalDataMapWithQuotaRetry(logs, map, write, null);
assert.equal(result.persisted, true);
assert.equal(result.written, map);
assert.deepEqual(calls, [5]);
});
test("writeTerminalDataMapWithQuotaRetry drops the oldest unsaved entries until it fits", () => {
const logs = quotaLogs(50);
// Newest first, matching pruneTerminalDataMapForStorage order.
const map = Object.fromEntries(
[...logs].reverse().map((log) => [log.id, "x".repeat(10)]),
);
const { calls, write } = quotaWriter(10 * 47);
const result = writeTerminalDataMapWithQuotaRetry(logs, map, write, null);
assert.equal(result.persisted, true);
// 50 fails, drop 1 → 49 fails, drop 2 → 48 fails, drop 4 → 46 fits.
assert.deepEqual(calls, [50, 49, 48, 46]);
assert.equal(result.written["log-49"], map["log-49"]);
assert.equal(result.written["log-4"], map["log-4"]);
for (const id of ["log-0", "log-1", "log-2", "log-3"]) {
assert.equal(result.written[id], undefined);
}
// Key order (newest first) is preserved.
assert.deepEqual(Object.keys(result.written), Object.keys(map).filter((id) => id in result.written));
});
test("writeTerminalDataMapWithQuotaRetry never drops saved or unknown entries and stops after all unsaved are gone", () => {
const logs: ConnectionLog[] = [
...quotaLogs(20),
{ ...baseLog, id: "saved", startTime: -1, saved: true },
];
const map: Record<string, string> = Object.fromEntries(
logs.map((log) => [log.id, "x".repeat(10)]),
);
map["other-window"] = "y".repeat(10);
const { calls, write } = quotaWriter(5); // nothing fits
const result = writeTerminalDataMapWithQuotaRetry(logs, map, write, null);
assert.equal(result.persisted, false);
assert.equal(result.written, map);
// 22, then drop 1, 2, 4, 8, 16, then all 20 unsaved: bounded attempts.
assert.deepEqual(calls, [22, 21, 20, 18, 14, 6, 2]);
assert.ok(result.attempts <= 2 + Math.ceil(Math.log2(20)));
});
const newestFirstMap = (logs: ConnectionLog[], chars: number) =>
Object.fromEntries([...logs].reverse().map((log) => [log.id, "x".repeat(chars)]));
test("writeTerminalDataMapWithQuotaRetry starts the next write from the newest set that last fit", () => {
const logs = quotaLogs(50);
const quota = quotaWriter(10 * 47);
const first = writeTerminalDataMapWithQuotaRetry(logs, newestFirstMap(logs, 10), quota.write, null);
// Next close: oldest drops out of the 50, a newer session arrives.
const nextLogs = [...logs.slice(1), { ...baseLog, id: "log-50", startTime: 50 }];
const second = quotaWriter(10 * 47);
const result = writeTerminalDataMapWithQuotaRetry(
nextLogs,
newestFirstMap(nextLogs, 10),
second.write,
first.quotaFailedChars,
);
assert.equal(result.persisted, true);
assert.deepEqual(second.calls, [47]);
assert.equal(result.written["log-50"] !== undefined, true);
assert.equal(result.written["log-3"], undefined);
});
test("writeTerminalDataMapWithQuotaRetry grows back when entries get smaller", () => {
const logs = quotaLogs(50);
const first = writeTerminalDataMapWithQuotaRetry(
logs,
newestFirstMap(logs, 10),
quotaWriter(10 * 47).write,
null,
);
const small = quotaWriter(10 * 47);
const result = writeTerminalDataMapWithQuotaRetry(
logs,
newestFirstMap(logs, 5),
small.write,
first.quotaFailedChars,
);
assert.deepEqual(small.calls, [50]);
assert.equal(Object.keys(result.written).length, 50);
});
test("writeTerminalDataMapWithQuotaRetry falls back to dropping when the hint is too optimistic", () => {
const logs = quotaLogs(50);
const first = writeTerminalDataMapWithQuotaRetry(
logs,
newestFirstMap(logs, 10),
quotaWriter(10 * 47).write,
null,
);
const tighter = quotaWriter(10 * 30); // quota shrank (other keys grew)
const result = writeTerminalDataMapWithQuotaRetry(
logs,
newestFirstMap(logs, 10),
tighter.write,
first.quotaFailedChars,
);
assert.equal(result.persisted, true);
assert.equal(tighter.calls[0], 47);
assert.ok(Object.keys(result.written).length <= 30);
assert.equal(result.written["log-49"] !== undefined, true);
assert.ok(result.attempts <= 2 + Math.ceil(Math.log2(50)));
assert.equal(Object.keys(pruned).length, MAX_PERSISTED_UNSAVED_TERMINAL_DATA_ENTRIES + 1);
assert.equal(Object.keys(diff.puts).length, 0);
assert.equal(diff.deleteIds.length, 10);
assert.equal(diff.deleteIds.includes("log-0"), true);
assert.equal(diff.deleteIds.includes("log-9"), true);
assert.equal(diff.deleteIds.includes("log-10"), false);
assert.equal(diff.deleteIds.includes("log-59"), false);
assert.equal(diff.deleteIds.includes("saved-keep"), false);
assert.equal(pruned["saved-keep"], "data-saved-keep");
});
+93 -55
View File
@@ -26,8 +26,8 @@ export const mergeTerminalDataIntoLogs = (
};
/**
* When another window or a pruned localStorage write reloads connection logs,
* keep in-memory / side-store terminal replay data instead of wiping it.
* When another window reloads connection logs, keep in-memory / side-store
* terminal replay data instead of wiping it.
*/
export const mergeConnectionLogsFromStorage = (
prev: ConnectionLog[],
@@ -49,6 +49,81 @@ export const mergeConnectionLogsFromStorage = (
return changed ? merged : next;
};
/**
* Apply a queued connection-log snapshot when it runs, not when it was captured.
* Disk may have gained another window's logs during the wait. Keep those logs.
* Metadata from this snapshot replaces a disk row only for ids the recipe added
* or updated (`changedIds`) that are still in memory. An id deleted locally
* after this snapshot was queued is not written back. Deletes from this
* snapshot apply only while memory still agrees.
*/
export const rebaseQueuedConnectionLogs = (
queued: readonly ConnectionLog[],
removedIds: ReadonlySet<string>,
changedIds: ReadonlySet<string>,
memory: readonly ConnectionLog[],
disk: readonly ConnectionLog[] | null,
): ConnectionLog[] => {
const memoryById = new Map(memory.map((log) => [log.id, log]));
const queuedById = new Map(queued.map((log) => [log.id, log]));
const diskKept = (disk ?? []).filter(
(log) => !(removedIds.has(log.id) && !memoryById.has(log.id)),
);
const present = new Set<string>();
const withLocalEdits: ConnectionLog[] = [];
for (const log of diskKept) {
const queuedLog = queuedById.get(log.id);
const memoryLog = memoryById.get(log.id);
if (changedIds.has(log.id)) {
if (!memoryLog) continue;
present.add(log.id);
withLocalEdits.push(memoryLog);
continue;
}
present.add(log.id);
const replay = queuedLog?.terminalData ?? memoryLog?.terminalData ?? log.terminalData;
if (!replay || log.terminalData === replay) {
withLocalEdits.push(log);
continue;
}
withLocalEdits.push({ ...log, terminalData: replay });
}
const extras: ConnectionLog[] = [];
for (const log of queued) {
if (present.has(log.id) || !changedIds.has(log.id)) continue;
const memoryLog = memoryById.get(log.id);
if (!memoryLog) continue;
extras.push(memoryLog);
}
if (extras.length === 0) return withLocalEdits;
return pruneInMemoryUnsavedTerminalData(
[...extras, ...withLocalEdits].sort((a, b) => b.startTime - a.startTime),
);
};
/**
* Drop `terminalData` from unsaved logs outside the newest `keep` entries.
* Saved logs and the newest unsaved replays stay intact.
*/
export const retainNewestUnsavedReplays = (
logs: readonly ConnectionLog[],
keep: number,
): ConnectionLog[] => {
const ranked = logs
.filter((log) => !log.saved && log.terminalData)
.sort((a, b) => b.startTime - a.startTime);
if (ranked.length <= keep) return [...logs];
const keepIds = new Set(ranked.slice(0, Math.max(0, keep)).map((log) => log.id));
let changed = false;
const next = logs.map((log) => {
if (log.saved || log.terminalData === undefined || keepIds.has(log.id)) return log;
changed = true;
const { terminalData: _omitted, ...rest } = log;
return rest;
});
return changed ? next : [...logs];
};
export const buildTerminalDataMapFromLogs = (
logs: ConnectionLog[],
): ConnectionLogTerminalDataMap => {
@@ -114,62 +189,25 @@ export const pruneInMemoryUnsavedTerminalData = (
return changed ? next : logs;
};
/**
* Write the side store; if the write is rejected (localStorage quota), retry
* without the oldest unsaved replay buffers so the newest sessions are kept.
* Drops 1, 2, 4, … oldest entries per retry, so attempts stay O(log n).
* Saved logs and ids not in `logs` are never dropped.
*
* `quotaFailedChars` is owned by the caller. It is the summed entry length of
* the smallest payload that last hit the quota, so the next write can start
* from the newest set that fits. A successful write of the full map returns
* null. The helper does not keep that hint itself.
*/
export const writeTerminalDataMapWithQuotaRetry = (
logs: ConnectionLog[],
map: ConnectionLogTerminalDataMap,
write: (next: ConnectionLogTerminalDataMap) => boolean,
quotaFailedChars: number | null,
): { persisted: boolean; written: ConnectionLogTerminalDataMap; attempts: number; quotaFailedChars: number | null } => {
const droppable = logs
.filter((log) => !log.saved && map[log.id] !== undefined)
.sort((a, b) => a.startTime - b.startTime)
.map((log) => log.id);
let chars = 0;
for (const data of Object.values(map)) chars += data.length;
export type ConnectionLogTerminalDataDiff = {
puts: ConnectionLogTerminalDataMap;
deleteIds: string[];
};
let hint = quotaFailedChars;
let dropCount = 0;
if (hint !== null) {
while (dropCount < droppable.length && chars >= hint) {
chars -= map[droppable[dropCount]].length;
dropCount += 1;
}
/** Changed replay blobs to write, and ids no longer in the persisted map. */
export const diffTerminalDataMaps = (
previous: ConnectionLogTerminalDataMap,
next: ConnectionLogTerminalDataMap,
): ConnectionLogTerminalDataDiff => {
const puts: ConnectionLogTerminalDataMap = {};
const deleteIds: string[] = [];
for (const id of Object.keys(next)) {
if (previous[id] !== next[id]) puts[id] = next[id];
}
let attempts = 0;
for (;;) {
let next = map;
if (dropCount > 0) {
const dropped = new Set(droppable.slice(0, dropCount));
next = {};
for (const [id, data] of Object.entries(map)) {
if (!dropped.has(id)) next[id] = data;
}
}
attempts += 1;
if (write(next)) {
if (dropCount === 0) hint = null;
return { persisted: true, written: next, attempts, quotaFailedChars: hint };
}
hint = hint === null ? chars : Math.min(hint, chars);
if (dropCount >= droppable.length) {
return { persisted: false, written: map, attempts, quotaFailedChars: hint };
}
const target = Math.min(Math.max(dropCount * 2, 1), droppable.length);
for (let i = dropCount; i < target; i += 1) chars -= map[droppable[i]].length;
dropCount = target;
for (const id of Object.keys(previous)) {
if (!Object.prototype.hasOwnProperty.call(next, id)) deleteIds.push(id);
}
return { puts, deleteIds };
};
/** Fold side-store maps together, then cap to the allowed unsaved/saved set. */
+9 -1
View File
@@ -45,8 +45,16 @@ export const STORAGE_KEY_COMMAND_HISTORY = 'netcatty:commandHistory';
/** Set once the user clears all command history; histfile seeding honors it. */
export const STORAGE_KEY_COMMAND_HISTORY_CLEARED = 'netcatty:commandHistoryCleared';
export const STORAGE_KEY_CONNECTION_LOGS = 'netcatty_connection_logs_v1';
/** Side store for unsaved connection-log terminal replay buffers (main blob omits them for perf). */
/**
* Legacy side store for unsaved connection-log terminal replay buffers.
* Read once to migrate into IndexedDB, then removed. Do not write it.
*/
export const STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA = 'netcatty_connection_log_terminal_data_v1';
/**
* Tiny cross-window signal for the IndexedDB replay side store.
* Value shape: `{ rev: number, ids?: string[], nonce?: string }`.
*/
export const STORAGE_KEY_CONNECTION_LOG_TERMINAL_DATA_REV = 'netcatty_connection_log_terminal_data_rev_v1';
export const STORAGE_KEY_SESSION_RESTORE = 'netcatty_session_restore_v1';
export const STORAGE_KEY_RESTORE_PREVIOUS_SESSION = 'netcatty_restore_previous_session_v1';
export const STORAGE_KEY_RESTORE_TERMINAL_CWD = 'netcatty_restore_terminal_cwd_v1';
@@ -0,0 +1,365 @@
import assert from "node:assert/strict";
import { after, beforeEach, test } from "node:test";
import {
loadAll,
migrateLegacyConnectionLogTerminalData,
putMany,
replaceAll,
writeTerminalDataMapDiff,
type ConnectionLogTerminalDataMap,
} from "./connectionLogTerminalDataStore.ts";
type Row = { id: string; data: string };
type FakeState = {
failWrites: boolean;
onGet?: (rows: Map<string, Row>) => void;
openConnections: Set<{ close: () => void; onversionchange: (() => void) | null }>;
};
const fakeState: FakeState = {
failWrites: false,
openConnections: new Set(),
};
const databases = new Map<string, { version: number; stores: Map<string, Map<string, Row>> }>();
const previousIndexedDB = Object.getOwnPropertyDescriptor(globalThis, "indexedDB");
class FakeRequest<T = unknown> {
result: T | undefined;
error: Error | null = null;
onsuccess: ((event?: Event) => void) | null = null;
onerror: ((event?: Event) => void) | null = null;
onupgradeneeded: ((event?: Event) => void) | null = null;
}
function installFakeIndexedDB(): void {
const indexedDB = {
open(name: string, version = 1) {
const request = new FakeRequest<FakeDatabase>();
queueMicrotask(() => {
let record = databases.get(name);
if (!record) {
record = { version: 0, stores: new Map() };
databases.set(name, record);
}
const db = new FakeDatabase(record);
fakeState.openConnections.add(db);
request.result = db;
if (version > record.version) {
record.version = version;
request.onupgradeneeded?.(new Event("upgradeneeded"));
}
request.onsuccess?.(new Event("success"));
});
return request;
},
deleteDatabase(name: string) {
const request = new FakeRequest();
queueMicrotask(() => {
for (const db of [...fakeState.openConnections]) db.onversionchange?.();
databases.delete(name);
request.onsuccess?.(new Event("success"));
});
return request;
},
};
Object.defineProperty(globalThis, "indexedDB", {
value: indexedDB,
configurable: true,
writable: true,
});
}
class FakeDatabase {
onversionchange: (() => void) | null = null;
onclose: (() => void) | null = null;
closed = false;
constructor(private record: { version: number; stores: Map<string, Map<string, Row>> }) {}
objectStoreNames = {
contains: (name: string) => this.record.stores.has(name),
};
createObjectStore(name: string): void {
if (!this.record.stores.has(name)) this.record.stores.set(name, new Map());
}
transaction(storeName: string): FakeTransaction {
const rows = this.record.stores.get(storeName);
if (!rows) throw new Error(`missing store ${storeName}`);
return new FakeTransaction(rows);
}
close(): void {
this.closed = true;
fakeState.openConnections.delete(this);
this.onclose?.();
}
}
class FakeTransaction {
error: Error | null = null;
oncomplete: (() => void) | null = null;
onabort: (() => void) | null = null;
onerror: (() => void) | null = null;
private pending = 0;
private settled = false;
constructor(private rows: Map<string, Row>) {
queueMicrotask(() => this.finish());
}
objectStore(): FakeStore {
return new FakeStore(this.rows, this);
}
abort(): void {
this.error = this.error ?? new Error("aborted");
if (this.settled) return;
this.settled = true;
this.onabort?.();
}
track<T>(produce: () => T, writes = true): FakeRequest<T> {
this.pending += 1;
const request = new FakeRequest<T>();
queueMicrotask(() => {
try {
if (writes && fakeState.failWrites) {
const error = new Error("indexedDB write failed");
this.error = error;
request.error = error;
request.onerror?.(new Event("error"));
this.settled = true;
this.onerror?.();
this.onabort?.();
return;
}
request.result = produce();
request.onsuccess?.(new Event("success"));
} catch (error) {
request.error = error instanceof Error ? error : new Error(String(error));
this.error = request.error;
request.onerror?.(new Event("error"));
this.settled = true;
this.onerror?.();
this.onabort?.();
return;
} finally {
this.pending -= 1;
this.finish();
}
});
return request;
}
private finish(): void {
if (this.settled || this.pending > 0) return;
this.settled = true;
this.oncomplete?.();
}
}
class FakeStore {
constructor(
private rows: Map<string, Row>,
private tx: FakeTransaction,
) {}
getAll(): FakeRequest<Row[]> {
return this.tx.track(() => [...this.rows.values()].map((row) => ({ ...row })), false);
}
get(id: string): FakeRequest<Row | undefined> {
return this.tx.track(() => {
fakeState.onGet?.(this.rows);
const row = this.rows.get(id);
return row ? { ...row } : undefined;
}, false);
}
put(value: Row): FakeRequest<string> {
return this.tx.track(() => {
this.rows.set(value.id, { ...value });
return value.id;
});
}
delete(id: string): FakeRequest<undefined> {
return this.tx.track(() => {
this.rows.delete(id);
return undefined;
});
}
clear(): FakeRequest<undefined> {
return this.tx.track(() => {
this.rows.clear();
return undefined;
});
}
}
installFakeIndexedDB();
const resetStore = async () => {
fakeState.failWrites = false;
fakeState.onGet = undefined;
await new Promise<void>((resolve) => {
const request = indexedDB.deleteDatabase("netcatty_connection_log_terminal_data");
request.onsuccess = () => resolve();
request.onerror = () => resolve();
});
await new Promise((resolve) => setTimeout(resolve, 0));
};
beforeEach(async () => {
await resetStore();
});
after(() => {
if (previousIndexedDB) Object.defineProperty(globalThis, "indexedDB", previousIndexedDB);
else delete (globalThis as { indexedDB?: unknown }).indexedDB;
});
test("putMany, deleteMany, replaceAll, and clear round-trip one row per log id", async () => {
assert.equal(await replaceAll({ a: "one", b: "two" }), true);
assert.deepEqual(await loadAll(), { a: "one", b: "two" });
const diff = await writeTerminalDataMapDiff(
{ a: "one", b: "two" },
{ a: "one", b: "edited", c: "three" },
);
assert.deepEqual(diff, { persisted: true, changed: true });
assert.deepEqual(await loadAll(), { a: "one", b: "edited", c: "three" });
assert.equal(await replaceAll({ only: "left" }), true);
assert.deepEqual(await loadAll(), { only: "left" });
const { clear } = await import("./connectionLogTerminalDataStore.ts");
assert.equal(await clear(), true);
assert.deepEqual(await loadAll(), {});
});
test("writeTerminalDataMapDiff skips indexedDB when the map is unchanged", async () => {
assert.equal(await putMany({ b: "2", a: "1" }), true);
const result = await writeTerminalDataMapDiff(
{ b: "2", a: "1" },
{ a: "1", b: "2" },
);
assert.deepEqual(result, { persisted: true, changed: false });
assert.deepEqual(await loadAll(), { a: "1", b: "2" });
});
test("migration inserts legacy rows without clearing a replay committed after the empty read", async () => {
fakeState.onGet = (rows) => {
rows.set("concurrent", { id: "concurrent", data: "new-window" });
fakeState.onGet = undefined;
};
let removed = false;
const loaded = await migrateLegacyConnectionLogTerminalData({
read: () => ({ "log-old": "legacy" }),
remove: () => {
removed = true;
},
});
assert.equal(removed, true);
assert.equal(loaded.durable, true);
assert.equal(loaded.map.concurrent, "new-window");
assert.equal(loaded.map["log-old"], "legacy");
assert.deepEqual(await loadAll(), { concurrent: "new-window", "log-old": "legacy" });
});
test("migration copies a legacy map into indexedDB and removes it only after commit", async () => {
let removed = false;
const legacy: ConnectionLogTerminalDataMap = { "log-new": "replay", "log-old": "older" };
const loaded = await migrateLegacyConnectionLogTerminalData({
read: () => legacy,
remove: () => {
removed = true;
},
});
assert.equal(removed, true);
assert.equal(loaded.migrated, true);
assert.deepEqual(loaded.map, legacy);
assert.deepEqual(await loadAll(), legacy);
});
test("migration keeps indexedDB rows and copies legacy ids that are not stored yet", async () => {
assert.equal(await putMany({ kept: "from-idb" }), true);
let removed = false;
const loaded = await migrateLegacyConnectionLogTerminalData({
read: () => ({ kept: "stale-legacy", missing: "from-legacy" }),
remove: () => {
removed = true;
},
});
assert.equal(removed, true);
assert.equal(loaded.migrated, true);
assert.equal(loaded.durable, true);
assert.deepEqual(loaded.map, { kept: "from-idb", missing: "from-legacy" });
assert.deepEqual(await loadAll(), { kept: "from-idb", missing: "from-legacy" });
});
test("migration keeps the legacy key when merging missing rows fails", async () => {
assert.equal(await putMany({ kept: "from-idb" }), true);
fakeState.failWrites = true;
let removed = false;
const loaded = await migrateLegacyConnectionLogTerminalData({
read: () => ({ missing: "from-legacy" }),
remove: () => {
removed = true;
},
});
assert.equal(removed, false);
assert.equal(loaded.migrated, false);
assert.equal(loaded.durable, false);
assert.equal(loaded.map.missing, "from-legacy");
assert.equal(loaded.map.kept, "from-idb");
fakeState.failWrites = false;
assert.deepEqual(await loadAll(), { kept: "from-idb" });
});
test("migration keeps the legacy key when the indexedDB replace fails", async () => {
let removed = false;
fakeState.failWrites = true;
const loaded = await migrateLegacyConnectionLogTerminalData({
read: () => ({ keep: "legacy" }),
remove: () => {
removed = true;
},
});
assert.equal(removed, false);
assert.equal(loaded.migrated, false);
assert.deepEqual(loaded.map, { keep: "legacy" });
fakeState.failWrites = false;
assert.deepEqual(await loadAll(), {});
});
test("store methods soft-fail when indexedDB is unavailable", async () => {
const descriptor = Object.getOwnPropertyDescriptor(globalThis, "indexedDB");
Object.defineProperty(globalThis, "indexedDB", {
value: undefined,
configurable: true,
writable: true,
});
try {
assert.deepEqual(await loadAll(), {});
assert.equal(await putMany({ a: "x" }), false);
assert.equal(await replaceAll({ a: "x" }), false);
const { deleteMany, clear } = await import("./connectionLogTerminalDataStore.ts");
assert.equal(await deleteMany(["a"]), false);
assert.equal(await clear(), false);
} finally {
if (descriptor) Object.defineProperty(globalThis, "indexedDB", descriptor);
}
});
@@ -0,0 +1,371 @@
import {
diffTerminalDataMaps,
type ConnectionLogTerminalDataMap,
} from "../../domain/connectionLogTerminalData.ts";
export const CONNECTION_LOG_TERMINAL_DATA_DB_NAME = "netcatty_connection_log_terminal_data";
export const CONNECTION_LOG_TERMINAL_DATA_STORE_NAME = "entries";
const DB_VERSION = 1;
export type ConnectionLogTerminalDataRev = {
rev: number;
/** Log ids currently in the side store. */
ids?: string[];
/**
* Distinguishes two writes that would otherwise share `rev` and `ids`
* in the same millisecond, so the other window still observes a change.
*/
nonce?: string;
};
export type LegacyConnectionLogTerminalDataMigration = {
read: () => ConnectionLogTerminalDataMap | null;
remove: () => void;
};
export type MigratedConnectionLogTerminalData = {
map: ConnectionLogTerminalDataMap;
migrated: boolean;
/**
* False when `map` contains replay rows that are not committed in IndexedDB.
* Callers must not use that map as a diff baseline until a later write commits.
*/
durable: boolean;
};
type TerminalDataRow = {
id: string;
data: string;
};
let cachedDatabase: IDBDatabase | null = null;
let openingDatabase: Promise<IDBDatabase | null> | null = null;
const warnStoreFailure = (message: string, error: unknown): void => {
console.warn(`[connectionLogTerminalDataStore] ${message}`, error);
};
const forgetDatabase = (): void => {
const db = cachedDatabase;
cachedDatabase = null;
openingDatabase = null;
try {
db?.close();
} catch {
// The connection is already closing.
}
};
const transactionComplete = (tx: IDBTransaction): Promise<void> => new Promise((resolve, reject) => {
let settled = false;
const finish = (fn: () => void) => {
if (settled) return;
settled = true;
fn();
};
tx.oncomplete = () => finish(() => resolve());
tx.onabort = () => finish(() => reject(tx.error ?? new Error("IndexedDB transaction aborted")));
tx.onerror = () => finish(() => reject(tx.error ?? new Error("IndexedDB transaction failed")));
});
const openDatabase = (): Promise<IDBDatabase | null> => new Promise((resolve) => {
try {
if (typeof indexedDB === "undefined" || indexedDB === null) {
resolve(null);
return;
}
const request = indexedDB.open(CONNECTION_LOG_TERMINAL_DATA_DB_NAME, DB_VERSION);
request.onupgradeneeded = () => {
const db = request.result;
if (!db.objectStoreNames.contains(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME)) {
db.createObjectStore(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME, { keyPath: "id" });
}
};
request.onsuccess = () => resolve(request.result);
request.onerror = () => {
warnStoreFailure("IndexedDB open failed", request.error);
resolve(null);
};
} catch (error) {
warnStoreFailure("IndexedDB open failed", error);
resolve(null);
}
});
const getDatabase = async (): Promise<IDBDatabase | null> => {
if (cachedDatabase) return cachedDatabase;
if (!openingDatabase) {
openingDatabase = openDatabase().then((db) => {
openingDatabase = null;
if (!db) return null;
cachedDatabase = db;
db.onversionchange = () => {
if (cachedDatabase === db) cachedDatabase = null;
try {
db.close();
} catch {
// Already closed by the deleting context.
}
};
db.onclose = () => {
if (cachedDatabase === db) cachedDatabase = null;
};
return db;
}).catch((error: unknown) => {
openingDatabase = null;
warnStoreFailure("IndexedDB open failed", error);
return null;
});
}
return openingDatabase;
};
const rowsToMap = (rows: TerminalDataRow[]): ConnectionLogTerminalDataMap => {
const map: ConnectionLogTerminalDataMap = {};
for (const row of rows) {
if (!row || typeof row.id !== "string" || typeof row.data !== "string") continue;
map[row.id] = row.data;
}
return map;
};
const sanitizeMap = (map: ConnectionLogTerminalDataMap | null): ConnectionLogTerminalDataMap => {
if (!map || typeof map !== "object" || Array.isArray(map)) return {};
const next: ConnectionLogTerminalDataMap = {};
for (const [id, data] of Object.entries(map)) {
if (typeof data === "string" && data.length > 0) next[id] = data;
}
return next;
};
const readAll = async (): Promise<ConnectionLogTerminalDataMap | null> => {
try {
const db = await getDatabase();
if (!db) return null;
const tx = db.transaction(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME, "readonly");
const store = tx.objectStore(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME);
const request = store.getAll() as IDBRequest<TerminalDataRow[]>;
let rows: TerminalDataRow[] = [];
request.onsuccess = () => {
rows = request.result ?? [];
};
await transactionComplete(tx);
return rowsToMap(rows);
} catch (error) {
forgetDatabase();
warnStoreFailure("IndexedDB read failed", error);
return null;
}
};
export const loadAll = async (): Promise<ConnectionLogTerminalDataMap> => (await readAll()) ?? {};
/** `null` when IndexedDB could not be read. `{}` is a successful empty store. */
export const tryLoadAll = async (): Promise<ConnectionLogTerminalDataMap | null> => readAll();
export const replaceAll = async (map: ConnectionLogTerminalDataMap): Promise<boolean> => {
try {
const db = await getDatabase();
if (!db) return false;
const entries = sanitizeMap(map);
const tx = db.transaction(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME, "readwrite");
const store = tx.objectStore(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME);
const done = transactionComplete(tx);
store.clear();
for (const [id, data] of Object.entries(entries)) {
store.put({ id, data });
}
await done;
return true;
} catch (error) {
forgetDatabase();
warnStoreFailure("IndexedDB replace failed", error);
return false;
}
};
/** Insert ids that are still absent. Does not clear or overwrite existing rows. */
const putMissing = async (entries: ConnectionLogTerminalDataMap): Promise<boolean> => {
const rows = sanitizeMap(entries);
const ids = Object.keys(rows);
if (ids.length === 0) return true;
try {
const db = await getDatabase();
if (!db) return false;
const tx = db.transaction(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME, "readwrite");
const store = tx.objectStore(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME);
const done = transactionComplete(tx);
for (const id of ids) {
const request = store.get(id) as IDBRequest<TerminalDataRow | undefined>;
request.onsuccess = () => {
if (request.result === undefined) store.put({ id, data: rows[id] });
};
}
await done;
return true;
} catch (error) {
forgetDatabase();
warnStoreFailure("IndexedDB put failed", error);
return false;
}
};
export const putMany = async (entries: ConnectionLogTerminalDataMap): Promise<boolean> => {
const rows = sanitizeMap(entries);
const ids = Object.keys(rows);
if (ids.length === 0) return true;
try {
const db = await getDatabase();
if (!db) return false;
const tx = db.transaction(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME, "readwrite");
const store = tx.objectStore(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME);
const done = transactionComplete(tx);
for (const id of ids) store.put({ id, data: rows[id] });
await done;
return true;
} catch (error) {
forgetDatabase();
warnStoreFailure("IndexedDB put failed", error);
return false;
}
};
export const deleteMany = async (ids: readonly string[]): Promise<boolean> => {
if (ids.length === 0) return true;
try {
const db = await getDatabase();
if (!db) return false;
const tx = db.transaction(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME, "readwrite");
const store = tx.objectStore(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME);
const done = transactionComplete(tx);
for (const id of ids) store.delete(id);
await done;
return true;
} catch (error) {
forgetDatabase();
warnStoreFailure("IndexedDB delete failed", error);
return false;
}
};
export const clear = async (): Promise<boolean> => {
try {
const db = await getDatabase();
if (!db) return false;
const tx = db.transaction(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME, "readwrite");
const store = tx.objectStore(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME);
const done = transactionComplete(tx);
store.clear();
await done;
return true;
} catch (error) {
forgetDatabase();
warnStoreFailure("IndexedDB clear failed", error);
return false;
}
};
/**
* Apply a persisted-map diff in one transaction. Unchanged maps do not open
* a write. A failed commit leaves the previous rows in place for a retry.
*/
export const writeTerminalDataMapDiff = async (
previous: ConnectionLogTerminalDataMap,
next: ConnectionLogTerminalDataMap,
): Promise<{ persisted: boolean; changed: boolean }> => {
const diff = diffTerminalDataMaps(previous, next);
const puts = sanitizeMap(diff.puts);
const putIds = Object.keys(puts);
if (putIds.length === 0 && diff.deleteIds.length === 0) {
return { persisted: true, changed: false };
}
try {
const db = await getDatabase();
if (!db) return { persisted: false, changed: true };
const tx = db.transaction(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME, "readwrite");
const store = tx.objectStore(CONNECTION_LOG_TERMINAL_DATA_STORE_NAME);
const done = transactionComplete(tx);
for (const id of putIds) store.put({ id, data: puts[id] });
for (const id of diff.deleteIds) store.delete(id);
await done;
return { persisted: true, changed: true };
} catch (error) {
forgetDatabase();
warnStoreFailure("IndexedDB diff write failed", error);
return { persisted: false, changed: true };
}
};
const safeRemove = (remove: () => void): void => {
try {
remove();
} catch (error) {
warnStoreFailure("Failed to remove legacy terminal replay blob", error);
}
};
const readLegacyMap = (
legacy: LegacyConnectionLogTerminalDataMigration,
): ConnectionLogTerminalDataMap | null => {
try {
return sanitizeMap(legacy.read());
} catch (error) {
warnStoreFailure("Failed to read legacy terminal replay blob", error);
return null;
}
};
/**
* One-shot move from the legacy localStorage blob into IndexedDB.
* An empty IndexedDB plus a legacy blob is copied, then the legacy key is
* removed only after the transaction commits. Rows already in IndexedDB are
* kept; legacy ids that are not there yet are inserted before the key is
* removed. A failed read does not treat the store as empty, and a failed
* write leaves the legacy key in place.
*/
export const migrateLegacyConnectionLogTerminalData = async (
legacy: LegacyConnectionLogTerminalDataMigration,
): Promise<MigratedConnectionLogTerminalData> => {
const loaded = await readAll();
if (loaded === null) {
const fallback = readLegacyMap(legacy);
return { map: fallback ?? {}, migrated: false, durable: false };
}
if (Object.keys(loaded).length > 0) {
const legacyMap = readLegacyMap(legacy);
if (legacyMap === null) return { map: loaded, migrated: false, durable: true };
const missing: ConnectionLogTerminalDataMap = {};
for (const [id, data] of Object.entries(legacyMap)) {
if (!Object.prototype.hasOwnProperty.call(loaded, id)) missing[id] = data;
}
if (Object.keys(missing).length === 0) {
safeRemove(legacy.remove);
return { map: loaded, migrated: false, durable: true };
}
const persisted = await putMissing(missing);
if (!persisted) {
return { map: { ...missing, ...loaded }, migrated: false, durable: false };
}
safeRemove(legacy.remove);
const committed = await readAll();
return {
map: committed ?? { ...loaded, ...missing },
migrated: true,
durable: committed !== null,
};
}
const legacyMap = readLegacyMap(legacy);
if (legacyMap === null) return { map: {}, migrated: false, durable: false };
if (Object.keys(legacyMap).length === 0) {
safeRemove(legacy.remove);
return { map: {}, migrated: false, durable: true };
}
// Insert without clearing. A row committed after the empty readAll must stay.
const persisted = await putMissing(legacyMap);
if (!persisted) return { map: legacyMap, migrated: false, durable: false };
safeRemove(legacy.remove);
const committed = await readAll();
return { map: committed ?? legacyMap, migrated: true, durable: committed !== null };
};