mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Fix SSE duplicate Transfer-Encoding headers
Replace Hono's streamSSE with manual stream implementation to avoid sending duplicate Transfer-Encoding: chunked headers which cause nginx/Cloudflare to reject responses with 502 errors. Manually formats SSE messages with proper event/data structure and sets Content-Type: text/event-stream headers.
This commit is contained in:
1 parent
5d5c4d140a
commit
1f868f46ca
1 file changed
+11
-21
+11
-21
@@ -8,10 +8,6 @@ export function createSSERoutes() {
|
||||
const app = new Hono()
|
||||
|
||||
app.get('/stream', async (c) => {
|
||||
c.header('Cache-Control', 'no-cache, no-store, no-transform')
|
||||
c.header('Connection', 'keep-alive')
|
||||
c.header('X-Accel-Buffering', 'no')
|
||||
|
||||
const directoriesParam = c.req.query('directories')
|
||||
const directories = directoriesParam ? directoriesParam.split(',').filter(Boolean) : []
|
||||
const clientId = `client_${Date.now()}_${Math.random().toString(36).slice(2)}`
|
||||
@@ -90,34 +86,28 @@ export function createSSERoutes() {
|
||||
})
|
||||
|
||||
app.get('/test-stream', async (c) => {
|
||||
c.header('Content-Type', 'text/event-stream')
|
||||
c.header('Cache-Control', 'no-cache, no-store, no-transform')
|
||||
c.header('Connection', 'keep-alive')
|
||||
c.header('X-Accel-Buffering', 'no')
|
||||
return stream(c, async (writer) => {
|
||||
const writeSSE = (event: string, data: string) => {
|
||||
const lines = []
|
||||
if (event) lines.push(`event: ${event}`)
|
||||
lines.push(`data: ${data}`)
|
||||
lines.push('')
|
||||
lines.push('')
|
||||
writer.write(new TextEncoder().encode(lines.join('\n')))
|
||||
}
|
||||
|
||||
return streamSSE(c, async (stream) => {
|
||||
await stream.writeSSE({
|
||||
event: 'test',
|
||||
data: JSON.stringify({ message: 'SSE working', timestamp: Date.now() })
|
||||
})
|
||||
writeSSE('test', JSON.stringify({ message: 'SSE working', timestamp: Date.now() }))
|
||||
|
||||
let count = 0
|
||||
const interval = setInterval(async () => {
|
||||
count++
|
||||
try {
|
||||
await stream.writeSSE({
|
||||
event: 'ping',
|
||||
data: JSON.stringify({ count, timestamp: Date.now() })
|
||||
})
|
||||
writeSSE('ping', JSON.stringify({ count, timestamp: Date.now() }))
|
||||
} catch {
|
||||
clearInterval(interval)
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
stream.onAbort(() => {
|
||||
clearInterval(interval)
|
||||
})
|
||||
|
||||
await new Promise(() => {})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user