diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index d7b7b4b6b3..c9b1e92b3e 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -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 { 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 }; } diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index 923ddfc4e5..21a0a49720 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -67,13 +67,23 @@ export type Callback = ( 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", diff --git a/packages/relay/src/index.ts b/packages/relay/src/index.ts index 28270ba03c..fcd458cc1e 100644 --- a/packages/relay/src/index.ts +++ b/packages/relay/src/index.ts @@ -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, diff --git a/packages/tests/tests/light-push/index.node.spec.ts b/packages/tests/tests/light-push/index.node.spec.ts index 4e47a4b451..2a08701c71 100644 --- a/packages/tests/tests/light-push/index.node.spec.ts +++ b/packages/tests/tests/light-push/index.node.spec.ts @@ -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) => { diff --git a/packages/utils/src/common/is_size_valid.ts b/packages/utils/src/common/is_size_valid.ts index 9585b32224..7c565b321b 100644 --- a/packages/utils/src/common/is_size_valid.ts +++ b/packages/utils/src/common/is_size_valid.ts @@ -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; }