mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-24 23:06:51 +00:00
fix: 修复 elevated helper 输出解析,支持同行多 JSON 拼接的情况
This commit is contained in:
@@ -382,21 +382,25 @@ export class KeyServiceMac {
|
|||||||
const lines = String(stdout).split(/\r?\n/).map(x => x.trim()).filter(Boolean)
|
const lines = String(stdout).split(/\r?\n/).map(x => x.trim()).filter(Boolean)
|
||||||
if (!lines.length) throw new Error('elevated helper returned empty output')
|
if (!lines.length) throw new Error('elevated helper returned empty output')
|
||||||
|
|
||||||
// 找最后一个能成功 parse 的 JSON 行(stderr 混入 stdout 时会有非 JSON 行)
|
// 从所有行里提取所有 JSON 对象(同一行可能有多个拼接),找含 key/result 的那个
|
||||||
let payload: any
|
const extractJsonObjects = (s: string): any[] => {
|
||||||
let lastJsonLine = ''
|
const results: any[] = []
|
||||||
for (let i = lines.length - 1; i >= 0; i--) {
|
const re = /\{[^{}]*\}/g
|
||||||
if (!lines[i].startsWith('{')) continue
|
let m: RegExpExecArray | null
|
||||||
try {
|
while ((m = re.exec(s)) !== null) {
|
||||||
payload = JSON.parse(lines[i])
|
try { results.push(JSON.parse(m[0])) } catch { }
|
||||||
lastJsonLine = lines[i]
|
|
||||||
break
|
|
||||||
} catch { continue }
|
|
||||||
}
|
}
|
||||||
if (!payload) throw new Error('elevated helper returned invalid json: ' + lines[lines.length - 1])
|
return results
|
||||||
if (payload?.success === true && typeof payload?.key === 'string') return payload.key
|
}
|
||||||
if (typeof payload?.result === 'string') return payload.result
|
const fullOutput = lines.join('\n')
|
||||||
throw new Error('elevated helper json missing key/result')
|
const allJson = extractJsonObjects(fullOutput)
|
||||||
|
// 优先找 success=true && key 字段
|
||||||
|
const successPayload = allJson.find(p => p?.success === true && typeof p?.key === 'string')
|
||||||
|
if (successPayload) return successPayload.key
|
||||||
|
// 其次找 result 字段
|
||||||
|
const resultPayload = allJson.find(p => typeof p?.result === 'string')
|
||||||
|
if (resultPayload) return resultPayload.result
|
||||||
|
throw new Error('elevated helper returned invalid json: ' + lines[lines.length - 1])
|
||||||
}
|
}
|
||||||
|
|
||||||
private mapDbKeyErrorMessage(code?: string, detail?: string): string {
|
private mapDbKeyErrorMessage(code?: string, detail?: string): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user