@onekeyfe/hd-transport-web-device 1.2.0-alpha.27 → 1.2.0-alpha.29

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.
@@ -183,6 +183,24 @@ describe('ElectronBleTransport protocol detection', () => {
183
183
  });
184
184
  });
185
185
 
186
+ test('uses the Protocol V2 BLE writer without changing the Protocol V1 packet path', async () => {
187
+ const device = { id: 'chunked-pro2-id', name: 'OneKey Pro 2' };
188
+ const nobleBle = createNobleBle(device);
189
+ const bleTransport = configureTransport(nobleBle) as any;
190
+ const context = {
191
+ messageName: 'Ping',
192
+ timeoutMs: 1000,
193
+ highVolume: false,
194
+ generation: 1,
195
+ signal: new AbortController().signal,
196
+ };
197
+
198
+ await bleTransport.writeProtocolV2Frame(device.id, new Uint8Array(193), context, jest.fn());
199
+
200
+ expect(nobleBle.write).toHaveBeenCalledTimes(2);
201
+ expect(nobleBle.write.mock.calls.map(([, hex]) => hex.length / 2)).toEqual([192, 1]);
202
+ });
203
+
186
204
  test('detects Protocol V2 after Protocol V1 probe timeout', async () => {
187
205
  const device = { id: 'unknown-pro2-id', name: 'Unknown BLE Device' };
188
206
  const nobleBle = createNobleBle(device);
@@ -61,8 +61,8 @@ export default class ElectronBleTransport {
61
61
  private resetProbeStateAfterProtocolProbe;
62
62
  private probeProtocolV1;
63
63
  private probeProtocolV2;
64
- private writeWithChunking;
65
64
  private writeOnce;
65
+ private writeProtocolV2Frame;
66
66
  private handleNotification;
67
67
  private handleProtocolV2Notification;
68
68
  private getProtocolV2FrameQueue;
@@ -1 +1 @@
1
- {"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";AAsBA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,E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AgB,EAAE,CAAC;IAqBxC,OAAO,CAAC,KAAK,EAAE,eAAe;;;;;;;;;;;IAoF9B,OAAO,CAAC,EAAE,EAAE,MAAM;IAgBxB,OAAO,CAAC,2BAA2B;IAOnC,OAAO,CAAC,4BAA4B;IAOpC,OAAO,CAAC,kBAAkB;YAMZ,cAAc;IA+C5B,OAAO,CAAC,8BAA8B;YAgBxB,iCAAiC;YAqCjC,eAAe;YAgBf,eAAe;YAuBf,SAAS;IASvB,OAAO,CAAC,oBAAoB;IAmB5B,OAAO,CAAC,kBAAkB;IAwB1B,OAAO,CAAC,4BAA4B;IAkBpC,OAAO,CAAC,uBAAuB;IAS/B,OAAO,CAAC,sBAAsB;IAU9B,OAAO,CAAC,yBAAyB;IAQjC,OAAO,CAAC,qBAAqB;IAK7B,OAAO,CAAC,sBAAsB;YAShB,mBAAmB;IAiBjC,OAAO,CAAC,4BAA4B;IAgB9B,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;YA2BlB,cAAc;YAuEd,cAAc;IA0B5B,OAAO,CAAC,uBAAuB;IAyC/B,OAAO,CAAC,6BAA6B;IAsCrC,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
package/dist/index.d.ts CHANGED
@@ -219,8 +219,8 @@ declare class ElectronBleTransport {
219
219
  private resetProbeStateAfterProtocolProbe;
220
220
  private probeProtocolV1;
221
221
  private probeProtocolV2;
222
- private writeWithChunking;
223
222
  private writeOnce;
223
+ private writeProtocolV2Frame;
224
224
  private handleNotification;
225
225
  private handleProtocolV2Notification;
226
226
  private getProtocolV2FrameQueue;
package/dist/index.js CHANGED
@@ -939,7 +939,7 @@ class ElectronBleTransport {
939
939
  this.handleBluetoothError(error);
940
940
  }
941
941
  this.v1Buffers.set(uuid, { buffer: [], bufferLength: 0 });
942
- this.v2Assemblers.set(uuid, new transport.ProtocolV2FrameAssembler());
942
+ this.v2Assemblers.set(uuid, new transport.ProtocolV2FrameAssembler(transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
943
943
  yield window.desktopApi.nobleBle.subscribe(uuid);
944
944
  const cleanup = this.createNotificationSubscription(uuid);
945
945
  this.notificationCleanups.set(uuid, cleanup);
@@ -1132,25 +1132,6 @@ class ElectronBleTransport {
1132
1132
  return detected;
1133
1133
  });
1134
1134
  }
1135
- writeWithChunking(uuid, hexData) {
1136
- return __awaiter(this, void 0, void 0, function* () {
1137
- const totalBytes = hexData.length / 2;
1138
- if (totalBytes <= BLE_PACKET_SIZE) {
1139
- yield hdShared.wait(BLE_WRITE_DELAY_MS);
1140
- yield this.writeOnce(uuid, hexData);
1141
- return;
1142
- }
1143
- for (let offset = 0; offset < hexData.length;) {
1144
- const chunkHexLen = Math.min(BLE_PACKET_SIZE * 2, hexData.length - offset);
1145
- const chunkHex = hexData.substring(offset, offset + chunkHexLen);
1146
- offset += chunkHexLen;
1147
- yield this.writeOnce(uuid, chunkHex);
1148
- if (offset < hexData.length) {
1149
- yield hdShared.wait(BLE_WRITE_DELAY_MS);
1150
- }
1151
- }
1152
- });
1153
- }
1154
1135
  writeOnce(uuid, hexData) {
1155
1136
  var _a;
1156
1137
  return __awaiter(this, void 0, void 0, function* () {
@@ -1161,6 +1142,19 @@ class ElectronBleTransport {
1161
1142
  yield nobleBle.write(uuid, hexData);
1162
1143
  });
1163
1144
  }
1145
+ writeProtocolV2Frame(uuid, frame, context, assertCurrentGeneration) {
1146
+ return transport.writeProtocolV2BleFrame({
1147
+ frame,
1148
+ packetCapacity: BLE_PACKET_SIZE,
1149
+ assertActive: assertCurrentGeneration,
1150
+ signal: context.signal,
1151
+ abortMessage: `Protocol V2 BLE write aborted for ${context.messageName}`,
1152
+ initialDelayMs: frame.length <= BLE_PACKET_SIZE ? BLE_WRITE_DELAY_MS : 0,
1153
+ burstSize: 1,
1154
+ burstPauseMs: BLE_WRITE_DELAY_MS,
1155
+ writePacket: packet => this.writeOnce(uuid, transport.bytesToHex(packet)),
1156
+ });
1157
+ }
1164
1158
  handleNotification(deviceId, hexData) {
1165
1159
  var _a, _b;
1166
1160
  if (hexData === 'PAIRING_REJECTED') {
@@ -1390,10 +1384,7 @@ class ElectronBleTransport {
1390
1384
  (_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
1391
1385
  this.resetProtocolV2Frames(uuid);
1392
1386
  },
1393
- writeFrame: (frame) => {
1394
- assertCurrentGeneration();
1395
- return this.writeWithChunking(uuid, transport.bytesToHex(frame));
1396
- },
1387
+ writeFrame: (frame, context) => this.writeProtocolV2Frame(uuid, frame, context, assertCurrentGeneration),
1397
1388
  readFrame: () => __awaiter(this, void 0, void 0, function* () {
1398
1389
  assertCurrentGeneration();
1399
1390
  const rxFrame = yield this.readProtocolV2Frame(uuid);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-web-device",
3
- "version": "1.2.0-alpha.27",
3
+ "version": "1.2.0-alpha.29",
4
4
  "author": "OneKey",
5
5
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
6
6
  "license": "MIT",
@@ -21,13 +21,13 @@
21
21
  "lint:fix": "eslint . --fix"
22
22
  },
23
23
  "dependencies": {
24
- "@onekeyfe/hd-shared": "1.2.0-alpha.27",
25
- "@onekeyfe/hd-transport": "1.2.0-alpha.27"
24
+ "@onekeyfe/hd-shared": "1.2.0-alpha.29",
25
+ "@onekeyfe/hd-transport": "1.2.0-alpha.29"
26
26
  },
27
27
  "devDependencies": {
28
- "@onekeyfe/hd-transport-electron": "1.2.0-alpha.27",
28
+ "@onekeyfe/hd-transport-electron": "1.2.0-alpha.29",
29
29
  "@types/w3c-web-usb": "^1.0.6",
30
30
  "@types/web-bluetooth": "^0.0.17"
31
31
  },
32
- "gitHead": "ea3c188364a6fb968875484805085e4466d3ef99"
32
+ "gitHead": "4a4019e439cc472f89fd15ab47e84ef0ed43166a"
33
33
  }
@@ -8,6 +8,7 @@ import transport, {
8
8
  bytesToHex,
9
9
  hexToBytes,
10
10
  probeProtocolV2 as probeProtocolV2Helper,
11
+ writeProtocolV2BleFrame,
11
12
  } from '@onekeyfe/hd-transport';
12
13
  import {
13
14
  ERRORS,
@@ -15,14 +16,18 @@ import {
15
16
  HardwareErrorCodeMessage,
16
17
  createDeferred,
17
18
  isHeaderChunk,
18
- wait,
19
19
  } from '@onekeyfe/hd-shared';
20
20
 
21
21
  import { createTransportCallLog, shouldSuppressHighVolumeCallLog } from './transportLog';
22
22
 
23
23
  import type { Deferred } from '@onekeyfe/hd-shared';
24
24
  import type { DesktopAPI } from '@onekeyfe/hd-transport-electron';
25
- import type { OneKeyDeviceInfo, ProtocolType, TransportCallOptions } from '@onekeyfe/hd-transport';
25
+ import type {
26
+ OneKeyDeviceInfo,
27
+ ProtocolType,
28
+ ProtocolV2CallContext,
29
+ TransportCallOptions,
30
+ } from '@onekeyfe/hd-transport';
26
31
  import type EventEmitter from 'events';
27
32
 
28
33
  const { parseConfigure, ProtocolV1, check } = transport;
@@ -292,7 +297,7 @@ export default class ElectronBleTransport {
292
297
  }
293
298
 
294
299
  this.v1Buffers.set(uuid, { buffer: [], bufferLength: 0 });
295
- this.v2Assemblers.set(uuid, new ProtocolV2FrameAssembler());
300
+ this.v2Assemblers.set(uuid, new ProtocolV2FrameAssembler(PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
296
301
 
297
302
  await window.desktopApi.nobleBle.subscribe(uuid);
298
303
 
@@ -509,28 +514,6 @@ export default class ElectronBleTransport {
509
514
  return detected;
510
515
  }
511
516
 
512
- private async writeWithChunking(uuid: string, hexData: string): Promise<void> {
513
- const totalBytes = hexData.length / 2;
514
-
515
- if (totalBytes <= BLE_PACKET_SIZE) {
516
- await wait(BLE_WRITE_DELAY_MS);
517
- await this.writeOnce(uuid, hexData);
518
- return;
519
- }
520
-
521
- for (let offset = 0; offset < hexData.length; ) {
522
- const chunkHexLen = Math.min(BLE_PACKET_SIZE * 2, hexData.length - offset);
523
- const chunkHex = hexData.substring(offset, offset + chunkHexLen);
524
- offset += chunkHexLen;
525
-
526
- await this.writeOnce(uuid, chunkHex);
527
-
528
- if (offset < hexData.length) {
529
- await wait(BLE_WRITE_DELAY_MS);
530
- }
531
- }
532
- }
533
-
534
517
  private async writeOnce(uuid: string, hexData: string): Promise<void> {
535
518
  const nobleBle = window.desktopApi?.nobleBle;
536
519
  if (!nobleBle) {
@@ -540,6 +523,25 @@ export default class ElectronBleTransport {
540
523
  await nobleBle.write(uuid, hexData);
541
524
  }
542
525
 
526
+ private writeProtocolV2Frame(
527
+ uuid: string,
528
+ frame: Uint8Array,
529
+ context: ProtocolV2CallContext,
530
+ assertCurrentGeneration: () => void
531
+ ) {
532
+ return writeProtocolV2BleFrame({
533
+ frame,
534
+ packetCapacity: BLE_PACKET_SIZE,
535
+ assertActive: assertCurrentGeneration,
536
+ signal: context.signal,
537
+ abortMessage: `Protocol V2 BLE write aborted for ${context.messageName}`,
538
+ initialDelayMs: frame.length <= BLE_PACKET_SIZE ? BLE_WRITE_DELAY_MS : 0,
539
+ burstSize: 1,
540
+ burstPauseMs: BLE_WRITE_DELAY_MS,
541
+ writePacket: packet => this.writeOnce(uuid, bytesToHex(packet)),
542
+ });
543
+ }
544
+
543
545
  private handleNotification(deviceId: string, hexData: string): void {
544
546
  if (hexData === 'PAIRING_REJECTED') {
545
547
  this.Log?.debug('[Electron BLE] Pairing rejection detected for device:', deviceId);
@@ -801,10 +803,8 @@ export default class ElectronBleTransport {
801
803
  this.v2Assemblers.get(uuid)?.reset();
802
804
  this.resetProtocolV2Frames(uuid);
803
805
  },
804
- writeFrame: (frame: Uint8Array) => {
805
- assertCurrentGeneration();
806
- return this.writeWithChunking(uuid, bytesToHex(frame));
807
- },
806
+ writeFrame: (frame: Uint8Array, context: ProtocolV2CallContext) =>
807
+ this.writeProtocolV2Frame(uuid, frame, context, assertCurrentGeneration),
808
808
  readFrame: async () => {
809
809
  assertCurrentGeneration();
810
810
  const rxFrame = await this.readProtocolV2Frame(uuid);