From 582e3c2f8ecd09f2ebad5ff64937b46db92a3078 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Tue, 17 Dec 2019 23:00:56 +0200 Subject: [PATCH 1/9] Create `verbose` option --- api.js | 27 ++++++++++++++++++++++++--- cli.js | 15 +++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/api.js b/api.js index f8c32fc..346a239 100644 --- a/api.js +++ b/api.js @@ -10,10 +10,10 @@ async function init(browser, page, observer, options) { /* eslint-disable no-constant-condition, no-await-in-loop */ while (true) { - const result = await page.evaluate(() => { + const result = await page.evaluate(options => { const $ = document.querySelector.bind(document); - return { + let stats = { downloadSpeed: Number($('#speed-value').textContent), uploadSpeed: Number($('#upload-value').textContent), downloadUnit: $('#speed-units').textContent.trim(), @@ -22,7 +22,28 @@ async function init(browser, page, observer, options) { $('#speed-value.succeeded') && $('#upload-value.succeeded') ) }; - }); + + if (options.verbose) { + stats = { + ...stats, + latency: Number($('#latency-value').textContent), + bufferbloat: Number($('#bufferbloat-value').textContent), + latencyUnit: $('#latency-units').textContent.trim(), + bufferbloatUnit: $('#bufferbloat-units').textContent.trim(), + client: { + location: $('#user-location').textContent.trim(), + ip: $('#user-ip').textContent.trim(), + isp: $('#user-isp').textContent.trim() + }, + serverLocations: $('#server-locations').textContent.trim(), + isDone: stats.isDone && Boolean( + $('#latency-value.succeeded') && $('#bufferbloat-value.succeeded') + ) + }; + } + + return stats; + }, options); if (result.downloadSpeed > 0 && !equals(result, prevResult)) { observer.next(result); diff --git a/cli.js b/cli.js index 5b42135..a9aeb0b 100755 --- a/cli.js +++ b/cli.js @@ -14,6 +14,7 @@ const cli = meow(` Options --upload, -u Measure upload speed in addition to download speed + --verbose Get verbose logging on latency and request metadata Examples $ fast --upload > file && cat file @@ -24,10 +25,15 @@ const cli = meow(` upload: { type: 'boolean', alias: 'u' + }, + verbose: { + type: 'boolean' } } }); +cli.flags.upload = cli.flags.upload || cli.flags.verbose; + // Check connections dns.lookup('fast.com', error => { if (error && error.code === 'ENOTFOUND') { @@ -59,14 +65,19 @@ const speedText = () => const speed = () => speedText() + '\n\n'; function exit() { + const verboseLog = `Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\nServer(s): ${data.serverLocations}`; + if (process.stdout.isTTY) { - logUpdate(`\n\n ${speed()}`); + logUpdate(`\n\n ${speed()}\n${chalk.dim(verboseLog)}`); } else { let output = `${data.downloadSpeed} ${data.downloadUnit}`; if (cli.flags.upload) { output += `\n${data.uploadSpeed} ${data.uploadUnit}`; } + if (cli.flags.verbose) { + output += `\n${verboseLog}`; + } console.log(output); } @@ -89,7 +100,7 @@ if (process.stdout.isTTY) { (async () => { try { - await api({measureUpload: cli.flags.upload}).forEach(result => { + await api({measureUpload: cli.flags.upload, verbose: cli.flags.verbose}).forEach(result => { data = result; }); From 2288aadda803376b51c983d3e2407ae0e9bd74cb Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Tue, 17 Dec 2019 23:11:30 +0200 Subject: [PATCH 2/9] Fix linting --- cli.js | 8 ++++---- readme.md | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index a9aeb0b..5ea925f 100755 --- a/cli.js +++ b/cli.js @@ -63,20 +63,20 @@ const speedText = () => downloadColor(downloadSpeed()); const speed = () => speedText() + '\n\n'; +const getVerboseLog = () => `Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\nServer(s): ${data.serverLocations}`; function exit() { - const verboseLog = `Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\nServer(s): ${data.serverLocations}`; - if (process.stdout.isTTY) { - logUpdate(`\n\n ${speed()}\n${chalk.dim(verboseLog)}`); + logUpdate(`\n\n ${speed()}${cli.flags.verbose ? `\n${chalk.dim(getVerboseLog())}` : ''}`); } else { let output = `${data.downloadSpeed} ${data.downloadUnit}`; if (cli.flags.upload) { output += `\n${data.uploadSpeed} ${data.uploadUnit}`; } + if (cli.flags.verbose) { - output += `\n${verboseLog}`; + output += `\n${getVerboseLog()}`; } console.log(output); diff --git a/readme.md b/readme.md index b6d386c..285f763 100644 --- a/readme.md +++ b/readme.md @@ -27,6 +27,7 @@ $ fast --help Options --upload, -u Measure upload speed in addition to download speed + --verbose Get verbose logging on latency and request metadata Examples $ fast --upload > file && cat file From 2f5296954350051e222527bba3a6fc53e5e0823d Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 23 Dec 2019 21:08:53 +0200 Subject: [PATCH 3/9] Add latency logging --- cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 5ea925f..9b27fc7 100755 --- a/cli.js +++ b/cli.js @@ -63,7 +63,7 @@ const speedText = () => downloadColor(downloadSpeed()); const speed = () => speedText() + '\n\n'; -const getVerboseLog = () => `Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\nServer(s): ${data.serverLocations}`; +const getVerboseLog = () => `Latency: ${data.latency}${data.latencyUnit} (unloaded) ${data.bufferbloat}${data.bufferbloatUnit} (loaded)\nClient: ${data.client.location} ${data.client.ip} ${data.client.isp}\nServer(s): ${data.serverLocations}`; function exit() { if (process.stdout.isTTY) { From 8b895ad33b8e4f124ff44b2ef1277c0d87428f79 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Fri, 28 Aug 2020 15:59:23 +0300 Subject: [PATCH 4/9] Fix CR comments --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 9b27fc7..41cac06 100755 --- a/cli.js +++ b/cli.js @@ -63,11 +63,11 @@ const speedText = () => downloadColor(downloadSpeed()); const speed = () => speedText() + '\n\n'; -const getVerboseLog = () => `Latency: ${data.latency}${data.latencyUnit} (unloaded) ${data.bufferbloat}${data.bufferbloatUnit} (loaded)\nClient: ${data.client.location} ${data.client.ip} ${data.client.isp}\nServer(s): ${data.serverLocations}`; +const getVerboseLog = () => ` Latency: ${data.latency}${data.latencyUnit} ${chalk.dim('(unloaded)')} ${data.bufferbloat}${data.bufferbloatUnit} ${chalk.dim('(loaded)')}\n Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\n Server(s): ${data.serverLocations}`; function exit() { if (process.stdout.isTTY) { - logUpdate(`\n\n ${speed()}${cli.flags.verbose ? `\n${chalk.dim(getVerboseLog())}` : ''}`); + logUpdate(`\n\n ${speed()}${cli.flags.verbose ? `\n${getVerboseLog()}` : ''}`); } else { let output = `${data.downloadSpeed} ${data.downloadUnit}`; From 251067c1027ac19836ba7a7c8dafe533c3de4961 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Sun, 6 Sep 2020 22:02:04 +0300 Subject: [PATCH 5/9] Update latency in real time and apply review suggestions --- api.js | 5 ++--- cli.js | 19 +++++++++++++++---- readme.md | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/api.js b/api.js index 346a239..b9ce379 100644 --- a/api.js +++ b/api.js @@ -36,9 +36,8 @@ async function init(browser, page, observer, options) { isp: $('#user-isp').textContent.trim() }, serverLocations: $('#server-locations').textContent.trim(), - isDone: stats.isDone && Boolean( - $('#latency-value.succeeded') && $('#bufferbloat-value.succeeded') - ) + isLatencyDone: Boolean($('#latency-value.succeeded')), + isBufferbloatDone: Boolean($('#bufferbloat-value.succeeded')) }; } diff --git a/cli.js b/cli.js index 41cac06..e40a1ae 100755 --- a/cli.js +++ b/cli.js @@ -14,7 +14,7 @@ const cli = meow(` Options --upload, -u Measure upload speed in addition to download speed - --verbose Get verbose logging on latency and request metadata + --verbose Include info on latency and request metadata Examples $ fast --upload > file && cat file @@ -57,13 +57,24 @@ const uploadColor = string => (data.isDone ? chalk.green(string) : chalk.cyan(st const downloadColor = string => ((data.isDone || data.uploadSpeed) ? chalk.green(string) : chalk.cyan(string)); +const latencyColor = string => (data.isLatencyDone ? chalk.white(string) : chalk.cyan(string)); +const bufferbloatColor = string => (data.isBufferbloatDone ? chalk.white(string) : chalk.cyan(string)); + const speedText = () => cli.flags.upload ? `${downloadColor(downloadSpeed())} ${chalk.dim('/')} ${uploadColor(uploadSpeed())}` : downloadColor(downloadSpeed()); -const speed = () => speedText() + '\n\n'; -const getVerboseLog = () => ` Latency: ${data.latency}${data.latencyUnit} ${chalk.dim('(unloaded)')} ${data.bufferbloat}${data.bufferbloatUnit} ${chalk.dim('(loaded)')}\n Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\n Server(s): ${data.serverLocations}`; +const latencyText = () => `Latency: ${latencyColor(data.latency + data.latencyUnit)} ${chalk.dim('(unloaded)')} ${bufferbloatColor(data.bufferbloat + data.bufferbloatUnit)} ${chalk.dim('(loaded)')}`; + +const speed = () => { + let speedLog = speedText() + '\n\n'; + if (cli.flags.verbose) { + speedLog += ` ${latencyText()}\n`; + } + return speedLog; +} +const getVerboseLog = () => ` Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\n Servers: ${data.serverLocations}`; function exit() { if (process.stdout.isTTY) { @@ -76,7 +87,7 @@ function exit() { } if (cli.flags.verbose) { - output += `\n${getVerboseLog()}`; + output += `\n ${latencyText()}\n${getVerboseLog()}`; } console.log(output); diff --git a/readme.md b/readme.md index 285f763..aa14fa7 100644 --- a/readme.md +++ b/readme.md @@ -27,7 +27,7 @@ $ fast --help Options --upload, -u Measure upload speed in addition to download speed - --verbose Get verbose logging on latency and request metadata + --verbose Include info on latency and request metadata Examples $ fast --upload > file && cat file From 420bdd03a840c239fe503711fa24a0301de8a1b4 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Sun, 6 Sep 2020 22:34:45 +0300 Subject: [PATCH 6/9] Improve `--single-line` support --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 15e5e3a..0b557a8 100755 --- a/cli.js +++ b/cli.js @@ -89,12 +89,12 @@ const getVerboseLog = () => `${spacing(5)}Client: ${data.client.location} ${dat function exit() { if (process.stdout.isTTY) { - logUpdate(`${lineBreak(2)}${spacing(4)}${speed()}${cli.flags.verbose ? `\n${getVerboseLog()}` : ''}`); + logUpdate(`${lineBreak(2)}${spacing(4)}${speed()}${cli.flags.verbose ? `${lineBreak(1)}${getVerboseLog()}` : ''}`); } else { let output = `${data.downloadSpeed} ${data.downloadUnit}`; if (cli.flags.upload) { - output += `\n${data.uploadSpeed} ${data.uploadUnit}`; + output += `${cli.flags.singleLine ? ' / ' : '\n'}${data.uploadSpeed} ${data.uploadUnit}`; } if (cli.flags.verbose) { From dca16159e43d9891d4754143eb93f3264875a458 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Sun, 6 Sep 2020 22:37:56 +0300 Subject: [PATCH 7/9] Fix linting --- cli.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 0b557a8..084d35f 100755 --- a/cli.js +++ b/cli.js @@ -83,8 +83,10 @@ const speed = () => { if (cli.flags.verbose) { speedLog += `${cli.flags.singleLine ? '\n' : ''}${spacing(4)}${latencyText()}\n`; } + return speedLog; -} +}; + const getVerboseLog = () => `${spacing(5)}Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\n${spacing(4)}Servers: ${data.serverLocations}`; function exit() { From 460f59e7930b6092f3384e7e8b298a57b80fb866 Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Sun, 27 Sep 2020 18:45:36 +0700 Subject: [PATCH 8/9] move verbose log into one function --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 084d35f..602bbbb 100755 --- a/cli.js +++ b/cli.js @@ -87,7 +87,7 @@ const speed = () => { return speedLog; }; -const getVerboseLog = () => `${spacing(5)}Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\n${spacing(4)}Servers: ${data.serverLocations}`; +const getVerboseLog = () => `${spacing(4)}${latencyText()}\n${spacing(5)}Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\n${spacing(4)}Servers: ${data.serverLocations}`; function exit() { if (process.stdout.isTTY) { @@ -100,7 +100,7 @@ function exit() { } if (cli.flags.verbose) { - output += `\n${spacing(4)}${latencyText()}\n${getVerboseLog()}`; + output += `\n${getVerboseLog()}`; } console.log(output); From 316f528782b77b9cb101c813e224f9e741241ba0 Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Sun, 27 Sep 2020 18:51:24 +0700 Subject: [PATCH 9/9] add space according https://github.com/sindresorhus/fast-cli/pull/41#issuecomment-689631809 --- cli.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli.js b/cli.js index 602bbbb..38a1fd4 100755 --- a/cli.js +++ b/cli.js @@ -99,6 +99,8 @@ function exit() { output += `${cli.flags.singleLine ? ' / ' : '\n'}${data.uploadSpeed} ${data.uploadUnit}`; } + output += "\n" + if (cli.flags.verbose) { output += `\n${getVerboseLog()}`; }