refactor(artifacts): share recognizer claim mapping (#15807)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-02 06:28:49 +00:00
1 parent b9e5a53ff9
commit d071d240fe
1 file changed
+23 -36
+23 -36
View File
@@ -194,6 +194,25 @@ const when =
({ prefix }: ArtifactRecognitionInput): Recognition | undefined =>
claims(prefix) ? { state: "recognized", format } : undefined;
const whenClaimed =
(
format: RecognizedArtifactFormat,
recognize: (
prefix: Buffer,
fileSize: number,
) =>
| { state: "recognized" }
| { state: "refused"; reason: string }
| undefined,
): ArtifactFormatRecognizer =>
({ prefix, fileSize }) => {
const claim = recognize(prefix, fileSize);
if (claim === undefined) return undefined;
return claim.state === "recognized"
? { state: "recognized", format }
: { state: "refused", format, reason: claim.reason };
};
/**
* The Mega Drive layout this prefix shows. It yields to ELF, Mach-O, DOS MZ
* and FAT, so a native header is not claimed because its bytes also match.
@@ -225,13 +244,7 @@ export const ARTIFACT_FORMAT_RECOGNIZERS: readonly ArtifactFormatRecognizer[] =
? { state: "recognized", format: "ines" }
: { state: "refused", format: "ines", reason: layout.error.detail };
},
({ prefix, fileSize }) => {
const claim = n64RomRecognition(prefix, fileSize);
if (claim === undefined) return undefined;
return claim.state === "recognized"
? { state: "recognized", format: "n64-rom" }
: { state: "refused", format: "n64-rom", reason: claim.reason };
},
whenClaimed("n64-rom", n64RomRecognition),
when("zip", hasZipSignature),
when("godot-pck", hasGodotPckHeader),
when("unityfs", hasUnityFsHeader),
@@ -263,35 +276,9 @@ export const ARTIFACT_FORMAT_RECOGNIZERS: readonly ArtifactFormatRecognizer[] =
when("android-boot-image", hasAndroidBootImageMagic),
when("android-dtbo", hasAndroidDtboMagic),
when("android-vbmeta", hasAndroidVbmetaMagic),
({ prefix }) => {
const ota = recognizeAndroidOtaPayload(prefix);
if (ota === undefined) return undefined;
return ota.state === "recognized"
? { state: "recognized", format: "android-ota-payload" }
: {
state: "refused",
format: "android-ota-payload",
reason: ota.reason,
};
},
({ prefix, fileSize }) => {
const ubi = recognizeUbiImage(prefix, fileSize);
if (ubi === undefined) return undefined;
return ubi.state === "recognized"
? { state: "recognized", format: "ubi-image" }
: { state: "refused", format: "ubi-image", reason: ubi.reason };
},
({ prefix }) => {
const sparse = recognizeAndroidSparseImage(prefix);
if (sparse === undefined) return undefined;
return sparse.state === "recognized"
? { state: "recognized", format: "android-sparse-image" }
: {
state: "refused",
format: "android-sparse-image",
reason: sparse.reason,
};
},
whenClaimed("android-ota-payload", recognizeAndroidOtaPayload),
whenClaimed("ubi-image", recognizeUbiImage),
whenClaimed("android-sparse-image", recognizeAndroidSparseImage),
({ prefix }) => {
const format = detectHexRecordEncoding(prefix);
return format === undefined ? undefined : { state: "recognized", format };