Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Fix an incorrect pathname in a WebView endpoint URL #940

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SERVER_TYPE_ATTR, SERVER_WEBVIEWS_ATTR_VALUE } from '../common/che-serv
import { inject, injectable, postConstruct } from 'inversify';

import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import URI from '@theia/core/lib/common/uri';
import { WebviewEnvironment } from '@theia/plugin-ext/lib/main/browser/webview/webview-environment';
import { WebviewExternalEndpoint } from '@theia/plugin-ext/lib/main/common/webview-protocol';
import { WorkspaceService } from '@eclipse-che/theia-remote-api/lib/common/workspace-service';
Expand All @@ -28,21 +29,31 @@ export class CheWebviewEnvironment extends WebviewEnvironment {
protected async init(): Promise<void> {
try {
const webviewExternalEndpointPattern = await this.environments.getValue(WebviewExternalEndpoint.pattern);
const webviewCheEndpoint = await this.workspaceService.findUniqueEndpointByAttribute(
SERVER_TYPE_ATTR,
SERVER_WEBVIEWS_ATTR_VALUE
);
let webviewCheEndpointHostname: string | undefined;
if (webviewCheEndpoint && webviewCheEndpoint.url) {
webviewCheEndpointHostname = new URL(webviewCheEndpoint.url).hostname;
}
const webviewHostname =
const webviewCheEndpoint = await this.getWebviewCheEndpoint();
const webviewHost =
(webviewExternalEndpointPattern && webviewExternalEndpointPattern.value) ||
webviewCheEndpointHostname ||
webviewCheEndpoint ||
WebviewExternalEndpoint.defaultPattern;
this.externalEndpointHost.resolve(webviewHostname.replace('{{hostname}}', window.location.host || 'localhost'));
this.externalEndpointHost.resolve(webviewHost.replace('{{hostname}}', window.location.host || 'localhost'));
} catch (e) {
this.externalEndpointHost.reject(e);
}
}

async externalEndpointUrl(): Promise<URI> {
const host = await this.externalEndpointHost.promise;
return new URI(host).resolve('webview');
}

protected async getWebviewCheEndpoint(): Promise<string | undefined> {
try {
const webviewCheEndpoint = await this.workspaceService.findUniqueEndpointByAttribute(
SERVER_TYPE_ATTR,
SERVER_WEBVIEWS_ATTR_VALUE
);
return webviewCheEndpoint.url;
} catch (error) {
return undefined;
}
}
}