mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-09 17:23:11 +00:00
Network message limitations are imposed on the whole message, not just the payload. Co-authored-by: Sasha <118575614+weboko@users.noreply.github.com>
24 lines
684 B
TypeScript
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;
|