@onekeyfe/hd-transport 1.2.8-alpha.113 → 1.2.8-alpha.114
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/constants.d.ts +0 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +7 -42
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38 -186
- package/dist/protocols/v2/session.d.ts +1 -4
- package/dist/protocols/v2/session.d.ts.map +1 -1
- package/dist/types/transport.d.ts +1 -5
- package/dist/types/transport.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/constants.ts +0 -7
- package/src/index.ts +0 -1
- package/src/protocols/v2/session.ts +63 -300
- package/src/types/transport.ts +6 -19
- package/__tests__/protocol-v2-file-write-pipeline.test.js +0 -417
|
@@ -1,417 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
-
const { ProtocolV2, ProtocolV2LinkManager } = require('../src');
|
|
3
|
-
const { parseConfigure } = require('../src/serialization/protobuf/messages');
|
|
4
|
-
const { ProtocolV2Session } = require('../src/protocols/v2/session');
|
|
5
|
-
const protocolV2 = require('../src/protocols/v2');
|
|
6
|
-
const { PROTOCOL_V2_FILE_WRITE_PIPELINE_WINDOW } = require('../src/constants');
|
|
7
|
-
|
|
8
|
-
const schemas = {
|
|
9
|
-
protocolV1: parseConfigure({
|
|
10
|
-
nested: {
|
|
11
|
-
Failure: { fields: { code: { type: 'uint32', id: 1 }, message: { type: 'string', id: 2 } } },
|
|
12
|
-
MessageType: { values: { MessageType_Failure: 3 } },
|
|
13
|
-
},
|
|
14
|
-
}),
|
|
15
|
-
protocolV2: parseConfigure({
|
|
16
|
-
nested: {
|
|
17
|
-
Failure: {
|
|
18
|
-
fields: {
|
|
19
|
-
code: { type: 'uint32', id: 1 },
|
|
20
|
-
subcode: { type: 'uint32', id: 2 },
|
|
21
|
-
message: { type: 'string', id: 3 },
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
ButtonRequest: { fields: { code: { type: 'uint32', id: 1 } } },
|
|
25
|
-
Success: { fields: { message: { type: 'string', id: 1 } } },
|
|
26
|
-
FilesystemFile: {
|
|
27
|
-
fields: {
|
|
28
|
-
path: { type: 'string', id: 1 },
|
|
29
|
-
offset: { type: 'uint32', id: 2 },
|
|
30
|
-
total_size: { type: 'uint32', id: 3 },
|
|
31
|
-
data: { type: 'bytes', id: 4 },
|
|
32
|
-
processed_byte: { type: 'uint32', id: 6 },
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
FilesystemFileWrite: {
|
|
36
|
-
fields: {
|
|
37
|
-
file: { type: 'FilesystemFile', id: 1 },
|
|
38
|
-
overwrite: { type: 'bool', id: 2 },
|
|
39
|
-
append: { type: 'bool', id: 3 },
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
MessageType: {
|
|
43
|
-
values: {
|
|
44
|
-
MessageType_Success: 60207,
|
|
45
|
-
MessageType_Failure: 60208,
|
|
46
|
-
MessageType_ButtonRequest: 60209,
|
|
47
|
-
MessageType_FilesystemFile: 60803,
|
|
48
|
-
MessageType_FilesystemFileWrite: 60805,
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
}),
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const PATH = 'vol0:/application_p1.bin';
|
|
56
|
-
|
|
57
|
-
const chunk = (offset, length, totalSize) => ({
|
|
58
|
-
file: { path: PATH, offset, total_size: totalSize, data: new Uint8Array(length) },
|
|
59
|
-
overwrite: offset === 0,
|
|
60
|
-
append: false,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const chunksOf = (totalSize, chunkSize) => {
|
|
64
|
-
const chunks = [];
|
|
65
|
-
for (let offset = 0; offset < totalSize; offset += chunkSize) {
|
|
66
|
-
chunks.push(chunk(offset, Math.min(chunkSize, totalSize - offset), totalSize));
|
|
67
|
-
}
|
|
68
|
-
return chunks;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const ackFrame = seq => {
|
|
72
|
-
const ack = new Uint8Array(8);
|
|
73
|
-
ack[0] = 0x5a;
|
|
74
|
-
ack[1] = 8;
|
|
75
|
-
ack[4] = 1;
|
|
76
|
-
ack[5] = 1;
|
|
77
|
-
ack[6] = seq;
|
|
78
|
-
ack[3] = protocolV2.crc8(ack, 3);
|
|
79
|
-
ack[7] = protocolV2.crc8(ack, 7);
|
|
80
|
-
return ack;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Simulates the bootloader: every written FileWrite frame is answered in order. `reply` may
|
|
85
|
-
* replace or prepend frames for a given request index.
|
|
86
|
-
*/
|
|
87
|
-
const createDevice = ({ reply } = {}) => {
|
|
88
|
-
let responseSeq = 100;
|
|
89
|
-
const queue = [];
|
|
90
|
-
const waiters = [];
|
|
91
|
-
const requests = [];
|
|
92
|
-
let maxInFlight = 0;
|
|
93
|
-
let answered = 0;
|
|
94
|
-
const push = frame => {
|
|
95
|
-
const waiter = waiters.shift();
|
|
96
|
-
if (waiter) waiter(frame);
|
|
97
|
-
else queue.push(frame);
|
|
98
|
-
};
|
|
99
|
-
const encode = (name, message) => {
|
|
100
|
-
responseSeq += 1;
|
|
101
|
-
return ProtocolV2.encodeFrame(schemas, name, message, {
|
|
102
|
-
router: 1,
|
|
103
|
-
packetSrc: 0,
|
|
104
|
-
seq: responseSeq,
|
|
105
|
-
});
|
|
106
|
-
};
|
|
107
|
-
const respond = () => {
|
|
108
|
-
const index = answered;
|
|
109
|
-
answered += 1;
|
|
110
|
-
const request = requests[index];
|
|
111
|
-
const { offset, total_size: totalSize, data } = request.message.file;
|
|
112
|
-
// Decoded protobuf bytes are hex strings.
|
|
113
|
-
const length = Number(typeof data === 'string' ? data.length / 2 : data.length);
|
|
114
|
-
const confirmation = encode('FilesystemFile', {
|
|
115
|
-
path: PATH,
|
|
116
|
-
offset,
|
|
117
|
-
total_size: totalSize,
|
|
118
|
-
processed_byte: Number(offset) + length,
|
|
119
|
-
});
|
|
120
|
-
const frames = reply ? reply({ index, request, confirmation, encode }) : [confirmation];
|
|
121
|
-
frames.forEach(push);
|
|
122
|
-
};
|
|
123
|
-
return {
|
|
124
|
-
requests,
|
|
125
|
-
get maxInFlight() {
|
|
126
|
-
return maxInFlight;
|
|
127
|
-
},
|
|
128
|
-
writeFrame: jest.fn(frame => {
|
|
129
|
-
const decoded = ProtocolV2.decodeFrame(schemas, frame);
|
|
130
|
-
requests.push({ seq: frame[6], message: decoded.message });
|
|
131
|
-
maxInFlight = Math.max(maxInFlight, requests.length - answered);
|
|
132
|
-
return Promise.resolve();
|
|
133
|
-
}),
|
|
134
|
-
inject: (name, message) => push(encode(name, message)),
|
|
135
|
-
readFrame: jest.fn(() => {
|
|
136
|
-
while (!queue.length && answered < requests.length) {
|
|
137
|
-
respond();
|
|
138
|
-
}
|
|
139
|
-
const frame = queue.shift();
|
|
140
|
-
if (frame) return Promise.resolve(frame);
|
|
141
|
-
return new Promise(resolve => {
|
|
142
|
-
waiters.push(resolve);
|
|
143
|
-
});
|
|
144
|
-
}),
|
|
145
|
-
};
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const createSession = (device, options = {}) =>
|
|
149
|
-
new ProtocolV2Session({
|
|
150
|
-
schemas,
|
|
151
|
-
router: 1,
|
|
152
|
-
writeFrame: device.writeFrame,
|
|
153
|
-
readFrame: device.readFrame,
|
|
154
|
-
...options,
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
const runPipeline = (session, chunks, callOptions = {}) => {
|
|
158
|
-
const [first, ...rest] = chunks;
|
|
159
|
-
const next = jest.fn(() => Promise.resolve(rest.shift()));
|
|
160
|
-
const onResponse = jest.fn();
|
|
161
|
-
return {
|
|
162
|
-
next,
|
|
163
|
-
onResponse,
|
|
164
|
-
result: session.call('FilesystemFileWrite', first, {
|
|
165
|
-
...callOptions,
|
|
166
|
-
fileWritePipeline: { next, onResponse },
|
|
167
|
-
}),
|
|
168
|
-
};
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
describe('Protocol V2 file write pipeline', () => {
|
|
172
|
-
test('keeps two ordered chunks in flight and confirms each one', async () => {
|
|
173
|
-
const device = createDevice();
|
|
174
|
-
const session = createSession(device);
|
|
175
|
-
const { result, onResponse } = runPipeline(session, chunksOf(10, 2));
|
|
176
|
-
|
|
177
|
-
await expect(result).resolves.toMatchObject({
|
|
178
|
-
type: 'FilesystemFile',
|
|
179
|
-
message: { processed_byte: 10 },
|
|
180
|
-
});
|
|
181
|
-
expect(PROTOCOL_V2_FILE_WRITE_PIPELINE_WINDOW).toBe(2);
|
|
182
|
-
expect(device.maxInFlight).toBe(2);
|
|
183
|
-
expect(device.requests.map(request => request.message.file.offset)).toEqual([0, 2, 4, 6, 8]);
|
|
184
|
-
expect(onResponse.mock.calls.map(([response]) => response.message.processed_byte)).toEqual([
|
|
185
|
-
2, 4, 6, 8, 10,
|
|
186
|
-
]);
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
test('sends a single-chunk file without asking for more than the source has', async () => {
|
|
190
|
-
const device = createDevice();
|
|
191
|
-
const session = createSession(device);
|
|
192
|
-
const { result, next } = runPipeline(session, chunksOf(3, 4));
|
|
193
|
-
|
|
194
|
-
await expect(result).resolves.toMatchObject({ message: { processed_byte: 3 } });
|
|
195
|
-
expect(device.requests).toHaveLength(1);
|
|
196
|
-
expect(next).toHaveBeenCalledTimes(1);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
test('skips an ACK of an in-flight chunk', async () => {
|
|
200
|
-
const device = createDevice({
|
|
201
|
-
reply: ({ index, request, confirmation }) =>
|
|
202
|
-
index === 0 ? [ackFrame(request.seq), confirmation] : [confirmation],
|
|
203
|
-
});
|
|
204
|
-
const session = createSession(device);
|
|
205
|
-
const { result } = runPipeline(session, chunksOf(4, 2));
|
|
206
|
-
|
|
207
|
-
await expect(result).resolves.toMatchObject({ message: { processed_byte: 4 } });
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
test('drains in-flight chunks after a device Failure and returns it', async () => {
|
|
211
|
-
const device = createDevice({
|
|
212
|
-
reply: ({ index, confirmation, encode }) =>
|
|
213
|
-
index >= 1
|
|
214
|
-
? [encode('Failure', { code: 9, message: 'Write offset mismatch' })]
|
|
215
|
-
: [confirmation],
|
|
216
|
-
});
|
|
217
|
-
const session = createSession(device);
|
|
218
|
-
const { result, next, onResponse } = runPipeline(session, chunksOf(10, 2));
|
|
219
|
-
|
|
220
|
-
await expect(result).resolves.toEqual({
|
|
221
|
-
type: 'Failure',
|
|
222
|
-
message: expect.objectContaining({ message: 'Write offset mismatch' }),
|
|
223
|
-
});
|
|
224
|
-
// Chunk 0 confirmed, chunk 1 failed; chunk 2 was already in flight and is drained.
|
|
225
|
-
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
226
|
-
expect(device.requests).toHaveLength(3);
|
|
227
|
-
expect(device.readFrame).toHaveBeenCalledTimes(3);
|
|
228
|
-
expect(next).toHaveBeenCalledTimes(2);
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
test('keeps the link after a device Failure', async () => {
|
|
232
|
-
const device = createDevice({
|
|
233
|
-
reply: ({ encode }) => [encode('Failure', { code: 9, message: 'Write offset mismatch' })],
|
|
234
|
-
});
|
|
235
|
-
const adapter = {
|
|
236
|
-
router: 1,
|
|
237
|
-
generation: 1,
|
|
238
|
-
prepareCall: jest.fn(),
|
|
239
|
-
writeFrame: device.writeFrame,
|
|
240
|
-
readFrame: device.readFrame,
|
|
241
|
-
reset: jest.fn(),
|
|
242
|
-
};
|
|
243
|
-
const manager = new ProtocolV2LinkManager({
|
|
244
|
-
getSchemas: () => schemas,
|
|
245
|
-
classifyError: () => 'link-fatal',
|
|
246
|
-
});
|
|
247
|
-
const [first, ...rest] = chunksOf(6, 2);
|
|
248
|
-
|
|
249
|
-
await expect(
|
|
250
|
-
manager.call('device-a', () => adapter, 'FilesystemFileWrite', first, {
|
|
251
|
-
fileWritePipeline: { next: () => Promise.resolve(rest.shift()), onResponse: jest.fn() },
|
|
252
|
-
})
|
|
253
|
-
).resolves.toMatchObject({ type: 'Failure' });
|
|
254
|
-
expect(adapter.reset).not.toHaveBeenCalled();
|
|
255
|
-
expect(adapter.prepareCall).toHaveBeenCalledTimes(1);
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
test('invalidates the link when a confirmation does not match the oldest chunk', async () => {
|
|
259
|
-
const device = createDevice({
|
|
260
|
-
reply: ({ index, confirmation, encode }) =>
|
|
261
|
-
index === 1
|
|
262
|
-
? [encode('FilesystemFile', { path: PATH, offset: 4, total_size: 6, processed_byte: 6 })]
|
|
263
|
-
: [confirmation],
|
|
264
|
-
});
|
|
265
|
-
const adapter = {
|
|
266
|
-
router: 1,
|
|
267
|
-
generation: 1,
|
|
268
|
-
prepareCall: jest.fn(),
|
|
269
|
-
writeFrame: device.writeFrame,
|
|
270
|
-
readFrame: device.readFrame,
|
|
271
|
-
reset: jest.fn(() => Promise.resolve()),
|
|
272
|
-
};
|
|
273
|
-
const manager = new ProtocolV2LinkManager({
|
|
274
|
-
getSchemas: () => schemas,
|
|
275
|
-
classifyError: () => 'link-fatal',
|
|
276
|
-
});
|
|
277
|
-
const [first, ...rest] = chunksOf(6, 2);
|
|
278
|
-
|
|
279
|
-
await expect(
|
|
280
|
-
manager.call('device-a', () => adapter, 'FilesystemFileWrite', first, {
|
|
281
|
-
fileWritePipeline: { next: () => Promise.resolve(rest.shift()), onResponse: jest.fn() },
|
|
282
|
-
})
|
|
283
|
-
).rejects.toEqual(expect.objectContaining({ name: 'ProtocolV2LinkError', code: 'frame' }));
|
|
284
|
-
expect(adapter.reset).toHaveBeenCalled();
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
test('rejects an interactive response inside the pipeline', async () => {
|
|
288
|
-
const device = createDevice({
|
|
289
|
-
reply: ({ encode }) => [encode('ButtonRequest', { code: 1 })],
|
|
290
|
-
});
|
|
291
|
-
const session = createSession(device);
|
|
292
|
-
const { result } = runPipeline(session, chunksOf(4, 2));
|
|
293
|
-
|
|
294
|
-
await expect(result).rejects.toEqual(
|
|
295
|
-
expect.objectContaining({ name: 'ProtocolV2LinkError', code: 'frame' })
|
|
296
|
-
);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
test('rejects a source chunk that skips an offset before writing it', async () => {
|
|
300
|
-
const device = createDevice();
|
|
301
|
-
const session = createSession(device);
|
|
302
|
-
const { result } = runPipeline(session, [chunk(0, 2, 8), chunk(4, 2, 8)]);
|
|
303
|
-
|
|
304
|
-
await expect(result).rejects.toThrow('chunk out of order: expected offset 2, got 4');
|
|
305
|
-
expect(device.requests).toHaveLength(1);
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
test('drains the in-flight chunk and returns the last confirmation when the source stops', async () => {
|
|
309
|
-
const device = createDevice();
|
|
310
|
-
const session = createSession(device);
|
|
311
|
-
const { result, onResponse } = runPipeline(session, [chunk(0, 2, 6), chunk(2, 2, 6)]);
|
|
312
|
-
|
|
313
|
-
await expect(result).resolves.toMatchObject({
|
|
314
|
-
type: 'FilesystemFile',
|
|
315
|
-
message: { processed_byte: 4 },
|
|
316
|
-
});
|
|
317
|
-
expect(onResponse).toHaveBeenCalledTimes(2);
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
test('detects a chunk lost before a bootloader that appends at its own cursor', async () => {
|
|
321
|
-
let cursor = 0;
|
|
322
|
-
const device = createDevice({
|
|
323
|
-
reply: ({ index, request, encode }) => {
|
|
324
|
-
// The third frame never reaches the bootloader, which echoes the request offset but
|
|
325
|
-
// reports its own cursor in processed_byte.
|
|
326
|
-
if (index === 2) return [];
|
|
327
|
-
const { offset, total_size: totalSize, data } = request.message.file;
|
|
328
|
-
cursor += data.length / 2;
|
|
329
|
-
return [
|
|
330
|
-
encode('FilesystemFile', {
|
|
331
|
-
path: PATH,
|
|
332
|
-
offset,
|
|
333
|
-
total_size: totalSize,
|
|
334
|
-
processed_byte: cursor,
|
|
335
|
-
}),
|
|
336
|
-
];
|
|
337
|
-
},
|
|
338
|
-
});
|
|
339
|
-
const session = createSession(device);
|
|
340
|
-
const { result, onResponse } = runPipeline(session, chunksOf(10, 2));
|
|
341
|
-
|
|
342
|
-
await expect(result).rejects.toEqual(
|
|
343
|
-
expect.objectContaining({ name: 'ProtocolV2LinkError', code: 'frame' })
|
|
344
|
-
);
|
|
345
|
-
expect(onResponse.mock.calls.map(([response]) => response.message.processed_byte)).toEqual([
|
|
346
|
-
2, 4,
|
|
347
|
-
]);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
test('does not let skipped frames extend the wait for a response', async () => {
|
|
351
|
-
const device = createDevice({ reply: () => [] });
|
|
352
|
-
const noise = setInterval(() => device.inject('Success', { message: 'unrelated' }), 10);
|
|
353
|
-
try {
|
|
354
|
-
const session = createSession(device);
|
|
355
|
-
const { result } = runPipeline(session, chunksOf(2, 2), { timeoutMs: 60 });
|
|
356
|
-
|
|
357
|
-
await expect(result).rejects.toThrow('Protocol V2 response timeout after 60ms');
|
|
358
|
-
} finally {
|
|
359
|
-
clearInterval(noise);
|
|
360
|
-
}
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
test('rejects a chunk whose frame exceeds the transport limit', async () => {
|
|
364
|
-
const device = createDevice();
|
|
365
|
-
const session = createSession(device, { maxFrameBytes: 64 });
|
|
366
|
-
const { result } = runPipeline(session, [chunk(0, 128, 128)]);
|
|
367
|
-
|
|
368
|
-
await expect(result).rejects.toThrow('Protocol V2 frame too large for transport');
|
|
369
|
-
expect(device.requests).toHaveLength(0);
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
test('bounds each response wait instead of the whole file', async () => {
|
|
373
|
-
const device = createDevice();
|
|
374
|
-
const read = device.readFrame.getMockImplementation();
|
|
375
|
-
device.readFrame.mockImplementation(
|
|
376
|
-
() =>
|
|
377
|
-
new Promise(resolve => {
|
|
378
|
-
setTimeout(() => resolve(read()), 40);
|
|
379
|
-
})
|
|
380
|
-
);
|
|
381
|
-
const session = createSession(device);
|
|
382
|
-
const { result } = runPipeline(session, chunksOf(8, 2), { timeoutMs: 100 });
|
|
383
|
-
|
|
384
|
-
// Four 40 ms responses outlast the 100 ms timeout, but no single wait does.
|
|
385
|
-
await expect(result).resolves.toMatchObject({ message: { processed_byte: 8 } });
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
test('times out a stalled response with the transport error', async () => {
|
|
389
|
-
const device = createDevice();
|
|
390
|
-
device.readFrame.mockImplementation(() => new Promise(() => {}));
|
|
391
|
-
const createTimeoutError = jest.fn(
|
|
392
|
-
(name, timeoutMs) => new Error(`BLE timeout ${name} ${timeoutMs}`)
|
|
393
|
-
);
|
|
394
|
-
const session = createSession(device, { createTimeoutError });
|
|
395
|
-
const { result } = runPipeline(session, chunksOf(4, 2), { timeoutMs: 50 });
|
|
396
|
-
|
|
397
|
-
await expect(result).rejects.toThrow('BLE timeout FilesystemFileWrite 50');
|
|
398
|
-
expect(device.writeFrame.mock.calls[0][1].signal.aborted).toBe(true);
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
test('stops when the caller rejects a confirmed chunk', async () => {
|
|
402
|
-
const device = createDevice();
|
|
403
|
-
const session = createSession(device);
|
|
404
|
-
const [first, ...rest] = chunksOf(10, 2);
|
|
405
|
-
const result = session.call('FilesystemFileWrite', first, {
|
|
406
|
-
fileWritePipeline: {
|
|
407
|
-
next: () => Promise.resolve(rest.shift()),
|
|
408
|
-
onResponse: () => {
|
|
409
|
-
throw new Error('cancelled');
|
|
410
|
-
},
|
|
411
|
-
},
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
await expect(result).rejects.toThrow('cancelled');
|
|
415
|
-
expect(device.requests).toHaveLength(2);
|
|
416
|
-
});
|
|
417
|
-
});
|