-
Notifications
You must be signed in to change notification settings - Fork 33
/
app.js
99 lines (91 loc) · 3.15 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const http = require('http');
const https = require('https');
let { URL, Url } = require('url');
/**
* Use the default Node URL Class if found i.e. Inside a Node environment
* to allow http and https, otherwise use the Url consturctor for browser environments
* strictly limited to https for secure connections
*/
URL = URL ? URL : Url;
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+`-=[]{}|;':,./<>?";
class NetworkSpeedCheck {
/**
* Function to check download speed
* @param {String} baseUrl {Required} The url to which the request should be made
* @param {Number} fileSizeInBytes {Required} The size (in bytes) of the file to be downloaded
* @returns {Object}
*/
_protocol (url) {
var u = new URL(url)
return u.protocol === 'http:' ? http : https
}
checkDownloadSpeed(baseUrl, fileSizeInBytes) {
this.validateDownloadSpeedParams(baseUrl, fileSizeInBytes)
let startTime;
let protocol = this._protocol(baseUrl)
return new Promise((resolve, _) => {
return protocol.get(baseUrl, response => {
response.once('data', () => {
startTime = new Date().getTime();
});
response.once('end', () => {
const endTime = new Date().getTime();
const duration = (endTime - startTime) / 1000;
// Convert bytes into bits by multiplying with 8
const bitsLoaded = fileSizeInBytes * 8;
const bps = (bitsLoaded / duration).toFixed(2);
const kbps = (bps / 1000).toFixed(2);
const mbps = (kbps / 1000).toFixed(2);
resolve({ bps, kbps, mbps });
});
});
}).catch(error => {
throw new Error(error);
});
}
checkUploadSpeed(options, fileSizeInBytes = 2000000) {
let startTime;
const defaultData = this.generateTestData(fileSizeInBytes / 1000);
const data = JSON.stringify({ defaultData });
return new Promise((resolve, reject) => {
let req = http.request(options, res => {
res.setEncoding("utf8");
res.on('data', () => {});
res.on("end", () => {
const endTime = new Date().getTime();
const duration = (endTime - startTime) / 1000;
const bitsLoaded = fileSizeInBytes * 8;
const bps = (bitsLoaded / duration).toFixed(2);
const kbps = (bps / 1000).toFixed(2);
const mbps = (kbps / 1000).toFixed(2);
resolve({ bps, kbps, mbps });
});
});
startTime = new Date().getTime();
req.on('error', error => {
reject(error)
});
req.write(data)
req.end()
})
}
validateDownloadSpeedParams(baseUrl, fileSizeInBytes) {
if (typeof baseUrl !== 'string') {
throw new Error('baseUrl must be a string')
}
if (typeof fileSizeInBytes !== 'number') {
throw new Error('fileSizeInBytes must be a number')
}
return
}
generateTestData(sizeInKmb) {
const iterations = sizeInKmb * 1000; //get byte count
let result = '';
for (var index = 0; index < iterations; index++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
}
module.exports = NetworkSpeedCheck;