Skip to content

Commit 8fc0531

Browse files
committed
refactor: extract storage file path helpers
1 parent 39bf72a commit 8fc0531

2 files changed

Lines changed: 64 additions & 33 deletions

File tree

lib/storage.ts

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ export {
2323
} from "./storage/identity.js";
2424

2525
import { normalizeEmailKey } from "./storage/identity.js";
26+
import {
27+
ACCOUNTS_BACKUP_SUFFIX,
28+
ACCOUNTS_WAL_SUFFIX,
29+
getFlaggedAccountsPath as buildFlaggedAccountsPath,
30+
getLegacyFlaggedAccountsPath as buildLegacyFlaggedAccountsPath,
31+
getAccountsBackupPath,
32+
getAccountsBackupRecoveryCandidates,
33+
getAccountsWalPath,
34+
getIntentionalResetMarkerPath,
35+
RESET_MARKER_SUFFIX,
36+
} from "./storage/file-paths.js";
2637
import {
2738
type AccountMetadataV1,
2839
type AccountMetadataV3,
@@ -54,12 +65,8 @@ const log = createLogger("storage");
5465
const ACCOUNTS_FILE_NAME = "openai-codex-accounts.json";
5566
const FLAGGED_ACCOUNTS_FILE_NAME = "openai-codex-flagged-accounts.json";
5667
const LEGACY_FLAGGED_ACCOUNTS_FILE_NAME = "openai-codex-blocked-accounts.json";
57-
const ACCOUNTS_BACKUP_SUFFIX = ".bak";
58-
const ACCOUNTS_WAL_SUFFIX = ".wal";
59-
const ACCOUNTS_BACKUP_HISTORY_DEPTH = 3;
6068
const BACKUP_COPY_MAX_ATTEMPTS = 5;
6169
const BACKUP_COPY_BASE_DELAY_MS = 10;
62-
const RESET_MARKER_SUFFIX = ".reset-intent";
6370
let storageBackupEnabled = true;
6471
let lastAccountsSaveTimestamp = 0;
6572

@@ -367,25 +374,6 @@ export function setStorageBackupEnabled(enabled: boolean): void {
367374
storageBackupEnabled = enabled;
368375
}
369376

370-
function getAccountsBackupPath(path: string): string {
371-
return `${path}${ACCOUNTS_BACKUP_SUFFIX}`;
372-
}
373-
374-
function getAccountsBackupPathAtIndex(path: string, index: number): string {
375-
if (index <= 0) {
376-
return getAccountsBackupPath(path);
377-
}
378-
return `${path}${ACCOUNTS_BACKUP_SUFFIX}.${index}`;
379-
}
380-
381-
function getAccountsBackupRecoveryCandidates(path: string): string[] {
382-
const candidates: string[] = [];
383-
for (let i = 0; i < ACCOUNTS_BACKUP_HISTORY_DEPTH; i += 1) {
384-
candidates.push(getAccountsBackupPathAtIndex(path, i));
385-
}
386-
return candidates;
387-
}
388-
389377
async function getAccountsBackupRecoveryCandidatesWithDiscovery(
390378
path: string,
391379
): Promise<string[]> {
@@ -425,10 +413,6 @@ async function getAccountsBackupRecoveryCandidatesWithDiscovery(
425413
return [...knownCandidates, ...discoveredOrdered];
426414
}
427415

428-
function getAccountsWalPath(path: string): string {
429-
return `${path}${ACCOUNTS_WAL_SUFFIX}`;
430-
}
431-
432416
async function copyFileWithRetry(
433417
sourcePath: string,
434418
destinationPath: string,
@@ -605,10 +589,6 @@ function computeSha256(value: string): string {
605589
return createHash("sha256").update(value).digest("hex");
606590
}
607591

608-
function getIntentionalResetMarkerPath(path: string): string {
609-
return `${path}${RESET_MARKER_SUFFIX}`;
610-
}
611-
612592
function createEmptyStorageWithMetadata(
613593
restoreEligible: boolean,
614594
restoreReason: RestoreReason,
@@ -996,11 +976,14 @@ export async function exportNamedBackup(
996976
}
997977

998978
export function getFlaggedAccountsPath(): string {
999-
return join(dirname(getStoragePath()), FLAGGED_ACCOUNTS_FILE_NAME);
979+
return buildFlaggedAccountsPath(getStoragePath(), FLAGGED_ACCOUNTS_FILE_NAME);
1000980
}
1001981

1002982
function getLegacyFlaggedAccountsPath(): string {
1003-
return join(dirname(getStoragePath()), LEGACY_FLAGGED_ACCOUNTS_FILE_NAME);
983+
return buildLegacyFlaggedAccountsPath(
984+
getStoragePath(),
985+
LEGACY_FLAGGED_ACCOUNTS_FILE_NAME,
986+
);
1004987
}
1005988

1006989
async function migrateLegacyProjectStorageIfNeeded(

lib/storage/file-paths.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { dirname, join } from "node:path";
2+
3+
export const ACCOUNTS_BACKUP_SUFFIX = ".bak";
4+
export const ACCOUNTS_WAL_SUFFIX = ".wal";
5+
const ACCOUNTS_BACKUP_HISTORY_DEPTH = 3;
6+
export const RESET_MARKER_SUFFIX = ".reset-intent";
7+
8+
export function getAccountsBackupPath(path: string): string {
9+
return `${path}${ACCOUNTS_BACKUP_SUFFIX}`;
10+
}
11+
12+
export function getAccountsBackupPathAtIndex(
13+
path: string,
14+
index: number,
15+
): string {
16+
if (index <= 0) return getAccountsBackupPath(path);
17+
return `${path}${ACCOUNTS_BACKUP_SUFFIX}.${index}`;
18+
}
19+
20+
export function getAccountsBackupRecoveryCandidates(path: string): string[] {
21+
const candidates: string[] = [];
22+
for (let i = 0; i < ACCOUNTS_BACKUP_HISTORY_DEPTH; i += 1) {
23+
candidates.push(getAccountsBackupPathAtIndex(path, i));
24+
}
25+
return candidates;
26+
}
27+
28+
export function getAccountsWalPath(path: string): string {
29+
return `${path}${ACCOUNTS_WAL_SUFFIX}`;
30+
}
31+
32+
export function getIntentionalResetMarkerPath(path: string): string {
33+
return `${path}${RESET_MARKER_SUFFIX}`;
34+
}
35+
36+
export function getFlaggedAccountsPath(
37+
storagePath: string,
38+
fileName: string,
39+
): string {
40+
return join(dirname(storagePath), fileName);
41+
}
42+
43+
export function getLegacyFlaggedAccountsPath(
44+
storagePath: string,
45+
legacyFileName: string,
46+
): string {
47+
return join(dirname(storagePath), legacyFileName);
48+
}

0 commit comments

Comments
 (0)