Socket
Socket
Sign inDemoInstall

usb

Package Overview
Dependencies
Maintainers
3
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

usb - npm Package Compare versions

Comparing version 2.4.3 to 2.5.0

src/hotplug/hotplug.h

5

CHANGELOG.md
# Changelog
## [2.5.0] - 2022-07-30
### Fixed
- Native USB device plug/unplug detection on Windows - [`524`](https://github.com/node-usb/node-usb/pull/524) ([Julian Waller](https://github.com/Julusian))
## [2.4.3] - 2022-06-21

@@ -4,0 +9,0 @@

1

dist/usb/bindings.d.ts

@@ -20,2 +20,3 @@ /// <reference types="node" />

export declare function useUsbDkBackend(): void;
export declare function _supportedHotplugEvents(): number;
export declare function _enableHotplugEvents(): void;

@@ -22,0 +23,0 @@ export declare function _disableHotplugEvents(): void;

@@ -13,2 +13,4 @@ import { ExtendedDevice } from './device';

detach: Device;
attachIds: undefined;
detachIds: undefined;
}

@@ -15,0 +17,0 @@ function addListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;

38

dist/usb/index.js

@@ -24,17 +24,9 @@ "use strict";

});
// Polling mechanism for discovering device changes until this is fixed:
// https://github.com/libusb/libusb/issues/86
// Polling mechanism for discovering device changes where hotplug detection is not available
var pollTimeout = 500;
var hotplugSupported = usb._getLibusbCapability(usb.LIBUSB_CAP_HAS_HOTPLUG) > 0;
var hotplugSupported = usb._supportedHotplugEvents();
var pollingHotplug = false;
var pollDevices = new Set();
var pollHotplug = function (start) {
var pollHotplugOnce = function (start) {
var e_1, _a, e_2, _b;
if (start === void 0) { start = false; }
if (start) {
pollingHotplug = true;
}
else if (!pollingHotplug) {
return;
}
// Collect current devices

@@ -75,6 +67,26 @@ var devices = new Set(usb.getDeviceList());

pollDevices = devices;
};
var pollHotplugLoop = function (start) {
if (start === void 0) { start = false; }
if (start) {
pollingHotplug = true;
}
else if (!pollingHotplug) {
return;
}
pollHotplugOnce(start);
setTimeout(function () {
pollHotplug();
pollHotplugLoop();
}, pollTimeout);
};
var hotplugModeIsIdsOnly = hotplugSupported === 2;
if (hotplugModeIsIdsOnly) {
// The hotplug backend doesnt emit 'attach' or 'detach', so we need to do some conversion
var hotplugEventConversion = function () {
// Future: This might want a debounce, to avoid doing multiple polls when attaching a usb hub or something
pollHotplugOnce(false);
};
usb.on('attachIds', hotplugEventConversion);
usb.on('detachIds', hotplugEventConversion);
}
usb.on('newListener', function (event) {

@@ -90,3 +102,3 @@ if (event !== 'attach' && event !== 'detach') {

else {
pollHotplug(true);
pollHotplugLoop(true);
}

@@ -93,0 +105,0 @@ }

@@ -5,3 +5,3 @@ {

"license": "MIT",
"version": "2.4.3",
"version": "2.5.0",
"main": "dist/index.js",

@@ -8,0 +8,0 @@ "engines": {

@@ -550,2 +550,36 @@ # USB Library for Node.JS

# Migrating from node-usb-detection
If you have been referred here by `node-usb-detection`, the following may be helpful for you to update your existing code.
## usbDetect.startMonitoring() & usbDetect.stopMonitoring()
There is no direct equivalent to these methods. This is handled automatically for you when you add and remove event listeners.
You may find `usb.unrefHotplugEvents()` useful as it is intended to help with exit conditions.
## usbDetect.find()
You can instead use `usb.getDeviceList()`. Be aware that this will do an enumeration to find all of the devices, and not look at a cache of the known devices. If you call it too often it may have a performance impact.
To find a specific device by vid and pid, call `usb.findByIds`. e.g. `usb.findByIds(0x12, 0x34)`.
## usbDetect.on('add', function(device) { ... })
These should be changed to `usb.on('attach', function(device) { ... })`.
There is no equivalent to filter based on the vid or pid, instead you should do a check inside the callback you provide.
The contents of the device object has also changed.
## usbDetect.on('remove', function(device) { ... })
These should be changed to `usb.on('detach', function(device) { ... })`.
There is no equivalent to filter based on the vid or pid, instead you should do a check inside the callback you provide.
The contents of the device object has also changed.
## usbDetect.on('change', function(device) { ... })
There is no direct equivalent to this. Instead you can listen to both `attach` and `detach` to get the same behaviour.
# Development

@@ -552,0 +586,0 @@ The library is based on native bindings wrapping the [libusb](https://github.com/libusb/libusb) library.

@@ -34,2 +34,3 @@ // Definitions from DefinitelyTyped, thanks to:

export declare function _supportedHotplugEvents(): number;
export declare function _enableHotplugEvents(): void;

@@ -36,0 +37,0 @@ export declare function _disableHotplugEvents(): void;

@@ -29,2 +29,4 @@ import { EventEmitter } from 'events';

detach: Device;
attachIds: undefined;
detachIds: undefined;
}

@@ -44,16 +46,9 @@

// Polling mechanism for discovering device changes until this is fixed:
// https://github.com/libusb/libusb/issues/86
// Polling mechanism for discovering device changes where hotplug detection is not available
const pollTimeout = 500;
const hotplugSupported = usb._getLibusbCapability(usb.LIBUSB_CAP_HAS_HOTPLUG) > 0;
const hotplugSupported = usb._supportedHotplugEvents();
let pollingHotplug = false;
let pollDevices = new Set<usb.Device>();
const pollHotplug = (start = false) => {
if (start) {
pollingHotplug = true;
} else if (!pollingHotplug) {
return;
}
const pollHotplugOnce = (start: boolean) => {
// Collect current devices

@@ -77,7 +72,30 @@ const devices = new Set(usb.getDeviceList());

pollDevices = devices;
};
const pollHotplugLoop = (start = false) => {
if (start) {
pollingHotplug = true;
} else if (!pollingHotplug) {
return;
}
pollHotplugOnce(start);
setTimeout(() => {
pollHotplug();
pollHotplugLoop();
}, pollTimeout);
};
const hotplugModeIsIdsOnly = hotplugSupported === 2;
if (hotplugModeIsIdsOnly) {
// The hotplug backend doesnt emit 'attach' or 'detach', so we need to do some conversion
const hotplugEventConversion = () => {
// Future: This might want a debounce, to avoid doing multiple polls when attaching a usb hub or something
pollHotplugOnce(false);
};
usb.on('attachIds', hotplugEventConversion);
usb.on('detachIds', hotplugEventConversion);
}
usb.on('newListener', event => {

@@ -92,3 +110,3 @@ if (event !== 'attach' && event !== 'detach') {

} else {
pollHotplug(true);
pollHotplugLoop(true);
}

@@ -95,0 +113,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc