mirror of https://github.com/waku-org/js-waku.git
chore: improve preparePushMessage (#1509)
* chore: improve readability a bit * rename * decople to a type * fix error
This commit is contained in:
parent
423abb9211
commit
27177a7efa
|
@ -27,6 +27,16 @@ const log = debug("waku:light-push");
|
|||
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
||||
export { PushResponse };
|
||||
|
||||
type PreparePushMessageResult =
|
||||
| {
|
||||
query: PushRpc;
|
||||
error: null;
|
||||
}
|
||||
| {
|
||||
query: null;
|
||||
error: SendError;
|
||||
};
|
||||
|
||||
/**
|
||||
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
||||
*/
|
||||
|
@ -42,26 +52,32 @@ class LightPush extends BaseProtocol implements ILightPush {
|
|||
encoder: IEncoder,
|
||||
message: IMessage,
|
||||
pubSubTopic: string
|
||||
): Promise<{
|
||||
query: PushRpc | null;
|
||||
error?: SendError;
|
||||
}> {
|
||||
if (!isSizeValid(message.payload)) {
|
||||
log("Failed to send waku light push: message is bigger than 1MB");
|
||||
return { query: null, error: SendError.SIZE_TOO_BIG };
|
||||
}
|
||||
): Promise<PreparePushMessageResult> {
|
||||
try {
|
||||
if (!isSizeValid(message.payload)) {
|
||||
log("Failed to send waku light push: message is bigger than 1MB");
|
||||
return { query: null, error: SendError.SIZE_TOO_BIG };
|
||||
}
|
||||
|
||||
const protoMessage = await encoder.toProtoObj(message);
|
||||
if (!protoMessage) {
|
||||
log("Failed to encode to protoMessage, aborting push");
|
||||
return {
|
||||
query: null,
|
||||
error: SendError.ENCODE_FAILED
|
||||
};
|
||||
}
|
||||
|
||||
const query = PushRpc.createRequest(protoMessage, pubSubTopic);
|
||||
return { query, error: null };
|
||||
} catch (error) {
|
||||
log("Failed to prepare push message", error);
|
||||
|
||||
const protoMessage = await encoder.toProtoObj(message);
|
||||
if (!protoMessage) {
|
||||
log("Failed to encode to protoMessage, aborting push");
|
||||
return {
|
||||
query: null,
|
||||
error: SendError.ENCODE_FAILED
|
||||
error: SendError.GENERIC_FAIL
|
||||
};
|
||||
}
|
||||
|
||||
const query = PushRpc.createRequest(protoMessage, pubSubTopic);
|
||||
return { query };
|
||||
}
|
||||
|
||||
async send(
|
||||
|
@ -71,33 +87,21 @@ class LightPush extends BaseProtocol implements ILightPush {
|
|||
): Promise<SendResult> {
|
||||
const { pubSubTopic = DefaultPubSubTopic } = this.options;
|
||||
const recipients: PeerId[] = [];
|
||||
let error: undefined | SendError = undefined;
|
||||
|
||||
let query: PushRpc | null = null;
|
||||
const { query, error: preparationError } = await this.preparePushMessage(
|
||||
encoder,
|
||||
message,
|
||||
pubSubTopic
|
||||
);
|
||||
|
||||
try {
|
||||
const { query: preparedQuery, error: preparationError } =
|
||||
await this.preparePushMessage(encoder, message, pubSubTopic);
|
||||
|
||||
if (preparationError) {
|
||||
return {
|
||||
recipients,
|
||||
error: preparationError
|
||||
};
|
||||
}
|
||||
|
||||
query = preparedQuery;
|
||||
} catch (error) {
|
||||
log("Failed to prepare push message", error);
|
||||
}
|
||||
|
||||
if (!query) {
|
||||
if (preparationError || !query) {
|
||||
return {
|
||||
recipients,
|
||||
error: SendError.GENERIC_FAIL
|
||||
error: preparationError
|
||||
};
|
||||
}
|
||||
|
||||
let error: undefined | SendError = undefined;
|
||||
const peer = await this.getPeer(opts?.peerId);
|
||||
const stream = await this.newStream(peer);
|
||||
|
||||
|
|
Loading…
Reference in New Issue