Skip to content

Commit 1fb1bb9

Browse files
committed
Migrate to "got" library from "request"
1 parent bc4d945 commit 1fb1bb9

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

src/home.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import { getCoreDir, runPIOCommand } from './core';
1111

1212
import crypto from 'crypto';
1313
import fs from 'fs';
14+
import got from 'got';
1415
import jsonrpc from 'jsonrpc-lite';
1516
import path from 'path';
1617
import qs from 'querystringify';
17-
import request from 'request';
1818
import tcpPortUsed from 'tcp-port-used';
1919
import ws from 'ws';
2020

@@ -59,20 +59,11 @@ export function getFrontendUrl(options) {
5959
}
6060

6161
export async function getFrontendVersion() {
62-
return await new Promise((resolve) => {
63-
request(
64-
constructServerUrl({ path: '/package.json' }),
65-
function (error, response, body) {
66-
if (error || !response || response.statusCode !== 200) {
67-
return resolve(undefined);
68-
}
69-
try {
70-
return resolve(JSON.parse(body).version);
71-
} catch (err) {}
72-
return resolve(undefined);
73-
}
74-
);
75-
});
62+
try {
63+
return (
64+
await got(constructServerUrl({ path: '/package.json' }), { timeout: 1000 }).json()
65+
).version;
66+
} catch (err) {}
7667
}
7768

7869
async function listenIDECommands(callback) {
@@ -206,21 +197,24 @@ async function _ensureServerStarted(options = {}) {
206197
return true;
207198
}
208199

209-
export function shutdownServer() {
200+
export async function shutdownServer() {
210201
if (!_HTTP_PORT) {
211202
return;
212203
}
213-
return request.post(constructServerUrl({ path: '/__shutdown__' }));
204+
return await got.post(constructServerUrl({ path: '/__shutdown__' }), {
205+
timeout: 1000,
206+
});
214207
}
215208

216209
export async function shutdownAllServers() {
217210
let port = HTTP_PORT_BEGIN;
218211
while (port < HTTP_PORT_END) {
219-
request
220-
.get(
221-
constructServerUrl({ port, includeSID: false, query: { __shutdown__: '1' } })
222-
)
223-
.on('error', () => {});
212+
try {
213+
got(
214+
constructServerUrl({ port, includeSID: false, query: { __shutdown__: '1' } }),
215+
{ timeout: 1000, throwHttpErrors: false }
216+
);
217+
} catch (err) {}
224218
port++;
225219
}
226220
await misc.sleep(2000); // wait for 2 secs while server stops

src/misc.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,14 @@
77
*/
88

99
import { promises as fs } from 'fs';
10+
import got from 'got';
1011
import os from 'os';
1112
import qs from 'querystringify';
12-
import request from 'request';
1313

1414
export function sleep(ms) {
1515
return new Promise((resolve) => setTimeout(resolve, ms));
1616
}
1717

18-
export function processHTTPRequest(url, callback, options) {
19-
options = options || {};
20-
options.url = url;
21-
if (!options.headers) {
22-
options.headers = {
23-
'User-Agent': 'PlatformIO',
24-
};
25-
}
26-
console.info('processHTTPRequest', options);
27-
return request(options, (err, response, body) => {
28-
return callback(err, response, body);
29-
});
30-
}
31-
3218
export async function loadJSON(filePath) {
3319
try {
3420
await fs.access(filePath);
@@ -61,7 +47,7 @@ function uuid() {
6147
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
6248
}
6349

64-
export function reportError(err) {
50+
export async function reportError(err) {
6551
const data = {
6652
v: 1,
6753
tid: 'UA-1768265-13',
@@ -76,8 +62,9 @@ export function reportError(err) {
7662
if (process.env.PLATFORMIO_CALLER) {
7763
data['cd1'] = process.env.PLATFORMIO_CALLER;
7864
}
79-
request.post('https://www.google-analytics.com/collect', {
65+
await got.post('https://www.google-analytics.com/collect', {
8066
body: qs.stringify(data),
67+
timeout: 2000,
8168
});
8269
}
8370

src/proc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* the root directory of this source tree.
77
*/
88

9+
import { bootstrap } from 'global-agent';
910
import fs from 'fs';
1011
import path from 'path';
1112
import spawn from 'cross-spawn';
@@ -77,6 +78,10 @@ export function patchOSEnviron({ caller, extraPath, extraVars }) {
7778
// Configure NO_PROXY for PIO Home
7879
process.env.NO_PROXY =
7980
'127.0.0.1' + (process.env.NO_PROXY ? `,${process.env.NO_PROXY}` : '');
81+
if (process.env.HTTP_PROXY || process.env.HTTPS_PROXY) {
82+
process.env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE = '';
83+
bootstrap();
84+
}
8085
}
8186

8287
export function extendOSEnvironPath(name, items, prepend = true) {

0 commit comments

Comments
 (0)