@depup/systeminformation 5.31.4-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,259 @@
1
+ 'use strict';
2
+ // @ts-check
3
+ // ==================================================================================
4
+ // internet.js
5
+ // ----------------------------------------------------------------------------------
6
+ // Description: System Information - library
7
+ // for Node.js
8
+ // Copyright: (c) 2014 - 2026
9
+ // Author: Sebastian Hildebrandt
10
+ // ----------------------------------------------------------------------------------
11
+ // License: MIT
12
+ // ==================================================================================
13
+ // 12. Internet
14
+ // ----------------------------------------------------------------------------------
15
+
16
+ const util = require('./util');
17
+
18
+ const _platform = process.platform;
19
+
20
+ const _linux = _platform === 'linux' || _platform === 'android';
21
+ const _darwin = _platform === 'darwin';
22
+ const _windows = _platform === 'win32';
23
+ const _freebsd = _platform === 'freebsd';
24
+ const _openbsd = _platform === 'openbsd';
25
+ const _netbsd = _platform === 'netbsd';
26
+ const _sunos = _platform === 'sunos';
27
+
28
+ // --------------------------
29
+ // check if external site is available
30
+
31
+ function inetChecksite(url, callback) {
32
+ return new Promise((resolve) => {
33
+ process.nextTick(() => {
34
+ let result = {
35
+ url: url,
36
+ ok: false,
37
+ status: 404,
38
+ ms: null
39
+ };
40
+ if (typeof url !== 'string') {
41
+ if (callback) {
42
+ callback(result);
43
+ }
44
+ return resolve(result);
45
+ }
46
+ let urlSanitized = '';
47
+ const s = util.sanitizeShellString(url, true);
48
+ const l = util.mathMin(s.length, 2000);
49
+ for (let i = 0; i <= l; i++) {
50
+ if (s[i] !== undefined) {
51
+ try {
52
+ s[i].__proto__.toLowerCase = util.stringToLower;
53
+ } catch {
54
+ Object.setPrototypeOf(s[i], util.stringObj);
55
+ }
56
+
57
+ const sl = s[i].toLowerCase();
58
+ if (sl && sl[0] && !sl[1] && sl[0].length === 1) {
59
+ urlSanitized = urlSanitized + sl[0];
60
+ }
61
+ }
62
+ }
63
+ result.url = urlSanitized;
64
+ try {
65
+ if (urlSanitized && !util.isPrototypePolluted()) {
66
+ try {
67
+ urlSanitized.__proto__.startsWith = util.stringStartWith;
68
+ } catch {
69
+ Object.setPrototypeOf(urlSanitized, util.stringObj);
70
+ }
71
+
72
+ if (
73
+ urlSanitized.startsWith('file:') ||
74
+ urlSanitized.startsWith('gopher:') ||
75
+ urlSanitized.startsWith('telnet:') ||
76
+ urlSanitized.startsWith('mailto:') ||
77
+ urlSanitized.startsWith('news:') ||
78
+ urlSanitized.startsWith('nntp:')
79
+ ) {
80
+ if (callback) {
81
+ callback(result);
82
+ }
83
+ return resolve(result);
84
+ }
85
+
86
+ util.checkWebsite(urlSanitized).then((res) => {
87
+ result.status = res.statusCode;
88
+ result.ok = res.statusCode >= 200 && res.statusCode <= 399;
89
+ result.ms = result.ok ? res.time : null;
90
+ if (callback) {
91
+ callback(result);
92
+ }
93
+ resolve(result);
94
+ });
95
+ } else {
96
+ if (callback) {
97
+ callback(result);
98
+ }
99
+ resolve(result);
100
+ }
101
+ } catch {
102
+ if (callback) {
103
+ callback(result);
104
+ }
105
+ resolve(result);
106
+ }
107
+ });
108
+ });
109
+ }
110
+
111
+ exports.inetChecksite = inetChecksite;
112
+
113
+ // --------------------------
114
+ // check inet latency
115
+
116
+ function inetLatency(host, callback) {
117
+ // fallback - if only callback is given
118
+ if (util.isFunction(host) && !callback) {
119
+ callback = host;
120
+ host = '';
121
+ }
122
+
123
+ host = host || '8.8.8.8';
124
+
125
+ return new Promise((resolve) => {
126
+ process.nextTick(() => {
127
+ if (typeof host !== 'string') {
128
+ if (callback) {
129
+ callback(null);
130
+ }
131
+ return resolve(null);
132
+ }
133
+ let hostSanitized = '';
134
+ const s = (util.isPrototypePolluted() ? '8.8.8.8' : util.sanitizeShellString(host, true)).trim();
135
+ const l = util.mathMin(s.length, 2000);
136
+ for (let i = 0; i <= l; i++) {
137
+ if (!(s[i] === undefined)) {
138
+ try {
139
+ s[i].__proto__.toLowerCase = util.stringToLower;
140
+ } catch {
141
+ Object.setPrototypeOf(s[i], util.stringObj);
142
+ }
143
+
144
+ const sl = s[i].toLowerCase();
145
+ if (sl && sl[0] && !sl[1]) {
146
+ hostSanitized = hostSanitized + sl[0];
147
+ }
148
+ }
149
+ }
150
+ try {
151
+ hostSanitized.__proto__.startsWith = util.stringStartWith;
152
+ } catch {
153
+ Object.setPrototypeOf(hostSanitized, util.stringObj);
154
+ }
155
+
156
+ if (
157
+ hostSanitized.startsWith('file:') ||
158
+ hostSanitized.startsWith('gopher:') ||
159
+ hostSanitized.startsWith('telnet:') ||
160
+ hostSanitized.startsWith('mailto:') ||
161
+ hostSanitized.startsWith('news:') ||
162
+ hostSanitized.startsWith('nntp:')
163
+ ) {
164
+ if (callback) {
165
+ callback(null);
166
+ }
167
+ return resolve(null);
168
+ }
169
+ let params;
170
+ if (_linux || _freebsd || _openbsd || _netbsd || _darwin) {
171
+ if (_linux) {
172
+ params = ['-c', '2', '-w', '3', hostSanitized];
173
+ }
174
+ if (_freebsd || _openbsd || _netbsd) {
175
+ params = ['-c', '2', '-t', '3', hostSanitized];
176
+ }
177
+ if (_darwin) {
178
+ params = ['-c2', '-t3', hostSanitized];
179
+ }
180
+ util.execSafe('ping', params).then((stdout) => {
181
+ let result = null;
182
+ if (stdout) {
183
+ const lines = stdout
184
+ .split('\n')
185
+ .filter((line) => line.indexOf('rtt') >= 0 || line.indexOf('round-trip') >= 0 || line.indexOf('avg') >= 0)
186
+ .join('\n');
187
+
188
+ const line = lines.split('=');
189
+ if (line.length > 1) {
190
+ const parts = line[1].split('/');
191
+ if (parts.length > 1) {
192
+ result = parseFloat(parts[1]);
193
+ }
194
+ }
195
+ }
196
+ if (callback) {
197
+ callback(result);
198
+ }
199
+ resolve(result);
200
+ });
201
+ }
202
+ if (_sunos) {
203
+ const params = ['-s', '-a', hostSanitized, '56', '2'];
204
+ const filt = 'avg';
205
+ util.execSafe('ping', params, { timeout: 3000 }).then((stdout) => {
206
+ let result = null;
207
+ if (stdout) {
208
+ const lines = stdout
209
+ .split('\n')
210
+ .filter((line) => line.indexOf(filt) >= 0)
211
+ .join('\n');
212
+ const line = lines.split('=');
213
+ if (line.length > 1) {
214
+ const parts = line[1].split('/');
215
+ if (parts.length > 1) {
216
+ result = parseFloat(parts[1].replace(',', '.'));
217
+ }
218
+ }
219
+ }
220
+ if (callback) {
221
+ callback(result);
222
+ }
223
+ resolve(result);
224
+ });
225
+ }
226
+ if (_windows) {
227
+ let result = null;
228
+ try {
229
+ const params = [hostSanitized, '-n', '1'];
230
+ util.execSafe('ping', params, util.execOptsWin).then((stdout) => {
231
+ if (stdout) {
232
+ const lines = stdout.split('\r\n');
233
+ lines.shift();
234
+ lines.forEach((line) => {
235
+ if ((line.toLowerCase().match(/ms/g) || []).length === 3) {
236
+ let l = line.replace(/ +/g, ' ').split(' ');
237
+ if (l.length > 6) {
238
+ result = parseFloat(l[l.length - 1]);
239
+ }
240
+ }
241
+ });
242
+ }
243
+ if (callback) {
244
+ callback(result);
245
+ }
246
+ resolve(result);
247
+ });
248
+ } catch {
249
+ if (callback) {
250
+ callback(result);
251
+ }
252
+ resolve(result);
253
+ }
254
+ }
255
+ });
256
+ });
257
+ }
258
+
259
+ exports.inetLatency = inetLatency;