@onekeyfe/hd-transport-web-device 1.2.8-alpha.103 → 1.2.8-alpha.104

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.
@@ -120,53 +120,6 @@ describe('WebUSB identity probe ownership', () => {
120
120
  }
121
121
  );
122
122
 
123
- test('clears the Core session while retaining ownership after normal native close fails', async () => {
124
- const previous = TransportManager.transport;
125
- const { settings } = DataManager;
126
- const active = createAcquiredTransport();
127
- TransportManager.transport = active as unknown as Transport;
128
- const selected = new Device({
129
- path: 'USB_PATH',
130
- id: 'USB_PATH',
131
- name: 'Mock device',
132
- commType: 'webusb',
133
- session: 'USB_PATH',
134
- debug: false,
135
- });
136
- selected.mainId = 'USB_PATH';
137
- selected.deviceConnector = {
138
- release: (path: string) => active.release(path),
139
- } as never;
140
- (selected as unknown as { deviceAcquired: boolean }).deviceAcquired = true;
141
-
142
- try {
143
- await active.acquire({ path: 'USB_PATH' });
144
- device.close.mockRejectedValueOnce(new Error('native close failed'));
145
-
146
- await selected.release();
147
-
148
- expect(selected.originalDescriptor.session).toBeNull();
149
- expect(selected.hasDeviceAcquire()).toBe(false);
150
- expect(device.opened).toBe(true);
151
-
152
- const competing = new WebUsbTransport();
153
- competing.init({ debug: jest.fn() });
154
- await expect(competing.connectToDevice('USB_PATH', true)).rejects.toThrow(
155
- 'USB device is already in use'
156
- );
157
- } finally {
158
- device.close.mockImplementation(() => {
159
- device.opened = false;
160
- return Promise.resolve();
161
- });
162
- await active.release('USB_PATH');
163
- await active.stop();
164
- TransportManager.transport = previous;
165
- DataManager.settings = settings;
166
- jest.restoreAllMocks();
167
- }
168
- });
169
-
170
123
  test('does not replace the transport until USB cleanup completes, or after it fails', async () => {
171
124
  const previous = TransportManager.transport;
172
125
  const { settings } = DataManager;
@@ -1,5 +1,5 @@
1
1
  import transport from '@onekeyfe/hd-transport';
2
- import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
2
+ import { HardwareErrorCode } from '@onekeyfe/hd-shared';
3
3
 
4
4
  import WebUsbTransport from '../src/webusb';
5
5
 
@@ -64,97 +64,6 @@ describe('WebUsbTransport protocol probe cache', () => {
64
64
  }
65
65
  );
66
66
 
67
- test('uses the SDK 1.1.31 WebUSB sequence with descriptor endpoints for Protocol V1', async () => {
68
- const webusb = new WebUsbTransport() as any;
69
- const logger = { debug: jest.fn() };
70
- const path = 'legacy-v1-device';
71
- const device = {
72
- opened: false,
73
- configuration: { configurationValue: 1 },
74
- configurations: [
75
- {
76
- interfaces: [
77
- {
78
- interfaceNumber: 7,
79
- alternates: [
80
- {
81
- interfaceClass: 0xff,
82
- endpoints: [
83
- { direction: 'in', endpointNumber: 3 },
84
- { direction: 'out', endpointNumber: 4 },
85
- ],
86
- },
87
- ],
88
- },
89
- ],
90
- },
91
- ],
92
- open: jest.fn().mockImplementation(() => {
93
- device.opened = true;
94
- return Promise.resolve();
95
- }),
96
- reset: jest.fn().mockResolvedValue(undefined),
97
- selectConfiguration: jest.fn().mockResolvedValue(undefined),
98
- claimInterface: jest.fn().mockResolvedValue(undefined),
99
- clearHalt: jest.fn().mockResolvedValue(undefined),
100
- };
101
- webusb.Log = logger;
102
- webusb.findDevice = jest.fn().mockResolvedValue(device);
103
- webusb.getConnectedDevices = jest.fn().mockResolvedValue([]);
104
-
105
- await webusb.connect(path, true, { expectedProtocol: 'V1' });
106
-
107
- expect(device.open).toHaveBeenCalledTimes(1);
108
- expect(device.selectConfiguration).toHaveBeenCalledWith(1);
109
- expect(device.reset).toHaveBeenCalledTimes(1);
110
- expect(device.claimInterface).toHaveBeenCalledWith(7);
111
- expect(webusb.deviceEndpoints.get(path)).toEqual({
112
- interfaceNumber: 7,
113
- endpointIn: 3,
114
- endpointOut: 4,
115
- });
116
- expect(device.selectConfiguration.mock.invocationCallOrder[0]).toBeLessThan(
117
- device.reset.mock.invocationCallOrder[0]
118
- );
119
- expect(device.reset.mock.invocationCallOrder[0]).toBeLessThan(
120
- device.claimInterface.mock.invocationCallOrder[0]
121
- );
122
- expect(webusb.getConnectedDevices).not.toHaveBeenCalled();
123
- expect(device.clearHalt).not.toHaveBeenCalled();
124
- expect(logger.debug).toHaveBeenCalledWith(
125
- '[WebUsbTransport][lifecycle]',
126
- expect.objectContaining({
127
- event: 'connect-attempt',
128
- phase: 'start',
129
- connectionStrategy: 'protocol-v1-legacy-1.1.31',
130
- })
131
- );
132
- });
133
-
134
- test('keeps the current WebUSB sequence when a cached V1 protocol is force-reprobed', async () => {
135
- const webusb = new WebUsbTransport() as any;
136
- const path = 'force-reprobe-device';
137
- const device = {
138
- opened: true,
139
- configuration: { configurationValue: 1 },
140
- configurations: [],
141
- reset: jest.fn().mockResolvedValue(undefined),
142
- selectConfiguration: jest.fn().mockResolvedValue(undefined),
143
- claimInterface: jest.fn().mockResolvedValue(undefined),
144
- clearHalt: jest.fn().mockResolvedValue(undefined),
145
- };
146
- webusb.Log = { debug: jest.fn() };
147
- webusb.deviceProtocol.set(path, 'V1');
148
- webusb.findDevice = jest.fn().mockResolvedValue(device);
149
- webusb.getConnectedDevices = jest.fn().mockResolvedValue([]);
150
-
151
- await webusb.connectToDevice(path, true, { forceProtocolDetection: true });
152
-
153
- expect(device.reset).toHaveBeenCalledTimes(1);
154
- expect(webusb.getConnectedDevices).toHaveBeenCalledTimes(1);
155
- expect(device.clearHalt).toHaveBeenCalledTimes(2);
156
- });
157
-
158
67
  test('reports claimInterface failures as WebUSB device access errors', async () => {
159
68
  const webusb = new WebUsbTransport();
160
69
  const path = 'connected-usb-device';
@@ -182,129 +91,6 @@ describe('WebUsbTransport protocol probe cache', () => {
182
91
  });
183
92
  });
184
93
 
185
- test('does not retry a typed WebUSB handle error', async () => {
186
- const webusb = new WebUsbTransport() as any;
187
- const logger = { debug: jest.fn() };
188
- const path = 'sensitive-device-path';
189
- const accessError = ERRORS.TypedError(
190
- HardwareErrorCode.WebUsbDeviceAccessError,
191
- 'Unable to claim interface',
192
- { operation: 'claimInterface' }
193
- );
194
- webusb.Log = logger;
195
- webusb.connectToDevice = jest.fn().mockRejectedValue(accessError);
196
-
197
- await expect(webusb.connect(path, true)).rejects.toBe(accessError);
198
-
199
- expect(webusb.connectToDevice).toHaveBeenCalledTimes(1);
200
- expect(logger.debug).toHaveBeenCalledWith(
201
- '[WebUsbTransport][lifecycle]',
202
- expect.objectContaining({
203
- event: 'connect-attempt',
204
- phase: 'error',
205
- attempt: 1,
206
- maxAttempts: 5,
207
- willRetry: false,
208
- errorCode: HardwareErrorCode.WebUsbDeviceAccessError,
209
- })
210
- );
211
- expect(JSON.stringify(logger.debug.mock.calls)).not.toContain(path);
212
- });
213
-
214
- test('preserves the existing reconnect retry for a transient disconnect', async () => {
215
- const webusb = new WebUsbTransport() as any;
216
- const disconnected = ERRORS.TypedError(HardwareErrorCode.BridgeDeviceDisconnected);
217
- webusb.Log = { debug: jest.fn() };
218
- webusb.connectToDevice = jest
219
- .fn()
220
- .mockRejectedValueOnce(disconnected)
221
- .mockResolvedValueOnce(undefined);
222
-
223
- await expect(webusb.connect('device-path', true)).resolves.toBeUndefined();
224
-
225
- expect(webusb.connectToDevice).toHaveBeenCalledTimes(2);
226
- });
227
-
228
- test('closes every owned handle after claimInterface fails across re-enumeration', async () => {
229
- const webusb = new WebUsbTransport() as any;
230
- const logger = { debug: jest.fn() };
231
- const path = 'sensitive-device-path';
232
- const claimError = Object.assign(new Error('Unable to claim interface'), {
233
- name: 'NetworkError',
234
- });
235
- const usbInterface = {
236
- interfaceNumber: 0,
237
- claimed: false,
238
- alternates: [],
239
- };
240
- const originalDevice = {
241
- vendorId: 0x1209,
242
- productId: 0x53c1,
243
- serialNumber: path,
244
- opened: false,
245
- configuration: { configurationValue: 1, interfaces: [usbInterface] },
246
- configurations: [],
247
- open: jest.fn().mockImplementation(() => {
248
- originalDevice.opened = true;
249
- return Promise.resolve();
250
- }),
251
- reset: jest.fn().mockResolvedValue(undefined),
252
- selectConfiguration: jest.fn().mockResolvedValue(undefined),
253
- claimInterface: jest.fn().mockResolvedValue(undefined),
254
- releaseInterface: jest.fn().mockResolvedValue(undefined),
255
- close: jest.fn().mockImplementation(() => {
256
- originalDevice.opened = false;
257
- return Promise.resolve();
258
- }),
259
- clearHalt: jest.fn().mockResolvedValue(undefined),
260
- };
261
- const replacementDevice = {
262
- ...originalDevice,
263
- opened: false,
264
- open: jest.fn().mockImplementation(() => {
265
- replacementDevice.opened = true;
266
- return Promise.resolve();
267
- }),
268
- reset: jest.fn().mockResolvedValue(undefined),
269
- selectConfiguration: jest.fn().mockResolvedValue(undefined),
270
- claimInterface: jest.fn().mockRejectedValue(claimError),
271
- releaseInterface: jest.fn().mockResolvedValue(undefined),
272
- close: jest.fn().mockImplementation(() => {
273
- replacementDevice.opened = false;
274
- return Promise.resolve();
275
- }),
276
- clearHalt: jest.fn().mockResolvedValue(undefined),
277
- };
278
- webusb.Log = logger;
279
- webusb.usb = { getDevices: jest.fn().mockResolvedValue([replacementDevice]) };
280
- webusb.deviceList = [{ path, device: originalDevice, commType: 'webusb' }];
281
- webusb.rotateProtocolV2UsbGeneration = jest.fn().mockResolvedValue(undefined);
282
-
283
- await expect(webusb.acquire({ path })).rejects.toMatchObject({
284
- errorCode: HardwareErrorCode.WebUsbDeviceAccessError,
285
- });
286
-
287
- expect(originalDevice.claimInterface).not.toHaveBeenCalled();
288
- expect(replacementDevice.claimInterface).toHaveBeenCalledTimes(1);
289
- expect(originalDevice.releaseInterface).not.toHaveBeenCalled();
290
- expect(replacementDevice.releaseInterface).not.toHaveBeenCalled();
291
- expect(originalDevice.close).toHaveBeenCalledTimes(1);
292
- expect(replacementDevice.close).toHaveBeenCalledTimes(1);
293
- expect(originalDevice.opened).toBe(false);
294
- expect(replacementDevice.opened).toBe(false);
295
- expect(logger.debug).toHaveBeenCalledWith(
296
- '[WebUsbTransport][lifecycle]',
297
- expect.objectContaining({
298
- event: 'handle-operation',
299
- operation: 'releaseInterface',
300
- phase: 'skipped',
301
- reason: 'interface-not-claimed',
302
- deviceRefId: expect.any(Number),
303
- })
304
- );
305
- expect(JSON.stringify(logger.debug.mock.calls)).not.toContain(path);
306
- });
307
-
308
94
  test('reports transferIn failures as WebUSB device access errors', async () => {
309
95
  const webusb = new WebUsbTransport() as any;
310
96
  const path = 'connected-usb-device';
@@ -407,10 +193,6 @@ describe('WebUsbTransport protocol probe cache', () => {
407
193
 
408
194
  await expect(webusb.acquire({ path, expectedProtocol: 'V1' })).resolves.toBe(path);
409
195
 
410
- expect(webusb.connect).toHaveBeenCalledWith(path, true, {
411
- expectedProtocol: 'V1',
412
- forceProtocolDetection: undefined,
413
- });
414
196
  expect(webusb.detectProtocol).not.toHaveBeenCalled();
415
197
  expect(webusb.deviceProtocol.get(path)).toBe('V1');
416
198
  expect(webusb.acquiredPaths.has(path)).toBe(true);
package/dist/index.d.ts CHANGED
@@ -12,16 +12,8 @@ interface DeviceInfo extends OneKeyDeviceInfoBase {
12
12
  device: USBDevice;
13
13
  protocolType?: ProtocolType;
14
14
  }
15
- interface WebUsbConnectOptions {
16
- expectedProtocol?: ProtocolType;
17
- forceProtocolDetection?: boolean;
18
- }
19
15
  declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
20
16
  private readonly identityProbe;
21
- private readonly transportInstanceId;
22
- private handleOperationSequence;
23
- private deviceLogSequence;
24
- private deviceLogIds;
25
17
  messages: ReturnType<typeof _onekeyfe_hd_transport__default.parseConfigure> | undefined;
26
18
  /** Protobuf schema for Protocol V2 transports. */
27
19
  messagesV2: ReturnType<typeof _onekeyfe_hd_transport__default.parseConfigure> | undefined;
@@ -76,13 +68,6 @@ declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
76
68
  private stopResources;
77
69
  private assertRunning;
78
70
  private runHandleOperation;
79
- private getInterfaceClaimed;
80
- private getDeviceLogId;
81
- private getHandleLogState;
82
- private getDiagnosticError;
83
- private logLifecycle;
84
- private logSkippedHandleOperation;
85
- private runLoggedHandleOperation;
86
71
  /**
87
72
  * Announce that a USB device left. Called from the module-scoped disconnect
88
73
  * listener, which is why it is public rather than inlined.
@@ -143,8 +128,7 @@ declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
143
128
  /**
144
129
  * Connect to device with retry mechanism
145
130
  */
146
- connect(path: string, first: boolean, options?: WebUsbConnectOptions): Promise<undefined>;
147
- private shouldUseLegacyV1Connection;
131
+ connect(path: string, first: boolean): Promise<undefined>;
148
132
  /**
149
133
  * Discover vendor-class (0xFF) interface and its IN/OUT endpoint numbers from USB descriptors.
150
134
  * Falls back to legacy hardcoded values if no vendor interface is found.
@@ -154,8 +138,7 @@ declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
154
138
  * Connect to specific device.
155
139
  * Discovers interface/endpoint numbers from USB descriptors on first connection.
156
140
  */
157
- connectToDevice(path: string, first: boolean, options?: WebUsbConnectOptions): Promise<undefined>;
158
- private claimDeviceInterface;
141
+ connectToDevice(path: string, first: boolean): Promise<undefined>;
159
142
  private assertHandleAvailable;
160
143
  private claimNormalHandle;
161
144
  private releaseNormalHandles;
@@ -163,7 +146,6 @@ declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
163
146
  private claimIdentityProbeHandle;
164
147
  private releaseIdentityProbeHandle;
165
148
  private closeOpenDevice;
166
- private closeDeviceHandle;
167
149
  private clearEndpointHalt;
168
150
  /**
169
151
  * With the protocol cache surviving release(), deviceProtocol presence no