fix: prevent double response with responded guard in spawn handlers
This commit is contained in:
@@ -67,15 +67,20 @@ function apiSavePlugin() {
|
||||
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
let responded = false
|
||||
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
|
||||
child.stderr.on('data', (d: Buffer) => stderr += d.toString())
|
||||
|
||||
child.on('error', (err) => {
|
||||
child.on('error', () => {
|
||||
if (responded) return
|
||||
responded = true
|
||||
res.writeHead(503)
|
||||
res.end(JSON.stringify({ error: 'opencode 未安装,请运行 npm install' }))
|
||||
})
|
||||
|
||||
child.on('close', async (code) => {
|
||||
if (responded) return
|
||||
responded = true
|
||||
if (code !== 0) {
|
||||
res.writeHead(500)
|
||||
res.end(JSON.stringify({ error: 'opencode exited with code ' + code, stderr }))
|
||||
@@ -131,12 +136,17 @@ function apiSavePlugin() {
|
||||
try {
|
||||
const child = spawn('npx', ['opencode', 'session', 'list', '--format', 'json'], { timeout: 5000 })
|
||||
let stdout = ''
|
||||
let responded = false
|
||||
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
|
||||
child.on('error', () => {
|
||||
if (responded) return
|
||||
responded = true
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end('[]')
|
||||
})
|
||||
child.on('close', () => {
|
||||
if (responded) return
|
||||
responded = true
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(stdout || '[]')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user