From e062422931b45d98d5680c5d0685d3314bbb6566 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 18 Jun 2026 03:59:20 +0000 Subject: [PATCH] fix: parse first web probe report object --- scripts/src/hwlab-node.ts | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/scripts/src/hwlab-node.ts b/scripts/src/hwlab-node.ts index d2f75fbe..9c9c575c 100644 --- a/scripts/src/hwlab-node.ts +++ b/scripts/src/hwlab-node.ts @@ -9884,11 +9884,10 @@ function parseJsonObject(text: string): Record | null { const parsed = JSON.parse(trimmed) as unknown; return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed as Record : null; } catch { - const start = trimmed.indexOf("{"); - const end = trimmed.lastIndexOf("}"); - if (start >= 0 && end > start) { + const objectText = firstJsonObjectText(trimmed); + if (objectText) { try { - const parsed = JSON.parse(trimmed.slice(start, end + 1)) as unknown; + const parsed = JSON.parse(objectText) as unknown; return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed as Record : null; } catch {} } @@ -9896,6 +9895,37 @@ function parseJsonObject(text: string): Record | null { } } +function firstJsonObjectText(text: string): string | null { + const start = text.indexOf("{"); + if (start < 0) return null; + let depth = 0; + let inString = false; + let escaped = false; + for (let index = start; index < text.length; index += 1) { + const char = text[index]; + if (inString) { + if (escaped) { + escaped = false; + } else if (char === "\\") { + escaped = true; + } else if (char === "\"") { + inString = false; + } + continue; + } + if (char === "\"") { + inString = true; + continue; + } + if (char === "{") depth += 1; + else if (char === "}") { + depth -= 1; + if (depth === 0) return text.slice(start, index + 1); + } + } + return null; +} + function compactWebProbeResult(report: Record | null): Record | null { if (report === null) return null; const dom = record(report.dom);