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.3.0 to 2.3.1

6

CHANGELOG.md
# Changelog
## [2.3.1] - 2022-04-11
### Changed
- Removed device access mutex from WebUSB API - [`501`](https://github.com/node-usb/node-usb/pull/501) ([Rob Moran](https://github.com/thegecko))
## [2.3.0] - 2022-04-11

@@ -7,3 +12,2 @@

- Changed libusb dependency to upstream v1.0.26 - [`505`](https://github.com/node-usb/node-usb/pull/505) ([Rob Moran](https://github.com/thegecko))
- Cleaned up Windows device polling method - [`496`](https://github.com/node-usb/node-usb/pull/496) ([Alba Mendez](https://github.com/mildsunrise))

@@ -10,0 +14,0 @@

1

dist/webusb/webusb-device.d.ts

@@ -24,3 +24,2 @@ /// <reference types="w3c-web-usb" />

configurations: USBConfiguration[];
private deviceMutex;
private constructor();

@@ -27,0 +26,0 @@ get configuration(): USBConfiguration | undefined;

@@ -53,3 +53,2 @@ "use strict";

var util_1 = require("util");
var mutex_1 = require("./mutex");
var LIBUSB_TRANSFER_TYPE_MASK = 0x03;

@@ -66,3 +65,2 @@ var ENDPOINT_NUMBER_MASK = 0x7f;

this.configurations = [];
this.deviceMutex = new mutex_1.Mutex();
var usbVersion = this.decodeVersion(device.deviceDescriptor.bcdUSB);

@@ -122,23 +120,13 @@ this.usbVersionMajor = usbVersion.major;

return __awaiter(this, void 0, void 0, function () {
var error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, 3, 4]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
if (this.opened) {
return [2 /*return*/];
}
this.device.open();
return [3 /*break*/, 4];
case 2:
error_1 = _a.sent();
throw new Error("open error: " + error_1);
case 3:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 4: return [2 /*return*/];
try {
if (this.opened) {
return [2 /*return*/];
}
this.device.open();
}
catch (error) {
throw new Error("open error: " + error);
}
return [2 /*return*/];
});

@@ -150,3 +138,3 @@ });

return __awaiter(this, void 0, void 0, function () {
var _b, _c, iface, e_1_1, _error_1, error_2;
var _b, _c, iface, e_1_1, _error_1, error_1;
var e_1, _d;

@@ -156,23 +144,20 @@ return __generator(this, function (_e) {

case 0:
_e.trys.push([0, 13, 14, 15]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_e.sent();
_e.trys.push([0, 12, , 13]);
if (!this.opened) {
return [2 /*return*/];
}
_e.label = 1;
case 1:
_e.trys.push([1, 10, , 11]);
if (!this.configuration) return [3 /*break*/, 9];
_e.label = 2;
case 2:
_e.trys.push([2, 11, , 12]);
if (!this.configuration) return [3 /*break*/, 10];
_e.trys.push([2, 7, 8, 9]);
_b = __values((_a = this.configuration) === null || _a === void 0 ? void 0 : _a.interfaces), _c = _b.next();
_e.label = 3;
case 3:
_e.trys.push([3, 8, 9, 10]);
_b = __values((_a = this.configuration) === null || _a === void 0 ? void 0 : _a.interfaces), _c = _b.next();
_e.label = 4;
case 4:
if (!!_c.done) return [3 /*break*/, 7];
if (!!_c.done) return [3 /*break*/, 6];
iface = _c.value;
return [4 /*yield*/, this._releaseInterface(iface.interfaceNumber)];
case 5:
case 4:
_e.sent();

@@ -186,12 +171,12 @@ // Re-create the USBInterface to set the claimed attribute

};
_e.label = 6;
case 6:
_e.label = 5;
case 5:
_c = _b.next();
return [3 /*break*/, 4];
case 7: return [3 /*break*/, 10];
case 8:
return [3 /*break*/, 3];
case 6: return [3 /*break*/, 9];
case 7:
e_1_1 = _e.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 10];
case 9:
return [3 /*break*/, 9];
case 8:
try {

@@ -202,16 +187,13 @@ if (_c && !_c.done && (_d = _b.return)) _d.call(_b);

return [7 /*endfinally*/];
case 10: return [3 /*break*/, 12];
case 9: return [3 /*break*/, 11];
case 10:
_error_1 = _e.sent();
return [3 /*break*/, 11];
case 11:
_error_1 = _e.sent();
return [3 /*break*/, 12];
this.device.close();
return [3 /*break*/, 13];
case 12:
this.device.close();
return [3 /*break*/, 15];
case 13:
error_2 = _e.sent();
throw new Error("close error: " + error_2);
case 14:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 15: return [2 /*return*/];
error_1 = _e.sent();
throw new Error("close error: " + error_1);
case 13: return [2 /*return*/];
}

@@ -223,10 +205,6 @@ });

return __awaiter(this, void 0, void 0, function () {
var config, setConfiguration, error_3;
var config, setConfiguration, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, , 6, 7]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
if (!this.opened || !this.device.configDescriptor) {

@@ -242,18 +220,14 @@ throw new Error('selectConfiguration error: invalid state');

}
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
setConfiguration = util_1.promisify(this.device.setConfiguration).bind(this.device);
return [4 /*yield*/, setConfiguration(configurationValue)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
_a.sent();
return [3 /*break*/, 5];
case 4:
error_3 = _a.sent();
throw new Error("selectConfiguration error: " + error_3);
case 5: return [3 /*break*/, 7];
case 6:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
error_2 = _a.sent();
throw new Error("selectConfiguration error: " + error_2);
case 4: return [2 /*return*/];
}

@@ -267,40 +241,29 @@ });

return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, , 2, 3]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
if (!this.opened) {
throw new Error('claimInterface error: invalid state');
}
if (!this.configuration) {
throw new Error('claimInterface error: interface not found');
}
iface = this.configuration.interfaces.find(function (usbInterface) { return usbInterface.interfaceNumber === interfaceNumber; });
if (!iface) {
throw new Error('claimInterface error: interface not found');
}
if (iface.claimed) {
return [2 /*return*/];
}
try {
this.device.interface(interfaceNumber).claim();
// Re-create the USBInterface to set the claimed attribute
this.configuration.interfaces[this.configuration.interfaces.indexOf(iface)] = {
interfaceNumber: interfaceNumber,
alternate: iface.alternate,
alternates: iface.alternates,
claimed: true
};
}
catch (error) {
throw new Error("claimInterface error: " + error);
}
return [3 /*break*/, 3];
case 2:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 3: return [2 /*return*/];
if (!this.opened) {
throw new Error('claimInterface error: invalid state');
}
if (!this.configuration) {
throw new Error('claimInterface error: interface not found');
}
iface = this.configuration.interfaces.find(function (usbInterface) { return usbInterface.interfaceNumber === interfaceNumber; });
if (!iface) {
throw new Error('claimInterface error: interface not found');
}
if (iface.claimed) {
return [2 /*return*/];
}
try {
this.device.interface(interfaceNumber).claim();
// Re-create the USBInterface to set the claimed attribute
this.configuration.interfaces[this.configuration.interfaces.indexOf(iface)] = {
interfaceNumber: interfaceNumber,
alternate: iface.alternate,
alternates: iface.alternates,
claimed: true
};
}
catch (error) {
throw new Error("claimInterface error: " + error);
}
return [2 /*return*/];
});

@@ -314,10 +277,5 @@ });

switch (_a.label) {
case 0:
_a.trys.push([0, , 3, 4]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 0: return [4 /*yield*/, this._releaseInterface(interfaceNumber)];
case 1:
_a.sent();
return [4 /*yield*/, this._releaseInterface(interfaceNumber)];
case 2:
_a.sent();
if (this.configuration) {

@@ -335,7 +293,3 @@ iface = this.configuration.interfaces.find(function (usbInterface) { return usbInterface.interfaceNumber === interfaceNumber; });

}
return [3 /*break*/, 4];
case 3:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 4: return [2 /*return*/];
return [2 /*return*/];
}

@@ -347,10 +301,6 @@ });

return __awaiter(this, void 0, void 0, function () {
var iface, iface_1, setAltSetting, error_4;
var iface, iface_1, setAltSetting, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, , 6, 7]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
if (!this.opened) {

@@ -369,19 +319,15 @@ throw new Error('selectAlternateInterface error: invalid state');

}
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
iface_1 = this.device.interface(interfaceNumber);
setAltSetting = util_1.promisify(iface_1.setAltSetting).bind(iface_1);
return [4 /*yield*/, setAltSetting(alternateSetting)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
_a.sent();
return [3 /*break*/, 5];
case 4:
error_4 = _a.sent();
throw new Error("selectAlternateInterface error: " + error_4);
case 5: return [3 /*break*/, 7];
case 6:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
error_3 = _a.sent();
throw new Error("selectAlternateInterface error: " + error_3);
case 4: return [2 /*return*/];
}

@@ -393,14 +339,11 @@ });

return __awaiter(this, void 0, void 0, function () {
var type, controlTransfer, result, error_5;
var type, controlTransfer, result, error_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, 4, 5]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
_a.trys.push([0, 2, , 3]);
type = this.controlTransferParamsToType(setup, usb.LIBUSB_ENDPOINT_IN);
controlTransfer = util_1.promisify(this.device.controlTransfer).bind(this.device);
return [4 /*yield*/, controlTransfer(type, setup.request, setup.value, setup.index, length)];
case 2:
case 1:
result = _a.sent();

@@ -411,5 +354,5 @@ return [2 /*return*/, {

}];
case 3:
error_5 = _a.sent();
if (error_5.errno === usb.LIBUSB_TRANSFER_STALL) {
case 2:
error_4 = _a.sent();
if (error_4.errno === usb.LIBUSB_TRANSFER_STALL) {
return [2 /*return*/, {

@@ -419,3 +362,3 @@ status: 'stall'

}
if (error_5.errno === usb.LIBUSB_TRANSFER_OVERFLOW) {
if (error_4.errno === usb.LIBUSB_TRANSFER_OVERFLOW) {
return [2 /*return*/, {

@@ -425,7 +368,4 @@ status: 'babble'

}
throw new Error("controlTransferIn error: " + error_5);
case 4:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
throw new Error("controlTransferIn error: " + error_4);
case 3: return [2 /*return*/];
}

@@ -437,10 +377,7 @@ });

return __awaiter(this, void 0, void 0, function () {
var type, controlTransfer, buffer, bytesWritten, error_6;
var type, controlTransfer, buffer, bytesWritten, error_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, 4, 5]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
_a.trys.push([0, 2, , 3]);
type = this.controlTransferParamsToType(setup, usb.LIBUSB_ENDPOINT_OUT);

@@ -450,3 +387,3 @@ controlTransfer = util_1.promisify(this.device.controlTransfer).bind(this.device);

return [4 /*yield*/, controlTransfer(type, setup.request, setup.value, setup.index, buffer)];
case 2:
case 1:
bytesWritten = _a.sent();

@@ -457,5 +394,5 @@ return [2 /*return*/, {

}];
case 3:
error_6 = _a.sent();
if (error_6.errno === usb.LIBUSB_TRANSFER_STALL) {
case 2:
error_5 = _a.sent();
if (error_5.errno === usb.LIBUSB_TRANSFER_STALL) {
return [2 /*return*/, {

@@ -466,7 +403,4 @@ bytesWritten: 0,

}
throw new Error("controlTransferOut error: " + error_6);
case 4:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
throw new Error("controlTransferOut error: " + error_5);
case 3: return [2 /*return*/];
}

@@ -478,23 +412,17 @@ });

return __awaiter(this, void 0, void 0, function () {
var wIndex, controlTransfer, error_7;
var wIndex, controlTransfer, error_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, 4, 5]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
_a.trys.push([0, 2, , 3]);
wIndex = endpointNumber | (direction === 'in' ? usb.LIBUSB_ENDPOINT_IN : usb.LIBUSB_ENDPOINT_OUT);
controlTransfer = util_1.promisify(this.device.controlTransfer).bind(this.device);
return [4 /*yield*/, controlTransfer(usb.LIBUSB_RECIPIENT_ENDPOINT, CLEAR_FEATURE, ENDPOINT_HALT, wIndex, 0)];
case 1:
_a.sent();
return [3 /*break*/, 3];
case 2:
_a.sent();
return [3 /*break*/, 5];
case 3:
error_7 = _a.sent();
throw new Error("clearHalt error: " + error_7);
case 4:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
error_6 = _a.sent();
throw new Error("clearHalt error: " + error_6);
case 3: return [2 /*return*/];
}

@@ -506,14 +434,11 @@ });

return __awaiter(this, void 0, void 0, function () {
var endpoint, transfer, result, error_8;
var endpoint, transfer, result, error_7;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, 4, 5]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
_a.trys.push([0, 2, , 3]);
endpoint = this.getEndpoint(endpointNumber | usb.LIBUSB_ENDPOINT_IN);
transfer = util_1.promisify(endpoint.transfer).bind(endpoint);
return [4 /*yield*/, transfer(length)];
case 2:
case 1:
result = _a.sent();

@@ -524,5 +449,5 @@ return [2 /*return*/, {

}];
case 3:
error_8 = _a.sent();
if (error_8.errno === usb.LIBUSB_TRANSFER_STALL) {
case 2:
error_7 = _a.sent();
if (error_7.errno === usb.LIBUSB_TRANSFER_STALL) {
return [2 /*return*/, {

@@ -532,3 +457,3 @@ status: 'stall'

}
if (error_8.errno === usb.LIBUSB_TRANSFER_OVERFLOW) {
if (error_7.errno === usb.LIBUSB_TRANSFER_OVERFLOW) {
return [2 /*return*/, {

@@ -538,7 +463,4 @@ status: 'babble'

}
throw new Error("transferIn error: " + error_8);
case 4:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
throw new Error("transferIn error: " + error_7);
case 3: return [2 /*return*/];
}

@@ -550,10 +472,7 @@ });

return __awaiter(this, void 0, void 0, function () {
var endpoint, transfer, buffer, bytesWritten, error_9;
var endpoint, transfer, buffer, bytesWritten, error_8;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, 4, 5]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_a.sent();
_a.trys.push([0, 2, , 3]);
endpoint = this.getEndpoint(endpointNumber | usb.LIBUSB_ENDPOINT_OUT);

@@ -563,3 +482,3 @@ transfer = util_1.promisify(endpoint.transfer).bind(endpoint);

return [4 /*yield*/, transfer(buffer)];
case 2:
case 1:
bytesWritten = _a.sent();

@@ -570,5 +489,5 @@ return [2 /*return*/, {

}];
case 3:
error_9 = _a.sent();
if (error_9.errno === usb.LIBUSB_TRANSFER_STALL) {
case 2:
error_8 = _a.sent();
if (error_8.errno === usb.LIBUSB_TRANSFER_STALL) {
return [2 /*return*/, {

@@ -579,7 +498,4 @@ bytesWritten: 0,

}
throw new Error("transferOut error: " + error_9);
case 4:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
throw new Error("transferOut error: " + error_8);
case 3: return [2 /*return*/];
}

@@ -591,22 +507,16 @@ });

return __awaiter(this, void 0, void 0, function () {
var reset, error_10;
var reset, error_9;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, 4, 5]);
return [4 /*yield*/, this.deviceMutex.lock()];
_a.trys.push([0, 2, , 3]);
reset = util_1.promisify(this.device.reset).bind(this.device);
return [4 /*yield*/, reset()];
case 1:
_a.sent();
reset = util_1.promisify(this.device.reset).bind(this.device);
return [4 /*yield*/, reset()];
return [3 /*break*/, 3];
case 2:
_a.sent();
return [3 /*break*/, 5];
case 3:
error_10 = _a.sent();
throw new Error("reset error: " + error_10);
case 4:
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
error_9 = _a.sent();
throw new Error("reset error: " + error_9);
case 3: return [2 /*return*/];
}

@@ -639,10 +549,7 @@ });

return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, _d, error_11;
var _a, _b, _c, _d, error_10;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
_e.trys.push([0, 6, 7, 8]);
return [4 /*yield*/, this.deviceMutex.lock()];
case 1:
_e.sent();
_e.trys.push([0, 5, 6, 7]);
if (!this.opened) {

@@ -653,27 +560,26 @@ this.device.open();

return [4 /*yield*/, this.getStringDescriptor(this.device.deviceDescriptor.iManufacturer)];
case 2:
case 1:
_a.manufacturerName = _e.sent();
_b = this;
return [4 /*yield*/, this.getStringDescriptor(this.device.deviceDescriptor.iProduct)];
case 3:
case 2:
_b.productName = _e.sent();
_c = this;
return [4 /*yield*/, this.getStringDescriptor(this.device.deviceDescriptor.iSerialNumber)];
case 4:
case 3:
_c.serialNumber = _e.sent();
_d = this;
return [4 /*yield*/, this.getConfigurations()];
case 4:
_d.configurations = _e.sent();
return [3 /*break*/, 7];
case 5:
_d.configurations = _e.sent();
return [3 /*break*/, 8];
error_10 = _e.sent();
throw new Error("initialize error: " + error_10);
case 6:
error_11 = _e.sent();
throw new Error("initialize error: " + error_11);
case 7:
if (this.opened) {
this.device.close();
}
this.deviceMutex.unlock();
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
case 7: return [2 /*return*/];
}

@@ -693,3 +599,3 @@ });

return __awaiter(this, void 0, void 0, function () {
var getStringDescriptor, buffer, error_12;
var getStringDescriptor, buffer, error_11;
return __generator(this, function (_a) {

@@ -705,3 +611,3 @@ switch (_a.label) {

case 2:
error_12 = _a.sent();
error_11 = _a.sent();
return [2 /*return*/, ''];

@@ -901,3 +807,3 @@ case 3: return [2 /*return*/];

return __awaiter(this, void 0, void 0, function () {
var iface, iface_3, release, error_13;
var iface, iface_3, release, error_12;
return __generator(this, function (_a) {

@@ -929,4 +835,4 @@ switch (_a.label) {

case 3:
error_13 = _a.sent();
throw new Error("releaseInterface error: " + error_13);
error_12 = _a.sent();
throw new Error("releaseInterface error: " + error_12);
case 4: return [2 /*return*/];

@@ -933,0 +839,0 @@ }

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

"license": "MIT",
"version": "2.3.0",
"version": "2.3.1",
"main": "dist/index.js",

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

import * as usb from '../usb';
import { promisify } from 'util';
import { Endpoint, InEndpoint, OutEndpoint } from '../usb/endpoint';
import { Mutex } from './mutex';

@@ -42,4 +41,2 @@ const LIBUSB_TRANSFER_TYPE_MASK = 0x03;

private deviceMutex = new Mutex();
private constructor(private device: usb.Device) {

@@ -77,4 +74,2 @@ const usbVersion = this.decodeVersion(device.deviceDescriptor.bcdUSB);

try {
await this.deviceMutex.lock();
if (this.opened) {

@@ -87,4 +82,2 @@ return;

throw new Error(`open error: ${error}`);
} finally {
this.deviceMutex.unlock();
}

@@ -95,4 +88,2 @@ }

try {
await this.deviceMutex.lock();
if (!this.opened) {

@@ -122,4 +113,2 @@ return;

throw new Error(`close error: ${error}`);
} finally {
this.deviceMutex.unlock();
}

@@ -129,26 +118,20 @@ }

public async selectConfiguration(configurationValue: number): Promise<void> {
try {
await this.deviceMutex.lock();
if (!this.opened || !this.device.configDescriptor) {
throw new Error('selectConfiguration error: invalid state');
}
if (!this.opened || !this.device.configDescriptor) {
throw new Error('selectConfiguration error: invalid state');
}
if (this.device.configDescriptor.bConfigurationValue === configurationValue) {
return;
}
if (this.device.configDescriptor.bConfigurationValue === configurationValue) {
return;
}
const config = this.configurations.find(configuration => configuration.configurationValue === configurationValue);
if (!config) {
throw new Error('selectConfiguration error: configuration not found');
}
const config = this.configurations.find(configuration => configuration.configurationValue === configurationValue);
if (!config) {
throw new Error('selectConfiguration error: configuration not found');
}
try {
const setConfiguration = promisify(this.device.setConfiguration).bind(this.device);
await setConfiguration(configurationValue);
} catch (error) {
throw new Error(`selectConfiguration error: ${error}`);
}
} finally {
this.deviceMutex.unlock();
try {
const setConfiguration = promisify(this.device.setConfiguration).bind(this.device);
await setConfiguration(configurationValue);
} catch (error) {
throw new Error(`selectConfiguration error: ${error}`);
}

@@ -158,25 +141,40 @@ }

public async claimInterface(interfaceNumber: number): Promise<void> {
try {
await this.deviceMutex.lock();
if (!this.opened) {
throw new Error('claimInterface error: invalid state');
}
if (!this.opened) {
throw new Error('claimInterface error: invalid state');
}
if (!this.configuration) {
throw new Error('claimInterface error: interface not found');
}
if (!this.configuration) {
throw new Error('claimInterface error: interface not found');
}
const iface = this.configuration.interfaces.find(usbInterface => usbInterface.interfaceNumber === interfaceNumber);
if (!iface) {
throw new Error('claimInterface error: interface not found');
}
const iface = this.configuration.interfaces.find(usbInterface => usbInterface.interfaceNumber === interfaceNumber);
if (!iface) {
throw new Error('claimInterface error: interface not found');
}
if (iface.claimed) {
return;
}
if (iface.claimed) {
return;
}
try {
this.device.interface(interfaceNumber).claim();
try {
this.device.interface(interfaceNumber).claim();
// Re-create the USBInterface to set the claimed attribute
this.configuration.interfaces[this.configuration.interfaces.indexOf(iface)] = {
interfaceNumber,
alternate: iface.alternate,
alternates: iface.alternates,
claimed: true
};
} catch (error) {
throw new Error(`claimInterface error: ${error}`);
}
}
public async releaseInterface(interfaceNumber: number): Promise<void> {
await this._releaseInterface(interfaceNumber);
if (this.configuration) {
const iface = this.configuration.interfaces.find(usbInterface => usbInterface.interfaceNumber === interfaceNumber);
if (iface) {
// Re-create the USBInterface to set the claimed attribute

@@ -187,65 +185,32 @@ this.configuration.interfaces[this.configuration.interfaces.indexOf(iface)] = {

alternates: iface.alternates,
claimed: true
claimed: false
};
} catch (error) {
throw new Error(`claimInterface error: ${error}`);
}
} finally {
this.deviceMutex.unlock();
}
}
public async releaseInterface(interfaceNumber: number): Promise<void> {
try {
await this.deviceMutex.lock();
await this._releaseInterface(interfaceNumber);
public async selectAlternateInterface(interfaceNumber: number, alternateSetting: number): Promise<void> {
if (!this.opened) {
throw new Error('selectAlternateInterface error: invalid state');
}
if (this.configuration) {
const iface = this.configuration.interfaces.find(usbInterface => usbInterface.interfaceNumber === interfaceNumber);
if (iface) {
// Re-create the USBInterface to set the claimed attribute
this.configuration.interfaces[this.configuration.interfaces.indexOf(iface)] = {
interfaceNumber,
alternate: iface.alternate,
alternates: iface.alternates,
claimed: false
};
}
}
if (!this.configuration) {
throw new Error('selectAlternateInterface error: interface not found');
}
} finally {
this.deviceMutex.unlock();
const iface = this.configuration.interfaces.find(usbInterface => usbInterface.interfaceNumber === interfaceNumber);
if (!iface) {
throw new Error('selectAlternateInterface error: interface not found');
}
}
public async selectAlternateInterface(interfaceNumber: number, alternateSetting: number): Promise<void> {
if (!iface.claimed) {
throw new Error('selectAlternateInterface error: invalid state');
}
try {
await this.deviceMutex.lock();
if (!this.opened) {
throw new Error('selectAlternateInterface error: invalid state');
}
if (!this.configuration) {
throw new Error('selectAlternateInterface error: interface not found');
}
const iface = this.configuration.interfaces.find(usbInterface => usbInterface.interfaceNumber === interfaceNumber);
if (!iface) {
throw new Error('selectAlternateInterface error: interface not found');
}
if (!iface.claimed) {
throw new Error('selectAlternateInterface error: invalid state');
}
try {
const iface = this.device.interface(interfaceNumber);
const setAltSetting = promisify(iface.setAltSetting).bind(iface);
await setAltSetting(alternateSetting);
} catch (error) {
throw new Error(`selectAlternateInterface error: ${error}`);
}
} finally {
this.deviceMutex.unlock();
const iface = this.device.interface(interfaceNumber);
const setAltSetting = promisify(iface.setAltSetting).bind(iface);
await setAltSetting(alternateSetting);
} catch (error) {
throw new Error(`selectAlternateInterface error: ${error}`);
}

@@ -256,3 +221,2 @@ }

try {
await this.deviceMutex.lock();
const type = this.controlTransferParamsToType(setup, usb.LIBUSB_ENDPOINT_IN);

@@ -280,4 +244,2 @@ const controlTransfer = promisify(this.device.controlTransfer).bind(this.device);

throw new Error(`controlTransferIn error: ${error}`);
} finally {
this.deviceMutex.unlock();
}

@@ -288,3 +250,2 @@ }

try {
await this.deviceMutex.lock();
const type = this.controlTransferParamsToType(setup, usb.LIBUSB_ENDPOINT_OUT);

@@ -308,4 +269,2 @@ const controlTransfer = promisify(this.device.controlTransfer).bind(this.device);

throw new Error(`controlTransferOut error: ${error}`);
} finally {
this.deviceMutex.unlock();
}

@@ -316,3 +275,2 @@ }

try {
await this.deviceMutex.lock();
const wIndex = endpointNumber | (direction === 'in' ? usb.LIBUSB_ENDPOINT_IN : usb.LIBUSB_ENDPOINT_OUT);

@@ -323,4 +281,2 @@ const controlTransfer = promisify(this.device.controlTransfer).bind(this.device);

throw new Error(`clearHalt error: ${error}`);
} finally {
this.deviceMutex.unlock();
}

@@ -331,3 +287,2 @@ }

try {
await this.deviceMutex.lock();
const endpoint = this.getEndpoint(endpointNumber | usb.LIBUSB_ENDPOINT_IN) as InEndpoint;

@@ -355,4 +310,2 @@ const transfer = promisify(endpoint.transfer).bind(endpoint);

throw new Error(`transferIn error: ${error}`);
} finally {
this.deviceMutex.unlock();
}

@@ -363,3 +316,2 @@ }

try {
await this.deviceMutex.lock();
const endpoint = this.getEndpoint(endpointNumber | usb.LIBUSB_ENDPOINT_OUT) as OutEndpoint;

@@ -383,4 +335,2 @@ const transfer = promisify(endpoint.transfer).bind(endpoint);

throw new Error(`transferOut error: ${error}`);
} finally {
this.deviceMutex.unlock();
}

@@ -391,3 +341,2 @@ }

try {
await this.deviceMutex.lock();
const reset = promisify(this.device.reset).bind(this.device);

@@ -397,4 +346,2 @@ await reset();

throw new Error(`reset error: ${error}`);
} finally {
this.deviceMutex.unlock();
}

@@ -417,4 +364,2 @@ }

try {
await this.deviceMutex.lock();
if (!this.opened) {

@@ -434,4 +379,2 @@ this.device.open();

}
this.deviceMutex.unlock();
}

@@ -438,0 +381,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

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