@@ -38,44 +38,78 @@ class LightPush extends BaseProtocol implements ILightPush {
38
38
this . options = options || { } ;
39
39
}
40
40
41
+ private async preparePushMessage (
42
+ encoder : IEncoder ,
43
+ message : IMessage ,
44
+ pubSubTopic : string
45
+ ) : Promise < {
46
+ query : PushRpc | null ;
47
+ error ?: SendError ;
48
+ } > {
49
+ if ( ! isSizeValid ( message . payload ) ) {
50
+ log ( "Failed to send waku light push: message is bigger than 1MB" ) ;
51
+ return { query : null , error : SendError . SIZE_TOO_BIG } ;
52
+ }
53
+
54
+ const protoMessage = await encoder . toProtoObj ( message ) ;
55
+ if ( ! protoMessage ) {
56
+ log ( "Failed to encode to protoMessage, aborting push" ) ;
57
+ return {
58
+ query : null ,
59
+ error : SendError . ENCODE_FAILED
60
+ } ;
61
+ }
62
+
63
+ const query = PushRpc . createRequest ( protoMessage , pubSubTopic ) ;
64
+ return { query } ;
65
+ }
66
+
41
67
async send (
42
68
encoder : IEncoder ,
43
69
message : IMessage ,
44
70
opts ?: ProtocolOptions
45
71
) : Promise < SendResult > {
46
72
const { pubSubTopic = DefaultPubSubTopic } = this . options ;
47
-
48
- const peer = await this . getPeer ( opts ?. peerId ) ;
49
- const stream = await this . newStream ( peer ) ;
50
-
51
73
const recipients : PeerId [ ] = [ ] ;
52
74
let error : undefined | SendError = undefined ;
53
75
76
+ let query : PushRpc | null = null ;
77
+
54
78
try {
55
- if ( ! isSizeValid ( message . payload ) ) {
56
- log ( "Failed to send waku light push: message is bigger that 1MB" ) ;
57
- return {
58
- recipients,
59
- error : SendError . SIZE_TOO_BIG
60
- } ;
61
- }
79
+ const { query : preparedQuery , error : preparationError } =
80
+ await this . preparePushMessage ( encoder , message , pubSubTopic ) ;
62
81
63
- const protoMessage = await encoder . toProtoObj ( message ) ;
64
- if ( ! protoMessage ) {
65
- log ( "Failed to encode to protoMessage, aborting push" ) ;
82
+ if ( preparationError ) {
66
83
return {
67
84
recipients,
68
- error : SendError . ENCODE_FAILED
85
+ error : preparationError
69
86
} ;
70
87
}
71
- const query = PushRpc . createRequest ( protoMessage , pubSubTopic ) ;
88
+
89
+ query = preparedQuery ;
90
+ } catch ( error ) {
91
+ log ( "Failed to prepare push message" , error ) ;
92
+ }
93
+
94
+ if ( ! query ) {
95
+ return {
96
+ recipients,
97
+ error : SendError . GENERIC_FAIL
98
+ } ;
99
+ }
100
+
101
+ const peer = await this . getPeer ( opts ?. peerId ) ;
102
+ const stream = await this . newStream ( peer ) ;
103
+
104
+ try {
72
105
const res = await pipe (
73
106
[ query . encode ( ) ] ,
74
107
lp . encode ,
75
108
stream ,
76
109
lp . decode ,
77
110
async ( source ) => await all ( source )
78
111
) ;
112
+
79
113
try {
80
114
const bytes = new Uint8ArrayList ( ) ;
81
115
res . forEach ( ( chunk ) => {
@@ -98,9 +132,10 @@ class LightPush extends BaseProtocol implements ILightPush {
98
132
log ( "Failed to send waku light push request" , err ) ;
99
133
error = SendError . GENERIC_FAIL ;
100
134
}
135
+
101
136
return {
102
- error ,
103
- recipients
137
+ recipients ,
138
+ error
104
139
} ;
105
140
}
106
141
}
0 commit comments