refactor(deno): reuse section UTF-8 decoders (#17363)

This commit is contained in:
Vincent Koc
2026-12-06 10:34:36 +08:00
committed by GitHub
parent a545387d46
commit a80b6b79a9
+13 -19
View File
@@ -13,7 +13,10 @@ import {
} from "../../domain/artifactReaderLimits.js";
import { walkDenoVirtualFs, type DenoVfsWalk } from "./DenoVirtualFs.js";
import { parseJsonWithDuplicatePolicy } from "../../domain/jsonDuplicateMember.js";
import { strictUtf8Decoder } from "../../domain/strictUtf8.js";
import {
decodeStrictUtf8,
decodeUtf8PreservingBomOrNull,
} from "../../domain/strictUtf8.js";
/** Opening and closing marker of a Deno compile data section. */
export const DENO_SECTION_MAGIC = Buffer.from("d3n0l4nd", "latin1");
@@ -26,8 +29,6 @@ const FLAG_TRANSPILED = 1 << 0;
const FLAG_SOURCE_MAP = 1 << 1;
const FLAG_CJS = 1 << 2;
const FLAG_UTF8 = 1 << 3;
const specifierDecoder = strictUtf8Decoder("preserve");
const jsonDecoder = strictUtf8Decoder("strip-signature");
const MEDIA_TYPE_NAMES = [
"JavaScript",
@@ -221,9 +222,9 @@ export const parseDenoSection = async (
const id = await u32();
if (specifierText.has(id))
throw denoFormatFailure(`duplicate specifier id ${String(id)}`);
const decoded = decodeStrict(bytes, specifierDecoder);
if (decoded === undefined) invalidUtf8 = true;
specifierText.set(id, decoded ?? null);
const decoded = decodeUtf8PreservingBomOrNull(bytes);
if (decoded === null) invalidUtf8 = true;
specifierText.set(id, decoded);
}
const specifiers: DenoCountedRegion = {
count: specifierCount,
@@ -393,20 +394,13 @@ const assertTableCount = (count: number, limits: DenoReaderLimits): void => {
);
};
const decodeStrict = (
bytes: Buffer,
decoder: TextDecoder,
): string | undefined => {
try {
return decoder.decode(bytes);
} catch {
return undefined;
}
};
const decodeJson = (bytes: Buffer, scope: string): unknown => {
const text = decodeStrict(bytes, jsonDecoder);
if (text === undefined) throw denoFormatFailure(`${scope} is invalid`);
let text: string;
try {
text = decodeStrictUtf8(bytes, "strip-signature");
} catch {
throw denoFormatFailure(`${scope} is invalid`);
}
const parsed = parseJsonWithDuplicatePolicy(text, "reject");
if (!parsed.ok) {
if (parsed.duplicate !== undefined)