feat: support configurable hosted upstream#93
Open
jiechic wants to merge 1 commit into
Open
Conversation
ricardoboss
requested changes
Jun 10, 2026
ricardoboss
left a comment
Owner
There was a problem hiding this comment.
Hi! Thanks for opening a PR.
This is a great suggestion and I want to include it. For options and option validation, we should use .NETs options pattern (linked below).
Comment on lines
+31
to
+57
| const string defaultHostedUpstreamBaseUrl = "https://pub.dev/api/"; | ||
|
|
||
| Uri ResolveHostedUpstreamBaseAddress(IServiceProvider services) | ||
| { | ||
| var configuration = services.GetRequiredService<IConfiguration>(); | ||
| var logger = services.GetRequiredService<ILoggerFactory>().CreateLogger("HostedUpstream"); | ||
| var configuredBaseUrl = configuration["HostedUpstream:BaseUrl"]; | ||
|
|
||
| if (string.IsNullOrWhiteSpace(configuredBaseUrl)) | ||
| return new(defaultHostedUpstreamBaseUrl); | ||
|
|
||
| if (!Uri.TryCreate(configuredBaseUrl, UriKind.Absolute, out var configuredUri) || | ||
| (configuredUri.Scheme != Uri.UriSchemeHttp && configuredUri.Scheme != Uri.UriSchemeHttps)) | ||
| { | ||
| logger.LogWarning( | ||
| "Invalid HostedUpstream:BaseUrl value \"{HostedUpstreamBaseUrl}\". Falling back to default {DefaultHostedUpstreamBaseUrl}", | ||
| configuredBaseUrl, defaultHostedUpstreamBaseUrl); | ||
|
|
||
| return new(defaultHostedUpstreamBaseUrl); | ||
| } | ||
|
|
||
| var normalizedBaseUrl = configuredUri.ToString(); | ||
| if (!normalizedBaseUrl.EndsWith('/')) | ||
| normalizedBaseUrl += "/"; | ||
|
|
||
| return new(normalizedBaseUrl); | ||
| } |
Owner
There was a problem hiding this comment.
This kind of validation should happen using the options pattern (https://learn.microsoft.com/en-us/dotnet/core/extensions/options) + options validation.
Comment on lines
+44
to
+46
| "HostedUpstream": { | ||
| "BaseUrl": "https://pub.dev/api/" | ||
| }, |
Owner
There was a problem hiding this comment.
This is the default anyway, no need to put it in here too.
Comment on lines
+15
to
+17
| "HostedUpstream": { | ||
| "BaseUrl": "https://pub.dev/api/" | ||
| }, |
Owner
There was a problem hiding this comment.
This is the default, no need to include it here.
Comment on lines
+16
to
+18
| "HostedUpstream": { | ||
| "BaseUrl": "https://pub.dev/api/" | ||
| }, |
Owner
There was a problem hiding this comment.
Default value; don't include
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes PubNet's hosted upstream configurable instead of hardcoding
https://pub.dev/api/.It adds support for configuring the upstream through application configuration or environment variables, while keeping
https://pub.dev/api/as the fallback default.Why
Today, PubNet can fall back to
pub.devfor package metadata and archive downloads, but the upstream is fixed in code.Making the upstream configurable improves deployment flexibility in cases such as:
unpubdeployment during a migration to PubNetpub.devis slow or unreliableThis makes it easier to use PubNet as the main entry point while still relying on another hosted repository for packages that are not stored
locally.
Changes
HostedUpstream:BaseUrlHostedUpstream__BaseUrlhttps://pub.dev/api/when the configured value is missing or invalidNotes on URL handling
The upstream client now uses relative request paths such as:
packages/{name}packages/{name}/versions/{version}instead of absolute paths starting with
/api/....This is important because absolute paths would ignore any configured path prefix in
BaseAddress. Using relative paths ensures values like:https://pub.dev/api/https://unpub.example.com/api/https://example.com/some/prefix/api/all resolve correctly.
Example configuration
{ "HostedUpstream": { "BaseUrl": "https://unpub.example.com/api/" } } Or through environment variables: HostedUpstream__BaseUrl=https://unpub.example.com/api/ ## Validation I do not currently have a working verification environment for this repository, so this PR has not been runtime-validated. Specifically: - I did not run a full build locally - I did not run tests - I did not validate the behavior against a live unpub deployment or a regional mirror The change was made based on code inspection and URI resolution behavior, but it should still be reviewed and verified in a proper development environment before merge.