Skip to content

Commit

Permalink
CS
Browse files Browse the repository at this point in the history
  • Loading branch information
dominickolbe committed Oct 11, 2020
1 parent 8c760da commit 89f8b67
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node

import ora from 'ora';
import chalk from 'chalk';
import speedTest from 'speedtest-net';
import ora from "ora";
import speedTest from "speedtest-net";

const spinner = new ora();
const multiplier = 1 / 8;
Expand All @@ -15,14 +14,24 @@ const stats = {
ping: null,
};

const render = finished => {
const placeholder = '...';
const render = (finished) => {
const placeholder = "...";
const ping = stats.ping ? `${stats.ping}ms` : placeholder;
const download = stats.download ? `${(stats.download).toFixed(2)} Mbit/s (${(stats.download * multiplier).toFixed(2)} MB/s)` : placeholder;
const upload = stats.upload ? `${(stats.upload).toFixed(2)} Mbit/s (${(stats.upload * multiplier).toFixed(2)} MB/s)` : placeholder;
const info = finished ? `Your IP: ${stats.client.ip} | Testserver: ${stats.server.host} in ${stats.server.location} ${stats.server.country}` : '';

spinner.text = `${finished ? 'Test done': 'In progress ...'}
const download = stats.download
? `${stats.download.toFixed(2)} Mbit/s (${(
stats.download * multiplier
).toFixed(2)} MB/s)`
: placeholder;
const upload = stats.upload
? `${stats.upload.toFixed(2)} Mbit/s (${(stats.upload * multiplier).toFixed(
2
)} MB/s)`
: placeholder;
const info = finished
? `Your IP: ${stats.client.ip} | Testserver: ${stats.server.host} in ${stats.server.location} ${stats.server.country}`
: "";

spinner.text = `${finished ? "Test done" : "In progress ..."}
Ping: ${ping}
Download: ${download}
Expand All @@ -36,40 +45,40 @@ render();

const test = speedTest({ maxTime: 20000 });

test.on('downloadspeedprogress', speed => {
test.on("downloadspeedprogress", (speed) => {
stats.download = speed;
render();
});

test.on('uploadspeedprogress', speed => {
test.on("uploadspeedprogress", (speed) => {
stats.upload = speed;
render();
});

test.once('testserver', server => {
test.once("testserver", (server) => {
stats.ping = Math.round(server.bestPing);
render();
});

test.on('downloadspeed', speed => {
test.on("downloadspeed", (speed) => {
stats.download = speed;
render();
});

test.on('uploadspeed', speed => {
test.on("uploadspeed", (speed) => {
stats.upload = speed;
render();
});

test.once('data', data => {
test.once("data", (data) => {
stats.client = data.client;
stats.server = data.server;
render(true);
spinner.succeed();
process.exit();
});

test.on('error', err => {
test.on("error", (err) => {
spinner.fail();
console.error(err);
process.exit(1);
Expand Down

0 comments on commit 89f8b67

Please sign in to comment.