@onekeyfe/hd-transport-web-device 1.2.0-alpha.169 → 1.2.0-alpha.170

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.
@@ -69,6 +69,23 @@ describe('WebUsbTransport protocol probe cache', () => {
69
69
  expect(webusb.detectProtocol).toHaveBeenCalledWith(path, 'V1', undefined);
70
70
  });
71
71
 
72
+ test('acquire trusts the expected protocol during an explicit no-probe recovery', async () => {
73
+ const webusb = buildAcquirableTransport();
74
+ const path = 'pro-webusb';
75
+ webusb.deviceProtocol.set(path, 'V1');
76
+ webusb.markProtocolStale(path);
77
+ webusb.detectProtocol = jest.fn();
78
+
79
+ await expect(
80
+ webusb.acquire({ path, expectedProtocol: 'V2', skipProtocolProbe: true })
81
+ ).resolves.toBe(path);
82
+
83
+ expect(webusb.detectProtocol).not.toHaveBeenCalled();
84
+ expect(webusb.deviceProtocol.get(path)).toBe('V2');
85
+ expect(webusb.staleProtocolPaths.has(path)).toBe(false);
86
+ expect(webusb.acquiredPaths.has(path)).toBe(true);
87
+ });
88
+
72
89
  test('forced protocol detection bypasses a valid cache and probes on the wire', async () => {
73
90
  const webusb = buildAcquirableTransport();
74
91
  const path = 'pro-webusb';
package/dist/index.js CHANGED
@@ -182,7 +182,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
182
182
  });
183
183
  }
184
184
  acquire(input) {
185
- var _a, _b, _c, _d, _e, _f;
185
+ var _a, _b, _c, _d, _e, _f, _g;
186
186
  return __awaiter(this, void 0, void 0, function* () {
187
187
  if (!input.path)
188
188
  return;
@@ -190,35 +190,49 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
190
190
  yield this.rotateProtocolV2UsbGeneration(input.path, 'WebUSB transport acquired');
191
191
  yield this.closeOpenDevice(input.path);
192
192
  yield this.connect((_a = input.path) !== null && _a !== void 0 ? _a : '', true);
193
- if (input.forceProtocolDetection) {
193
+ if (input.skipProtocolProbe) {
194
+ if (!input.expectedProtocol) {
195
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'skipProtocolProbe requires an expected protocol');
196
+ }
197
+ this.staleProtocolPaths.delete(input.path);
198
+ this.deviceProtocol.set(input.path, input.expectedProtocol);
199
+ const currentDevice = (_b = this.deviceList.find(d => d.path === input.path)) === null || _b === void 0 ? void 0 : _b.device;
200
+ if (currentDevice) {
201
+ this.probedDeviceObjects.set(input.path, currentDevice);
202
+ }
203
+ }
204
+ else if (input.forceProtocolDetection) {
194
205
  this.staleProtocolPaths.delete(input.path);
195
206
  this.deviceProtocol.delete(input.path);
196
207
  this.deviceProtocolHints.delete(input.path);
197
208
  }
198
- if (this.staleProtocolPaths.has(input.path)) {
209
+ if (!input.skipProtocolProbe && this.staleProtocolPaths.has(input.path)) {
199
210
  this.staleProtocolPaths.delete(input.path);
200
211
  this.deviceProtocol.delete(input.path);
201
212
  }
202
- if (this.deviceProtocol.has(input.path)) {
203
- const currentDevice = (_b = this.deviceList.find(d => d.path === input.path)) === null || _b === void 0 ? void 0 : _b.device;
213
+ if (!input.skipProtocolProbe && this.deviceProtocol.has(input.path)) {
214
+ const currentDevice = (_c = this.deviceList.find(d => d.path === input.path)) === null || _c === void 0 ? void 0 : _c.device;
204
215
  if (!currentDevice || currentDevice !== this.probedDeviceObjects.get(input.path)) {
205
216
  this.deviceProtocol.delete(input.path);
206
217
  }
207
218
  }
208
219
  const cachedProtocol = this.deviceProtocol.get(input.path);
209
- if (cachedProtocol && input.expectedProtocol && cachedProtocol !== input.expectedProtocol) {
220
+ if (!input.skipProtocolProbe &&
221
+ cachedProtocol &&
222
+ input.expectedProtocol &&
223
+ cachedProtocol !== input.expectedProtocol) {
210
224
  this.deviceProtocol.delete(input.path);
211
225
  }
212
226
  if (!this.deviceProtocol.has(input.path)) {
213
- const usbDevice = (_c = this.deviceList.find(device => device.path === input.path)) === null || _c === void 0 ? void 0 : _c.device;
227
+ const usbDevice = (_d = this.deviceList.find(device => device.path === input.path)) === null || _d === void 0 ? void 0 : _d.device;
214
228
  const protocolHint = input.expectedProtocol
215
229
  ? undefined
216
- : (_e = (_d = input.protocolHint) !== null && _d !== void 0 ? _d : this.deviceProtocolHints.get(input.path)) !== null && _e !== void 0 ? _e : hdShared.inferProtocolHintFromUsbId(usbDevice === null || usbDevice === void 0 ? void 0 : usbDevice.vendorId, usbDevice === null || usbDevice === void 0 ? void 0 : usbDevice.productId);
230
+ : (_f = (_e = input.protocolHint) !== null && _e !== void 0 ? _e : this.deviceProtocolHints.get(input.path)) !== null && _f !== void 0 ? _f : hdShared.inferProtocolHintFromUsbId(usbDevice === null || usbDevice === void 0 ? void 0 : usbDevice.vendorId, usbDevice === null || usbDevice === void 0 ? void 0 : usbDevice.productId);
217
231
  if (protocolHint) {
218
232
  this.deviceProtocolHints.set(input.path, protocolHint);
219
233
  }
220
234
  yield this.detectProtocol(input.path, input.expectedProtocol, protocolHint);
221
- const probedDevice = (_f = this.deviceList.find(d => d.path === input.path)) === null || _f === void 0 ? void 0 : _f.device;
235
+ const probedDevice = (_g = this.deviceList.find(d => d.path === input.path)) === null || _g === void 0 ? void 0 : _g.device;
222
236
  if (probedDevice) {
223
237
  this.probedDeviceObjects.set(input.path, probedDevice);
224
238
  }
@@ -1 +1 @@
1
- {"version":3,"file":"webusb.d.ts","sourceRoot":"","sources":["../src/webusb.ts"],"names":[],"mappings":";;AACA,OAAO,SAAS,EAAE,EAQhB,0BAA0B,EAG3B,MAAM,wBAAwB,CAAC;AAYhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,YAAY,EACZ,oBAAoB,EACpB,YAAY,EACZ,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAuBhC,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,SAAS,CAAC;IAClB,YAAY,CAAC,EAAE,YAAY,C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uB;YA0CvB,eAAe;YAoBf,eAAe;IAgBvB,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;YA2BlB,cAAc;YAkCd,cAAc;IAYtB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;IAgD5C,OAAO,CAAC,IAAI,EAAE,MAAM;IAY1B,SAAS,CAAC,uBAAuB,IAAI,iBAAiB;IAUtD,SAAS,CAAC,sBAAsB;cAIhB,wBAAwB,CACtC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,qBAAqB,GAC9B,OAAO,CAAC,IAAI,CAAC;cAIA,uBAAuB,CACrC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,qBAAqB,GAC9B,OAAO,CAAC,UAAU,CAAC;cASN,4BAA4B,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI1F,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;IAWjF,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.169",
3
+ "version": "1.2.0-alpha.170",
4
4
  "author": "OneKey",
5
5
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
6
6
  "license": "MIT",
@@ -21,13 +21,13 @@
21
21
  "lint:fix": "eslint . --fix"
22
22
  },
23
23
  "dependencies": {
24
- "@onekeyfe/hd-shared": "1.2.0-alpha.169",
25
- "@onekeyfe/hd-transport": "1.2.0-alpha.169"
24
+ "@onekeyfe/hd-shared": "1.2.0-alpha.170",
25
+ "@onekeyfe/hd-transport": "1.2.0-alpha.170"
26
26
  },
27
27
  "devDependencies": {
28
- "@onekeyfe/hd-transport-electron": "1.2.0-alpha.169",
28
+ "@onekeyfe/hd-transport-electron": "1.2.0-alpha.170",
29
29
  "@types/w3c-web-usb": "^1.0.6",
30
30
  "@types/web-bluetooth": "^0.0.17"
31
31
  },
32
- "gitHead": "79c2f32cf0d21d9f1ff3d4d8728b8c83bf48c0e9"
32
+ "gitHead": "d702db069d6b19f74cd5f1cf42277a7152beb3a8"
33
33
  }
package/src/webusb.ts CHANGED
@@ -309,7 +309,20 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
309
309
  await this.rotateProtocolV2UsbGeneration(input.path, 'WebUSB transport acquired');
310
310
  await this.closeOpenDevice(input.path);
311
311
  await this.connect(input.path ?? '', true);
312
- if (input.forceProtocolDetection) {
312
+ if (input.skipProtocolProbe) {
313
+ if (!input.expectedProtocol) {
314
+ throw ERRORS.TypedError(
315
+ HardwareErrorCode.RuntimeError,
316
+ 'skipProtocolProbe requires an expected protocol'
317
+ );
318
+ }
319
+ this.staleProtocolPaths.delete(input.path);
320
+ this.deviceProtocol.set(input.path, input.expectedProtocol);
321
+ const currentDevice = this.deviceList.find(d => d.path === input.path)?.device;
322
+ if (currentDevice) {
323
+ this.probedDeviceObjects.set(input.path, currentDevice);
324
+ }
325
+ } else if (input.forceProtocolDetection) {
313
326
  // Explicit recovery/discovery (e.g. detectDeviceConnectProtocol) must
314
327
  // probe on the wire regardless of any cached result — the cached
315
328
  // binding may be exactly what the caller is trying to recover from.
@@ -317,13 +330,13 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
317
330
  this.deviceProtocol.delete(input.path);
318
331
  this.deviceProtocolHints.delete(input.path);
319
332
  }
320
- if (this.staleProtocolPaths.has(input.path)) {
333
+ if (!input.skipProtocolProbe && this.staleProtocolPaths.has(input.path)) {
321
334
  // The device disconnected (possibly rebooting into another mode) since
322
335
  // the protocol was probed — drop the cache so it is re-probed below.
323
336
  this.staleProtocolPaths.delete(input.path);
324
337
  this.deviceProtocol.delete(input.path);
325
338
  }
326
- if (this.deviceProtocol.has(input.path)) {
339
+ if (!input.skipProtocolProbe && this.deviceProtocol.has(input.path)) {
327
340
  const currentDevice = this.deviceList.find(d => d.path === input.path)?.device;
328
341
  if (!currentDevice || currentDevice !== this.probedDeviceObjects.get(input.path)) {
329
342
  // The OS re-enumerated the device since the probe (a replug/reboot we
@@ -333,7 +346,12 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
333
346
  }
334
347
  }
335
348
  const cachedProtocol = this.deviceProtocol.get(input.path);
336
- if (cachedProtocol && input.expectedProtocol && cachedProtocol !== input.expectedProtocol) {
349
+ if (
350
+ !input.skipProtocolProbe &&
351
+ cachedProtocol &&
352
+ input.expectedProtocol &&
353
+ cachedProtocol !== input.expectedProtocol
354
+ ) {
337
355
  // The caller expects a different protocol than the cached probe result;
338
356
  // the cache is stale — drop it and re-probe on the wire below.
339
357
  this.deviceProtocol.delete(input.path);