Skip to content

Commit b9b93f2

Browse files
Depend on platform implementations for common features (microsoft#165950)
* don't depend on externals * remove unneeded externals
1 parent 1784a7e commit b9b93f2

File tree

13 files changed

+50
-40
lines changed

13 files changed

+50
-40
lines changed

extensions/github-authentication/extension-browser.webpack.config.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ module.exports = withBrowserDefaults({
1616
entry: {
1717
extension: './src/extension.ts',
1818
},
19-
externals: {
20-
'keytar': 'commonjs keytar',
21-
},
2219
resolve: {
2320
alias: {
24-
'node-fetch': path.resolve(__dirname, 'node_modules/node-fetch/browser.js'),
2521
'uuid': path.resolve(__dirname, 'node_modules/uuid/dist/esm-browser/index.js'),
26-
'./authServer': path.resolve(__dirname, 'src/env/browser/authServer'),
22+
'./node/authServer': path.resolve(__dirname, 'src/browser/authServer'),
23+
'./node/crypto': path.resolve(__dirname, 'src/browser/crypto'),
24+
'./node/fetch': path.resolve(__dirname, 'src/browser/fetch')
2725
}
2826
}
2927
});

extensions/github-authentication/extension.webpack.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,5 @@ module.exports = withDefaults({
1313
context: __dirname,
1414
entry: {
1515
extension: './src/extension.ts',
16-
},
17-
externals: {
18-
'keytar': 'commonjs keytar'
1916
}
2017
});

extensions/github-authentication/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@
6666
},
6767
"dependencies": {
6868
"node-fetch": "2.6.7",
69-
"uuid": "8.1.0",
7069
"@vscode/extension-telemetry": "0.7.0-preview",
7170
"vscode-tas-client": "^0.1.47"
7271
},
7372
"devDependencies": {
7473
"@types/node": "16.x",
75-
"@types/node-fetch": "^2.5.7",
76-
"@types/uuid": "8.0.0"
74+
"@types/node-fetch": "^2.5.7"
7775
},
7876
"repository": {
7977
"type": "git",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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+
export const crypto = globalThis.crypto;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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+
export const fetching = fetch;

extensions/github-authentication/src/github.ts

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

66
import * as vscode from 'vscode';
7-
import { v4 as uuid } from 'uuid';
7+
import TelemetryReporter from '@vscode/extension-telemetry';
88
import { Keychain } from './common/keychain';
99
import { GitHubServer, IGitHubServer } from './githubServer';
1010
import { arrayEquals } from './common/utils';
11-
import { ExperimentationTelemetry } from './experimentationService';
12-
import TelemetryReporter from '@vscode/extension-telemetry';
11+
import { ExperimentationTelemetry } from './common/experimentationService';
1312
import { Log } from './common/logger';
13+
import { crypto } from './node/crypto';
1414

1515
interface SessionData {
1616
id: string;
@@ -286,7 +286,7 @@ export class GitHubAuthenticationProvider implements vscode.AuthenticationProvid
286286
private async tokenToSession(token: string, scopes: string[]): Promise<vscode.AuthenticationSession> {
287287
const userInfo = await this._githubServer.getUserInfo(token);
288288
return {
289-
id: uuid(),
289+
id: crypto.getRandomValues(new Uint32Array(2)).reduce((prev, curr) => prev += curr.toString(16), ''),
290290
accessToken: token,
291291
account: { label: userInfo.accountName, id: userInfo.id },
292292
scopes

extensions/github-authentication/src/githubServer.ts

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

66
import * as vscode from 'vscode';
7-
import fetch, { Response } from 'node-fetch';
8-
import { v4 as uuid } from 'uuid';
7+
import * as path from 'path';
98
import { PromiseAdapter, promiseFromEvent } from './common/utils';
10-
import { ExperimentationTelemetry } from './experimentationService';
9+
import { ExperimentationTelemetry } from './common/experimentationService';
1110
import { AuthProviderType, UriEventHandler } from './github';
1211
import { Log } from './common/logger';
1312
import { isSupportedEnvironment } from './common/env';
14-
import { LoopbackAuthServer } from './authServer';
15-
import path = require('path');
13+
import { LoopbackAuthServer } from './node/authServer';
14+
import { crypto } from './node/crypto';
15+
import { fetching } from './node/fetch';
1616

1717
const CLIENT_ID = '01ab8ac9400c4e429b23';
1818
const GITHUB_TOKEN_URL = 'https://vscode.dev/codeExchangeProxyEndpoints/github/login/oauth/access_token';
@@ -38,7 +38,7 @@ interface IGitHubDeviceCodeResponse {
3838
async function getScopes(token: string, serverUri: vscode.Uri, logger: Log): Promise<string[]> {
3939
try {
4040
logger.info('Getting token scopes...');
41-
const result = await fetch(serverUri.toString(), {
41+
const result = await fetching(serverUri.toString(), {
4242
headers: {
4343
Authorization: `token ${token}`,
4444
'User-Agent': 'Visual-Studio-Code'
@@ -99,7 +99,7 @@ export class GitHubServer implements IGitHubServer {
9999
return this._redirectEndpoint;
100100
} else {
101101
// GHES
102-
const result = await fetch(this.getServerUri('/meta').toString(true));
102+
const result = await fetching(this.getServerUri('/meta').toString(true));
103103
if (result.ok) {
104104
try {
105105
const json: { installed_version: string } = await result.json();
@@ -151,7 +151,7 @@ export class GitHubServer implements IGitHubServer {
151151
}
152152
};
153153

154-
const nonce = uuid();
154+
const nonce: string = crypto.getRandomValues(new Uint32Array(2)).reduce((prev, curr) => prev += curr.toString(16), '');
155155
const callbackUri = await vscode.env.asExternalUri(vscode.Uri.parse(`${vscode.env.uriScheme}://vscode.github-authentication/did-authenticate?nonce=${encodeURIComponent(nonce)}`));
156156

157157
const supported = isSupportedEnvironment(callbackUri);
@@ -298,7 +298,7 @@ export class GitHubServer implements IGitHubServer {
298298
path: '/login/device/code',
299299
query: `client_id=${CLIENT_ID}&scope=${scopes}`
300300
});
301-
const result = await fetch(uri.toString(true), {
301+
const result = await fetching(uri.toString(true), {
302302
method: 'POST',
303303
headers: {
304304
Accept: 'application/json'
@@ -382,7 +382,7 @@ export class GitHubServer implements IGitHubServer {
382382
}
383383
let accessTokenResult;
384384
try {
385-
accessTokenResult = await fetch(refreshTokenUri.toString(true), {
385+
accessTokenResult = await fetching(refreshTokenUri.toString(true), {
386386
method: 'POST',
387387
headers: {
388388
Accept: 'application/json'
@@ -452,7 +452,7 @@ export class GitHubServer implements IGitHubServer {
452452
body.append('github_enterprise', this.baseUri.toString(true));
453453
body.append('redirect_uri', await this.getRedirectEndpoint());
454454
}
455-
const result = await fetch(endpointUrl, {
455+
const result = await fetching(endpointUrl, {
456456
method: 'POST',
457457
headers: {
458458
Accept: 'application/json',
@@ -485,10 +485,10 @@ export class GitHubServer implements IGitHubServer {
485485
}
486486

487487
public async getUserInfo(token: string): Promise<{ id: string; accountName: string }> {
488-
let result: Response;
488+
let result;
489489
try {
490490
this._logger.info('Getting user info...');
491-
result = await fetch(this.getServerUri('/user').toString(), {
491+
result = await fetching(this.getServerUri('/user').toString(), {
492492
headers: {
493493
Authorization: `token ${token}`,
494494
'User-Agent': 'Visual-Studio-Code'
@@ -544,7 +544,7 @@ export class GitHubServer implements IGitHubServer {
544544

545545
private async checkEduDetails(token: string): Promise<void> {
546546
try {
547-
const result = await fetch('https://education.github.com/api/user', {
547+
const result = await fetching('https://education.github.com/api/user', {
548548
headers: {
549549
Authorization: `token ${token}`,
550550
'faculty-check-preview': 'true',
@@ -577,7 +577,7 @@ export class GitHubServer implements IGitHubServer {
577577
private async checkEnterpriseVersion(token: string): Promise<void> {
578578
try {
579579

580-
const result = await fetch(this.getServerUri('/meta').toString(), {
580+
const result = await fetching(this.getServerUri('/meta').toString(), {
581581
headers: {
582582
Authorization: `token ${token}`,
583583
'User-Agent': 'Visual-Studio-Code'

0 commit comments

Comments
 (0)