usb 2.17.0 → 3.0.0-alpha.2
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/LICENSE +18 -4
- package/README.md +25 -516
- package/dist/index.d.ts +67 -13
- package/dist/index.js +333 -64
- package/index.d.ts +89 -0
- package/index.js +583 -0
- package/package.json +53 -33
- package/CHANGELOG.md +0 -294
- package/binding.gyp +0 -128
- package/dist/index.js.map +0 -1
- package/dist/usb/bindings.d.ts +0 -266
- package/dist/usb/bindings.js +0 -10
- package/dist/usb/bindings.js.map +0 -1
- package/dist/usb/capability.d.ts +0 -13
- package/dist/usb/capability.js +0 -17
- package/dist/usb/capability.js.map +0 -1
- package/dist/usb/descriptors.d.ts +0 -128
- package/dist/usb/descriptors.js +0 -3
- package/dist/usb/descriptors.js.map +0 -1
- package/dist/usb/device.d.ts +0 -100
- package/dist/usb/device.js +0 -297
- package/dist/usb/device.js.map +0 -1
- package/dist/usb/endpoint.d.ts +0 -94
- package/dist/usb/endpoint.js +0 -219
- package/dist/usb/endpoint.js.map +0 -1
- package/dist/usb/index.d.ts +0 -31
- package/dist/usb/index.js +0 -116
- package/dist/usb/index.js.map +0 -1
- package/dist/usb/interface.d.ts +0 -80
- package/dist/usb/interface.js +0 -133
- package/dist/usb/interface.js.map +0 -1
- package/dist/webusb/index.d.ts +0 -64
- package/dist/webusb/index.js +0 -295
- package/dist/webusb/index.js.map +0 -1
- package/dist/webusb/webusb-device.d.ts +0 -54
- package/dist/webusb/webusb-device.js +0 -434
- package/dist/webusb/webusb-device.js.map +0 -1
- package/libusb/.clang-tidy +0 -34
- package/libusb/.codespellrc +0 -3
- package/libusb/.private/README.txt +0 -5
- package/libusb/.private/appveyor_build.sh +0 -26
- package/libusb/.private/bm.sh +0 -54
- package/libusb/.private/ci-build.sh +0 -92
- package/libusb/.private/ci-container-build.sh +0 -67
- package/libusb/.private/post-rewrite.sh +0 -32
- package/libusb/.private/pre-commit.sh +0 -52
- package/libusb/.private/wbs.txt +0 -43
- package/libusb/.travis.yml +0 -58
- package/libusb/AUTHORS +0 -231
- package/libusb/COPYING +0 -504
- package/libusb/ChangeLog +0 -365
- package/libusb/HACKING +0 -25
- package/libusb/INSTALL_WIN.txt +0 -52
- package/libusb/KEYS +0 -123
- package/libusb/Makefile.am +0 -50
- package/libusb/NEWS +0 -2
- package/libusb/PORTING +0 -94
- package/libusb/README +0 -29
- package/libusb/README.git +0 -41
- package/libusb/TODO +0 -2
- package/libusb/Xcode/common.xcconfig +0 -92
- package/libusb/Xcode/config.h +0 -31
- package/libusb/Xcode/debug.xcconfig +0 -32
- package/libusb/Xcode/libusb.xcconfig +0 -21
- package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +0 -1391
- package/libusb/Xcode/libusb_debug.xcconfig +0 -21
- package/libusb/Xcode/libusb_release.xcconfig +0 -21
- package/libusb/Xcode/release.xcconfig +0 -30
- package/libusb/android/README +0 -152
- package/libusb/android/config.h +0 -55
- package/libusb/android/examples/unrooted_android.c +0 -301
- package/libusb/android/examples/unrooted_android.h +0 -36
- package/libusb/android/jni/Android.mk +0 -23
- package/libusb/android/jni/Application.mk +0 -40
- package/libusb/android/jni/examples.mk +0 -168
- package/libusb/android/jni/libusb.mk +0 -60
- package/libusb/android/jni/tests.mk +0 -45
- package/libusb/appveyor.yml +0 -108
- package/libusb/autogen.sh +0 -10
- package/libusb/bootstrap.sh +0 -10
- package/libusb/configure.ac +0 -450
- package/libusb/doc/Makefile.in +0 -22
- package/libusb/doc/doxygen.cfg.in +0 -2571
- package/libusb/doc/libusb.png +0 -0
- package/libusb/examples/Makefile.am +0 -12
- package/libusb/examples/dpfp.c +0 -711
- package/libusb/examples/ezusb.c +0 -846
- package/libusb/examples/ezusb.h +0 -109
- package/libusb/examples/fxload.c +0 -310
- package/libusb/examples/hotplugtest.c +0 -147
- package/libusb/examples/listdevs.c +0 -73
- package/libusb/examples/sam3u_benchmark.c +0 -228
- package/libusb/examples/testlibusb.c +0 -312
- package/libusb/examples/xusb.c +0 -1254
- package/libusb/libusb/Makefile.am +0 -98
- package/libusb/libusb/Makefile.am.extra +0 -26
- package/libusb/libusb/core.c +0 -2925
- package/libusb/libusb/descriptor.c +0 -1558
- package/libusb/libusb/hotplug.c +0 -489
- package/libusb/libusb/io.c +0 -2865
- package/libusb/libusb/libusb-1.0.def +0 -199
- package/libusb/libusb/libusb-1.0.rc +0 -53
- package/libusb/libusb/libusb.h +0 -2421
- package/libusb/libusb/libusbi.h +0 -1535
- package/libusb/libusb/os/darwin_usb.c +0 -2977
- package/libusb/libusb/os/darwin_usb.h +0 -156
- package/libusb/libusb/os/emscripten_webusb.cpp +0 -875
- package/libusb/libusb/os/events_posix.c +0 -340
- package/libusb/libusb/os/events_posix.h +0 -62
- package/libusb/libusb/os/events_windows.c +0 -214
- package/libusb/libusb/os/events_windows.h +0 -46
- package/libusb/libusb/os/haiku_pollfs.cpp +0 -372
- package/libusb/libusb/os/haiku_usb.h +0 -113
- package/libusb/libusb/os/haiku_usb_backend.cpp +0 -532
- package/libusb/libusb/os/haiku_usb_raw.cpp +0 -231
- package/libusb/libusb/os/haiku_usb_raw.h +0 -188
- package/libusb/libusb/os/linux_netlink.c +0 -401
- package/libusb/libusb/os/linux_udev.c +0 -321
- package/libusb/libusb/os/linux_usbfs.c +0 -2829
- package/libusb/libusb/os/linux_usbfs.h +0 -221
- package/libusb/libusb/os/netbsd_usb.c +0 -617
- package/libusb/libusb/os/null_usb.c +0 -111
- package/libusb/libusb/os/openbsd_usb.c +0 -700
- package/libusb/libusb/os/sunos_usb.c +0 -1619
- package/libusb/libusb/os/sunos_usb.h +0 -79
- package/libusb/libusb/os/threads_posix.c +0 -126
- package/libusb/libusb/os/threads_posix.h +0 -98
- package/libusb/libusb/os/threads_windows.c +0 -40
- package/libusb/libusb/os/threads_windows.h +0 -113
- package/libusb/libusb/os/windows_common.c +0 -923
- package/libusb/libusb/os/windows_common.h +0 -424
- package/libusb/libusb/os/windows_usbdk.c +0 -724
- package/libusb/libusb/os/windows_usbdk.h +0 -106
- package/libusb/libusb/os/windows_winusb.c +0 -4766
- package/libusb/libusb/os/windows_winusb.h +0 -787
- package/libusb/libusb/strerror.c +0 -223
- package/libusb/libusb/sync.c +0 -342
- package/libusb/libusb/version.h +0 -18
- package/libusb/libusb/version_nano.h +0 -1
- package/libusb/libusb-1.0.pc.in +0 -11
- package/libusb/msvc/Base.props +0 -60
- package/libusb/msvc/Configuration.Application.props +0 -7
- package/libusb/msvc/Configuration.Base.props +0 -47
- package/libusb/msvc/Configuration.DynamicLibrary.props +0 -21
- package/libusb/msvc/Configuration.StaticLibrary.props +0 -7
- package/libusb/msvc/ProjectConfigurations.Base.props +0 -69
- package/libusb/msvc/build_all.ps1 +0 -17
- package/libusb/msvc/config.h +0 -58
- package/libusb/msvc/dpfp.vcxproj +0 -33
- package/libusb/msvc/dpfp_threaded.vcxproj +0 -38
- package/libusb/msvc/fxload.vcxproj +0 -46
- package/libusb/msvc/getopt/getopt.c +0 -1060
- package/libusb/msvc/getopt/getopt.h +0 -180
- package/libusb/msvc/getopt/getopt1.c +0 -188
- package/libusb/msvc/getopt.vcxproj +0 -33
- package/libusb/msvc/hotplugtest.vcxproj +0 -32
- package/libusb/msvc/init_context.vcxproj +0 -35
- package/libusb/msvc/libusb.sln +0 -542
- package/libusb/msvc/libusb_dll.vcxproj +0 -61
- package/libusb/msvc/libusb_static.vcxproj +0 -49
- package/libusb/msvc/listdevs.vcxproj +0 -32
- package/libusb/msvc/sam3u_benchmark.vcxproj +0 -33
- package/libusb/msvc/set_option.vcxproj +0 -35
- package/libusb/msvc/stress.vcxproj +0 -35
- package/libusb/msvc/stress_mt.vcxproj +0 -33
- package/libusb/msvc/testlibusb.vcxproj +0 -32
- package/libusb/msvc/xusb.vcxproj +0 -38
- package/libusb/tests/Makefile.am +0 -40
- package/libusb/tests/init_context.c +0 -153
- package/libusb/tests/libusb_testlib.h +0 -76
- package/libusb/tests/macos.c +0 -130
- package/libusb/tests/set_option.c +0 -253
- package/libusb/tests/stress.c +0 -172
- package/libusb/tests/stress_mt.c +0 -267
- package/libusb/tests/testlib.c +0 -184
- package/libusb/tests/umockdev.c +0 -1175
- package/libusb/tests/webusb-test-shim/index.js +0 -12
- package/libusb/tests/webusb-test-shim/package-lock.json +0 -50
- package/libusb/tests/webusb-test-shim/package.json +0 -10
- package/libusb.gypi +0 -154
- package/libusb_config/config.h +0 -1
- package/prebuilds/android-arm/node.napi.armv7.node +0 -0
- package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
- package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
- package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
- package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
- package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
- package/prebuilds/linux-ia32/node.napi.node +0 -0
- package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
- package/prebuilds/linux-x64/node.napi.musl.node +0 -0
- package/prebuilds/win32-arm64/node.napi.node +0 -0
- package/prebuilds/win32-ia32/node.napi.node +0 -0
- package/prebuilds/win32-x64/node.napi.node +0 -0
- package/src/device.cc +0 -439
- package/src/helpers.h +0 -64
- package/src/hotplug/hotplug.h +0 -22
- package/src/hotplug/libusb.cc +0 -90
- package/src/hotplug/windows.cc +0 -168
- package/src/node_usb.cc +0 -314
- package/src/node_usb.h +0 -131
- package/src/thread_name.cc +0 -79
- package/src/thread_name.h +0 -11
- package/src/transfer.cc +0 -143
- package/src/uv_async_queue.h +0 -41
- package/test/usb.coffee +0 -250
- package/test/webusb.coffee +0 -227
- package/test/worker.cjs +0 -13
package/LICENSE
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Copyright (c) 2026 Rob Moran
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,46 +1,34 @@
|
|
|
1
1
|
# USB Library for Node.JS
|
|
2
2
|
|
|
3
|
-
[](https://github.com/node-usb/node-usb-rs/actions)
|
|
4
4
|
[](https://www.npmjs.com/package/usb)
|
|
5
5
|
[](http://opensource.org/licenses/MIT)
|
|
6
6
|
|
|
7
7
|
Node.JS library for communicating with USB devices.
|
|
8
8
|
|
|
9
|
-
This is a
|
|
9
|
+
This is a complete rewrite in rust using [@kevinmehall](https://github.com/kevinmehall)'s excellent [nusb library](https://docs.rs/nusb/latest/nusb) and [napi-rs](https://napi.rs/).
|
|
10
10
|
|
|
11
11
|
# Prerequisites
|
|
12
12
|
|
|
13
|
-
[Node.js >=
|
|
13
|
+
[Node.js >= v12.22.0](https://nodejs.org), which includes `npm`.
|
|
14
14
|
|
|
15
15
|
## Windows
|
|
16
16
|
|
|
17
|
-
On Windows, if you get `
|
|
17
|
+
On Windows, if you get a `NOT_SUPPORTED` error when attempting to open your device, it's possible your device doesn't have a WinUSB driver to use.
|
|
18
18
|
|
|
19
|
-
You can install one using [Zadig](http://zadig.akeo.ie/)
|
|
20
|
-
|
|
21
|
-
Note that you cannot use multiple drivers on Windows as they get exclusive access to the device. So if you want to switch between drivers (e.g. using a printer with this software or the system), you will need to uninstall/install drivers as required.
|
|
22
|
-
|
|
23
|
-
For further info, check [How to use libusb on Windows](https://github.com/libusb/libusb/wiki/Windows#user-content-How_to_use_libusb_on_Windows) in the libusb's wiki.
|
|
19
|
+
You can install one using [Zadig](http://zadig.akeo.ie/).
|
|
24
20
|
|
|
25
21
|
## Linux
|
|
26
22
|
|
|
27
|
-
On Linux, you'll need libudev to build libusb if a prebuild is not available. On Ubuntu/Debian:
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
sudo apt-get install build-essential libudev-dev
|
|
31
|
-
```
|
|
32
|
-
|
|
33
23
|
You may need to modify your udev and permission rules in order to access your desired device. Along the lines of:
|
|
34
24
|
|
|
25
|
+
```code
|
|
35
26
|
SUBSYSTEM=="usb", ATTR{idVendor}=="USB-VENDOR-ID", ATTR{idProduct}=="USB-PRODUCT-ID", MODE="0660", GROUP="GROUP-YOUR-USER-IS-IN"
|
|
36
|
-
|
|
37
|
-
# Troubleshooting
|
|
38
|
-
|
|
39
|
-
For libusb issues, please refer to the FAQ at https://github.com/libusb/libusb/wiki/FAQ
|
|
27
|
+
```
|
|
40
28
|
|
|
41
29
|
# Installation
|
|
42
30
|
|
|
43
|
-
Native modules are bundled
|
|
31
|
+
Native modules are bundled as separate optional packages, so installation should be as simple as installing the package.
|
|
44
32
|
|
|
45
33
|
With `npm`:
|
|
46
34
|
|
|
@@ -54,153 +42,25 @@ With `yarn`:
|
|
|
54
42
|
yarn add usb
|
|
55
43
|
```
|
|
56
44
|
|
|
57
|
-
__Note:__ the library is now written in `TypeScript`, so a separate types file is not longer required to be installed (e.g. don't install `@types/usb`).
|
|
58
|
-
|
|
59
45
|
# License
|
|
60
46
|
[MIT](LICENSE.md)
|
|
61
47
|
|
|
62
|
-
Note that the compiled Node extension includes [libusb](https://github.com/libusb/libusb), and is thus subject to the [LGPL](https://github.com/libusb/libusb/blob/main/COPYING).
|
|
63
|
-
|
|
64
48
|
# Limitations
|
|
65
49
|
Does not support:
|
|
66
50
|
|
|
67
|
-
- Configurations other than the default one
|
|
68
51
|
- Isochronous transfers
|
|
69
52
|
|
|
70
53
|
# Getting Started
|
|
71
54
|
Use the following examples to kickstart your development. Once you have a desired device, use the APIs below to interact with it.
|
|
72
55
|
|
|
73
|
-
##
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
To use `v2.0.0` simply update your import statements and the function calls;
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
// import * as usb from 'usb';
|
|
80
|
-
// const devices: usb.Device[] = usb.getDeviceList();
|
|
81
|
-
|
|
82
|
-
import { usb, getDeviceList } from 'usb';
|
|
83
|
-
const devices: usb.Device[] = getDeviceList();
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
## List all legacy devices
|
|
87
|
-
```typescript
|
|
88
|
-
import { getDeviceList } from 'usb';
|
|
89
|
-
|
|
90
|
-
const devices = getDeviceList();
|
|
91
|
-
|
|
92
|
-
for (const device of devices) {
|
|
93
|
-
console.log(device); // Legacy device
|
|
94
|
-
}
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
## Find legacy device by vid/pid
|
|
98
|
-
```typescript
|
|
99
|
-
import { findByIds } from 'usb';
|
|
100
|
-
|
|
101
|
-
const device = findByIds(0x59e3, 0x0a23);
|
|
102
|
-
|
|
103
|
-
if (device) {
|
|
104
|
-
console.log(device); // Legacy device
|
|
105
|
-
}
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
## Find legacy device by SerialNumber
|
|
109
|
-
```typescript
|
|
110
|
-
import { findBySerialNumber } from 'usb';
|
|
111
|
-
|
|
112
|
-
(async () => {
|
|
113
|
-
// Uses a blocking call, so is async
|
|
114
|
-
const device = await findBySerialNumber('TEST_DEVICE');
|
|
115
|
-
|
|
116
|
-
if (device) {
|
|
117
|
-
console.log(device); // Legacy device
|
|
118
|
-
}
|
|
119
|
-
})();
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
## Turn legacy Device into WebUSB compatible device
|
|
123
|
-
```typescript
|
|
124
|
-
import { findBySerialNumber, WebUSBDevice } from 'usb';
|
|
125
|
-
|
|
126
|
-
(async () => {
|
|
127
|
-
// Uses a blocking call, so is async
|
|
128
|
-
const device = await findBySerialNumber('TEST_DEVICE');
|
|
129
|
-
|
|
130
|
-
// Uses blocking calls, so is async
|
|
131
|
-
const webDevice = await WebUSBDevice.createInstance(device);
|
|
132
|
-
|
|
133
|
-
if (webDevice) {
|
|
134
|
-
console.log(webDevice); // WebUSB device
|
|
135
|
-
}
|
|
136
|
-
})();
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## Use WebUSB approach to find a device
|
|
140
|
-
```typescript
|
|
141
|
-
import { webusb } from 'usb';
|
|
142
|
-
|
|
143
|
-
(async () => {
|
|
144
|
-
// Returns first matching device
|
|
145
|
-
const device = await webusb.requestDevice({
|
|
146
|
-
filters: [{}]
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
console.log(device); // WebUSB device
|
|
150
|
-
})();
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
## Use WebUSB approach to find a device with custom selection method
|
|
154
|
-
```typescript
|
|
155
|
-
import { WebUSB } from 'usb';
|
|
156
|
-
|
|
157
|
-
(async () => {
|
|
158
|
-
const customWebUSB = new WebUSB({
|
|
159
|
-
// This function can return a promise which allows a UI to be displayed if required
|
|
160
|
-
devicesFound: devices => devices.find(device => device.serialNumber === 'TEST_DEVICE')
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
// Returns device based on injected 'devicesFound' function
|
|
164
|
-
const device = await customWebUSB.requestDevice({
|
|
165
|
-
filters: [{}]
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
console.log(device); // WebUSB device
|
|
169
|
-
})();
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
## Use WebUSB approach to list authorised devices
|
|
173
|
-
```typescript
|
|
174
|
-
import { webusb } from 'usb';
|
|
56
|
+
## APIs
|
|
57
|
+
Since `v3.0.0`, the `node-usb` library supports the WebUSB API which follows the [WebUSB Specification](https://wicg.github.io/webusb/)
|
|
175
58
|
|
|
176
|
-
|
|
177
|
-
// The default webusb instance follows the WebUSB spec and only returns authorised devices
|
|
178
|
-
const devices = await webusb.getDevices();
|
|
59
|
+
Convenience methods also exist to easily list or find devices.
|
|
179
60
|
|
|
180
|
-
|
|
181
|
-
console.log(device); // WebUSB device
|
|
182
|
-
}
|
|
183
|
-
})();
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
## Use WebUSB approach to list all devices
|
|
187
|
-
```typescript
|
|
188
|
-
import { WebUSB } from 'usb';
|
|
189
|
-
|
|
190
|
-
(async () => {
|
|
191
|
-
const customWebUSB = new WebUSB({
|
|
192
|
-
// Bypass checking for authorised devices
|
|
193
|
-
allowAllDevices: true
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
// Uses blocking calls, so is async
|
|
197
|
-
const devices = await customWebUSB.getDevices();
|
|
61
|
+
Full auto-generated API documentation can be seen here:
|
|
198
62
|
|
|
199
|
-
|
|
200
|
-
console.log(device); // WebUSB device
|
|
201
|
-
}
|
|
202
|
-
})();
|
|
203
|
-
```
|
|
63
|
+
https://node-usb.github.io/node-usb-rs/
|
|
204
64
|
|
|
205
65
|
## Electron
|
|
206
66
|
Please refer to the maintained example for using `node-usb` in electron:
|
|
@@ -213,37 +73,16 @@ If using a packaging system for electron, ensure the `node-usb` library does not
|
|
|
213
73
|
- nodeGypRebuild: false
|
|
214
74
|
- npmRebuild: false
|
|
215
75
|
|
|
216
|
-
# APIs
|
|
217
|
-
Since `v2.0.0`, the `node-usb` library supports two APIs:
|
|
218
|
-
|
|
219
|
-
- `WebUSB` which follows the [WebUSB Specification](https://wicg.github.io/webusb/) (recommended)
|
|
220
|
-
- `Legacy API` which retains the previous 'non-blocking' API
|
|
221
|
-
|
|
222
|
-
Convenience methods also exist to easily list or find devices as well as convert between a legacy usb.Device device and WebUSB device.
|
|
223
|
-
|
|
224
|
-
Full auto-generated API documentation can be seen here:
|
|
225
|
-
|
|
226
|
-
https://node-usb.github.io/node-usb/
|
|
227
|
-
|
|
228
76
|
## Convenience Functions
|
|
229
77
|
|
|
230
78
|
### getDeviceList()
|
|
231
|
-
Return a list of
|
|
79
|
+
Return a list of `USB` objects for the USB devices attached to the system.
|
|
232
80
|
|
|
233
81
|
### findByIds(vid, pid)
|
|
234
|
-
Convenience method to get the first
|
|
82
|
+
Convenience method to get the first device with the specified VID and PID, or `undefined` if no such device is present.
|
|
235
83
|
|
|
236
84
|
### findBySerialNumber(serialNumber)
|
|
237
|
-
Convenience method to get
|
|
238
|
-
|
|
239
|
-
### getWebUsb()
|
|
240
|
-
Return the `navigator.usb` instance if it exists, otherwise a `webusb` instance.
|
|
241
|
-
|
|
242
|
-
### WebUSBDevice
|
|
243
|
-
WebUSB Device class for wrapping a legacy Device into a WebUSB device
|
|
244
|
-
|
|
245
|
-
#### WebUSBDevice.createInstance(device)
|
|
246
|
-
Convenience method to return a promise of a WebUSB device based on a legacy device
|
|
85
|
+
Convenience method to get the device with the specified serial number, or `undefined` if no such device is present.
|
|
247
86
|
|
|
248
87
|
## WebUSB
|
|
249
88
|
|
|
@@ -284,372 +123,42 @@ https://wicg.github.io/webusb/
|
|
|
284
123
|
- [x] releaseInterface()
|
|
285
124
|
- [x] selectAlternateInterface()
|
|
286
125
|
- [x] controlTransferIn()
|
|
287
|
-
- [x] controlTransferOut()
|
|
126
|
+
- [x] controlTransferOut()
|
|
288
127
|
- [x] transferIn()
|
|
289
|
-
- [x] transferOut()
|
|
128
|
+
- [x] transferOut()
|
|
290
129
|
- [x] clearHalt()
|
|
291
130
|
- [x] reset()
|
|
131
|
+
- [x] forget()
|
|
292
132
|
- [ ] isochronousTransferIn()
|
|
293
133
|
- [ ] isochronousTransferOut()
|
|
294
|
-
- [ ] forget()
|
|
295
134
|
|
|
296
135
|
#### Events
|
|
297
136
|
|
|
298
137
|
- [x] connect
|
|
299
138
|
- [x] disconnect
|
|
300
139
|
|
|
301
|
-
## Legacy API
|
|
302
|
-
|
|
303
|
-
### usb
|
|
304
|
-
Legacy usb object.
|
|
305
|
-
|
|
306
|
-
#### usb.LIBUSB_*
|
|
307
|
-
Constant properties from libusb
|
|
308
|
-
|
|
309
|
-
#### usb.getDeviceList()
|
|
310
|
-
Return a list of legacy `Device` objects for the USB devices attached to the system.
|
|
311
|
-
|
|
312
|
-
#### usb.pollHotplug
|
|
313
|
-
Force polling loop for hotplug events.
|
|
314
|
-
|
|
315
|
-
#### usb.setDebugLevel(level : int)
|
|
316
|
-
Set the libusb debug level (between 0 and 4)
|
|
317
|
-
|
|
318
|
-
#### usb.useUsbDkBackend()
|
|
319
|
-
On Windows, use the USBDK backend of libusb instead of WinUSB
|
|
320
|
-
|
|
321
|
-
### Device
|
|
322
|
-
Represents a USB device.
|
|
323
|
-
|
|
324
|
-
#### .busNumber
|
|
325
|
-
Integer USB device number
|
|
326
|
-
|
|
327
|
-
#### .deviceAddress
|
|
328
|
-
Integer USB device address
|
|
329
|
-
|
|
330
|
-
#### .portNumbers
|
|
331
|
-
Array containing the USB device port numbers, or `undefined` if not supported on this platform.
|
|
332
|
-
|
|
333
|
-
#### .deviceDescriptor
|
|
334
|
-
Object with properties for the fields of the device descriptor:
|
|
335
|
-
|
|
336
|
-
- bLength
|
|
337
|
-
- bDescriptorType
|
|
338
|
-
- bcdUSB
|
|
339
|
-
- bDeviceClass
|
|
340
|
-
- bDeviceSubClass
|
|
341
|
-
- bDeviceProtocol
|
|
342
|
-
- bMaxPacketSize0
|
|
343
|
-
- idVendor
|
|
344
|
-
- idProduct
|
|
345
|
-
- bcdDevice
|
|
346
|
-
- iManufacturer
|
|
347
|
-
- iProduct
|
|
348
|
-
- iSerialNumber
|
|
349
|
-
- bNumConfigurations
|
|
350
|
-
|
|
351
|
-
#### .configDescriptor
|
|
352
|
-
Object with properties for the fields of the configuration descriptor:
|
|
353
|
-
|
|
354
|
-
- bLength
|
|
355
|
-
- bDescriptorType
|
|
356
|
-
- wTotalLength
|
|
357
|
-
- bNumInterfaces
|
|
358
|
-
- bConfigurationValue
|
|
359
|
-
- iConfiguration
|
|
360
|
-
- bmAttributes
|
|
361
|
-
- bMaxPower
|
|
362
|
-
- extra (Buffer containing any extra data or additional descriptors)
|
|
363
|
-
|
|
364
|
-
#### .allConfigDescriptors
|
|
365
|
-
Contains all config descriptors of the device (same structure as .configDescriptor above)
|
|
366
|
-
|
|
367
|
-
#### .parent
|
|
368
|
-
Contains the parent of the device, such as a hub. If there is no parent this property is set to `null`.
|
|
369
|
-
|
|
370
|
-
#### .open()
|
|
371
|
-
Open the device. All methods below require the device to be open before use.
|
|
372
|
-
|
|
373
|
-
#### .close()
|
|
374
|
-
Close the device.
|
|
375
|
-
|
|
376
|
-
#### .controlTransfer(bmRequestType, bRequest, wValue, wIndex, data_or_length, callback(error, data))
|
|
377
|
-
Perform a control transfer with `libusb_control_transfer`.
|
|
378
|
-
|
|
379
|
-
Parameter `data_or_length` can be a integer length for an IN transfer, or a Buffer for an out transfer. The type must match the direction specified in the MSB of bmRequestType.
|
|
380
|
-
|
|
381
|
-
The `data` parameter of the callback is always undefined for OUT transfers, or will be passed a Buffer for IN transfers.
|
|
382
|
-
|
|
383
|
-
A [package is available to calculate bmRequestType](https://www.npmjs.com/package/bmrequesttype) if needed.
|
|
384
|
-
|
|
385
|
-
#### .setConfiguration(id, callback(error))
|
|
386
|
-
Set the device configuration to something other than the default (0). To use this, first call `.open(false)` (which tells it not to auto configure), then before claiming an interface, call this method.
|
|
387
|
-
|
|
388
|
-
#### .getStringDescriptor(index, callback(error, data))
|
|
389
|
-
Perform a control transfer to retrieve a string descriptor
|
|
390
|
-
|
|
391
|
-
#### .getBosDescriptor(callback(error, bosDescriptor))
|
|
392
|
-
Perform a control transfer to retrieve an object with properties for the fields of the Binary Object Store descriptor:
|
|
393
|
-
|
|
394
|
-
- bLength
|
|
395
|
-
- bDescriptorType
|
|
396
|
-
- wTotalLength
|
|
397
|
-
- bNumDeviceCaps
|
|
398
|
-
|
|
399
|
-
#### .getCapabilities(callback(error, capabilities))
|
|
400
|
-
Retrieve a list of Capability objects for the Binary Object Store capabilities of the device.
|
|
401
|
-
|
|
402
|
-
#### .interface(interface)
|
|
403
|
-
Return the interface with the specified interface number.
|
|
404
|
-
|
|
405
|
-
#### .interfaces
|
|
406
|
-
List of Interface objects for the interfaces of the default configuration of the device.
|
|
407
|
-
|
|
408
|
-
#### .timeout
|
|
409
|
-
Timeout in milliseconds to use for control transfers.
|
|
410
|
-
|
|
411
|
-
#### .reset(callback(error))
|
|
412
|
-
Performs a reset of the device. Callback is called when complete.
|
|
413
|
-
|
|
414
|
-
#### .setAutoDetachKernelDriver(enable)
|
|
415
|
-
Enable/disable libusb's automatic kernel driver detachment (defaults to true in the WebUSB API for non-Windows platforms)
|
|
416
|
-
|
|
417
|
-
### Interface
|
|
418
|
-
|
|
419
|
-
#### .endpoint(address)
|
|
420
|
-
Return the InEndpoint or OutEndpoint with the specified address.
|
|
421
|
-
|
|
422
|
-
#### .endpoints
|
|
423
|
-
List of endpoints on this interface: InEndpoint and OutEndpoint objects.
|
|
424
|
-
|
|
425
|
-
#### .interface
|
|
426
|
-
Integer interface number.
|
|
427
|
-
|
|
428
|
-
#### .altSetting
|
|
429
|
-
Integer alternate setting number.
|
|
430
|
-
|
|
431
|
-
#### .setAltSetting(altSetting, callback(error))
|
|
432
|
-
Sets the alternate setting. It updates the `interface.endpoints` array to reflect the endpoints found in the alternate setting.
|
|
433
|
-
|
|
434
|
-
#### .claim()
|
|
435
|
-
Claims the interface. This method must be called before using any endpoints of this interface.
|
|
436
|
-
|
|
437
|
-
#### .release([closeEndpoints], callback(error))
|
|
438
|
-
Releases the interface and resets the alternate setting. Calls callback when complete.
|
|
439
|
-
|
|
440
|
-
It is an error to release an interface with pending transfers. If the optional closeEndpoints parameter is true, any active endpoint streams are stopped (see `Endpoint.stopStream`), and the interface is released after the stream transfers are cancelled. Transfers submitted individually with `Endpoint.transfer` are not affected by this parameter.
|
|
441
|
-
|
|
442
|
-
#### .isKernelDriverActive()
|
|
443
|
-
Returns `false` if a kernel driver is not active; `true` if active.
|
|
444
|
-
|
|
445
|
-
#### .detachKernelDriver()
|
|
446
|
-
Detaches the kernel driver from the interface.
|
|
447
|
-
If a `LIBUSB_ERROR_ACCESS` error is raised, you may need to execute this with elevated privileges.
|
|
448
|
-
|
|
449
|
-
#### .attachKernelDriver()
|
|
450
|
-
Re-attaches the kernel driver for the interface.
|
|
451
|
-
|
|
452
|
-
#### .descriptor
|
|
453
|
-
Object with fields from the interface descriptor -- see libusb documentation or USB spec.
|
|
454
|
-
|
|
455
|
-
- bLength
|
|
456
|
-
- bDescriptorType
|
|
457
|
-
- bInterfaceNumber
|
|
458
|
-
- bAlternateSetting
|
|
459
|
-
- bNumEndpoints
|
|
460
|
-
- bInterfaceClass
|
|
461
|
-
- bInterfaceSubClass
|
|
462
|
-
- bInterfaceProtocol
|
|
463
|
-
- iInterface
|
|
464
|
-
- extra (Buffer containing any extra data or additional descriptors)
|
|
465
|
-
|
|
466
|
-
### Capability
|
|
467
|
-
|
|
468
|
-
#### .type
|
|
469
|
-
Integer capability type.
|
|
470
|
-
|
|
471
|
-
#### .data
|
|
472
|
-
Buffer capability data.
|
|
473
|
-
|
|
474
|
-
#### .descriptor
|
|
475
|
-
Object with fields from the capability descriptor -- see libusb documentation or USB spec.
|
|
476
|
-
|
|
477
|
-
- bLength
|
|
478
|
-
- bDescriptorType
|
|
479
|
-
- bDevCapabilityType
|
|
480
|
-
|
|
481
|
-
### Endpoint
|
|
482
|
-
Common base for InEndpoint and OutEndpoint, see below.
|
|
483
|
-
|
|
484
|
-
#### .direction
|
|
485
|
-
Endpoint direction: `"in"` or `"out"`.
|
|
486
|
-
|
|
487
|
-
#### .transferType
|
|
488
|
-
Endpoint type: `usb.LIBUSB_TRANSFER_TYPE_BULK`, `usb.LIBUSB_TRANSFER_TYPE_INTERRUPT`, or `usb.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS`.
|
|
489
|
-
|
|
490
|
-
#### .descriptor
|
|
491
|
-
Object with fields from the endpoint descriptor -- see libusb documentation or USB spec.
|
|
492
|
-
|
|
493
|
-
- bLength
|
|
494
|
-
- bDescriptorType
|
|
495
|
-
- bEndpointAddress
|
|
496
|
-
- bmAttributes
|
|
497
|
-
- wMaxPacketSize
|
|
498
|
-
- bInterval
|
|
499
|
-
- bRefresh
|
|
500
|
-
- bSynchAddress
|
|
501
|
-
- extra (Buffer containing any extra data or additional descriptors)
|
|
502
|
-
|
|
503
|
-
#### .timeout
|
|
504
|
-
Sets the timeout in milliseconds for transfers on this endpoint. The default, `0`, is infinite timeout.
|
|
505
|
-
|
|
506
|
-
#### .clearHalt(callback(error))
|
|
507
|
-
Clear the halt/stall condition for this endpoint.
|
|
508
|
-
|
|
509
|
-
### InEndpoint
|
|
510
|
-
Endpoints in the IN direction (device->PC) have this type.
|
|
511
|
-
|
|
512
|
-
#### .transfer(length, callback(error, data))
|
|
513
|
-
Perform a transfer to read data from the endpoint.
|
|
514
|
-
|
|
515
|
-
If length is greater than maxPacketSize, libusb will automatically split the transfer in multiple packets, and you will receive one callback with all data once all packets are complete.
|
|
516
|
-
|
|
517
|
-
`this` in the callback is the InEndpoint object.
|
|
518
|
-
|
|
519
|
-
#### .startPoll(nTransfers=3, transferSize=maxPacketSize)
|
|
520
|
-
Start polling the endpoint.
|
|
521
|
-
|
|
522
|
-
The library will keep `nTransfers` transfers of size `transferSize` pending in
|
|
523
|
-
the kernel at all times to ensure continuous data flow. This is handled by the
|
|
524
|
-
libusb event thread, so it continues even if the Node v8 thread is busy. The
|
|
525
|
-
`data` and `error` events are emitted as transfers complete.
|
|
526
|
-
|
|
527
|
-
#### .stopPoll(cb)
|
|
528
|
-
Stop polling.
|
|
529
|
-
|
|
530
|
-
Further data may still be received. The `end` event is emitted and the callback
|
|
531
|
-
is called once all transfers have completed or canceled.
|
|
532
|
-
|
|
533
|
-
#### Event: data(data : Buffer)
|
|
534
|
-
Emitted with data received by the polling transfers
|
|
535
|
-
|
|
536
|
-
#### Event: error(error)
|
|
537
|
-
Emitted when polling encounters an error. All in flight transfers will be automatically canceled and no further polling will be done. You have to wait for the `end` event before you can start polling again.
|
|
538
|
-
|
|
539
|
-
#### Event: end
|
|
540
|
-
Emitted when polling has been canceled
|
|
541
|
-
|
|
542
|
-
### OutEndpoint
|
|
543
|
-
Endpoints in the OUT direction (PC->device) have this type.
|
|
544
|
-
|
|
545
|
-
#### .transfer(data, callback(error))
|
|
546
|
-
Perform a transfer to write `data` to the endpoint.
|
|
547
|
-
|
|
548
|
-
If length is greater than maxPacketSize, libusb will automatically split the transfer in multiple packets, and you will receive one callback once all packets are complete.
|
|
549
|
-
|
|
550
|
-
`this` in the callback is the OutEndpoint object.
|
|
551
|
-
|
|
552
|
-
#### Event: error(error)
|
|
553
|
-
Emitted when the stream encounters an error.
|
|
554
|
-
|
|
555
|
-
#### Event: end
|
|
556
|
-
Emitted when the stream has been stopped and all pending requests have been completed.
|
|
557
|
-
|
|
558
|
-
### UsbDetection
|
|
559
|
-
|
|
560
|
-
#### usb.on('attach', function(device) { ... });
|
|
561
|
-
Attaches a callback to plugging in a `device`.
|
|
562
|
-
|
|
563
|
-
#### usb.on('detach', function(device) { ... });
|
|
564
|
-
Attaches a callback to unplugging a `device`.
|
|
565
|
-
|
|
566
|
-
#### usb.refHotplugEvents();
|
|
567
|
-
Restore (re-reference) the hotplug events unreferenced by `unrefHotplugEvents()`
|
|
568
|
-
|
|
569
|
-
#### usb.unrefHotplugEvents();
|
|
570
|
-
Listening to events will prevent the process to exit. By calling this function, hotplug events will be unreferenced by the event loop, allowing the process to exit even when listening for the `attach` and `detach` events.
|
|
571
|
-
|
|
572
|
-
# Migrating from node-usb-detection
|
|
573
|
-
|
|
574
|
-
If you have been referred here by `node-usb-detection`, the following may be helpful for you to update your existing code.
|
|
575
|
-
|
|
576
|
-
## usbDetect.startMonitoring() & usbDetect.stopMonitoring()
|
|
577
|
-
|
|
578
|
-
There is no direct equivalent to these methods. This is handled automatically for you when you add and remove event listeners.
|
|
579
|
-
|
|
580
|
-
You may find `usb.unrefHotplugEvents()` useful as it is intended to help with exit conditions.
|
|
581
|
-
|
|
582
|
-
## usbDetect.find()
|
|
583
|
-
|
|
584
|
-
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.
|
|
585
|
-
|
|
586
|
-
To find a specific device by vid and pid, call `usb.findByIds`. e.g. `usb.findByIds(0x12, 0x34)`.
|
|
587
|
-
|
|
588
|
-
## usbDetect.on('add', function(device) { ... })
|
|
589
|
-
|
|
590
|
-
These should be changed to `usb.on('attach', function(device) { ... })`.
|
|
591
|
-
|
|
592
|
-
There is no equivalent to filter based on the vid or pid, instead you should do a check inside the callback you provide.
|
|
593
|
-
The contents of the device object has also changed.
|
|
594
|
-
|
|
595
|
-
## usbDetect.on('remove', function(device) { ... })
|
|
596
|
-
|
|
597
|
-
These should be changed to `usb.on('detach', function(device) { ... })`.
|
|
598
|
-
|
|
599
|
-
There is no equivalent to filter based on the vid or pid, instead you should do a check inside the callback you provide.
|
|
600
|
-
The contents of the device object has also changed.
|
|
601
|
-
|
|
602
|
-
## usbDetect.on('change', function(device) { ... })
|
|
603
|
-
|
|
604
|
-
There is no direct equivalent to this. Instead you can listen to both `attach` and `detach` to get the same behaviour.
|
|
605
|
-
|
|
606
140
|
# Development
|
|
607
|
-
The library is based on native bindings wrapping the [
|
|
141
|
+
The library is based on native rust bindings wrapping the [nusb](https://docs.rs/nusb/latest/nusb) crate.
|
|
608
142
|
|
|
609
143
|
## Setup
|
|
610
|
-
Libusb is included as a submodule, clone this repository and then the submodule as follows:
|
|
611
144
|
|
|
612
145
|
```bash
|
|
613
|
-
git clone https://github.com/node-usb/node-usb
|
|
614
|
-
cd node-usb
|
|
615
|
-
git submodule update --init
|
|
146
|
+
git clone https://github.com/node-usb/node-usb-rs
|
|
616
147
|
```
|
|
617
148
|
|
|
618
|
-
Alternatively, if you want to build this library against your system `libusb` set the `use_system_libusb` variable to `true` in the [binding.gyp](./binding.gyp) and [libusb.gypi](./libusb.gypi) files.
|
|
619
|
-
|
|
620
149
|
## Building
|
|
621
|
-
The package
|
|
622
|
-
|
|
623
|
-
```bash
|
|
624
|
-
yarn
|
|
625
|
-
yarn compile
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
The native binaries can be rebuilt with:
|
|
629
|
-
|
|
630
|
-
```bash
|
|
631
|
-
yarn rebuild
|
|
632
|
-
```
|
|
633
|
-
|
|
634
|
-
__Note:__ On Linux, you'll need libudev to build libusb. On Ubuntu/Debian:
|
|
150
|
+
The package can be built as follows:
|
|
635
151
|
|
|
636
152
|
```bash
|
|
637
|
-
|
|
153
|
+
npm install
|
|
154
|
+
npm run build:all
|
|
638
155
|
```
|
|
639
156
|
|
|
640
157
|
## Testing
|
|
641
158
|
To execute the unit tests, Run:
|
|
642
159
|
|
|
643
160
|
```bash
|
|
644
|
-
|
|
161
|
+
npm run full-test
|
|
645
162
|
```
|
|
646
163
|
|
|
647
164
|
Some tests require an [attached STM32F103 Microprocessor USB device with specific firmware](https://github.com/node-usb/node-usb-test-firmware).
|
|
648
|
-
|
|
649
|
-
```bash
|
|
650
|
-
yarn full-test
|
|
651
|
-
yarn valgrind
|
|
652
|
-
```
|
|
653
|
-
|
|
654
|
-
## Releasing
|
|
655
|
-
Please refer to the [Wiki](https://github.com/node-usb/node-usb/wiki/Release-Process) for release instructions.
|