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