forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubServer.ts
More file actions
58 lines (51 loc) · 1.56 KB
/
Copy pathgithubServer.ts
File metadata and controls
58 lines (51 loc) · 1.56 KB
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
import * as https from 'https';
import * as vscode from 'vscode';
import Logger from '../common/logger';
import { agent } from '../env/node/net';
import { HostHelper } from './configuration';
export class GitHubManager {
private _servers: Map<string, boolean> = new Map().set('github.com', true);
public async isGitHub(host: vscode.Uri): Promise<boolean> {
if (host === null) {
return false;
}
if (this._servers.has(host.authority)) {
return !!this._servers.get(host.authority);
}
const options = await GitHubManager.getOptions(host, 'HEAD', '/rate_limit');
return new Promise<boolean>((resolve, _) => {
const get = https.request(options, res => {
const ret = res.headers['x-github-request-id'];
resolve(ret !== undefined);
});
get.end();
get.on('error', err => {
Logger.appendLine(`No response from host ${host}: ${err.message}`, 'GitHubServer');
resolve(false);
});
}).then(isGitHub => {
Logger.debug(`Host ${host} is associated with GitHub: ${isGitHub}`, 'GitHubServer');
this._servers.set(host.authority, isGitHub);
return isGitHub;
});
}
public static async getOptions(hostUri: vscode.Uri, method: string = 'GET', path: string, token?: string) {
const headers: {
'user-agent': string;
authorization?: string;
} = {
'user-agent': 'GitHub VSCode Pull Requests',
};
if (token) {
headers.authorization = `token ${token}`;
}
return {
host: (await HostHelper.getApiHost(hostUri)).authority,
port: 443,
method,
path: HostHelper.getApiPath(hostUri, path),
headers,
agent,
};
}
}