Extract `getPayloadSize` logic

This commit is contained in:
Franck Royer 2022-05-19 19:54:16 +10:00
parent 95485f0a69
commit 33f52ef56d
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 9 additions and 1 deletions

View File

@ -94,7 +94,7 @@ export function clearDecode(
const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message); const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message);
if (sizeOfPayloadSizeField === 0) return; if (sizeOfPayloadSizeField === 0) return;
const payloadSize = buf.readUIntLE(start, sizeOfPayloadSizeField); const payloadSize = getPayloadSize(message, sizeOfPayloadSizeField);
start += sizeOfPayloadSizeField; start += sizeOfPayloadSizeField;
const payload = buf.slice(start, start + payloadSize); const payload = buf.slice(start, start + payloadSize);
@ -114,6 +114,14 @@ function getSizeOfPayloadSizeField(message: Uint8Array): number {
return messageDataView.getUint8(0) & FlagMask; return messageDataView.getUint8(0) & FlagMask;
} }
function getPayloadSize(
message: Uint8Array,
sizeOfPayloadSizeField: number
): number {
const buf = Buffer.from(message);
return buf.readUIntLE(1, sizeOfPayloadSizeField);
}
/** /**
* Proceed with Asymmetric encryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/). * Proceed with Asymmetric encryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
* The data MUST be flags | payload-length | payload | [signature]. * The data MUST be flags | payload-length | payload | [signature].