From 865ab5819a4ba9fa3929eb97bbdc37dcff82f5ef Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 6 Dec 2026 10:35:29 +0800 Subject: [PATCH] refactor(json): reuse the shape inference UTF-8 decoder (#17360) --- src/domain/jsonShape.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/domain/jsonShape.ts b/src/domain/jsonShape.ts index 9c16a16130..050bcc9045 100644 --- a/src/domain/jsonShape.ts +++ b/src/domain/jsonShape.ts @@ -5,7 +5,7 @@ import { safeParseJson } from "./safeJson.js"; import { jsonStringByteLength } from "./jsonStringByteLength.js"; import { jsonValueByteLength } from "./jsonValueByteLength.js"; import { err, ok, type Result } from "./result.js"; -import { strictUtf8Decoder } from "./strictUtf8.js"; +import { decodeUtf8PreservingBomOrNull } from "./strictUtf8.js"; import { isRecord } from "../kernel/isRecord.js"; const jsonValueTypeSchema = z.enum([ @@ -82,15 +82,9 @@ export const inferJsonShape = ( throw new RangeError( "JSON shape byte allowance must be a non-negative safe integer", ); - let text: string; - try { - text = - typeof source === "string" - ? source - : strictUtf8Decoder("preserve").decode(source); - } catch { - return err({ kind: "invalid-json" }); - } + const text = + typeof source === "string" ? source : decodeUtf8PreservingBomOrNull(source); + if (text === null) return err({ kind: "invalid-json" }); const parsed = safeParseJson(text); if (!parsed.ok) return err({ kind: "invalid-json" }); const root = parsed.value;