File size: 2,687 Bytes
b6faf15 10b580f b6faf15 10b580f ef8bb5f dca3016 ef8bb5f dca3016 ef8bb5f dca3016 ef8bb5f 10b580f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | // Browser/account identifier used as the user_id query parameter on
// plugin API calls. Two flavours:
//
// * For a guest (no HF session): a per-browser anonymous UUID
// persisted in localStorage. Plugins stored under this id live in
// /tmp on the Space (volatile, per-browser).
// * For a signed-in HF user: never read by getUserId() directly —
// the server overrides the query value with the session-derived
// hf_<username> whenever it sees a session cookie. We still send
// the anon UUID as a fallback so the route works without cookies.
//
// In other words: the frontend always sends the anon UUID. The server
// decides which namespace to operate on (session > query).
const LS_KEY = "quda.userId";
/** Read the current anonymous user id, or mint one on first visit.
*
* Even when the user is signed in, we still surface the anon id —
* the server treats it as the fallback namespace for unauthenticated
* routes. The actual logged-in namespace is server-derived, not
* client-controlled (anti-spoofing).
*/
export function getUserId(): string {
try {
const existing = localStorage.getItem(LS_KEY);
if (existing && /^[a-zA-Z0-9_-]{8,64}$/.test(existing)) {
return existing;
}
} catch {
/* ignore — fall through to mint */
}
const fresh = mintUserId();
try {
localStorage.setItem(LS_KEY, fresh);
} catch {
/* if storage is blocked the id is still valid for this tab */
}
return fresh;
}
/** Bump a localStorage counter so other tabs' `storage` listeners fire.
* Plugin lists themselves live in Zustand (in-memory per tab); this
* counter is the only thing that crosses the localStorage boundary
* for cross-tab sync. */
export function bumpPluginsRev(): void {
bumpRev("quda.pluginsRev");
}
function bumpRev(key: string): void {
try {
const prev = Number(localStorage.getItem(key) || "0");
localStorage.setItem(
key,
String(Number.isFinite(prev) ? prev + 1 : 1),
);
} catch {
/* storage blocked — multi-tab sync is best-effort */
}
}
/** RFC4122-ish random hex, no hyphens — fits the backend regex
* `[a-zA-Z0-9_-]{8,64}` without ambiguity. */
function mintUserId(): string {
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
const arr = new Uint8Array(16);
crypto.getRandomValues(arr);
return Array.from(arr, (b) => b.toString(16).padStart(2, "0")).join("");
}
// Fallback: Math.random based. Not cryptographically strong but the
// ID is just a namespace separator, not a secret.
return (
Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)
).padEnd(16, "0").slice(0, 32);
}
|