From 0a23ed6ef42a17700c4e60e079a57652da48ec8d Mon Sep 17 00:00:00 2001 From: hicccc77 <98377878+hicccc77@users.noreply.github.com> Date: Sat, 14 Mar 2026 21:09:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20elevated=20helper?= =?UTF-8?q?=20=E8=BE=93=E5=87=BA=E8=A7=A3=E6=9E=90=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=90=8C=E8=A1=8C=E5=A4=9A=20JSON=20=E6=8B=BC?= =?UTF-8?q?=E6=8E=A5=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/services/keyServiceMac.ts | 32 +++++++++++++++++------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/electron/services/keyServiceMac.ts b/electron/services/keyServiceMac.ts index f215870..5f1e34f 100644 --- a/electron/services/keyServiceMac.ts +++ b/electron/services/keyServiceMac.ts @@ -382,21 +382,25 @@ export class KeyServiceMac { const lines = String(stdout).split(/\r?\n/).map(x => x.trim()).filter(Boolean) if (!lines.length) throw new Error('elevated helper returned empty output') - // 找最后一个能成功 parse 的 JSON 行(stderr 混入 stdout 时会有非 JSON 行) - let payload: any - let lastJsonLine = '' - for (let i = lines.length - 1; i >= 0; i--) { - if (!lines[i].startsWith('{')) continue - try { - payload = JSON.parse(lines[i]) - lastJsonLine = lines[i] - break - } catch { continue } + // 从所有行里提取所有 JSON 对象(同一行可能有多个拼接),找含 key/result 的那个 + const extractJsonObjects = (s: string): any[] => { + const results: any[] = [] + const re = /\{[^{}]*\}/g + let m: RegExpExecArray | null + while ((m = re.exec(s)) !== null) { + try { results.push(JSON.parse(m[0])) } catch { } + } + return results } - if (!payload) throw new Error('elevated helper returned invalid json: ' + lines[lines.length - 1]) - if (payload?.success === true && typeof payload?.key === 'string') return payload.key - if (typeof payload?.result === 'string') return payload.result - throw new Error('elevated helper json missing key/result') + const fullOutput = lines.join('\n') + 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 {