|
| 1 | +import type { AccountProfile, ManagedUserInfo } from "./types"; |
| 2 | + |
| 3 | +const CACHE_KEY_PREFIX = "kimi-code.account-profile.v1:"; |
| 4 | +const CACHE_VERSION = 1; |
| 5 | +const CACHE_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000; |
| 6 | + |
| 7 | +export interface AccountProfileStorage { |
| 8 | + readonly length: number; |
| 9 | + getItem(key: string): string | null; |
| 10 | + key(index: number): string | null; |
| 11 | + removeItem(key: string): void; |
| 12 | + setItem(key: string, value: string): void; |
| 13 | +} |
| 14 | + |
| 15 | +interface CachedAccountProfile { |
| 16 | + version: typeof CACHE_VERSION; |
| 17 | + provider: string; |
| 18 | + updatedAt: number; |
| 19 | + profile: AccountProfile; |
| 20 | +} |
| 21 | + |
| 22 | +function browserStorage(): AccountProfileStorage | undefined { |
| 23 | + try { |
| 24 | + return typeof window === "undefined" ? undefined : window.localStorage; |
| 25 | + } catch { |
| 26 | + return undefined; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function cacheKey(provider: string): string { |
| 31 | + return `${CACHE_KEY_PREFIX}${provider}`; |
| 32 | +} |
| 33 | + |
| 34 | +function optionalString(value: unknown): string | undefined { |
| 35 | + return typeof value === "string" ? value : undefined; |
| 36 | +} |
| 37 | + |
| 38 | +function parseProfile(value: unknown): AccountProfile | undefined { |
| 39 | + if (!value || typeof value !== "object") return undefined; |
| 40 | + const profile = value as Record<string, unknown>; |
| 41 | + if ( |
| 42 | + typeof profile.userId !== "string" || |
| 43 | + typeof profile.nickname !== "string" || |
| 44 | + typeof profile.userLevel !== "number" || |
| 45 | + typeof profile.userLevelName !== "string" |
| 46 | + ) { |
| 47 | + return undefined; |
| 48 | + } |
| 49 | + return { |
| 50 | + userId: profile.userId, |
| 51 | + nickname: profile.nickname, |
| 52 | + userLevel: profile.userLevel, |
| 53 | + userLevelName: profile.userLevelName, |
| 54 | + avatar: optionalString(profile.avatar), |
| 55 | + username: optionalString(profile.username), |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +export function accountProfileFromUserInfo( |
| 60 | + userInfo: ManagedUserInfo, |
| 61 | +): AccountProfile { |
| 62 | + return { |
| 63 | + userId: userInfo.userId, |
| 64 | + nickname: userInfo.nickname, |
| 65 | + userLevel: userInfo.userLevel, |
| 66 | + userLevelName: userInfo.userLevelName, |
| 67 | + avatar: userInfo.avatar, |
| 68 | + username: userInfo.username, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +export function readCachedAccountProfile( |
| 73 | + provider: string, |
| 74 | + storage: AccountProfileStorage | undefined = browserStorage(), |
| 75 | + now = Date.now(), |
| 76 | +): AccountProfile | undefined { |
| 77 | + if (!storage) return undefined; |
| 78 | + const key = cacheKey(provider); |
| 79 | + try { |
| 80 | + const raw = storage.getItem(key); |
| 81 | + if (!raw) return undefined; |
| 82 | + const cached = JSON.parse(raw) as Partial<CachedAccountProfile>; |
| 83 | + const profile = parseProfile(cached.profile); |
| 84 | + if ( |
| 85 | + cached.version !== CACHE_VERSION || |
| 86 | + cached.provider !== provider || |
| 87 | + typeof cached.updatedAt !== "number" || |
| 88 | + now - cached.updatedAt > CACHE_MAX_AGE_MS || |
| 89 | + cached.updatedAt > now || |
| 90 | + !profile |
| 91 | + ) { |
| 92 | + storage.removeItem(key); |
| 93 | + return undefined; |
| 94 | + } |
| 95 | + return profile; |
| 96 | + } catch { |
| 97 | + try { |
| 98 | + storage.removeItem(key); |
| 99 | + } catch { |
| 100 | + // Storage access is best-effort. |
| 101 | + } |
| 102 | + return undefined; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +export function writeCachedAccountProfile( |
| 107 | + provider: string, |
| 108 | + profile: AccountProfile, |
| 109 | + storage: AccountProfileStorage | undefined = browserStorage(), |
| 110 | + now = Date.now(), |
| 111 | +): void { |
| 112 | + if (!storage) return; |
| 113 | + const cached: CachedAccountProfile = { |
| 114 | + version: CACHE_VERSION, |
| 115 | + provider, |
| 116 | + updatedAt: now, |
| 117 | + profile, |
| 118 | + }; |
| 119 | + try { |
| 120 | + storage.setItem(cacheKey(provider), JSON.stringify(cached)); |
| 121 | + } catch { |
| 122 | + // Profile display remains functional when persistence is unavailable. |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export function clearCachedAccountProfiles( |
| 127 | + storage: AccountProfileStorage | undefined = browserStorage(), |
| 128 | +): void { |
| 129 | + if (!storage) return; |
| 130 | + try { |
| 131 | + for (let index = storage.length - 1; index >= 0; index -= 1) { |
| 132 | + const key = storage.key(index); |
| 133 | + if (key?.startsWith(CACHE_KEY_PREFIX)) storage.removeItem(key); |
| 134 | + } |
| 135 | + } catch { |
| 136 | + // Storage access is best-effort. |
| 137 | + } |
| 138 | +} |
0 commit comments