-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.js
174 lines (149 loc) · 4.76 KB
/
renderer.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const electron = require('electron');
const ipc = electron.ipcRenderer;
const crypto = require('crypto');
const http = require('http');
const https = require('https');
const url = require('url');
const fs = require('fs');
const HttpProxyAgent = require('http-proxy-agent');
const HttpsProxyAgent = require('https-proxy-agent');
const startButton = document.getElementById('start');
const resultsElement = document.getElementById('results');
const proxyInput = document.getElementById('proxy');
const cafileInput = document.getElementById('cafile');
const httpUrl = 'http://code.visualstudio.com/docs/tools/vscecli';
const httpsUrl = 'https://code.visualstudio.com/docs/tools/vscecli';
function hash(contents) {
return crypto.createHash('sha1').update(contents).digest('hex');
}
function runXHR(url) {
return new Promise(resolve => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onerror = () => {
resolve({
status: xhr.status,
result: 'Error'
});
};
xhr.onload = () => {
resolve({
status: xhr.status,
result: hash(xhr.response)
});
};
xhr.send();
});
}
function getProxyAgent(rawRequestURL, proxyURL, strictSSL) {
if (!proxyURL) {
return null;
}
const requestURL = url.parse(rawRequestURL);
const proxyEndpoint = url.parse(proxyURL);
if (!/^https?:$/.test(proxyEndpoint.protocol)) {
return null;
}
const opts = {
host: proxyEndpoint.hostname,
port: Number(proxyEndpoint.port),
auth: proxyEndpoint.auth,
rejectUnauthorized: strictSSL
};
return requestURL.protocol === 'http:' ? new HttpProxyAgent(opts) : new HttpsProxyAgent(opts);
}
function runNode(requestUrl, ca, proxyUrl = '', strictSSL = true) {
return new Promise(resolve => {
const endpoint = url.parse(requestUrl);
const rawRequest = endpoint.protocol === 'https:' ? https.request : http.request;
const opts = {
hostname: endpoint.hostname,
port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
path: endpoint.path,
method: 'GET',
rejectUnauthorized: strictSSL,
agent: getProxyAgent(requestUrl, proxyUrl, strictSSL),
ca
};
req = rawRequest(opts, res => {
let buffer = [];
res.on('data', d => buffer.push(d));
res.on('end', () => {
resolve({
status: res.statusCode,
result: hash(buffer.join(''))
});
});
res.on('error', (err) => {
console.log(err);
resolve({
status: 0,
result: 'Error'
});
});
});
req.on('error', err => {
console.log(err);
resolve({
status: 0,
result: 'Error'
});
});
req.end();
});
}
let REQUESTS = 0;
function runElectron(requestUrl, ca) {
return new Promise(resolve => {
const requestId = `req${REQUESTS++}`;
ipc.once(requestId, (event, message) => resolve(message));
ipc.send('url', requestId, requestUrl, ca);
});
}
function timeout(promise, millis, onTimeout) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(onTimeout), millis);
promise.then(resolve, reject);
});
}
function run(proxyUrl) {
resultsElement.textContent = 'Running tests...';
let ca = null;
if (cafileInput.value) {
ca = fs.readFileSync(cafileInput.files[0].path, 'utf8');
}
const tests = [
['HTTP XHR', runXHR(httpUrl)],
['HTTPS XHR', runXHR(httpsUrl)],
['HTTP Node', runNode(httpUrl, ca)],
['HTTPS Node', runNode(httpsUrl, ca)],
['HTTP Node (not strict)', runNode(httpUrl, ca, null, false)],
['HTTPS Node (not strict)', runNode(httpsUrl, ca, null, false)],
['HTTP Node Agent', runNode(httpUrl, ca, proxyUrl)],
['HTTPS Node Agent', runNode(httpsUrl, ca, proxyUrl)],
['HTTP Node Agent (not strict)', runNode(httpUrl, ca, proxyUrl, false)],
['HTTPS Node Agent (not strict)', runNode(httpsUrl, ca, proxyUrl, false)],
['HTTP Electron', runElectron(httpUrl, ca)],
['HTTPS Electron', runElectron(httpsUrl, ca)]
];
const promises = tests.map(([name, promise]) => {
return timeout(promise, 10000, { status: '0', result: 'Timeout' })
.then(r => `| ${name} | ${r.status} | ${r.result} |`);
});
Promise.all(promises).then(results => {
const textarea = document.createElement('textarea');
textarea.value = `| Test | Status | Hash or Error |
|---|---|---|
${results.join('\n')}`;
resultsElement.innerHTML = '';
resultsElement.appendChild(textarea);
startButton.disabled = false;
});
}
startButton.onclick = () => {
startButton.disabled = true;
run(proxyInput.value);
};