Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/browserView/common/browserViewUri.ts
4777 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Schemas } from '../../../base/common/network.js';
7
import { URI } from '../../../base/common/uri.js';
8
import { generateUuid } from '../../../base/common/uuid.js';
9
10
/**
11
* Helper for creating and parsing browser view URIs.
12
*/
13
export namespace BrowserViewUri {
14
15
export const scheme = Schemas.vscodeBrowser;
16
17
/**
18
* Creates a resource URI for a browser view with the given URL.
19
* Optionally accepts an ID; if not provided, a new UUID is generated.
20
*/
21
export function forUrl(url: string | undefined, id?: string): URI {
22
const viewId = id ?? generateUuid();
23
return URI.from({
24
scheme,
25
path: `/${viewId}`,
26
query: url ? `url=${encodeURIComponent(url)}` : undefined
27
});
28
}
29
30
/**
31
* Parses a browser view resource URI to extract the ID and URL.
32
*/
33
export function parse(resource: URI): { id: string; url: string } | undefined {
34
if (resource.scheme !== scheme) {
35
return undefined;
36
}
37
38
// Remove leading slash if present
39
const id = resource.path.startsWith('/') ? resource.path.substring(1) : resource.path;
40
if (!id) {
41
return undefined;
42
}
43
44
const url = resource.query ? new URLSearchParams(resource.query).get('url') ?? '' : '';
45
46
return { id, url };
47
}
48
49
/**
50
* Extracts the ID from a browser view resource URI.
51
*/
52
export function getId(resource: URI): string | undefined {
53
return parse(resource)?.id;
54
}
55
56
/**
57
* Extracts the URL from a browser view resource URI.
58
*/
59
export function getUrl(resource: URI): string | undefined {
60
return parse(resource)?.url;
61
}
62
}
63
64