@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.
package/lib/usb.js ADDED
@@ -0,0 +1,313 @@
1
+ 'use strict';
2
+ // @ts-check
3
+ // ==================================================================================
4
+ // usb.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
+ // 16. usb
14
+ // ----------------------------------------------------------------------------------
15
+
16
+ const exec = require('child_process').exec;
17
+ const util = require('./util');
18
+
19
+ let _platform = process.platform;
20
+
21
+ const _linux = _platform === 'linux' || _platform === 'android';
22
+ const _darwin = _platform === 'darwin';
23
+ const _windows = _platform === 'win32';
24
+ const _freebsd = _platform === 'freebsd';
25
+ const _openbsd = _platform === 'openbsd';
26
+ const _netbsd = _platform === 'netbsd';
27
+ const _sunos = _platform === 'sunos';
28
+
29
+ function getLinuxUsbType(type, name) {
30
+ let result = type;
31
+ const str = (name + ' ' + type).toLowerCase();
32
+ if (str.indexOf('camera') >= 0) {
33
+ result = 'Camera';
34
+ } else if (str.indexOf('hub') >= 0) {
35
+ result = 'Hub';
36
+ } else if (str.indexOf('keybrd') >= 0) {
37
+ result = 'Keyboard';
38
+ } else if (str.indexOf('keyboard') >= 0) {
39
+ result = 'Keyboard';
40
+ } else if (str.indexOf('mouse') >= 0) {
41
+ result = 'Mouse';
42
+ } else if (str.indexOf('stora') >= 0) {
43
+ result = 'Storage';
44
+ } else if (str.indexOf('microp') >= 0) {
45
+ result = 'Microphone';
46
+ } else if (str.indexOf('headset') >= 0) {
47
+ result = 'Audio';
48
+ } else if (str.indexOf('audio') >= 0) {
49
+ result = 'Audio';
50
+ }
51
+
52
+ return result;
53
+ }
54
+
55
+ function parseLinuxUsb(usb) {
56
+ const result = {};
57
+ const lines = usb.split('\n');
58
+ if (lines && lines.length && lines[0].indexOf('Device') >= 0) {
59
+ const parts = lines[0].split(' ');
60
+ result.bus = parseInt(parts[0], 10);
61
+ if (parts[2]) {
62
+ result.deviceId = parseInt(parts[2], 10);
63
+ } else {
64
+ result.deviceId = null;
65
+ }
66
+ } else {
67
+ result.bus = null;
68
+ result.deviceId = null;
69
+ }
70
+ const idVendor = util.getValue(lines, 'idVendor', ' ', true).trim();
71
+ let vendorParts = idVendor.split(' ');
72
+ vendorParts.shift();
73
+ const vendor = vendorParts.join(' ');
74
+
75
+ const idProduct = util.getValue(lines, 'idProduct', ' ', true).trim();
76
+ let productParts = idProduct.split(' ');
77
+ productParts.shift();
78
+ const product = productParts.join(' ');
79
+
80
+ const interfaceClass = util.getValue(lines, 'bInterfaceClass', ' ', true).trim();
81
+ let interfaceClassParts = interfaceClass.split(' ');
82
+ interfaceClassParts.shift();
83
+ const usbType = interfaceClassParts.join(' ');
84
+
85
+ const iManufacturer = util.getValue(lines, 'iManufacturer', ' ', true).trim();
86
+ let iManufacturerParts = iManufacturer.split(' ');
87
+ iManufacturerParts.shift();
88
+ const manufacturer = iManufacturerParts.join(' ');
89
+
90
+ const iSerial = util.getValue(lines, 'iSerial', ' ', true).trim();
91
+ let iSerialParts = iSerial.split(' ');
92
+ iSerialParts.shift();
93
+ const serial = iSerialParts.join(' ');
94
+
95
+ result.id = (idVendor.startsWith('0x') ? idVendor.split(' ')[0].substr(2, 10) : '') + ':' + (idProduct.startsWith('0x') ? idProduct.split(' ')[0].substr(2, 10) : '');
96
+ result.name = product;
97
+ result.type = getLinuxUsbType(usbType, product);
98
+ result.removable = null;
99
+ result.vendor = vendor;
100
+ result.manufacturer = manufacturer;
101
+ result.maxPower = util.getValue(lines, 'MaxPower', ' ', true);
102
+ result.serialNumber = serial;
103
+
104
+ return result;
105
+ }
106
+
107
+ function getDarwinUsbType(name) {
108
+ let result = '';
109
+ if (name.indexOf('camera') >= 0) {
110
+ result = 'Camera';
111
+ } else if (name.indexOf('touch bar') >= 0) {
112
+ result = 'Touch Bar';
113
+ } else if (name.indexOf('controller') >= 0) {
114
+ result = 'Controller';
115
+ } else if (name.indexOf('headset') >= 0) {
116
+ result = 'Audio';
117
+ } else if (name.indexOf('keyboard') >= 0) {
118
+ result = 'Keyboard';
119
+ } else if (name.indexOf('trackpad') >= 0) {
120
+ result = 'Trackpad';
121
+ } else if (name.indexOf('sensor') >= 0) {
122
+ result = 'Sensor';
123
+ } else if (name.indexOf('bthusb') >= 0) {
124
+ result = 'Bluetooth';
125
+ } else if (name.indexOf('bth') >= 0) {
126
+ result = 'Bluetooth';
127
+ } else if (name.indexOf('rfcomm') >= 0) {
128
+ result = 'Bluetooth';
129
+ } else if (name.indexOf('usbhub') >= 0) {
130
+ result = 'Hub';
131
+ } else if (name.indexOf(' hub') >= 0) {
132
+ result = 'Hub';
133
+ } else if (name.indexOf('mouse') >= 0) {
134
+ result = 'Mouse';
135
+ } else if (name.indexOf('microp') >= 0) {
136
+ result = 'Microphone';
137
+ } else if (name.indexOf('removable') >= 0) {
138
+ result = 'Storage';
139
+ }
140
+ return result;
141
+ }
142
+
143
+ function parseDarwinUsb(usb, id) {
144
+ const result = {};
145
+ result.id = id;
146
+
147
+ usb = usb.replace(/ \|/g, '');
148
+ usb = usb.trim();
149
+ let lines = usb.split('\n');
150
+ lines.shift();
151
+ try {
152
+ for (let i = 0; i < lines.length; i++) {
153
+ lines[i] = lines[i].trim();
154
+ lines[i] = lines[i].replace(/=/g, ':');
155
+ if (lines[i] !== '{' && lines[i] !== '}' && lines[i + 1] && lines[i + 1].trim() !== '}') {
156
+ lines[i] = lines[i] + ',';
157
+ }
158
+
159
+ lines[i] = lines[i].replace(':Yes,', ':"Yes",');
160
+ lines[i] = lines[i].replace(': Yes,', ': "Yes",');
161
+ lines[i] = lines[i].replace(': Yes', ': "Yes"');
162
+ lines[i] = lines[i].replace(':No,', ':"No",');
163
+ lines[i] = lines[i].replace(': No,', ': "No",');
164
+ lines[i] = lines[i].replace(': No', ': "No"');
165
+
166
+ // In this case (("com.apple.developer.driverkit.transport.usb"))
167
+ lines[i] = lines[i].replace('((', '').replace('))', '');
168
+
169
+ // In case we have <923c11> we need make it "<923c11>" for correct JSON parse
170
+ const match = /<(\w+)>/.exec(lines[i]);
171
+ if (match) {
172
+ const number = match[0];
173
+ lines[i] = lines[i].replace(number, `"${number}"`);
174
+ }
175
+ }
176
+ const usbObj = JSON.parse(lines.join('\n'));
177
+ const removableDrive = (usbObj['Built-In'] ? usbObj['Built-In'].toLowerCase() !== 'yes' : true) && (usbObj['non-removable'] ? usbObj['non-removable'].toLowerCase() === 'no' : true);
178
+
179
+ result.bus = null;
180
+ result.deviceId = null;
181
+ result.id = usbObj['USB Address'] || null;
182
+ result.name = usbObj['kUSBProductString'] || usbObj['USB Product Name'] || null;
183
+ result.type = getDarwinUsbType((usbObj['kUSBProductString'] || usbObj['USB Product Name'] || '').toLowerCase() + (removableDrive ? ' removable' : ''));
184
+ result.removable = usbObj['non-removable'] ? usbObj['non-removable'].toLowerCase() || '' === 'no' : true;
185
+ result.vendor = usbObj['kUSBVendorString'] || usbObj['USB Vendor Name'] || null;
186
+ result.manufacturer = usbObj['kUSBVendorString'] || usbObj['USB Vendor Name'] || null;
187
+
188
+ result.maxPower = null;
189
+ result.serialNumber = usbObj['kUSBSerialNumberString'] || null;
190
+
191
+ if (result.name) {
192
+ return result;
193
+ } else {
194
+ return null;
195
+ }
196
+ } catch (e) {
197
+ return null;
198
+ }
199
+ }
200
+
201
+ function getWindowsUsbTypeCreation(creationclass, name) {
202
+ let result = '';
203
+ if (name.indexOf('storage') >= 0) {
204
+ result = 'Storage';
205
+ } else if (name.indexOf('speicher') >= 0) {
206
+ result = 'Storage';
207
+ } else if (creationclass.indexOf('usbhub') >= 0) {
208
+ result = 'Hub';
209
+ } else if (creationclass.indexOf('storage') >= 0) {
210
+ result = 'Storage';
211
+ } else if (creationclass.indexOf('usbcontroller') >= 0) {
212
+ result = 'Controller';
213
+ } else if (creationclass.indexOf('keyboard') >= 0) {
214
+ result = 'Keyboard';
215
+ } else if (creationclass.indexOf('pointing') >= 0) {
216
+ result = 'Mouse';
217
+ } else if (creationclass.indexOf('microp') >= 0) {
218
+ result = 'Microphone';
219
+ } else if (creationclass.indexOf('disk') >= 0) {
220
+ result = 'Storage';
221
+ }
222
+ return result;
223
+ }
224
+
225
+ function parseWindowsUsb(lines, id) {
226
+ const usbType = getWindowsUsbTypeCreation(util.getValue(lines, 'CreationClassName', ':').toLowerCase(), util.getValue(lines, 'name', ':').toLowerCase());
227
+
228
+ if (usbType) {
229
+ const result = {};
230
+ result.bus = null;
231
+ result.deviceId = util.getValue(lines, 'deviceid', ':');
232
+ result.id = id;
233
+ result.name = util.getValue(lines, 'name', ':');
234
+ result.type = usbType;
235
+ result.removable = null;
236
+ result.vendor = null;
237
+ result.manufacturer = util.getValue(lines, 'Manufacturer', ':');
238
+ result.maxPower = null;
239
+ result.serialNumber = null;
240
+
241
+ return result;
242
+ } else {
243
+ return null;
244
+ }
245
+ }
246
+
247
+ function usb(callback) {
248
+ return new Promise((resolve) => {
249
+ process.nextTick(() => {
250
+ let result = [];
251
+ if (_linux) {
252
+ const cmd = 'export LC_ALL=C; lsusb -v 2>/dev/null; unset LC_ALL';
253
+ exec(cmd, { maxBuffer: 1024 * 1024 * 128 }, function (error, stdout) {
254
+ if (!error) {
255
+ const parts = ('\n\n' + stdout.toString()).split('\n\nBus ');
256
+ for (let i = 1; i < parts.length; i++) {
257
+ const usb = parseLinuxUsb(parts[i]);
258
+ result.push(usb);
259
+ }
260
+ }
261
+ if (callback) {
262
+ callback(result);
263
+ }
264
+ resolve(result);
265
+ });
266
+ }
267
+ if (_darwin) {
268
+ let cmd = 'ioreg -p IOUSB -c AppleUSBRootHubDevice -w0 -l';
269
+ exec(cmd, { maxBuffer: 1024 * 1024 * 128 }, function (error, stdout) {
270
+ if (!error) {
271
+ const parts = stdout.toString().split(' +-o ');
272
+ for (let i = 1; i < parts.length; i++) {
273
+ const usb = parseDarwinUsb(parts[i]);
274
+ if (usb) {
275
+ result.push(usb);
276
+ }
277
+ }
278
+ if (callback) {
279
+ callback(result);
280
+ }
281
+ resolve(result);
282
+ }
283
+ if (callback) {
284
+ callback(result);
285
+ }
286
+ resolve(result);
287
+ });
288
+ }
289
+ if (_windows) {
290
+ util.powerShell('Get-CimInstance CIM_LogicalDevice | where { $_.Description -match "USB"} | select Name,CreationClassName,DeviceId,Manufacturer | fl').then((stdout, error) => {
291
+ if (!error) {
292
+ const parts = stdout.toString().split(/\n\s*\n/);
293
+ for (let i = 0; i < parts.length; i++) {
294
+ const usb = parseWindowsUsb(parts[i].split('\n'), i);
295
+ if (usb && result.filter((x) => x.deviceId === usb.deviceId).length === 0) {
296
+ result.push(usb);
297
+ }
298
+ }
299
+ }
300
+ if (callback) {
301
+ callback(result);
302
+ }
303
+ resolve(result);
304
+ });
305
+ }
306
+ if (_sunos || _freebsd || _openbsd || _netbsd) {
307
+ resolve(null);
308
+ }
309
+ });
310
+ });
311
+ }
312
+
313
+ exports.usb = usb;