Skip to content

Commit

Permalink
🖌️ Code stylization (more eslint rules)
Browse files Browse the repository at this point in the history
  • Loading branch information
karelkryda committed May 11, 2022
1 parent 02c69f7 commit 79ef119
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 61 deletions.
83 changes: 50 additions & 33 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,55 @@
"rules": {
"no-console": "off",
"linebreak-style": "off",
"camelcase": [
"warn",
{
"properties": "never",
"ignoreImports": true
}
],
"no-unused-vars": [
"warn",
{
"args": "none"
}
],
"quotes": [
"error",
"double",
{
"allowTemplateLiterals": true
}
],
"object-curly-spacing": [
"error",
"always"
],
"keyword-spacing": [
"error",
{
"before": true
}
],
"space-before-blocks": [
"error"
]
"camelcase": [ "warn", {
"properties": "never",
"ignoreImports": true
}],
"no-unused-vars": [ "warn", {
"args": "none"
}],
"quotes": [ "error", "double", {
"allowTemplateLiterals": true
}],
"array-bracket-spacing": [ "warn", "always", {
"singleValue": true,
"objectsInArrays": false,
"arraysInArrays": false
}],
"space-before-function-paren": [ "error", {
"anonymous": "always",
"named": "never",
"asyncArrow": "always"
}],
"object-curly-spacing": [ "error", "always" ],
"object-property-newline": "error",
"brace-style": "error",
"no-var": "error",
"keyword-spacing": [ "error", {
"before": true
}],
"arrow-spacing": "warn",
"no-constant-condition": [ "error", {
"checkLoops": false
}],
"space-before-blocks": [ "error" ],
"no-multiple-empty-lines": [ "warn", {
"max": 1,
"maxBOF": 0
}],
"lines-between-class-members": [ "warn", "always", {
"exceptAfterSingleLine": true
}],
"no-unneeded-ternary": "error",
"array-bracket-newline": [ "error", "consistent" ],
"eol-last": [ "error", "always" ],
"comma-dangle": [ "warn", "only-multiline" ],
"no-empty": [ "error", {
"allowEmptyCatch": true
}],
"one-var": [ "error", "never" ],
"max-statements-per-line": [ "error", {
"max": 1
}]
}
}
20 changes: 10 additions & 10 deletions src/Cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { createRequest } from "./helpers/UrllibHelper";
import { avg, convertUnits, getDistance, getQuartile, jitter, sortObject } from "./Utils";
import { SpeedUnits } from "./index";


export class Cloudflare {
private readonly options: CloudflareOptions;
private readonly result: CloudflareResult = {} as CloudflareResult;
Expand All @@ -14,18 +13,19 @@ export class Cloudflare {
private clientLon: number;
private readonly tests = {
download: [
[101000, 10],
[1001000, 8],
[10001000, 6],
[25001000, 4],
[100001000, 1]
[ 101000, 10 ],
[ 1001000, 8 ],
[ 10001000, 6 ],
[ 25001000, 4 ],
[ 100001000, 1 ]
],
upload: [
[11000, 10],
[101000, 10],
[1001000, 8]
[ 11000, 10 ],
[ 101000, 10 ],
[ 1001000, 8 ]
]
}

private servers = {};
private bestServer;

Expand Down Expand Up @@ -172,7 +172,7 @@ export class Cloudflare {
times.push(total);
});
}
return [Number(avg(times).toFixed(3)), Number(jitter(times).toFixed(3))];
return [ Number(avg(times).toFixed(3)), Number(jitter(times).toFixed(3)) ];
} catch {
throw new Error("An error occurred while measuring latency.");
}
Expand Down
13 changes: 6 additions & 7 deletions src/Speedtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import { convertUnits, getDistance, jitter, sortObject, sum } from "./Utils";
import { HTTPUploaderData } from "./helpers/HTTPUploaderData";
import { SpeedUnits } from "./index";


export class Speedtest {
private readonly options: SpeedtestOptions;
private readonly result: SpeedtestResult = {} as SpeedtestResult;

private testConfig;
private clientLat: number;
private clientLon: number;
private readonly uploadSizes = [32768, 65536, 131072, 262144, 524288, 1048576, 7340032];
private readonly uploadSizes = [ 32768, 65536, 131072, 262144, 524288, 1048576, 7340032 ];
private servers = {};
private fastestServer;

Expand Down Expand Up @@ -108,8 +107,8 @@ export class Speedtest {

const sizes = {
"upload": this.uploadSizes.slice(uploadRatio - 1, this.uploadSizes.length),
"download": [350, 500, 750, 1000, 1500, 2000, 2500,
3000, 3500, 4000]
"download": [ 350, 500, 750, 1000, 1500, 2000, 2500,
3000, 3500, 4000 ]
};

const uploadSizeCount = sizes["upload"].length;
Expand Down Expand Up @@ -176,7 +175,7 @@ export class Speedtest {
const distance = getDistance(this.clientLat, this.clientLon, Number(server.$.lat), Number(server.$.lon));

server.$.distance = distance;
this.servers[distance] = [server.$];
this.servers[distance] = [ server.$ ];
});
} catch (e) {
throw new Error("Error getting server list");
Expand Down Expand Up @@ -264,7 +263,7 @@ export class Speedtest {
const requestCount = urls.length;
const requests = [];
urls.forEach((url, i) => {
requests.push([url, this.options.secure, i.toString(), (this.options.wait) ? this.options.timeout : this.testConfig.length.download]);
requests.push([ url, this.options.secure, i.toString(), (this.options.wait) ? this.options.timeout : this.testConfig.length.download ]);
});

const maxThreads = this.testConfig.threads.download;
Expand Down Expand Up @@ -340,7 +339,7 @@ export class Speedtest {
const data = new HTTPUploaderData(size);
data.preAllocate();

requests.push([this.fastestServer.url, headers, this.options.secure, data.read(size), i.toString(), (this.options.wait) ? this.options.timeout : this.testConfig.length.upload, data.total]);
requests.push([ this.fastestServer.url, headers, this.options.secure, data.read(size), i.toString(), (this.options.wait) ? this.options.timeout : this.testConfig.length.upload, data.total ]);
});

const maxThreads = this.testConfig.threads.upload;
Expand Down
1 change: 0 additions & 1 deletion src/UniversalSpeedTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export class UniversalSpeedtest {
}
}


interface UniversalSpeedTestOptions {
/** Display debug messages. */
debug?: boolean,
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ export function sum(arr): number {
export function convertUnits(actualUnit: string, newUnit: string, speed: number): number {
const convert = configureMeasurements(allMeasures);
return convert(speed).from(actualUnit.slice(0, -2)).to(newUnit.slice(0, -2));
}
}
2 changes: 1 addition & 1 deletion src/helpers/HTTPDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ export class HTTPDownloader {

return results;
}
}
}
2 changes: 1 addition & 1 deletion src/helpers/HTTPUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export class HTTPUploader {
await this.request;
return sum(this.total);
}
}
}
4 changes: 2 additions & 2 deletions src/helpers/HTTPUploaderData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class HTTPUploaderData {
constructor(length) {
this.length = length;
this._data = null;
this._total = [0];
this._total = [ 0 ];
}

/**
Expand Down Expand Up @@ -43,4 +43,4 @@ export class HTTPUploaderData {
this._total.push(chunk.length);
return chunk;
}
}
}
2 changes: 1 addition & 1 deletion src/thread_workers/download_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const { workerData } = require("worker_threads");

// eslint-disable-next-line @typescript-eslint/no-var-requires
require("ts-node").register();
require(path.resolve(__dirname, workerData.path));
require(path.resolve(__dirname, workerData.path));
2 changes: 1 addition & 1 deletion src/thread_workers/download_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ if (wait || (((performance.now() - startTime) / 1000) <= timeout)) {
thread.run().then(result => {
parentPort.postMessage(result);
});
} else parentPort.postMessage([0]);
} else parentPort.postMessage([ 0 ]);
2 changes: 1 addition & 1 deletion src/thread_workers/upload_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const { workerData } = require("worker_threads");

// eslint-disable-next-line @typescript-eslint/no-var-requires
require("ts-node").register();
require(path.resolve(__dirname, workerData.path));
require(path.resolve(__dirname, workerData.path));
2 changes: 1 addition & 1 deletion tests/Cloudflare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ describe("Cloudflare test", () => {
expect(result.uploadSpeed).not.to.be.undefined;
expect(result.downloadSpeed).to.be.above(20);
}).timeout(120000);
});
});
2 changes: 1 addition & 1 deletion tests/Speedtest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ describe("Speedtest test", () => {
expect(result.uploadSpeed).to.be.undefined;
expect(result.downloadSpeed).to.be.above(20);
}).timeout(120000);
});
});

0 comments on commit 79ef119

Please sign in to comment.