@onekeyfe/hd-transport-web-device 1.2.0-alpha.12 → 1.2.0-alpha.14
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/__tests__/webusb-protocol-v2-timeout.test.ts +40 -0
- package/dist/index.js +18 -1
- package/dist/webusb.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/webusb.ts +27 -6
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import transport, { ProtocolV2FrameAssembler } from '@onekeyfe/hd-transport';
|
|
2
|
+
|
|
3
|
+
import WebUsbTransport from '../src/webusb';
|
|
4
|
+
|
|
5
|
+
const schema = {
|
|
6
|
+
nested: {
|
|
7
|
+
Ping: { fields: { message: { type: 'string', id: 1 } } },
|
|
8
|
+
Success: { fields: { message: { type: 'string', id: 1 } } },
|
|
9
|
+
MessageType: {
|
|
10
|
+
values: {
|
|
11
|
+
MessageType_Ping: 60206,
|
|
12
|
+
MessageType_Success: 60207,
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
describe('WebUsbTransport Protocol V2 timeout recovery', () => {
|
|
19
|
+
test('invalidates and resets the cached connection before another call can start', async () => {
|
|
20
|
+
const webusb = new WebUsbTransport() as any;
|
|
21
|
+
const path = 'pro2-webusb';
|
|
22
|
+
webusb.messages = transport.parseConfigure(schema);
|
|
23
|
+
webusb.messagesV2 = transport.parseConfigure(schema);
|
|
24
|
+
webusb.protocolV2Assemblers.set(path, new ProtocolV2FrameAssembler());
|
|
25
|
+
webusb.transferOutOnce = jest.fn().mockResolvedValue(undefined);
|
|
26
|
+
webusb.receiveProtocolV2Frame = jest.fn(() => new Promise(() => undefined));
|
|
27
|
+
webusb.resetConnectionAfterProbe = jest.fn().mockImplementation(async () => {
|
|
28
|
+
webusb.protocolV2Sessions.delete(path);
|
|
29
|
+
webusb.protocolV2ReadTimeouts.delete(path);
|
|
30
|
+
webusb.protocolV2Assemblers.get(path)?.reset();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
await expect(
|
|
34
|
+
webusb.callProtocolV2(path, 'Ping', { message: 'timeout' }, { timeoutMs: 10 })
|
|
35
|
+
).rejects.toThrow('timeout');
|
|
36
|
+
|
|
37
|
+
expect(webusb.resetConnectionAfterProbe).toHaveBeenCalledWith(path);
|
|
38
|
+
expect(webusb.protocolV2Sessions.has(path)).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -138,7 +138,10 @@ class WebUsbTransport {
|
|
|
138
138
|
if (!this.usb)
|
|
139
139
|
return [];
|
|
140
140
|
const devices = yield this.usb.getDevices();
|
|
141
|
-
const onekeyDevices = devices.filter(dev =>
|
|
141
|
+
const onekeyDevices = devices.filter(dev => {
|
|
142
|
+
const isOneKey = hdShared.ONEKEY_WEBUSB_FILTER.some((desc) => dev.vendorId === desc.vendorId && dev.productId === desc.productId);
|
|
143
|
+
return isOneKey && !hdShared.isKnownTrezorWebUsbDevice(dev);
|
|
144
|
+
});
|
|
142
145
|
this.deviceList = onekeyDevices.map(device => {
|
|
143
146
|
const path = this.getDevicePath(device);
|
|
144
147
|
const protocolHint = inferProtocolHintFromDeviceName$1(device.productName);
|
|
@@ -640,6 +643,18 @@ class WebUsbTransport {
|
|
|
640
643
|
try {
|
|
641
644
|
return yield session.call(name, data, options);
|
|
642
645
|
}
|
|
646
|
+
catch (error) {
|
|
647
|
+
const message = error instanceof Error ? error.message.toLowerCase() : String(error).toLowerCase();
|
|
648
|
+
if (message.includes('protocol v2 read timeout') || message.includes('response timeout')) {
|
|
649
|
+
try {
|
|
650
|
+
yield this.resetConnectionAfterProbe(path);
|
|
651
|
+
}
|
|
652
|
+
catch (resetError) {
|
|
653
|
+
this.Log.debug('[WebUsbTransport] Protocol V2 timeout reset failed:', resetError);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
throw error;
|
|
657
|
+
}
|
|
643
658
|
finally {
|
|
644
659
|
this.protocolV2ReadTimeouts.delete(path);
|
|
645
660
|
}
|
|
@@ -722,6 +737,8 @@ class WebUsbTransport {
|
|
|
722
737
|
this.deviceProtocolHints.delete(path);
|
|
723
738
|
(_b = this.protocolV2Assemblers.get(path)) === null || _b === void 0 ? void 0 : _b.reset();
|
|
724
739
|
this.protocolV2Assemblers.delete(path);
|
|
740
|
+
this.protocolV2Sessions.delete(path);
|
|
741
|
+
this.protocolV2ReadTimeouts.delete(path);
|
|
725
742
|
this.deviceEndpoints.delete(path);
|
|
726
743
|
});
|
|
727
744
|
}
|
package/dist/webusb.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webusb.d.ts","sourceRoot":"","sources":["../src/webusb.ts"],"names":[],"mappings":";AACA,OAAO,SAUN,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"webusb.d.ts","sourceRoot":"","sources":["../src/webusb.ts"],"names":[],"mappings":";AACA,OAAO,SAUN,MAAM,wBAAwB,CAAC;AAYhC,OAAO,KAAK,EACV,YAAY,EACZ,oBAAoB,EACpB,YAAY,EACZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAqBhC,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,SAAS,CAAC;IAClB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AAaD,MAAM,CAAC,OAAO,OAAO,eAAe;IAClC,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;IAGpE,OAAO,CAAC,cAAc,CAAwC;IAE9D,OAAO,CAAC,mBAAmB,CAAwC;IAGnE,OAAO,CAAC,oBAAoB,CAAoD;IAGhF,OAAO,CAAC,kBAAkB,CAA6C;IAGvE,OAAO,CAAC,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EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;YA8BlB,cAAc;YAkCd,cAAc;YAuDd,sBAAsB;IA4C9B,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;IAgD5C,OAAO,CAAC,IAAI,EAAE,MAAM;IAmB1B,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-web-device",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.14",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,13 +20,13 @@
|
|
|
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.14",
|
|
24
|
+
"@onekeyfe/hd-transport": "1.2.0-alpha.14"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@onekeyfe/hd-transport-electron": "1.2.0-alpha.
|
|
27
|
+
"@onekeyfe/hd-transport-electron": "1.2.0-alpha.14",
|
|
28
28
|
"@types/w3c-web-usb": "^1.0.6",
|
|
29
29
|
"@types/web-bluetooth": "^0.0.17"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "c1edaed6435fb6049c7516c64b298ee62519e797"
|
|
32
32
|
}
|
package/src/webusb.ts
CHANGED
|
@@ -10,7 +10,13 @@ import transport, {
|
|
|
10
10
|
ProtocolV2Session,
|
|
11
11
|
probeProtocolV2 as probeProtocolV2Helper,
|
|
12
12
|
} from '@onekeyfe/hd-transport';
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
ERRORS,
|
|
15
|
+
HardwareErrorCode,
|
|
16
|
+
ONEKEY_WEBUSB_FILTER,
|
|
17
|
+
isKnownTrezorWebUsbDevice,
|
|
18
|
+
wait,
|
|
19
|
+
} from '@onekeyfe/hd-shared';
|
|
14
20
|
import ByteBuffer from 'bytebuffer';
|
|
15
21
|
|
|
16
22
|
import { createTransportCallLog, shouldSuppressHighVolumeCallLog } from './transportLog';
|
|
@@ -202,11 +208,13 @@ export default class WebUsbTransport {
|
|
|
202
208
|
if (!this.usb) return [];
|
|
203
209
|
|
|
204
210
|
const devices = await this.usb.getDevices();
|
|
205
|
-
const onekeyDevices = devices.filter(dev =>
|
|
206
|
-
ONEKEY_WEBUSB_FILTER.some(
|
|
207
|
-
desc
|
|
208
|
-
|
|
209
|
-
|
|
211
|
+
const onekeyDevices = devices.filter(dev => {
|
|
212
|
+
const isOneKey = ONEKEY_WEBUSB_FILTER.some(
|
|
213
|
+
(desc: { vendorId: number; productId: number }) =>
|
|
214
|
+
dev.vendorId === desc.vendorId && dev.productId === desc.productId
|
|
215
|
+
);
|
|
216
|
+
return isOneKey && !isKnownTrezorWebUsbDevice(dev);
|
|
217
|
+
});
|
|
210
218
|
|
|
211
219
|
this.deviceList = onekeyDevices.map(device => {
|
|
212
220
|
const path = this.getDevicePath(device);
|
|
@@ -813,6 +821,17 @@ export default class WebUsbTransport {
|
|
|
813
821
|
this.protocolV2Assemblers.get(path)?.reset();
|
|
814
822
|
try {
|
|
815
823
|
return await session.call(name, data, options);
|
|
824
|
+
} catch (error) {
|
|
825
|
+
const message =
|
|
826
|
+
error instanceof Error ? error.message.toLowerCase() : String(error).toLowerCase();
|
|
827
|
+
if (message.includes('protocol v2 read timeout') || message.includes('response timeout')) {
|
|
828
|
+
try {
|
|
829
|
+
await this.resetConnectionAfterProbe(path);
|
|
830
|
+
} catch (resetError) {
|
|
831
|
+
this.Log.debug('[WebUsbTransport] Protocol V2 timeout reset failed:', resetError);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
throw error;
|
|
816
835
|
} finally {
|
|
817
836
|
this.protocolV2ReadTimeouts.delete(path);
|
|
818
837
|
}
|
|
@@ -920,6 +939,8 @@ export default class WebUsbTransport {
|
|
|
920
939
|
this.deviceProtocolHints.delete(path);
|
|
921
940
|
this.protocolV2Assemblers.get(path)?.reset();
|
|
922
941
|
this.protocolV2Assemblers.delete(path);
|
|
942
|
+
this.protocolV2Sessions.delete(path);
|
|
943
|
+
this.protocolV2ReadTimeouts.delete(path);
|
|
923
944
|
this.deviceEndpoints.delete(path);
|
|
924
945
|
}
|
|
925
946
|
|