refactor(lib): make checks explicit and add comments to zip decompressor

This commit is contained in:
Erica Marigold 2025-01-16 07:24:09 +00:00
parent 863d1ce95d
commit 324a4089b3
Signed by: DevComp
GPG key ID: 429EF1C337871656

View file

@ -42,40 +42,50 @@ local extractBinary: {
} = } =
{ {
Zip = function(compressed, binaryName, targetPlatform) Zip = function(compressed, binaryName, targetPlatform)
-- Load the compressed data into a `ZipReader`
local reader = unzip.load(compressed) local reader = unzip.load(compressed)
local binaryContents: buffer? local binaryContents: buffer?
local function extractEntry(entry: unzip.ZipEntry) --- Extracts a specific entry from the ZIP and validates that its `PlatformDescriptor`
--- matches the expected one
local function extractEntry(entry: unzip.ZipEntry): buffer?
local contents = reader:extract(entry, { type = "binary" }) :: buffer local contents = reader:extract(entry, { type = "binary" }) :: buffer
local executablePlatform = PlatformDescriptor.fromExecutable(contents) local executablePlatform = PlatformDescriptor.fromExecutable(contents)
if executablePlatform:isOk() and eq(executablePlatform:unwrap(), targetPlatform) then if executablePlatform:isOk() and eq(executablePlatform:unwrap(), targetPlatform) then
binaryContents = contents return contents
end end
end end
-- Find the entry and attempt to extract it
local binaryEntry = reader:findEntry(binaryName) local binaryEntry = reader:findEntry(binaryName)
if binaryEntry then if binaryEntry then
extractEntry(binaryEntry) binaryContents = extractEntry(binaryEntry)
end end
if not binaryContents then -- Fallback for if we cannot find the `ZipEntry`
if binaryContents == nil then
-- Walk through the entries to find an executable -- Walk through the entries to find an executable
reader:walk(function(entry) reader:walk(function(entry)
if binaryContents then if binaryContents ~= nil then
-- If we successfully extracted the binary on a previous iteration,
-- we skip this entry
return return
end end
if entry.isDirectory then if entry.isDirectory then
-- Ignore directories
return return
end end
extractEntry(entry) binaryContents = extractEntry(entry)
end) end)
end end
if not binaryContents then if binaryContents == nil then
-- If both the fallback and the initial attempts did not yield an extracted binary,
-- we return an error
return Result.Err("ExtractBinaryError::BinaryNotFound" :: string) return Result.Err("ExtractBinaryError::BinaryNotFound" :: string)
end end