Spaces:
Running
Running
Download server/http/userParam.ts from evalstate/mcp-webcam: direct link, hf CLI and curl.
- Browser
- Download file 871 Bytes
-
https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/server/http/userParam.ts
- Command line
-
hf download hf://spaces/evalstate/mcp-webcam/server/http/userParam.ts
-
curl -L -o userParam.ts https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/server/http/userParam.ts
871 Bytes
| const VALID_USER_RE = /^[A-Za-z0-9_-]{1,30}$/; | |
| export function sanitizeUser(raw: string | null | undefined): string { | |
| if (!raw) return 'default'; | |
| const trimmed = raw.trim(); | |
| if (VALID_USER_RE.test(trimmed)) { | |
| return trimmed; | |
| } | |
| const cleaned = trimmed.replace(/[^A-Za-z0-9_-]/g, '').slice(0, 30); | |
| return cleaned.length > 0 ? cleaned : 'default'; | |
| } | |
| export function resolveUser(req: Request): string { | |
| try { | |
| const url = new URL(req.url, 'http://localhost'); | |
| const queryUser = url.searchParams.get('user'); | |
| if (queryUser !== null && queryUser.trim() !== '') { | |
| return sanitizeUser(queryUser); | |
| } | |
| } catch { | |
| // ignore malformed URL and fall back to header | |
| } | |
| const headerUser = req.headers.get('x-user-id'); | |
| if (headerUser !== null && headerUser.trim() !== '') { | |
| return sanitizeUser(headerUser); | |
| } | |
| return 'default'; | |
| } | |