@onekeyfe/hd-transport-electron 1.2.8-alpha.3 → 1.2.8-alpha.5

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-688faeb6.js'); });
58
+ const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-804f0a41.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-688faeb6.js'); });
64
+ const handler = yield Promise.resolve().then(function () { return require('./noble-ble-handler-804f0a41.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-98d6f724.js');
5
+ var index = require('./index-2231e562.js');
6
6
 
7
7
 
8
8
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-98d6f724.js');
3
+ var index = require('./index-2231e562.js');
4
4
  var hdShared = require('@onekeyfe/hd-shared');
5
5
  var pRetry = require('p-retry');
6
6
 
@@ -376,8 +376,18 @@ function updateBluetoothState(state) {
376
376
  function initializeNoble() {
377
377
  return index.__awaiter(this, void 0, void 0, function* () {
378
378
  assertBleActive();
379
- if (noble)
379
+ if (noble) {
380
+ if (noble.state === 'poweredOff') {
381
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
382
+ }
383
+ if (noble.state === 'unsupported') {
384
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
385
+ }
386
+ if (noble.state === 'unauthorized') {
387
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleLocationError);
388
+ }
380
389
  return;
390
+ }
381
391
  try {
382
392
  noble = require('@stoprocent/noble');
383
393
  logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Noble library loaded');
@@ -869,9 +879,7 @@ function performTargetedScan(targetDeviceId, timeoutMs = NOBLE_BLE_TARGETED_SCAN
869
879
  }
870
880
  function enumerateDevices(isWindowDestroyed) {
871
881
  return index.__awaiter(this, void 0, void 0, function* () {
872
- if (!noble) {
873
- yield initializeNoble();
874
- }
882
+ yield initializeNoble();
875
883
  if (!noble) {
876
884
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Noble not available');
877
885
  }
@@ -1268,6 +1276,7 @@ function tryDirectConnectById(deviceId) {
1268
1276
  function connectDevice(deviceId, webContents) {
1269
1277
  var _a, _b, _c;
1270
1278
  return index.__awaiter(this, void 0, void 0, function* () {
1279
+ yield initializeNoble();
1271
1280
  logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Connect device request:', {
1272
1281
  deviceId,
1273
1282
  hasDiscovered: discoveredDevices.has(deviceId),
@@ -1281,9 +1290,6 @@ function connectDevice(deviceId, webContents) {
1281
1290
  peripheral = undefined;
1282
1291
  }
1283
1292
  if (!peripheral) {
1284
- if (!noble) {
1285
- yield initializeNoble();
1286
- }
1287
1293
  if (!noble) {
1288
1294
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Noble not available');
1289
1295
  }
@@ -1 +1 @@
1
- {"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"AAiCA,OAAO,KAAK,EAA+B,WAAW,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;A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;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.8-alpha.3",
3
+ "version": "1.2.8-alpha.5",
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.8-alpha.3",
29
- "@onekeyfe/hd-shared": "1.2.8-alpha.3",
30
- "@onekeyfe/hd-transport": "1.2.8-alpha.3",
28
+ "@onekeyfe/hd-core": "1.2.8-alpha.5",
29
+ "@onekeyfe/hd-shared": "1.2.8-alpha.5",
30
+ "@onekeyfe/hd-transport": "1.2.8-alpha.5",
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": "18c57239359413a079fffa730b36149c8516aa85"
39
+ "gitHead": "c1f24dc0a59d0c02fe05594a9c4ae7652ec9cc32"
40
40
  }
@@ -923,16 +923,21 @@ describe('Noble BLE process shutdown', () => {
923
923
  ['unauthorized', HardwareErrorCode.BleLocationError],
924
924
  ['unsupported', HardwareErrorCode.BleUnsupported],
925
925
  ])(
926
- 'classifies the initial adapter state %s without waiting for another event',
926
+ 'preserves adapter state %s across repeated scans and connections',
927
927
  async (state, errorCode) => {
928
928
  jest.useFakeTimers({ doNotFake: ['performance'] });
929
929
  const { sdk, native, handlers } = await setup(state);
930
- await expect(
931
- handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE)?.({})
932
- ).resolves.toMatchObject({
933
- success: false,
934
- error: { errorCode },
935
- });
930
+ for (let attempt = 0; attempt < 2; attempt += 1) {
931
+ for (const channel of [
932
+ EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE,
933
+ EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT,
934
+ ]) {
935
+ await expect(handlers.get(channel)?.({}, 'device')).resolves.toMatchObject({
936
+ success: false,
937
+ error: { errorCode },
938
+ });
939
+ }
940
+ }
936
941
  expect(native.startScanning).not.toHaveBeenCalled();
937
942
  expect(native.listenerCount('stateChange')).toBe(1);
938
943
  expect(jest.getTimerCount()).toBe(0);
@@ -940,6 +945,32 @@ describe('Noble BLE process shutdown', () => {
940
945
  }
941
946
  );
942
947
 
948
+ test.each([
949
+ ['poweredOff', HardwareErrorCode.BlePoweredOff],
950
+ ['unauthorized', HardwareErrorCode.BleLocationError],
951
+ ['unsupported', HardwareErrorCode.BleUnsupported],
952
+ ])('checks adapter state %s again after initialization', async (state, errorCode) => {
953
+ const { sdk, native, handlers } = await setup();
954
+ const checkAvailability = handlers.get(EOneKeyBleMessageKeys.BLE_AVAILABILITY_CHECK);
955
+ await expect(checkAvailability?.({})).resolves.toMatchObject({ available: true });
956
+ native.state = state;
957
+ native.emit('stateChange', state);
958
+ for (const channel of [
959
+ EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE,
960
+ EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT,
961
+ ]) {
962
+ await expect(handlers.get(channel)?.({}, 'device')).resolves.toMatchObject({
963
+ success: false,
964
+ error: { errorCode },
965
+ });
966
+ }
967
+ expect(native.startScanning).not.toHaveBeenCalled();
968
+ native.state = 'poweredOn';
969
+ native.emit('stateChange', native.state);
970
+ await expect(checkAvailability?.({})).resolves.toMatchObject({ available: true });
971
+ await sdk.disposeNobleBleSupport();
972
+ });
973
+
943
974
  test('cancels an active scan, ignores its late callback and releases native once', async () => {
944
975
  jest.useFakeTimers({ doNotFake: ['performance'] });
945
976
  const { sdk, native, handlers } = await setup();
@@ -429,7 +429,18 @@ function updateBluetoothState(state: string): void {
429
429
  // Initialize Noble
430
430
  async function initializeNoble(): Promise<void> {
431
431
  assertBleActive();
432
- if (noble) return;
432
+ if (noble) {
433
+ if (noble.state === 'poweredOff') {
434
+ throw ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
435
+ }
436
+ if (noble.state === 'unsupported') {
437
+ throw ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
438
+ }
439
+ if (noble.state === 'unauthorized') {
440
+ throw ERRORS.TypedError(HardwareErrorCode.BleLocationError);
441
+ }
442
+ return;
443
+ }
433
444
 
434
445
  try {
435
446
  // eslint-disable-next-line @typescript-eslint/no-var-requires, global-require
@@ -1138,9 +1149,7 @@ async function performTargetedScan(
1138
1149
 
1139
1150
  // Enumerate devices
1140
1151
  async function enumerateDevices(isWindowDestroyed: () => boolean): Promise<DeviceInfo[]> {
1141
- if (!noble) {
1142
- await initializeNoble();
1143
- }
1152
+ await initializeNoble();
1144
1153
 
1145
1154
  if (!noble) {
1146
1155
  throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Noble not available');
@@ -1745,6 +1754,7 @@ async function tryDirectConnectById(deviceId: string): Promise<Peripheral | unde
1745
1754
 
1746
1755
  // Connect to device - supports both discovered and direct connection modes
1747
1756
  async function connectDevice(deviceId: string, webContents: WebContents): Promise<void> {
1757
+ await initializeNoble();
1748
1758
  logger?.info('[NobleBLE] Connect device request:', {
1749
1759
  deviceId,
1750
1760
  hasDiscovered: discoveredDevices.has(deviceId),
@@ -1762,11 +1772,6 @@ async function connectDevice(deviceId: string, webContents: WebContents): Promis
1762
1772
  }
1763
1773
 
1764
1774
  if (!peripheral) {
1765
- // Initialize Noble if not already done
1766
- if (!noble) {
1767
- await initializeNoble();
1768
- }
1769
-
1770
1775
  if (!noble) {
1771
1776
  throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Noble not available');
1772
1777
  }