Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ class FetchCoinAssetsBuildStep extends BuildStep {
String? githubToken,
}) {
final config = buildConfig.coinCIConfig.copyWith(
// If the branch is `master`, use the repository mirror URL to avoid
// rate limiting issues. Consider refactoring config to allow branch
// specific mirror URLs to remove this workaround.
coinsRepoContentUrl:
buildConfig.coinCIConfig.isMainBranch
? buildConfig.coinCIConfig.coinsRepoContentUrl
: buildConfig.coinCIConfig.rawContentUrl,
// Use the effective content URL which checks CDN mirrors
coinsRepoContentUrl: buildConfig.coinCIConfig.effectiveContentUrl,
);

final provider = GithubApiProvider.withBaseUrl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CoinBuildConfig {
required this.mappedFiles,
required this.mappedFolders,
required this.concurrentDownloadsEnabled,
this.cdnBranchMirrors = const {},
});

/// Creates a new instance of [CoinBuildConfig] from a JSON object.
Expand All @@ -42,17 +43,17 @@ class CoinBuildConfig {
mappedFolders: Map<String, String>.from(
json['mapped_folders'] as Map<String, dynamic>? ?? {},
),
cdnBranchMirrors: Map<String, String>.from(
json['cdn_branch_mirrors'] as Map<String, dynamic>? ?? {},
),
);
}

bool get isMainBranch =>
coinsRepoBranch == 'master' || coinsRepoBranch == 'main';

String get rawContentUrl =>
'https://raw.githubusercontent.com/KomodoPlatform/coins/refs/heads/$coinsRepoBranch';

static const String cdnContentUrl =
'https://api.github.com/repos/KomodoPlatform/coins';
/// Gets the appropriate content URL for the current branch.
/// If a CDN mirror is configured for the branch, it uses that.
/// Otherwise, it falls back to the configured coinsRepoContentUrl.
String get effectiveContentUrl =>
cdnBranchMirrors[coinsRepoBranch] ?? coinsRepoContentUrl;

/// Indicates whether fetching updates of the coins assets are enabled.
final bool fetchAtBuildEnabled;
Expand Down Expand Up @@ -97,6 +98,12 @@ class CoinBuildConfig {
/// corresponding paths in the GitHub repository.
final Map<String, String> mappedFolders;

/// A map of branch names to CDN mirror URLs.
/// When downloading assets, if the current branch has a CDN mirror configured,
/// it will be used instead of the default content URL.
/// This helps avoid rate limiting for commonly used branches.
final Map<String, String> cdnBranchMirrors;

CoinBuildConfig copyWith({
String? bundledCoinsRepoCommit,
bool? fetchAtBuildEnabled,
Expand All @@ -108,6 +115,7 @@ class CoinBuildConfig {
bool? concurrentDownloadsEnabled,
Map<String, String>? mappedFiles,
Map<String, String>? mappedFolders,
Map<String, String>? cdnBranchMirrors,
}) {
return CoinBuildConfig(
fetchAtBuildEnabled: fetchAtBuildEnabled ?? this.fetchAtBuildEnabled,
Expand All @@ -123,6 +131,7 @@ class CoinBuildConfig {
concurrentDownloadsEnabled ?? this.concurrentDownloadsEnabled,
mappedFiles: mappedFiles ?? this.mappedFiles,
mappedFolders: mappedFolders ?? this.mappedFolders,
cdnBranchMirrors: cdnBranchMirrors ?? this.cdnBranchMirrors,
);
}

Expand All @@ -138,6 +147,7 @@ class CoinBuildConfig {
'mapped_files': mappedFiles,
'mapped_folders': mappedFolders,
'concurrent_downloads_enabled': concurrentDownloadsEnabled,
'cdn_branch_mirrors': cdnBranchMirrors,
};

/// Loads the coins runtime update configuration synchronously from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,34 @@ class GitHubFile {

/// Creates a new instance of [GitHubFile] from a JSON map.
factory GitHubFile.fromJson(Map<String, dynamic> data) => GitHubFile(
name: data['name'] as String,
path: data['path'] as String,
sha: data['sha'] as String,
size: data['size'] as int,
url: data['url'] as String?,
htmlUrl: data['html_url'] as String?,
gitUrl: data['git_url'] as String?,
downloadUrl: data['download_url'] as String,
type: data['type'] as String,
links: data['_links'] == null
name: data['name'] as String,
path: data['path'] as String,
sha: data['sha'] as String,
size: data['size'] as int,
url: data['url'] as String?,
htmlUrl: data['html_url'] as String?,
gitUrl: data['git_url'] as String?,
downloadUrl: data['download_url'] as String,
type: data['type'] as String,
links:
data['_links'] == null
? null
: Links.fromJson(data['_links'] as Map<String, dynamic>),
);
);

/// Converts the [GitHubFile] instance to a JSON map.
Map<String, dynamic> toJson() => <String, dynamic>{
'name': name,
'path': path,
'sha': sha,
'size': size,
'url': url,
'html_url': htmlUrl,
'git_url': gitUrl,
'download_url': downloadUrl,
'type': type,
'_links': links?.toJson(),
};
'name': name,
'path': path,
'sha': sha,
'size': size,
'url': url,
'html_url': htmlUrl,
'git_url': gitUrl,
'download_url': downloadUrl,
'type': type,
'_links': links?.toJson(),
};

/// The name of the file.
final String name;
Expand Down Expand Up @@ -103,15 +104,15 @@ class GitHubFile {
);
}

GitHubFile withStaticHostingUrl(String branch) {
final staticHostingUrls = {
'master': 'https://komodoplatform.github.io/coins',
};
GitHubFile withStaticHostingUrl(
String branch,
Map<String, String> cdnMirrors,
) {
// Check if a CDN mirror is configured for this branch
final cdnUrl = cdnMirrors[branch];

return copyWith(
downloadUrl: staticHostingUrls.containsKey(branch)
? '${staticHostingUrls[branch]}/$path'
: downloadUrl,
downloadUrl: cdnUrl != null ? '$cdnUrl/$path' : downloadUrl,
);
}
}
Loading