diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index 4ecdea0be4..de78c3b144 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -111,50 +111,50 @@ class LightPush extends BaseProtocol implements ILightPush { } const promises = peers.map(async (peer) => { - let error: SendError | undefined; - let stream: Stream | undefined; try { stream = await this.getStream(peer); } catch (err) { log(`Failed to get a stream for remote peer${peer.id.toString()}`, err); - error = SendError.REMOTE_PEER_FAULT; - return { recipients, error }; + return { recipients, error: SendError.REMOTE_PEER_FAULT }; } + let res: Uint8ArrayList[] | undefined; try { - const res = await pipe( + res = await pipe( [query.encode()], lp.encode, stream, lp.decode, async (source) => await all(source) ); - try { - const bytes = new Uint8ArrayList(); - res.forEach((chunk) => { - bytes.append(chunk); - }); - - const response = PushRpc.decode(bytes).response; - - if (response?.isSuccess) { - recipients.some((recipient) => recipient.equals(peer.id)) || - recipients.push(peer.id); - } else { - log("Remote peer fault: No response in PushRPC"); - error = SendError.REMOTE_PEER_FAULT; - } - } catch (err) { - log("Failed to decode push reply", err); - error = SendError.DECODE_FAILED; - } } catch (err) { log("Failed to send waku light push request", err); - error = SendError.GENERIC_FAIL; + return { recipients, error: SendError.GENERIC_FAIL }; } - return { recipients, error }; + const bytes = new Uint8ArrayList(); + res.forEach((chunk) => { + bytes.append(chunk); + }); + + let response: PushResponse | undefined; + try { + response = PushRpc.decode(bytes).response; + } catch (err) { + log("Failed to decode push reply", err); + return { recipients, error: SendError.DECODE_FAILED }; + } + + if (response?.isSuccess) { + recipients.some((recipient) => recipient.equals(peer.id)) || + recipients.push(peer.id); + } else { + log("Remote peer fault: No response in PushRPC"); + return { recipients, error: SendError.REMOTE_PEER_FAULT }; + } + + return { recipients }; }); const results = await Promise.allSettled(promises);