Download frontend/src/lib/userId.ts from jqub21/quaim: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/spaces/jqub21/quaim/resolve/8311ef0480c4e9dd94de89120b420e1957f31fc9/frontend/src/lib/userId.ts
- Command line
-
hf download hf://spaces/jqub21/quaim@8311ef0480c4e9dd94de89120b420e1957f31fc9/frontend/src/lib/userId.ts
-
curl -L -o userId.ts https://huggingface.co/spaces/jqub21/quaim/resolve/8311ef0480c4e9dd94de89120b420e1957f31fc9/frontend/src/lib/userId.ts
2.69 kB
| // 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); | |
| } | |