-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathwebview.ts
65 lines (57 loc) · 2.16 KB
/
webview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Schemas } from 'vs/base/common/network';
import { URI } from 'vs/base/common/uri';
import type * as vscode from 'vscode';
export interface WebviewInitData {
readonly remote: {
readonly isRemote: boolean;
readonly authority: string | undefined
};
}
/**
* Root from which resources in webviews are loaded.
*
* This is hardcoded because we never expect to actually hit it. Instead these requests
* should always go to a service worker.
*/
export const webviewResourceBaseHost = 'vscode-webview.net';
export const webviewRootResourceAuthority = `vscode-resource.${webviewResourceBaseHost}`;
export const webviewGenericCspSource = `https://*.${webviewResourceBaseHost}`;
/**
* Construct a uri that can load resources inside a webview
*
* We encode the resource component of the uri so that on the main thread
* we know where to load the resource from (remote or truly local):
*
* ```txt
* ${scheme}+${resource-authority}.vscode-resource.vscode-webview.net/${path}
* ```
*
* @param resource Uri of the resource to load.
* @param remoteInfo Optional information about the remote that specifies where `resource` should be resolved from.
*/
export function asWebviewUri(
resource: vscode.Uri,
remoteInfo?: { authority: string | undefined, isRemote: boolean }
): vscode.Uri {
if (resource.scheme === Schemas.http || resource.scheme === Schemas.https) {
return resource;
}
if (remoteInfo && remoteInfo.authority && remoteInfo.isRemote && resource.scheme === Schemas.file) {
resource = URI.from({
scheme: Schemas.vscodeRemote,
authority: remoteInfo.authority,
path: resource.path,
});
}
return URI.from({
scheme: Schemas.https,
authority: `${resource.scheme}+${resource.authority}.${webviewRootResourceAuthority}`,
path: resource.path,
fragment: resource.fragment,
query: resource.query,
});
}