fix(protocol): ignore unexpected chunks in ThpAck after send

This commit is contained in:
Szymon Lesisz
2025-08-21 16:45:17 +02:00
committed by martin
parent 0789c96862
commit 30e20f7096
3 changed files with 6 additions and 9 deletions

View File

@@ -198,8 +198,6 @@ export const decodeSendAck = (decodedMessage: MessageV2) => {
if (magic === THP_READ_ACK_HEADER_BYTE) {
return decodeReadAck();
}
throw new Error('Unexpected send response: ' + magic);
};
// Decode protocol-v2 message from thp receive process

View File

@@ -96,15 +96,14 @@ describe('protocol-thp', () => {
});
it('decodeSendAck', () => {
const thpError2 = decodeSendAck(decodeV2(Buffer.from('42122200050270303cfa', 'hex')));
expect(thpError2.type).toBe('ThpError');
const thpError = decodeSendAck(decodeV2(Buffer.from('42122200050270303cfa', 'hex')));
expect(thpError?.type).toBe('ThpError');
const ack = decodeSendAck(decodeV2(Buffer.from('2812340004e98c8599', 'hex')));
expect(ack.type).toBe('ThpAck');
expect(ack?.type).toBe('ThpAck');
expect(() => decodeSendAck(decodeV2(Buffer.from('40ffff0004f9215951', 'hex')))).toThrow(
'Unexpected send response',
);
const unexpected = decodeSendAck(decodeV2(Buffer.from('40ffff0004f9215951', 'hex')));
expect(unexpected).toBe(undefined);
expect(() => decodeSendAck(decodeV2(Buffer.from('40ffff000499999999', 'hex')))).toThrow(
'Invalid CRC',

View File

@@ -94,7 +94,7 @@ export const sendThpMessage = async ({
// parse and check the result
const decodedResult = protocolThp.decodeSendAck(protocolV2.decode(result.payload));
// fail on ThpError
if (decodedResult.type === 'ThpError') {
if (decodedResult?.type === 'ThpError') {
const { code, message } = decodedResult.message;
return error({ error: code, message });