js-waku/packages/utils/src/common/is_size_valid.ts
fryorcraken b7dc3d7576
fix: measure total message size (#1643)
Network message limitations are imposed on the whole message, not just
the payload.

Co-authored-by: Sasha <118575614+weboko@users.noreply.github.com>
2023-10-26 13:14:47 +02:00

24 lines
684 B
TypeScript

import type { IEncoder, IMessage } from "@waku/interfaces";
const MB = 1024 ** 2;
const SIZE_CAP_IN_MB = 1;
/**
* Return whether the size of the message is under the upper limit for the network.
* This performs a protobuf encoding! If you have access to the fully encoded message,
* use {@link isSizeUnderCapBuf} instead.
* @param message
* @param encoder
*/
export async function isMessageSizeUnderCap(
encoder: IEncoder,
message: IMessage
): Promise<boolean> {
const buf = await encoder.toWire(message);
if (!buf) return false;
return isWireSizeUnderCap(buf);
}
export const isWireSizeUnderCap = (buf: Uint8Array): boolean =>
buf.length / MB <= SIZE_CAP_IN_MB;