Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ let speedtest = new FastSpeedtest({
https: true, // default: true
urlCount: 5, // default: 5
bufferSize: 8, // default: 8
unit: FastSpeedtest.UNITS.Mbps // default: Bps
unit: FastSpeedtest.UNITS.Mbps, // default: Bps
proxy: 'http://optional:auth@my-proxy:123' // default: undefined
});

speedtest.getSpeed().then(s => {
Expand Down
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"eslint-plugin-import": "^2.14.0",
"mocha": "^5.2.0",
"release-it": "^9.8.1"
},
"dependencies": {
"https-proxy-agent": "^2.2.2"
}
}
21 changes: 17 additions & 4 deletions src/Api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const https = require('https');
const http = require('http');
const HttpsProxyAgent = require('https-proxy-agent');
const url = require('url');
const Timer = require('./Timer');
const ApiError = require('./ApiError');

Expand Down Expand Up @@ -28,6 +30,10 @@ class Api {
throw new Error('Invalid unit');
}

if (options.proxy) {
this.proxy = new HttpsProxyAgent(options.proxy);
}

this.token = options.token;
this.verbose = options.verbose || false;
this.timeout = options.timeout || DEFAULT_SPEEDTEST_TIMEOUT;
Expand Down Expand Up @@ -59,12 +65,12 @@ class Api {
* Get data from the specified URL
*
* @async
* @param {string} url The URL to download from
* @param {string} options The http/s get options to download from
* @return {Promise} The request and response from the URL
*/
async get(url) {
async get(options) {
return new Promise((resolve, reject) => {
const request = (this.https ? https : http).get(url, (response) => {
const request = (this.https ? https : http).get(options, (response) => {
if (response.headers['content-type'].includes('json')) {
response.setEncoding('utf8');
let rawData = '';
Expand Down Expand Up @@ -102,13 +108,20 @@ class Api {
try {
const targets = [];
while (targets.length < this.urlCount) {
const target = `http${this.https ? 's' : ''}://api.fast.com/netflix/speedtest?https=${this.https ? 'true' : 'false'}&token=${this.token}&urlCount=${this.urlCount - targets.length}`;
const options = url.parse(target);
if (this.proxy) options.agent = this.proxy;
/* eslint-disable no-await-in-loop */
const { response } = await this.get(`http${this.https ? 's' : ''}://api.fast.com/netflix/speedtest?https=${this.https ? 'true' : 'false'}&token=${this.token}&urlCount=${this.urlCount - targets.length}`);
const { response } = await this.get(options);
/* eslint-enable no-await-in-loop */
if (response.statusCode !== 200) {
if (response.statusCode === 403) {
throw new ApiError({ code: ApiError.CODES.BAD_TOKEN });
}
if (response.statusCode === 407) {
throw new ApiError({ code: ApiError.CODES.PROXY_NOT_AUTHENTICATED });
}
console.log(response.statusCode);
throw new ApiError({ code: ApiError.CODES.UNKNOWN });
}
targets.push(...response.data);
Expand Down
1 change: 1 addition & 0 deletions src/ApiError.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ApiError.CODES = {
BAD_TOKEN: 'Unknown app token',
UNREACHABLE_HTTPS_API: 'Fast api is unreachable with https, try with http',
UNREACHABLE_HTTP_API: 'Fast api is unreachable, check your network connection',
PROXY_NOT_AUTHENTICATED: 'Failed to authenticate with provided proxy credentials',
UNKNOWN: 'Unknown error',
};

Expand Down
4 changes: 3 additions & 1 deletion src/cli.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const args = process.argv.slice(2);
if (args.includes('-h') || args.includes('--help')) {
console.log([
'fast-speedtest - speed test powered by fast.com',
'usage: fast-speedtest token [-v, --verbose] [-r, --raw] [-n, --no-https] [-t, --timeout timeout] [-c, --count url-count] [-b, --buffer buffer-size] [-u, --unit output-unit]',
'usage: fast-speedtest token [-v, --verbose] [-r, --raw] [-n, --no-https] [-t, --timeout timeout] [-c, --count url-count] [-b, --buffer buffer-size] [-u, --unit output-unit] [-p, --proxy proxy]',
].join('\n'));
process.exit(0);
}
Expand Down Expand Up @@ -42,6 +42,7 @@ const timeout = getArgParam('-t', '--timeout');
const urlCount = getArgParam('-c', '--count');
const bufferSize = getArgParam('-b', '--buffer');
const unitName = getArgParam('-u', '--unit');
const proxy = getArgParam('-p', '--proxy');
if (unitName && !(unitName in Api.UNITS)) {
throw new Error(`Unit not valide, must be one of ${Object.keys(Api.UNITS)}`);
}
Expand All @@ -55,6 +56,7 @@ const api = new Api({
bufferSize,
https,
unit,
proxy,
});

api.getSpeed().then((s) => {
Expand Down