@waku/core 0.0.33-f599932.0 → 0.0.34-09108d9.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/CHANGELOG.md +22 -0
- package/bundle/base_protocol-Bp5a9PNG.js +152 -0
- package/bundle/{index-BVysxsMu.js → index-G1eRBjeI.js} +136 -215
- package/bundle/index.js +1963 -621
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-C5ObpJ_0.js → version_0-DJZG2fB2.js} +285 -41
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +3 -23
- package/dist/lib/base_protocol.js +3 -51
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager/connection_manager.d.ts +118 -0
- package/dist/lib/{connection_manager.js → connection_manager/connection_manager.js} +136 -36
- package/dist/lib/connection_manager/connection_manager.js.map +1 -0
- package/dist/lib/connection_manager/index.d.ts +1 -0
- package/dist/lib/connection_manager/index.js +2 -0
- package/dist/lib/connection_manager/index.js.map +1 -0
- package/dist/lib/{keep_alive_manager.d.ts → connection_manager/keep_alive_manager.d.ts} +4 -2
- package/dist/lib/{keep_alive_manager.js → connection_manager/keep_alive_manager.js} +2 -2
- package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -0
- package/dist/lib/connection_manager/utils.d.ts +7 -0
- package/dist/lib/connection_manager/utils.js +22 -0
- package/dist/lib/connection_manager/utils.js.map +1 -0
- package/dist/lib/filter/filter.d.ts +18 -0
- package/dist/lib/filter/filter.js +209 -0
- package/dist/lib/filter/filter.js.map +1 -0
- package/dist/lib/filter/index.d.ts +1 -19
- package/dist/lib/filter/index.js +1 -211
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/light_push/index.d.ts +1 -15
- package/dist/lib/light_push/index.js +1 -143
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/light_push.d.ts +15 -0
- package/dist/lib/light_push/light_push.js +144 -0
- package/dist/lib/light_push/light_push.js.map +1 -0
- package/dist/lib/light_push/utils.d.ts +0 -2
- package/dist/lib/light_push/utils.js +8 -16
- package/dist/lib/light_push/utils.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +1 -3
- package/dist/lib/metadata/index.js +1 -118
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/metadata/metadata.d.ts +3 -0
- package/dist/lib/metadata/metadata.js +119 -0
- package/dist/lib/metadata/metadata.js.map +1 -0
- package/dist/lib/store/index.d.ts +1 -9
- package/dist/lib/store/index.js +1 -82
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/store/store.d.ts +9 -0
- package/dist/lib/store/store.js +83 -0
- package/dist/lib/store/store.js.map +1 -0
- package/dist/lib/stream_manager/stream_manager.d.ts +13 -10
- package/dist/lib/stream_manager/stream_manager.js +88 -58
- package/dist/lib/stream_manager/stream_manager.js.map +1 -1
- package/dist/lib/stream_manager/utils.d.ts +1 -1
- package/dist/lib/stream_manager/utils.js +5 -17
- package/dist/lib/stream_manager/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -7
- package/src/lib/base_protocol.ts +3 -82
- package/src/lib/{connection_manager.ts → connection_manager/connection_manager.ts} +168 -63
- package/src/lib/connection_manager/index.ts +1 -0
- package/src/lib/{keep_alive_manager.ts → connection_manager/keep_alive_manager.ts} +7 -3
- package/src/lib/connection_manager/utils.ts +25 -0
- package/src/lib/filter/filter.ts +315 -0
- package/src/lib/filter/index.ts +1 -323
- package/src/lib/light_push/index.ts +1 -188
- package/src/lib/light_push/light_push.ts +188 -0
- package/src/lib/light_push/utils.ts +12 -20
- package/src/lib/metadata/index.ts +1 -182
- package/src/lib/metadata/metadata.ts +182 -0
- package/src/lib/store/index.ts +1 -136
- package/src/lib/store/store.ts +136 -0
- package/src/lib/stream_manager/stream_manager.ts +125 -69
- package/src/lib/stream_manager/utils.ts +5 -17
- package/bundle/base_protocol-CS0EDeEY.js +0 -260
- package/dist/lib/connection_manager.d.ts +0 -62
- package/dist/lib/connection_manager.js.map +0 -1
- package/dist/lib/filterPeers.d.ts +0 -13
- package/dist/lib/filterPeers.js +0 -38
- package/dist/lib/filterPeers.js.map +0 -1
- package/dist/lib/health_manager.d.ts +0 -14
- package/dist/lib/health_manager.js +0 -70
- package/dist/lib/health_manager.js.map +0 -1
- package/dist/lib/keep_alive_manager.js.map +0 -1
- package/dist/lib/wait_for_remote_peer.d.ts +0 -22
- package/dist/lib/wait_for_remote_peer.js +0 -142
- package/dist/lib/wait_for_remote_peer.js.map +0 -1
- package/src/lib/filterPeers.ts +0 -51
- package/src/lib/health_manager.ts +0 -90
- package/src/lib/wait_for_remote_peer.ts +0 -200
package/CHANGELOG.md
CHANGED
@@ -5,6 +5,28 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The file is maintained by [Release Please](https://github.com/googleapis/release-please) based on [Conventional Commits](https://www.conventionalcommits.org) specification,
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [0.0.33](https://github.com/waku-org/js-waku/compare/core-v0.0.32...core-v0.0.33) (2024-10-16)
|
9
|
+
|
10
|
+
|
11
|
+
### Features
|
12
|
+
|
13
|
+
* **filter:** Enhancing protocol peer management with mutex locks ([#2137](https://github.com/waku-org/js-waku/issues/2137)) ([b2efce5](https://github.com/waku-org/js-waku/commit/b2efce5ec27807325685cc32f9333805e6321ac7))
|
14
|
+
* **lightPush:** Improve peer usage and improve readability ([#2155](https://github.com/waku-org/js-waku/issues/2155)) ([1d68526](https://github.com/waku-org/js-waku/commit/1d68526e724155f76bb786239f475a774115ee97))
|
15
|
+
|
16
|
+
|
17
|
+
### Bug Fixes
|
18
|
+
|
19
|
+
* Peer renewal connection drop & stream management ([#2145](https://github.com/waku-org/js-waku/issues/2145)) ([b93134a](https://github.com/waku-org/js-waku/commit/b93134a517006d3850ef13c1290194767ce40c21))
|
20
|
+
|
21
|
+
|
22
|
+
### Dependencies
|
23
|
+
|
24
|
+
* The following workspace dependencies were updated
|
25
|
+
* dependencies
|
26
|
+
* @waku/enr bumped from ^0.0.26 to ^0.0.27
|
27
|
+
* @waku/interfaces bumped from 0.0.27 to 0.0.28
|
28
|
+
* @waku/utils bumped from 0.0.20 to 0.0.21
|
29
|
+
|
8
30
|
## [0.0.32](https://github.com/waku-org/js-waku/compare/core-v0.0.31...core-v0.0.32) (2024-09-05)
|
9
31
|
|
10
32
|
|
@@ -0,0 +1,152 @@
|
|
1
|
+
import { L as Logger } from './index-G1eRBjeI.js';
|
2
|
+
|
3
|
+
function selectOpenConnection(connections) {
|
4
|
+
return connections
|
5
|
+
.filter((c) => c.status === "open")
|
6
|
+
.sort((left, right) => right.timeline.open - left.timeline.open)
|
7
|
+
.at(0);
|
8
|
+
}
|
9
|
+
|
10
|
+
const STREAM_LOCK_KEY = "consumed";
|
11
|
+
class StreamManager {
|
12
|
+
multicodec;
|
13
|
+
getConnections;
|
14
|
+
addEventListener;
|
15
|
+
log;
|
16
|
+
ongoingCreation = new Set();
|
17
|
+
streamPool = new Map();
|
18
|
+
constructor(multicodec, getConnections, addEventListener) {
|
19
|
+
this.multicodec = multicodec;
|
20
|
+
this.getConnections = getConnections;
|
21
|
+
this.addEventListener = addEventListener;
|
22
|
+
this.log = new Logger(`stream-manager:${multicodec}`);
|
23
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
24
|
+
}
|
25
|
+
async getStream(peerId) {
|
26
|
+
const peerIdStr = peerId.toString();
|
27
|
+
const scheduledStream = this.streamPool.get(peerIdStr);
|
28
|
+
if (scheduledStream) {
|
29
|
+
this.streamPool.delete(peerIdStr);
|
30
|
+
await scheduledStream;
|
31
|
+
}
|
32
|
+
let stream = this.getOpenStreamForCodec(peerId);
|
33
|
+
if (stream) {
|
34
|
+
this.log.info(`Found existing stream peerId=${peerIdStr} multicodec=${this.multicodec}`);
|
35
|
+
this.lockStream(peerIdStr, stream);
|
36
|
+
return stream;
|
37
|
+
}
|
38
|
+
stream = await this.createStream(peerId);
|
39
|
+
this.lockStream(peerIdStr, stream);
|
40
|
+
return stream;
|
41
|
+
}
|
42
|
+
async createStream(peerId, retries = 0) {
|
43
|
+
const connections = this.getConnections(peerId);
|
44
|
+
const connection = selectOpenConnection(connections);
|
45
|
+
if (!connection) {
|
46
|
+
throw new Error(`Failed to get a connection to the peer peerId=${peerId.toString()} multicodec=${this.multicodec}`);
|
47
|
+
}
|
48
|
+
let lastError;
|
49
|
+
let stream;
|
50
|
+
for (let i = 0; i < retries + 1; i++) {
|
51
|
+
try {
|
52
|
+
this.log.info(`Attempting to create a stream for peerId=${peerId.toString()} multicodec=${this.multicodec}`);
|
53
|
+
stream = await connection.newStream(this.multicodec);
|
54
|
+
this.log.info(`Created stream for peerId=${peerId.toString()} multicodec=${this.multicodec}`);
|
55
|
+
break;
|
56
|
+
}
|
57
|
+
catch (error) {
|
58
|
+
lastError = error;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
if (!stream) {
|
62
|
+
throw new Error(`Failed to create a new stream for ${peerId.toString()} -- ` + lastError);
|
63
|
+
}
|
64
|
+
return stream;
|
65
|
+
}
|
66
|
+
async createStreamWithLock(peer) {
|
67
|
+
const peerId = peer.id.toString();
|
68
|
+
if (this.ongoingCreation.has(peerId)) {
|
69
|
+
this.log.info(`Skipping creation of a stream due to lock for peerId=${peerId} multicodec=${this.multicodec}`);
|
70
|
+
return;
|
71
|
+
}
|
72
|
+
try {
|
73
|
+
this.ongoingCreation.add(peerId);
|
74
|
+
await this.createStream(peer.id);
|
75
|
+
}
|
76
|
+
catch (error) {
|
77
|
+
this.log.error(`Failed to createStreamWithLock:`, error);
|
78
|
+
}
|
79
|
+
finally {
|
80
|
+
this.ongoingCreation.delete(peerId);
|
81
|
+
}
|
82
|
+
return;
|
83
|
+
}
|
84
|
+
handlePeerUpdateStreamPool = (evt) => {
|
85
|
+
const { peer } = evt.detail;
|
86
|
+
if (!peer.protocols.includes(this.multicodec)) {
|
87
|
+
return;
|
88
|
+
}
|
89
|
+
const stream = this.getOpenStreamForCodec(peer.id);
|
90
|
+
if (stream) {
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
this.scheduleNewStream(peer);
|
94
|
+
};
|
95
|
+
scheduleNewStream(peer) {
|
96
|
+
this.log.info(`Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
97
|
+
// abandon previous attempt
|
98
|
+
if (this.streamPool.has(peer.id.toString())) {
|
99
|
+
this.streamPool.delete(peer.id.toString());
|
100
|
+
}
|
101
|
+
this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
|
102
|
+
}
|
103
|
+
getOpenStreamForCodec(peerId) {
|
104
|
+
const connections = this.getConnections(peerId);
|
105
|
+
const connection = selectOpenConnection(connections);
|
106
|
+
if (!connection) {
|
107
|
+
return;
|
108
|
+
}
|
109
|
+
const stream = connection.streams.find((s) => s.protocol === this.multicodec);
|
110
|
+
if (!stream) {
|
111
|
+
return;
|
112
|
+
}
|
113
|
+
const isStreamUnusable = ["done", "closed", "closing"].includes(stream.writeStatus || "");
|
114
|
+
if (isStreamUnusable || this.isStreamLocked(stream)) {
|
115
|
+
return;
|
116
|
+
}
|
117
|
+
return stream;
|
118
|
+
}
|
119
|
+
lockStream(peerId, stream) {
|
120
|
+
this.log.info(`Locking stream for peerId:${peerId}\tstreamId:${stream.id}`);
|
121
|
+
stream.metadata[STREAM_LOCK_KEY] = true;
|
122
|
+
}
|
123
|
+
isStreamLocked(stream) {
|
124
|
+
return !!stream.metadata[STREAM_LOCK_KEY];
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
/**
|
129
|
+
* A class with predefined helpers, to be used as a base to implement Waku
|
130
|
+
* Protocols.
|
131
|
+
*/
|
132
|
+
class BaseProtocol {
|
133
|
+
multicodec;
|
134
|
+
components;
|
135
|
+
pubsubTopics;
|
136
|
+
addLibp2pEventListener;
|
137
|
+
removeLibp2pEventListener;
|
138
|
+
streamManager;
|
139
|
+
constructor(multicodec, components, pubsubTopics) {
|
140
|
+
this.multicodec = multicodec;
|
141
|
+
this.components = components;
|
142
|
+
this.pubsubTopics = pubsubTopics;
|
143
|
+
this.addLibp2pEventListener = components.events.addEventListener.bind(components.events);
|
144
|
+
this.removeLibp2pEventListener = components.events.removeEventListener.bind(components.events);
|
145
|
+
this.streamManager = new StreamManager(multicodec, components.connectionManager.getConnections.bind(components.connectionManager), this.addLibp2pEventListener);
|
146
|
+
}
|
147
|
+
async getStream(peerId) {
|
148
|
+
return this.streamManager.getStream(peerId);
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
export { BaseProtocol as B, StreamManager as S };
|