@onekeyfe/hd-transport-usb 1.2.0-alpha.3 → 1.2.0-alpha.4
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -34
- package/package.json +4 -4
- package/src/index.ts +14 -40
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,SAWN,MAAM,wBAAwB,CAAC;AAGhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,SAWN,MAAM,wBAAwB,CAAC;AAGhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAgKhC,MAAM,CAAC,OAAO,OAAO,gBAAgB;IACnC,QAAQ,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAGlE,UAAU,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAEpE,IAAI,SAAsB;IAE1B,OAAO,SAAM;IAEb,UAAU,UAAS;IAEnB,UAAU,UAAS;IAEnB,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;IAGvB,OAAO,CAAC,aAAa,CAA6B;IAGlD,OAAO,CAAC,WAAW,CAAiC;IAGpD,OAAO,CAA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YAOtB,cAAc;YA4Cd,yBAAyB;YAezB,uBAAuB;YA0CvB,eAAe;YAcf,eAAe;YAcf,oBAAoB;YA+BpB,sBAAsB;YAwCtB,cAAc;YAgDd,WAAW;IA6CzB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
|
package/dist/index.js
CHANGED
|
@@ -71,7 +71,7 @@ const TRANSFER_TIMEOUT_MS = 30000;
|
|
|
71
71
|
const SERIAL_READ_TIMEOUT_MS = 5000;
|
|
72
72
|
const PACKET_IO_MAX_RETRIES = 3;
|
|
73
73
|
const PACKET_IO_RETRY_DELAY = 300;
|
|
74
|
-
const PROTOCOL_PROBE_TIMEOUT =
|
|
74
|
+
const PROTOCOL_PROBE_TIMEOUT = 5000;
|
|
75
75
|
function getBusId(dev) {
|
|
76
76
|
return `usb:${dev.busNumber}:${dev.deviceAddress}`;
|
|
77
77
|
}
|
|
@@ -152,26 +152,6 @@ function transferOutOnce(ep, data) {
|
|
|
152
152
|
});
|
|
153
153
|
});
|
|
154
154
|
}
|
|
155
|
-
function resetUsbDevice(dev) {
|
|
156
|
-
return new Promise(resolve => {
|
|
157
|
-
const reset = dev.reset;
|
|
158
|
-
if (typeof reset !== 'function') {
|
|
159
|
-
resolve();
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
reset.call(dev, () => resolve());
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
function clearEndpointHalt(ep) {
|
|
166
|
-
return new Promise(resolve => {
|
|
167
|
-
const clearHalt = ep.clearHalt;
|
|
168
|
-
if (typeof clearHalt !== 'function') {
|
|
169
|
-
resolve();
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
clearHalt.call(ep, () => resolve());
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
155
|
function skipReportByte(packet) {
|
|
176
156
|
if (packet[0] === REPORT_ID) {
|
|
177
157
|
return packet.subarray(1);
|
|
@@ -397,6 +377,10 @@ class NodeUsbTransport {
|
|
|
397
377
|
message.includes('timeout') ||
|
|
398
378
|
message.includes('interrupt'));
|
|
399
379
|
}
|
|
380
|
+
isUsbTransferTimeout(error) {
|
|
381
|
+
const message = this.getErrorMessage(error).toLowerCase();
|
|
382
|
+
return message.includes('timeout') || message.includes('timed_out');
|
|
383
|
+
}
|
|
400
384
|
getDeviceInterface(dev) {
|
|
401
385
|
var _a;
|
|
402
386
|
const { interfaces } = dev;
|
|
@@ -472,7 +456,7 @@ class NodeUsbTransport {
|
|
|
472
456
|
throw lastError;
|
|
473
457
|
});
|
|
474
458
|
}
|
|
475
|
-
transferInWithRetry(path, openDev, length) {
|
|
459
|
+
transferInWithRetry(path, openDev, length, options) {
|
|
476
460
|
var _a;
|
|
477
461
|
return __awaiter(this, void 0, void 0, function* () {
|
|
478
462
|
let lastError;
|
|
@@ -486,6 +470,10 @@ class NodeUsbTransport {
|
|
|
486
470
|
}
|
|
487
471
|
catch (error) {
|
|
488
472
|
lastError = error;
|
|
473
|
+
if ((options === null || options === void 0 ? void 0 : options.waitIndefinitelyOnTimeout) && this.isUsbTransferTimeout(error)) {
|
|
474
|
+
attempt -= 1;
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
489
477
|
const shouldRetry = attempt < PACKET_IO_MAX_RETRIES && this.isRetryableError(error);
|
|
490
478
|
if (!shouldRetry) {
|
|
491
479
|
throw error;
|
|
@@ -518,13 +506,6 @@ class NodeUsbTransport {
|
|
|
518
506
|
try {
|
|
519
507
|
dev.open();
|
|
520
508
|
dev.timeout = TRANSFER_TIMEOUT_MS;
|
|
521
|
-
yield resetUsbDevice(dev);
|
|
522
|
-
try {
|
|
523
|
-
dev.open();
|
|
524
|
-
}
|
|
525
|
-
catch (_d) {
|
|
526
|
-
}
|
|
527
|
-
dev.timeout = TRANSFER_TIMEOUT_MS;
|
|
528
509
|
const iface = this.getDeviceInterface(dev);
|
|
529
510
|
if (process.platform === 'linux') {
|
|
530
511
|
try {
|
|
@@ -532,7 +513,7 @@ class NodeUsbTransport {
|
|
|
532
513
|
iface.detachKernelDriver();
|
|
533
514
|
}
|
|
534
515
|
}
|
|
535
|
-
catch (
|
|
516
|
+
catch (_d) {
|
|
536
517
|
}
|
|
537
518
|
}
|
|
538
519
|
iface.claim();
|
|
@@ -543,8 +524,6 @@ class NodeUsbTransport {
|
|
|
543
524
|
}
|
|
544
525
|
epIn.timeout = TRANSFER_TIMEOUT_MS;
|
|
545
526
|
epOut.timeout = TRANSFER_TIMEOUT_MS;
|
|
546
|
-
yield clearEndpointHalt(epIn);
|
|
547
|
-
yield clearEndpointHalt(epOut);
|
|
548
527
|
yield this.drainStaleInput(epIn);
|
|
549
528
|
this.openDevices.set(path, { device: dev, iface, epIn, epOut });
|
|
550
529
|
}
|
|
@@ -552,7 +531,7 @@ class NodeUsbTransport {
|
|
|
552
531
|
try {
|
|
553
532
|
dev.close();
|
|
554
533
|
}
|
|
555
|
-
catch (
|
|
534
|
+
catch (_e) {
|
|
556
535
|
}
|
|
557
536
|
throw err;
|
|
558
537
|
}
|
|
@@ -745,7 +724,7 @@ class NodeUsbTransport {
|
|
|
745
724
|
let frame = assembler.push(new Uint8Array(0));
|
|
746
725
|
const deadline = timeoutMs ? Date.now() + timeoutMs : undefined;
|
|
747
726
|
while (!frame) {
|
|
748
|
-
const transferIn = this.transferInWithRetry(path, this.getOpenDevice(path), transport.PROTOCOL_V2_FRAME_MAX_BYTES);
|
|
727
|
+
const transferIn = this.transferInWithRetry(path, this.getOpenDevice(path), transport.PROTOCOL_V2_FRAME_MAX_BYTES, { waitIndefinitelyOnTimeout: !deadline });
|
|
749
728
|
const packet = deadline
|
|
750
729
|
? yield this.withProtocolReadTimeout(path, transferIn, Math.max(deadline - Date.now(), 1), 'V2')
|
|
751
730
|
: yield transferIn;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-usb",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.4",
|
|
4
4
|
"description": "OneKey hardware wallet direct USB transport plugin (libusb)",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"lint:fix": "eslint . --fix"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-shared": "1.2.0-alpha.
|
|
24
|
-
"@onekeyfe/hd-transport": "1.2.0-alpha.
|
|
23
|
+
"@onekeyfe/hd-shared": "1.2.0-alpha.4",
|
|
24
|
+
"@onekeyfe/hd-transport": "1.2.0-alpha.4",
|
|
25
25
|
"bytebuffer": "^5.0.1",
|
|
26
26
|
"usb": "^2.14.0"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "6f83d130f022a75003385bc4b2386cd0b2b539b5"
|
|
29
29
|
}
|
package/src/index.ts
CHANGED
|
@@ -44,7 +44,7 @@ const SERIAL_READ_TIMEOUT_MS = 5000;
|
|
|
44
44
|
/** Packet I/O retry configuration (matches WebUsbTransport) */
|
|
45
45
|
const PACKET_IO_MAX_RETRIES = 3;
|
|
46
46
|
const PACKET_IO_RETRY_DELAY = 300;
|
|
47
|
-
const PROTOCOL_PROBE_TIMEOUT =
|
|
47
|
+
const PROTOCOL_PROBE_TIMEOUT = 5000;
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
* Opened device state — holds the USB device, claimed interface, and endpoints.
|
|
@@ -154,32 +154,6 @@ function transferOutOnce(ep: usb.OutEndpoint, data: Buffer): Promise<void> {
|
|
|
154
154
|
});
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
function resetUsbDevice(dev: usb.Device): Promise<void> {
|
|
158
|
-
return new Promise(resolve => {
|
|
159
|
-
const reset = (dev as usb.Device & { reset?: (callback: (err?: Error) => void) => void }).reset;
|
|
160
|
-
if (typeof reset !== 'function') {
|
|
161
|
-
resolve();
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
reset.call(dev, () => resolve());
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function clearEndpointHalt(ep: usb.InEndpoint | usb.OutEndpoint): Promise<void> {
|
|
169
|
-
return new Promise(resolve => {
|
|
170
|
-
const clearHalt = (
|
|
171
|
-
ep as (usb.InEndpoint | usb.OutEndpoint) & {
|
|
172
|
-
clearHalt?: (callback: (err?: Error) => void) => void;
|
|
173
|
-
}
|
|
174
|
-
).clearHalt;
|
|
175
|
-
if (typeof clearHalt !== 'function') {
|
|
176
|
-
resolve();
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
clearHalt.call(ep, () => resolve());
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
|
|
183
157
|
/**
|
|
184
158
|
* Skip the 0x3F protocol marker byte from a USB packet.
|
|
185
159
|
*/
|
|
@@ -515,6 +489,11 @@ export default class NodeUsbTransport {
|
|
|
515
489
|
);
|
|
516
490
|
}
|
|
517
491
|
|
|
492
|
+
private isUsbTransferTimeout(error: unknown): boolean {
|
|
493
|
+
const message = this.getErrorMessage(error).toLowerCase();
|
|
494
|
+
return message.includes('timeout') || message.includes('timed_out');
|
|
495
|
+
}
|
|
496
|
+
|
|
518
497
|
private getDeviceInterface(dev: usb.Device): usb.Interface {
|
|
519
498
|
const { interfaces } = dev;
|
|
520
499
|
if (!interfaces?.length) {
|
|
@@ -629,7 +608,8 @@ export default class NodeUsbTransport {
|
|
|
629
608
|
private async transferInWithRetry(
|
|
630
609
|
path: string,
|
|
631
610
|
openDev: OpenDevice,
|
|
632
|
-
length: number
|
|
611
|
+
length: number,
|
|
612
|
+
options?: { waitIndefinitelyOnTimeout?: boolean }
|
|
633
613
|
): Promise<Buffer> {
|
|
634
614
|
let lastError: unknown;
|
|
635
615
|
let currentDev = openDev;
|
|
@@ -641,6 +621,10 @@ export default class NodeUsbTransport {
|
|
|
641
621
|
return await transferInOnce(currentDev.epIn, length);
|
|
642
622
|
} catch (error) {
|
|
643
623
|
lastError = error;
|
|
624
|
+
if (options?.waitIndefinitelyOnTimeout && this.isUsbTransferTimeout(error)) {
|
|
625
|
+
attempt -= 1;
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
644
628
|
const shouldRetry = attempt < PACKET_IO_MAX_RETRIES && this.isRetryableError(error);
|
|
645
629
|
if (!shouldRetry) {
|
|
646
630
|
throw error;
|
|
@@ -680,15 +664,6 @@ export default class NodeUsbTransport {
|
|
|
680
664
|
dev.open();
|
|
681
665
|
dev.timeout = TRANSFER_TIMEOUT_MS;
|
|
682
666
|
|
|
683
|
-
await resetUsbDevice(dev);
|
|
684
|
-
|
|
685
|
-
try {
|
|
686
|
-
dev.open();
|
|
687
|
-
} catch {
|
|
688
|
-
// libusb keeps some devices open across reset; continue with the current handle.
|
|
689
|
-
}
|
|
690
|
-
dev.timeout = TRANSFER_TIMEOUT_MS;
|
|
691
|
-
|
|
692
667
|
const iface = this.getDeviceInterface(dev);
|
|
693
668
|
|
|
694
669
|
// On Linux, detach kernel driver if active
|
|
@@ -723,8 +698,6 @@ export default class NodeUsbTransport {
|
|
|
723
698
|
epIn.timeout = TRANSFER_TIMEOUT_MS;
|
|
724
699
|
epOut.timeout = TRANSFER_TIMEOUT_MS;
|
|
725
700
|
|
|
726
|
-
await clearEndpointHalt(epIn);
|
|
727
|
-
await clearEndpointHalt(epOut);
|
|
728
701
|
await this.drainStaleInput(epIn);
|
|
729
702
|
|
|
730
703
|
this.openDevices.set(path, { device: dev, iface, epIn, epOut });
|
|
@@ -943,7 +916,8 @@ export default class NodeUsbTransport {
|
|
|
943
916
|
const transferIn = this.transferInWithRetry(
|
|
944
917
|
path,
|
|
945
918
|
this.getOpenDevice(path),
|
|
946
|
-
PROTOCOL_V2_FRAME_MAX_BYTES
|
|
919
|
+
PROTOCOL_V2_FRAME_MAX_BYTES,
|
|
920
|
+
{ waitIndefinitelyOnTimeout: !deadline }
|
|
947
921
|
);
|
|
948
922
|
const packet = deadline
|
|
949
923
|
? await this.withProtocolReadTimeout(
|