Skip to content

Commit 33d629c

Browse files
authored
Merge pull request #613 from scratchfoundation/fix/telemetry-replace-nets
fix(deps): replace nets with Electron net.request in telemetry
2 parents b55b7ca + 1a584fd commit 33d629c

3 files changed

Lines changed: 44 additions & 25 deletions

File tree

package-lock.json

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
"minilog": "3.1.0",
6767
"minimist": "1.2.8",
6868
"mkdirp": "1.0.4",
69-
"nets": "3.2.0",
7069
"postcss-import": "12.0.1",
7170
"postcss-loader": "4.3.0",
7271
"postcss-simple-vars": "5.0.2",

src/main/telemetry/TelemetryClient.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
1+
import {app, net} from 'electron';
12
import ElectronStore from 'electron-store';
2-
import nets from 'nets';
33
import * as os from 'os';
44
import {
55
v1 as uuidv1, // semi-persistent client ID
66
v4 as uuidv4 // random ID
77
} from 'uuid';
88

9+
/**
10+
* Minimal `nets`-shaped wrapper around Electron's `net.request`. Calls
11+
* `callback(err)` on transport or response-stream error, or
12+
* `callback(null, {statusCode})` when the response is complete. The
13+
* response body is drained and discarded since this client only cares
14+
* about status codes. The callback fires at most once, so callers can
15+
* rely on it as a single completion seam.
16+
*
17+
* Waits for `app.whenReady()` before dispatching, because `net.request`
18+
* throws if used pre-ready and the TelemetryClient constructor may
19+
* schedule requests before that point.
20+
* @param {object} opts - {method, url, headers, body}
21+
* @param {function(Error?, {statusCode: number}=): void} callback - completion callback
22+
*/
23+
const httpRequest = (opts, callback) => {
24+
let settled = false;
25+
const done = (err, res) => {
26+
if (settled) return;
27+
settled = true;
28+
callback(err, res);
29+
};
30+
app.whenReady().then(() => {
31+
const request = net.request({method: opts.method, url: opts.url, headers: opts.headers});
32+
request.on('response', response => {
33+
response.on('data', () => {}); // drain so 'end' fires
34+
response.on('end', () => done(null, {statusCode: response.statusCode}));
35+
response.on('error', err => done(err));
36+
response.on('aborted', () => done(new Error('response aborted')));
37+
});
38+
request.on('error', err => done(err));
39+
if (opts.body) {
40+
request.write(opts.body);
41+
}
42+
request.end();
43+
})
44+
.catch(err => done(err));
45+
};
46+
947
/**
1048
* Basic telemetry event data. These fields are filled automatically by the `addEvent` call.
1149
* @typedef {object} BasicTelemetryEvent
@@ -17,15 +55,9 @@ import {
1755
*/
1856

1957
/**
20-
* Default telemetry service URLs
21-
*/
22-
const TelemetryServerURL = Object.freeze({
23-
staging: 'http://scratch-telemetry-staging.us-east-1.elasticbeanstalk.com/',
24-
production: 'https://telemetry.scratch.mit.edu/'
25-
});
26-
const DefaultServerURL = (
27-
process.env.NODE_ENV === 'production' ? TelemetryServerURL.production : TelemetryServerURL.staging
28-
);
58+
* Default telemetry service URL. Production and staging both target the same endpoint.
59+
*/
60+
const DefaultServerURL = 'https://telemetry.scratch.mit.edu/';
2961

3062
/**
3163
* Default name for persistent configuration & queue storage
@@ -260,7 +292,7 @@ class TelemetryClient {
260292
const packetInfo = this._packetQueue.shift();
261293
++packetInfo.attempts;
262294
const packet = packetInfo.packet;
263-
nets({
295+
httpRequest({
264296
body: JSON.stringify(packet),
265297
headers: {'Content-Type': 'application/json'},
266298
method: 'POST',
@@ -288,7 +320,7 @@ class TelemetryClient {
288320
* Check if the telemetry service is available
289321
*/
290322
_updateNetworkStatus () {
291-
nets({
323+
httpRequest({
292324
method: 'GET',
293325
url: this._serverURL
294326
}, (err, res) => {

0 commit comments

Comments
 (0)