diff --git a/src/artifacts/bun/BunModuleGraph.test.ts b/src/artifacts/bun/BunModuleGraph.test.ts index 5055bd00b1..1ef8ee3a76 100644 --- a/src/artifacts/bun/BunModuleGraph.test.ts +++ b/src/artifacts/bun/BunModuleGraph.test.ts @@ -479,3 +479,25 @@ it.each([ failure: { reason: "format" }, }); }); + +it("preserves Bun metadata UTF-8 text and keeps undecodable bytes as hex", async () => { + const result = await locate( + elfAppendedBunPayload( + bunModuleGraphPayload({ + modules: [ + { name: "\uFEFF資料𐐷.js" }, + { name: Buffer.from([0xc3, 0x28]) }, + ], + compileExecArgv: Buffer.from([0xff]), + }), + ), + ); + if (result.status !== "present" || !result.graph) + throw new Error("missing graph"); + const decoded = await decodeBunModuleGraph(result.graph); + expect(decoded.modules.map(({ nameText }) => nameText)).toEqual([ + { text: "\uFEFF資料𐐷.js", hex: null }, + { text: null, hex: "c328" }, + ]); + expect(decoded.compileExecArgv).toEqual({ text: null, hex: "ff" }); +}); diff --git a/src/artifacts/bun/BunModuleGraphMembers.ts b/src/artifacts/bun/BunModuleGraphMembers.ts index 6a3565dab0..b5b56ebbad 100644 --- a/src/artifacts/bun/BunModuleGraphMembers.ts +++ b/src/artifacts/bun/BunModuleGraphMembers.ts @@ -2,6 +2,7 @@ import { DEFAULT_BUN_READER_LIMITS, type BunReaderLimits, } from "../../domain/artifactReaderLimits.js"; +import { isStrictUtf8 } from "../../domain/strictUtf8.js"; import { setImmediate } from "node:timers/promises"; import { ArtifactReaderFailure } from "../../resources/index.js"; import { @@ -315,8 +316,7 @@ const named = ( const textFact = (bytes: Buffer, emptyIsText: boolean): BunTextFact => { if (bytes.length === 0) return emptyIsText ? { text: "", hex: null } : { text: null, hex: null }; - const text = bytes.toString("utf8"); - if (Buffer.from(text, "utf8").equals(bytes)) return { text, hex: null }; + if (isStrictUtf8(bytes)) return { text: bytes.toString("utf8"), hex: null }; return { text: null, hex: bytes.toString("hex") }; };