perf(bun): validate metadata UTF-8 without round-tripping bytes (#12254)

This commit is contained in:
ShuaixinHuang authored and GitHub committed 2026-11-19 02:41:47 -05:00
1 parent 56d31bc7a8
commit d7aee71bc7
2 files changed
+24 -2

No files matched your search

+22
View File
@@ -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" });
});
+2 -2
View File
@@ -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 = <Names extends readonly string[]>(
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") };
};