@waku/core 0.0.29 → 0.0.30-00c77c6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,20 @@
1
- import type { Peer } from "@libp2p/interface";
1
+ import type { Peer, Stream } from "@libp2p/interface";
2
2
  import type { IncomingStreamData } from "@libp2p/interface-internal";
3
- import type {
4
- ContentTopic,
5
- IBaseProtocolCore,
6
- Libp2p,
7
- ProtocolCreateOptions,
8
- PubsubTopic
3
+ import {
4
+ type ContentTopic,
5
+ type CoreProtocolResult,
6
+ type IBaseProtocolCore,
7
+ type Libp2p,
8
+ type ProtocolCreateOptions,
9
+ ProtocolError,
10
+ type PubsubTopic
9
11
  } from "@waku/interfaces";
10
12
  import { WakuMessage } from "@waku/proto";
11
13
  import { Logger } from "@waku/utils";
12
14
  import all from "it-all";
13
15
  import * as lp from "it-length-prefixed";
14
16
  import { pipe } from "it-pipe";
17
+ import { Uint8ArrayList } from "uint8arraylist";
15
18
 
16
19
  import { BaseProtocol } from "../base_protocol.js";
17
20
 
@@ -90,7 +93,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
90
93
  pubsubTopic: PubsubTopic,
91
94
  peer: Peer,
92
95
  contentTopics: ContentTopic[]
93
- ): Promise<void> {
96
+ ): Promise<CoreProtocolResult> {
94
97
  const stream = await this.getStream(peer);
95
98
 
96
99
  const request = FilterSubscribeRpc.createSubscribeRequest(
@@ -98,45 +101,98 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
98
101
  contentTopics
99
102
  );
100
103
 
101
- const res = await pipe(
102
- [request.encode()],
103
- lp.encode,
104
- stream,
105
- lp.decode,
106
- async (source) => await all(source)
107
- );
108
-
109
- if (!res || !res.length) {
110
- throw Error(
111
- `No response received for request ${request.requestId}: ${res}`
104
+ let res: Uint8ArrayList[] | undefined;
105
+ try {
106
+ res = await pipe(
107
+ [request.encode()],
108
+ lp.encode,
109
+ stream,
110
+ lp.decode,
111
+ async (source) => await all(source)
112
112
  );
113
+ } catch (error) {
114
+ log.error("Failed to send subscribe request", error);
115
+ return {
116
+ success: null,
117
+ failure: {
118
+ error: ProtocolError.GENERIC_FAIL,
119
+ peerId: peer.id
120
+ }
121
+ };
113
122
  }
114
123
 
115
124
  const { statusCode, requestId, statusDesc } =
116
125
  FilterSubscribeResponse.decode(res[0].slice());
117
126
 
118
127
  if (statusCode < 200 || statusCode >= 300) {
119
- throw new Error(
128
+ log.error(
120
129
  `Filter subscribe request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
121
130
  );
131
+ return {
132
+ failure: {
133
+ error: ProtocolError.REMOTE_PEER_REJECTED,
134
+ peerId: peer.id
135
+ },
136
+ success: null
137
+ };
122
138
  }
139
+
140
+ return {
141
+ failure: null,
142
+ success: peer.id
143
+ };
123
144
  }
124
145
 
125
146
  async unsubscribe(
126
147
  pubsubTopic: PubsubTopic,
127
148
  peer: Peer,
128
149
  contentTopics: ContentTopic[]
129
- ): Promise<void> {
130
- const stream = await this.getStream(peer);
150
+ ): Promise<CoreProtocolResult> {
151
+ let stream: Stream | undefined;
152
+ try {
153
+ stream = await this.getStream(peer);
154
+ } catch (error) {
155
+ log.error(
156
+ `Failed to get a stream for remote peer${peer.id.toString()}`,
157
+ error
158
+ );
159
+ return {
160
+ success: null,
161
+ failure: {
162
+ error: ProtocolError.REMOTE_PEER_FAULT,
163
+ peerId: peer.id
164
+ }
165
+ };
166
+ }
167
+
131
168
  const unsubscribeRequest = FilterSubscribeRpc.createUnsubscribeRequest(
132
169
  pubsubTopic,
133
170
  contentTopics
134
171
  );
135
172
 
136
- await pipe([unsubscribeRequest.encode()], lp.encode, stream.sink);
173
+ try {
174
+ await pipe([unsubscribeRequest.encode()], lp.encode, stream.sink);
175
+ } catch (error) {
176
+ log.error("Failed to send unsubscribe request", error);
177
+ return {
178
+ success: null,
179
+ failure: {
180
+ error: ProtocolError.GENERIC_FAIL,
181
+ peerId: peer.id
182
+ }
183
+ };
184
+ }
185
+
186
+ return {
187
+ success: peer.id,
188
+ failure: null
189
+ };
137
190
  }
138
191
 
139
- async unsubscribeAll(pubsubTopic: PubsubTopic, peer: Peer): Promise<void> {
192
+ async unsubscribeAll(
193
+ pubsubTopic: PubsubTopic,
194
+ peer: Peer
195
+ ): Promise<CoreProtocolResult> {
140
196
  const stream = await this.getStream(peer);
141
197
 
142
198
  const request = FilterSubscribeRpc.createUnsubscribeAllRequest(pubsubTopic);
@@ -150,53 +206,105 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
150
206
  );
151
207
 
152
208
  if (!res || !res.length) {
153
- throw Error(
154
- `No response received for request ${request.requestId}: ${res}`
155
- );
209
+ return {
210
+ failure: {
211
+ error: ProtocolError.REMOTE_PEER_FAULT,
212
+ peerId: peer.id
213
+ },
214
+ success: null
215
+ };
156
216
  }
157
217
 
158
218
  const { statusCode, requestId, statusDesc } =
159
219
  FilterSubscribeResponse.decode(res[0].slice());
160
220
 
161
221
  if (statusCode < 200 || statusCode >= 300) {
162
- throw new Error(
222
+ log.error(
163
223
  `Filter unsubscribe all request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
164
224
  );
225
+ return {
226
+ failure: {
227
+ error: ProtocolError.REMOTE_PEER_REJECTED,
228
+ peerId: peer.id
229
+ },
230
+ success: null
231
+ };
165
232
  }
233
+
234
+ return {
235
+ failure: null,
236
+ success: peer.id
237
+ };
166
238
  }
167
239
 
168
- async ping(peer: Peer): Promise<void> {
169
- const stream = await this.getStream(peer);
240
+ async ping(peer: Peer): Promise<CoreProtocolResult> {
241
+ let stream: Stream | undefined;
242
+ try {
243
+ stream = await this.getStream(peer);
244
+ } catch (error) {
245
+ log.error(
246
+ `Failed to get a stream for remote peer${peer.id.toString()}`,
247
+ error
248
+ );
249
+ return {
250
+ success: null,
251
+ failure: {
252
+ error: ProtocolError.REMOTE_PEER_FAULT,
253
+ peerId: peer.id
254
+ }
255
+ };
256
+ }
170
257
 
171
258
  const request = FilterSubscribeRpc.createSubscriberPingRequest();
172
259
 
260
+ let res: Uint8ArrayList[] | undefined;
173
261
  try {
174
- const res = await pipe(
262
+ res = await pipe(
175
263
  [request.encode()],
176
264
  lp.encode,
177
265
  stream,
178
266
  lp.decode,
179
267
  async (source) => await all(source)
180
268
  );
181
-
182
- if (!res || !res.length) {
183
- throw Error(
184
- `No response received for request ${request.requestId}: ${res}`
185
- );
186
- }
187
-
188
- const { statusCode, requestId, statusDesc } =
189
- FilterSubscribeResponse.decode(res[0].slice());
190
-
191
- if (statusCode < 200 || statusCode >= 300) {
192
- throw new Error(
193
- `Filter ping request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
194
- );
195
- }
196
- log.info(`Ping successful for peer ${peer.id.toString()}`);
197
269
  } catch (error) {
198
- log.error("Error pinging: ", error);
199
- throw error; // Rethrow the actual error instead of wrapping it
270
+ log.error("Failed to send ping request", error);
271
+ return {
272
+ success: null,
273
+ failure: {
274
+ error: ProtocolError.GENERIC_FAIL,
275
+ peerId: peer.id
276
+ }
277
+ };
278
+ }
279
+
280
+ if (!res || !res.length) {
281
+ return {
282
+ success: null,
283
+ failure: {
284
+ error: ProtocolError.REMOTE_PEER_FAULT,
285
+ peerId: peer.id
286
+ }
287
+ };
288
+ }
289
+
290
+ const { statusCode, requestId, statusDesc } =
291
+ FilterSubscribeResponse.decode(res[0].slice());
292
+
293
+ if (statusCode < 200 || statusCode >= 300) {
294
+ log.error(
295
+ `Filter ping request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
296
+ );
297
+ return {
298
+ success: null,
299
+ failure: {
300
+ error: ProtocolError.REMOTE_PEER_REJECTED,
301
+ peerId: peer.id
302
+ }
303
+ };
200
304
  }
305
+ return {
306
+ success: peer.id,
307
+ failure: null
308
+ };
201
309
  }
202
310
  }
@@ -1,13 +1,13 @@
1
- import type { Peer, PeerId, Stream } from "@libp2p/interface";
1
+ import type { Peer, Stream } from "@libp2p/interface";
2
2
  import {
3
- Failure,
4
- IBaseProtocolCore,
5
- IEncoder,
6
- IMessage,
7
- Libp2p,
8
- ProtocolCreateOptions,
3
+ type CoreProtocolResult,
4
+ type IBaseProtocolCore,
5
+ type IEncoder,
6
+ type IMessage,
7
+ type Libp2p,
8
+ type ProtocolCreateOptions,
9
9
  ProtocolError,
10
- ProtocolResult
10
+ type ThisOrThat
11
11
  } from "@waku/interfaces";
12
12
  import { PushResponse } from "@waku/proto";
13
13
  import { isMessageSizeUnderCap } from "@waku/utils";
@@ -26,9 +26,7 @@ const log = new Logger("light-push");
26
26
  export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
27
27
  export { PushResponse };
28
28
 
29
- type PreparePushMessageResult = ProtocolResult<"query", PushRpc>;
30
-
31
- type CoreSendResult = ProtocolResult<"success", PeerId, "failure", Failure>;
29
+ type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
32
30
 
33
31
  /**
34
32
  * Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
@@ -84,7 +82,7 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
84
82
  encoder: IEncoder,
85
83
  message: IMessage,
86
84
  peer: Peer
87
- ): Promise<CoreSendResult> {
85
+ ): Promise<CoreProtocolResult> {
88
86
  const { query, error: preparationError } = await this.preparePushMessage(
89
87
  encoder,
90
88
  message
@@ -3,9 +3,9 @@ import { IncomingStreamData } from "@libp2p/interface";
3
3
  import {
4
4
  type IMetadata,
5
5
  type Libp2pComponents,
6
+ type MetadataQueryResult,
6
7
  type PeerIdStr,
7
8
  ProtocolError,
8
- QueryResult,
9
9
  type ShardInfo
10
10
  } from "@waku/interfaces";
11
11
  import { proto_metadata } from "@waku/proto";
@@ -74,7 +74,7 @@ class Metadata extends BaseProtocol implements IMetadata {
74
74
  /**
75
75
  * Make a metadata query to a peer
76
76
  */
77
- async query(peerId: PeerId): Promise<QueryResult> {
77
+ async query(peerId: PeerId): Promise<MetadataQueryResult> {
78
78
  const request = proto_metadata.WakuMetadataRequest.encode(this.shardInfo);
79
79
 
80
80
  const peer = await this.peerStore.get(peerId);
@@ -112,7 +112,9 @@ class Metadata extends BaseProtocol implements IMetadata {
112
112
  };
113
113
  }
114
114
 
115
- public async confirmOrAttemptHandshake(peerId: PeerId): Promise<QueryResult> {
115
+ public async confirmOrAttemptHandshake(
116
+ peerId: PeerId
117
+ ): Promise<MetadataQueryResult> {
116
118
  const shardInfo = this.handshakesConfirmed.get(peerId.toString());
117
119
  if (shardInfo) {
118
120
  return {
@@ -126,7 +128,7 @@ class Metadata extends BaseProtocol implements IMetadata {
126
128
 
127
129
  private decodeMetadataResponse(
128
130
  encodedResponse: Uint8ArrayList[]
129
- ): QueryResult {
131
+ ): MetadataQueryResult {
130
132
  const bytes = new Uint8ArrayList();
131
133
 
132
134
  encodedResponse.forEach((chunk) => {