mirror of https://github.com/status-im/js-waku.git
feat: fail early when trying to send empty payload (#1642)
* refactor: rename as this method does not check empty payloads * feat: fail early when trying to send empty payload --------- Co-authored-by: Sasha <oleksandr@status.im>
This commit is contained in:
parent
0e4ceb6ac9
commit
6bad4ea7d1
|
@ -11,7 +11,7 @@ import {
|
|||
SendResult
|
||||
} from "@waku/interfaces";
|
||||
import { PushResponse } from "@waku/proto";
|
||||
import { ensurePubsubTopicIsConfigured, isSizeValid } from "@waku/utils";
|
||||
import { ensurePubsubTopicIsConfigured, isSizeUnderCap } from "@waku/utils";
|
||||
import debug from "debug";
|
||||
import all from "it-all";
|
||||
import * as lp from "it-length-prefixed";
|
||||
|
@ -56,7 +56,12 @@ class LightPush extends BaseProtocol implements ILightPush {
|
|||
pubsubTopic: string
|
||||
): Promise<PreparePushMessageResult> {
|
||||
try {
|
||||
if (!isSizeValid(message.payload)) {
|
||||
if (!message.payload || message.payload.length === 0) {
|
||||
log("Failed to send waku light push: payload is empty");
|
||||
return { query: null, error: SendError.EMPTY_PAYLOAD };
|
||||
}
|
||||
|
||||
if (!isSizeUnderCap(message.payload)) {
|
||||
log("Failed to send waku light push: message is bigger than 1MB");
|
||||
return { query: null, error: SendError.SIZE_TOO_BIG };
|
||||
}
|
||||
|
|
|
@ -67,13 +67,23 @@ export type Callback<T extends IDecodedMessage> = (
|
|||
export enum SendError {
|
||||
/** Could not determine the origin of the fault. Best to check connectivity and try again */
|
||||
GENERIC_FAIL = "Generic error",
|
||||
/** Failure to protobuf encode the message. This is not recoverable and needs
|
||||
* further investigation. */
|
||||
/**
|
||||
* Failure to protobuf encode the message. This is not recoverable and needs
|
||||
* further investigation.
|
||||
*/
|
||||
ENCODE_FAILED = "Failed to encode",
|
||||
/** Failure to protobuf decode the message. May be due to a remote peer issue,
|
||||
* ensuring that messages are sent via several peer enable mitigation of this error.. */
|
||||
/**
|
||||
* Failure to protobuf decode the message. May be due to a remote peer issue,
|
||||
* ensuring that messages are sent via several peer enable mitigation of this error.
|
||||
*/
|
||||
DECODE_FAILED = "Failed to decode",
|
||||
/** The message size is above the maximum message size allowed on the Waku Network.
|
||||
/**
|
||||
* The message payload is empty, making the message invalid. Ensure that a non-empty
|
||||
* payload is set on the outgoing message.
|
||||
*/
|
||||
EMPTY_PAYLOAD = "Payload is empty",
|
||||
/**
|
||||
* The message size is above the maximum message size allowed on the Waku Network.
|
||||
* Compressing the message or using an alternative strategy for large messages is recommended.
|
||||
*/
|
||||
SIZE_TOO_BIG = "Size is too big",
|
||||
|
|
|
@ -25,7 +25,7 @@ import {
|
|||
SendError,
|
||||
SendResult
|
||||
} from "@waku/interfaces";
|
||||
import { isSizeValid, toAsyncIterator } from "@waku/utils";
|
||||
import { isSizeUnderCap, toAsyncIterator } from "@waku/utils";
|
||||
import { pushOrInitMapSet } from "@waku/utils";
|
||||
import debug from "debug";
|
||||
|
||||
|
@ -112,7 +112,7 @@ class Relay implements IRelay {
|
|||
};
|
||||
}
|
||||
|
||||
if (!isSizeValid(message.payload)) {
|
||||
if (!isSizeUnderCap(message.payload)) {
|
||||
log("Failed to send waku relay: message is bigger that 1MB");
|
||||
return {
|
||||
recipients,
|
||||
|
|
|
@ -74,24 +74,13 @@ describe("Waku Light Push", function () {
|
|||
}
|
||||
});
|
||||
|
||||
it("Fails to push message with empty payload", async function () {
|
||||
it("Throws when trying to push message with empty payload", async function () {
|
||||
const pushResponse = await waku.lightPush.send(TestEncoder, {
|
||||
payload: utf8ToBytes("")
|
||||
payload: new Uint8Array()
|
||||
});
|
||||
|
||||
if (nwaku.type() == "go-waku") {
|
||||
expect(pushResponse.recipients.length).to.eq(1);
|
||||
expect(await messageCollector.waitForMessages(1)).to.eq(true);
|
||||
messageCollector.verifyReceivedMessage(0, {
|
||||
expectedMessageText: undefined,
|
||||
expectedContentTopic: TestContentTopic
|
||||
});
|
||||
} else {
|
||||
expect(pushResponse.recipients.length).to.eq(0);
|
||||
// This should be `REMOTE_PEER_REJECTED`, tracked with https://github.com/waku-org/nwaku/issues/1641
|
||||
expect(pushResponse.errors).to.include(SendError.REMOTE_PEER_FAULT);
|
||||
expect(await messageCollector.waitForMessages(1)).to.eq(false);
|
||||
}
|
||||
expect(pushResponse.errors).to.include(SendError.EMPTY_PAYLOAD);
|
||||
expect(await messageCollector.waitForMessages(1)).to.eq(false);
|
||||
});
|
||||
|
||||
TEST_STRING.forEach((testItem) => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const MB = 1024 ** 2;
|
||||
const SIZE_CAP = 1; // 1 MB
|
||||
|
||||
export const isSizeValid = (payload: Uint8Array): boolean => {
|
||||
export const isSizeUnderCap = (payload: Uint8Array): boolean => {
|
||||
if (payload.length / MB > SIZE_CAP) {
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue