@waku/core 0.0.26 → 0.0.28-070b625.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/bundle/{base_protocol-pDODy0G6.js → base_protocol-D0Zdzb-v.js} +134 -89
  3. package/bundle/{browser-mTOOnVZp.js → browser-DoQRY-an.js} +518 -712
  4. package/bundle/{index-cmONXM-V.js → index-BJwgMx4y.js} +116 -88
  5. package/bundle/index.js +2967 -21667
  6. package/bundle/lib/base_protocol.js +3 -3
  7. package/bundle/lib/message/version_0.js +3 -3
  8. package/bundle/lib/predefined_bootstrap_nodes.js +17 -17
  9. package/bundle/version_0-C6o0DvNW.js +4055 -0
  10. package/dist/.tsbuildinfo +1 -1
  11. package/dist/index.d.ts +3 -6
  12. package/dist/index.js +3 -6
  13. package/dist/index.js.map +1 -1
  14. package/dist/lib/base_protocol.d.ts +15 -13
  15. package/dist/lib/base_protocol.js +35 -22
  16. package/dist/lib/base_protocol.js.map +1 -1
  17. package/dist/lib/connection_manager.d.ts +2 -2
  18. package/dist/lib/connection_manager.js +16 -6
  19. package/dist/lib/connection_manager.js.map +1 -1
  20. package/dist/lib/filter/index.d.ts +1 -1
  21. package/dist/lib/filter/index.js +144 -82
  22. package/dist/lib/filter/index.js.map +1 -1
  23. package/dist/lib/filterPeers.d.ts +8 -5
  24. package/dist/lib/filterPeers.js +12 -5
  25. package/dist/lib/filterPeers.js.map +1 -1
  26. package/dist/lib/keep_alive_manager.d.ts +2 -3
  27. package/dist/lib/keep_alive_manager.js.map +1 -1
  28. package/dist/lib/light_push/index.d.ts +12 -2
  29. package/dist/lib/light_push/index.js +80 -80
  30. package/dist/lib/light_push/index.js.map +1 -1
  31. package/dist/lib/message/version_0.js +1 -1
  32. package/dist/lib/message/version_0.js.map +1 -1
  33. package/dist/lib/metadata/index.d.ts +2 -2
  34. package/dist/lib/metadata/index.js +58 -16
  35. package/dist/lib/metadata/index.js.map +1 -1
  36. package/dist/lib/predefined_bootstrap_nodes.d.ts +11 -11
  37. package/dist/lib/predefined_bootstrap_nodes.js +16 -16
  38. package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
  39. package/dist/lib/store/history_rpc.js +1 -1
  40. package/dist/lib/store/history_rpc.js.map +1 -1
  41. package/dist/lib/store/index.d.ts +14 -6
  42. package/dist/lib/store/index.js +51 -235
  43. package/dist/lib/store/index.js.map +1 -1
  44. package/dist/lib/stream_manager.d.ts +2 -2
  45. package/dist/lib/stream_manager.js.map +1 -1
  46. package/dist/lib/wait_for_remote_peer.d.ts +1 -1
  47. package/dist/lib/wait_for_remote_peer.js +42 -10
  48. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  49. package/package.json +1 -127
  50. package/src/index.ts +3 -7
  51. package/src/lib/base_protocol.ts +57 -37
  52. package/src/lib/connection_manager.ts +17 -10
  53. package/src/lib/filter/index.ts +234 -136
  54. package/src/lib/filterPeers.ts +15 -7
  55. package/src/lib/keep_alive_manager.ts +2 -3
  56. package/src/lib/light_push/index.ts +104 -124
  57. package/src/lib/metadata/index.ts +92 -30
  58. package/src/lib/predefined_bootstrap_nodes.ts +22 -22
  59. package/src/lib/store/index.ts +79 -344
  60. package/src/lib/stream_manager.ts +2 -3
  61. package/src/lib/wait_for_remote_peer.ts +68 -12
  62. package/bundle/version_0-LQTFNC7k.js +0 -5008
  63. package/dist/lib/waku.d.ts +0 -57
  64. package/dist/lib/waku.js +0 -130
  65. package/dist/lib/waku.js.map +0 -1
  66. package/src/lib/waku.ts +0 -214
@@ -1,57 +0,0 @@
1
- import type { Stream } from "@libp2p/interface/connection";
2
- import { PeerId } from "@libp2p/interface/peer-id";
3
- import { MultiaddrInput } from "@multiformats/multiaddr";
4
- import type { IFilter, ILightPush, IRelay, IStore, Libp2p, PubsubTopic, ShardingParams, Waku } from "@waku/interfaces";
5
- import { Protocols } from "@waku/interfaces";
6
- import { ConnectionManager } from "./connection_manager.js";
7
- export declare const DefaultPingKeepAliveValueSecs: number;
8
- export declare const DefaultRelayKeepAliveValueSecs: number;
9
- export declare const DefaultUserAgent = "js-waku";
10
- export interface WakuOptions {
11
- /**
12
- * Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
13
- * request to each peer after the set number of seconds. Set to 0 to disable.
14
- *
15
- * @default {@link @waku/core.DefaultPingKeepAliveValueSecs}
16
- */
17
- pingKeepAlive?: number;
18
- /**
19
- * Set keep alive frequency in seconds: Waku will send a ping message over
20
- * relay to each peer after the set number of seconds. Set to 0 to disable.
21
- *
22
- * @default {@link @waku/core.DefaultRelayKeepAliveValueSecs}
23
- */
24
- relayKeepAlive?: number;
25
- /**
26
- * Set the user agent string to be used in identification of the node.
27
- * @default {@link @waku/core.DefaultUserAgent}
28
- */
29
- userAgent?: string;
30
- }
31
- export declare class WakuNode implements Waku {
32
- libp2p: Libp2p;
33
- relay?: IRelay;
34
- store?: IStore;
35
- filter?: IFilter;
36
- lightPush?: ILightPush;
37
- connectionManager: ConnectionManager;
38
- readonly pubsubTopics: PubsubTopic[];
39
- constructor(options: WakuOptions, pubsubTopics: string[] | undefined, libp2p: Libp2p, pubsubShardInfo?: ShardingParams, store?: (libp2p: Libp2p) => IStore, lightPush?: (libp2p: Libp2p) => ILightPush, filter?: (libp2p: Libp2p) => IFilter, relay?: (libp2p: Libp2p) => IRelay);
40
- /**
41
- * Dials to the provided peer.
42
- *
43
- * @param peer The peer to dial
44
- * @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
45
- */
46
- dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise<Stream>;
47
- start(): Promise<void>;
48
- stop(): Promise<void>;
49
- isStarted(): boolean;
50
- isConnected(): boolean;
51
- /**
52
- * Return the local multiaddr with peer id on which libp2p is listening.
53
- *
54
- * @throws if libp2p is not listening on localhost.
55
- */
56
- getLocalMultiaddrWithID(): string;
57
- }
package/dist/lib/waku.js DELETED
@@ -1,130 +0,0 @@
1
- import { isPeerId } from "@libp2p/interface/peer-id";
2
- import { multiaddr } from "@multiformats/multiaddr";
3
- import { DefaultPubsubTopic, Protocols } from "@waku/interfaces";
4
- import { Logger, shardInfoToPubsubTopics } from "@waku/utils";
5
- import { ConnectionManager } from "./connection_manager.js";
6
- export const DefaultPingKeepAliveValueSecs = 5 * 60;
7
- export const DefaultRelayKeepAliveValueSecs = 5 * 60;
8
- export const DefaultUserAgent = "js-waku";
9
- const log = new Logger("waku");
10
- export class WakuNode {
11
- libp2p;
12
- relay;
13
- store;
14
- filter;
15
- lightPush;
16
- connectionManager;
17
- pubsubTopics;
18
- constructor(options, pubsubTopics = [], libp2p, pubsubShardInfo, store, lightPush, filter, relay) {
19
- if (!pubsubShardInfo) {
20
- this.pubsubTopics =
21
- pubsubTopics.length > 0 ? pubsubTopics : [DefaultPubsubTopic];
22
- }
23
- else {
24
- this.pubsubTopics = shardInfoToPubsubTopics(pubsubShardInfo);
25
- }
26
- this.libp2p = libp2p;
27
- if (store) {
28
- this.store = store(libp2p);
29
- }
30
- if (filter) {
31
- this.filter = filter(libp2p);
32
- }
33
- if (lightPush) {
34
- this.lightPush = lightPush(libp2p);
35
- }
36
- if (relay) {
37
- this.relay = relay(libp2p);
38
- }
39
- const pingKeepAlive = options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
40
- const relayKeepAlive = this.relay
41
- ? options.relayKeepAlive || DefaultRelayKeepAliveValueSecs
42
- : 0;
43
- const peerId = this.libp2p.peerId.toString();
44
- this.connectionManager = ConnectionManager.create(peerId, libp2p, { pingKeepAlive, relayKeepAlive }, this.pubsubTopics, this.relay);
45
- log.info("Waku node created", peerId, `relay: ${!!this.relay}, store: ${!!this.store}, light push: ${!!this
46
- .lightPush}, filter: ${!!this.filter}`);
47
- }
48
- /**
49
- * Dials to the provided peer.
50
- *
51
- * @param peer The peer to dial
52
- * @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
53
- */
54
- async dial(peer, protocols) {
55
- const _protocols = protocols ?? [];
56
- const peerId = mapToPeerIdOrMultiaddr(peer);
57
- if (typeof protocols === "undefined") {
58
- this.relay && _protocols.push(Protocols.Relay);
59
- this.store && _protocols.push(Protocols.Store);
60
- this.filter && _protocols.push(Protocols.Filter);
61
- this.lightPush && _protocols.push(Protocols.LightPush);
62
- }
63
- const codecs = [];
64
- if (_protocols.includes(Protocols.Relay)) {
65
- if (this.relay) {
66
- this.relay.gossipSub.multicodecs.forEach((codec) => codecs.push(codec));
67
- }
68
- else {
69
- log.error("Relay codec not included in dial codec: protocol not mounted locally");
70
- }
71
- }
72
- if (_protocols.includes(Protocols.Store)) {
73
- if (this.store) {
74
- codecs.push(this.store.multicodec);
75
- }
76
- else {
77
- log.error("Store codec not included in dial codec: protocol not mounted locally");
78
- }
79
- }
80
- if (_protocols.includes(Protocols.LightPush)) {
81
- if (this.lightPush) {
82
- codecs.push(this.lightPush.multicodec);
83
- }
84
- else {
85
- log.error("Light Push codec not included in dial codec: protocol not mounted locally");
86
- }
87
- }
88
- if (_protocols.includes(Protocols.Filter)) {
89
- if (this.filter) {
90
- codecs.push(this.filter.multicodec);
91
- }
92
- else {
93
- log.error("Filter codec not included in dial codec: protocol not mounted locally");
94
- }
95
- }
96
- log.info(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
97
- return this.libp2p.dialProtocol(peerId, codecs);
98
- }
99
- async start() {
100
- await this.libp2p.start();
101
- }
102
- async stop() {
103
- this.connectionManager.stop();
104
- await this.libp2p.stop();
105
- }
106
- isStarted() {
107
- return this.libp2p.isStarted();
108
- }
109
- isConnected() {
110
- return this.connectionManager.isConnected();
111
- }
112
- /**
113
- * Return the local multiaddr with peer id on which libp2p is listening.
114
- *
115
- * @throws if libp2p is not listening on localhost.
116
- */
117
- getLocalMultiaddrWithID() {
118
- const localMultiaddr = this.libp2p
119
- .getMultiaddrs()
120
- .find((addr) => addr.toString().match(/127\.0\.0\.1/));
121
- if (!localMultiaddr || localMultiaddr.toString() === "") {
122
- throw "Not listening on localhost";
123
- }
124
- return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
125
- }
126
- }
127
- function mapToPeerIdOrMultiaddr(peerId) {
128
- return isPeerId(peerId) ? peerId : multiaddr(peerId);
129
- }
130
- //# sourceMappingURL=waku.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"waku.js","sourceRoot":"","sources":["../../src/lib/waku.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAU,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,SAAS,EAA6B,MAAM,yBAAyB,CAAC;AAW/E,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,GAAG,EAAE,CAAC;AACpD,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAAC,GAAG,EAAE,CAAC;AACrD,MAAM,CAAC,MAAM,gBAAgB,GAAG,SAAS,CAAC;AAE1C,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC;AAwB/B,MAAM,OAAO,QAAQ;IACZ,MAAM,CAAS;IACf,KAAK,CAAU;IACf,KAAK,CAAU;IACf,MAAM,CAAW;IACjB,SAAS,CAAc;IACvB,iBAAiB,CAAoB;IAC5B,YAAY,CAAgB;IAE5C,YACE,OAAoB,EACpB,eAA8B,EAAE,EAChC,MAAc,EACd,eAAgC,EAChC,KAAkC,EAClC,SAA0C,EAC1C,MAAoC,EACpC,KAAkC;QAElC,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,IAAI,CAAC,YAAY;gBACf,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC;QAClE,CAAC;aAAM,CAAC;YACN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AAE,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;QACzD,IAAI,CAAC,cAAc,IAAI,cAAc,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC;YACxD,MAAM,4BAA4B,CAAC;QACrC,CAAC;QACD,OAAO,cAAc,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;IAClE,CAAC;CACF;AACD,SAAS,sBAAsB,CAC7B,MAA+B;IAE/B,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;AACvD,CAAC"}
package/src/lib/waku.ts DELETED
@@ -1,214 +0,0 @@
1
- import type { Stream } from "@libp2p/interface/connection";
2
- import { isPeerId, PeerId } from "@libp2p/interface/peer-id";
3
- import { multiaddr, Multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
4
- import type {
5
- IFilter,
6
- ILightPush,
7
- IRelay,
8
- IStore,
9
- Libp2p,
10
- PubsubTopic,
11
- ShardingParams,
12
- Waku
13
- } from "@waku/interfaces";
14
- import { DefaultPubsubTopic, Protocols } from "@waku/interfaces";
15
- import { Logger, shardInfoToPubsubTopics } from "@waku/utils";
16
-
17
- import { ConnectionManager } from "./connection_manager.js";
18
-
19
- export const DefaultPingKeepAliveValueSecs = 5 * 60;
20
- export const DefaultRelayKeepAliveValueSecs = 5 * 60;
21
- export const DefaultUserAgent = "js-waku";
22
-
23
- const log = new Logger("waku");
24
-
25
- export interface WakuOptions {
26
- /**
27
- * Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
28
- * request to each peer after the set number of seconds. Set to 0 to disable.
29
- *
30
- * @default {@link @waku/core.DefaultPingKeepAliveValueSecs}
31
- */
32
- pingKeepAlive?: number;
33
- /**
34
- * Set keep alive frequency in seconds: Waku will send a ping message over
35
- * relay to each peer after the set number of seconds. Set to 0 to disable.
36
- *
37
- * @default {@link @waku/core.DefaultRelayKeepAliveValueSecs}
38
- */
39
- relayKeepAlive?: number;
40
- /**
41
- * Set the user agent string to be used in identification of the node.
42
- * @default {@link @waku/core.DefaultUserAgent}
43
- */
44
- userAgent?: string;
45
- }
46
-
47
- export class WakuNode implements Waku {
48
- public libp2p: Libp2p;
49
- public relay?: IRelay;
50
- public store?: IStore;
51
- public filter?: IFilter;
52
- public lightPush?: ILightPush;
53
- public connectionManager: ConnectionManager;
54
- public readonly pubsubTopics: PubsubTopic[];
55
-
56
- constructor(
57
- options: WakuOptions,
58
- pubsubTopics: PubsubTopic[] = [],
59
- libp2p: Libp2p,
60
- pubsubShardInfo?: ShardingParams,
61
- store?: (libp2p: Libp2p) => IStore,
62
- lightPush?: (libp2p: Libp2p) => ILightPush,
63
- filter?: (libp2p: Libp2p) => IFilter,
64
- relay?: (libp2p: Libp2p) => IRelay
65
- ) {
66
- if (!pubsubShardInfo) {
67
- this.pubsubTopics =
68
- pubsubTopics.length > 0 ? pubsubTopics : [DefaultPubsubTopic];
69
- } else {
70
- this.pubsubTopics = shardInfoToPubsubTopics(pubsubShardInfo);
71
- }
72
-
73
- this.libp2p = libp2p;
74
-
75
- if (store) {
76
- this.store = store(libp2p);
77
- }
78
- if (filter) {
79
- this.filter = filter(libp2p);
80
- }
81
- if (lightPush) {
82
- this.lightPush = lightPush(libp2p);
83
- }
84
-
85
- if (relay) {
86
- this.relay = relay(libp2p);
87
- }
88
-
89
- const pingKeepAlive =
90
- options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
91
- const relayKeepAlive = this.relay
92
- ? options.relayKeepAlive || DefaultRelayKeepAliveValueSecs
93
- : 0;
94
-
95
- const peerId = this.libp2p.peerId.toString();
96
-
97
- this.connectionManager = ConnectionManager.create(
98
- peerId,
99
- libp2p,
100
- { pingKeepAlive, relayKeepAlive },
101
- this.pubsubTopics,
102
- this.relay
103
- );
104
-
105
- log.info(
106
- "Waku node created",
107
- peerId,
108
- `relay: ${!!this.relay}, store: ${!!this.store}, light push: ${!!this
109
- .lightPush}, filter: ${!!this.filter}`
110
- );
111
- }
112
-
113
- /**
114
- * Dials to the provided peer.
115
- *
116
- * @param peer The peer to dial
117
- * @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
118
- */
119
- async dial(
120
- peer: PeerId | MultiaddrInput,
121
- protocols?: Protocols[]
122
- ): Promise<Stream> {
123
- const _protocols = protocols ?? [];
124
- const peerId = mapToPeerIdOrMultiaddr(peer);
125
-
126
- if (typeof protocols === "undefined") {
127
- this.relay && _protocols.push(Protocols.Relay);
128
- this.store && _protocols.push(Protocols.Store);
129
- this.filter && _protocols.push(Protocols.Filter);
130
- this.lightPush && _protocols.push(Protocols.LightPush);
131
- }
132
-
133
- const codecs: string[] = [];
134
- if (_protocols.includes(Protocols.Relay)) {
135
- if (this.relay) {
136
- this.relay.gossipSub.multicodecs.forEach((codec: string) =>
137
- codecs.push(codec)
138
- );
139
- } else {
140
- log.error(
141
- "Relay codec not included in dial codec: protocol not mounted locally"
142
- );
143
- }
144
- }
145
- if (_protocols.includes(Protocols.Store)) {
146
- if (this.store) {
147
- codecs.push(this.store.multicodec);
148
- } else {
149
- log.error(
150
- "Store codec not included in dial codec: protocol not mounted locally"
151
- );
152
- }
153
- }
154
- if (_protocols.includes(Protocols.LightPush)) {
155
- if (this.lightPush) {
156
- codecs.push(this.lightPush.multicodec);
157
- } else {
158
- log.error(
159
- "Light Push codec not included in dial codec: protocol not mounted locally"
160
- );
161
- }
162
- }
163
- if (_protocols.includes(Protocols.Filter)) {
164
- if (this.filter) {
165
- codecs.push(this.filter.multicodec);
166
- } else {
167
- log.error(
168
- "Filter codec not included in dial codec: protocol not mounted locally"
169
- );
170
- }
171
- }
172
-
173
- log.info(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
174
-
175
- return this.libp2p.dialProtocol(peerId, codecs);
176
- }
177
-
178
- async start(): Promise<void> {
179
- await this.libp2p.start();
180
- }
181
-
182
- async stop(): Promise<void> {
183
- this.connectionManager.stop();
184
- await this.libp2p.stop();
185
- }
186
-
187
- isStarted(): boolean {
188
- return this.libp2p.isStarted();
189
- }
190
-
191
- isConnected(): boolean {
192
- return this.connectionManager.isConnected();
193
- }
194
-
195
- /**
196
- * Return the local multiaddr with peer id on which libp2p is listening.
197
- *
198
- * @throws if libp2p is not listening on localhost.
199
- */
200
- getLocalMultiaddrWithID(): string {
201
- const localMultiaddr = this.libp2p
202
- .getMultiaddrs()
203
- .find((addr) => addr.toString().match(/127\.0\.0\.1/));
204
- if (!localMultiaddr || localMultiaddr.toString() === "") {
205
- throw "Not listening on localhost";
206
- }
207
- return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
208
- }
209
- }
210
- function mapToPeerIdOrMultiaddr(
211
- peerId: PeerId | MultiaddrInput
212
- ): PeerId | Multiaddr {
213
- return isPeerId(peerId) ? peerId : multiaddr(peerId);
214
- }