Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor LightPush send #1487

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions packages/core/src/lib/light_push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,78 @@ class LightPush extends BaseProtocol implements ILightPush {
this.options = options || {};
}

private async preparePushMessage(
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 };
}

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 };
}

async send(
encoder: IEncoder,
message: IMessage,
opts?: ProtocolOptions
): Promise<SendResult> {
const { pubSubTopic = DefaultPubSubTopic } = this.options;

const peer = await this.getPeer(opts?.peerId);
const stream = await this.newStream(peer);

const recipients: PeerId[] = [];
let error: undefined | SendError = undefined;

let query: PushRpc | null = null;

try {
if (!isSizeValid(message.payload)) {
log("Failed to send waku light push: message is bigger that 1MB");
return {
recipients,
error: SendError.SIZE_TOO_BIG
};
}
const { query: preparedQuery, error: preparationError } =
await this.preparePushMessage(encoder, message, pubSubTopic);

const protoMessage = await encoder.toProtoObj(message);
if (!protoMessage) {
log("Failed to encode to protoMessage, aborting push");
if (preparationError) {
return {
recipients,
error: SendError.ENCODE_FAILED
error: preparationError
};
}
const query = PushRpc.createRequest(protoMessage, pubSubTopic);

query = preparedQuery;
} catch (error) {
log("Failed to prepare push message", error);
}

if (!query) {
return {
recipients,
error: SendError.GENERIC_FAIL
};
}

const peer = await this.getPeer(opts?.peerId);
const stream = await this.newStream(peer);

try {
const res = await pipe(
[query.encode()],
lp.encode,
stream,
lp.decode,
async (source) => await all(source)
);

try {
const bytes = new Uint8ArrayList();
res.forEach((chunk) => {
Expand All @@ -98,9 +132,10 @@ class LightPush extends BaseProtocol implements ILightPush {
log("Failed to send waku light push request", err);
error = SendError.GENERIC_FAIL;
}

return {
error,
recipients
recipients,
error
};
}
}
Expand Down
Loading