mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-01 14:12:51 +00:00
* fix(app): validate persisted client state Persisted replica data could satisfy TypeScript through an assertion while missing fields required by renderers. Treat every AsyncStorage value as untrusted input, clear values that fail strict schemas, and version the replica DTO so incompatible caches cannot hydrate. * fix(app): preserve validated legacy state * fix(app): preserve validated preferences Preserve supported legacy location fields and every shipped theme while keeping unknown persisted fields rejected. Align browser test seeds with the current preference shape so strict validation exercises the intended provider. * fix(app): migrate legacy agent preferences Released clients stored host/location fields and per-provider thinkingOptionId together. Migrate those exact strict bytes into current preferences without admitting unknown fields. * fix(app): preserve drafts with legacy reviews Accept the exact strict shape released for workspace review attachments so migration can discard that obsolete attachment without clearing every persisted draft.
75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
import { z } from "zod";
|
|
import { readValidatedString } from "@/storage/validated-storage";
|
|
|
|
const CLIENT_ID_STORAGE_KEY = "@paseo:client-id-v1";
|
|
|
|
export interface ClientIdStorage {
|
|
getItem(key: string): Promise<string | null>;
|
|
setItem(key: string, value: string): Promise<void>;
|
|
removeItem(key: string): Promise<void>;
|
|
}
|
|
|
|
export interface ClientIdResolver {
|
|
getOrCreate(): Promise<string>;
|
|
}
|
|
|
|
const ClientIdSchema = z.string().trim().min(1);
|
|
|
|
export function createClientIdResolver(deps: {
|
|
storage: ClientIdStorage;
|
|
generateUuid: () => string;
|
|
storageKey?: string;
|
|
}): ClientIdResolver {
|
|
const storageKey = deps.storageKey ?? CLIENT_ID_STORAGE_KEY;
|
|
let cached: string | null = null;
|
|
let inFlight: Promise<string> | null = null;
|
|
|
|
return {
|
|
async getOrCreate(): Promise<string> {
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
if (inFlight) {
|
|
return inFlight;
|
|
}
|
|
|
|
inFlight = (async () => {
|
|
const existing = await readValidatedString(deps.storage, storageKey, ClientIdSchema);
|
|
if (existing) {
|
|
cached = existing;
|
|
return existing;
|
|
}
|
|
|
|
const next = `cid_${deps.generateUuid()}`;
|
|
await deps.storage.setItem(storageKey, next);
|
|
cached = next;
|
|
return next;
|
|
})();
|
|
|
|
try {
|
|
return await inFlight;
|
|
} finally {
|
|
inFlight = null;
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
function generateUuidFromGlobalCrypto(): string {
|
|
const cryptoObj = globalThis.crypto;
|
|
if (cryptoObj && typeof cryptoObj.randomUUID === "function") {
|
|
return cryptoObj.randomUUID().replace(/-/g, "");
|
|
}
|
|
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`;
|
|
}
|
|
|
|
const defaultResolver = createClientIdResolver({
|
|
storage: AsyncStorage,
|
|
generateUuid: generateUuidFromGlobalCrypto,
|
|
});
|
|
|
|
export async function getOrCreateClientId(): Promise<string> {
|
|
return defaultResolver.getOrCreate();
|
|
}
|