( url: string, proxyHost: string, agentName: string, workspaceName: string, username: string, )
| 60 | * localhost address or when parsing fails. |
| 61 | */ |
| 62 | export 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. |
no test coverage detected