Skip to content

Commit 812310a

Browse files
chore: refactor LightPush send (#1487)
* refactor lightpush send * add trycatch
1 parent 9d4fa3f commit 812310a

File tree

1 file changed

+53
-18
lines changed
  • packages/core/src/lib/light_push

1 file changed

+53
-18
lines changed

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

+53-18
Original file line numberDiff line numberDiff line change
@@ -38,44 +38,78 @@ class LightPush extends BaseProtocol implements ILightPush {
3838
this.options = options || {};
3939
}
4040

41+
private async preparePushMessage(
42+
encoder: IEncoder,
43+
message: IMessage,
44+
pubSubTopic: string
45+
): Promise<{
46+
query: PushRpc | null;
47+
error?: SendError;
48+
}> {
49+
if (!isSizeValid(message.payload)) {
50+
log("Failed to send waku light push: message is bigger than 1MB");
51+
return { query: null, error: SendError.SIZE_TOO_BIG };
52+
}
53+
54+
const protoMessage = await encoder.toProtoObj(message);
55+
if (!protoMessage) {
56+
log("Failed to encode to protoMessage, aborting push");
57+
return {
58+
query: null,
59+
error: SendError.ENCODE_FAILED
60+
};
61+
}
62+
63+
const query = PushRpc.createRequest(protoMessage, pubSubTopic);
64+
return { query };
65+
}
66+
4167
async send(
4268
encoder: IEncoder,
4369
message: IMessage,
4470
opts?: ProtocolOptions
4571
): Promise<SendResult> {
4672
const { pubSubTopic = DefaultPubSubTopic } = this.options;
47-
48-
const peer = await this.getPeer(opts?.peerId);
49-
const stream = await this.newStream(peer);
50-
5173
const recipients: PeerId[] = [];
5274
let error: undefined | SendError = undefined;
5375

76+
let query: PushRpc | null = null;
77+
5478
try {
55-
if (!isSizeValid(message.payload)) {
56-
log("Failed to send waku light push: message is bigger that 1MB");
57-
return {
58-
recipients,
59-
error: SendError.SIZE_TOO_BIG
60-
};
61-
}
79+
const { query: preparedQuery, error: preparationError } =
80+
await this.preparePushMessage(encoder, message, pubSubTopic);
6281

63-
const protoMessage = await encoder.toProtoObj(message);
64-
if (!protoMessage) {
65-
log("Failed to encode to protoMessage, aborting push");
82+
if (preparationError) {
6683
return {
6784
recipients,
68-
error: SendError.ENCODE_FAILED
85+
error: preparationError
6986
};
7087
}
71-
const query = PushRpc.createRequest(protoMessage, pubSubTopic);
88+
89+
query = preparedQuery;
90+
} catch (error) {
91+
log("Failed to prepare push message", error);
92+
}
93+
94+
if (!query) {
95+
return {
96+
recipients,
97+
error: SendError.GENERIC_FAIL
98+
};
99+
}
100+
101+
const peer = await this.getPeer(opts?.peerId);
102+
const stream = await this.newStream(peer);
103+
104+
try {
72105
const res = await pipe(
73106
[query.encode()],
74107
lp.encode,
75108
stream,
76109
lp.decode,
77110
async (source) => await all(source)
78111
);
112+
79113
try {
80114
const bytes = new Uint8ArrayList();
81115
res.forEach((chunk) => {
@@ -98,9 +132,10 @@ class LightPush extends BaseProtocol implements ILightPush {
98132
log("Failed to send waku light push request", err);
99133
error = SendError.GENERIC_FAIL;
100134
}
135+
101136
return {
102-
error,
103-
recipients
137+
recipients,
138+
error
104139
};
105140
}
106141
}

0 commit comments

Comments
 (0)