-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lance <[email protected]>
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Replace the URL based on the environment variable and optional hosts | ||
*/ | ||
export function convertDockerHost( | ||
url: string, | ||
moreHostsToReplace?: string, | ||
newHost?: string, | ||
): string { | ||
if (newHost) { | ||
return replaceUrlHost(url, newHost, moreHostsToReplace); | ||
} | ||
if (process.env.USE_DOCKER_INTERNAL === "true") { | ||
url = replaceUrlHost(url, "host.docker.internal", moreHostsToReplace); | ||
return url; | ||
} | ||
if (process.env.DOCKER_HOST) { | ||
url = replaceUrlHost(url, process.env.DOCKER_HOST, moreHostsToReplace); | ||
return url; | ||
} | ||
return url; | ||
} | ||
|
||
/** | ||
* Replace the URL based on the environment variable and optional hosts | ||
*/ | ||
export function replaceUrlHost( | ||
url: string, | ||
newHost: string = "host.docker.internal", | ||
moreHostsToReplace: string = "", | ||
): string { | ||
const defaultHosts = ["127.0.0.1", "localhost"]; | ||
const hostsToReplace = moreHostsToReplace | ||
? defaultHosts.concat(moreHostsToReplace.split(",")) | ||
: defaultHosts; | ||
|
||
for (const host of hostsToReplace) { | ||
if (url.includes(host)) { | ||
console.log(`Replacing URL host: ${host} -> ${newHost}`); | ||
return url.replace(host, newHost); | ||
} | ||
} | ||
|
||
throw new Error(`Error appling replacement for URL: ${url}`); | ||
} |