Comparing version 2.8.0 to 2.8.1
# Changelog | ||
## [2.8.1] - 2023-02-26 | ||
### Changed | ||
- Reworked WebUSB options.allowedDevices to allow pre-authorisation using any filter - [`574`](https://github.com/node-usb/node-usb/pull/574) ([Rob Moran](https://github.com/thegecko)) | ||
- Updated requestDevice errors to use correct names - [`575`](https://github.com/node-usb/node-usb/pull/575) ([Rob Moran](https://github.com/thegecko)) | ||
## [2.8.0] - 2023-02-11 | ||
@@ -4,0 +10,0 @@ |
@@ -35,3 +35,3 @@ /// <reference types="w3c-web-usb" /> | ||
protected knownDevices: Map<usb.Device, WebUSBDevice>; | ||
protected allowedDevices: USBDeviceFilter[]; | ||
protected authorisedDevices: Set<USBDevice>; | ||
constructor(options?: USBOptions); | ||
@@ -60,5 +60,5 @@ private _onconnect; | ||
private getWebDevice; | ||
private preFilterDevices; | ||
private quickFilter; | ||
private filterDevice; | ||
private isAllowedDevice; | ||
private isAuthorisedDevice; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -85,2 +100,11 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
exports.getWebUsb = getWebUsb; | ||
var NamedError = /** @class */ (function (_super) { | ||
__extends(NamedError, _super); | ||
function NamedError(message, name) { | ||
var _this = _super.call(this, message) || this; | ||
_this.name = name; | ||
return _this; | ||
} | ||
return NamedError; | ||
}(Error)); | ||
var WebUSB = /** @class */ (function () { | ||
@@ -93,3 +117,3 @@ function WebUSB(options) { | ||
this.knownDevices = new Map(); | ||
this.allowedDevices = options.allowedDevices || []; | ||
this.authorisedDevices = new Set(); | ||
var deviceConnectCallback = function (device) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -103,3 +127,3 @@ var webDevice, event_1; | ||
// When connected, emit an event if it is an allowed device | ||
if (webDevice && this.isAllowedDevice(webDevice)) { | ||
if (webDevice && this.isAuthorisedDevice(webDevice)) { | ||
event_1 = { | ||
@@ -121,3 +145,3 @@ type: 'connect', | ||
webDevice = this.knownDevices.get(device); | ||
if (webDevice && this.isAllowedDevice(webDevice)) { | ||
if (webDevice && this.isAuthorisedDevice(webDevice)) { | ||
event_2 = { | ||
@@ -238,5 +262,5 @@ type: 'disconnect', | ||
devices = _b.sent(); | ||
devices = devices.filter(function (device) { return _this.filterDevice(options, device); }); | ||
devices = devices.filter(function (device) { return _this.filterDevice(device, options.filters); }); | ||
if (devices.length === 0) { | ||
throw new Error('requestDevice error: no devices found'); | ||
throw new NamedError('Failed to execute \'requestDevice\' on \'USB\': No device selected.', 'NotFoundError'); | ||
} | ||
@@ -257,15 +281,9 @@ _b.label = 2; | ||
if (!device) { | ||
throw new Error('selected device not found'); | ||
throw new NamedError('Failed to execute \'requestDevice\' on \'USB\': No device selected.', 'NotFoundError'); | ||
} | ||
if (!this.isAllowedDevice(device)) { | ||
this.allowedDevices.push({ | ||
vendorId: device.vendorId, | ||
productId: device.productId, | ||
serialNumber: device.serialNumber | ||
}); | ||
} | ||
this.authorisedDevices.add(device); | ||
return [2 /*return*/, device]; | ||
case 6: | ||
error_1 = _b.sent(); | ||
throw new Error("requestDevice error: " + error_1); | ||
throw new NamedError('Failed to execute \'requestDevice\' on \'USB\': No device selected.', 'NotFoundError'); | ||
case 7: return [2 /*return*/]; | ||
@@ -287,14 +305,7 @@ } | ||
case 0: | ||
if (!this.options.allowAllDevices) { | ||
// Create pre-filters | ||
preFilters = this.allowedDevices.map(function (device) { return ({ | ||
vendorId: device.vendorId || undefined, | ||
productId: device.productId || undefined, | ||
serialNumber: device.serialNumber || undefined | ||
}); }); | ||
} | ||
preFilters = this.options.allowAllDevices ? undefined : this.options.allowedDevices; | ||
return [4 /*yield*/, this.loadDevices(preFilters)]; | ||
case 1: | ||
devices = _a.sent(); | ||
return [2 /*return*/, devices.filter(function (device) { return _this.isAllowedDevice(device); })]; | ||
return [2 /*return*/, devices.filter(function (device) { return _this.isAuthorisedDevice(device); })]; | ||
} | ||
@@ -313,3 +324,3 @@ }); | ||
// Pre-filter devices | ||
devices = this.preFilterDevices(devices, preFilters); | ||
devices = this.quickFilter(devices, preFilters); | ||
refreshedKnownDevices = new Map(); | ||
@@ -381,3 +392,4 @@ _b.label = 1; | ||
}; | ||
WebUSB.prototype.preFilterDevices = function (devices, preFilters) { | ||
// Undertake quick filter on devices before creating WebUSB devices if possible | ||
WebUSB.prototype.quickFilter = function (devices, preFilters) { | ||
if (!preFilters || !preFilters.length) { | ||
@@ -394,2 +406,3 @@ return devices; | ||
return false; | ||
// Ignore Class, Subclass and Protocol as these need to check interfaces, too | ||
// Ignore serial number for node-usb as it requires device connection | ||
@@ -399,7 +412,8 @@ return true; | ||
}; | ||
WebUSB.prototype.filterDevice = function (options, device) { | ||
if (!options.filters || !options.filters.length) { | ||
// Filter WebUSB devices | ||
WebUSB.prototype.filterDevice = function (device, filters) { | ||
if (!filters || !filters.length) { | ||
return true; | ||
} | ||
return options.filters.some(function (filter) { | ||
return filters.some(function (filter) { | ||
// Vendor | ||
@@ -448,17 +462,13 @@ if (filter.vendorId && filter.vendorId !== device.vendorId) | ||
}; | ||
WebUSB.prototype.isAllowedDevice = function (device) { | ||
// Check whether a device is authorised | ||
WebUSB.prototype.isAuthorisedDevice = function (device) { | ||
// All devices are authorised | ||
if (this.options.allowAllDevices) { | ||
return true; | ||
} | ||
var isSameDevice = function (device1, device2) { | ||
return (device1.productId === device2.productId | ||
&& device1.vendorId === device2.vendorId | ||
&& device1.serialNumber === device2.serialNumber); | ||
}; | ||
for (var i in this.allowedDevices) { | ||
if (isSameDevice(device, this.allowedDevices[i])) { | ||
return true; | ||
} | ||
// Check any allowed device filters | ||
if (this.options.allowedDevices && this.filterDevice(device, this.options.allowedDevices)) { | ||
return true; | ||
} | ||
return false; | ||
return this.authorisedDevices.has(device); | ||
}; | ||
@@ -465,0 +475,0 @@ return WebUSB; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "2.8.0", | ||
"version": "2.8.1", | ||
"main": "dist/index.js", | ||
@@ -8,0 +8,0 @@ "engines": { |
@@ -139,5 +139,3 @@ # USB Library for Node.JS | ||
if (device) { | ||
console.log(device); // WebUSB device | ||
} | ||
console.log(device); // WebUSB device | ||
})(); | ||
@@ -161,5 +159,3 @@ ``` | ||
if (device) { | ||
console.log(device); // WebUSB device | ||
} | ||
console.log(device); // WebUSB device | ||
})(); | ||
@@ -166,0 +162,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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6214559
3096
628