@onekeyfe/hd-transport-electron 1.2.0-alpha.165 → 1.2.0-alpha.166

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.
@@ -32,7 +32,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
32
32
 
33
33
  function initNobleBleSupport(webContents) {
34
34
  return __awaiter(this, void 0, void 0, function* () {
35
- const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-255383f1.js'); });
35
+ const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-038532d9.js'); });
36
36
  setupNobleBleHandlers(webContents);
37
37
  });
38
38
  }
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('./index-5478256a.js');
5
+ var index = require('./index-f4b406db.js');
6
6
 
7
7
 
8
8
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-5478256a.js');
3
+ var index = require('./index-f4b406db.js');
4
4
  var hdShared = require('@onekeyfe/hd-shared');
5
5
  var pRetry = require('p-retry');
6
6
 
@@ -396,6 +396,10 @@ function cleanupDevice(deviceId, webContents, options = {}) {
396
396
  pairedDevices.delete(deviceId);
397
397
  }
398
398
  if (cleanupDiscoveredCache) {
399
+ const evicted = discoveredDevices.get(deviceId);
400
+ if (evicted) {
401
+ stalePeripherals.set(deviceId, evicted);
402
+ }
399
403
  discoveredDevices.delete(deviceId);
400
404
  }
401
405
  if (sendDisconnectEvent) {
@@ -600,6 +604,7 @@ function transmitHexDataToDevice(deviceId, hexData, options) {
600
604
  });
601
605
  }
602
606
  const BLE_COLD_CONNECT_SCAN_TIMEOUT_MS = 1500;
607
+ const stalePeripherals = new Map();
603
608
  const bleNamesById = new Map();
604
609
  function rememberBleName(deviceId, name) {
605
610
  const trimmed = name === null || name === void 0 ? void 0 : name.trim();
@@ -909,7 +914,12 @@ function forceReconnectPeripheral(peripheral, deviceId) {
909
914
  yield runBleCallbackOperation(callback => peripheral.disconnect(() => {
910
915
  logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Force disconnect completed');
911
916
  callback();
912
- }), { timeoutMs: BLE_CLEANUP_TIMEOUT, timeoutBehavior: 'resolve' });
917
+ }), { timeoutMs: BLE_DISCONNECT_CONFIRM_TIMEOUT_MS, timeoutBehavior: 'resolve' });
918
+ if (peripheral.state === 'connected') {
919
+ logger === null || logger === void 0 ? void 0 : logger.warn('[NobleBLE] Reset disconnect did not confirm, reconnecting anyway', {
920
+ deviceId,
921
+ });
922
+ }
913
923
  }
914
924
  yield runBleCallbackOperation(callback => peripheral.connect(callback), {
915
925
  timeoutMs: NOBLE_BLE_CONNECTION_TIMEOUT_MS,
@@ -1112,6 +1122,16 @@ function connectDevice(deviceId, webContents) {
1112
1122
  if (!peripheral) {
1113
1123
  peripheral = byIdFirst ? yield scanForPeripheral() : yield connectById();
1114
1124
  }
1125
+ if (!peripheral) {
1126
+ const stale = stalePeripherals.get(deviceId);
1127
+ if (stale) {
1128
+ logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Falling back to the last known peripheral:', deviceId);
1129
+ peripheral = stale;
1130
+ }
1131
+ }
1132
+ else {
1133
+ stalePeripherals.delete(deviceId);
1134
+ }
1115
1135
  }
1116
1136
  if (!peripheral) {
1117
1137
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotFound, `Device ${deviceId} not found`);
@@ -1 +1 @@
1
- {"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"AAgCA,OAAO,KAAK,EAA+B,WAAW,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAoFhE,wBAAgB,+BAA+B,CAAC,OAAO,CAAC,EAAE,oBAAoB,UAI7E;AAksDD,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAkMpE"}
1
+ {"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"AAgCA,OAAO,KAAK,EAA+B,WAAW,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAoFhE,wBAAgB,+BAA+B,CAAC,OAAO,CAAC,EAAE,oBAAoB,UAI7E;AAkuDD,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAkMpE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-electron",
3
- "version": "1.2.0-alpha.165",
3
+ "version": "1.2.0-alpha.166",
4
4
  "author": "OneKey",
5
5
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
6
6
  "license": "MIT",
@@ -25,9 +25,9 @@
25
25
  "electron-log": ">=4.0.0"
26
26
  },
27
27
  "dependencies": {
28
- "@onekeyfe/hd-core": "1.2.0-alpha.165",
29
- "@onekeyfe/hd-shared": "1.2.0-alpha.165",
30
- "@onekeyfe/hd-transport": "1.2.0-alpha.165",
28
+ "@onekeyfe/hd-core": "1.2.0-alpha.166",
29
+ "@onekeyfe/hd-shared": "1.2.0-alpha.166",
30
+ "@onekeyfe/hd-transport": "1.2.0-alpha.166",
31
31
  "@stoprocent/noble": "2.3.16",
32
32
  "p-retry": "^4.6.2"
33
33
  },
@@ -36,5 +36,5 @@
36
36
  "electron": "^25.0.0",
37
37
  "typescript": "^5.3.3"
38
38
  },
39
- "gitHead": "dc82ee336f9dc638352195bebd22ee606fa3ef5c"
39
+ "gitHead": "3dd1b964d8503ee800f44466b8c08e3d59be67e8"
40
40
  }
@@ -503,8 +503,16 @@ function cleanupDevice(
503
503
  pairedDevices.delete(deviceId);
504
504
  }
505
505
 
506
- // 2. Clean up discovered cache (optional)
506
+ // 2. Clean up discovered cache (optional). A peripheral that has been
507
+ // through a disconnect is never preferred again — reconnecting it is what
508
+ // produces stale-session links — but it is kept aside as the last resort for
509
+ // a bonded device that stays silent, so eviction cannot turn a working
510
+ // reconnect into DeviceNotFound.
507
511
  if (cleanupDiscoveredCache) {
512
+ const evicted = discoveredDevices.get(deviceId);
513
+ if (evicted) {
514
+ stalePeripherals.set(deviceId, evicted);
515
+ }
508
516
  discoveredDevices.delete(deviceId);
509
517
  }
510
518
 
@@ -796,6 +804,10 @@ async function transmitHexDataToDevice(
796
804
  // 631ms, so this leaves ~2x headroom while keeping a miss cheap.
797
805
  const BLE_COLD_CONNECT_SCAN_TIMEOUT_MS = 1500;
798
806
 
807
+ // Peripherals evicted by a disconnect, kept only as the fallback of last
808
+ // resort in connectDevice (see cleanupDevice).
809
+ const stalePeripherals = new Map<string, Peripheral>();
810
+
799
811
  // Advertised names, kept for the life of the process: device caches are purged
800
812
  // on every disconnect, but the family a device belongs to never changes and
801
813
  // decides which connection strategy is safe for it.
@@ -1225,7 +1237,10 @@ async function forceReconnectPeripheral(peripheral: Peripheral, deviceId: string
1225
1237
  reason: 'force-reconnect',
1226
1238
  });
1227
1239
 
1228
- // Step 2: Force disconnect if connected
1240
+ // Step 2: Force disconnect if connected. Wait for the teardown to be
1241
+ // confirmed: reconnecting while CoreBluetooth is still closing the old link
1242
+ // rebuilds the very stale-session state this reset exists to clear, and
1243
+ // 250ms is not enough to prove a physical teardown on macOS.
1229
1244
  if (peripheral.state === 'connected') {
1230
1245
  await runBleCallbackOperation(
1231
1246
  callback =>
@@ -1233,8 +1248,13 @@ async function forceReconnectPeripheral(peripheral: Peripheral, deviceId: string
1233
1248
  logger?.info('[NobleBLE] Force disconnect completed');
1234
1249
  callback();
1235
1250
  }),
1236
- { timeoutMs: BLE_CLEANUP_TIMEOUT, timeoutBehavior: 'resolve' }
1251
+ { timeoutMs: BLE_DISCONNECT_CONFIRM_TIMEOUT_MS, timeoutBehavior: 'resolve' }
1237
1252
  );
1253
+ if (peripheral.state === 'connected') {
1254
+ logger?.warn('[NobleBLE] Reset disconnect did not confirm, reconnecting anyway', {
1255
+ deviceId,
1256
+ });
1257
+ }
1238
1258
  }
1239
1259
 
1240
1260
  // Step 3: Re-establish connection
@@ -1535,6 +1555,18 @@ async function connectDevice(deviceId: string, webContents: WebContents): Promis
1535
1555
  // silent), or not reachable by id. Try the other one before giving up.
1536
1556
  peripheral = byIdFirst ? await scanForPeripheral() : await connectById();
1537
1557
  }
1558
+ if (!peripheral) {
1559
+ // Neither route resolved a peripheral: a bonded device that has gone
1560
+ // silent, or connect-by-id sitting in its cooldown. The evicted object is
1561
+ // worse than a fresh one, but it beats failing the call outright.
1562
+ const stale = stalePeripherals.get(deviceId);
1563
+ if (stale) {
1564
+ logger?.info('[NobleBLE] Falling back to the last known peripheral:', deviceId);
1565
+ peripheral = stale;
1566
+ }
1567
+ } else {
1568
+ stalePeripherals.delete(deviceId);
1569
+ }
1538
1570
  }
1539
1571
 
1540
1572
  // At this point, peripheral is guaranteed to be defined