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.6.0 to 2.7.0

5

CHANGELOG.md
# Changelog
## [2.7.0] - 2023-01-25
### Fixed
- Return same WebUSB device on subsequent calls - [`567`](https://github.com/node-usb/node-usb/pull/567) ([Nisarg Jhaveri](https://github.com/nisargjhaveri))
## [2.6.0] - 2022-12-10

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

6

dist/webusb/index.d.ts
/// <reference types="w3c-web-usb" />
/// <reference types="node" />
import * as usb from '../usb';
import { EventEmitter } from 'events';
import { WebUSBDevice } from './webusb-device';
/**

@@ -32,3 +34,3 @@ * USB Options

protected emitter: EventEmitter;
protected knownDevices: Map<string, USBDevice>;
protected knownDevices: Map<usb.Device, WebUSBDevice>;
protected allowedDevices: USBDeviceFilter[];

@@ -57,6 +59,6 @@ constructor(options?: USBOptions);

private loadDevices;
private getWebDevice;
private preFilterDevices;
private filterDevice;
private getDeviceId;
private isAllowedDevice;
}

@@ -49,2 +49,23 @@ "use strict";

};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -74,21 +95,10 @@ exports.WebUSB = exports.getWebUsb = void 0;

var deviceConnectCallback = function (device) { return __awaiter(_this, void 0, void 0, function () {
var webDevice, _a, deviceId, event_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, webusb_device_1.WebUSBDevice.createInstance(device)];
var webDevice, event_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWebDevice(device)];
case 1:
webDevice = _b.sent();
return [3 /*break*/, 3];
case 2:
_a = _b.sent();
return [3 /*break*/, 3];
case 3:
webDevice = _a.sent();
// When connected, emit an event if it is an allowed device
if (webDevice && this.isAllowedDevice(webDevice)) {
deviceId = this.getDeviceId(device);
if (deviceId) {
this.knownDevices.set(deviceId, webDevice);
}
event_1 = {

@@ -105,8 +115,7 @@ type: 'connect',

var deviceDisconnectCallback = function (device) { return __awaiter(_this, void 0, void 0, function () {
var deviceId, webDevice, event_2;
var webDevice, event_2;
return __generator(this, function (_a) {
deviceId = this.getDeviceId(device);
// When disconnected, emit an event if the device was a known allowed device
if (deviceId !== undefined && this.knownDevices.has(deviceId)) {
webDevice = this.knownDevices.get(deviceId);
if (this.knownDevices.has(device)) {
webDevice = this.knownDevices.get(device);
if (webDevice && this.isAllowedDevice(webDevice)) {

@@ -293,6 +302,6 @@ event_2 = {

return __awaiter(this, void 0, void 0, function () {
var devices, webDevices, devices_1, devices_1_1, device, webDevice, _a, deviceId, e_1_1;
var e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
var devices, refreshedKnownDevices, devices_1, devices_1_1, device, webDevice, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:

@@ -302,49 +311,36 @@ devices = usb.getDeviceList();

devices = this.preFilterDevices(devices, preFilters);
webDevices = [];
_c.label = 1;
refreshedKnownDevices = new Map();
_b.label = 1;
case 1:
_c.trys.push([1, 9, 10, 11]);
_b.trys.push([1, 6, 7, 8]);
devices_1 = __values(devices), devices_1_1 = devices_1.next();
_c.label = 2;
_b.label = 2;
case 2:
if (!!devices_1_1.done) return [3 /*break*/, 8];
if (!!devices_1_1.done) return [3 /*break*/, 5];
device = devices_1_1.value;
if (this.options.deviceTimeout) {
device.timeout = this.options.deviceTimeout;
}
webDevice = void 0;
_c.label = 3;
return [4 /*yield*/, this.getWebDevice(device)];
case 3:
_c.trys.push([3, 5, , 6]);
return [4 /*yield*/, webusb_device_1.WebUSBDevice.createInstance(device)];
case 4:
webDevice = _c.sent();
return [3 /*break*/, 6];
case 5:
_a = _c.sent();
return [3 /*break*/, 6];
case 6:
webDevice = _b.sent();
if (webDevice) {
webDevices.push(webDevice);
deviceId = this.getDeviceId(device);
if (deviceId) {
this.knownDevices.set(deviceId, webDevice);
}
refreshedKnownDevices.set(device, webDevice);
}
_c.label = 7;
case 7:
_b.label = 4;
case 4:
devices_1_1 = devices_1.next();
return [3 /*break*/, 2];
case 8: return [3 /*break*/, 11];
case 9:
e_1_1 = _c.sent();
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 11];
case 10:
return [3 /*break*/, 8];
case 7:
try {
if (devices_1_1 && !devices_1_1.done && (_b = devices_1.return)) _b.call(devices_1);
if (devices_1_1 && !devices_1_1.done && (_a = devices_1.return)) _a.call(devices_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 11: return [2 /*return*/, webDevices];
case 8:
// Refresh knownDevices to remove old devices from the map
this.knownDevices = refreshedKnownDevices;
return [2 /*return*/, __spreadArray([], __read(this.knownDevices.values()))];
}

@@ -354,2 +350,30 @@ });

};
// Get a WebUSBDevice corresponding to underlying device.
// Returns undefined the device was not found and could not be created.
WebUSB.prototype.getWebDevice = function (device) {
return __awaiter(this, void 0, void 0, function () {
var webDevice, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!!this.knownDevices.has(device)) return [3 /*break*/, 4];
if (this.options.deviceTimeout) {
device.timeout = this.options.deviceTimeout;
}
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4 /*yield*/, webusb_device_1.WebUSBDevice.createInstance(device)];
case 2:
webDevice = _b.sent();
this.knownDevices.set(device, webDevice);
return [3 /*break*/, 4];
case 3:
_a = _b.sent();
return [3 /*break*/, 4];
case 4: return [2 /*return*/, this.knownDevices.get(device)];
}
});
});
};
WebUSB.prototype.preFilterDevices = function (devices, preFilters) {

@@ -419,8 +443,2 @@ if (!preFilters || !preFilters.length) {

};
WebUSB.prototype.getDeviceId = function (device) {
if (device.busNumber === undefined || device.deviceAddress === undefined) {
return undefined;
}
return device.busNumber + "." + device.deviceAddress;
};
WebUSB.prototype.isAllowedDevice = function (device) {

@@ -427,0 +445,0 @@ if (this.options.allowAllDevices) {

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

"license": "MIT",
"version": "2.6.0",
"version": "2.7.0",
"main": "dist/index.js",

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

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