@waku/core 0.0.36-f911bf8.0 → 0.0.36

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 (55) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/bundle/index.js +1143 -610
  3. package/bundle/lib/message/version_0.js +1 -2
  4. package/bundle/{version_0-CiYGrPc2.js → version_0-9DPFjcJG.js} +1586 -10
  5. package/dist/.tsbuildinfo +1 -1
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +1 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/lib/connection_manager/connection_manager.d.ts +2 -1
  10. package/dist/lib/connection_manager/connection_manager.js +16 -8
  11. package/dist/lib/connection_manager/connection_manager.js.map +1 -1
  12. package/dist/lib/filter/filter.d.ts +8 -5
  13. package/dist/lib/filter/filter.js +30 -10
  14. package/dist/lib/filter/filter.js.map +1 -1
  15. package/dist/lib/light_push/light_push.d.ts +4 -3
  16. package/dist/lib/light_push/light_push.js +6 -4
  17. package/dist/lib/light_push/light_push.js.map +1 -1
  18. package/dist/lib/message/version_0.d.ts +3 -4
  19. package/dist/lib/message/version_0.js +1 -4
  20. package/dist/lib/message/version_0.js.map +1 -1
  21. package/dist/lib/message_hash/index.d.ts +1 -0
  22. package/dist/lib/message_hash/index.js +2 -0
  23. package/dist/lib/message_hash/index.js.map +1 -0
  24. package/dist/lib/message_hash/message_hash.d.ts +52 -0
  25. package/dist/lib/message_hash/message_hash.js +84 -0
  26. package/dist/lib/message_hash/message_hash.js.map +1 -0
  27. package/dist/lib/metadata/metadata.js +6 -4
  28. package/dist/lib/metadata/metadata.js.map +1 -1
  29. package/dist/lib/store/rpc.js +16 -10
  30. package/dist/lib/store/rpc.js.map +1 -1
  31. package/dist/lib/store/store.d.ts +4 -3
  32. package/dist/lib/store/store.js +18 -6
  33. package/dist/lib/store/store.js.map +1 -1
  34. package/dist/lib/stream_manager/stream_manager.d.ts +3 -4
  35. package/dist/lib/stream_manager/stream_manager.js +6 -8
  36. package/dist/lib/stream_manager/stream_manager.js.map +1 -1
  37. package/package.json +125 -1
  38. package/src/index.ts +2 -0
  39. package/src/lib/connection_manager/connection_manager.ts +24 -16
  40. package/src/lib/filter/filter.ts +46 -14
  41. package/src/lib/light_push/light_push.ts +8 -5
  42. package/src/lib/message/version_0.ts +3 -7
  43. package/src/lib/message_hash/index.ts +1 -0
  44. package/src/lib/message_hash/message_hash.ts +106 -0
  45. package/src/lib/metadata/metadata.ts +8 -5
  46. package/src/lib/store/rpc.ts +23 -19
  47. package/src/lib/store/store.ts +21 -6
  48. package/src/lib/stream_manager/stream_manager.ts +8 -6
  49. package/bundle/base_protocol-DvQrudwy.js +0 -152
  50. package/bundle/index-CTo1my9M.js +0 -1543
  51. package/bundle/lib/base_protocol.js +0 -2
  52. package/dist/lib/base_protocol.d.ts +0 -18
  53. package/dist/lib/base_protocol.js +0 -25
  54. package/dist/lib/base_protocol.js.map +0 -1
  55. package/src/lib/base_protocol.ts +0 -44
@@ -14,6 +14,7 @@ export class StoreQueryRequest {
14
14
  public static create(params: QueryRequestParams): StoreQueryRequest {
15
15
  const request = new StoreQueryRequest({
16
16
  ...params,
17
+ contentTopics: params.contentTopics || [],
17
18
  requestId: uuid(),
18
19
  timeStart: params.timeStart
19
20
  ? BigInt(params.timeStart.getTime() * ONE_MILLION)
@@ -27,26 +28,29 @@ export class StoreQueryRequest {
27
28
  : undefined
28
29
  });
29
30
 
30
- // Validate request parameters based on RFC
31
- if (
32
- (params.pubsubTopic && !params.contentTopics) ||
33
- (!params.pubsubTopic && params.contentTopics)
34
- ) {
35
- throw new Error(
36
- "Both pubsubTopic and contentTopics must be set or unset"
37
- );
38
- }
31
+ const isHashQuery = params.messageHashes && params.messageHashes.length > 0;
32
+ const hasContentTopics =
33
+ params.contentTopics && params.contentTopics.length > 0;
34
+ const hasTimeFilter = params.timeStart || params.timeEnd;
39
35
 
40
- if (
41
- params.messageHashes &&
42
- (params.pubsubTopic ||
43
- params.contentTopics ||
44
- params.timeStart ||
45
- params.timeEnd)
46
- ) {
47
- throw new Error(
48
- "Message hash lookup queries cannot include content filter criteria"
49
- );
36
+ if (isHashQuery) {
37
+ if (hasContentTopics || hasTimeFilter) {
38
+ throw new Error(
39
+ "Message hash lookup queries cannot include content filter criteria (contentTopics, timeStart, or timeEnd)"
40
+ );
41
+ }
42
+ } else {
43
+ if (
44
+ (params.pubsubTopic &&
45
+ (!params.contentTopics || params.contentTopics.length === 0)) ||
46
+ (!params.pubsubTopic &&
47
+ params.contentTopics &&
48
+ params.contentTopics.length > 0)
49
+ ) {
50
+ throw new Error(
51
+ "Both pubsubTopic and contentTopics must be set together for content-filtered queries"
52
+ );
53
+ }
50
54
  }
51
55
 
52
56
  return request;
@@ -2,7 +2,6 @@ import type { PeerId } from "@libp2p/interface";
2
2
  import {
3
3
  IDecodedMessage,
4
4
  IDecoder,
5
- IStoreCore,
6
5
  Libp2p,
7
6
  PubsubTopic,
8
7
  QueryRequestParams
@@ -13,7 +12,7 @@ import * as lp from "it-length-prefixed";
13
12
  import { pipe } from "it-pipe";
14
13
  import { Uint8ArrayList } from "uint8arraylist";
15
14
 
16
- import { BaseProtocol } from "../base_protocol.js";
15
+ import { StreamManager } from "../stream_manager/index.js";
17
16
  import { toProtoMessage } from "../to_proto_message.js";
18
17
 
19
18
  import {
@@ -27,12 +26,16 @@ const log = new Logger("store");
27
26
 
28
27
  export const StoreCodec = "/vac/waku/store-query/3.0.0";
29
28
 
30
- export class StoreCore extends BaseProtocol implements IStoreCore {
29
+ export class StoreCore {
30
+ private readonly streamManager: StreamManager;
31
+
32
+ public readonly multicodec = StoreCodec;
33
+
31
34
  public constructor(
32
35
  public readonly pubsubTopics: PubsubTopic[],
33
36
  libp2p: Libp2p
34
37
  ) {
35
- super(StoreCodec, libp2p.components, pubsubTopics);
38
+ this.streamManager = new StreamManager(StoreCodec, libp2p.components);
36
39
  }
37
40
 
38
41
  public async *queryPerPage<T extends IDecodedMessage>(
@@ -40,9 +43,14 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
40
43
  decoders: Map<string, IDecoder<T>>,
41
44
  peerId: PeerId
42
45
  ): AsyncGenerator<Promise<T | undefined>[]> {
46
+ // Only validate decoder content topics for content-filtered queries
47
+ const isHashQuery =
48
+ queryOpts.messageHashes && queryOpts.messageHashes.length > 0;
43
49
  if (
50
+ !isHashQuery &&
51
+ queryOpts.contentTopics &&
44
52
  queryOpts.contentTopics.toString() !==
45
- Array.from(decoders.keys()).toString()
53
+ Array.from(decoders.keys()).toString()
46
54
  ) {
47
55
  throw new Error(
48
56
  "Internal error, the decoders should match the query's content topics"
@@ -56,9 +64,16 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
56
64
  paginationCursor: currentCursor
57
65
  });
58
66
 
67
+ log.info("Sending store query request:", {
68
+ hasMessageHashes: !!queryOpts.messageHashes?.length,
69
+ messageHashCount: queryOpts.messageHashes?.length,
70
+ pubsubTopic: queryOpts.pubsubTopic,
71
+ contentTopics: queryOpts.contentTopics
72
+ });
73
+
59
74
  let stream;
60
75
  try {
61
- stream = await this.getStream(peerId);
76
+ stream = await this.streamManager.getStream(peerId);
62
77
  } catch (e) {
63
78
  log.error("Failed to get stream", e);
64
79
  break;
@@ -1,5 +1,5 @@
1
1
  import type { Peer, PeerId, PeerUpdate, Stream } from "@libp2p/interface";
2
- import type { Libp2p } from "@waku/interfaces";
2
+ import type { Libp2pComponents } from "@waku/interfaces";
3
3
  import { Logger } from "@waku/utils";
4
4
 
5
5
  import { selectOpenConnection } from "./utils.js";
@@ -14,11 +14,13 @@ export class StreamManager {
14
14
 
15
15
  public constructor(
16
16
  private multicodec: string,
17
- private getConnections: Libp2p["getConnections"],
18
- private addEventListener: Libp2p["addEventListener"]
17
+ private readonly libp2p: Libp2pComponents
19
18
  ) {
20
19
  this.log = new Logger(`stream-manager:${multicodec}`);
21
- this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
20
+ this.libp2p.events.addEventListener(
21
+ "peer:update",
22
+ this.handlePeerUpdateStreamPool
23
+ );
22
24
  }
23
25
 
24
26
  public async getStream(peerId: PeerId): Promise<Stream> {
@@ -47,7 +49,7 @@ export class StreamManager {
47
49
  }
48
50
 
49
51
  private async createStream(peerId: PeerId, retries = 0): Promise<Stream> {
50
- const connections = this.getConnections(peerId);
52
+ const connections = this.libp2p.connectionManager.getConnections(peerId);
51
53
  const connection = selectOpenConnection(connections);
52
54
 
53
55
  if (!connection) {
@@ -135,7 +137,7 @@ export class StreamManager {
135
137
  }
136
138
 
137
139
  private getOpenStreamForCodec(peerId: PeerId): Stream | undefined {
138
- const connections = this.getConnections(peerId);
140
+ const connections = this.libp2p.connectionManager.getConnections(peerId);
139
141
  const connection = selectOpenConnection(connections);
140
142
 
141
143
  if (!connection) {
@@ -1,152 +0,0 @@
1
- import { L as Logger } from './index-CTo1my9M.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 };