From a1828eed50c83a75e04b3d8492b89ecfb2da5ce5 Mon Sep 17 00:00:00 2001 From: Sasha Date: Mon, 28 Aug 2023 12:55:53 +0200 Subject: [PATCH 1/4] chore: improve readability a bit --- packages/core/src/lib/light_push/index.ts | 73 ++++++++++++----------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index a0259b3d12..b9d3d8e6c8 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -42,26 +42,41 @@ 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< + | { + query: PushRpc; + error: null; + } + | { + query: null; + error: SendError; + } + > { + 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 +86,21 @@ class LightPush extends BaseProtocol implements ILightPush { ): Promise { const { pubSubTopic = DefaultPubSubTopic } = this.options; const recipients: PeerId[] = []; - let error: undefined | SendError = undefined; - - let query: PushRpc | null = null; - - 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); - } + const { query, error: prepareError } = await this.preparePushMessage( + encoder, + message, + pubSubTopic + ); - if (!query) { + if (prepareError || !query) { return { recipients, - error: SendError.GENERIC_FAIL + error: prepareError }; } + let error: SendError; const peer = await this.getPeer(opts?.peerId); const stream = await this.newStream(peer); From 932b5efdf2fc59a6252fcb3b297d545521bcbc08 Mon Sep 17 00:00:00 2001 From: Sasha Date: Mon, 28 Aug 2023 12:56:22 +0200 Subject: [PATCH 2/4] rename --- packages/core/src/lib/light_push/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index b9d3d8e6c8..5f0136f7ee 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -87,16 +87,16 @@ class LightPush extends BaseProtocol implements ILightPush { const { pubSubTopic = DefaultPubSubTopic } = this.options; const recipients: PeerId[] = []; - const { query, error: prepareError } = await this.preparePushMessage( + const { query, error: preparationError } = await this.preparePushMessage( encoder, message, pubSubTopic ); - if (prepareError || !query) { + if (preparationError || !query) { return { recipients, - error: prepareError + error: preparationError }; } From 456dc09bba3d0d0efbec1f29ca5cc657b4ad6003 Mon Sep 17 00:00:00 2001 From: Sasha Date: Mon, 28 Aug 2023 12:58:27 +0200 Subject: [PATCH 3/4] decople to a type --- packages/core/src/lib/light_push/index.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index 5f0136f7ee..e3b2294c45 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -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,16 +52,7 @@ class LightPush extends BaseProtocol implements ILightPush { encoder: IEncoder, message: IMessage, pubSubTopic: string - ): Promise< - | { - query: PushRpc; - error: null; - } - | { - query: null; - error: SendError; - } - > { + ): Promise { try { if (!isSizeValid(message.payload)) { log("Failed to send waku light push: message is bigger than 1MB"); From c4c30257c77aecf51bf254687817cc7d8945f4d2 Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 29 Aug 2023 14:17:45 +0200 Subject: [PATCH 4/4] fix error --- packages/core/src/lib/light_push/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index e3b2294c45..ce886410ea 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -101,7 +101,7 @@ class LightPush extends BaseProtocol implements ILightPush { }; } - let error: SendError; + let error: undefined | SendError = undefined; const peer = await this.getPeer(opts?.peerId); const stream = await this.newStream(peer);