From 45627ea4a988de65810e81427e4610cebb2e530c Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Fri, 13 Dec 2024 13:51:38 +0000 Subject: [PATCH] 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. --- toolchainlib/src/init.luau | 16 +++++++--------- toolchainlib/src/utils/result_option_conv.luau | 10 ++++++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/toolchainlib/src/init.luau b/toolchainlib/src/init.luau index e257ed0..58ec2c0 100644 --- a/toolchainlib/src/init.luau +++ b/toolchainlib/src/init.luau @@ -46,17 +46,15 @@ local function downloadAndDecompress(asset: { size: number, content_type: string, }): Option - -- 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 end) :: Option end diff --git a/toolchainlib/src/utils/result_option_conv.luau b/toolchainlib/src/utils/result_option_conv.luau index 96500a7..915d8d4 100644 --- a/toolchainlib/src/utils/result_option_conv.luau +++ b/toolchainlib/src/utils/result_option_conv.luau @@ -7,14 +7,20 @@ local Option = {} local Result = {} export type Option = OptionImpl.Option & typeof(Option) -export type Result = ResultImpl.Result +export type Result = ResultImpl.Result & typeof(Result) function Option.okOr(self: Option, err: E): Result return self:mapOrElse(function() return ResultImpl.Err(err) end, function(val) return ResultImpl.Ok(val) - end) + end) :: Result +end + +function Result.ok(self: Result): Option + return self:mapOr(OptionImpl.None, function(val: T) + return OptionImpl.Some(val) + end) :: Option end return {