Skip to content

Commit d27ab54

Browse files
committed
Extract port mapping helper function
1 parent 402281b commit d27ab54

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

src/vs/workbench/contrib/webview/common/portMapping.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ import * as modes from 'vs/editor/common/modes';
99
import { REMOTE_HOST_SCHEME } from 'vs/platform/remote/common/remoteHosts';
1010
import { ITunnelService, RemoteTunnel } from 'vs/platform/remote/common/tunnel';
1111

12+
export function extractLocalHostUriMetaDataForPortMapping(uri: URI): { address: string, port: number } | undefined {
13+
if (uri.scheme !== 'http' && uri.scheme !== 'https') {
14+
return undefined;
15+
}
16+
const localhostMatch = /^(localhost):(\d+)$/.exec(uri.authority);
17+
if (!localhostMatch) {
18+
return undefined;
19+
}
20+
return {
21+
address: localhostMatch[1],
22+
port: +localhostMatch[2],
23+
};
24+
}
25+
1226
export class WebviewPortMappingManager extends Disposable {
1327

1428
private readonly _tunnels = new Map<number, Promise<RemoteTunnel>>();
@@ -23,31 +37,25 @@ export class WebviewPortMappingManager extends Disposable {
2337

2438
public async getRedirect(url: string): Promise<string | undefined> {
2539
const uri = URI.parse(url);
26-
if (uri.scheme !== 'http' && uri.scheme !== 'https') {
27-
return undefined;
28-
}
29-
30-
const localhostMatch = /^localhost:(\d+)$/.exec(uri.authority);
31-
if (!localhostMatch) {
32-
return undefined;
40+
const requestLocalHostInfo = extractLocalHostUriMetaDataForPortMapping(uri);
41+
if (!requestLocalHostInfo) {
42+
return requestLocalHostInfo;
3343
}
34-
35-
const port = +localhostMatch[1];
3644
for (const mapping of this.mappings()) {
37-
if (mapping.webviewPort === port) {
45+
if (mapping.webviewPort === requestLocalHostInfo.port) {
3846
if (this.extensionLocation && this.extensionLocation.scheme === REMOTE_HOST_SCHEME) {
3947
const tunnel = await this.getOrCreateTunnel(mapping.extensionHostPort);
4048
if (tunnel) {
41-
return url.replace(
42-
new RegExp(`^${uri.scheme}://localhost:${mapping.webviewPort}(/|$)`),
43-
`${uri.scheme}://localhost:${tunnel.tunnelLocalPort}$1`);
49+
return uri.with({
50+
authority: `127.0.0.1:${tunnel.tunnelLocalPort}`,
51+
}).toString();
4452
}
4553
}
4654

4755
if (mapping.webviewPort !== mapping.extensionHostPort) {
48-
return url.replace(
49-
new RegExp(`^${uri.scheme}://localhost:${mapping.webviewPort}(/|$)`),
50-
`${uri.scheme}://localhost:${mapping.extensionHostPort}$1`);
56+
return uri.with({
57+
authority: `${requestLocalHostInfo.address}:${mapping.extensionHostPort}`
58+
}).toString();
5159
}
5260
}
5361
}

0 commit comments

Comments
 (0)