usb 1.8.0-libusb.2 → 1.8.1-libusb.5

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/index.js ADDED
@@ -0,0 +1,20 @@
1
+ const usb = require('./');
2
+
3
+ const a = d => console.log('a', d);
4
+ const d = d => console.log('d', d);
5
+
6
+ console.log('listening');
7
+ usb.on('attach', a);
8
+ usb.on('detach', d);
9
+
10
+ setTimeout(() => {
11
+ console.log('ignoring');
12
+ usb.off('attach', a);
13
+ usb.off('detach', d);
14
+
15
+ setTimeout(() => {
16
+ console.log('listening');
17
+ usb.on('attach', a);
18
+ usb.on('detach', d);
19
+ }, 5000);
20
+ }, 5000);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "usb",
3
3
  "description": "Library to access USB devices",
4
- "version": "1.8.0-libusb.2",
4
+ "version": "1.8.1-libusb.5",
5
5
  "engines": {
6
6
  "node": ">=10.16.0"
7
7
  },
Binary file
Binary file
package/usb.js CHANGED
@@ -1,6 +1,9 @@
1
1
  var usb = exports = module.exports = require('node-gyp-build')(__dirname);
2
- var events = require('events')
3
- var util = require('util')
2
+ var events = require('events');
3
+ var util = require('util');
4
+ var os = require('os');
5
+
6
+ var isWindows = os.platform() === 'win32';
4
7
 
5
8
  var isBuffer = function(obj) {
6
9
  return obj && obj instanceof Uint8Array
@@ -512,13 +515,59 @@ var hotplugListeners = 0;
512
515
  exports.on('newListener', function(name) {
513
516
  if (name !== 'attach' && name !== 'detach') return;
514
517
  if (++hotplugListeners === 1) {
515
- usb._enableHotplugEvents();
518
+ if (isWindows) {
519
+ pollHotplug(true);
520
+ } else {
521
+ usb._enableHotplugEvents();
522
+ }
516
523
  }
517
524
  });
518
525
 
519
526
  exports.on('removeListener', function(name) {
520
527
  if (name !== 'attach' && name !== 'detach') return;
521
528
  if (--hotplugListeners === 0) {
522
- usb._disableHotplugEvents();
529
+ if (isWindows) {
530
+ pollingHotplug = false;
531
+ } else {
532
+ usb._disableHotplugEvents();
533
+ }
523
534
  }
524
535
  });
536
+
537
+ // Polling mechanism for discovering Windows device changes until this is fixed:
538
+ // https://github.com/libusb/libusb/issues/86
539
+ exports._windowsPollTimeout = 500;
540
+ var pollingHotplug = false;
541
+ var windowsDevices = [];
542
+ function pollHotplug(start) {
543
+ if (start) {
544
+ pollingHotplug = true;
545
+ } else if (!pollingHotplug) {
546
+ return;
547
+ }
548
+
549
+ var devices = usb.getDeviceList();
550
+
551
+ if (!start) {
552
+ // Find attached devices
553
+ for (var device of devices) {
554
+ var found = windowsDevices.find(item => item.deviceAddress === device.deviceAddress);
555
+ if (!found) {
556
+ usb.emit('attach', device);
557
+ }
558
+ }
559
+
560
+ // Find detached devices
561
+ for (var device of windowsDevices) {
562
+ var found = devices.find(item => item.deviceAddress === device.deviceAddress);
563
+ if (!found) {
564
+ usb.emit('detach', device);
565
+ }
566
+ }
567
+ }
568
+
569
+ windowsDevices = devices;
570
+ setTimeout(() => {
571
+ pollHotplug();
572
+ }, exports._windowsPollTimeout);
573
+ }