Spaces:
Running
Running
Download lib/client-anon.ts from Jacksuyu/demandsolution-codex: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/Jacksuyu/demandsolution-codex/resolve/main/lib/client-anon.ts
- Command line
-
hf download hf://spaces/Jacksuyu/demandsolution-codex/lib/client-anon.ts
-
curl -L -o client-anon.ts https://huggingface.co/spaces/Jacksuyu/demandsolution-codex/resolve/main/lib/client-anon.ts
1.01 kB
| const STORAGE_KEY = "anon_id"; | |
| function fallbackId(): string { | |
| return `anon_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`; | |
| } | |
| export function getOrCreateClientAnonId(): string { | |
| if (typeof window === "undefined") return ""; | |
| try { | |
| const existing = window.localStorage.getItem(STORAGE_KEY); | |
| if (existing) return existing; | |
| const created = typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID() : fallbackId(); | |
| window.localStorage.setItem(STORAGE_KEY, created); | |
| return created; | |
| } catch { | |
| return ""; | |
| } | |
| } | |
| export function withAnonHeaders(init: RequestInit = {}): RequestInit { | |
| const headers = new Headers(init.headers || {}); | |
| const anonId = getOrCreateClientAnonId(); | |
| if (anonId) { | |
| headers.set("x-anon-id", anonId); | |
| } | |
| return { | |
| ...init, | |
| headers, | |
| credentials: "include", | |
| }; | |
| } | |
| export function fetchWithAnon(input: RequestInfo | URL, init: RequestInit = {}) { | |
| return fetch(input, withAnonHeaders(init)); | |
| } | |