Add remote-authority to webview uri

This commit is contained in:
Matt Bierner
2021-05-11 17:30:59 -07:00
parent 927e791753
commit b847eb35e7
9 changed files with 77 additions and 121 deletions

View File

@@ -10,21 +10,29 @@ export interface WebviewInitData {
readonly isExtensionDevelopmentDebug: boolean;
readonly webviewResourceRoot: string;
readonly webviewCspSource: string;
readonly remote: { readonly authority: string | undefined };
}
/**
* 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
* /remote-authority?/scheme/resource-authority/path...
* ```
*/
export function asWebviewUri(
initData: WebviewInitData,
uuid: string,
resource: vscode.Uri,
): vscode.Uri {
const uri = initData.webviewResourceRoot
// Make sure we preserve the scheme of the resource but convert it into a normal path segment
// The scheme is important as we need to know if we are requesting a local or a remote resource.
.replace('{{resource}}', resource.scheme + withoutScheme(resource))
.replace('{{resource}}', (initData.remote.authority ?? '') + '/' + resource.scheme + '/' + encodeURIComponent(resource.authority) + resource.path)
.replace('{{uuid}}', uuid);
return URI.parse(uri);
}
function withoutScheme(resource: vscode.Uri): string {
return resource.toString().replace(/^\S+?:/, '');
return URI.parse(uri).with({
fragment: resource.fragment,
query: resource.query,
});
}