Skip to content

Commit b7b2184

Browse files
author
Benjamin Pasero
committed
api - fold app uri creation into asExternalUri (fix microsoft#82884)
1 parent d186653 commit b7b2184

9 files changed

Lines changed: 82 additions & 47 deletions

File tree

extensions/vscode-api-tests/src/singlefolder-tests/env.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as assert from 'assert';
7-
import { env, extensions, ExtensionKind, UIKind } from 'vscode';
7+
import { env, extensions, ExtensionKind, UIKind, Uri } from 'vscode';
88

99
suite('env-namespace', () => {
1010

@@ -45,8 +45,29 @@ suite('env-namespace', () => {
4545
}
4646
});
4747

48-
test('env.uiKind', function () {
48+
test('env.uiKind', async function () {
49+
const uri = Uri.parse(`${env.uriScheme}:://vscode.vscode-api-tests/path?key=value&other=false`);
50+
const result = await env.asExternalUri(uri);
51+
4952
const kind = env.uiKind;
50-
assert.equal(kind, UIKind.Desktop);
53+
if (result.scheme === 'http' || result.scheme === 'https') {
54+
assert.equal(kind, UIKind.Web);
55+
} else {
56+
assert.equal(kind, UIKind.Desktop);
57+
}
58+
});
59+
60+
test('env.asExternalUri - with env.uriScheme', async function () {
61+
const uri = Uri.parse(`${env.uriScheme}:://vscode.vscode-api-tests/path?key=value&other=false`);
62+
const result = await env.asExternalUri(uri);
63+
assert.ok(result);
64+
65+
if (env.uiKind === UIKind.Desktop) {
66+
assert.equal(uri.scheme, result.scheme);
67+
assert.equal(uri.authority, result.authority);
68+
assert.equal(uri.path, result.path);
69+
} else {
70+
assert.ok(result.scheme === 'http' || result.scheme === 'https');
71+
}
5172
});
5273
});

src/vs/platform/product/common/product.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ if (isWeb) {
2323
assign(product, {
2424
version: '1.39.0-dev',
2525
nameLong: 'Visual Studio Code Web Dev',
26-
nameShort: 'VSCode Web Dev'
26+
nameShort: 'VSCode Web Dev',
27+
urlProtocol: 'code-oss'
2728
});
2829
}
2930
}

src/vs/vscode.d.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6433,22 +6433,51 @@ declare module 'vscode' {
64336433
export function openExternal(target: Uri): Thenable<boolean>;
64346434

64356435
/**
6436+
* Resolves a uri to form that is accessible externally. Currently only supports `https:`, `http:` and
6437+
* `vscode.env.uriScheme` uris.
6438+
*
6439+
* #### `http:` or `https:` scheme
6440+
*
64366441
* Resolves an *external* uri, such as a `http:` or `https:` link, from where the extension is running to a
64376442
* uri to the same resource on the client machine.
64386443
*
6439-
* This is a no-op if the extension is running on the client machine. Currently only supports
6440-
* `https:` and `http:` uris.
6444+
* This is a no-op if the extension is running on the client machine.
64416445
*
64426446
* If the extension is running remotely, this function automatically establishes a port forwarding tunnel
64436447
* from the local machine to `target` on the remote and returns a local uri to the tunnel. The lifetime of
64446448
* the port fowarding tunnel is managed by VS Code and the tunnel can be closed by the user.
64456449
*
6446-
* Extensions should not cache the result of `asExternalUri` as the resolved uri may become invalid due to
6447-
* a system or user action — for example, in remote cases, a user may close a port forwardng tunnel
6448-
* that was opened by `asExternalUri`.
6450+
* *Note* that uris passed through `openExternal` are automatically resolved and you should not call `asExternalUri` on them.
6451+
*
6452+
* #### `vscode.env.uriScheme`
6453+
*
6454+
* Creates a uri that - if opened in a browser (e.g. via `openExternal`) - will result in a registered [UriHandler](#UriHandler)
6455+
* to trigger.
6456+
*
6457+
* Extensions should not make any assumptions about the resulting uri and should not alter it in anyway.
6458+
* Rather, extensions can e.g. use this uri in an authentication flow, by adding the uri as callback query
6459+
* argument to the server to authenticate to.
6460+
*
6461+
* *Note* that if the server decides to add additional query parameters to the uri (e.g. a token or secret), it
6462+
* will appear in the uri that is passed to the [UriHandler](#UriHandler).
6463+
*
6464+
* **Example** of an authentication flow:
6465+
* ```typescript
6466+
* vscode.window.registerUriHandler({
6467+
* handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
6468+
* if (uri.path === '/did-authenticate') {
6469+
* console.log(uri.toString());
6470+
* }
6471+
* }
6472+
* });
6473+
*
6474+
* const callableUri = await vscode.env.asExternalUri(vscode.Uri.parse(`${env.uriScheme}:://my.extension/did-authenticate`));
6475+
* await vscode.env.openExternal(callableUri);
6476+
* ```
64496477
*
6450-
* *Note* that uris passed through `openExternal` are automatically resolved and you should not call `asExternalUri`
6451-
* on them.
6478+
* *Note* that extensions should not cache the result of `asExternalUri` as the resolved uri may become invalid due to
6479+
* a system or user action — for example, in remote cases, a user may close a port forwarding tunnel that was opened by
6480+
* `asExternalUri`.
64526481
*
64536482
* @return A uri that can be used on the client machine.
64546483
*/

src/vs/vscode.proposed.d.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -855,33 +855,7 @@ declare module 'vscode' {
855855
export namespace env {
856856

857857
/**
858-
* Creates a Uri that - if opened in a browser - will result in a
859-
* registered [UriHandler](#UriHandler) to fire. The handler's
860-
* Uri will be configured with the path, query and fragment of
861-
* [AppUriOptions](#AppUriOptions) if provided, otherwise it will be empty.
862-
*
863-
* Extensions should not make any assumptions about the resulting
864-
* Uri and should not alter it in anyway. Rather, extensions can e.g.
865-
* use this Uri in an authentication flow, by adding the Uri as
866-
* callback query argument to the server to authenticate to.
867-
*
868-
* Note: If the server decides to add additional query parameters to the Uri
869-
* (e.g. a token or secret), it will appear in the Uri that is passed
870-
* to the [UriHandler](#UriHandler).
871-
*
872-
* **Example** of an authentication flow:
873-
* ```typescript
874-
* vscode.window.registerUriHandler({
875-
* handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
876-
* if (uri.path === '/did-authenticate') {
877-
* console.log(uri.toString());
878-
* }
879-
* }
880-
* });
881-
*
882-
* const callableUri = await vscode.env.createAppUri({ payload: { path: '/did-authenticate' } });
883-
* await vscode.env.openExternal(callableUri);
884-
* ```
858+
* @deprecated use `vscode.env.asExternalUri` instead.
885859
*/
886860
export function createAppUri(options?: AppUriOptions): Thenable<Uri>;
887861
}

src/vs/workbench/api/browser/mainThreadUrls.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ export class MainThreadUrls implements MainThreadUrlsShape {
6868
return Promise.resolve(undefined);
6969
}
7070

71-
async $createAppUri(extensionId: ExtensionIdentifier, options?: { payload?: Partial<UriComponents> }): Promise<URI> {
71+
async $createAppUri(uri: UriComponents): Promise<URI> {
72+
return this.urlService.create(uri);
73+
}
74+
75+
async $proposedCreateAppUri(extensionId: ExtensionIdentifier, options?: { payload?: Partial<UriComponents> }): Promise<URI> {
7276
const payload: Partial<UriComponents> = options && options.payload ? options.payload : Object.create(null);
7377

7478
// we define the authority to be the extension ID to ensure

src/vs/workbench/api/common/extHost.api.impl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
228228
get uriScheme() { return initData.environment.appUriScheme; },
229229
createAppUri(options?) {
230230
checkProposedApiEnabled(extension);
231-
return extHostUrls.createAppUri(extension.identifier, options);
231+
return extHostUrls.proposedCreateAppUri(extension.identifier, options);
232232
},
233233
get logLevel() {
234234
checkProposedApiEnabled(extension);
@@ -248,6 +248,10 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
248248
return extHostWindow.openUri(uri, { allowTunneling: !!initData.remote.isRemote });
249249
},
250250
asExternalUri(uri: URI) {
251+
if (uri.scheme === initData.environment.appUriScheme) {
252+
return extHostUrls.createAppUri(uri);
253+
}
254+
251255
return extHostWindow.asExternalUri(uri, { allowTunneling: !!initData.remote.isRemote });
252256
},
253257
get remoteName() {

src/vs/workbench/api/common/extHost.protocol.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,8 @@ export interface ExtHostWebviewsShape {
594594
export interface MainThreadUrlsShape extends IDisposable {
595595
$registerUriHandler(handle: number, extensionId: ExtensionIdentifier): Promise<void>;
596596
$unregisterUriHandler(handle: number): Promise<void>;
597-
$createAppUri(extensionId: ExtensionIdentifier, options?: { payload?: Partial<UriComponents> }): Promise<UriComponents>;
597+
$createAppUri(uri: UriComponents): Promise<UriComponents>;
598+
$proposedCreateAppUri(extensionId: ExtensionIdentifier, options?: { payload?: Partial<UriComponents> }): Promise<UriComponents>;
598599
}
599600

600601
export interface ExtHostUrlsShape {

src/vs/workbench/api/common/extHostUrls.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ export class ExtHostUrls implements ExtHostUrlsShape {
5656
return Promise.resolve(undefined);
5757
}
5858

59-
async createAppUri(extensionId: ExtensionIdentifier, options?: vscode.AppUriOptions): Promise<vscode.Uri> {
60-
return URI.revive(await this._proxy.$createAppUri(extensionId, options));
59+
async createAppUri(uri: URI): Promise<vscode.Uri> {
60+
return URI.revive(await this._proxy.$createAppUri(uri));
61+
}
62+
63+
async proposedCreateAppUri(extensionId: ExtensionIdentifier, options?: vscode.AppUriOptions): Promise<vscode.Uri> {
64+
return URI.revive(await this._proxy.$proposedCreateAppUri(extensionId, options));
6165
}
6266
}

src/vs/workbench/browser/web.main.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ class BrowserMain extends Disposable {
162162
// Product
163163
const productService = {
164164
_serviceBrand: undefined,
165-
...{
166-
...product, // dev or built time config
167-
...{ urlProtocol: '' } // web related overrides from us
168-
}
165+
...product
169166
};
170167
serviceCollection.set(IProductService, productService);
171168

0 commit comments

Comments
 (0)