@onekeyfe/hd-transport-electron 1.2.2-alpha.5 → 1.2.2-alpha.7
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.
- package/dist/ble-packet-capacity.d.ts +2 -0
- package/dist/ble-packet-capacity.d.ts.map +1 -1
- package/dist/{index-a518d449.js → index-00297050.js} +1 -1
- package/dist/index.js +1 -1
- package/dist/{noble-ble-handler-56d2eab3.js → noble-ble-handler-95c5cf0d.js} +18 -6
- package/dist/noble-ble-handler.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/ble-packet-capacity.test.ts +8 -1
- package/src/ble-packet-capacity.ts +14 -0
- package/src/noble-ble-handler.ts +9 -6
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare function resolveNobleAttMtu(reportedMtu: number | null | undefined, platform?: NodeJS.Platform): number | undefined;
|
|
1
3
|
export declare function resolveBlePacketCapacity(mtu: number | null | undefined, maximumPacketCapacity: number, fallbackPacketCapacity: number): number;
|
|
2
4
|
//# sourceMappingURL=ble-packet-capacity.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ble-packet-capacity.d.ts","sourceRoot":"","sources":["../src/ble-packet-capacity.ts"],"names":[],"mappings":"AAEA,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC9B,qBAAqB,EAAE,MAAM,EAC7B,sBAAsB,EAAE,MAAM,GAC7B,MAAM,CAMR"}
|
|
1
|
+
{"version":3,"file":"ble-packet-capacity.d.ts","sourceRoot":"","sources":["../src/ble-packet-capacity.ts"],"names":[],"mappings":";AAEA,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACtC,QAAQ,GAAE,MAAM,CAAC,QAA2B,GAC3C,MAAM,GAAG,SAAS,CASpB;AAED,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC9B,qBAAqB,EAAE,MAAM,EAC7B,sBAAsB,EAAE,MAAM,GAC7B,MAAM,CAMR"}
|
|
@@ -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-
|
|
35
|
+
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-95c5cf0d.js'); });
|
|
36
36
|
setupNobleBleHandlers(webContents);
|
|
37
37
|
});
|
|
38
38
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var index = require('./index-
|
|
3
|
+
var index = require('./index-00297050.js');
|
|
4
4
|
var hdShared = require('@onekeyfe/hd-shared');
|
|
5
5
|
var pRetry = require('p-retry');
|
|
6
6
|
|
|
@@ -9,6 +9,15 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
9
9
|
var pRetry__default = /*#__PURE__*/_interopDefaultLegacy(pRetry);
|
|
10
10
|
|
|
11
11
|
const BLE_ATT_HEADER_BYTES = 3;
|
|
12
|
+
function resolveNobleAttMtu(reportedMtu, platform = process.platform) {
|
|
13
|
+
if (typeof reportedMtu !== 'number' || !Number.isFinite(reportedMtu) || reportedMtu <= 0) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const normalizedMtu = Math.floor(reportedMtu);
|
|
17
|
+
return platform === 'darwin' || platform === 'win32'
|
|
18
|
+
? normalizedMtu + BLE_ATT_HEADER_BYTES
|
|
19
|
+
: normalizedMtu;
|
|
20
|
+
}
|
|
12
21
|
function resolveBlePacketCapacity(mtu, maximumPacketCapacity, fallbackPacketCapacity) {
|
|
13
22
|
if (typeof mtu !== 'number' || !Number.isFinite(mtu) || mtu <= BLE_ATT_HEADER_BYTES) {
|
|
14
23
|
return fallbackPacketCapacity;
|
|
@@ -441,13 +450,14 @@ function setupMtuListener(peripheral, deviceId, webContents) {
|
|
|
441
450
|
existing.peripheral.removeListener('mtu', existing.listener);
|
|
442
451
|
}
|
|
443
452
|
const listener = (mtu) => {
|
|
444
|
-
|
|
453
|
+
const normalizedMtu = resolveNobleAttMtu(mtu);
|
|
454
|
+
if (normalizedMtu === undefined)
|
|
445
455
|
return;
|
|
446
456
|
if (webContents.isDestroyed())
|
|
447
457
|
return;
|
|
448
458
|
webContents.send(hdShared.EOneKeyBleMessageKeys.NOBLE_BLE_MTU_CHANGED, {
|
|
449
459
|
id: deviceId,
|
|
450
|
-
mtu,
|
|
460
|
+
mtu: normalizedMtu,
|
|
451
461
|
});
|
|
452
462
|
};
|
|
453
463
|
deviceMtuListeners.set(deviceId, { peripheral, listener });
|
|
@@ -554,7 +564,7 @@ function transmitHexDataToDevice(deviceId, hexData, options) {
|
|
|
554
564
|
armIdleDisconnect(deviceId, BLE_BUSY_BACKSTOP_MS, 'busy-backstop');
|
|
555
565
|
const toBuffer = Buffer.from(hexData, 'hex');
|
|
556
566
|
const doGetWriteCharacteristic = () => { var _a; return (_a = deviceCharacteristics.get(deviceId)) === null || _a === void 0 ? void 0 : _a.write; };
|
|
557
|
-
const packetCapacity = resolveBlePacketCapacity(peripheral.mtu, BLE_PACKET_SIZE_MAXIMUM, BLE_PACKET_SIZE_FALLBACK);
|
|
567
|
+
const packetCapacity = resolveBlePacketCapacity(resolveNobleAttMtu(peripheral.mtu), BLE_PACKET_SIZE_MAXIMUM, BLE_PACKET_SIZE_FALLBACK);
|
|
558
568
|
const pacingDelayMs = resolveNobleBleWritePacingDelay(options);
|
|
559
569
|
if (!IS_WINDOWS || pairedDevices.has(deviceId)) {
|
|
560
570
|
const writeCharacteristic = doGetWriteCharacteristic();
|
|
@@ -791,12 +801,14 @@ function getDevice(deviceId) {
|
|
|
791
801
|
const peripheral = discoveredDevices.get(deviceId);
|
|
792
802
|
if (peripheral) {
|
|
793
803
|
const deviceName = ((_a = peripheral.advertisement) === null || _a === void 0 ? void 0 : _a.localName) || 'Unknown Device';
|
|
794
|
-
|
|
804
|
+
const mtu = resolveNobleAttMtu(peripheral.mtu);
|
|
805
|
+
return Object.assign({ commType: 'electron-ble', id: peripheral.id, name: deviceName, state: peripheral.state || 'disconnected' }, (mtu === undefined ? {} : { mtu }));
|
|
795
806
|
}
|
|
796
807
|
const connectedPeripheral = connectedDevices.get(deviceId);
|
|
797
808
|
if (connectedPeripheral) {
|
|
798
809
|
const deviceName = ((_b = connectedPeripheral.advertisement) === null || _b === void 0 ? void 0 : _b.localName) || 'Unknown Device';
|
|
799
|
-
|
|
810
|
+
const mtu = resolveNobleAttMtu(connectedPeripheral.mtu);
|
|
811
|
+
return Object.assign({ commType: 'electron-ble', id: connectedPeripheral.id, name: deviceName, state: connectedPeripheral.state || 'connected' }, (mtu === undefined ? {} : { mtu }));
|
|
800
812
|
}
|
|
801
813
|
return {
|
|
802
814
|
commType: 'electron-ble',
|
|
@@ -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;
|
|
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;AAyvDD,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.2-alpha.
|
|
3
|
+
"version": "1.2.2-alpha.7",
|
|
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.
|
|
29
|
-
"@onekeyfe/hd-shared": "1.2.2-alpha.
|
|
30
|
-
"@onekeyfe/hd-transport": "1.2.2-alpha.
|
|
28
|
+
"@onekeyfe/hd-core": "1.2.2-alpha.7",
|
|
29
|
+
"@onekeyfe/hd-shared": "1.2.2-alpha.7",
|
|
30
|
+
"@onekeyfe/hd-transport": "1.2.2-alpha.7",
|
|
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": "
|
|
39
|
+
"gitHead": "29b87f1c04fea60459d900ef1f61cb9f17eaa4d3"
|
|
40
40
|
}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import { resolveBlePacketCapacity } from '../ble-packet-capacity';
|
|
1
|
+
import { resolveBlePacketCapacity, resolveNobleAttMtu } from '../ble-packet-capacity';
|
|
2
2
|
|
|
3
3
|
describe('resolveBlePacketCapacity', () => {
|
|
4
|
+
test('normalizes Noble platform reports to ATT MTU', () => {
|
|
5
|
+
expect(resolveNobleAttMtu(244, 'darwin')).toBe(247);
|
|
6
|
+
expect(resolveNobleAttMtu(244, 'win32')).toBe(247);
|
|
7
|
+
expect(resolveNobleAttMtu(247, 'linux')).toBe(247);
|
|
8
|
+
expect(resolveNobleAttMtu(null, 'darwin')).toBeUndefined();
|
|
9
|
+
});
|
|
10
|
+
|
|
4
11
|
test('uses ATT MTU payload capacity with an upper bound', () => {
|
|
5
12
|
expect(resolveBlePacketCapacity(247, 244, 192)).toBe(244);
|
|
6
13
|
expect(resolveBlePacketCapacity(185, 244, 192)).toBe(182);
|
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
const BLE_ATT_HEADER_BYTES = 3;
|
|
2
2
|
|
|
3
|
+
export function resolveNobleAttMtu(
|
|
4
|
+
reportedMtu: number | null | undefined,
|
|
5
|
+
platform: NodeJS.Platform = process.platform
|
|
6
|
+
): number | undefined {
|
|
7
|
+
if (typeof reportedMtu !== 'number' || !Number.isFinite(reportedMtu) || reportedMtu <= 0) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const normalizedMtu = Math.floor(reportedMtu);
|
|
12
|
+
return platform === 'darwin' || platform === 'win32'
|
|
13
|
+
? normalizedMtu + BLE_ATT_HEADER_BYTES
|
|
14
|
+
: normalizedMtu;
|
|
15
|
+
}
|
|
16
|
+
|
|
3
17
|
export function resolveBlePacketCapacity(
|
|
4
18
|
mtu: number | null | undefined,
|
|
5
19
|
maximumPacketCapacity: number,
|
package/src/noble-ble-handler.ts
CHANGED
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
} from '@onekeyfe/hd-shared';
|
|
23
23
|
import pRetry from 'p-retry';
|
|
24
24
|
|
|
25
|
-
import { resolveBlePacketCapacity } from './ble-packet-capacity';
|
|
25
|
+
import { resolveBlePacketCapacity, resolveNobleAttMtu } from './ble-packet-capacity';
|
|
26
26
|
import { safeLog } from './types/noble-extended';
|
|
27
27
|
import { runBleCallbackOperation, softRefreshSubscription } from './ble-ops';
|
|
28
28
|
import {
|
|
@@ -574,12 +574,13 @@ function setupMtuListener(
|
|
|
574
574
|
}
|
|
575
575
|
|
|
576
576
|
const listener = (mtu: number) => {
|
|
577
|
-
|
|
577
|
+
const normalizedMtu = resolveNobleAttMtu(mtu);
|
|
578
|
+
if (normalizedMtu === undefined) return;
|
|
578
579
|
// A kept-alive link can outlive the renderer this closure captured.
|
|
579
580
|
if (webContents.isDestroyed()) return;
|
|
580
581
|
webContents.send(EOneKeyBleMessageKeys.NOBLE_BLE_MTU_CHANGED, {
|
|
581
582
|
id: deviceId,
|
|
582
|
-
mtu,
|
|
583
|
+
mtu: normalizedMtu,
|
|
583
584
|
});
|
|
584
585
|
};
|
|
585
586
|
deviceMtuListeners.set(deviceId, { peripheral, listener });
|
|
@@ -727,7 +728,7 @@ async function transmitHexDataToDevice(
|
|
|
727
728
|
const toBuffer = Buffer.from(hexData, 'hex');
|
|
728
729
|
const doGetWriteCharacteristic = () => deviceCharacteristics.get(deviceId)?.write;
|
|
729
730
|
const packetCapacity = resolveBlePacketCapacity(
|
|
730
|
-
peripheral.mtu,
|
|
731
|
+
resolveNobleAttMtu(peripheral.mtu),
|
|
731
732
|
BLE_PACKET_SIZE_MAXIMUM,
|
|
732
733
|
BLE_PACKET_SIZE_FALLBACK
|
|
733
734
|
);
|
|
@@ -1047,12 +1048,13 @@ function getDevice(deviceId: string): DeviceInfo | null {
|
|
|
1047
1048
|
const peripheral = discoveredDevices.get(deviceId);
|
|
1048
1049
|
if (peripheral) {
|
|
1049
1050
|
const deviceName = peripheral.advertisement?.localName || 'Unknown Device';
|
|
1051
|
+
const mtu = resolveNobleAttMtu(peripheral.mtu);
|
|
1050
1052
|
return {
|
|
1051
1053
|
commType: 'electron-ble',
|
|
1052
1054
|
id: peripheral.id,
|
|
1053
1055
|
name: deviceName,
|
|
1054
1056
|
state: peripheral.state || 'disconnected',
|
|
1055
|
-
...(
|
|
1057
|
+
...(mtu === undefined ? {} : { mtu }),
|
|
1056
1058
|
};
|
|
1057
1059
|
}
|
|
1058
1060
|
|
|
@@ -1060,12 +1062,13 @@ function getDevice(deviceId: string): DeviceInfo | null {
|
|
|
1060
1062
|
const connectedPeripheral = connectedDevices.get(deviceId);
|
|
1061
1063
|
if (connectedPeripheral) {
|
|
1062
1064
|
const deviceName = connectedPeripheral.advertisement?.localName || 'Unknown Device';
|
|
1065
|
+
const mtu = resolveNobleAttMtu(connectedPeripheral.mtu);
|
|
1063
1066
|
return {
|
|
1064
1067
|
commType: 'electron-ble',
|
|
1065
1068
|
id: connectedPeripheral.id,
|
|
1066
1069
|
name: deviceName,
|
|
1067
1070
|
state: connectedPeripheral.state || 'connected',
|
|
1068
|
-
...(
|
|
1071
|
+
...(mtu === undefined ? {} : { mtu }),
|
|
1069
1072
|
};
|
|
1070
1073
|
}
|
|
1071
1074
|
|