usb 2.5.2-alpha.1 → 2.6.0

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/index.d.ts +10 -6
  3. package/dist/index.js +27 -11
  4. package/dist/index.js.map +1 -1
  5. package/dist/usb/endpoint.js +3 -1
  6. package/dist/usb/endpoint.js.map +1 -1
  7. package/dist/webusb/index.js +40 -24
  8. package/dist/webusb/index.js.map +1 -1
  9. package/dist/webusb/webusb-device.d.ts +1 -1
  10. package/dist/webusb/webusb-device.js +24 -30
  11. package/dist/webusb/webusb-device.js.map +1 -1
  12. package/package.json +12 -12
  13. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  14. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  15. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  16. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  17. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  18. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  19. package/prebuilds/linux-ia32/node.napi.node +0 -0
  20. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  21. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  22. package/prebuilds/win32-ia32/node.napi.node +0 -0
  23. package/prebuilds/win32-x64/node.napi.node +0 -0
  24. package/.eslintignore +0 -4
  25. package/.eslintrc.json +0 -87
  26. package/.gitattributes +0 -1
  27. package/.gitmodules +0 -3
  28. package/.vscode/launch.json +0 -15
  29. package/.vscode/tasks.json +0 -23
  30. package/libusb/.gitattributes +0 -7
  31. package/libusb/msvc/.gitattributes +0 -3
  32. package/tsc/index.ts +0 -75
  33. package/tsc/usb/bindings.ts +0 -310
  34. package/tsc/usb/capability.ts +0 -22
  35. package/tsc/usb/descriptors.ts +0 -180
  36. package/tsc/usb/device.ts +0 -331
  37. package/tsc/usb/endpoint.ts +0 -229
  38. package/tsc/usb/index.ts +0 -149
  39. package/tsc/usb/interface.ts +0 -167
  40. package/tsc/webusb/index.ts +0 -374
  41. package/tsc/webusb/webusb-device.ts +0 -490
  42. package/tsconfig.json +0 -17
  43. package/typedoc.json +0 -9
@@ -1,229 +0,0 @@
1
- import { EventEmitter } from 'events';
2
- import { LibUSBException, LIBUSB_TRANSFER_CANCELLED, Transfer, Device } from './bindings';
3
- import { EndpointDescriptor } from './descriptors';
4
-
5
- const isBuffer = (obj: ArrayBuffer | Buffer): obj is Uint8Array => obj && obj instanceof Uint8Array;
6
-
7
- /** Common base for InEndpoint and OutEndpoint. */
8
- export abstract class Endpoint extends EventEmitter {
9
- public address: number;
10
-
11
- /** Endpoint direction: `"in"` or `"out"`. */
12
- public abstract direction: 'in' | 'out';
13
-
14
- /** Endpoint type: `usb.LIBUSB_TRANSFER_TYPE_BULK`, `usb.LIBUSB_TRANSFER_TYPE_INTERRUPT`, or `usb.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS`. */
15
- public transferType: number;
16
-
17
- /** Sets the timeout in milliseconds for transfers on this endpoint. The default, `0`, is infinite timeout. */
18
- public timeout = 0;
19
-
20
- /** Object with fields from the endpoint descriptor -- see libusb documentation or USB spec. */
21
- public descriptor: EndpointDescriptor;
22
-
23
- constructor(protected device: Device, descriptor: EndpointDescriptor) {
24
- super();
25
- this.descriptor = descriptor;
26
- this.address = descriptor.bEndpointAddress;
27
- this.transferType = descriptor.bmAttributes & 0x03;
28
- }
29
-
30
- /** Clear the halt/stall condition for this endpoint. */
31
- public clearHalt(callback: (error: LibUSBException | undefined) => void): void {
32
- return this.device.__clearHalt(this.address, callback);
33
- }
34
-
35
- /**
36
- * Create a new `Transfer` object for this endpoint.
37
- *
38
- * The passed callback will be called when the transfer is submitted and finishes. Its arguments are the error (if any), the submitted buffer, and the amount of data actually written (for
39
- * OUT transfers) or read (for IN transfers).
40
- *
41
- * @param timeout Timeout for the transfer (0 means unlimited).
42
- * @param callback Transfer completion callback.
43
- */
44
- public makeTransfer(timeout: number, callback: (error: LibUSBException | undefined, buffer: Buffer, actualLength: number) => void): Transfer {
45
- return new Transfer(this.device, this.address, this.transferType, timeout, callback);
46
- }
47
- }
48
-
49
- /** Endpoints in the IN direction (device->PC) have this type. */
50
- export class InEndpoint extends Endpoint {
51
-
52
- /** Endpoint direction. */
53
- public direction: 'in' | 'out' = 'in';
54
-
55
- protected pollTransfers: Transfer[] = [];
56
- protected pollTransferSize = 0;
57
- protected pollPending = 0;
58
- public pollActive = false;
59
-
60
- constructor(device: Device, descriptor: EndpointDescriptor) {
61
- super(device, descriptor);
62
- }
63
-
64
- /**
65
- * Perform a transfer to read data from the endpoint.
66
- *
67
- * 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.
68
- *
69
- * `this` in the callback is the InEndpoint object.
70
- *
71
- * The device must be open to use this method.
72
- * @param length
73
- * @param callback
74
- */
75
- public transfer(length: number, callback: (error: LibUSBException | undefined, data?: Buffer) => void): InEndpoint {
76
- const buffer = Buffer.alloc(length);
77
-
78
- const cb = (error: LibUSBException | undefined, _buffer?: Buffer, actualLength?: number) => {
79
- callback.call(this, error, buffer.slice(0, actualLength));
80
- };
81
-
82
- try {
83
- this.makeTransfer(this.timeout, cb).submit(buffer);
84
- } catch (e) {
85
- process.nextTick(() => callback.call(this, e));
86
- }
87
- return this;
88
- }
89
-
90
- /**
91
- * Start polling the endpoint.
92
- *
93
- * The library will keep `nTransfers` transfers of size `transferSize` pending in the kernel at all times to ensure continuous data flow.
94
- * This is handled by the libusb event thread, so it continues even if the Node v8 thread is busy. The `data` and `error` events are emitted as transfers complete.
95
- *
96
- * The device must be open to use this method.
97
- * @param nTransfers
98
- * @param transferSize
99
- */
100
- public startPoll(nTransfers?: number, transferSize?: number, _callback?: (error: LibUSBException | undefined, buffer: Buffer, actualLength: number) => void): Transfer[] {
101
- const transferDone = (error: LibUSBException | undefined, transfer: Transfer, buffer: Buffer, actualLength: number) => {
102
- if (!error) {
103
- this.emit('data', buffer.slice(0, actualLength));
104
- } else if (error.errno != LIBUSB_TRANSFER_CANCELLED) {
105
- this.emit('error', error);
106
- this.stopPoll();
107
- }
108
-
109
- if (this.pollActive) {
110
- startTransfer(transfer);
111
- } else {
112
- this.pollPending--;
113
-
114
- if (this.pollPending === 0) {
115
- this.pollTransfers = [];
116
- this.pollActive = false;
117
- this.emit('end');
118
- }
119
- }
120
- };
121
-
122
- const startTransfer = (transfer: Transfer) => {
123
- try {
124
- transfer.submit(Buffer.alloc(this.pollTransferSize), (error, buffer, actualLength) => {
125
- transferDone(error, transfer, buffer, actualLength);
126
- });
127
- } catch (e) {
128
- this.emit('error', e);
129
- this.stopPoll();
130
- }
131
- };
132
-
133
- this.pollTransfers = this.startPollTransfers(nTransfers, transferSize, function (this: Transfer, error, buffer, actualLength) {
134
- transferDone(error, this, buffer, actualLength);
135
- });
136
- this.pollTransfers.forEach(startTransfer);
137
- this.pollPending = this.pollTransfers.length;
138
- return this.pollTransfers;
139
- }
140
-
141
- protected startPollTransfers(nTransfers = 3, transferSize = this.descriptor.wMaxPacketSize, callback: (error: LibUSBException | undefined, buffer: Buffer, actualLength: number) => void): Transfer[] {
142
- if (this.pollActive) {
143
- throw new Error('Polling already active');
144
- }
145
-
146
- this.pollTransferSize = transferSize;
147
- this.pollActive = true;
148
- this.pollPending = 0;
149
-
150
- const transfers: Transfer[] = [];
151
- for (let i = 0; i < nTransfers; i++) {
152
- const transfer = this.makeTransfer(0, callback);
153
- transfers[i] = transfer;
154
- }
155
- return transfers;
156
- }
157
-
158
- /**
159
- * Stop polling.
160
- *
161
- * Further data may still be received. The `end` event is emitted and the callback is called once all transfers have completed or canceled.
162
- *
163
- * The device must be open to use this method.
164
- * @param callback
165
- */
166
- public stopPoll(callback?: () => void): void {
167
- if (!this.pollActive) {
168
- throw new Error('Polling is not active.');
169
- }
170
- for (let i = 0; i < this.pollTransfers.length; i++) {
171
- try {
172
- this.pollTransfers[i].cancel();
173
- } catch (error) {
174
- this.emit('error', error);
175
- }
176
- }
177
- this.pollActive = false;
178
- if (callback) this.once('end', callback);
179
- }
180
- }
181
-
182
- /** Endpoints in the OUT direction (PC->device) have this type. */
183
- export class OutEndpoint extends Endpoint {
184
-
185
- /** Endpoint direction. */
186
- public direction: 'in' | 'out' = 'out';
187
-
188
- /**
189
- * Perform a transfer to write `data` to the endpoint.
190
- *
191
- * 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.
192
- *
193
- * `this` in the callback is the OutEndpoint object.
194
- *
195
- * The device must be open to use this method.
196
- * @param buffer
197
- * @param callback
198
- */
199
- public transfer(buffer: Buffer, callback?: (error: LibUSBException | undefined, actual: number) => void): OutEndpoint {
200
- if (!buffer) {
201
- buffer = Buffer.alloc(0);
202
- } else if (!isBuffer(buffer)) {
203
- buffer = Buffer.from(buffer);
204
- }
205
-
206
- const cb = (error: LibUSBException | undefined, _buffer?: Buffer, actual?: number) => {
207
- if (callback) {
208
- callback.call(this, error, actual || 0);
209
- }
210
- };
211
-
212
- try {
213
- this.makeTransfer(this.timeout, cb).submit(buffer);
214
- } catch (e) {
215
- process.nextTick(() => cb(e));
216
- }
217
-
218
- return this;
219
- }
220
-
221
- public transferWithZLP(buffer: Buffer, callback: (error: LibUSBException | undefined) => void): void {
222
- if (buffer.length % this.descriptor.wMaxPacketSize === 0) {
223
- this.transfer(buffer);
224
- this.transfer(Buffer.alloc(0), callback);
225
- } else {
226
- this.transfer(buffer, callback);
227
- }
228
- }
229
- }
package/tsc/usb/index.ts DELETED
@@ -1,149 +0,0 @@
1
- import { EventEmitter } from 'events';
2
- import { ExtendedDevice } from './device';
3
- import * as usb from './bindings';
4
-
5
- if (usb.INIT_ERROR) {
6
- /* eslint-disable no-console */
7
- console.warn('Failed to initialize libusb.');
8
- }
9
-
10
- Object.setPrototypeOf(usb, EventEmitter.prototype);
11
-
12
- Object.getOwnPropertyNames(ExtendedDevice.prototype).forEach(name => {
13
- Object.defineProperty(usb.Device.prototype, name, Object.getOwnPropertyDescriptor(ExtendedDevice.prototype, name) || Object.create(null));
14
- });
15
-
16
- interface EventListeners<T> {
17
- newListener: keyof T;
18
- removeListener: keyof T;
19
- }
20
-
21
- interface DeviceIds {
22
- idVendor: number;
23
- idProduct: number;
24
- }
25
-
26
- declare module './bindings' {
27
-
28
- /* eslint-disable @typescript-eslint/no-empty-interface */
29
- interface Device extends ExtendedDevice { }
30
-
31
- interface DeviceEvents extends EventListeners<DeviceEvents> {
32
- attach: Device;
33
- detach: Device;
34
- attachIds: DeviceIds;
35
- detachIds: DeviceIds;
36
- }
37
-
38
- function addListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
39
- function removeListener<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
40
- function on<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
41
- function off<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
42
- function once<K extends keyof DeviceEvents>(event: K, listener: (arg: DeviceEvents[K]) => void): void;
43
- function listeners<K extends keyof DeviceEvents>(event: K): ((arg: DeviceEvents[K]) => void)[];
44
- function rawListeners<K extends keyof DeviceEvents>(event: K): ((arg: DeviceEvents[K]) => void)[];
45
- function removeAllListeners<K extends keyof DeviceEvents>(event?: K): void;
46
- function emit<K extends keyof DeviceEvents>(event: K, arg: DeviceEvents[K]): boolean;
47
- function listenerCount<K extends keyof DeviceEvents>(event: K): number;
48
- }
49
-
50
- // Hotplug support
51
- const hotplugSupported = usb._supportedHotplugEvents();
52
-
53
- // Devices delta support for non-libusb hotplug events
54
- // This methd needs to be used for attach/detach IDs (hotplugSupportType === 2) rather than a lookup because vid/pid are not unique
55
- let hotPlugDevices = new Set<usb.Device>();
56
- const emitHotplugEvents = () => {
57
- // Collect current devices
58
- const devices = new Set(usb.getDeviceList());
59
-
60
- // Find attached devices
61
- for (const device of devices) {
62
- if (!hotPlugDevices.has(device)) {
63
- usb.emit('attach', device);
64
- }
65
- }
66
-
67
- // Find detached devices
68
- for (const device of hotPlugDevices) {
69
- if (!devices.has(device)) {
70
- usb.emit('detach', device);
71
- }
72
- }
73
-
74
- hotPlugDevices = devices;
75
- };
76
-
77
- // Polling mechanism for checking device changes where hotplug detection is not available
78
- let pollingHotplug = false;
79
- const pollHotplug = (start = false) => {
80
- if (start) {
81
- pollingHotplug = true;
82
- } else if (!pollingHotplug) {
83
- return;
84
- } else {
85
- emitHotplugEvents();
86
- }
87
-
88
- setTimeout(() => pollHotplug(), 500);
89
- };
90
-
91
- // Hotplug control
92
- const startHotplug = () => {
93
- if (hotplugSupported !== 1) {
94
- // Collect initial devices when not using libusb
95
- hotPlugDevices = new Set(usb.getDeviceList());
96
- }
97
-
98
- if (hotplugSupported) {
99
- // Use hotplug event emitters
100
- usb._enableHotplugEvents();
101
-
102
- if (hotplugSupported === 2) {
103
- // Use hotplug ID events to trigger a change check
104
- usb.on('attachIds', emitHotplugEvents);
105
- usb.on('detachIds', emitHotplugEvents);
106
- }
107
- } else {
108
- // Fallback to using polling to check for changes
109
- pollHotplug(true);
110
- }
111
- };
112
-
113
- const stopHotplug = () => {
114
- if (hotplugSupported) {
115
- // Disable hotplug events
116
- usb._disableHotplugEvents();
117
-
118
- if (hotplugSupported === 2) {
119
- // Remove hotplug ID event listeners
120
- usb.off('attachIds', emitHotplugEvents);
121
- usb.off('detachIds', emitHotplugEvents);
122
- }
123
- } else {
124
- // Stop polling
125
- pollingHotplug = false;
126
- }
127
- };
128
-
129
- usb.on('newListener', event => {
130
- if (event !== 'attach' && event !== 'detach') {
131
- return;
132
- }
133
- const listenerCount = usb.listenerCount('attach') + usb.listenerCount('detach');
134
- if (listenerCount === 0) {
135
- startHotplug();
136
- }
137
- });
138
-
139
- usb.on('removeListener', event => {
140
- if (event !== 'attach' && event !== 'detach') {
141
- return;
142
- }
143
- const listenerCount = usb.listenerCount('attach') + usb.listenerCount('detach');
144
- if (listenerCount === 0) {
145
- stopHotplug();
146
- }
147
- });
148
-
149
- export = usb;
@@ -1,167 +0,0 @@
1
- import { LibUSBException, LIBUSB_ENDPOINT_IN, Device } from './bindings';
2
- import { InterfaceDescriptor } from './descriptors';
3
- import { Endpoint, InEndpoint, OutEndpoint } from './endpoint';
4
-
5
- export class Interface {
6
- /** Integer interface number. */
7
- public interfaceNumber!: number;
8
-
9
- /** Integer alternate setting number. */
10
- public altSetting = 0;
11
-
12
- /** Object with fields from the interface descriptor -- see libusb documentation or USB spec. */
13
- public descriptor!: InterfaceDescriptor;
14
-
15
- /** List of endpoints on this interface: InEndpoint and OutEndpoint objects. */
16
- public endpoints!: Endpoint[];
17
-
18
- constructor(protected device: Device, protected id: number) {
19
- this.refresh();
20
- }
21
-
22
- protected refresh(): void {
23
- if (!this.device.configDescriptor) {
24
- return;
25
- }
26
-
27
- this.descriptor = this.device.configDescriptor.interfaces[this.id][this.altSetting];
28
- this.interfaceNumber = this.descriptor.bInterfaceNumber;
29
- this.endpoints = [];
30
- const len = this.descriptor.endpoints.length;
31
- for (let i = 0; i < len; i++) {
32
- const desc = this.descriptor.endpoints[i];
33
- const c = (desc.bEndpointAddress & LIBUSB_ENDPOINT_IN) ? InEndpoint : OutEndpoint;
34
- this.endpoints[i] = new c(this.device, desc);
35
- }
36
- }
37
-
38
- /**
39
- * Claims the interface. This method must be called before using any endpoints of this interface.
40
- *
41
- * The device must be open to use this method.
42
- */
43
- public claim(): void {
44
- this.device.__claimInterface(this.id);
45
- }
46
-
47
- /**
48
- * Releases the interface and resets the alternate setting. Calls callback when complete.
49
- *
50
- * It is an error to release an interface with pending transfers.
51
- *
52
- * The device must be open to use this method.
53
- * @param callback
54
- */
55
- public release(callback?: (error?: LibUSBException) => void): void;
56
-
57
- /**
58
- * Releases the interface and resets the alternate setting. Calls callback when complete.
59
- *
60
- * It is an error to release an interface with pending transfers. If the optional closeEndpoints
61
- * parameter is true, any active endpoint streams are stopped (see `Endpoint.stopStream`),
62
- * and the interface is released after the stream transfers are cancelled. Transfers submitted
63
- * individually with `Endpoint.transfer` are not affected by this parameter.
64
- *
65
- * The device must be open to use this method.
66
- * @param closeEndpoints
67
- * @param callback
68
- */
69
- public release(closeEndpoints?: boolean, callback?: (error?: LibUSBException) => void): void;
70
- public release(closeEndpointsOrCallback?: boolean | ((error?: LibUSBException) => void), callback?: (error: LibUSBException | undefined) => void): void {
71
-
72
- let closeEndpoints = false;
73
- if (typeof closeEndpointsOrCallback === 'boolean') {
74
- closeEndpoints = closeEndpointsOrCallback;
75
- } else {
76
- callback = closeEndpointsOrCallback;
77
- }
78
-
79
- const next = () => {
80
- this.device.__releaseInterface(this.id, error => {
81
- if (!error) {
82
- this.altSetting = 0;
83
- this.refresh();
84
- }
85
- if (callback) {
86
- callback.call(this, error);
87
- }
88
- });
89
- };
90
-
91
- if (!closeEndpoints || this.endpoints.length === 0) {
92
- next();
93
- } else {
94
- let n = this.endpoints.length;
95
- this.endpoints.forEach(ep => {
96
- if (ep.direction === 'in' && (ep as InEndpoint).pollActive) {
97
- ep.once('end', () => {
98
- if (--n === 0) {
99
- next();
100
- }
101
- });
102
- (ep as InEndpoint).stopPoll();
103
- } else {
104
- if (--n === 0) {
105
- next();
106
- }
107
- }
108
- });
109
- }
110
- }
111
-
112
- /**
113
- * Returns `false` if a kernel driver is not active; `true` if active.
114
- *
115
- * The device must be open to use this method.
116
- */
117
- public isKernelDriverActive(): boolean {
118
- return this.device.__isKernelDriverActive(this.id);
119
- }
120
-
121
- /**
122
- * Detaches the kernel driver from the interface.
123
- *
124
- * The device must be open to use this method.
125
- */
126
- public detachKernelDriver(): void {
127
- return this.device.__detachKernelDriver(this.id);
128
- }
129
-
130
- /**
131
- * Re-attaches the kernel driver for the interface.
132
- *
133
- * The device must be open to use this method.
134
- */
135
- public attachKernelDriver(): void {
136
- return this.device.__attachKernelDriver(this.id);
137
- }
138
-
139
- /**
140
- * Sets the alternate setting. It updates the `interface.endpoints` array to reflect the endpoints found in the alternate setting.
141
- *
142
- * The device must be open to use this method.
143
- * @param altSetting
144
- * @param callback
145
- */
146
- public setAltSetting(altSetting: number, callback?: (error: LibUSBException | undefined) => void): void {
147
- this.device.__setInterface(this.id, altSetting, error => {
148
- if (!error) {
149
- this.altSetting = altSetting;
150
- this.refresh();
151
- }
152
- if (callback) {
153
- callback.call(this, error);
154
- }
155
- });
156
- }
157
-
158
- /**
159
- * Return the InEndpoint or OutEndpoint with the specified address.
160
- *
161
- * The device must be open to use this method.
162
- * @param addr
163
- */
164
- public endpoint(addr: number): Endpoint | undefined {
165
- return this.endpoints.find(item => item.address === addr);
166
- }
167
- }