export interface TimerRefHandle { current: ReturnType | null } /** * Supabase Edge Function URL을 검증·생성한다. * HTTPS 또는 로컬 loopback HTTP만 허용하고, credentials 포함 URL을 거부한다. */ export function resolveSupabaseFunctionUrl( functionName: string, rawBaseUrl: string | undefined = process.env.NEXT_PUBLIC_SUPABASE_URL, ): string { const baseUrl = rawBaseUrl?.trim() if (!baseUrl) { throw new Error('서버 설정이 완료되지 않았습니다.') } const normalizedName = functionName.trim().replace(/^\/+/, '') if (!normalizedName || normalizedName.includes('/') || normalizedName.includes('..')) { throw new Error('잘못된 함수 경로입니다.') } try { const url = new URL(`/functions/v1/${encodeURIComponent(normalizedName)}`, baseUrl) const isLocalHttp = url.protocol === 'http:' && (url.hostname === 'localhost' || url.hostname === '127.0.0.1') if ((url.protocol !== 'https:' && !isLocalHttp) || url.username || url.password) { throw new Error('invalid_origin') } return url.toString() } catch { throw new Error('서버 주소 설정이 올바르지 않습니다.') } } export interface TimedAbortHandle { controller: AbortController didTimeout: () => boolean dispose: () => void } /** * 타임아웃으로 자동 중단되며, 완료/언마운트 시 타이머와 참조를 정리할 수 있는 AbortController 핸들. */ export function createTimedAbortSignal( timeoutMs: number, activeRef?: { current: AbortController | null }, ): TimedAbortHandle { const controller = new AbortController() let timedOut = false if (activeRef) { activeRef.current?.abort() activeRef.current = controller } const timer = globalThis.setTimeout(() => { timedOut = true controller.abort() }, timeoutMs) return { controller, didTimeout: () => timedOut, dispose: () => { globalThis.clearTimeout(timer) if (activeRef && activeRef.current === controller) { activeRef.current = null } }, } } export function clearTrackedTimeout(timerRef: TimerRefHandle): void { if (timerRef.current !== null) { globalThis.clearTimeout(timerRef.current) timerRef.current = null } } export function scheduleTrackedTimeout( timerRef: TimerRefHandle, callback: () => void, delayMs: number, ): void { clearTrackedTimeout(timerRef) timerRef.current = globalThis.setTimeout(() => { timerRef.current = null callback() }, delayMs) }