@onekeyfe/hd-transport-electron 1.2.3-alpha.7 → 1.2.3-alpha.8

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-d0991fa0.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-d0991fa0.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-e2e3d2ba.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-e2e3d2ba.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
 
@@ -123,7 +123,8 @@ function assertBleActive() {
123
123
  }
124
124
  function createNobleBleConnectionError(error, messagePrefix = '') {
125
125
  const errorMessage = error.message;
126
- const isInvalidMacOsBond = error.nativeErrorCode === 14 && error.nativeErrorDomain === 'CBErrorDomain';
126
+ const isInvalidMacOsBond = (error.nativeErrorCode === 14 && error.nativeErrorDomain === 'CBErrorDomain') ||
127
+ (error.nativeErrorCode === 15 && error.nativeErrorDomain === 'CBATTErrorDomain');
127
128
  if (isInvalidMacOsBond) {
128
129
  const nativeErrorMessage = `${messagePrefix}${errorMessage}`;
129
130
  return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleBondInvalid, `${hdShared.HardwareErrorCodeMessage[hdShared.HardwareErrorCode.BleBondInvalid]} (${nativeErrorMessage})`, {
@@ -1254,15 +1255,11 @@ function tryDirectConnectById(deviceId) {
1254
1255
  return peripheral;
1255
1256
  }
1256
1257
  catch (error) {
1258
+ directConnectCooldownUntil.set(deviceId, Date.now() + DIRECT_CONNECT_COOLDOWN_MS);
1257
1259
  logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Direct connect-by-id failed, falling back to scan', {
1258
1260
  deviceId,
1259
1261
  error: String(error),
1260
1262
  });
1261
- const nativeError = error;
1262
- if (nativeError.nativeErrorCode === 14 && nativeError.nativeErrorDomain === 'CBErrorDomain') {
1263
- throw createNobleBleConnectionError(nativeError);
1264
- }
1265
- directConnectCooldownUntil.set(deviceId, Date.now() + DIRECT_CONNECT_COOLDOWN_MS);
1266
1263
  return undefined;
1267
1264
  }
1268
1265
  finally {
@@ -1313,29 +1310,17 @@ function connectDevice(deviceId, webContents) {
1313
1310
  return undefined;
1314
1311
  }
1315
1312
  });
1316
- let staleBondError;
1317
1313
  const connectById = () => index.__awaiter(this, void 0, void 0, function* () {
1318
- try {
1319
- const found = yield tryDirectConnectById(deviceId);
1320
- if (found) {
1321
- discoveredDevices.set(deviceId, found);
1322
- }
1323
- return found;
1324
- }
1325
- catch (error) {
1326
- if (error.errorCode !== hdShared.HardwareErrorCode.BleBondInvalid) {
1327
- throw error;
1328
- }
1329
- staleBondError = error;
1330
- return undefined;
1314
+ const found = yield tryDirectConnectById(deviceId);
1315
+ if (found) {
1316
+ discoveredDevices.set(deviceId, found);
1331
1317
  }
1318
+ return found;
1332
1319
  });
1333
1320
  peripheral = byIdFirst ? yield connectById() : yield scanForPeripheral();
1334
1321
  if (!peripheral) {
1335
1322
  peripheral = byIdFirst ? yield scanForPeripheral() : yield connectById();
1336
1323
  }
1337
- if (!peripheral && staleBondError)
1338
- throw staleBondError;
1339
1324
  }
1340
1325
  assertBleActive();
1341
1326
  if (!peripheral) {
@@ -1532,12 +1517,7 @@ function subscribeNotifications(deviceId, callback) {
1532
1517
  });
1533
1518
  }
1534
1519
  catch (error) {
1535
- const nativeError = error;
1536
- if (nativeError.nativeErrorCode === 15 &&
1537
- nativeError.nativeErrorDomain === 'CBATTErrorDomain') {
1538
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded, hdShared.HardwareErrorCodeMessage[hdShared.HardwareErrorCode.BleDeviceNotBonded], { nativeErrorMessage: `Notification subscription failed: ${nativeError.message}` });
1539
- }
1540
- throw createNobleBleConnectionError(nativeError, 'Notification subscription failed: ');
1520
+ throw createNobleBleConnectionError(error, 'Notification subscription failed: ');
1541
1521
  }
1542
1522
  finally {
1543
1523
  if (!disposing)
@@ -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,+CAgB3F;AAED,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,OAAO,GAAG,wBAAwB,CAgCvF;AA+KD,wBAAgB,+BAA+B,CAAC,OAAO,CAAC,EAAE,oBAAoB,UAI7E;AA2zDD,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.3-alpha.7",
3
+ "version": "1.2.3-alpha.8",
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.3-alpha.7",
29
- "@onekeyfe/hd-shared": "1.2.3-alpha.7",
30
- "@onekeyfe/hd-transport": "1.2.3-alpha.7",
28
+ "@onekeyfe/hd-core": "1.2.3-alpha.8",
29
+ "@onekeyfe/hd-shared": "1.2.3-alpha.8",
30
+ "@onekeyfe/hd-transport": "1.2.3-alpha.8",
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": "b71eb64f2fc6b58ec7847f93cc99225bfec04d5b"
39
+ "gitHead": "170aac4b7ca98bf24b5dd36b9729c98a10389161"
40
40
  }
@@ -68,7 +68,7 @@ describe('Electron Noble BLE device discovery', () => {
68
68
  })
69
69
  )
70
70
  ).toMatchObject({
71
- errorCode: HardwareErrorCode.BleConnectedError,
71
+ errorCode: HardwareErrorCode.BleBondInvalid,
72
72
  });
73
73
  expect(createNobleBleConnectionError(new Error('connection failed'))).toMatchObject({
74
74
  errorCode: HardwareErrorCode.BleConnectedError,
@@ -352,87 +352,6 @@ describe('Electron Noble BLE device discovery', () => {
352
352
  }
353
353
  );
354
354
 
355
- test.each([
356
- { rediscovered: false, expectedCode: HardwareErrorCode.BleBondInvalid },
357
- { rediscovered: true, expectedCode: HardwareErrorCode.BleConnectedError },
358
- ])(
359
- 'reports a macOS stale bond only when fallback scan misses (rediscovered=$rediscovered)',
360
- async ({ rediscovered, expectedCode }) => {
361
- jest.useFakeTimers({ doNotFake: ['performance'] });
362
- const handlers = new Map<string, IpcHandler>();
363
- const stalePeripheral = createPeripheral('device', 'Pro2 A1B2');
364
- const freshPeripheral = Object.assign(
365
- new EventEmitter(),
366
- createPeripheral('device', 'Pro2 A1B2'),
367
- {
368
- connect: jest.fn((callback: (error?: Error) => void) => {
369
- callback(new Error('fresh connection failed'));
370
- }),
371
- }
372
- );
373
- const staleBondError = Object.assign(new Error('Peer removed pairing information'), {
374
- nativeErrorCode: 14,
375
- nativeErrorDomain: 'CBErrorDomain',
376
- });
377
- let scanStarted: (() => void) | undefined;
378
- const scanning = new Promise<void>(resolve => {
379
- scanStarted = resolve;
380
- });
381
- const noble = Object.assign(new EventEmitter(), {
382
- state: 'poweredOn',
383
- startScanning: jest.fn((_services, _duplicates, callback) => {
384
- callback?.();
385
- if (rediscovered) noble.emit('discover', freshPeripheral);
386
- scanStarted?.();
387
- }),
388
- stopScanning: jest.fn(callback => callback?.()),
389
- connectAsync: jest.fn(() => Promise.reject(staleBondError)),
390
- });
391
- jest.doMock('@stoprocent/noble', () => noble);
392
- jest.doMock('electron', () => ({
393
- ipcMain: {
394
- handle: (channel: string, handler: IpcHandler) => handlers.set(channel, handler),
395
- removeHandler: (channel: string) => handlers.delete(channel),
396
- },
397
- }));
398
- jest.doMock('electron-log', () => ({
399
- info: jest.fn(),
400
- debug: jest.fn(),
401
- error: jest.fn(),
402
- }));
403
- const { setupNobleBleHandlers } = await import('../noble-ble-handler');
404
- setupNobleBleHandlers({ on: jest.fn(), send: jest.fn() } as unknown as WebContents);
405
- const availability = handlers.get(EOneKeyBleMessageKeys.BLE_AVAILABILITY_CHECK);
406
- const connect = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT);
407
- if (!availability || !connect) throw new Error('Noble handlers were not registered');
408
- await availability();
409
- noble.emit('discover', stalePeripheral);
410
-
411
- const connecting = Promise.resolve(connect(undefined, stalePeripheral.id));
412
- await scanning;
413
- jest.advanceTimersByTime(1500);
414
- await expect(connecting).resolves.toMatchObject({
415
- success: false,
416
- error: { errorCode: expectedCode },
417
- });
418
- if (!rediscovered) {
419
- const secondScan = new Promise<void>(resolve => {
420
- scanStarted = resolve;
421
- });
422
- const retry = Promise.resolve(connect(undefined, stalePeripheral.id));
423
- await secondScan;
424
- jest.advanceTimersByTime(1500);
425
- await expect(retry).resolves.toMatchObject({
426
- success: false,
427
- error: { errorCode: HardwareErrorCode.BleBondInvalid },
428
- });
429
- }
430
- expect(noble.connectAsync).toHaveBeenCalledTimes(rediscovered ? 1 : 2);
431
- expect(noble.startScanning).toHaveBeenCalledTimes(rediscovered ? 1 : 2);
432
- expect(freshPeripheral.connect).toHaveBeenCalledTimes(rediscovered ? 1 : 0);
433
- }
434
- );
435
-
436
355
  test('waits for a targeted scan to stop before connecting', async () => {
437
356
  jest.useFakeTimers({ doNotFake: ['performance'] });
438
357
 
@@ -786,7 +705,7 @@ describe('Electron Noble BLE device discovery', () => {
786
705
  type: 'NobleBleIpcError',
787
706
  success: false,
788
707
  error: {
789
- errorCode: HardwareErrorCode.BleDeviceNotBonded,
708
+ errorCode: HardwareErrorCode.BleBondInvalid,
790
709
  params: {
791
710
  nativeErrorMessage: 'Notification subscription failed: Encryption is insufficient',
792
711
  },
@@ -75,7 +75,8 @@ type NobleBleNativeError = Error & {
75
75
  export function createNobleBleConnectionError(error: NobleBleNativeError, messagePrefix = '') {
76
76
  const errorMessage = error.message;
77
77
  const isInvalidMacOsBond =
78
- error.nativeErrorCode === 14 && error.nativeErrorDomain === 'CBErrorDomain';
78
+ (error.nativeErrorCode === 14 && error.nativeErrorDomain === 'CBErrorDomain') ||
79
+ (error.nativeErrorCode === 15 && error.nativeErrorDomain === 'CBATTErrorDomain');
79
80
  if (isInvalidMacOsBond) {
80
81
  const nativeErrorMessage = `${messagePrefix}${errorMessage}`;
81
82
  return ERRORS.TypedError(
@@ -1741,15 +1742,11 @@ async function tryDirectConnectById(deviceId: string): Promise<Peripheral | unde
1741
1742
  logger?.info('[NobleBLE] Direct connect-by-id succeeded', { deviceId });
1742
1743
  return peripheral;
1743
1744
  } catch (error) {
1745
+ directConnectCooldownUntil.set(deviceId, Date.now() + DIRECT_CONNECT_COOLDOWN_MS);
1744
1746
  logger?.info('[NobleBLE] Direct connect-by-id failed, falling back to scan', {
1745
1747
  deviceId,
1746
1748
  error: String(error),
1747
1749
  });
1748
- const nativeError = error as NobleBleNativeError;
1749
- if (nativeError.nativeErrorCode === 14 && nativeError.nativeErrorDomain === 'CBErrorDomain') {
1750
- throw createNobleBleConnectionError(nativeError);
1751
- }
1752
- directConnectCooldownUntil.set(deviceId, Date.now() + DIRECT_CONNECT_COOLDOWN_MS);
1753
1750
  return undefined;
1754
1751
  } finally {
1755
1752
  clearTimeout(timer);
@@ -1812,21 +1809,12 @@ async function connectDevice(deviceId: string, webContents: WebContents): Promis
1812
1809
  }
1813
1810
  };
1814
1811
 
1815
- let staleBondError: Error | undefined;
1816
1812
  const connectById = async () => {
1817
- try {
1818
- const found = await tryDirectConnectById(deviceId);
1819
- if (found) {
1820
- discoveredDevices.set(deviceId, found);
1821
- }
1822
- return found;
1823
- } catch (error) {
1824
- if ((error as { errorCode?: number }).errorCode !== HardwareErrorCode.BleBondInvalid) {
1825
- throw error;
1826
- }
1827
- staleBondError = error as Error;
1828
- return undefined;
1813
+ const found = await tryDirectConnectById(deviceId);
1814
+ if (found) {
1815
+ discoveredDevices.set(deviceId, found);
1829
1816
  }
1817
+ return found;
1830
1818
  };
1831
1819
 
1832
1820
  peripheral = byIdFirst ? await connectById() : await scanForPeripheral();
@@ -1835,7 +1823,6 @@ async function connectDevice(deviceId: string, webContents: WebContents): Promis
1835
1823
  // silent), or not reachable by id. Try the other one before giving up.
1836
1824
  peripheral = byIdFirst ? await scanForPeripheral() : await connectById();
1837
1825
  }
1838
- if (!peripheral && staleBondError) throw staleBondError;
1839
1826
  }
1840
1827
 
1841
1828
  assertBleActive();
@@ -2133,18 +2120,10 @@ async function subscribeNotifications(
2133
2120
  ms: Date.now() - subscribeStartedAt,
2134
2121
  });
2135
2122
  } catch (error) {
2136
- const nativeError = error as NobleBleNativeError;
2137
- if (
2138
- nativeError.nativeErrorCode === 15 &&
2139
- nativeError.nativeErrorDomain === 'CBATTErrorDomain'
2140
- ) {
2141
- throw ERRORS.TypedError(
2142
- HardwareErrorCode.BleDeviceNotBonded,
2143
- HardwareErrorCodeMessage[HardwareErrorCode.BleDeviceNotBonded],
2144
- { nativeErrorMessage: `Notification subscription failed: ${nativeError.message}` }
2145
- );
2146
- }
2147
- throw createNobleBleConnectionError(nativeError, 'Notification subscription failed: ');
2123
+ throw createNobleBleConnectionError(
2124
+ error as NobleBleNativeError,
2125
+ 'Notification subscription failed: '
2126
+ );
2148
2127
  } finally {
2149
2128
  // 🔒 CRITICAL: Always clear operation state (even on error)
2150
2129
  if (!disposing) subscriptionOperations.set(deviceId, 'idle');