@onekeyfe/hd-transport-usb 1.2.0-alpha.12 → 1.2.0-alpha.121

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.
@@ -62,6 +62,7 @@ const createHarness = () => {
62
62
  const path = '6136';
63
63
  const responseQueue: Buffer[] = [];
64
64
  const sentSeqs: number[] = [];
65
+ let cancelledTransferCount = 0;
65
66
  let writeError: Error | undefined;
66
67
  let holdNextRead:
67
68
  | {
@@ -71,29 +72,84 @@ const createHarness = () => {
71
72
  }
72
73
  | undefined;
73
74
 
75
+ const performInTransfer = (callback: (error?: Error, data?: Buffer) => void) => {
76
+ if (epIn.timeout === 50) {
77
+ callback(new Error('LIBUSB_TRANSFER_TIMED_OUT'));
78
+ return;
79
+ }
80
+ if (holdNextRead) {
81
+ const pending = holdNextRead;
82
+ holdNextRead = undefined;
83
+ pending.callback = callback;
84
+ pending.markStarted();
85
+ return;
86
+ }
87
+ const response = responseQueue.shift();
88
+ if (!response) {
89
+ callback(new Error('LIBUSB_TRANSFER_TIMED_OUT'));
90
+ return;
91
+ }
92
+ callback(undefined, response);
93
+ };
94
+
74
95
  const epIn = {
75
96
  direction: 'in',
76
97
  address: 0x81,
77
98
  timeout: 30_000,
78
99
  transfer: jest.fn((_length: number, callback: (error?: Error, data?: Buffer) => void) => {
79
- if (epIn.timeout === 50) {
80
- callback(new Error('LIBUSB_TRANSFER_TIMED_OUT'));
81
- return;
82
- }
83
- if (holdNextRead) {
84
- const pending = holdNextRead;
85
- holdNextRead = undefined;
86
- pending.callback = callback;
87
- pending.markStarted();
88
- return;
89
- }
90
- const response = responseQueue.shift();
91
- if (!response) {
92
- callback(new Error('LIBUSB_TRANSFER_TIMED_OUT'));
93
- return;
94
- }
95
- callback(undefined, response);
100
+ performInTransfer(callback);
96
101
  }),
102
+ makeTransfer: jest.fn(
103
+ (
104
+ _timeout: number,
105
+ callback: (error: Error | undefined, data: Buffer, actualLength: number) => void
106
+ ) => {
107
+ let settled = false;
108
+ const finish = (error?: Error, data = Buffer.alloc(0)) => {
109
+ if (settled) return;
110
+ settled = true;
111
+ callback(error, data, data.length);
112
+ };
113
+ return {
114
+ submit: jest.fn((buffer: Buffer) => {
115
+ performInTransfer((error, data) => {
116
+ if (error) {
117
+ finish(error);
118
+ return;
119
+ }
120
+ data?.copy(buffer);
121
+ finish(undefined, buffer.subarray(0, data?.length ?? 0));
122
+ });
123
+ }),
124
+ cancel: jest.fn(() => {
125
+ cancelledTransferCount += 1;
126
+ finish(new Error('LIBUSB_TRANSFER_CANCELLED'));
127
+ }),
128
+ };
129
+ }
130
+ ),
131
+ };
132
+
133
+ const performOutTransfer = (data: Buffer, callback: (error?: Error) => void) => {
134
+ const seq = data[6];
135
+ sentSeqs.push(seq);
136
+ if (writeError) {
137
+ const error = writeError;
138
+ writeError = undefined;
139
+ callback(error);
140
+ return;
141
+ }
142
+ responseQueue.push(
143
+ Buffer.from(
144
+ ProtocolV2.encodeFrame(
145
+ schemas,
146
+ 'Success',
147
+ { message: 'ok' },
148
+ { router: PROTOCOL_V2_CHANNEL_USB, seq }
149
+ )
150
+ )
151
+ );
152
+ callback();
97
153
  };
98
154
 
99
155
  const epOut = {
@@ -101,33 +157,40 @@ const createHarness = () => {
101
157
  address: 0x01,
102
158
  timeout: 30_000,
103
159
  transfer: jest.fn((data: Buffer, callback: (error?: Error) => void) => {
104
- const seq = data[6];
105
- sentSeqs.push(seq);
106
- if (writeError) {
107
- const error = writeError;
108
- writeError = undefined;
109
- callback(error);
110
- return;
111
- }
112
- responseQueue.push(
113
- Buffer.from(
114
- ProtocolV2.encodeFrame(
115
- schemas,
116
- 'Success',
117
- { message: 'ok' },
118
- { router: PROTOCOL_V2_CHANNEL_USB, seq }
119
- )
120
- )
121
- );
122
- callback();
160
+ performOutTransfer(data, callback);
123
161
  }),
162
+ makeTransfer: jest.fn(
163
+ (
164
+ _timeout: number,
165
+ callback: (error: Error | undefined, data: Buffer, actualLength: number) => void
166
+ ) => {
167
+ let settled = false;
168
+ const finish = (error: Error | undefined, data: Buffer) => {
169
+ if (settled) return;
170
+ settled = true;
171
+ callback(error, data, data.length);
172
+ };
173
+ return {
174
+ submit: jest.fn((data: Buffer) => {
175
+ performOutTransfer(data, error => finish(error, data));
176
+ }),
177
+ cancel: jest.fn(() => {
178
+ cancelledTransferCount += 1;
179
+ finish(new Error('LIBUSB_TRANSFER_CANCELLED'), Buffer.alloc(0));
180
+ }),
181
+ };
182
+ }
183
+ ),
124
184
  };
125
185
 
126
186
  const iface = {
127
187
  descriptor: { bInterfaceClass: 0xff, bInterfaceNumber: 0 },
128
188
  endpoints: [epIn, epOut],
129
189
  claim: jest.fn(),
130
- release: jest.fn((callback: () => void) => callback()),
190
+ release: jest.fn((closeEndpointsOrCallback: boolean | (() => void), callback?: () => void) => {
191
+ if (typeof closeEndpointsOrCallback === 'function') closeEndpointsOrCallback();
192
+ else callback?.();
193
+ }),
131
194
  isKernelDriverActive: jest.fn(() => false),
132
195
  detachKernelDriver: jest.fn(),
133
196
  };
@@ -164,6 +227,7 @@ const createHarness = () => {
164
227
  epIn,
165
228
  epOut,
166
229
  sentSeqs,
230
+ getCancelledTransferCount: () => cancelledTransferCount,
167
231
  async acquire() {
168
232
  await transport.enumerate();
169
233
  await transport.acquire({ path, expectedProtocol: 'V2' });
@@ -189,7 +253,108 @@ const createHarness = () => {
189
253
  };
190
254
 
191
255
  describe('NodeUsbTransport Protocol V2 link lifecycle', () => {
192
- test('keeps seq across probe, call and reacquire', async () => {
256
+ test('falls back to Protocol V2 when a cached V1 hint is stale', async () => {
257
+ const transport = new NodeUsbTransport() as any;
258
+ const events: string[] = [];
259
+ transport.probeProtocolV1 = jest.fn().mockImplementation(() => {
260
+ events.push('probe-v1');
261
+ return Promise.resolve(false);
262
+ });
263
+ transport.resetConnectionAfterProbe = jest.fn().mockImplementation(() => {
264
+ events.push('reset');
265
+ return Promise.resolve();
266
+ });
267
+ transport.probeProtocolV2 = jest.fn().mockImplementation(() => {
268
+ events.push('probe-v2');
269
+ return Promise.resolve(true);
270
+ });
271
+
272
+ await expect(transport.detectProtocol('pro-usb', undefined, 'V1')).resolves.toBe('V2');
273
+
274
+ expect(events).toEqual(['probe-v1', 'reset', 'probe-v2']);
275
+ expect(transport.getProtocolType('pro-usb')).toBe('V2');
276
+ });
277
+
278
+ test('keeps active links when the Protocol V2 schema is configured repeatedly', () => {
279
+ const transport = new NodeUsbTransport() as any;
280
+ transport.invalidateAllProtocolV2UsbLinks = jest.fn().mockResolvedValue(undefined);
281
+ const schemaSource = JSON.stringify(protocolV2Schema);
282
+
283
+ transport.configureProtocolV2(schemaSource);
284
+ transport.configureProtocolV2(schemaSource);
285
+
286
+ expect(transport.invalidateAllProtocolV2UsbLinks).not.toHaveBeenCalled();
287
+ });
288
+
289
+ test('does not retry a native transfer cancelled by probe cleanup', () => {
290
+ const { transport } = createHarness();
291
+
292
+ expect((transport as any).isRetryableError(new Error('LIBUSB_TRANSFER_CANCELLED'))).toBe(false);
293
+ });
294
+
295
+ test('cancels pending native transfers before closing a timed-out protocol probe', async () => {
296
+ const harness = createHarness();
297
+ const { transport, path } = harness;
298
+ await harness.acquire();
299
+ const cancelActiveTransfers = jest.spyOn(transport as any, 'cancelActiveTransfers');
300
+ const closeOpenDevice = jest.spyOn(transport as any, 'closeOpenDevice');
301
+
302
+ await (transport as any).resetConnectionAfterProbe(path);
303
+
304
+ expect(cancelActiveTransfers).toHaveBeenCalledWith(path);
305
+ expect(closeOpenDevice).toHaveBeenCalledWith(path);
306
+ expect(cancelActiveTransfers.mock.invocationCallOrder[0]).toBeLessThan(
307
+ closeOpenDevice.mock.invocationCallOrder[0]
308
+ );
309
+ });
310
+
311
+ test('releases the USB interface when protocol detection rejects acquire', async () => {
312
+ const harness = createHarness();
313
+ const { transport, path, device, iface } = harness;
314
+ await transport.enumerate();
315
+ device.close.mockClear();
316
+ iface.release.mockClear();
317
+ jest
318
+ .spyOn(transport as any, 'detectProtocol')
319
+ .mockRejectedValueOnce(new Error('terminal protocol probe failure'));
320
+
321
+ await expect(transport.acquire({ path })).rejects.toMatchObject({
322
+ errorCode: expect.any(Number),
323
+ });
324
+
325
+ expect(iface.release).toHaveBeenCalledTimes(1);
326
+ expect(device.close).toHaveBeenCalledTimes(1);
327
+ expect((transport as any).openDevices.has(path)).toBe(false);
328
+ });
329
+
330
+ test('stop releases an acquired USB interface even before a Protocol V2 call', async () => {
331
+ const harness = createHarness();
332
+ const { transport, path, device, iface } = harness;
333
+ await transport.enumerate();
334
+ await transport.acquire({ path, expectedProtocol: 'V2' });
335
+ device.close.mockClear();
336
+ iface.release.mockClear();
337
+
338
+ await transport.stop();
339
+
340
+ expect(iface.release).toHaveBeenCalledTimes(1);
341
+ expect(device.close).toHaveBeenCalledTimes(1);
342
+ expect(transport.getProtocolType(path)).toBeUndefined();
343
+ });
344
+
345
+ test('actively probes explicit Protocol V2 during bootloader reconnect', async () => {
346
+ const harness = createHarness();
347
+ const { transport, path, epOut } = harness;
348
+
349
+ await transport.enumerate();
350
+ await transport.acquire({ path, expectedProtocol: 'V2' });
351
+
352
+ expect(epOut.makeTransfer).toHaveBeenCalledTimes(1);
353
+ expect(transport.getProtocolType(path)).toBe('V2');
354
+ await transport.release(path);
355
+ });
356
+
357
+ test('keeps seq across calls and actively probed reacquire', async () => {
193
358
  const harness = createHarness();
194
359
  const { transport, path, sentSeqs } = harness;
195
360
 
@@ -207,14 +372,14 @@ describe('NodeUsbTransport Protocol V2 link lifecycle', () => {
207
372
  const harness = createHarness();
208
373
  const { transport, path, epOut } = harness;
209
374
  await harness.acquire();
210
- epOut.transfer.mockClear();
375
+ epOut.makeTransfer.mockClear();
211
376
  harness.failNextWrite(new Error('LIBUSB_ERROR_IO'));
212
377
 
213
378
  await expect(transport.call(path, 'Ping', { message: 'write-failure' })).rejects.toThrow(
214
379
  'LIBUSB_ERROR_IO'
215
380
  );
216
381
 
217
- expect(epOut.transfer).toHaveBeenCalledTimes(1);
382
+ expect(epOut.makeTransfer).toHaveBeenCalledTimes(1);
218
383
  });
219
384
 
220
385
  test('rejects a pending read when release invalidates the link', async () => {
@@ -241,6 +406,20 @@ describe('NodeUsbTransport Protocol V2 link lifecycle', () => {
241
406
  expect(settled).not.toBe('still pending');
242
407
  });
243
408
 
409
+ test('stop cancels an in-flight native USB transfer before releasing the interface', async () => {
410
+ const harness = createHarness();
411
+ const { transport, path } = harness;
412
+ await harness.acquire();
413
+ const pendingRead = harness.holdRead();
414
+
415
+ const call = transport.call(path, 'Ping', { message: 'pending' }, { timeoutMs: 5000 });
416
+ await pendingRead.started;
417
+ await transport.stop();
418
+
419
+ await expect(call).rejects.toThrow();
420
+ expect(harness.getCancelledTransferCount()).toBe(1);
421
+ });
422
+
244
423
  test('keeps the cursor after a response timeout rebuilds the USB connection', async () => {
245
424
  const harness = createHarness();
246
425
  const { transport, path, sentSeqs } = harness;
package/dist/index.d.ts CHANGED
@@ -2,48 +2,119 @@ import * as transport from '@onekeyfe/hd-transport';
2
2
  import transport__default, { ProtocolV2UsbTransportBase, OneKeyDeviceInfo, AcquireInput, TransportCallOptions, ProtocolV2Schemas, ProtocolV2CallContext, ProtocolType } from '@onekeyfe/hd-transport';
3
3
  import EventEmitter from 'events';
4
4
 
5
+ /**
6
+ * Node.js USB Transport — complete transport implementation using libusb.
7
+ *
8
+ * Unlike the old UsbPlugin (which was a LowlevelTransportSharedPlugin piped
9
+ * through LowlevelTransport), this class is a standalone transport that handles
10
+ * both protocol encoding/decoding and USB I/O directly.
11
+ *
12
+ * Modeled after WebUsbTransport.
13
+ */
5
14
  declare class NodeUsbTransport extends ProtocolV2UsbTransportBase<string> {
6
15
  messages: ReturnType<typeof transport__default.parseConfigure> | undefined;
16
+ /** Protobuf schema for Protocol V2 transports. */
7
17
  messagesV2: ReturnType<typeof transport__default.parseConfigure> | undefined;
18
+ private protocolV2SchemaSource;
8
19
  name: string;
9
20
  version: string;
10
21
  configured: boolean;
11
22
  isOutdated: boolean;
12
23
  Log?: any;
13
24
  emitter?: EventEmitter;
25
+ /** serial → bus id, built during enumerate */
14
26
  private serialToBusId;
27
+ /** path → opened device state */
15
28
  private openDevices;
29
+ /** Per-path protocol type detected by active wire-level probe. */
16
30
  private deviceProtocol;
31
+ /** per-path reconnect lock to prevent concurrent reconnects */
17
32
  private reconnectLocks;
33
+ /**
34
+ * Retain the low-level Transfer so release()/stop() can cancel native pending reads.
35
+ * Endpoint.transfer() hides it and may keep the CLI alive after output completes.
36
+ */
37
+ private activeTransfers;
38
+ /** set to true when cancel() is called; checked by retry loops */
18
39
  private cancelled;
19
40
  constructor();
41
+ /**
42
+ * Initialize transport.
43
+ * Signature matches the Transport.init interface (logger, emitter).
44
+ */
20
45
  init(logger: any, emitter?: EventEmitter): Promise<string>;
21
46
  configure(signedData: any): Promise<void>;
22
47
  configureProtocolV2(signedData: any): void;
23
48
  listen(): void;
24
- stop(): void;
49
+ stop(): Promise<void>;
50
+ /**
51
+ * Low-level post (send only, no response). Not used by NodeUsbTransport
52
+ * since call() handles the full send+receive cycle, but required by the Transport interface.
53
+ */
25
54
  post(path: string, name: string, data: Record<string, unknown>): Promise<void>;
55
+ /**
56
+ * Low-level read (receive only). Not used by NodeUsbTransport
57
+ * since call() handles the full send+receive cycle, but required by the Transport interface.
58
+ */
26
59
  read(path: string): Promise<{
27
60
  message: {
28
61
  [key: string]: any;
29
62
  };
30
63
  type: string;
31
64
  }>;
65
+ /**
66
+ * Enumerate connected OneKey USB devices.
67
+ * Opens each device briefly to read its serial number (used as `path`),
68
+ * then closes it. acquire() re-opens from a fresh getDeviceList().
69
+ */
32
70
  enumerate(): Promise<OneKeyDeviceInfo[]>;
71
+ /**
72
+ * Acquire device — open USB device, claim interface, return path (string).
73
+ */
33
74
  acquire(input: AcquireInput): Promise<string>;
75
+ /**
76
+ * Release device — release interface and close.
77
+ */
34
78
  release(path: string, _onclose?: boolean): Promise<void>;
79
+ private cancelActiveTransfers;
80
+ private createTrackedTransfer;
81
+ private transferInOnce;
82
+ private transferOutOnce;
35
83
  private closeOpenDevice;
84
+ /**
85
+ * Call device method — encode protobuf, send packets, receive response.
86
+ * This is the core method that replaces LowlevelTransport's call + UsbPlugin's send/receive.
87
+ */
36
88
  call(path: string, name: string, data: Record<string, unknown>, options?: TransportCallOptions): Promise<transport.MessageFromOneKey>;
37
89
  private callProtocolV1;
38
90
  cancel(): void;
91
+ /**
92
+ * Get the current open device for a path, re-resolving from the map
93
+ * so callers always use a fresh reference after reconnect.
94
+ */
39
95
  private getOpenDevice;
40
96
  private getErrorMessage;
41
97
  private isRetryableError;
42
98
  private isUsbTransferTimeout;
43
99
  private getDeviceInterface;
100
+ /**
101
+ * Reconnect device before retrying a failed transfer (aligned with WebUsbTransport).
102
+ * Uses per-path lock to prevent concurrent reconnects to the same device.
103
+ */
44
104
  private reconnectForRetry;
105
+ /**
106
+ * Send all encoded chunks to the device with retry.
107
+ * If a chunk fails and triggers reconnect, the entire sequence restarts
108
+ * from chunk 0 because the device resets protocol state on reconnect.
109
+ */
45
110
  private sendAllChunksWithRetry;
111
+ /**
112
+ * USB IN transfer with retry and reconnect (aligned with WebUsbTransport).
113
+ */
46
114
  private transferInWithRetry;
115
+ /**
116
+ * Open a USB device by path (serial number), claim interface, cache endpoints.
117
+ */
47
118
  private openDevice;
48
119
  private drainStaleInput;
49
120
  private createProtocolMismatchError;
@@ -61,6 +132,10 @@ declare class NodeUsbTransport extends ProtocolV2UsbTransportBase<string> {
61
132
  protected onProtocolV2UsbLinkInvalidated(path: string, reason: string): void;
62
133
  protected createProtocolV2UsbTimeoutError(name: string, timeoutMs: number): Error;
63
134
  private callProtocolV2;
135
+ /**
136
+ * Receive a complete protobuf response from the device.
137
+ * Reads 64-byte packets, strips 0x3F marker, reassembles into hex string.
138
+ */
64
139
  private receiveData;
65
140
  getProtocolType(path: string): ProtocolType | undefined;
66
141
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,SA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M,EACZ,QAAQ,EAAE,qBAAqB,GAC9B,OAAO,CAAC,UAAU,CAAC;cAsBN,4BAA4B,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK1F,SAAS,CAAC,8BAA8B,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAKrE,SAAS,CAAC,+BAA+B,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,KAAK;YAOnE,cAAc;YAad,WAAW;IA6CzB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
package/dist/index.js CHANGED
@@ -59,14 +59,6 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
59
59
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
60
60
  };
61
61
 
62
- const HIGH_VOLUME_CALLS = new Set(['FileWrite', 'FilesystemFileWrite', 'EmmcFileWrite']);
63
- function shouldSuppressHighVolumeCallLog(name) {
64
- return HIGH_VOLUME_CALLS.has(name);
65
- }
66
- function createTransportCallLog(name, protocol) {
67
- return { name, protocol };
68
- }
69
-
70
62
  const { parseConfigure, ProtocolV1, check } = transport__default["default"];
71
63
  const PACKET_SIZE = transport.PROTOCOL_V1_USB_PACKET_SIZE;
72
64
  const REPORT_ID = transport.PROTOCOL_V1_REPORT_ID;
@@ -140,26 +132,6 @@ function readSerialNumber(dev, openDevices) {
140
132
  }
141
133
  });
142
134
  }
143
- function transferInOnce(ep, length) {
144
- return new Promise((resolve, reject) => {
145
- ep.transfer(length, (err, data) => {
146
- if (err)
147
- return reject(err);
148
- if (!data || data.length === 0)
149
- return reject(new Error('Empty USB transfer'));
150
- resolve(data);
151
- });
152
- });
153
- }
154
- function transferOutOnce(ep, data) {
155
- return new Promise((resolve, reject) => {
156
- ep.transfer(data, (err) => {
157
- if (err)
158
- return reject(err);
159
- resolve();
160
- });
161
- });
162
- }
163
135
  function skipReportByte(packet) {
164
136
  if (packet[0] === REPORT_ID) {
165
137
  return packet.subarray(1);
@@ -184,6 +156,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
184
156
  this.openDevices = new Map();
185
157
  this.deviceProtocol = new Map();
186
158
  this.reconnectLocks = new Map();
159
+ this.activeTransfers = new Map();
187
160
  this.cancelled = false;
188
161
  }
189
162
  init(logger, emitter) {
@@ -198,13 +171,39 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
198
171
  return Promise.resolve();
199
172
  }
200
173
  configureProtocolV2(signedData) {
174
+ var _a;
175
+ const schemaSource = typeof signedData === 'string'
176
+ ? signedData
177
+ : (_a = JSON.stringify(signedData)) !== null && _a !== void 0 ? _a : String(signedData);
178
+ if (schemaSource === this.protocolV2SchemaSource)
179
+ return;
180
+ const hadProtocolV2Schema = this.protocolV2SchemaSource !== undefined;
201
181
  this.messagesV2 = parseConfigure(signedData);
182
+ this.protocolV2SchemaSource = schemaSource;
183
+ if (!hadProtocolV2Schema)
184
+ return;
202
185
  this.invalidateAllProtocolV2UsbLinks('Protocol V2 schema reconfigured').catch(error => { var _a; return (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[NodeUsbTransport] schema link cleanup failed:', error); });
203
186
  }
204
187
  listen() {
205
188
  }
206
189
  stop() {
207
- this.disposeProtocolV2UsbLinks('Node USB transport stopped').catch(error => { var _a; return (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[NodeUsbTransport] stop link cleanup failed:', error); });
190
+ var _a;
191
+ return __awaiter(this, void 0, void 0, function* () {
192
+ this.cancelled = true;
193
+ yield this.cancelActiveTransfers();
194
+ try {
195
+ yield this.disposeProtocolV2UsbLinks('Node USB transport stopped');
196
+ }
197
+ catch (error) {
198
+ (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[NodeUsbTransport] stop link cleanup failed:', error);
199
+ }
200
+ yield Promise.allSettled(this.reconnectLocks.values());
201
+ yield this.cancelActiveTransfers();
202
+ yield Promise.all(Array.from(this.openDevices.keys(), path => this.closeOpenDevice(path)));
203
+ this.reconnectLocks.clear();
204
+ this.deviceProtocol.clear();
205
+ this.serialToBusId.clear();
206
+ });
208
207
  }
209
208
  post(path, name, data) {
210
209
  return __awaiter(this, void 0, void 0, function* () {
@@ -254,7 +253,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
254
253
  });
255
254
  }
256
255
  acquire(input) {
257
- var _a, _b, _c;
256
+ var _a, _b, _c, _d;
258
257
  return __awaiter(this, void 0, void 0, function* () {
259
258
  this.cancelled = false;
260
259
  const path = (_a = input.path) !== null && _a !== void 0 ? _a : '';
@@ -265,22 +264,100 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
265
264
  yield this.rotateProtocolV2UsbGeneration(path, 'Node USB transport acquired');
266
265
  yield this.closeOpenDevice(path);
267
266
  yield this.openDevice(path);
268
- yield this.detectProtocol(path, input.expectedProtocol);
267
+ yield this.detectProtocol(path, input.expectedProtocol, input.protocolHint);
269
268
  return path;
270
269
  }
271
270
  catch (error) {
272
271
  (_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug('NodeUsbTransport acquire error: ', error);
273
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotFound, (_c = error.message) !== null && _c !== void 0 ? _c : String(error));
272
+ try {
273
+ yield this.release(path);
274
+ }
275
+ catch (cleanupError) {
276
+ (_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug('NodeUsbTransport acquire cleanup error: ', cleanupError);
277
+ }
278
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotFound, (_d = error.message) !== null && _d !== void 0 ? _d : String(error));
274
279
  }
275
280
  });
276
281
  }
277
282
  release(path, _onclose) {
278
283
  return __awaiter(this, void 0, void 0, function* () {
284
+ yield this.cancelActiveTransfers(path);
279
285
  yield this.invalidateProtocolV2UsbLink(path, 'Node USB transport released');
280
286
  yield this.closeOpenDevice(path);
281
287
  this.deviceProtocol.delete(path);
282
288
  });
283
289
  }
290
+ cancelActiveTransfers(path) {
291
+ return __awaiter(this, void 0, void 0, function* () {
292
+ const transfers = Array.from(this.activeTransfers.entries()).filter(([, active]) => path === undefined || active.path === path);
293
+ transfers.forEach(([transfer]) => {
294
+ try {
295
+ transfer.cancel();
296
+ }
297
+ catch (_a) {
298
+ }
299
+ });
300
+ yield Promise.allSettled(transfers.map(([, active]) => active.settled));
301
+ });
302
+ }
303
+ createTrackedTransfer(path, endpoint, callback) {
304
+ let resolveSettled = () => undefined;
305
+ const settled = new Promise(resolve => {
306
+ resolveSettled = resolve;
307
+ });
308
+ const transfer = endpoint.makeTransfer(endpoint.timeout, (error, buffer, actualLength) => {
309
+ this.activeTransfers.delete(transfer);
310
+ resolveSettled();
311
+ callback(error, buffer, actualLength);
312
+ });
313
+ this.activeTransfers.set(transfer, { path, settled, resolveSettled });
314
+ return transfer;
315
+ }
316
+ transferInOnce(path, ep, length) {
317
+ return new Promise((resolve, reject) => {
318
+ const buffer = Buffer.alloc(length);
319
+ const transfer = this.createTrackedTransfer(path, ep, (error, _submittedBuffer, actualLength) => {
320
+ if (error) {
321
+ reject(error);
322
+ return;
323
+ }
324
+ if (actualLength <= 0) {
325
+ reject(new Error('Empty USB transfer'));
326
+ return;
327
+ }
328
+ resolve(buffer.subarray(0, actualLength));
329
+ });
330
+ try {
331
+ transfer.submit(buffer);
332
+ }
333
+ catch (error) {
334
+ const active = this.activeTransfers.get(transfer);
335
+ this.activeTransfers.delete(transfer);
336
+ active === null || active === void 0 ? void 0 : active.resolveSettled();
337
+ reject(error);
338
+ }
339
+ });
340
+ }
341
+ transferOutOnce(path, ep, data) {
342
+ return new Promise((resolve, reject) => {
343
+ const transfer = this.createTrackedTransfer(path, ep, error => {
344
+ if (error) {
345
+ reject(error);
346
+ return;
347
+ }
348
+ resolve();
349
+ });
350
+ try {
351
+ transfer.submit(data);
352
+ }
353
+ catch (error) {
354
+ const active = this.activeTransfers.get(transfer);
355
+ this.activeTransfers.delete(transfer);
356
+ active === null || active === void 0 ? void 0 : active.resolveSettled();
357
+ reject(error);
358
+ }
359
+ });
360
+ }
284
361
  closeOpenDevice(path) {
285
362
  return __awaiter(this, void 0, void 0, function* () {
286
363
  const openDev = this.openDevices.get(path);
@@ -309,7 +386,6 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
309
386
  });
310
387
  }
311
388
  call(path, name, data, options) {
312
- var _a;
313
389
  return __awaiter(this, void 0, void 0, function* () {
314
390
  this.cancelled = false;
315
391
  if (!this.messages) {
@@ -322,9 +398,6 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
322
398
  if (!protocol) {
323
399
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `Device protocol has not been detected for ${path}`);
324
400
  }
325
- if (!shouldSuppressHighVolumeCallLog(name)) {
326
- (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('transport call', createTransportCallLog(name, protocol));
327
- }
328
401
  if (protocol === 'V2') {
329
402
  return this.callProtocolV2(path, name, data, options);
330
403
  }
@@ -370,6 +443,9 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
370
443
  }
371
444
  isRetryableError(error) {
372
445
  const message = this.getErrorMessage(error).toLowerCase();
446
+ if (message.includes('cancelled') || message.includes('canceled')) {
447
+ return false;
448
+ }
373
449
  return (message.includes('libusb') ||
374
450
  message.includes('transfer') ||
375
451
  message.includes('disconnected') ||
@@ -438,7 +514,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
438
514
  const packet = new Uint8Array(PACKET_SIZE);
439
515
  packet[0] = REPORT_ID;
440
516
  packet.set(new Uint8Array(buffer), 1);
441
- yield transferOutOnce(this.getOpenDevice(path).epOut, Buffer.from(packet));
517
+ yield this.transferOutOnce(path, this.getOpenDevice(path).epOut, Buffer.from(packet));
442
518
  }
443
519
  return;
444
520
  }
@@ -471,7 +547,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
471
547
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInterruptedFromOutside, 'Cancelled');
472
548
  }
473
549
  try {
474
- return yield transferInOnce(currentDev.epIn, length);
550
+ return yield this.transferInOnce(path, currentDev.epIn, length);
475
551
  }
476
552
  catch (error) {
477
553
  lastError = error;
@@ -530,7 +606,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
530
606
  }
531
607
  epIn.timeout = TRANSFER_TIMEOUT_MS;
532
608
  epOut.timeout = TRANSFER_TIMEOUT_MS;
533
- yield this.drainStaleInput(epIn);
609
+ yield this.drainStaleInput(path, epIn);
534
610
  this.openDevices.set(path, { device: dev, iface, epIn, epOut });
535
611
  }
536
612
  catch (err) {
@@ -543,14 +619,14 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
543
619
  }
544
620
  });
545
621
  }
546
- drainStaleInput(epIn) {
622
+ drainStaleInput(path, epIn) {
547
623
  return __awaiter(this, void 0, void 0, function* () {
548
624
  const originalTimeout = epIn.timeout;
549
625
  epIn.timeout = 50;
550
626
  try {
551
627
  for (let index = 0; index < 16; index += 1) {
552
628
  try {
553
- yield transferInOnce(epIn, PACKET_SIZE);
629
+ yield this.transferInOnce(path, epIn, PACKET_SIZE);
554
630
  }
555
631
  catch (_a) {
556
632
  break;
@@ -568,33 +644,34 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
568
644
  createProtocolDetectionError() {
569
645
  return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Unable to detect USB protocol: device did not respond to Protocol V1 Initialize or Protocol V2 Ping');
570
646
  }
571
- detectProtocol(path, expectedProtocol) {
647
+ detectProtocol(path, expectedProtocol, protocolHint) {
648
+ var _a;
572
649
  return __awaiter(this, void 0, void 0, function* () {
573
- if (expectedProtocol === 'V1') {
574
- if (yield this.probeProtocolV1(path)) {
575
- this.deviceProtocol.set(path, 'V1');
576
- return 'V1';
577
- }
578
- throw this.createProtocolMismatchError(expectedProtocol);
579
- }
580
650
  if (expectedProtocol === 'V2') {
581
651
  if (yield this.probeProtocolV2(path)) {
582
652
  this.deviceProtocol.set(path, 'V2');
653
+ (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug(`[NodeUsbTransport] detectProtocol: path=${path} -> V2 (expected)`);
583
654
  return 'V2';
584
655
  }
585
656
  throw this.createProtocolMismatchError(expectedProtocol);
586
657
  }
587
- if (this.deviceProtocol.get(path) === 'V2' && (yield this.probeProtocolV2(path))) {
588
- this.deviceProtocol.set(path, 'V2');
589
- return 'V2';
590
- }
591
- if (yield this.probeProtocolV1(path)) {
592
- this.deviceProtocol.set(path, 'V1');
593
- return 'V1';
658
+ if (expectedProtocol === 'V1') {
659
+ if (yield this.probeProtocolV1(path)) {
660
+ this.deviceProtocol.set(path, 'V1');
661
+ return 'V1';
662
+ }
663
+ throw this.createProtocolMismatchError(expectedProtocol);
594
664
  }
595
- if (yield this.probeProtocolV2(path)) {
596
- this.deviceProtocol.set(path, 'V2');
597
- return 'V2';
665
+ const probeOrder = protocolHint === 'V2' || this.deviceProtocol.get(path) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
666
+ for (const [index, protocol] of probeOrder.entries()) {
667
+ if (index > 0) {
668
+ yield this.resetConnectionAfterProbe(path);
669
+ }
670
+ const detected = protocol === 'V1' ? yield this.probeProtocolV1(path) : yield this.probeProtocolV2(path);
671
+ if (detected) {
672
+ this.deviceProtocol.set(path, protocol);
673
+ return protocol;
674
+ }
598
675
  }
599
676
  this.deviceProtocol.delete(path);
600
677
  throw this.createProtocolDetectionError();
@@ -605,6 +682,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
605
682
  return __awaiter(this, void 0, void 0, function* () {
606
683
  yield this.rotateProtocolV2UsbGeneration(path, 'Node USB protocol probe reset');
607
684
  try {
685
+ yield this.cancelActiveTransfers(path);
608
686
  yield this.closeOpenDevice(path);
609
687
  }
610
688
  catch (error) {
@@ -696,7 +774,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
696
774
  if (this.cancelled) {
697
775
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInterruptedFromOutside, 'Cancelled');
698
776
  }
699
- yield transferOutOnce(this.getOpenDevice(path).epOut, Buffer.from(frame));
777
+ yield this.transferOutOnce(path, this.getOpenDevice(path).epOut, Buffer.from(frame));
700
778
  });
701
779
  }
702
780
  readProtocolV2UsbPacket(path, _context) {
@@ -706,7 +784,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
706
784
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInterruptedFromOutside, 'Cancelled');
707
785
  }
708
786
  try {
709
- const packet = yield transferInOnce(this.getOpenDevice(path).epIn, transport.PROTOCOL_V2_FRAME_MAX_BYTES);
787
+ const packet = yield this.transferInOnce(path, this.getOpenDevice(path).epIn, transport.PROTOCOL_V2_FRAME_MAX_BYTES);
710
788
  return new Uint8Array(packet.buffer.slice(packet.byteOffset, packet.byteOffset + packet.byteLength));
711
789
  }
712
790
  catch (error) {
@@ -719,6 +797,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
719
797
  }
720
798
  resetProtocolV2UsbNativeLink(path, _reason) {
721
799
  return __awaiter(this, void 0, void 0, function* () {
800
+ yield this.cancelActiveTransfers(path);
722
801
  yield this.closeOpenDevice(path);
723
802
  });
724
803
  }
@@ -728,7 +807,7 @@ class NodeUsbTransport extends transport.ProtocolV2UsbTransportBase {
728
807
  (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug(`[NodeUsbTransport] Protocol V2 link invalidated: ${path}`, reason);
729
808
  }
730
809
  createProtocolV2UsbTimeoutError(name, timeoutMs) {
731
- return new Error(`Protocol V2 response timeout after ${timeoutMs}ms for ${name}`);
810
+ return new transport.ProtocolV2LinkError('response-timeout', `Protocol V2 response timeout after ${timeoutMs}ms for ${name}`);
732
811
  }
733
812
  callProtocolV2(path, name, data, options) {
734
813
  return __awaiter(this, void 0, void 0, function* () {
@@ -1,7 +1,2 @@
1
- import type { ProtocolType } from '@onekeyfe/hd-transport';
2
- export declare function shouldSuppressHighVolumeCallLog(name: string): boolean;
3
- export declare function createTransportCallLog(name: string, protocol: ProtocolType): {
4
- name: string;
5
- protocol: ProtocolType;
6
- };
1
+ export { createTransportCallLog, shouldSuppressHighVolumeCallLog } from '@onekeyfe/hd-transport';
7
2
  //# sourceMappingURL=transportLog.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transportLog.d.ts","sourceRoot":"","sources":["../src/transportLog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAI3D,wBAAgB,+BAA+B,CAAC,IAAI,EAAE,MAAM,WAE3D;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY;;;EAE1E"}
1
+ {"version":3,"file":"transportLog.d.ts","sourceRoot":"","sources":["../src/transportLog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,+BAA+B,EAAE,MAAM,wBAAwB,CAAC"}
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ preset: '../../jest.config.js',
3
+ testEnvironment: 'node',
4
+ modulePathIgnorePatterns: ['node_modules', '<rootDir>/dist'],
5
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-usb",
3
- "version": "1.2.0-alpha.12",
3
+ "version": "1.2.0-alpha.121",
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",
@@ -16,14 +16,15 @@
16
16
  "scripts": {
17
17
  "dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w",
18
18
  "build": "rimraf dist && rollup -c ../../build/rollup.config.js",
19
+ "test": "jest",
19
20
  "lint": "eslint .",
20
21
  "lint:fix": "eslint . --fix"
21
22
  },
22
23
  "dependencies": {
23
- "@onekeyfe/hd-shared": "1.2.0-alpha.12",
24
- "@onekeyfe/hd-transport": "1.2.0-alpha.12",
24
+ "@onekeyfe/hd-shared": "1.2.0-alpha.121",
25
+ "@onekeyfe/hd-transport": "1.2.0-alpha.121",
25
26
  "bytebuffer": "^5.0.1",
26
27
  "usb": "^2.14.0"
27
28
  },
28
- "gitHead": "3601e8003c96a2a4ca7856e865d16f48c0c5b758"
29
+ "gitHead": "916633f46e4d8e0599af2956a639474dd3cbd9ec"
29
30
  }
package/src/index.ts CHANGED
@@ -7,13 +7,12 @@ import transport, {
7
7
  PROTOCOL_V1_USB_PACKET_SIZE,
8
8
  PROTOCOL_V2_CHANNEL_USB,
9
9
  PROTOCOL_V2_FRAME_MAX_BYTES,
10
+ ProtocolV2LinkError,
10
11
  ProtocolV2UsbTransportBase,
11
12
  probeProtocolV2 as probeProtocolV2Helper,
12
13
  } from '@onekeyfe/hd-transport';
13
14
  import { ERRORS, HardwareErrorCode, ONEKEY_WEBUSB_FILTER, wait } from '@onekeyfe/hd-shared';
14
15
 
15
- import { createTransportCallLog, shouldSuppressHighVolumeCallLog } from './transportLog';
16
-
17
16
  import type EventEmitter from 'events';
18
17
  import type {
19
18
  AcquireInput,
@@ -131,31 +130,6 @@ function readSerialNumber(dev: usb.Device, openDevices?: Map<string, OpenDevice>
131
130
  });
132
131
  }
133
132
 
134
- /**
135
- * Promisified USB IN transfer (single attempt).
136
- */
137
- function transferInOnce(ep: usb.InEndpoint, length: number): Promise<Buffer> {
138
- return new Promise((resolve, reject) => {
139
- ep.transfer(length, (err: Error | undefined, data: Buffer | undefined) => {
140
- if (err) return reject(err);
141
- if (!data || data.length === 0) return reject(new Error('Empty USB transfer'));
142
- resolve(data);
143
- });
144
- });
145
- }
146
-
147
- /**
148
- * Promisified USB OUT transfer (single attempt).
149
- */
150
- function transferOutOnce(ep: usb.OutEndpoint, data: Buffer): Promise<void> {
151
- return new Promise((resolve, reject) => {
152
- ep.transfer(data, (err: Error | undefined) => {
153
- if (err) return reject(err);
154
- resolve();
155
- });
156
- });
157
- }
158
-
159
133
  /**
160
134
  * Skip the 0x3F protocol marker byte from a USB packet.
161
135
  */
@@ -188,6 +162,8 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
188
162
  /** Protobuf schema for Protocol V2 transports. */
189
163
  messagesV2: ReturnType<typeof transport.parseConfigure> | undefined;
190
164
 
165
+ private protocolV2SchemaSource: string | undefined;
166
+
191
167
  name = 'NodeUsbTransport';
192
168
 
193
169
  version = '';
@@ -212,6 +188,15 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
212
188
  /** per-path reconnect lock to prevent concurrent reconnects */
213
189
  private reconnectLocks = new Map<string, Promise<OpenDevice>>();
214
190
 
191
+ /**
192
+ * Retain the low-level Transfer so release()/stop() can cancel native pending reads.
193
+ * Endpoint.transfer() hides it and may keep the CLI alive after output completes.
194
+ */
195
+ private activeTransfers = new Map<
196
+ usb.Transfer,
197
+ { path: string; settled: Promise<void>; resolveSettled: () => void }
198
+ >();
199
+
215
200
  /** set to true when cancel() is called; checked by retry loops */
216
201
  private cancelled = false;
217
202
 
@@ -241,7 +226,17 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
241
226
  }
242
227
 
243
228
  configureProtocolV2(signedData: any) {
229
+ const schemaSource =
230
+ typeof signedData === 'string'
231
+ ? signedData
232
+ : JSON.stringify(signedData) ?? String(signedData);
233
+ if (schemaSource === this.protocolV2SchemaSource) return;
234
+
235
+ const hadProtocolV2Schema = this.protocolV2SchemaSource !== undefined;
244
236
  this.messagesV2 = parseConfigure(signedData);
237
+ this.protocolV2SchemaSource = schemaSource;
238
+ if (!hadProtocolV2Schema) return;
239
+
245
240
  this.invalidateAllProtocolV2UsbLinks('Protocol V2 schema reconfigured').catch(error =>
246
241
  this.Log?.debug('[NodeUsbTransport] schema link cleanup failed:', error)
247
242
  );
@@ -251,10 +246,27 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
251
246
  // empty — could add hotplug events via usb.on('attach'/'detach')
252
247
  }
253
248
 
254
- stop() {
255
- this.disposeProtocolV2UsbLinks('Node USB transport stopped').catch(error =>
256
- this.Log?.debug('[NodeUsbTransport] stop link cleanup failed:', error)
257
- );
249
+ async stop(): Promise<void> {
250
+ this.cancelled = true;
251
+
252
+ await this.cancelActiveTransfers();
253
+
254
+ try {
255
+ await this.disposeProtocolV2UsbLinks('Node USB transport stopped');
256
+ } catch (error) {
257
+ this.Log?.debug('[NodeUsbTransport] stop link cleanup failed:', error);
258
+ }
259
+
260
+ // Reconnect may already be in flight when dispose starts. Wait for it to
261
+ // settle, then close every remaining handle so it cannot reopen USB after
262
+ // the first cleanup pass.
263
+ await Promise.allSettled(this.reconnectLocks.values());
264
+ await this.cancelActiveTransfers();
265
+ await Promise.all(Array.from(this.openDevices.keys(), path => this.closeOpenDevice(path)));
266
+
267
+ this.reconnectLocks.clear();
268
+ this.deviceProtocol.clear();
269
+ this.serialToBusId.clear();
258
270
  }
259
271
 
260
272
  /**
@@ -332,10 +344,15 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
332
344
  await this.rotateProtocolV2UsbGeneration(path, 'Node USB transport acquired');
333
345
  await this.closeOpenDevice(path);
334
346
  await this.openDevice(path);
335
- await this.detectProtocol(path, input.expectedProtocol);
347
+ await this.detectProtocol(path, input.expectedProtocol, input.protocolHint);
336
348
  return path;
337
349
  } catch (error: any) {
338
350
  this.Log?.debug('NodeUsbTransport acquire error: ', error);
351
+ try {
352
+ await this.release(path);
353
+ } catch (cleanupError) {
354
+ this.Log?.debug('NodeUsbTransport acquire cleanup error: ', cleanupError);
355
+ }
339
356
  throw ERRORS.TypedError(HardwareErrorCode.DeviceNotFound, error.message ?? String(error));
340
357
  }
341
358
  }
@@ -344,11 +361,93 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
344
361
  * Release device — release interface and close.
345
362
  */
346
363
  async release(path: string, _onclose?: boolean): Promise<void> {
364
+ await this.cancelActiveTransfers(path);
347
365
  await this.invalidateProtocolV2UsbLink(path, 'Node USB transport released');
348
366
  await this.closeOpenDevice(path);
349
367
  this.deviceProtocol.delete(path);
350
368
  }
351
369
 
370
+ private async cancelActiveTransfers(path?: string): Promise<void> {
371
+ const transfers = Array.from(this.activeTransfers.entries()).filter(
372
+ ([, active]) => path === undefined || active.path === path
373
+ );
374
+ transfers.forEach(([transfer]) => {
375
+ try {
376
+ transfer.cancel();
377
+ } catch {
378
+ // A transfer may finish between the snapshot and cancel().
379
+ }
380
+ });
381
+ await Promise.allSettled(transfers.map(([, active]) => active.settled));
382
+ }
383
+
384
+ private createTrackedTransfer(
385
+ path: string,
386
+ endpoint: usb.InEndpoint | usb.OutEndpoint,
387
+ callback: (error: Error | undefined, buffer: Buffer, actualLength: number) => void
388
+ ): usb.Transfer {
389
+ let resolveSettled: () => void = () => undefined;
390
+ const settled = new Promise<void>(resolve => {
391
+ resolveSettled = resolve;
392
+ });
393
+ const transfer = endpoint.makeTransfer(endpoint.timeout, (error, buffer, actualLength) => {
394
+ this.activeTransfers.delete(transfer);
395
+ resolveSettled();
396
+ callback(error, buffer, actualLength);
397
+ });
398
+ this.activeTransfers.set(transfer, { path, settled, resolveSettled });
399
+ return transfer;
400
+ }
401
+
402
+ private transferInOnce(path: string, ep: usb.InEndpoint, length: number): Promise<Buffer> {
403
+ return new Promise((resolve, reject) => {
404
+ const buffer = Buffer.alloc(length);
405
+ const transfer = this.createTrackedTransfer(
406
+ path,
407
+ ep,
408
+ (error, _submittedBuffer, actualLength) => {
409
+ if (error) {
410
+ reject(error);
411
+ return;
412
+ }
413
+ if (actualLength <= 0) {
414
+ reject(new Error('Empty USB transfer'));
415
+ return;
416
+ }
417
+ resolve(buffer.subarray(0, actualLength));
418
+ }
419
+ );
420
+ try {
421
+ transfer.submit(buffer);
422
+ } catch (error) {
423
+ const active = this.activeTransfers.get(transfer);
424
+ this.activeTransfers.delete(transfer);
425
+ active?.resolveSettled();
426
+ reject(error);
427
+ }
428
+ });
429
+ }
430
+
431
+ private transferOutOnce(path: string, ep: usb.OutEndpoint, data: Buffer): Promise<void> {
432
+ return new Promise((resolve, reject) => {
433
+ const transfer = this.createTrackedTransfer(path, ep, error => {
434
+ if (error) {
435
+ reject(error);
436
+ return;
437
+ }
438
+ resolve();
439
+ });
440
+ try {
441
+ transfer.submit(data);
442
+ } catch (error) {
443
+ const active = this.activeTransfers.get(transfer);
444
+ this.activeTransfers.delete(transfer);
445
+ active?.resolveSettled();
446
+ reject(error);
447
+ }
448
+ });
449
+ }
450
+
352
451
  private async closeOpenDevice(path: string): Promise<void> {
353
452
  const openDev = this.openDevices.get(path);
354
453
  if (!openDev) return;
@@ -401,10 +500,6 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
401
500
  `Device protocol has not been detected for ${path}`
402
501
  );
403
502
  }
404
- if (!shouldSuppressHighVolumeCallLog(name)) {
405
- this.Log?.debug('transport call', createTransportCallLog(name, protocol));
406
- }
407
-
408
503
  if (protocol === 'V2') {
409
504
  return this.callProtocolV2(path, name, data, options);
410
505
  }
@@ -468,6 +563,12 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
468
563
 
469
564
  private isRetryableError(error: unknown): boolean {
470
565
  const message = this.getErrorMessage(error).toLowerCase();
566
+ // cancelActiveTransfers() terminates timed-out or releasing native requests.
567
+ // LIBUSB_TRANSFER_CANCELLED is not transient. Retrying would start another pending
568
+ // read on the rebuilt interface and race protocol probing against release.
569
+ if (message.includes('cancelled') || message.includes('canceled')) {
570
+ return false;
571
+ }
471
572
  return (
472
573
  message.includes('libusb') ||
473
574
  message.includes('transfer') ||
@@ -572,7 +673,7 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
572
673
  const packet = new Uint8Array(PACKET_SIZE);
573
674
  packet[0] = REPORT_ID;
574
675
  packet.set(new Uint8Array(buffer), 1);
575
- await transferOutOnce(this.getOpenDevice(path).epOut, Buffer.from(packet));
676
+ await this.transferOutOnce(path, this.getOpenDevice(path).epOut, Buffer.from(packet));
576
677
  }
577
678
  return; // all chunks sent successfully
578
679
  } catch (error) {
@@ -615,7 +716,7 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
615
716
  throw ERRORS.TypedError(HardwareErrorCode.DeviceInterruptedFromOutside, 'Cancelled');
616
717
  }
617
718
  try {
618
- return await transferInOnce(currentDev.epIn, length);
719
+ return await this.transferInOnce(path, currentDev.epIn, length);
619
720
  } catch (error) {
620
721
  lastError = error;
621
722
  if (options?.waitIndefinitelyOnTimeout && this.isUsbTransferTimeout(error)) {
@@ -695,7 +796,7 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
695
796
  epIn.timeout = TRANSFER_TIMEOUT_MS;
696
797
  epOut.timeout = TRANSFER_TIMEOUT_MS;
697
798
 
698
- await this.drainStaleInput(epIn);
799
+ await this.drainStaleInput(path, epIn);
699
800
 
700
801
  this.openDevices.set(path, { device: dev, iface, epIn, epOut });
701
802
  } catch (err) {
@@ -708,14 +809,14 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
708
809
  }
709
810
  }
710
811
 
711
- private async drainStaleInput(epIn: usb.InEndpoint): Promise<void> {
812
+ private async drainStaleInput(path: string, epIn: usb.InEndpoint): Promise<void> {
712
813
  const originalTimeout = epIn.timeout;
713
814
  epIn.timeout = 50;
714
815
  try {
715
816
  // Drain a small bounded number of packets left by the previous USB session.
716
817
  for (let index = 0; index < 16; index += 1) {
717
818
  try {
718
- await transferInOnce(epIn, PACKET_SIZE);
819
+ await this.transferInOnce(path, epIn, PACKET_SIZE);
719
820
  } catch {
720
821
  break;
721
822
  }
@@ -741,37 +842,39 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
741
842
 
742
843
  private async detectProtocol(
743
844
  path: string,
744
- expectedProtocol?: ProtocolType
845
+ expectedProtocol?: ProtocolType,
846
+ protocolHint?: ProtocolType
745
847
  ): Promise<ProtocolType> {
746
- if (expectedProtocol === 'V1') {
747
- if (await this.probeProtocolV1(path)) {
748
- this.deviceProtocol.set(path, 'V1');
749
- return 'V1';
750
- }
751
- throw this.createProtocolMismatchError(expectedProtocol);
752
- }
753
-
754
848
  if (expectedProtocol === 'V2') {
755
849
  if (await this.probeProtocolV2(path)) {
756
850
  this.deviceProtocol.set(path, 'V2');
851
+ this.Log?.debug(`[NodeUsbTransport] detectProtocol: path=${path} -> V2 (expected)`);
757
852
  return 'V2';
758
853
  }
759
854
  throw this.createProtocolMismatchError(expectedProtocol);
760
855
  }
761
856
 
762
- if (this.deviceProtocol.get(path) === 'V2' && (await this.probeProtocolV2(path))) {
763
- this.deviceProtocol.set(path, 'V2');
764
- return 'V2';
857
+ if (expectedProtocol === 'V1') {
858
+ if (await this.probeProtocolV1(path)) {
859
+ this.deviceProtocol.set(path, 'V1');
860
+ return 'V1';
861
+ }
862
+ throw this.createProtocolMismatchError(expectedProtocol);
765
863
  }
766
864
 
767
- if (await this.probeProtocolV1(path)) {
768
- this.deviceProtocol.set(path, 'V1');
769
- return 'V1';
770
- }
865
+ const probeOrder: ProtocolType[] =
866
+ protocolHint === 'V2' || this.deviceProtocol.get(path) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
771
867
 
772
- if (await this.probeProtocolV2(path)) {
773
- this.deviceProtocol.set(path, 'V2');
774
- return 'V2';
868
+ for (const [index, protocol] of probeOrder.entries()) {
869
+ if (index > 0) {
870
+ await this.resetConnectionAfterProbe(path);
871
+ }
872
+ const detected =
873
+ protocol === 'V1' ? await this.probeProtocolV1(path) : await this.probeProtocolV2(path);
874
+ if (detected) {
875
+ this.deviceProtocol.set(path, protocol);
876
+ return protocol;
877
+ }
775
878
  }
776
879
 
777
880
  this.deviceProtocol.delete(path);
@@ -782,6 +885,10 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
782
885
  await this.rotateProtocolV2UsbGeneration(path, 'Node USB protocol probe reset');
783
886
 
784
887
  try {
888
+ // A timed-out probe may leave an IN transfer pending. Cancel and await it before
889
+ // closing the interface, or libusb may never callback and release()/stop() will
890
+ // wait forever, surfacing as Polling timeout (809).
891
+ await this.cancelActiveTransfers(path);
785
892
  await this.closeOpenDevice(path);
786
893
  } catch (error) {
787
894
  this.Log?.debug('[NodeUsbTransport] close after protocol probe error:', error);
@@ -882,7 +989,7 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
882
989
  if (this.cancelled) {
883
990
  throw ERRORS.TypedError(HardwareErrorCode.DeviceInterruptedFromOutside, 'Cancelled');
884
991
  }
885
- await transferOutOnce(this.getOpenDevice(path).epOut, Buffer.from(frame));
992
+ await this.transferOutOnce(path, this.getOpenDevice(path).epOut, Buffer.from(frame));
886
993
  }
887
994
 
888
995
  protected async readProtocolV2UsbPacket(
@@ -894,7 +1001,8 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
894
1001
  throw ERRORS.TypedError(HardwareErrorCode.DeviceInterruptedFromOutside, 'Cancelled');
895
1002
  }
896
1003
  try {
897
- const packet = await transferInOnce(
1004
+ const packet = await this.transferInOnce(
1005
+ path,
898
1006
  this.getOpenDevice(path).epIn,
899
1007
  PROTOCOL_V2_FRAME_MAX_BYTES
900
1008
  );
@@ -910,6 +1018,7 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
910
1018
  }
911
1019
 
912
1020
  protected async resetProtocolV2UsbNativeLink(path: string, _reason: string): Promise<void> {
1021
+ await this.cancelActiveTransfers(path);
913
1022
  await this.closeOpenDevice(path);
914
1023
  }
915
1024
 
@@ -919,7 +1028,10 @@ export default class NodeUsbTransport extends ProtocolV2UsbTransportBase<string>
919
1028
  }
920
1029
 
921
1030
  protected createProtocolV2UsbTimeoutError(name: string, timeoutMs: number): Error {
922
- return new Error(`Protocol V2 response timeout after ${timeoutMs}ms for ${name}`);
1031
+ return new ProtocolV2LinkError(
1032
+ 'response-timeout',
1033
+ `Protocol V2 response timeout after ${timeoutMs}ms for ${name}`
1034
+ );
923
1035
  }
924
1036
 
925
1037
  private async callProtocolV2(
@@ -1,11 +1 @@
1
- import type { ProtocolType } from '@onekeyfe/hd-transport';
2
-
3
- const HIGH_VOLUME_CALLS = new Set(['FileWrite', 'FilesystemFileWrite', 'EmmcFileWrite']);
4
-
5
- export function shouldSuppressHighVolumeCallLog(name: string) {
6
- return HIGH_VOLUME_CALLS.has(name);
7
- }
8
-
9
- export function createTransportCallLog(name: string, protocol: ProtocolType) {
10
- return { name, protocol };
11
- }
1
+ export { createTransportCallLog, shouldSuppressHighVolumeCallLog } from '@onekeyfe/hd-transport';