Preserve persisted settings and secrets on hydration failures
- Keep existing secrets when encryption is unavailable - Treat malformed secrets files as empty instead of crashing - Fix client settings hydration state resets for repeated loads
This commit is contained in:
1 parent
5331bd2f45
commit
f51a46b46b
4 files changed
+45
-9
No files matched your search
@@ -105,7 +105,7 @@ describe("clientPersistence", () => {
|
||||
).toBe("bearer-token");
|
||||
});
|
||||
|
||||
it("drops persisted secrets when encryption is unavailable", () => {
|
||||
it("preserves existing secrets when encryption is unavailable", () => {
|
||||
const secretsPath = makeTempPath("saved-environment-secrets.json");
|
||||
const availableSecretStorage = makeSecretStorage(true);
|
||||
|
||||
@@ -131,7 +131,7 @@ describe("clientPersistence", () => {
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secretStorage: availableSecretStorage,
|
||||
}),
|
||||
).toBeNull();
|
||||
).toBe("bearer-token");
|
||||
});
|
||||
|
||||
it("removes saved environment secrets", () => {
|
||||
@@ -158,4 +158,24 @@ describe("clientPersistence", () => {
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("treats malformed secrets documents as empty", () => {
|
||||
const secretsPath = makeTempPath("saved-environment-secrets.json");
|
||||
fs.writeFileSync(secretsPath, "{}\n", "utf8");
|
||||
|
||||
expect(
|
||||
readSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secretStorage: makeSecretStorage(true),
|
||||
}),
|
||||
).toBeNull();
|
||||
|
||||
expect(() =>
|
||||
removeSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
}),
|
||||
).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import * as FS from "node:fs";
|
||||
import * as Path from "node:path";
|
||||
|
||||
import type { ClientSettings, PersistedSavedEnvironmentRecord } from "@t3tools/contracts";
|
||||
import { Predicate } from "effect";
|
||||
|
||||
interface ClientSettingsDocument {
|
||||
readonly settings: ClientSettings;
|
||||
@@ -41,7 +42,8 @@ function writeJsonFile(filePath: string, value: unknown): void {
|
||||
}
|
||||
|
||||
function readSecretsDocument(filePath: string): SavedEnvironmentSecretsDocument {
|
||||
return readJsonFile<SavedEnvironmentSecretsDocument>(filePath) ?? { byId: {} };
|
||||
const parsed = readJsonFile<SavedEnvironmentSecretsDocument>(filePath);
|
||||
return Predicate.isObject(parsed) && Predicate.isObject(parsed.byId) ? parsed : { byId: {} };
|
||||
}
|
||||
|
||||
export function readClientSettings(settingsPath: string): ClientSettings | null {
|
||||
@@ -96,7 +98,6 @@ export function writeSavedEnvironmentSecret(input: {
|
||||
const { [input.environmentId]: _previous, ...remaining } = document.byId;
|
||||
|
||||
if (!input.secretStorage.isEncryptionAvailable()) {
|
||||
writeJsonFile(input.secretsPath, { byId: remaining } satisfies SavedEnvironmentSecretsDocument);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -114,7 +115,7 @@ export function removeSavedEnvironmentSecret(input: {
|
||||
readonly environmentId: string;
|
||||
}): void {
|
||||
const document = readSecretsDocument(input.secretsPath);
|
||||
if (!(input.environmentId in document.byId)) {
|
||||
if (!(Predicate.isObject(document.byId) && input.environmentId in document.byId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -95,11 +95,12 @@ async function hydrateSavedEnvironmentRegistry(): Promise<void> {
|
||||
}
|
||||
})();
|
||||
|
||||
savedEnvironmentRegistryHydrationPromise = nextHydration.finally(() => {
|
||||
if (savedEnvironmentRegistryHydrationPromise === nextHydration) {
|
||||
const hydrationPromise = nextHydration.finally(() => {
|
||||
if (savedEnvironmentRegistryHydrationPromise === hydrationPromise) {
|
||||
savedEnvironmentRegistryHydrationPromise = null;
|
||||
}
|
||||
});
|
||||
savedEnvironmentRegistryHydrationPromise = hydrationPromise;
|
||||
|
||||
return savedEnvironmentRegistryHydrationPromise;
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ const CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE = "[CLIENT_SETTINGS]";
|
||||
|
||||
const clientSettingsListeners = new Set<() => void>();
|
||||
let clientSettingsSnapshot = DEFAULT_CLIENT_SETTINGS;
|
||||
let clientSettingsHydrated = false;
|
||||
let clientSettingsHydrationPromise: Promise<void> | null = null;
|
||||
|
||||
function emitClientSettingsChange() {
|
||||
@@ -68,6 +69,9 @@ function subscribeClientSettings(listener: () => void): () => void {
|
||||
}
|
||||
|
||||
async function hydrateClientSettings(): Promise<void> {
|
||||
if (clientSettingsHydrated) {
|
||||
return;
|
||||
}
|
||||
if (clientSettingsHydrationPromise) {
|
||||
return clientSettingsHydrationPromise;
|
||||
}
|
||||
@@ -81,14 +85,17 @@ async function hydrateClientSettings(): Promise<void> {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`${CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE} hydrate failed`, error);
|
||||
} finally {
|
||||
clientSettingsHydrated = true;
|
||||
}
|
||||
})();
|
||||
|
||||
clientSettingsHydrationPromise = nextHydration.finally(() => {
|
||||
if (clientSettingsHydrationPromise === nextHydration) {
|
||||
const hydrationPromise = nextHydration.finally(() => {
|
||||
if (clientSettingsHydrationPromise === hydrationPromise) {
|
||||
clientSettingsHydrationPromise = null;
|
||||
}
|
||||
});
|
||||
clientSettingsHydrationPromise = hydrationPromise;
|
||||
|
||||
return clientSettingsHydrationPromise;
|
||||
}
|
||||
@@ -320,3 +327,10 @@ export function migrateLocalSettingsToServer(): void {
|
||||
localStorage.removeItem(OLD_SETTINGS_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
export function __resetClientSettingsPersistenceForTests(): void {
|
||||
clientSettingsSnapshot = DEFAULT_CLIENT_SETTINGS;
|
||||
clientSettingsHydrated = false;
|
||||
clientSettingsHydrationPromise = null;
|
||||
clientSettingsListeners.clear();
|
||||
}
|
||||
Reference in new issue
Block a user