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 const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
||||||
export { PushResponse };
|
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/).
|
* 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,
|
encoder: IEncoder,
|
||||||
message: IMessage,
|
message: IMessage,
|
||||||
pubSubTopic: string
|
pubSubTopic: string
|
||||||
): Promise<{
|
): Promise<PreparePushMessageResult> {
|
||||||
query: PushRpc | null;
|
try {
|
||||||
error?: SendError;
|
if (!isSizeValid(message.payload)) {
|
||||||
}> {
|
log("Failed to send waku light push: message is bigger than 1MB");
|
||||||
if (!isSizeValid(message.payload)) {
|
return { query: null, error: SendError.SIZE_TOO_BIG };
|
||||||
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 {
|
return {
|
||||||
query: null,
|
query: null,
|
||||||
error: SendError.ENCODE_FAILED
|
error: SendError.GENERIC_FAIL
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = PushRpc.createRequest(protoMessage, pubSubTopic);
|
|
||||||
return { query };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async send(
|
async send(
|
||||||
|
@ -71,33 +87,21 @@ class LightPush extends BaseProtocol implements ILightPush {
|
||||||
): Promise<SendResult> {
|
): Promise<SendResult> {
|
||||||
const { pubSubTopic = DefaultPubSubTopic } = this.options;
|
const { pubSubTopic = DefaultPubSubTopic } = this.options;
|
||||||
const recipients: PeerId[] = [];
|
const recipients: PeerId[] = [];
|
||||||
let error: undefined | SendError = undefined;
|
|
||||||
|
|
||||||
let query: PushRpc | null = null;
|
const { query, error: preparationError } = await this.preparePushMessage(
|
||||||
|
encoder,
|
||||||
|
message,
|
||||||
|
pubSubTopic
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
if (preparationError || !query) {
|
||||||
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) {
|
|
||||||
return {
|
return {
|
||||||
recipients,
|
recipients,
|
||||||
error: SendError.GENERIC_FAIL
|
error: preparationError
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let error: undefined | SendError = undefined;
|
||||||
const peer = await this.getPeer(opts?.peerId);
|
const peer = await this.getPeer(opts?.peerId);
|
||||||
const stream = await this.newStream(peer);
|
const stream = await this.newStream(peer);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue