feat(lib): only download asset when it is of a valid format

Small optimization to not fetch an asset if its need of a supported
compression format.
This commit is contained in:
Erica Marigold 2024-12-13 13:51:38 +00:00
parent f003ce8895
commit 45627ea4a9
Signed by: DevComp
GPG key ID: 429EF1C337871656
2 changed files with 15 additions and 11 deletions

View file

@ -46,17 +46,15 @@ local function downloadAndDecompress(asset: {
size: number,
content_type: string,
}): Option<pathfs.Path>
-- TODO: Small optimization by first detecting that its a valid format we support
-- before downloading the file
local contentsResp = net.request(asset.browser_download_url)
if not contentsResp.ok then
return error(`Failed to download asset {asset.name}: HTTP Code {contentsResp.statusCode}`)
end
return compression
.detectFormat(asset.name)
:map(function(format: compression.CompressionFormat)
return compression.decompress[format](buffer.fromstring(contentsResp.body)):unwrap() :: pathfs.Path
:andThen(function(format: compression.CompressionFormat)
local contentsResp = net.request(asset.browser_download_url)
if not contentsResp.ok then
return error(`Failed to download asset {asset.name}: HTTP Code {contentsResp.statusCode}`)
end
return compression.decompress[format](buffer.fromstring(contentsResp.body)):ok() :: Option<pathfs.Path>
end) :: Option<pathfs.Path>
end

View file

@ -7,14 +7,20 @@ local Option = {}
local Result = {}
export type Option<T> = OptionImpl.Option<T> & typeof(Option)
export type Result<T, E> = ResultImpl.Result<T, E>
export type Result<T, E> = ResultImpl.Result<T, E> & typeof(Result)
function Option.okOr<T, E>(self: Option<T>, err: E): Result<T, E>
return self:mapOrElse(function()
return ResultImpl.Err(err)
end, function(val)
return ResultImpl.Ok(val)
end)
end) :: Result<T, E>
end
function Result.ok<T, E>(self: Result<T, E>): Option<T>
return self:mapOr(OptionImpl.None, function(val: T)
return OptionImpl.Some(val)
end) :: Option<T>
end
return {