2023-10-26 22:14:47 +11:00
|
|
|
import type { IEncoder, IMessage } from "@waku/interfaces";
|
|
|
|
|
|
2023-05-17 23:40:52 +02:00
|
|
|
const MB = 1024 ** 2;
|
2023-10-26 22:14:47 +11:00
|
|
|
const SIZE_CAP_IN_MB = 1;
|
2023-05-17 23:40:52 +02:00
|
|
|
|
2023-10-26 22:14:47 +11:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
2023-05-17 23:40:52 +02:00
|
|
|
|
2023-10-26 22:14:47 +11:00
|
|
|
export const isWireSizeUnderCap = (buf: Uint8Array): boolean =>
|
|
|
|
|
buf.length / MB <= SIZE_CAP_IN_MB;
|