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,10 +52,8 @@ class LightPush extends BaseProtocol implements ILightPush {
|
|||
encoder: IEncoder,
|
||||
message: IMessage,
|
||||
pubSubTopic: string
|
||||
): Promise<{
|
||||
query: PushRpc | null;
|
||||
error?: SendError;
|
||||
}> {
|
||||
): 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 };
|
||||
|
@ -61,7 +69,15 @@ class LightPush extends BaseProtocol implements ILightPush {
|
|||
}
|
||||
|
||||
const query = PushRpc.createRequest(protoMessage, pubSubTopic);
|
||||
return { query };
|
||||
return { query, error: null };
|
||||
} catch (error) {
|
||||
log("Failed to prepare push message", error);
|
||||
|
||||
return {
|
||||
query: null,
|
||||
error: SendError.GENERIC_FAIL
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
if (preparationError || !query) {
|
||||
return {
|
||||
recipients,
|
||||
error: preparationError
|
||||
};
|
||||
}
|
||||
|
||||
query = preparedQuery;
|
||||
} catch (error) {
|
||||
log("Failed to prepare push message", error);
|
||||
}
|
||||
|
||||
if (!query) {
|
||||
return {
|
||||
recipients,
|
||||
error: SendError.GENERIC_FAIL
|
||||
};
|
||||
}
|
||||
|
||||
let error: undefined | SendError = undefined;
|
||||
const peer = await this.getPeer(opts?.peerId);
|
||||
const stream = await this.newStream(peer);
|
||||
|
||||
|
|
Loading…
Reference in New Issue