Skip to content

Commit 6bad4ea

Browse files
fryorcrakenweboko
andauthored
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>
1 parent 0e4ceb6 commit 6bad4ea

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

packages/core/src/lib/light_push/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
SendResult
1212
} from "@waku/interfaces";
1313
import { PushResponse } from "@waku/proto";
14-
import { ensurePubsubTopicIsConfigured, isSizeValid } from "@waku/utils";
14+
import { ensurePubsubTopicIsConfigured, isSizeUnderCap } from "@waku/utils";
1515
import debug from "debug";
1616
import all from "it-all";
1717
import * as lp from "it-length-prefixed";
@@ -56,7 +56,12 @@ class LightPush extends BaseProtocol implements ILightPush {
5656
pubsubTopic: string
5757
): Promise<PreparePushMessageResult> {
5858
try {
59-
if (!isSizeValid(message.payload)) {
59+
if (!message.payload || message.payload.length === 0) {
60+
log("Failed to send waku light push: payload is empty");
61+
return { query: null, error: SendError.EMPTY_PAYLOAD };
62+
}
63+
64+
if (!isSizeUnderCap(message.payload)) {
6065
log("Failed to send waku light push: message is bigger than 1MB");
6166
return { query: null, error: SendError.SIZE_TOO_BIG };
6267
}

packages/interfaces/src/protocols.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,23 @@ export type Callback<T extends IDecodedMessage> = (
6767
export enum SendError {
6868
/** Could not determine the origin of the fault. Best to check connectivity and try again */
6969
GENERIC_FAIL = "Generic error",
70-
/** Failure to protobuf encode the message. This is not recoverable and needs
71-
* further investigation. */
70+
/**
71+
* Failure to protobuf encode the message. This is not recoverable and needs
72+
* further investigation.
73+
*/
7274
ENCODE_FAILED = "Failed to encode",
73-
/** Failure to protobuf decode the message. May be due to a remote peer issue,
74-
* ensuring that messages are sent via several peer enable mitigation of this error.. */
75+
/**
76+
* Failure to protobuf decode the message. May be due to a remote peer issue,
77+
* ensuring that messages are sent via several peer enable mitigation of this error.
78+
*/
7579
DECODE_FAILED = "Failed to decode",
76-
/** The message size is above the maximum message size allowed on the Waku Network.
80+
/**
81+
* The message payload is empty, making the message invalid. Ensure that a non-empty
82+
* payload is set on the outgoing message.
83+
*/
84+
EMPTY_PAYLOAD = "Payload is empty",
85+
/**
86+
* The message size is above the maximum message size allowed on the Waku Network.
7787
* Compressing the message or using an alternative strategy for large messages is recommended.
7888
*/
7989
SIZE_TOO_BIG = "Size is too big",

packages/relay/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
SendError,
2626
SendResult
2727
} from "@waku/interfaces";
28-
import { isSizeValid, toAsyncIterator } from "@waku/utils";
28+
import { isSizeUnderCap, toAsyncIterator } from "@waku/utils";
2929
import { pushOrInitMapSet } from "@waku/utils";
3030
import debug from "debug";
3131

@@ -112,7 +112,7 @@ class Relay implements IRelay {
112112
};
113113
}
114114

115-
if (!isSizeValid(message.payload)) {
115+
if (!isSizeUnderCap(message.payload)) {
116116
log("Failed to send waku relay: message is bigger that 1MB");
117117
return {
118118
recipients,

packages/tests/tests/light-push/index.node.spec.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,13 @@ describe("Waku Light Push", function () {
7474
}
7575
});
7676

77-
it("Fails to push message with empty payload", async function () {
77+
it("Throws when trying to push message with empty payload", async function () {
7878
const pushResponse = await waku.lightPush.send(TestEncoder, {
79-
payload: utf8ToBytes("")
79+
payload: new Uint8Array()
8080
});
8181

82-
if (nwaku.type() == "go-waku") {
83-
expect(pushResponse.recipients.length).to.eq(1);
84-
expect(await messageCollector.waitForMessages(1)).to.eq(true);
85-
messageCollector.verifyReceivedMessage(0, {
86-
expectedMessageText: undefined,
87-
expectedContentTopic: TestContentTopic
88-
});
89-
} else {
90-
expect(pushResponse.recipients.length).to.eq(0);
91-
// This should be `REMOTE_PEER_REJECTED`, tracked with https://github.com/waku-org/nwaku/issues/1641
92-
expect(pushResponse.errors).to.include(SendError.REMOTE_PEER_FAULT);
93-
expect(await messageCollector.waitForMessages(1)).to.eq(false);
94-
}
82+
expect(pushResponse.errors).to.include(SendError.EMPTY_PAYLOAD);
83+
expect(await messageCollector.waitForMessages(1)).to.eq(false);
9584
});
9685

9786
TEST_STRING.forEach((testItem) => {

packages/utils/src/common/is_size_valid.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const MB = 1024 ** 2;
22
const SIZE_CAP = 1; // 1 MB
33

4-
export const isSizeValid = (payload: Uint8Array): boolean => {
4+
export const isSizeUnderCap = (payload: Uint8Array): boolean => {
55
if (payload.length / MB > SIZE_CAP) {
66
return false;
77
}

0 commit comments

Comments
 (0)