@onekeyfe/hd-transport-lowlevel 1.2.0-alpha.33 → 1.2.0-alpha.35
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/__tests__/protocol-v2.test.js +23 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -25
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +15 -26
- package/src/types.ts +1 -0
|
@@ -122,6 +122,29 @@ const splitFrame = (frame, index) => [
|
|
|
122
122
|
];
|
|
123
123
|
|
|
124
124
|
describe('LowlevelTransport protocol framing', () => {
|
|
125
|
+
test('falls back to Protocol V2 when a cached V1 hint is stale', async () => {
|
|
126
|
+
const plugin = createPlugin({ devices: [], responses: [] });
|
|
127
|
+
const lowlevel = configureTransport(plugin);
|
|
128
|
+
const events = [];
|
|
129
|
+
lowlevel.probeProtocolV1 = jest.fn().mockImplementation(() => {
|
|
130
|
+
events.push('probe-v1');
|
|
131
|
+
return Promise.resolve(false);
|
|
132
|
+
});
|
|
133
|
+
lowlevel.resetConnectionAfterProbe = jest.fn().mockImplementation(() => {
|
|
134
|
+
events.push('reset');
|
|
135
|
+
return Promise.resolve();
|
|
136
|
+
});
|
|
137
|
+
lowlevel.probeProtocolV2 = jest.fn().mockImplementation(() => {
|
|
138
|
+
events.push('probe-v2');
|
|
139
|
+
return Promise.resolve(true);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
await expect(lowlevel.detectProtocol('pro-lowlevel', undefined, 'V1')).resolves.toBe('V2');
|
|
143
|
+
|
|
144
|
+
expect(events).toEqual(['probe-v1', 'reset', 'probe-v2']);
|
|
145
|
+
expect(lowlevel.getProtocolType('pro-lowlevel')).toBe('V2');
|
|
146
|
+
});
|
|
147
|
+
|
|
125
148
|
test('keeps active links when the Protocol V2 schema is configured repeatedly', () => {
|
|
126
149
|
const lowlevel = new LowlevelTransport();
|
|
127
150
|
const invalidateAllLinks = jest.fn().mockResolvedValue(undefined);
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,SAYN,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,cAAc,EACd,6BAA6B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,SAYN,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,cAAc,EACd,6BAA6B,EAC7B,YAAY,EAEZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAUpD,wBAAgB,+BAA+B,CAAC,EAC9C,OAAO,EACP,iBAAiB,EACjB,GAAG,EACH,YAAY,GACb,EAAE;IACD,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,MAAM,CAAC;CACtB,WAMA;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM;;cAEpD;AAcD,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAEnE,WAAW,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAErE,UAAU,UAAS;IAEnB,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;IAEvB,MAAM,EAAE,6BAA6B,CAAuC;IAE5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void 0, function* () {
|
|
150
150
|
const alreadyConnected = this.connectedDevices.has(input.uuid);
|
|
151
151
|
try {
|
|
@@ -163,7 +163,7 @@ class LowlevelTransport {
|
|
|
163
163
|
this.protocolV2Assemblers.set(input.uuid, new transport.ProtocolV2FrameAssembler(transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
|
|
164
164
|
const protocolHint = input.expectedProtocol
|
|
165
165
|
? undefined
|
|
166
|
-
: this.deviceProtocolHints.get(input.uuid);
|
|
166
|
+
: (_b = input.protocolHint) !== null && _b !== void 0 ? _b : this.deviceProtocolHints.get(input.uuid);
|
|
167
167
|
const protocolType = yield this.detectProtocol(input.uuid, input.expectedProtocol, protocolHint);
|
|
168
168
|
return { uuid: input.uuid, protocolType };
|
|
169
169
|
});
|
|
@@ -283,7 +283,7 @@ class LowlevelTransport {
|
|
|
283
283
|
}
|
|
284
284
|
}
|
|
285
285
|
detectProtocol(uuid, expectedProtocol, protocolHint) {
|
|
286
|
-
var _a, _b, _c
|
|
286
|
+
var _a, _b, _c;
|
|
287
287
|
return __awaiter(this, void 0, void 0, function* () {
|
|
288
288
|
if (expectedProtocol === 'V2') {
|
|
289
289
|
if (yield this.probeProtocolV2(uuid)) {
|
|
@@ -301,28 +301,17 @@ class LowlevelTransport {
|
|
|
301
301
|
}
|
|
302
302
|
throw this.createProtocolMismatchError(expectedProtocol);
|
|
303
303
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
(
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
const protocolV1Detected = yield this.probeProtocolV1(uuid);
|
|
316
|
-
if (protocolV1Detected) {
|
|
317
|
-
this.deviceProtocol.set(uuid, 'V1');
|
|
318
|
-
(_e = this.Log) === null || _e === void 0 ? void 0 : _e.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V1`);
|
|
319
|
-
return 'V1';
|
|
320
|
-
}
|
|
321
|
-
yield this.resetConnectionAfterProbe(uuid, 'V1');
|
|
322
|
-
if (yield this.probeProtocolV2(uuid)) {
|
|
323
|
-
this.deviceProtocol.set(uuid, 'V2');
|
|
324
|
-
(_f = this.Log) === null || _f === void 0 ? void 0 : _f.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V2`);
|
|
325
|
-
return 'V2';
|
|
304
|
+
const probeOrder = protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
305
|
+
for (const [index, protocol] of probeOrder.entries()) {
|
|
306
|
+
if (index > 0) {
|
|
307
|
+
yield this.resetConnectionAfterProbe(uuid, probeOrder[index - 1]);
|
|
308
|
+
}
|
|
309
|
+
const detected = protocol === 'V1' ? yield this.probeProtocolV1(uuid) : yield this.probeProtocolV2(uuid);
|
|
310
|
+
if (detected) {
|
|
311
|
+
this.deviceProtocol.set(uuid, protocol);
|
|
312
|
+
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
313
|
+
return protocol;
|
|
314
|
+
}
|
|
326
315
|
}
|
|
327
316
|
this.deviceProtocol.delete(uuid);
|
|
328
317
|
throw this.createProtocolDetectionError();
|
package/dist/types.d.ts
CHANGED
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,gBAAgB,CAAC,EAAE,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,gBAAgB,CAAC,EAAE,YAAY,CAAC;IAChC,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-lowlevel",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.35",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"lint:fix": "eslint . --fix"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-shared": "1.2.0-alpha.
|
|
24
|
-
"@onekeyfe/hd-transport": "1.2.0-alpha.
|
|
23
|
+
"@onekeyfe/hd-shared": "1.2.0-alpha.35",
|
|
24
|
+
"@onekeyfe/hd-transport": "1.2.0-alpha.35"
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "638bd33030686c4a3321e4feb4924c3f9f5c81a8"
|
|
27
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -17,8 +17,8 @@ import type EventEmitter from 'events';
|
|
|
17
17
|
import type {
|
|
18
18
|
LowLevelDevice,
|
|
19
19
|
LowlevelTransportSharedPlugin,
|
|
20
|
-
ProtocolV2CallContext,
|
|
21
20
|
ProtocolType,
|
|
21
|
+
ProtocolV2CallContext,
|
|
22
22
|
TransportCallOptions,
|
|
23
23
|
} from '@onekeyfe/hd-transport';
|
|
24
24
|
import type { LowLevelAcquireInput } from './types';
|
|
@@ -193,7 +193,7 @@ export default class LowlevelTransport {
|
|
|
193
193
|
);
|
|
194
194
|
const protocolHint = input.expectedProtocol
|
|
195
195
|
? undefined
|
|
196
|
-
: this.deviceProtocolHints.get(input.uuid);
|
|
196
|
+
: input.protocolHint ?? this.deviceProtocolHints.get(input.uuid);
|
|
197
197
|
const protocolType = await this.detectProtocol(
|
|
198
198
|
input.uuid,
|
|
199
199
|
input.expectedProtocol,
|
|
@@ -371,31 +371,20 @@ export default class LowlevelTransport {
|
|
|
371
371
|
throw this.createProtocolMismatchError(expectedProtocol);
|
|
372
372
|
}
|
|
373
373
|
|
|
374
|
-
|
|
375
|
-
this.deviceProtocol.
|
|
376
|
-
this.Log?.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V2 (hint)`);
|
|
377
|
-
return 'V2';
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
const cachedProtocol = this.deviceProtocol.get(uuid);
|
|
381
|
-
if (cachedProtocol === 'V2' && (await this.probeProtocolV2(uuid))) {
|
|
382
|
-
this.deviceProtocol.set(uuid, 'V2');
|
|
383
|
-
this.Log?.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V2 (cached)`);
|
|
384
|
-
return 'V2';
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
const protocolV1Detected = await this.probeProtocolV1(uuid);
|
|
388
|
-
if (protocolV1Detected) {
|
|
389
|
-
this.deviceProtocol.set(uuid, 'V1');
|
|
390
|
-
this.Log?.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> V1`);
|
|
391
|
-
return 'V1';
|
|
392
|
-
}
|
|
374
|
+
const probeOrder: ProtocolType[] =
|
|
375
|
+
protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
393
376
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
377
|
+
for (const [index, protocol] of probeOrder.entries()) {
|
|
378
|
+
if (index > 0) {
|
|
379
|
+
await this.resetConnectionAfterProbe(uuid, probeOrder[index - 1]);
|
|
380
|
+
}
|
|
381
|
+
const detected =
|
|
382
|
+
protocol === 'V1' ? await this.probeProtocolV1(uuid) : await this.probeProtocolV2(uuid);
|
|
383
|
+
if (detected) {
|
|
384
|
+
this.deviceProtocol.set(uuid, protocol);
|
|
385
|
+
this.Log?.debug(`[LowlevelTransport] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
386
|
+
return protocol;
|
|
387
|
+
}
|
|
399
388
|
}
|
|
400
389
|
|
|
401
390
|
this.deviceProtocol.delete(uuid);
|