MCPcopy Index your code
hub / github.com/HisMax/RedInk / readSseResponse

Function readSseResponse

frontend/src/api/client.ts:45–87  ·  view source on GitHub ↗
(
  response: Response,
  handlers: Record<string, (data: any) => void>
)

Source from the content-addressed store, hash-verified

43}
44
45export async function readSseResponse(
46 response: Response,
47 handlers: Record<string, (data: any) => void>
48) {
49 const reader = response.body?.getReader()
50 if (!reader) {
51 throw new Error('无法读取响应流')
52 }
53
54 const decoder = new TextDecoder()
55 let buffer = ''
56
57 try {
58 while (true) {
59 const { done, value } = await reader.read()
60
61 if (done) break
62
63 buffer += decoder.decode(value, { stream: true })
64 const lines = buffer.split('\n\n')
65 buffer = lines.pop() || ''
66
67 for (const line of lines) {
68 if (!line.trim()) continue
69
70 const [eventLine, dataLine] = line.split('\n')
71 if (!eventLine || !dataLine) continue
72
73 const eventType = eventLine.replace('event: ', '').trim()
74 const eventData = dataLine.replace('data: ', '').trim()
75
76 try {
77 const data = JSON.parse(eventData)
78 handlers[eventType]?.(data)
79 } catch (e) {
80 console.error('解析 SSE 数据失败:', e)
81 }
82 }
83 }
84 } finally {
85 reader.releaseLock()
86 }
87}

Callers 2

retryFailedImagesFunction · 0.90
generateImagesPostFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected