Skip to content

Commit 5a91058

Browse files
committed
refactor: extract restore metadata helpers
1 parent 1fe2c2d commit 5a91058

2 files changed

Lines changed: 42 additions & 35 deletions

File tree

lib/storage.ts

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import {
3131
buildRestoreAssessment,
3232
collectBackupMetadata,
3333
} from "./storage/restore-assessment.js";
34+
import {
35+
createEmptyStorageWithRestoreMetadata,
36+
withRestoreMetadata,
37+
} from "./storage/restore-metadata.js";
3438
import {
3539
describeAccountsWalSnapshot,
3640
describeFlaggedSnapshot,
@@ -116,11 +120,6 @@ export interface FlaggedAccountStorageV1 {
116120

117121
type RestoreReason = "empty-storage" | "intentional-reset" | "missing-storage";
118122

119-
type AccountStorageWithMetadata = AccountStorageV3 & {
120-
restoreEligible?: boolean;
121-
restoreReason?: RestoreReason;
122-
};
123-
124123
export type BackupMetadata = {
125124
accounts: BackupMetadataSection;
126125
flaggedAccounts: BackupMetadataSection;
@@ -472,32 +471,6 @@ async function cleanupStaleRotatingBackupArtifacts(
472471
}
473472
}
474473

475-
function createEmptyStorageWithMetadata(
476-
restoreEligible: boolean,
477-
restoreReason: RestoreReason,
478-
): AccountStorageWithMetadata {
479-
return {
480-
version: 3,
481-
accounts: [],
482-
activeIndex: 0,
483-
activeIndexByFamily: {},
484-
restoreEligible,
485-
restoreReason,
486-
};
487-
}
488-
489-
function withRestoreMetadata(
490-
storage: AccountStorageV3,
491-
restoreEligible: boolean,
492-
restoreReason: RestoreReason,
493-
): AccountStorageWithMetadata {
494-
return {
495-
...storage,
496-
restoreEligible,
497-
restoreReason,
498-
};
499-
}
500-
501474
async function loadFlaggedAccountsFromPath(
502475
path: string,
503476
): Promise<FlaggedAccountStorageV1> {
@@ -1313,7 +1286,7 @@ async function loadAccountsInternal(
13131286
}
13141287

13151288
if (existsSync(resetMarkerPath)) {
1316-
return createEmptyStorageWithMetadata(false, "intentional-reset");
1289+
return createEmptyStorageWithRestoreMetadata(false, "intentional-reset");
13171290
}
13181291

13191292
if (normalized && normalized.accounts.length === 0) {
@@ -1370,7 +1343,7 @@ async function loadAccountsInternal(
13701343
} catch (error) {
13711344
const code = (error as NodeJS.ErrnoException).code;
13721345
if (existsSync(resetMarkerPath)) {
1373-
return createEmptyStorageWithMetadata(false, "intentional-reset");
1346+
return createEmptyStorageWithRestoreMetadata(false, "intentional-reset");
13741347
}
13751348
if (code === "ENOENT" && migratedLegacyStorage) {
13761349
return migratedLegacyStorage;
@@ -1391,7 +1364,7 @@ async function loadAccountsInternal(
13911364
return recoveredFromWal;
13921365
}
13931366
if (existsSync(resetMarkerPath)) {
1394-
return createEmptyStorageWithMetadata(false, "intentional-reset");
1367+
return createEmptyStorageWithRestoreMetadata(false, "intentional-reset");
13951368
}
13961369

13971370
if (storageBackupEnabled) {
@@ -1439,7 +1412,7 @@ async function loadAccountsInternal(
14391412
log.error("Failed to load account storage", { error: String(error) });
14401413
}
14411414
if (code === "ENOENT") {
1442-
return createEmptyStorageWithMetadata(true, "missing-storage");
1415+
return createEmptyStorageWithRestoreMetadata(true, "missing-storage");
14431416
}
14441417
return null;
14451418
}

lib/storage/restore-metadata.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { AccountStorageV3 } from "../storage.js";
2+
3+
type RestoreReason = "empty-storage" | "intentional-reset" | "missing-storage";
4+
5+
type AccountStorageWithMetadata = AccountStorageV3 & {
6+
restoreEligible?: boolean;
7+
restoreReason?: RestoreReason;
8+
};
9+
10+
export function createEmptyStorageWithRestoreMetadata(
11+
restoreEligible: boolean,
12+
restoreReason: RestoreReason,
13+
): AccountStorageWithMetadata {
14+
return {
15+
version: 3,
16+
accounts: [],
17+
activeIndex: 0,
18+
activeIndexByFamily: {},
19+
restoreEligible,
20+
restoreReason,
21+
};
22+
}
23+
24+
export function withRestoreMetadata(
25+
storage: AccountStorageV3,
26+
restoreEligible: boolean,
27+
restoreReason: RestoreReason,
28+
): AccountStorageWithMetadata {
29+
return {
30+
...storage,
31+
restoreEligible,
32+
restoreReason,
33+
};
34+
}

0 commit comments

Comments
 (0)