@waku/core 0.0.34-b6339f7.0 → 0.0.34-c41b319.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 (80) hide show
  1. package/bundle/base_protocol-CCK9RCtH.js +152 -0
  2. package/bundle/{index-BIW3qNYx.js → index-Db7LxDrL.js} +78 -159
  3. package/bundle/index.js +1882 -158
  4. package/bundle/lib/base_protocol.js +2 -2
  5. package/bundle/lib/message/version_0.js +2 -2
  6. package/bundle/{version_0-CdmZMfkQ.js → version_0-ANFNAdFD.js} +283 -45
  7. package/dist/.tsbuildinfo +1 -1
  8. package/dist/index.d.ts +1 -3
  9. package/dist/index.js +1 -3
  10. package/dist/index.js.map +1 -1
  11. package/dist/lib/base_protocol.d.ts +4 -25
  12. package/dist/lib/base_protocol.js +3 -55
  13. package/dist/lib/base_protocol.js.map +1 -1
  14. package/dist/lib/connection_manager/connection_manager.d.ts +118 -0
  15. package/dist/lib/{connection_manager.js → connection_manager/connection_manager.js} +136 -36
  16. package/dist/lib/connection_manager/connection_manager.js.map +1 -0
  17. package/dist/lib/connection_manager/index.d.ts +1 -0
  18. package/dist/lib/connection_manager/index.js +2 -0
  19. package/dist/lib/connection_manager/index.js.map +1 -0
  20. package/dist/lib/{keep_alive_manager.d.ts → connection_manager/keep_alive_manager.d.ts} +4 -2
  21. package/dist/lib/{keep_alive_manager.js → connection_manager/keep_alive_manager.js} +2 -2
  22. package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -0
  23. package/dist/lib/connection_manager/utils.d.ts +7 -0
  24. package/dist/lib/connection_manager/utils.js +22 -0
  25. package/dist/lib/connection_manager/utils.js.map +1 -0
  26. package/dist/lib/filter/filter.d.ts +18 -0
  27. package/dist/lib/filter/filter.js +209 -0
  28. package/dist/lib/filter/filter.js.map +1 -0
  29. package/dist/lib/filter/index.d.ts +1 -18
  30. package/dist/lib/filter/index.js +1 -208
  31. package/dist/lib/filter/index.js.map +1 -1
  32. package/dist/lib/light_push/index.d.ts +1 -15
  33. package/dist/lib/light_push/index.js +1 -143
  34. package/dist/lib/light_push/index.js.map +1 -1
  35. package/dist/lib/light_push/light_push.d.ts +15 -0
  36. package/dist/lib/light_push/light_push.js +144 -0
  37. package/dist/lib/light_push/light_push.js.map +1 -0
  38. package/dist/lib/metadata/index.d.ts +1 -3
  39. package/dist/lib/metadata/index.js +1 -118
  40. package/dist/lib/metadata/index.js.map +1 -1
  41. package/dist/lib/metadata/metadata.d.ts +3 -0
  42. package/dist/lib/metadata/metadata.js +119 -0
  43. package/dist/lib/metadata/metadata.js.map +1 -0
  44. package/dist/lib/store/index.d.ts +1 -9
  45. package/dist/lib/store/index.js +1 -82
  46. package/dist/lib/store/index.js.map +1 -1
  47. package/dist/lib/store/store.d.ts +9 -0
  48. package/dist/lib/store/store.js +83 -0
  49. package/dist/lib/store/store.js.map +1 -0
  50. package/dist/lib/stream_manager/stream_manager.d.ts +2 -2
  51. package/dist/lib/stream_manager/stream_manager.js +16 -17
  52. package/dist/lib/stream_manager/stream_manager.js.map +1 -1
  53. package/package.json +1 -1
  54. package/src/index.ts +1 -4
  55. package/src/lib/base_protocol.ts +3 -92
  56. package/src/lib/{connection_manager.ts → connection_manager/connection_manager.ts} +168 -63
  57. package/src/lib/connection_manager/index.ts +1 -0
  58. package/src/lib/{keep_alive_manager.ts → connection_manager/keep_alive_manager.ts} +7 -3
  59. package/src/lib/connection_manager/utils.ts +25 -0
  60. package/src/lib/filter/filter.ts +315 -0
  61. package/src/lib/filter/index.ts +1 -315
  62. package/src/lib/light_push/index.ts +1 -188
  63. package/src/lib/light_push/light_push.ts +188 -0
  64. package/src/lib/metadata/index.ts +1 -182
  65. package/src/lib/metadata/metadata.ts +182 -0
  66. package/src/lib/store/index.ts +1 -136
  67. package/src/lib/store/store.ts +136 -0
  68. package/src/lib/stream_manager/stream_manager.ts +16 -18
  69. package/bundle/base_protocol-sjpC9_Fu.js +0 -283
  70. package/dist/lib/connection_manager.d.ts +0 -62
  71. package/dist/lib/connection_manager.js.map +0 -1
  72. package/dist/lib/filterPeers.d.ts +0 -13
  73. package/dist/lib/filterPeers.js +0 -38
  74. package/dist/lib/filterPeers.js.map +0 -1
  75. package/dist/lib/health_manager.d.ts +0 -14
  76. package/dist/lib/health_manager.js +0 -70
  77. package/dist/lib/health_manager.js.map +0 -1
  78. package/dist/lib/keep_alive_manager.js.map +0 -1
  79. package/src/lib/filterPeers.ts +0 -51
  80. package/src/lib/health_manager.ts +0 -90
@@ -1,188 +1 @@
1
- import type { Peer, Stream } from "@libp2p/interface";
2
- import {
3
- type CoreProtocolResult,
4
- type IBaseProtocolCore,
5
- type IEncoder,
6
- type IMessage,
7
- type Libp2p,
8
- ProtocolError,
9
- PubsubTopic,
10
- type ThisOrThat
11
- } from "@waku/interfaces";
12
- import { PushResponse } from "@waku/proto";
13
- import { isMessageSizeUnderCap } from "@waku/utils";
14
- import { Logger } from "@waku/utils";
15
- import all from "it-all";
16
- import * as lp from "it-length-prefixed";
17
- import { pipe } from "it-pipe";
18
- import { Uint8ArrayList } from "uint8arraylist";
19
-
20
- import { BaseProtocol } from "../base_protocol.js";
21
-
22
- import { PushRpc } from "./push_rpc.js";
23
- import { isRLNResponseError, matchRLNErrorMessage } from "./utils.js";
24
-
25
- const log = new Logger("light-push");
26
-
27
- export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
28
- export { PushResponse };
29
-
30
- type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
31
-
32
- /**
33
- * Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
34
- */
35
- export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
36
- public constructor(
37
- public readonly pubsubTopics: PubsubTopic[],
38
- libp2p: Libp2p
39
- ) {
40
- super(LightPushCodec, libp2p.components, log, pubsubTopics);
41
- }
42
-
43
- private async preparePushMessage(
44
- encoder: IEncoder,
45
- message: IMessage
46
- ): Promise<PreparePushMessageResult> {
47
- try {
48
- if (!message.payload || message.payload.length === 0) {
49
- log.error("Failed to send waku light push: payload is empty");
50
- return { query: null, error: ProtocolError.EMPTY_PAYLOAD };
51
- }
52
-
53
- if (!(await isMessageSizeUnderCap(encoder, message))) {
54
- log.error("Failed to send waku light push: message is bigger than 1MB");
55
- return { query: null, error: ProtocolError.SIZE_TOO_BIG };
56
- }
57
-
58
- const protoMessage = await encoder.toProtoObj(message);
59
- if (!protoMessage) {
60
- log.error("Failed to encode to protoMessage, aborting push");
61
- return {
62
- query: null,
63
- error: ProtocolError.ENCODE_FAILED
64
- };
65
- }
66
-
67
- const query = PushRpc.createRequest(protoMessage, encoder.pubsubTopic);
68
- return { query, error: null };
69
- } catch (error) {
70
- log.error("Failed to prepare push message", error);
71
-
72
- return {
73
- query: null,
74
- error: ProtocolError.GENERIC_FAIL
75
- };
76
- }
77
- }
78
-
79
- public async send(
80
- encoder: IEncoder,
81
- message: IMessage,
82
- peer: Peer
83
- ): Promise<CoreProtocolResult> {
84
- const { query, error: preparationError } = await this.preparePushMessage(
85
- encoder,
86
- message
87
- );
88
-
89
- if (preparationError || !query) {
90
- return {
91
- success: null,
92
- failure: {
93
- error: preparationError,
94
- peerId: peer.id
95
- }
96
- };
97
- }
98
-
99
- let stream: Stream;
100
- try {
101
- stream = await this.getStream(peer);
102
- } catch (error) {
103
- log.error("Failed to get stream", error);
104
- return {
105
- success: null,
106
- failure: {
107
- error: ProtocolError.NO_STREAM_AVAILABLE,
108
- peerId: peer.id
109
- }
110
- };
111
- }
112
-
113
- let res: Uint8ArrayList[] | undefined;
114
- try {
115
- res = await pipe(
116
- [query.encode()],
117
- lp.encode,
118
- stream,
119
- lp.decode,
120
- async (source) => await all(source)
121
- );
122
- } catch (err) {
123
- log.error("Failed to send waku light push request", err);
124
- return {
125
- success: null,
126
- failure: {
127
- error: ProtocolError.GENERIC_FAIL,
128
- peerId: peer.id
129
- }
130
- };
131
- }
132
-
133
- const bytes = new Uint8ArrayList();
134
- res.forEach((chunk) => {
135
- bytes.append(chunk);
136
- });
137
-
138
- let response: PushResponse | undefined;
139
- try {
140
- response = PushRpc.decode(bytes).response;
141
- } catch (err) {
142
- log.error("Failed to decode push reply", err);
143
- return {
144
- success: null,
145
- failure: {
146
- error: ProtocolError.DECODE_FAILED,
147
- peerId: peer.id
148
- }
149
- };
150
- }
151
-
152
- if (!response) {
153
- log.error("Remote peer fault: No response in PushRPC");
154
- return {
155
- success: null,
156
- failure: {
157
- error: ProtocolError.NO_RESPONSE,
158
- peerId: peer.id
159
- }
160
- };
161
- }
162
-
163
- if (isRLNResponseError(response.info)) {
164
- const rlnErrorCase = matchRLNErrorMessage(response.info!);
165
- log.error("Remote peer rejected the message: ", rlnErrorCase);
166
- return {
167
- success: null,
168
- failure: {
169
- error: rlnErrorCase,
170
- peerId: peer.id
171
- }
172
- };
173
- }
174
-
175
- if (!response.isSuccess) {
176
- log.error("Remote peer rejected the message: ", response.info);
177
- return {
178
- success: null,
179
- failure: {
180
- error: ProtocolError.REMOTE_PEER_REJECTED,
181
- peerId: peer.id
182
- }
183
- };
184
- }
185
-
186
- return { success: peer.id, failure: null };
187
- }
188
- }
1
+ export { LightPushCore, LightPushCodec, PushResponse } from "./light_push.js";
@@ -0,0 +1,188 @@
1
+ import type { PeerId, Stream } from "@libp2p/interface";
2
+ import {
3
+ type CoreProtocolResult,
4
+ type IBaseProtocolCore,
5
+ type IEncoder,
6
+ type IMessage,
7
+ type Libp2p,
8
+ ProtocolError,
9
+ PubsubTopic,
10
+ type ThisOrThat
11
+ } from "@waku/interfaces";
12
+ import { PushResponse } from "@waku/proto";
13
+ import { isMessageSizeUnderCap } from "@waku/utils";
14
+ import { Logger } from "@waku/utils";
15
+ import all from "it-all";
16
+ import * as lp from "it-length-prefixed";
17
+ import { pipe } from "it-pipe";
18
+ import { Uint8ArrayList } from "uint8arraylist";
19
+
20
+ import { BaseProtocol } from "../base_protocol.js";
21
+
22
+ import { PushRpc } from "./push_rpc.js";
23
+ import { isRLNResponseError, matchRLNErrorMessage } from "./utils.js";
24
+
25
+ const log = new Logger("light-push");
26
+
27
+ export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
28
+ export { PushResponse };
29
+
30
+ type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
31
+
32
+ /**
33
+ * Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
34
+ */
35
+ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
36
+ public constructor(
37
+ public readonly pubsubTopics: PubsubTopic[],
38
+ libp2p: Libp2p
39
+ ) {
40
+ super(LightPushCodec, libp2p.components, pubsubTopics);
41
+ }
42
+
43
+ private async preparePushMessage(
44
+ encoder: IEncoder,
45
+ message: IMessage
46
+ ): Promise<PreparePushMessageResult> {
47
+ try {
48
+ if (!message.payload || message.payload.length === 0) {
49
+ log.error("Failed to send waku light push: payload is empty");
50
+ return { query: null, error: ProtocolError.EMPTY_PAYLOAD };
51
+ }
52
+
53
+ if (!(await isMessageSizeUnderCap(encoder, message))) {
54
+ log.error("Failed to send waku light push: message is bigger than 1MB");
55
+ return { query: null, error: ProtocolError.SIZE_TOO_BIG };
56
+ }
57
+
58
+ const protoMessage = await encoder.toProtoObj(message);
59
+ if (!protoMessage) {
60
+ log.error("Failed to encode to protoMessage, aborting push");
61
+ return {
62
+ query: null,
63
+ error: ProtocolError.ENCODE_FAILED
64
+ };
65
+ }
66
+
67
+ const query = PushRpc.createRequest(protoMessage, encoder.pubsubTopic);
68
+ return { query, error: null };
69
+ } catch (error) {
70
+ log.error("Failed to prepare push message", error);
71
+
72
+ return {
73
+ query: null,
74
+ error: ProtocolError.GENERIC_FAIL
75
+ };
76
+ }
77
+ }
78
+
79
+ public async send(
80
+ encoder: IEncoder,
81
+ message: IMessage,
82
+ peerId: PeerId
83
+ ): Promise<CoreProtocolResult> {
84
+ const { query, error: preparationError } = await this.preparePushMessage(
85
+ encoder,
86
+ message
87
+ );
88
+
89
+ if (preparationError || !query) {
90
+ return {
91
+ success: null,
92
+ failure: {
93
+ error: preparationError,
94
+ peerId
95
+ }
96
+ };
97
+ }
98
+
99
+ let stream: Stream;
100
+ try {
101
+ stream = await this.getStream(peerId);
102
+ } catch (error) {
103
+ log.error("Failed to get stream", error);
104
+ return {
105
+ success: null,
106
+ failure: {
107
+ error: ProtocolError.NO_STREAM_AVAILABLE,
108
+ peerId: peerId
109
+ }
110
+ };
111
+ }
112
+
113
+ let res: Uint8ArrayList[] | undefined;
114
+ try {
115
+ res = await pipe(
116
+ [query.encode()],
117
+ lp.encode,
118
+ stream,
119
+ lp.decode,
120
+ async (source) => await all(source)
121
+ );
122
+ } catch (err) {
123
+ log.error("Failed to send waku light push request", err);
124
+ return {
125
+ success: null,
126
+ failure: {
127
+ error: ProtocolError.GENERIC_FAIL,
128
+ peerId: peerId
129
+ }
130
+ };
131
+ }
132
+
133
+ const bytes = new Uint8ArrayList();
134
+ res.forEach((chunk) => {
135
+ bytes.append(chunk);
136
+ });
137
+
138
+ let response: PushResponse | undefined;
139
+ try {
140
+ response = PushRpc.decode(bytes).response;
141
+ } catch (err) {
142
+ log.error("Failed to decode push reply", err);
143
+ return {
144
+ success: null,
145
+ failure: {
146
+ error: ProtocolError.DECODE_FAILED,
147
+ peerId: peerId
148
+ }
149
+ };
150
+ }
151
+
152
+ if (!response) {
153
+ log.error("Remote peer fault: No response in PushRPC");
154
+ return {
155
+ success: null,
156
+ failure: {
157
+ error: ProtocolError.NO_RESPONSE,
158
+ peerId: peerId
159
+ }
160
+ };
161
+ }
162
+
163
+ if (isRLNResponseError(response.info)) {
164
+ const rlnErrorCase = matchRLNErrorMessage(response.info!);
165
+ log.error("Remote peer rejected the message: ", rlnErrorCase);
166
+ return {
167
+ success: null,
168
+ failure: {
169
+ error: rlnErrorCase,
170
+ peerId: peerId
171
+ }
172
+ };
173
+ }
174
+
175
+ if (!response.isSuccess) {
176
+ log.error("Remote peer rejected the message: ", response.info);
177
+ return {
178
+ success: null,
179
+ failure: {
180
+ error: ProtocolError.REMOTE_PEER_REJECTED,
181
+ peerId: peerId
182
+ }
183
+ };
184
+ }
185
+
186
+ return { success: peerId, failure: null };
187
+ }
188
+ }
@@ -1,182 +1 @@
1
- import type { PeerId } from "@libp2p/interface";
2
- import { IncomingStreamData } from "@libp2p/interface";
3
- import {
4
- type IMetadata,
5
- type Libp2pComponents,
6
- type MetadataQueryResult,
7
- type PeerIdStr,
8
- ProtocolError,
9
- PubsubTopic,
10
- type ShardInfo
11
- } from "@waku/interfaces";
12
- import { proto_metadata } from "@waku/proto";
13
- import { encodeRelayShard, Logger, pubsubTopicsToShardInfo } from "@waku/utils";
14
- import all from "it-all";
15
- import * as lp from "it-length-prefixed";
16
- import { pipe } from "it-pipe";
17
- import { Uint8ArrayList } from "uint8arraylist";
18
-
19
- import { BaseProtocol } from "../base_protocol.js";
20
-
21
- const log = new Logger("metadata");
22
-
23
- export const MetadataCodec = "/vac/waku/metadata/1.0.0";
24
-
25
- class Metadata extends BaseProtocol implements IMetadata {
26
- private libp2pComponents: Libp2pComponents;
27
- protected handshakesConfirmed: Map<PeerIdStr, ShardInfo> = new Map();
28
-
29
- public constructor(
30
- public pubsubTopics: PubsubTopic[],
31
- libp2p: Libp2pComponents
32
- ) {
33
- super(MetadataCodec, libp2p.components, log, pubsubTopics);
34
- this.libp2pComponents = libp2p;
35
- void libp2p.registrar.handle(MetadataCodec, (streamData) => {
36
- void this.onRequest(streamData);
37
- });
38
- }
39
-
40
- /**
41
- * Make a metadata query to a peer
42
- */
43
- public async query(peerId: PeerId): Promise<MetadataQueryResult> {
44
- const request = proto_metadata.WakuMetadataRequest.encode(
45
- pubsubTopicsToShardInfo(this.pubsubTopics)
46
- );
47
-
48
- const peer = await this.libp2pComponents.peerStore.get(peerId);
49
- if (!peer) {
50
- return {
51
- shardInfo: null,
52
- error: ProtocolError.NO_PEER_AVAILABLE
53
- };
54
- }
55
-
56
- let stream;
57
- try {
58
- stream = await this.getStream(peer);
59
- } catch (error) {
60
- log.error("Failed to get stream", error);
61
- return {
62
- shardInfo: null,
63
- error: ProtocolError.NO_STREAM_AVAILABLE
64
- };
65
- }
66
-
67
- const encodedResponse = await pipe(
68
- [request],
69
- lp.encode,
70
- stream,
71
- lp.decode,
72
- async (source) => await all(source)
73
- );
74
-
75
- const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
76
-
77
- if (error) {
78
- return {
79
- shardInfo: null,
80
- error
81
- };
82
- }
83
-
84
- await this.savePeerShardInfo(peerId, shardInfo);
85
-
86
- return {
87
- shardInfo,
88
- error: null
89
- };
90
- }
91
-
92
- public async confirmOrAttemptHandshake(
93
- peerId: PeerId
94
- ): Promise<MetadataQueryResult> {
95
- const shardInfo = this.handshakesConfirmed.get(peerId.toString());
96
- if (shardInfo) {
97
- return {
98
- shardInfo,
99
- error: null
100
- };
101
- }
102
-
103
- return await this.query(peerId);
104
- }
105
-
106
- /**
107
- * Handle an incoming metadata request
108
- */
109
- private async onRequest(streamData: IncomingStreamData): Promise<void> {
110
- try {
111
- const { stream, connection } = streamData;
112
- const encodedShardInfo = proto_metadata.WakuMetadataResponse.encode(
113
- pubsubTopicsToShardInfo(this.pubsubTopics)
114
- );
115
-
116
- const encodedResponse = await pipe(
117
- [encodedShardInfo],
118
- lp.encode,
119
- stream,
120
- lp.decode,
121
- async (source) => await all(source)
122
- );
123
-
124
- const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
125
-
126
- if (error) {
127
- return;
128
- }
129
-
130
- await this.savePeerShardInfo(connection.remotePeer, shardInfo);
131
- } catch (error) {
132
- log.error("Error handling metadata request", error);
133
- }
134
- }
135
-
136
- private decodeMetadataResponse(
137
- encodedResponse: Uint8ArrayList[]
138
- ): MetadataQueryResult {
139
- const bytes = new Uint8ArrayList();
140
-
141
- encodedResponse.forEach((chunk) => {
142
- bytes.append(chunk);
143
- });
144
- const response = proto_metadata.WakuMetadataResponse.decode(
145
- bytes
146
- ) as ShardInfo;
147
-
148
- if (!response) {
149
- log.error("Error decoding metadata response");
150
- return {
151
- shardInfo: null,
152
- error: ProtocolError.DECODE_FAILED
153
- };
154
- }
155
-
156
- return {
157
- shardInfo: response,
158
- error: null
159
- };
160
- }
161
-
162
- private async savePeerShardInfo(
163
- peerId: PeerId,
164
- shardInfo: ShardInfo
165
- ): Promise<void> {
166
- // add or update the shardInfo to peer store
167
- await this.libp2pComponents.peerStore.merge(peerId, {
168
- metadata: {
169
- shardInfo: encodeRelayShard(shardInfo)
170
- }
171
- });
172
-
173
- this.handshakesConfirmed.set(peerId.toString(), shardInfo);
174
- }
175
- }
176
-
177
- export function wakuMetadata(
178
- pubsubTopics: PubsubTopic[]
179
- ): (components: Libp2pComponents) => IMetadata {
180
- return (components: Libp2pComponents) =>
181
- new Metadata(pubsubTopics, components);
182
- }
1
+ export { wakuMetadata, MetadataCodec } from "./metadata.js";