mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
refactor(artifacts): share recognizer claim mapping (#15807)
This commit is contained in:
1 parent
b9e5a53ff9
commit
d071d240fe
1 file changed
+23
-36
@@ -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 };
|
||||
|
||||
Reference in new issue
Block a user