MCPcopy Index your code
hub / github.com/coder/coder / rewriteLocalhostURL

Function rewriteLocalhostURL

site/src/utils/portForward.ts:62–91  ·  view source on GitHub ↗
(
	url: string,
	proxyHost: string,
	agentName: string,
	workspaceName: string,
	username: string,
)

Source from the content-addressed store, hash-verified

60 * localhost address or when parsing fails.
61 */
62export const rewriteLocalhostURL = (
63 url: string,
64 proxyHost: string,
65 agentName: string,
66 workspaceName: string,
67 username: string,
68): string => {
69 try {
70 const parsed = new URL(url);
71 if (!localHosts.has(parsed.hostname)) {
72 return url;
73 }
74 const protocol = parsed.protocol.replace(
75 ":",
76 "",
77 ) as WorkspaceAgentPortShareProtocol;
78 return portForwardURL(
79 proxyHost,
80 resolveLocalhostPort(parsed.port, parsed.protocol),
81 agentName,
82 workspaceName,
83 username,
84 protocol,
85 parsed.pathname,
86 parsed.search,
87 );
88 } catch {
89 return url;
90 }
91};
92
93// openMaybePortForwardedURL tries to open the provided URI through the
94// port-forwarded URL if it is localhost, otherwise opens it normally.

Callers 3

urlTransformFunction · 0.90

Calls 2

portForwardURLFunction · 0.85
resolveLocalhostPortFunction · 0.85

Tested by

no test coverage detected