@waku/core 0.0.31-1887f4f.0 → 0.0.31-39f8920.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-WTp5jKZK.js → base_protocol-Dge5_tvU.js} +3 -7
- package/bundle/{index-COT0phC8.js → index-Gts2Ddu_.js} +38 -52
- package/bundle/index.js +214 -193
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-SMRUXDNR.js → version_0-CNRKFufI.js} +154 -308
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +2 -3
- package/dist/lib/base_protocol.js +3 -7
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +1 -1
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/index.d.ts +3 -2
- package/dist/lib/filter/index.js +4 -2
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/health_manager.d.ts +14 -0
- package/dist/lib/health_manager.js +70 -0
- package/dist/lib/health_manager.js.map +1 -0
- package/dist/lib/light_push/index.d.ts +3 -2
- package/dist/lib/light_push/index.js +4 -2
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +2 -2
- package/dist/lib/metadata/index.js +9 -9
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/store/index.d.ts +5 -44
- package/dist/lib/store/index.js +39 -45
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/store/rpc.d.ts +22 -0
- package/dist/lib/store/rpc.js +74 -0
- package/dist/lib/store/rpc.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +2 -2
- package/src/lib/base_protocol.ts +3 -7
- package/src/lib/connection_manager.ts +1 -1
- package/src/lib/filter/index.ts +3 -10
- package/src/lib/health_manager.ts +90 -0
- package/src/lib/light_push/index.ts +6 -9
- package/src/lib/metadata/index.ts +11 -12
- package/src/lib/store/index.ts +53 -93
- package/src/lib/store/rpc.ts +92 -0
- package/dist/lib/store/history_rpc.d.ts +0 -27
- package/dist/lib/store/history_rpc.js +0 -72
- package/dist/lib/store/history_rpc.js.map +0 -1
- package/src/lib/store/history_rpc.ts +0 -93
package/src/lib/base_protocol.ts
CHANGED
@@ -3,10 +3,9 @@ import type { Peer, PeerStore, Stream } from "@libp2p/interface";
|
|
3
3
|
import type {
|
4
4
|
IBaseProtocolCore,
|
5
5
|
Libp2pComponents,
|
6
|
-
ProtocolCreateOptions,
|
7
6
|
PubsubTopic
|
8
7
|
} from "@waku/interfaces";
|
9
|
-
import {
|
8
|
+
import { Logger, pubsubTopicsToShardInfo } from "@waku/utils";
|
10
9
|
import {
|
11
10
|
getConnectedPeersForProtocolAndShard,
|
12
11
|
getPeersForProtocol,
|
@@ -29,8 +28,7 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
29
28
|
public multicodec: string,
|
30
29
|
private components: Libp2pComponents,
|
31
30
|
private log: Logger,
|
32
|
-
public readonly pubsubTopics: PubsubTopic[]
|
33
|
-
private options?: ProtocolCreateOptions
|
31
|
+
public readonly pubsubTopics: PubsubTopic[]
|
34
32
|
) {
|
35
33
|
this.addLibp2pEventListener = components.events.addEventListener.bind(
|
36
34
|
components.events
|
@@ -100,9 +98,7 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
100
98
|
this.components.connectionManager.getConnections(),
|
101
99
|
this.peerStore,
|
102
100
|
[this.multicodec],
|
103
|
-
this.
|
104
|
-
? ensureShardingConfigured(this.options.shardInfo).shardInfo
|
105
|
-
: undefined
|
101
|
+
pubsubTopicsToShardInfo(this.pubsubTopics)
|
106
102
|
);
|
107
103
|
|
108
104
|
// Filter the peers based on discovery & number of peers requested
|
@@ -173,7 +173,7 @@ export class ConnectionManager
|
|
173
173
|
private constructor(
|
174
174
|
libp2p: Libp2p,
|
175
175
|
keepAliveOptions: KeepAliveOptions,
|
176
|
-
|
176
|
+
public readonly configuredPubsubTopics: PubsubTopic[],
|
177
177
|
relay?: IRelay,
|
178
178
|
options?: Partial<ConnectionManagerOptions>
|
179
179
|
) {
|
package/src/lib/filter/index.ts
CHANGED
@@ -5,7 +5,6 @@ import {
|
|
5
5
|
type CoreProtocolResult,
|
6
6
|
type IBaseProtocolCore,
|
7
7
|
type Libp2p,
|
8
|
-
type ProtocolCreateOptions,
|
9
8
|
ProtocolError,
|
10
9
|
type PubsubTopic
|
11
10
|
} from "@waku/interfaces";
|
@@ -38,16 +37,10 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
38
37
|
wakuMessage: WakuMessage,
|
39
38
|
peerIdStr: string
|
40
39
|
) => Promise<void>,
|
41
|
-
|
42
|
-
|
40
|
+
public readonly pubsubTopics: PubsubTopic[],
|
41
|
+
libp2p: Libp2p
|
43
42
|
) {
|
44
|
-
super(
|
45
|
-
FilterCodecs.SUBSCRIBE,
|
46
|
-
libp2p.components,
|
47
|
-
log,
|
48
|
-
options!.pubsubTopics!,
|
49
|
-
options
|
50
|
-
);
|
43
|
+
super(FilterCodecs.SUBSCRIBE, libp2p.components, log, pubsubTopics);
|
51
44
|
|
52
45
|
libp2p
|
53
46
|
.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
|
@@ -0,0 +1,90 @@
|
|
1
|
+
import {
|
2
|
+
HealthStatus,
|
3
|
+
type IHealthManager,
|
4
|
+
NodeHealth,
|
5
|
+
type ProtocolHealth,
|
6
|
+
Protocols
|
7
|
+
} from "@waku/interfaces";
|
8
|
+
|
9
|
+
class HealthManager implements IHealthManager {
|
10
|
+
public static instance: HealthManager;
|
11
|
+
private readonly health: NodeHealth;
|
12
|
+
|
13
|
+
private constructor() {
|
14
|
+
this.health = {
|
15
|
+
overallStatus: HealthStatus.Unhealthy,
|
16
|
+
protocolStatuses: new Map()
|
17
|
+
};
|
18
|
+
}
|
19
|
+
|
20
|
+
public static getInstance(): HealthManager {
|
21
|
+
if (!HealthManager.instance) {
|
22
|
+
HealthManager.instance = new HealthManager();
|
23
|
+
}
|
24
|
+
return HealthManager.instance;
|
25
|
+
}
|
26
|
+
|
27
|
+
public getHealthStatus(): HealthStatus {
|
28
|
+
return this.health.overallStatus;
|
29
|
+
}
|
30
|
+
|
31
|
+
public getProtocolStatus(protocol: Protocols): ProtocolHealth | undefined {
|
32
|
+
return this.health.protocolStatuses.get(protocol);
|
33
|
+
}
|
34
|
+
|
35
|
+
public updateProtocolHealth(
|
36
|
+
multicodec: string,
|
37
|
+
connectedPeers: number
|
38
|
+
): void {
|
39
|
+
const protocol = this.getNameFromMulticodec(multicodec);
|
40
|
+
|
41
|
+
let status: HealthStatus = HealthStatus.Unhealthy;
|
42
|
+
if (connectedPeers == 1) {
|
43
|
+
status = HealthStatus.MinimallyHealthy;
|
44
|
+
} else if (connectedPeers >= 2) {
|
45
|
+
status = HealthStatus.SufficientlyHealthy;
|
46
|
+
}
|
47
|
+
|
48
|
+
this.health.protocolStatuses.set(protocol, {
|
49
|
+
name: protocol,
|
50
|
+
status: status,
|
51
|
+
lastUpdate: new Date()
|
52
|
+
});
|
53
|
+
|
54
|
+
this.updateOverallHealth();
|
55
|
+
}
|
56
|
+
|
57
|
+
private getNameFromMulticodec(multicodec: string): Protocols {
|
58
|
+
let name: Protocols;
|
59
|
+
if (multicodec.includes("filter")) {
|
60
|
+
name = Protocols.Filter;
|
61
|
+
} else if (multicodec.includes("lightpush")) {
|
62
|
+
name = Protocols.LightPush;
|
63
|
+
} else if (multicodec.includes("store")) {
|
64
|
+
name = Protocols.Store;
|
65
|
+
} else {
|
66
|
+
throw new Error(`Unknown protocol: ${multicodec}`);
|
67
|
+
}
|
68
|
+
return name;
|
69
|
+
}
|
70
|
+
|
71
|
+
private updateOverallHealth(): void {
|
72
|
+
const relevantProtocols = [Protocols.LightPush, Protocols.Filter];
|
73
|
+
const statuses = relevantProtocols.map(
|
74
|
+
(p) => this.getProtocolStatus(p)?.status
|
75
|
+
);
|
76
|
+
|
77
|
+
if (statuses.some((status) => status === HealthStatus.Unhealthy)) {
|
78
|
+
this.health.overallStatus = HealthStatus.Unhealthy;
|
79
|
+
} else if (
|
80
|
+
statuses.some((status) => status === HealthStatus.MinimallyHealthy)
|
81
|
+
) {
|
82
|
+
this.health.overallStatus = HealthStatus.MinimallyHealthy;
|
83
|
+
} else {
|
84
|
+
this.health.overallStatus = HealthStatus.SufficientlyHealthy;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
export const getHealthManager = (): HealthManager =>
|
90
|
+
HealthManager.getInstance();
|
@@ -5,8 +5,8 @@ import {
|
|
5
5
|
type IEncoder,
|
6
6
|
type IMessage,
|
7
7
|
type Libp2p,
|
8
|
-
type ProtocolCreateOptions,
|
9
8
|
ProtocolError,
|
9
|
+
PubsubTopic,
|
10
10
|
type ThisOrThat
|
11
11
|
} from "@waku/interfaces";
|
12
12
|
import { PushResponse } from "@waku/proto";
|
@@ -32,14 +32,11 @@ type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
|
|
32
32
|
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
33
33
|
*/
|
34
34
|
export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
35
|
-
public constructor(
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
options!.pubsubTopics!,
|
41
|
-
options
|
42
|
-
);
|
35
|
+
public constructor(
|
36
|
+
public readonly pubsubTopics: PubsubTopic[],
|
37
|
+
libp2p: Libp2p
|
38
|
+
) {
|
39
|
+
super(LightPushCodec, libp2p.components, log, pubsubTopics);
|
43
40
|
}
|
44
41
|
|
45
42
|
private async preparePushMessage(
|
@@ -6,10 +6,11 @@ import {
|
|
6
6
|
type MetadataQueryResult,
|
7
7
|
type PeerIdStr,
|
8
8
|
ProtocolError,
|
9
|
+
PubsubTopic,
|
9
10
|
type ShardInfo
|
10
11
|
} from "@waku/interfaces";
|
11
12
|
import { proto_metadata } from "@waku/proto";
|
12
|
-
import { encodeRelayShard, Logger,
|
13
|
+
import { encodeRelayShard, Logger, pubsubTopicsToShardInfo } from "@waku/utils";
|
13
14
|
import all from "it-all";
|
14
15
|
import * as lp from "it-length-prefixed";
|
15
16
|
import { pipe } from "it-pipe";
|
@@ -26,15 +27,10 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
26
27
|
protected handshakesConfirmed: Map<PeerIdStr, ShardInfo> = new Map();
|
27
28
|
|
28
29
|
public constructor(
|
29
|
-
public
|
30
|
+
public pubsubTopics: PubsubTopic[],
|
30
31
|
libp2p: Libp2pComponents
|
31
32
|
) {
|
32
|
-
super(
|
33
|
-
MetadataCodec,
|
34
|
-
libp2p.components,
|
35
|
-
log,
|
36
|
-
shardInfoToPubsubTopics(shardInfo)
|
37
|
-
);
|
33
|
+
super(MetadataCodec, libp2p.components, log, pubsubTopics);
|
38
34
|
this.libp2pComponents = libp2p;
|
39
35
|
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
|
40
36
|
void this.onRequest(streamData);
|
@@ -45,7 +41,9 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
45
41
|
* Make a metadata query to a peer
|
46
42
|
*/
|
47
43
|
public async query(peerId: PeerId): Promise<MetadataQueryResult> {
|
48
|
-
const request = proto_metadata.WakuMetadataRequest.encode(
|
44
|
+
const request = proto_metadata.WakuMetadataRequest.encode(
|
45
|
+
pubsubTopicsToShardInfo(this.pubsubTopics)
|
46
|
+
);
|
49
47
|
|
50
48
|
const peer = await this.peerStore.get(peerId);
|
51
49
|
if (!peer) {
|
@@ -112,7 +110,7 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
112
110
|
try {
|
113
111
|
const { stream, connection } = streamData;
|
114
112
|
const encodedShardInfo = proto_metadata.WakuMetadataResponse.encode(
|
115
|
-
this.
|
113
|
+
pubsubTopicsToShardInfo(this.pubsubTopics)
|
116
114
|
);
|
117
115
|
|
118
116
|
const encodedResponse = await pipe(
|
@@ -177,7 +175,8 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
177
175
|
}
|
178
176
|
|
179
177
|
export function wakuMetadata(
|
180
|
-
|
178
|
+
pubsubTopics: PubsubTopic[]
|
181
179
|
): (components: Libp2pComponents) => IMetadata {
|
182
|
-
return (components: Libp2pComponents) =>
|
180
|
+
return (components: Libp2pComponents) =>
|
181
|
+
new Metadata(pubsubTopics, components);
|
183
182
|
}
|
package/src/lib/store/index.ts
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
import type { Peer } from "@libp2p/interface";
|
2
2
|
import {
|
3
|
-
Cursor,
|
4
3
|
IDecodedMessage,
|
5
4
|
IDecoder,
|
6
5
|
IStoreCore,
|
7
6
|
Libp2p,
|
8
|
-
|
7
|
+
PubsubTopic,
|
8
|
+
QueryRequestParams
|
9
9
|
} from "@waku/interfaces";
|
10
|
-
import { proto_store as proto } from "@waku/proto";
|
11
10
|
import { Logger } from "@waku/utils";
|
12
11
|
import all from "it-all";
|
13
12
|
import * as lp from "it-length-prefixed";
|
@@ -17,63 +16,27 @@ import { Uint8ArrayList } from "uint8arraylist";
|
|
17
16
|
import { BaseProtocol } from "../base_protocol.js";
|
18
17
|
import { toProtoMessage } from "../to_proto_message.js";
|
19
18
|
|
20
|
-
import {
|
21
|
-
|
22
|
-
|
19
|
+
import {
|
20
|
+
DEFAULT_PAGE_SIZE,
|
21
|
+
MAX_PAGE_SIZE,
|
22
|
+
StoreQueryRequest,
|
23
|
+
StoreQueryResponse
|
24
|
+
} from "./rpc.js";
|
23
25
|
|
24
26
|
const log = new Logger("store");
|
25
27
|
|
26
|
-
export const StoreCodec = "/vac/waku/store/
|
27
|
-
|
28
|
-
export { PageDirection, Params };
|
29
|
-
|
30
|
-
export interface TimeFilter {
|
31
|
-
startTime: Date;
|
32
|
-
endTime: Date;
|
33
|
-
}
|
34
|
-
|
35
|
-
export interface QueryOptions {
|
36
|
-
/**
|
37
|
-
* The direction in which pages are retrieved:
|
38
|
-
* - { @link PageDirection.BACKWARD }: Most recent page first.
|
39
|
-
* - { @link PageDirection.FORWARD }: Oldest page first.
|
40
|
-
*
|
41
|
-
* Note: This does not affect the ordering of messages with the page
|
42
|
-
* (the oldest message is always first).
|
43
|
-
*
|
44
|
-
* @default { @link PageDirection.BACKWARD }
|
45
|
-
*/
|
46
|
-
pageDirection?: PageDirection;
|
47
|
-
/**
|
48
|
-
* The number of message per page.
|
49
|
-
*
|
50
|
-
* @default { @link DefaultPageSize }
|
51
|
-
*/
|
52
|
-
pageSize?: number;
|
53
|
-
/**
|
54
|
-
* Retrieve messages with a timestamp within the provided values.
|
55
|
-
*/
|
56
|
-
timeFilter?: TimeFilter;
|
57
|
-
/**
|
58
|
-
* Cursor as an index to start a query from.
|
59
|
-
* The cursor index will be exclusive (i.e. the message at the cursor index will not be included in the result).
|
60
|
-
* If undefined, the query will start from the beginning or end of the history, depending on the page direction.
|
61
|
-
*/
|
62
|
-
cursor?: Cursor;
|
63
|
-
}
|
28
|
+
export const StoreCodec = "/vac/waku/store-query/3.0.0";
|
64
29
|
|
65
|
-
/**
|
66
|
-
* Implements the [Waku v2 Store protocol](https://rfc.vac.dev/spec/13/).
|
67
|
-
*
|
68
|
-
* The Waku Store protocol can be used to retrieved historical messages.
|
69
|
-
*/
|
70
30
|
export class StoreCore extends BaseProtocol implements IStoreCore {
|
71
|
-
public constructor(
|
72
|
-
|
31
|
+
public constructor(
|
32
|
+
public readonly pubsubTopics: PubsubTopic[],
|
33
|
+
libp2p: Libp2p
|
34
|
+
) {
|
35
|
+
super(StoreCodec, libp2p.components, log, pubsubTopics);
|
73
36
|
}
|
74
37
|
|
75
38
|
public async *queryPerPage<T extends IDecodedMessage>(
|
76
|
-
queryOpts:
|
39
|
+
queryOpts: QueryRequestParams,
|
77
40
|
decoders: Map<string, IDecoder<T>>,
|
78
41
|
peer: Peer
|
79
42
|
): AsyncGenerator<Promise<T | undefined>[]> {
|
@@ -86,11 +49,12 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
|
|
86
49
|
);
|
87
50
|
}
|
88
51
|
|
89
|
-
let currentCursor = queryOpts.
|
52
|
+
let currentCursor = queryOpts.paginationCursor;
|
90
53
|
while (true) {
|
91
|
-
|
92
|
-
|
93
|
-
|
54
|
+
const storeQueryRequest = StoreQueryRequest.create({
|
55
|
+
...queryOpts,
|
56
|
+
paginationCursor: currentCursor
|
57
|
+
});
|
94
58
|
|
95
59
|
let stream;
|
96
60
|
try {
|
@@ -101,7 +65,7 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
|
|
101
65
|
}
|
102
66
|
|
103
67
|
const res = await pipe(
|
104
|
-
[
|
68
|
+
[storeQueryRequest.encode()],
|
105
69
|
lp.encode,
|
106
70
|
stream,
|
107
71
|
lp.decode,
|
@@ -113,61 +77,57 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
|
|
113
77
|
bytes.append(chunk);
|
114
78
|
});
|
115
79
|
|
116
|
-
const
|
117
|
-
|
118
|
-
if (!reply.response) {
|
119
|
-
log.warn("Stopping pagination due to store `response` field missing");
|
120
|
-
break;
|
121
|
-
}
|
122
|
-
|
123
|
-
const response = reply.response as proto.HistoryResponse;
|
80
|
+
const storeQueryResponse = StoreQueryResponse.decode(bytes);
|
124
81
|
|
125
|
-
if (
|
126
|
-
|
82
|
+
if (
|
83
|
+
!storeQueryResponse.statusCode ||
|
84
|
+
storeQueryResponse.statusCode >= 300
|
85
|
+
) {
|
86
|
+
const errorMessage = `Store query failed with status code: ${storeQueryResponse.statusCode}, description: ${storeQueryResponse.statusDesc}`;
|
87
|
+
log.error(errorMessage);
|
88
|
+
throw new Error(errorMessage);
|
127
89
|
}
|
128
90
|
|
129
|
-
if (!
|
130
|
-
log.warn(
|
131
|
-
"Stopping pagination due to store `response.messages` field missing or empty"
|
132
|
-
);
|
91
|
+
if (!storeQueryResponse.messages || !storeQueryResponse.messages.length) {
|
92
|
+
log.warn("Stopping pagination due to empty messages in response");
|
133
93
|
break;
|
134
94
|
}
|
135
95
|
|
136
|
-
log.
|
96
|
+
log.info(
|
97
|
+
`${storeQueryResponse.messages.length} messages retrieved from store`
|
98
|
+
);
|
137
99
|
|
138
|
-
|
139
|
-
|
140
|
-
|
100
|
+
const decodedMessages = storeQueryResponse.messages.map((protoMsg) => {
|
101
|
+
if (!protoMsg.message) {
|
102
|
+
return Promise.resolve(undefined);
|
103
|
+
}
|
104
|
+
const contentTopic = protoMsg.message.contentTopic;
|
105
|
+
if (contentTopic) {
|
141
106
|
const decoder = decoders.get(contentTopic);
|
142
107
|
if (decoder) {
|
143
108
|
return decoder.fromProtoObj(
|
144
|
-
|
145
|
-
toProtoMessage(protoMsg)
|
109
|
+
protoMsg.pubsubTopic || "",
|
110
|
+
toProtoMessage(protoMsg.message)
|
146
111
|
);
|
147
112
|
}
|
148
113
|
}
|
149
114
|
return Promise.resolve(undefined);
|
150
115
|
});
|
151
116
|
|
152
|
-
|
153
|
-
if (typeof nextCursor === "undefined") {
|
154
|
-
// If the server does not return cursor then there is an issue,
|
155
|
-
// Need to abort, or we end up in an infinite loop
|
156
|
-
log.warn(
|
157
|
-
"Stopping pagination due to `response.pagingInfo.cursor` missing from store response"
|
158
|
-
);
|
159
|
-
break;
|
160
|
-
}
|
117
|
+
yield decodedMessages;
|
161
118
|
|
162
|
-
|
119
|
+
if (queryOpts.paginationForward) {
|
120
|
+
currentCursor =
|
121
|
+
storeQueryResponse.messages[storeQueryResponse.messages.length - 1]
|
122
|
+
.messageHash;
|
123
|
+
} else {
|
124
|
+
currentCursor = storeQueryResponse.messages[0].messageHash;
|
125
|
+
}
|
163
126
|
|
164
|
-
const responsePageSize = response.pagingInfo?.pageSize;
|
165
|
-
const queryPageSize = historyRpcQuery.query?.pagingInfo?.pageSize;
|
166
127
|
if (
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
responsePageSize < queryPageSize
|
128
|
+
storeQueryResponse.messages.length > MAX_PAGE_SIZE &&
|
129
|
+
storeQueryResponse.messages.length <
|
130
|
+
(queryOpts.paginationLimit || DEFAULT_PAGE_SIZE)
|
171
131
|
) {
|
172
132
|
break;
|
173
133
|
}
|
@@ -0,0 +1,92 @@
|
|
1
|
+
import { QueryRequestParams } from "@waku/interfaces";
|
2
|
+
import { proto_store as proto } from "@waku/proto";
|
3
|
+
import type { Uint8ArrayList } from "uint8arraylist";
|
4
|
+
import { v4 as uuid } from "uuid";
|
5
|
+
|
6
|
+
// https://github.com/waku-org/nwaku/blob/7205f95cff9f49ca0bb762e8fd0bf56a6a7f3b3b/waku/waku_store/common.nim#L12
|
7
|
+
export const DEFAULT_PAGE_SIZE = 20;
|
8
|
+
export const MAX_PAGE_SIZE = 100;
|
9
|
+
const ONE_MILLION = 1_000000;
|
10
|
+
|
11
|
+
export class StoreQueryRequest {
|
12
|
+
public constructor(public proto: proto.StoreQueryRequest) {}
|
13
|
+
|
14
|
+
public static create(params: QueryRequestParams): StoreQueryRequest {
|
15
|
+
const request = new StoreQueryRequest({
|
16
|
+
...params,
|
17
|
+
requestId: uuid(),
|
18
|
+
timeStart: params.timeStart
|
19
|
+
? BigInt(params.timeStart.getTime() * ONE_MILLION)
|
20
|
+
: undefined,
|
21
|
+
timeEnd: params.timeEnd
|
22
|
+
? BigInt(params.timeEnd.getTime() * ONE_MILLION)
|
23
|
+
: undefined,
|
24
|
+
messageHashes: params.messageHashes || [],
|
25
|
+
paginationLimit: params.paginationLimit
|
26
|
+
? BigInt(params.paginationLimit)
|
27
|
+
: undefined
|
28
|
+
});
|
29
|
+
|
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
|
+
}
|
39
|
+
|
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
|
+
);
|
50
|
+
}
|
51
|
+
|
52
|
+
return request;
|
53
|
+
}
|
54
|
+
|
55
|
+
public static decode(bytes: Uint8ArrayList): StoreQueryRequest {
|
56
|
+
const res = proto.StoreQueryRequest.decode(bytes);
|
57
|
+
return new StoreQueryRequest(res);
|
58
|
+
}
|
59
|
+
|
60
|
+
public encode(): Uint8Array {
|
61
|
+
return proto.StoreQueryRequest.encode(this.proto);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
export class StoreQueryResponse {
|
66
|
+
public constructor(public proto: proto.StoreQueryResponse) {}
|
67
|
+
|
68
|
+
public static decode(bytes: Uint8ArrayList): StoreQueryResponse {
|
69
|
+
const res = proto.StoreQueryResponse.decode(bytes);
|
70
|
+
return new StoreQueryResponse(res);
|
71
|
+
}
|
72
|
+
|
73
|
+
public encode(): Uint8Array {
|
74
|
+
return proto.StoreQueryResponse.encode(this.proto);
|
75
|
+
}
|
76
|
+
|
77
|
+
public get statusCode(): number | undefined {
|
78
|
+
return this.proto.statusCode;
|
79
|
+
}
|
80
|
+
|
81
|
+
public get statusDesc(): string | undefined {
|
82
|
+
return this.proto.statusDesc;
|
83
|
+
}
|
84
|
+
|
85
|
+
public get messages(): proto.WakuMessageKeyValue[] {
|
86
|
+
return this.proto.messages;
|
87
|
+
}
|
88
|
+
|
89
|
+
public get paginationCursor(): Uint8Array | undefined {
|
90
|
+
return this.proto.paginationCursor;
|
91
|
+
}
|
92
|
+
}
|
@@ -1,27 +0,0 @@
|
|
1
|
-
import { proto_store as proto } from "@waku/proto";
|
2
|
-
import type { Uint8ArrayList } from "uint8arraylist";
|
3
|
-
export declare enum PageDirection {
|
4
|
-
BACKWARD = "backward",
|
5
|
-
FORWARD = "forward"
|
6
|
-
}
|
7
|
-
export interface Params {
|
8
|
-
contentTopics: string[];
|
9
|
-
pubsubTopic: string;
|
10
|
-
pageDirection: PageDirection;
|
11
|
-
pageSize: number;
|
12
|
-
startTime?: Date;
|
13
|
-
endTime?: Date;
|
14
|
-
cursor?: proto.Index;
|
15
|
-
}
|
16
|
-
export declare class HistoryRpc {
|
17
|
-
readonly proto: proto.HistoryRpc;
|
18
|
-
private constructor();
|
19
|
-
get query(): proto.HistoryQuery | undefined;
|
20
|
-
get response(): proto.HistoryResponse | undefined;
|
21
|
-
/**
|
22
|
-
* Create History Query.
|
23
|
-
*/
|
24
|
-
static createQuery(params: Params): HistoryRpc;
|
25
|
-
decode(bytes: Uint8ArrayList): HistoryRpc;
|
26
|
-
encode(): Uint8Array;
|
27
|
-
}
|
@@ -1,72 +0,0 @@
|
|
1
|
-
import { proto_store as proto } from "@waku/proto";
|
2
|
-
import { v4 as uuid } from "uuid";
|
3
|
-
const OneMillion = BigInt(1_000_000);
|
4
|
-
export var PageDirection;
|
5
|
-
(function (PageDirection) {
|
6
|
-
PageDirection["BACKWARD"] = "backward";
|
7
|
-
PageDirection["FORWARD"] = "forward";
|
8
|
-
})(PageDirection || (PageDirection = {}));
|
9
|
-
export class HistoryRpc {
|
10
|
-
proto;
|
11
|
-
constructor(proto) {
|
12
|
-
this.proto = proto;
|
13
|
-
}
|
14
|
-
get query() {
|
15
|
-
return this.proto.query;
|
16
|
-
}
|
17
|
-
get response() {
|
18
|
-
return this.proto.response;
|
19
|
-
}
|
20
|
-
/**
|
21
|
-
* Create History Query.
|
22
|
-
*/
|
23
|
-
static createQuery(params) {
|
24
|
-
const contentFilters = params.contentTopics.map((contentTopic) => {
|
25
|
-
return { contentTopic };
|
26
|
-
});
|
27
|
-
const direction = directionToProto(params.pageDirection);
|
28
|
-
const pagingInfo = {
|
29
|
-
pageSize: BigInt(params.pageSize),
|
30
|
-
cursor: params.cursor,
|
31
|
-
direction
|
32
|
-
};
|
33
|
-
let startTime, endTime;
|
34
|
-
if (params.startTime) {
|
35
|
-
// milliseconds 10^-3 to nanoseconds 10^-9
|
36
|
-
startTime = BigInt(params.startTime.valueOf()) * OneMillion;
|
37
|
-
}
|
38
|
-
if (params.endTime) {
|
39
|
-
// milliseconds 10^-3 to nanoseconds 10^-9
|
40
|
-
endTime = BigInt(params.endTime.valueOf()) * OneMillion;
|
41
|
-
}
|
42
|
-
return new HistoryRpc({
|
43
|
-
requestId: uuid(),
|
44
|
-
query: {
|
45
|
-
pubsubTopic: params.pubsubTopic,
|
46
|
-
contentFilters,
|
47
|
-
pagingInfo,
|
48
|
-
startTime,
|
49
|
-
endTime
|
50
|
-
},
|
51
|
-
response: undefined
|
52
|
-
});
|
53
|
-
}
|
54
|
-
decode(bytes) {
|
55
|
-
const res = proto.HistoryRpc.decode(bytes);
|
56
|
-
return new HistoryRpc(res);
|
57
|
-
}
|
58
|
-
encode() {
|
59
|
-
return proto.HistoryRpc.encode(this.proto);
|
60
|
-
}
|
61
|
-
}
|
62
|
-
function directionToProto(pageDirection) {
|
63
|
-
switch (pageDirection) {
|
64
|
-
case PageDirection.BACKWARD:
|
65
|
-
return proto.PagingInfo.Direction.BACKWARD;
|
66
|
-
case PageDirection.FORWARD:
|
67
|
-
return proto.PagingInfo.Direction.FORWARD;
|
68
|
-
default:
|
69
|
-
return proto.PagingInfo.Direction.BACKWARD;
|
70
|
-
}
|
71
|
-
}
|
72
|
-
//# sourceMappingURL=history_rpc.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"history_rpc.js","sourceRoot":"","sources":["../../../src/lib/store/history_rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,MAAM,UAAU,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AAErC,MAAM,CAAN,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,sCAAqB,CAAA;IACrB,oCAAmB,CAAA;AACrB,CAAC,EAHW,aAAa,KAAb,aAAa,QAGxB;AAYD,MAAM,OAAO,UAAU;IACe;IAApC,YAAoC,KAAuB;QAAvB,UAAK,GAAL,KAAK,CAAkB;IAAG,CAAC;IAE/D,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;IAC1B,CAAC;IAED,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;IAC7B,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,WAAW,CAAC,MAAc;QACtC,MAAM,cAAc,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,YAAY,EAAE,EAAE;YAC/D,OAAO,EAAE,YAAY,EAAE,CAAC;QAC1B,CAAC,CAAC,CAAC;QAEH,MAAM,SAAS,GAAG,gBAAgB,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QAEzD,MAAM,UAAU,GAAG;YACjB,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC;YACjC,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,SAAS;SACU,CAAC;QAEtB,IAAI,SAAS,EAAE,OAAO,CAAC;QACvB,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;YACrB,0CAA0C;YAC1C,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,UAAU,CAAC;QAC9D,CAAC;QAED,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACnB,0CAA0C;YAC1C,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,GAAG,UAAU,CAAC;QAC1D,CAAC;QACD,OAAO,IAAI,UAAU,CAAC;YACpB,SAAS,EAAE,IAAI,EAAE;YACjB,KAAK,EAAE;gBACL,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,cAAc;gBACd,UAAU;gBACV,SAAS;gBACT,OAAO;aACR;YACD,QAAQ,EAAE,SAAS;SACpB,CAAC,CAAC;IACL,CAAC;IAEM,MAAM,CAAC,KAAqB;QACjC,MAAM,GAAG,GAAG,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC3C,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAEM,MAAM;QACX,OAAO,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7C,CAAC;CACF;AAED,SAAS,gBAAgB,CACvB,aAA4B;IAE5B,QAAQ,aAAa,EAAE,CAAC;QACtB,KAAK,aAAa,CAAC,QAAQ;YACzB,OAAO,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC;QAC7C,KAAK,aAAa,CAAC,OAAO;YACxB,OAAO,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC;QAC5C;YACE,OAAO,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC;IAC/C,CAAC;AACH,CAAC"}
|