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
66
67
68
69
70
71
72
73
74
75
76
77
78
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CharCode } from 'vs/base/common/charCode';
import { Schemas } from 'vs/base/common/network';
import { URI } from 'vs/base/common/uri';
export interface WebviewRemoteInfo {
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-cdn.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-cdn.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: URI, remoteInfo?: WebviewRemoteInfo): 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}+${encodeAuthority(resource.authority)}.${webviewRootResourceAuthority}`,
path: resource.path,
fragment: resource.fragment,
query: resource.query,
});
}
function encodeAuthority(authority: string): string {
return authority.replace(/./g, char => {
const code = char.charCodeAt(0);
if (
(code >= CharCode.a && code <= CharCode.z)
|| (code >= CharCode.A && code <= CharCode.Z)
|| (code >= CharCode.Digit0 && code <= CharCode.Digit9)
) {
return char;
}
return '-' + code.toString(16).padStart(4, '0');
});
}
export function decodeAuthority(authority: string) {
return authority.replace(/-([0-9a-f]{4})/g, (_, code) => String.fromCharCode(parseInt(code, 16)));
}
|