You've already forked agentic-coding-workflow
fix(web): localStorage safety, default fetch timeout, visibility-aware polling
This commit is contained in:
+16
-1
@@ -1,6 +1,7 @@
|
|||||||
import { ApiException, type ApiError } from './types'
|
import { ApiException, type ApiError } from './types'
|
||||||
|
|
||||||
const BASE = '' // 同源;dev 走 vite proxy
|
const BASE = '' // 同源;dev 走 vite proxy
|
||||||
|
const DEFAULT_TIMEOUT_MS = 30_000
|
||||||
|
|
||||||
interface RequestOptions {
|
interface RequestOptions {
|
||||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE'
|
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE'
|
||||||
@@ -27,11 +28,22 @@ export async function request<T>(path: string, opts: RequestOptions = {}): Promi
|
|||||||
const token = authTokenProvider()
|
const token = authTokenProvider()
|
||||||
if (token) headers['Authorization'] = `Bearer ${token}`
|
if (token) headers['Authorization'] = `Bearer ${token}`
|
||||||
|
|
||||||
|
const controller = new AbortController()
|
||||||
|
const timer = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS)
|
||||||
|
if (opts.signal) {
|
||||||
|
if (opts.signal.aborted) {
|
||||||
|
controller.abort()
|
||||||
|
} else {
|
||||||
|
opts.signal.addEventListener('abort', () => controller.abort(), { once: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const resp = await fetch(BASE + path, {
|
const resp = await fetch(BASE + path, {
|
||||||
method: opts.method || 'GET',
|
method: opts.method || 'GET',
|
||||||
headers,
|
headers,
|
||||||
body: opts.body !== undefined ? JSON.stringify(opts.body) : undefined,
|
body: opts.body !== undefined ? JSON.stringify(opts.body) : undefined,
|
||||||
signal: opts.signal,
|
signal: controller.signal,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (resp.status === 401) {
|
if (resp.status === 401) {
|
||||||
@@ -52,4 +64,7 @@ export async function request<T>(path: string, opts: RequestOptions = {}): Promi
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
return data as T
|
return data as T
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timer)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,13 +18,35 @@ export function useUnreadPoller() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
function start() {
|
||||||
|
if (timer !== null) return
|
||||||
refresh()
|
refresh()
|
||||||
timer = setInterval(refresh, POLL_INTERVAL_MS)
|
timer = setInterval(refresh, POLL_INTERVAL_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
function stop() {
|
||||||
|
if (timer !== null) {
|
||||||
|
clearInterval(timer)
|
||||||
|
timer = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onVisibilityChange() {
|
||||||
|
if (document.hidden) {
|
||||||
|
stop()
|
||||||
|
} else {
|
||||||
|
start()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
start()
|
||||||
|
document.addEventListener('visibilitychange', onVisibilityChange)
|
||||||
})
|
})
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
if (timer) clearInterval(timer)
|
document.removeEventListener('visibilitychange', onVisibilityChange)
|
||||||
|
stop()
|
||||||
})
|
})
|
||||||
|
|
||||||
return { refresh, error }
|
return { refresh, error }
|
||||||
|
|||||||
+46
-8
@@ -8,26 +8,64 @@ export interface User {
|
|||||||
is_admin: boolean
|
is_admin: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function safeReadJSON<T>(key: string, fallback: T): T {
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem(key)
|
||||||
|
if (raw === null) return fallback
|
||||||
|
return JSON.parse(raw) as T
|
||||||
|
} catch {
|
||||||
|
// 脏数据 / 反序列化失败 → 清掉,回退默认。
|
||||||
|
try {
|
||||||
|
localStorage.removeItem(key)
|
||||||
|
} catch {
|
||||||
|
/* noop */
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeReadString(key: string): string | null {
|
||||||
|
try {
|
||||||
|
return localStorage.getItem(key)
|
||||||
|
} catch {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeWrite(key: string, value: string) {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(key, value)
|
||||||
|
} catch {
|
||||||
|
/* QuotaExceeded / disabled */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeRemove(key: string) {
|
||||||
|
try {
|
||||||
|
localStorage.removeItem(key)
|
||||||
|
} catch {
|
||||||
|
/* noop */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const useAuthStore = defineStore('auth', () => {
|
export const useAuthStore = defineStore('auth', () => {
|
||||||
const token = ref<string | null>(localStorage.getItem('token'))
|
const token = ref<string | null>(safeReadString('token'))
|
||||||
const user = ref<User | null>(
|
const user = ref<User | null>(safeReadJSON<User | null>('user', null))
|
||||||
JSON.parse(localStorage.getItem('user') || 'null') as User | null,
|
|
||||||
)
|
|
||||||
|
|
||||||
const isAuthenticated = computed(() => !!token.value)
|
const isAuthenticated = computed(() => !!token.value)
|
||||||
|
|
||||||
function setSession(t: string, u: User) {
|
function setSession(t: string, u: User) {
|
||||||
token.value = t
|
token.value = t
|
||||||
user.value = u
|
user.value = u
|
||||||
localStorage.setItem('token', t)
|
safeWrite('token', t)
|
||||||
localStorage.setItem('user', JSON.stringify(u))
|
safeWrite('user', JSON.stringify(u))
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearSession() {
|
function clearSession() {
|
||||||
token.value = null
|
token.value = null
|
||||||
user.value = null
|
user.value = null
|
||||||
localStorage.removeItem('token')
|
safeRemove('token')
|
||||||
localStorage.removeItem('user')
|
safeRemove('user')
|
||||||
}
|
}
|
||||||
|
|
||||||
return { token, user, isAuthenticated, setSession, clearSession }
|
return { token, user, isAuthenticated, setSession, clearSession }
|
||||||
|
|||||||
+30
-13
@@ -3,15 +3,34 @@ import { computed, ref, watch } from 'vue'
|
|||||||
|
|
||||||
type ThemeMode = 'light' | 'dark' | 'system'
|
type ThemeMode = 'light' | 'dark' | 'system'
|
||||||
|
|
||||||
export const useUIStore = defineStore('ui', () => {
|
function safeReadString(key: string): string | null {
|
||||||
const themeMode = ref<ThemeMode>(
|
try {
|
||||||
(localStorage.getItem('themeMode') as ThemeMode) || 'system',
|
return localStorage.getItem(key)
|
||||||
)
|
} catch {
|
||||||
const systemDark = ref(window.matchMedia('(prefers-color-scheme: dark)').matches)
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
window
|
function safeWrite(key: string, value: string) {
|
||||||
.matchMedia('(prefers-color-scheme: dark)')
|
try {
|
||||||
.addEventListener('change', (e) => (systemDark.value = e.matches))
|
localStorage.setItem(key, value)
|
||||||
|
} catch {
|
||||||
|
/* QuotaExceeded / disabled */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useUIStore = defineStore('ui', () => {
|
||||||
|
const storedTheme = safeReadString('themeMode') as ThemeMode | null
|
||||||
|
const themeMode = ref<ThemeMode>(storedTheme || 'system')
|
||||||
|
|
||||||
|
const mediaQuery =
|
||||||
|
typeof window !== 'undefined' && typeof window.matchMedia === 'function'
|
||||||
|
? window.matchMedia('(prefers-color-scheme: dark)')
|
||||||
|
: null
|
||||||
|
const systemDark = ref(mediaQuery ? mediaQuery.matches : false)
|
||||||
|
if (mediaQuery) {
|
||||||
|
mediaQuery.addEventListener('change', (e) => (systemDark.value = e.matches))
|
||||||
|
}
|
||||||
|
|
||||||
const effectiveDark = computed(() =>
|
const effectiveDark = computed(() =>
|
||||||
themeMode.value === 'system' ? systemDark.value : themeMode.value === 'dark',
|
themeMode.value === 'system' ? systemDark.value : themeMode.value === 'dark',
|
||||||
@@ -25,13 +44,11 @@ export const useUIStore = defineStore('ui', () => {
|
|||||||
|
|
||||||
function setThemeMode(mode: ThemeMode) {
|
function setThemeMode(mode: ThemeMode) {
|
||||||
themeMode.value = mode
|
themeMode.value = mode
|
||||||
localStorage.setItem('themeMode', mode)
|
safeWrite('themeMode', mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
const sidebarCollapsed = ref(localStorage.getItem('sidebarCollapsed') === 'true')
|
const sidebarCollapsed = ref(safeReadString('sidebarCollapsed') === 'true')
|
||||||
watch(sidebarCollapsed, (v) =>
|
watch(sidebarCollapsed, (v) => safeWrite('sidebarCollapsed', String(v)))
|
||||||
localStorage.setItem('sidebarCollapsed', String(v)),
|
|
||||||
)
|
|
||||||
|
|
||||||
return { themeMode, effectiveDark, setThemeMode, sidebarCollapsed }
|
return { themeMode, effectiveDark, setThemeMode, sidebarCollapsed }
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -58,4 +58,79 @@ describe('api client', () => {
|
|||||||
expect(ex.body.code).toBe('invalid_input')
|
expect(ex.body.code).toBe('invalid_input')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('schedules a default 30s timeout and forwards abort to fetch', async () => {
|
||||||
|
// Replace setTimeout with a stub that captures the scheduled callback,
|
||||||
|
// so we can assert the default-timeout scheduling and trigger it manually
|
||||||
|
// without waiting wall-clock 30 seconds.
|
||||||
|
const captured: Array<{ cb: () => void; ms: number }> = []
|
||||||
|
const realSetTimeout = globalThis.setTimeout
|
||||||
|
vi.stubGlobal('setTimeout', ((cb: () => void, ms?: number, ...args: unknown[]) => {
|
||||||
|
if (ms === 30_000) {
|
||||||
|
captured.push({ cb, ms })
|
||||||
|
return 0 as unknown as ReturnType<typeof setTimeout>
|
||||||
|
}
|
||||||
|
return realSetTimeout(cb, ms, ...args)
|
||||||
|
}) as typeof setTimeout)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(
|
||||||
|
(_input, init) =>
|
||||||
|
new Promise((_resolve, reject) => {
|
||||||
|
const sig = init?.signal as AbortSignal | undefined
|
||||||
|
sig?.addEventListener('abort', () => {
|
||||||
|
const err = new Error('aborted')
|
||||||
|
err.name = 'AbortError'
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
let rejected: unknown = null
|
||||||
|
const promise = request('/api/test').catch((e) => {
|
||||||
|
rejected = e
|
||||||
|
})
|
||||||
|
|
||||||
|
// Allow the request to register its setTimeout + start the fetch.
|
||||||
|
await Promise.resolve()
|
||||||
|
|
||||||
|
expect(captured.length).toBe(1)
|
||||||
|
expect(captured[0].ms).toBe(30_000)
|
||||||
|
|
||||||
|
// Fire the captured timeout callback to simulate elapsed 30s.
|
||||||
|
captured[0].cb()
|
||||||
|
await promise
|
||||||
|
|
||||||
|
expect(rejected).not.toBeNull()
|
||||||
|
const init = fetchMock.mock.calls[0][1] as RequestInit
|
||||||
|
expect((init.signal as AbortSignal).aborted).toBe(true)
|
||||||
|
} finally {
|
||||||
|
vi.unstubAllGlobals()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('forwards a caller-supplied signal abort to the fetch', async () => {
|
||||||
|
const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(
|
||||||
|
(_input, init) =>
|
||||||
|
new Promise((_resolve, reject) => {
|
||||||
|
const sig = init?.signal as AbortSignal | undefined
|
||||||
|
if (sig) {
|
||||||
|
sig.addEventListener('abort', () => {
|
||||||
|
const err = new Error('aborted')
|
||||||
|
err.name = 'AbortError'
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
const ctl = new AbortController()
|
||||||
|
const promise = request('/api/test', { signal: ctl.signal })
|
||||||
|
const settled = expect(promise).rejects.toThrow()
|
||||||
|
ctl.abort()
|
||||||
|
await settled
|
||||||
|
|
||||||
|
const init = fetchMock.mock.calls[0][1] as RequestInit
|
||||||
|
expect((init.signal as AbortSignal).aborted).toBe(true)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user