-
Notifications
You must be signed in to change notification settings - Fork 224
fix: normalize regional API base URL and prevent invalid multi-region… #2942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { | ||
| REGION_FRA, | ||
| REGION_NYC, | ||
| REGION_SFO, | ||
| REGION_SGP, | ||
| REGION_SYD, | ||
| REGION_TOR, | ||
| SUBDOMAIN_FRA, | ||
| SUBDOMAIN_NYC, | ||
| SUBDOMAIN_SFO, | ||
| SUBDOMAIN_SGP, | ||
| SUBDOMAIN_SYD, | ||
| SUBDOMAIN_TOR | ||
| } from '$lib/constants'; | ||
|
|
||
| /** Ordered list of region DNS prefixes (e.g. `fra.`) for stripping from API hostnames. */ | ||
| const REGION_SUBDOMAIN_PREFIXES: readonly string[] = [ | ||
| SUBDOMAIN_FRA, | ||
| SUBDOMAIN_NYC, | ||
| SUBDOMAIN_SYD, | ||
| SUBDOMAIN_SFO, | ||
| SUBDOMAIN_SGP, | ||
| SUBDOMAIN_TOR | ||
| ]; | ||
|
|
||
| /** | ||
| * Removes leading Appwrite Cloud region label(s) from `hostname` (e.g. `fra.`). | ||
| * Strips repeatedly so a doubled prefix (`fra.fra.cloud...`) does not become | ||
| * `nyc.fra.cloud...` after prepending another region. | ||
| */ | ||
| export function stripLeadingRegionSubdomain(hostname: string): string { | ||
| let host = hostname; | ||
| let changed = true; | ||
| while (changed) { | ||
| changed = false; | ||
| for (const prefix of REGION_SUBDOMAIN_PREFIXES) { | ||
| if (host.startsWith(prefix)) { | ||
| host = host.slice(prefix.length); | ||
| changed = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return host; | ||
| } | ||
|
|
||
| /** Region prefix (e.g. `fra.`) used before the API hostname when multi-region is enabled. */ | ||
| export function getRegionSubdomain(region?: string): string { | ||
| switch (region) { | ||
| case REGION_FRA: | ||
| return SUBDOMAIN_FRA; | ||
| case REGION_SYD: | ||
| return SUBDOMAIN_SYD; | ||
| case REGION_NYC: | ||
| return SUBDOMAIN_NYC; | ||
| case REGION_SFO: | ||
| return SUBDOMAIN_SFO; | ||
| case REGION_SGP: | ||
| return SUBDOMAIN_SGP; | ||
| case REGION_TOR: | ||
| return SUBDOMAIN_TOR; | ||
| default: | ||
| return ''; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds the `/v1` API base URL (protocol + host + `/v1`). | ||
| * When `isMultiRegion` is true, strips any known region prefix from the host, then prepends the requested region. | ||
| */ | ||
| export function buildRegionalV1Endpoint( | ||
| protocol: string, | ||
| hostname: string, | ||
| region: string | undefined, | ||
| isMultiRegion: boolean | ||
| ): string { | ||
| if (!isMultiRegion) { | ||
| return `${protocol}//${hostname}/v1`; | ||
| } | ||
|
|
||
| const hostWithoutRegion = stripLeadingRegionSubdomain(hostname); | ||
| const subdomain = getRegionSubdomain(region); | ||
|
|
||
| return `${protocol}//${subdomain}${hostWithoutRegion}/v1`; | ||
| } | ||
|
Comment on lines
+1
to
+85
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The project convention (documented in
Consider adding
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
buildRegionalV1Endpointis called withisMultiRegion = truebutregion = undefined(which happens for the initialconst endpoint = getApiEndpoint()atsdk.ts:67), the function will:hostnameviastripLeadingRegionSubdomaingetRegionSubdomain(undefined)→''This produces a hostname without any region prefix.
The original code avoided this problem because it only mutated the hostname when
subdomainwas non-empty (if (subdomain && hostname.startsWith(subdomain)) ...). WithgetSubdomain(undefined)returning'', the original hostname passed through unchanged.Affected scenario: Any self-hosted instance with
PUBLIC_APPWRITE_MULTI_REGION=truewhereAPPWRITE_ENDPOINT(or the browser URL) contains a region prefix (e.g.fra.example.com). CallinggetApiEndpoint()with the new code would stripfra.and route the console clients toexample.com/v1instead of the intendedfra.example.com/v1.A minimal guard fixes this: