@onekeyfe/hd-transport-electron 1.2.2-alpha.107 → 1.2.2-alpha.109

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.
@@ -55,13 +55,13 @@ function invokeNobleBleIpc(request) {
55
55
 
56
56
  function initNobleBleSupport(webContents) {
57
57
  return __awaiter(this, void 0, void 0, function* () {
58
- const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-d146f264.js'); });
58
+ const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-23c86603.js'); });
59
59
  setupNobleBleHandlers(webContents);
60
60
  });
61
61
  }
62
62
  function disposeNobleBleSupport(releaseNoble) {
63
63
  return __awaiter(this, void 0, void 0, function* () {
64
- const handler = yield Promise.resolve().then(function () { return require('./noble-ble-handler-d146f264.js'); });
64
+ const handler = yield Promise.resolve().then(function () { return require('./noble-ble-handler-23c86603.js'); });
65
65
  yield handler.disposeNobleBleSupport(releaseNoble);
66
66
  });
67
67
  }
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-47669b97.js');
5
+ var index = require('./index-474dbbae.js');
6
6
 
7
7
 
8
8
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-47669b97.js');
3
+ var index = require('./index-474dbbae.js');
4
4
  var hdShared = require('@onekeyfe/hd-shared');
5
5
  var pRetry = require('p-retry');
6
6
 
@@ -1280,7 +1280,10 @@ function connectDevice(deviceId, webContents) {
1280
1280
  totalDiscovered: discoveredDevices.size,
1281
1281
  totalConnected: connectedDevices.size,
1282
1282
  });
1283
- let peripheral = (_a = discoveredDevices.get(deviceId)) !== null && _a !== void 0 ? _a : connectedDevices.get(deviceId);
1283
+ let peripheral = (_a = connectedDevices.get(deviceId)) !== null && _a !== void 0 ? _a : discoveredDevices.get(deviceId);
1284
+ if ((peripheral === null || peripheral === void 0 ? void 0 : peripheral.state) !== 'connected') {
1285
+ peripheral = undefined;
1286
+ }
1284
1287
  if (!peripheral) {
1285
1288
  if (!noble) {
1286
1289
  yield initializeNoble();
@@ -1 +1 @@
1
- {"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"AAkCA,OAAO,KAAK,EAA+B,WAAW,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAiC1F,KAAK,mBAAmB,GAAG,KAAK,GAAG;IACjC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,mBAAmB,EAAE,aAAa,SAAK,+CAiB3F;AAED,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,OAAO,GAAG,wBAAwB,CAgCvF;AA+KD,wBAAgB,+BAA+B,CAAC,OAAO,CAAC,EAAE,oBAAoB,UAI7E;AAiyDD,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAkOpE;AAOD,wBAAgB,sBAAsB,CACpC,YAAY,GAAE,CAAC,QAAQ,EAAE;IAAE,IAAI,IAAI,IAAI,CAAA;CAAE,KAAK,IAAkC,GAC/E,OAAO,CAAC,IAAI,CAAC,CAiEf"}
1
+ {"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"AAkCA,OAAO,KAAK,EAA+B,WAAW,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAiC1F,KAAK,mBAAmB,GAAG,KAAK,GAAG;IACjC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,mBAAmB,EAAE,aAAa,SAAK,+CAiB3F;AAED,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,OAAO,GAAG,wBAAwB,CAgCvF;AA+KD,wBAAgB,+BAA+B,CAAC,OAAO,CAAC,EAAE,oBAAoB,UAI7E;AAqyDD,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAkOpE;AAOD,wBAAgB,sBAAsB,CACpC,YAAY,GAAE,CAAC,QAAQ,EAAE;IAAE,IAAI,IAAI,IAAI,CAAA;CAAE,KAAK,IAAkC,GAC/E,OAAO,CAAC,IAAI,CAAC,CAiEf"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-electron",
3
- "version": "1.2.2-alpha.107",
3
+ "version": "1.2.2-alpha.109",
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.2-alpha.107",
29
- "@onekeyfe/hd-shared": "1.2.2-alpha.107",
30
- "@onekeyfe/hd-transport": "1.2.2-alpha.107",
28
+ "@onekeyfe/hd-core": "1.2.2-alpha.109",
29
+ "@onekeyfe/hd-shared": "1.2.2-alpha.109",
30
+ "@onekeyfe/hd-transport": "1.2.2-alpha.109",
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": "0139b6e93599824f13f1e7121a70a59ec424f466"
39
+ "gitHead": "4ee0588f08abdde1c72b8044091a76106e4770c4"
40
40
  }
@@ -299,6 +299,59 @@ describe('Electron Noble BLE device discovery', () => {
299
299
  }
300
300
  );
301
301
 
302
+ test.each(['Pro A1B2', 'Pro2 A1B2'])(
303
+ 'refreshes a retained disconnected %s peripheral before cold connecting',
304
+ async name => {
305
+ const handlers = new Map<string, IpcHandler>();
306
+ const stalePeripheral = Object.assign(new EventEmitter(), createPeripheral('device', name), {
307
+ connect: jest.fn(),
308
+ });
309
+ const freshPeripheral = Object.assign(new EventEmitter(), createPeripheral('device', name), {
310
+ connect: jest.fn((callback: (error?: Error) => void) => {
311
+ callback(new Error('expected fresh connection failure'));
312
+ }),
313
+ });
314
+ const noble = Object.assign(new EventEmitter(), {
315
+ state: 'poweredOn',
316
+ startScanning: jest.fn((_services, _duplicates, callback) => {
317
+ callback?.();
318
+ noble.emit('discover', freshPeripheral);
319
+ }),
320
+ stopScanning: jest.fn(callback => callback?.()),
321
+ connectAsync: jest.fn(() => Promise.resolve(undefined)),
322
+ });
323
+ jest.doMock('@stoprocent/noble', () => noble);
324
+ jest.doMock('electron', () => ({
325
+ ipcMain: {
326
+ handle: (channel: string, handler: IpcHandler) => handlers.set(channel, handler),
327
+ removeHandler: (channel: string) => handlers.delete(channel),
328
+ },
329
+ }));
330
+ jest.doMock('electron-log', () => ({
331
+ info: jest.fn(),
332
+ debug: jest.fn(),
333
+ error: jest.fn(),
334
+ }));
335
+ const { setupNobleBleHandlers } = await import('../noble-ble-handler');
336
+ setupNobleBleHandlers({ on: jest.fn(), send: jest.fn() } as unknown as WebContents);
337
+ const availability = handlers.get(EOneKeyBleMessageKeys.BLE_AVAILABILITY_CHECK);
338
+ const connect = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT);
339
+ if (!availability || !connect) throw new Error('Noble handlers were not registered');
340
+ await availability();
341
+ noble.emit('discover', stalePeripheral);
342
+
343
+ await expect(Promise.resolve(connect(undefined, stalePeripheral.id))).resolves.toMatchObject({
344
+ success: false,
345
+ error: { message: 'expected fresh connection failure' },
346
+ });
347
+
348
+ expect(stalePeripheral.connect).not.toHaveBeenCalled();
349
+ expect(freshPeripheral.connect).toHaveBeenCalledTimes(1);
350
+ expect(noble.startScanning).toHaveBeenCalledTimes(1);
351
+ expect(noble.connectAsync).toHaveBeenCalledTimes(name.startsWith('Pro2') ? 1 : 0);
352
+ }
353
+ );
354
+
302
355
  test('waits for a targeted scan to stop before connecting', async () => {
303
356
  jest.useFakeTimers({ doNotFake: ['performance'] });
304
357
 
@@ -920,9 +973,12 @@ describe('Noble BLE process shutdown', () => {
920
973
  discoverServices: jest.fn(),
921
974
  });
922
975
  native.emit('discover', peripheral);
976
+ native.startScanning.mockImplementationOnce((_uuids, _duplicates, callback) => {
977
+ callback?.();
978
+ native.emit('discover', peripheral);
979
+ });
923
980
  const connecting = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT)?.({}, peripheral.id);
924
- await Promise.resolve();
925
- await Promise.resolve();
981
+ await flushCallbacks();
926
982
  expect(peripheral.connect).toHaveBeenCalledTimes(1);
927
983
  const disposing = sdk.disposeNobleBleSupport();
928
984
  await flushCallbacks();
@@ -960,7 +1016,13 @@ describe('Noble BLE process shutdown', () => {
960
1016
  );
961
1017
  const cancelConnect = jest.fn();
962
1018
  if (route === 'direct') Object.assign(native, { connectAsync, cancelConnect });
963
- else native.emit('discover', peripheral);
1019
+ else {
1020
+ native.emit('discover', peripheral);
1021
+ native.startScanning.mockImplementationOnce((_uuids, _duplicates, callback) => {
1022
+ callback?.();
1023
+ native.emit('discover', peripheral);
1024
+ });
1025
+ }
964
1026
  const connecting = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT)?.({}, peripheral.id);
965
1027
  await flushCallbacks();
966
1028
  if (route === 'direct') {
@@ -1765,8 +1765,12 @@ async function connectDevice(deviceId: string, webContents: WebContents): Promis
1765
1765
  totalConnected: connectedDevices.size,
1766
1766
  });
1767
1767
 
1768
- // A kept-alive link can outlive the discovery cache.
1769
- let peripheral = discoveredDevices.get(deviceId) ?? connectedDevices.get(deviceId);
1768
+ // Retained discoveries are display metadata, not proof of a reusable native link.
1769
+ // Prefer the live connection when an older discovery exists for the same device.
1770
+ let peripheral = connectedDevices.get(deviceId) ?? discoveredDevices.get(deviceId);
1771
+ if (peripheral?.state !== 'connected') {
1772
+ peripheral = undefined;
1773
+ }
1770
1774
 
1771
1775
  if (!peripheral) {
1772
1776
  // Initialize Noble if not already done