@waku/core 0.0.29 → 0.0.30-e49e728.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.
- package/bundle/{base_protocol-CrPXdVvO.js → base_protocol-DCOj0QWD.js} +54 -20
- package/bundle/{browser-DoQRY-an.js → browser-zSUobdfj.js} +10 -0
- package/bundle/{index-NYIjIEV5.js → index-BcSodzY4.js} +1 -1
- package/bundle/index.js +151 -31
- package/bundle/lib/base_protocol.js +3 -3
- package/bundle/lib/message/version_0.js +3 -3
- package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
- package/bundle/{version_0-C6GyvOeg.js → version_0-TEIsGmpJ.js} +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/filter/index.d.ts +5 -5
- package/dist/lib/filter/index.js +124 -20
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/light_push/index.d.ts +3 -4
- package/dist/lib/light_push/index.js +3 -3
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/metadata/index.js +11 -1
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/store/index.js +8 -1
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.d.ts +5 -4
- package/dist/lib/stream_manager.js +52 -18
- package/dist/lib/stream_manager.js.map +1 -1
- package/package.json +1 -134
- package/src/lib/filter/index.ts +157 -49
- package/src/lib/light_push/index.ts +14 -19
- package/src/lib/metadata/index.ts +16 -5
- package/src/lib/store/index.ts +7 -1
- package/src/lib/stream_manager.ts +66 -25
package/src/lib/filter/index.ts
CHANGED
@@ -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
|
4
|
-
ContentTopic,
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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<
|
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
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
-
|
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<
|
130
|
-
|
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
|
-
|
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(
|
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
|
-
|
154
|
-
|
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
|
-
|
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<
|
169
|
-
|
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
|
-
|
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("
|
199
|
-
|
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,
|
1
|
+
import type { Peer, Stream } from "@libp2p/interface";
|
2
2
|
import {
|
3
|
-
|
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
|
-
|
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 =
|
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<
|
85
|
+
): Promise<CoreProtocolResult> {
|
88
86
|
const { query, error: preparationError } = await this.preparePushMessage(
|
89
87
|
encoder,
|
90
88
|
message
|
@@ -100,18 +98,15 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
100
98
|
};
|
101
99
|
}
|
102
100
|
|
103
|
-
let stream: Stream
|
101
|
+
let stream: Stream;
|
104
102
|
try {
|
105
103
|
stream = await this.getStream(peer);
|
106
|
-
} catch (
|
107
|
-
log.error(
|
108
|
-
`Failed to get a stream for remote peer${peer.id.toString()}`,
|
109
|
-
err
|
110
|
-
);
|
104
|
+
} catch (error) {
|
105
|
+
log.error("Failed to get stream", error);
|
111
106
|
return {
|
112
107
|
success: null,
|
113
108
|
failure: {
|
114
|
-
error: ProtocolError.
|
109
|
+
error: ProtocolError.NO_STREAM_AVAILABLE,
|
115
110
|
peerId: peer.id
|
116
111
|
}
|
117
112
|
};
|
@@ -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<
|
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);
|
@@ -85,7 +85,16 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
85
85
|
};
|
86
86
|
}
|
87
87
|
|
88
|
-
|
88
|
+
let stream;
|
89
|
+
try {
|
90
|
+
stream = await this.getStream(peer);
|
91
|
+
} catch (error) {
|
92
|
+
log.error("Failed to get stream", error);
|
93
|
+
return {
|
94
|
+
shardInfo: null,
|
95
|
+
error: ProtocolError.NO_STREAM_AVAILABLE
|
96
|
+
};
|
97
|
+
}
|
89
98
|
|
90
99
|
const encodedResponse = await pipe(
|
91
100
|
[request],
|
@@ -112,7 +121,9 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
112
121
|
};
|
113
122
|
}
|
114
123
|
|
115
|
-
public async confirmOrAttemptHandshake(
|
124
|
+
public async confirmOrAttemptHandshake(
|
125
|
+
peerId: PeerId
|
126
|
+
): Promise<MetadataQueryResult> {
|
116
127
|
const shardInfo = this.handshakesConfirmed.get(peerId.toString());
|
117
128
|
if (shardInfo) {
|
118
129
|
return {
|
@@ -126,7 +137,7 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
126
137
|
|
127
138
|
private decodeMetadataResponse(
|
128
139
|
encodedResponse: Uint8ArrayList[]
|
129
|
-
):
|
140
|
+
): MetadataQueryResult {
|
130
141
|
const bytes = new Uint8ArrayList();
|
131
142
|
|
132
143
|
encodedResponse.forEach((chunk) => {
|
package/src/lib/store/index.ts
CHANGED
@@ -92,7 +92,13 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
|
|
92
92
|
|
93
93
|
const historyRpcQuery = HistoryRpc.createQuery(queryOpts);
|
94
94
|
|
95
|
-
|
95
|
+
let stream;
|
96
|
+
try {
|
97
|
+
stream = await this.getStream(peer);
|
98
|
+
} catch (e) {
|
99
|
+
log.error("Failed to get stream", e);
|
100
|
+
break;
|
101
|
+
}
|
96
102
|
|
97
103
|
const res = await pipe(
|
98
104
|
[historyRpcQuery.encode()],
|
@@ -1,11 +1,15 @@
|
|
1
1
|
import type { PeerUpdate, Stream } from "@libp2p/interface";
|
2
|
-
import { Peer } from "@libp2p/interface";
|
2
|
+
import type { Peer, PeerId } from "@libp2p/interface";
|
3
3
|
import { Libp2p } from "@waku/interfaces";
|
4
4
|
import { Logger } from "@waku/utils";
|
5
5
|
import { selectConnection } from "@waku/utils/libp2p";
|
6
6
|
|
7
|
+
const CONNECTION_TIMEOUT = 5_000;
|
8
|
+
const RETRY_BACKOFF_BASE = 1_000;
|
9
|
+
const MAX_RETRIES = 3;
|
10
|
+
|
7
11
|
export class StreamManager {
|
8
|
-
private streamPool: Map<string, Promise<Stream | void>>;
|
12
|
+
private readonly streamPool: Map<string, Promise<Stream | void>>;
|
9
13
|
private readonly log: Logger;
|
10
14
|
|
11
15
|
constructor(
|
@@ -14,12 +18,8 @@ export class StreamManager {
|
|
14
18
|
public addEventListener: Libp2p["addEventListener"]
|
15
19
|
) {
|
16
20
|
this.log = new Logger(`stream-manager:${multicodec}`);
|
17
|
-
this.addEventListener(
|
18
|
-
"peer:update",
|
19
|
-
this.handlePeerUpdateStreamPool.bind(this)
|
20
|
-
);
|
21
|
-
this.getStream = this.getStream.bind(this);
|
22
21
|
this.streamPool = new Map();
|
22
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
23
23
|
}
|
24
24
|
|
25
25
|
public async getStream(peer: Peer): Promise<Stream> {
|
@@ -27,47 +27,88 @@ export class StreamManager {
|
|
27
27
|
const streamPromise = this.streamPool.get(peerIdStr);
|
28
28
|
|
29
29
|
if (!streamPromise) {
|
30
|
-
return this.
|
30
|
+
return this.createStream(peer);
|
31
31
|
}
|
32
32
|
|
33
|
-
// We have the stream, let's remove it from the map
|
34
33
|
this.streamPool.delete(peerIdStr);
|
34
|
+
this.prepareStream(peer);
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
try {
|
37
|
+
const stream = await streamPromise;
|
38
|
+
if (stream && stream.status !== "closed") {
|
39
|
+
return stream;
|
40
|
+
}
|
41
|
+
} catch (error) {
|
42
|
+
this.log.warn(`Failed to get stream for ${peerIdStr} -- `, error);
|
43
|
+
this.log.warn("Attempting to create a new stream for the peer");
|
42
44
|
}
|
43
45
|
|
44
|
-
return
|
46
|
+
return this.createStream(peer);
|
45
47
|
}
|
46
48
|
|
47
|
-
private async
|
49
|
+
private async createStream(peer: Peer, retries = 0): Promise<Stream> {
|
48
50
|
const connections = this.getConnections(peer.id);
|
49
51
|
const connection = selectConnection(connections);
|
52
|
+
|
50
53
|
if (!connection) {
|
51
54
|
throw new Error("Failed to get a connection to the peer");
|
52
55
|
}
|
53
|
-
|
56
|
+
|
57
|
+
try {
|
58
|
+
return await connection.newStream(this.multicodec);
|
59
|
+
} catch (error) {
|
60
|
+
if (retries < MAX_RETRIES) {
|
61
|
+
const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries);
|
62
|
+
await new Promise((resolve) => setTimeout(resolve, backoff));
|
63
|
+
return this.createStream(peer, retries + 1);
|
64
|
+
}
|
65
|
+
throw new Error(
|
66
|
+
`Failed to create a new stream for ${peer.id.toString()} -- ` + error
|
67
|
+
);
|
68
|
+
}
|
54
69
|
}
|
55
70
|
|
56
|
-
private
|
57
|
-
const
|
58
|
-
|
71
|
+
private prepareStream(peer: Peer): void {
|
72
|
+
const timeoutPromise = new Promise<void>((resolve) =>
|
73
|
+
setTimeout(resolve, CONNECTION_TIMEOUT)
|
74
|
+
);
|
75
|
+
|
76
|
+
const streamPromise = Promise.race([
|
77
|
+
this.createStream(peer),
|
78
|
+
timeoutPromise.then(() => {
|
79
|
+
throw new Error("Connection timeout");
|
80
|
+
})
|
81
|
+
]).catch((error) => {
|
59
82
|
this.log.error(
|
60
|
-
`Failed to prepare a new stream for ${peer.id.toString()}
|
83
|
+
`Failed to prepare a new stream for ${peer.id.toString()} -- `,
|
84
|
+
error
|
61
85
|
);
|
62
86
|
});
|
87
|
+
|
63
88
|
this.streamPool.set(peer.id.toString(), streamPromise);
|
64
89
|
}
|
65
90
|
|
66
91
|
private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
|
67
|
-
const peer = evt.detail
|
92
|
+
const { peer } = evt.detail;
|
93
|
+
|
68
94
|
if (peer.protocols.includes(this.multicodec)) {
|
69
|
-
this.
|
70
|
-
|
95
|
+
const isConnected = this.isConnectedTo(peer.id);
|
96
|
+
|
97
|
+
if (isConnected) {
|
98
|
+
this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
|
99
|
+
this.prepareStream(peer);
|
100
|
+
} else {
|
101
|
+
const peerIdStr = peer.id.toString();
|
102
|
+
this.streamPool.delete(peerIdStr);
|
103
|
+
this.log.info(
|
104
|
+
`Removed pending stream for disconnected peer ${peerIdStr}`
|
105
|
+
);
|
106
|
+
}
|
71
107
|
}
|
72
108
|
};
|
109
|
+
|
110
|
+
private isConnectedTo(peerId: PeerId): boolean {
|
111
|
+
const connections = this.getConnections(peerId);
|
112
|
+
return connections.some((connection) => connection.status === "open");
|
113
|
+
}
|
73
114
|
}
|