@waku/core 0.0.31-04bd518.0 → 0.0.31-15400a5.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.
@@ -1,4 +1,4 @@
1
- import { h as bytesToUtf8, T as Tags, L as Logger, i as ensureShardingConfigured } from './index-DnW8ifxc.js';
1
+ import { h as bytesToUtf8, T as Tags, L as Logger, i as ensureShardingConfigured } from './index-D-yd4hdM.js';
2
2
 
3
3
  const decodeRelayShard = (bytes) => {
4
4
  // explicitly converting to Uint8Array to avoid Buffer
@@ -1037,6 +1037,11 @@ var ProtocolError;
1037
1037
  * Ensure that the pubsub topic used for decoder creation is the same as the one used for protocol.
1038
1038
  */
1039
1039
  ProtocolError["TOPIC_DECODER_MISMATCH"] = "Topic decoder mismatch";
1040
+ /**
1041
+ * The topics passed in the decoders do not match each other, or don't exist at all.
1042
+ * Ensure that all the pubsub topics used in the decoders are valid and match each other.
1043
+ */
1044
+ ProtocolError["INVALID_DECODER_TOPICS"] = "Invalid decoder topics";
1040
1045
  /**
1041
1046
  * Failure to find a peer with suitable protocols. This may due to a connection issue.
1042
1047
  * Mitigation can be: retrying after a given time period, display connectivity issue
@@ -1067,12 +1072,6 @@ var ProtocolError;
1067
1072
  ProtocolError["REQUEST_TIMEOUT"] = "Request timeout";
1068
1073
  })(ProtocolError || (ProtocolError = {}));
1069
1074
 
1070
- var PageDirection;
1071
- (function (PageDirection) {
1072
- PageDirection["BACKWARD"] = "backward";
1073
- PageDirection["FORWARD"] = "forward";
1074
- })(PageDirection || (PageDirection = {}));
1075
-
1076
1075
  var Tags;
1077
1076
  (function (Tags) {
1078
1077
  Tags["BOOTSTRAP"] = "bootstrap";
@@ -1091,14 +1090,18 @@ var EConnectionStateEvents;
1091
1090
  EConnectionStateEvents["CONNECTION_STATUS"] = "waku:connection";
1092
1091
  })(EConnectionStateEvents || (EConnectionStateEvents = {}));
1093
1092
 
1094
- /**
1095
- * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
1096
- */
1097
1093
  /**
1098
1094
  * The default cluster ID for The Waku Network
1099
1095
  */
1100
1096
  const DEFAULT_CLUSTER_ID = 1;
1101
1097
 
1098
+ var HealthStatus;
1099
+ (function (HealthStatus) {
1100
+ HealthStatus["Unhealthy"] = "Unhealthy";
1101
+ HealthStatus["MinimallyHealthy"] = "MinimallyHealthy";
1102
+ HealthStatus["SufficientlyHealthy"] = "SufficientlyHealthy";
1103
+ })(HealthStatus || (HealthStatus = {}));
1104
+
1102
1105
  /**
1103
1106
  * Turns a `Uint8Array` into a string.
1104
1107
  *
@@ -1246,7 +1249,9 @@ function contentTopicToPubsubTopic(contentTopic, clusterId = DEFAULT_CLUSTER_ID,
1246
1249
  /**
1247
1250
  * Used when creating encoders/decoders to determine which pubsub topic to use
1248
1251
  */
1249
- function determinePubsubTopic(contentTopic, pubsubTopicShardInfo) {
1252
+ function determinePubsubTopic(contentTopic,
1253
+ // TODO: make it accept ShardInfo https://github.com/waku-org/js-waku/issues/2086
1254
+ pubsubTopicShardInfo) {
1250
1255
  if (typeof pubsubTopicShardInfo == "string") {
1251
1256
  return pubsubTopicShardInfo;
1252
1257
  }
@@ -2052,4 +2057,4 @@ class Logger {
2052
2057
  }
2053
2058
  }
2054
2059
 
2055
- export { EConnectionStateEvents as E, Logger as L, ProtocolError as P, Tags as T, allocUnsafe as a, alloc as b, Protocols as c, EPeersByDiscoveryEvents as d, determinePubsubTopic as e, fromString as f, getDefaultExportFromCjs as g, bytesToUtf8 as h, ensureShardingConfigured as i, pubsubTopicToSingleShardInfo as p, shardInfoToPubsubTopics as s, utf8ToBytes as u };
2060
+ export { EConnectionStateEvents as E, HealthStatus as H, Logger as L, ProtocolError as P, Tags as T, allocUnsafe as a, alloc as b, Protocols as c, EPeersByDiscoveryEvents as d, determinePubsubTopic as e, fromString as f, getDefaultExportFromCjs as g, bytesToUtf8 as h, ensureShardingConfigured as i, pubsubTopicToSingleShardInfo as p, shardInfoToPubsubTopics as s, utf8ToBytes as u };
package/bundle/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { v as version_0, e as encodingLength, a as encode$1, d as decode$1, M as MessagePush, F as FilterSubscribeRequest, b as FilterSubscribeResponse$1, P as PushRpc$1, c as PushResponse, H as HistoryRpc$1, f as PagingInfo, g as HistoryResponse, h as createEncoder, W as WakuMetadataRequest, i as WakuMetadataResponse } from './version_0-WhJLc79v.js';
2
- export { j as createDecoder } from './version_0-WhJLc79v.js';
3
- import { a as allocUnsafe, b as alloc, g as getDefaultExportFromCjs, L as Logger, P as ProtocolError, c as Protocols, u as utf8ToBytes, p as pubsubTopicToSingleShardInfo, E as EConnectionStateEvents, T as Tags, d as EPeersByDiscoveryEvents, s as shardInfoToPubsubTopics } from './index-DnW8ifxc.js';
4
- import { B as BaseProtocol, d as decodeRelayShard, e as encodeRelayShard } from './base_protocol-BM6GbS_d.js';
5
- export { S as StreamManager } from './base_protocol-BM6GbS_d.js';
1
+ import { v as version_0, e as encodingLength, a as encode$1, d as decode$1, M as MessagePush, F as FilterSubscribeRequest, b as FilterSubscribeResponse$1, P as PushRpc$1, c as PushResponse, S as StoreQueryRequest$1, f as StoreQueryResponse$1, g as createEncoder, W as WakuMetadataRequest, h as WakuMetadataResponse } from './version_0-Du1FfEvY.js';
2
+ export { i as createDecoder } from './version_0-Du1FfEvY.js';
3
+ import { a as allocUnsafe, b as alloc, g as getDefaultExportFromCjs, L as Logger, P as ProtocolError, c as Protocols, u as utf8ToBytes, p as pubsubTopicToSingleShardInfo, E as EConnectionStateEvents, T as Tags, d as EPeersByDiscoveryEvents, s as shardInfoToPubsubTopics, H as HealthStatus } from './index-D-yd4hdM.js';
4
+ import { B as BaseProtocol, d as decodeRelayShard, e as encodeRelayShard } from './base_protocol-0VPHaGce.js';
5
+ export { S as StreamManager } from './base_protocol-0VPHaGce.js';
6
6
 
7
7
  const MB = 1024 ** 2;
8
8
  const SIZE_CAP_IN_MB = 1;
@@ -1979,97 +1979,95 @@ function toProtoMessage(wire) {
1979
1979
  return { ...EmptyMessage, ...wire };
1980
1980
  }
1981
1981
 
1982
- const OneMillion = BigInt(1_000_000);
1983
- var PageDirection;
1984
- (function (PageDirection) {
1985
- PageDirection["BACKWARD"] = "backward";
1986
- PageDirection["FORWARD"] = "forward";
1987
- })(PageDirection || (PageDirection = {}));
1988
- class HistoryRpc {
1982
+ // https://github.com/waku-org/nwaku/blob/7205f95cff9f49ca0bb762e8fd0bf56a6a7f3b3b/waku/waku_store/common.nim#L12
1983
+ const DEFAULT_PAGE_SIZE = 20;
1984
+ const MAX_PAGE_SIZE = 100;
1985
+ const ONE_MILLION = 1_000000;
1986
+ class StoreQueryRequest {
1989
1987
  proto;
1990
1988
  constructor(proto) {
1991
1989
  this.proto = proto;
1992
1990
  }
1993
- get query() {
1994
- return this.proto.query;
1995
- }
1996
- get response() {
1997
- return this.proto.response;
1998
- }
1999
- /**
2000
- * Create History Query.
2001
- */
2002
- static createQuery(params) {
2003
- const contentFilters = params.contentTopics.map((contentTopic) => {
2004
- return { contentTopic };
1991
+ static create(params) {
1992
+ const request = new StoreQueryRequest({
1993
+ ...params,
1994
+ requestId: v4(),
1995
+ timeStart: params.timeStart
1996
+ ? BigInt(params.timeStart.getTime() * ONE_MILLION)
1997
+ : undefined,
1998
+ timeEnd: params.timeEnd
1999
+ ? BigInt(params.timeEnd.getTime() * ONE_MILLION)
2000
+ : undefined,
2001
+ messageHashes: params.messageHashes || [],
2002
+ paginationLimit: params.paginationLimit
2003
+ ? BigInt(params.paginationLimit)
2004
+ : undefined
2005
2005
  });
2006
- const direction = directionToProto(params.pageDirection);
2007
- const pagingInfo = {
2008
- pageSize: BigInt(params.pageSize),
2009
- cursor: params.cursor,
2010
- direction
2011
- };
2012
- let startTime, endTime;
2013
- if (params.startTime) {
2014
- // milliseconds 10^-3 to nanoseconds 10^-9
2015
- startTime = BigInt(params.startTime.valueOf()) * OneMillion;
2006
+ // Validate request parameters based on RFC
2007
+ if ((params.pubsubTopic && !params.contentTopics) ||
2008
+ (!params.pubsubTopic && params.contentTopics)) {
2009
+ throw new Error("Both pubsubTopic and contentTopics must be set or unset");
2016
2010
  }
2017
- if (params.endTime) {
2018
- // milliseconds 10^-3 to nanoseconds 10^-9
2019
- endTime = BigInt(params.endTime.valueOf()) * OneMillion;
2011
+ if (params.messageHashes &&
2012
+ (params.pubsubTopic ||
2013
+ params.contentTopics ||
2014
+ params.timeStart ||
2015
+ params.timeEnd)) {
2016
+ throw new Error("Message hash lookup queries cannot include content filter criteria");
2020
2017
  }
2021
- return new HistoryRpc({
2022
- requestId: v4(),
2023
- query: {
2024
- pubsubTopic: params.pubsubTopic,
2025
- contentFilters,
2026
- pagingInfo,
2027
- startTime,
2028
- endTime
2029
- },
2030
- response: undefined
2031
- });
2018
+ return request;
2032
2019
  }
2033
- decode(bytes) {
2034
- const res = HistoryRpc$1.decode(bytes);
2035
- return new HistoryRpc(res);
2020
+ static decode(bytes) {
2021
+ const res = StoreQueryRequest$1.decode(bytes);
2022
+ return new StoreQueryRequest(res);
2036
2023
  }
2037
2024
  encode() {
2038
- return HistoryRpc$1.encode(this.proto);
2025
+ return StoreQueryRequest$1.encode(this.proto);
2039
2026
  }
2040
2027
  }
2041
- function directionToProto(pageDirection) {
2042
- switch (pageDirection) {
2043
- case PageDirection.BACKWARD:
2044
- return PagingInfo.Direction.BACKWARD;
2045
- case PageDirection.FORWARD:
2046
- return PagingInfo.Direction.FORWARD;
2047
- default:
2048
- return PagingInfo.Direction.BACKWARD;
2028
+ class StoreQueryResponse {
2029
+ proto;
2030
+ constructor(proto) {
2031
+ this.proto = proto;
2032
+ }
2033
+ static decode(bytes) {
2034
+ const res = StoreQueryResponse$1.decode(bytes);
2035
+ return new StoreQueryResponse(res);
2036
+ }
2037
+ encode() {
2038
+ return StoreQueryResponse$1.encode(this.proto);
2039
+ }
2040
+ get statusCode() {
2041
+ return this.proto.statusCode;
2042
+ }
2043
+ get statusDesc() {
2044
+ return this.proto.statusDesc;
2045
+ }
2046
+ get messages() {
2047
+ return this.proto.messages;
2048
+ }
2049
+ get paginationCursor() {
2050
+ return this.proto.paginationCursor;
2049
2051
  }
2050
2052
  }
2051
2053
 
2052
- var HistoryError = HistoryResponse.HistoryError;
2053
2054
  const log$4 = new Logger("store");
2054
- const StoreCodec = "/vac/waku/store/2.0.0-beta4";
2055
- /**
2056
- * Implements the [Waku v2 Store protocol](https://rfc.vac.dev/spec/13/).
2057
- *
2058
- * The Waku Store protocol can be used to retrieved historical messages.
2059
- */
2055
+ const StoreCodec = "/vac/waku/store-query/3.0.0";
2060
2056
  class StoreCore extends BaseProtocol {
2061
2057
  constructor(libp2p, options) {
2062
- super(StoreCodec, libp2p.components, log$4, options.pubsubTopics, options);
2058
+ super(StoreCodec, libp2p.components, log$4, options?.pubsubTopics || [], options);
2063
2059
  }
2064
2060
  async *queryPerPage(queryOpts, decoders, peer) {
2065
2061
  if (queryOpts.contentTopics.toString() !==
2066
2062
  Array.from(decoders.keys()).toString()) {
2067
2063
  throw new Error("Internal error, the decoders should match the query's content topics");
2068
2064
  }
2069
- let currentCursor = queryOpts.cursor;
2065
+ let currentCursor = queryOpts.paginationCursor;
2070
2066
  while (true) {
2071
- queryOpts.cursor = currentCursor;
2072
- const historyRpcQuery = HistoryRpc.createQuery(queryOpts);
2067
+ const storeQueryRequest = StoreQueryRequest.create({
2068
+ ...queryOpts,
2069
+ paginationCursor: currentCursor
2070
+ });
2073
2071
  let stream;
2074
2072
  try {
2075
2073
  stream = await this.getStream(peer);
@@ -2078,50 +2076,48 @@ class StoreCore extends BaseProtocol {
2078
2076
  log$4.error("Failed to get stream", e);
2079
2077
  break;
2080
2078
  }
2081
- const res = await pipe([historyRpcQuery.encode()], encode, stream, decode, async (source) => await all(source));
2079
+ const res = await pipe([storeQueryRequest.encode()], encode, stream, decode, async (source) => await all(source));
2082
2080
  const bytes = new Uint8ArrayList();
2083
2081
  res.forEach((chunk) => {
2084
2082
  bytes.append(chunk);
2085
2083
  });
2086
- const reply = historyRpcQuery.decode(bytes);
2087
- if (!reply.response) {
2088
- log$4.warn("Stopping pagination due to store `response` field missing");
2089
- break;
2090
- }
2091
- const response = reply.response;
2092
- if (response.error && response.error !== HistoryError.NONE) {
2093
- throw "History response contains an Error: " + response.error;
2094
- }
2095
- if (!response.messages || !response.messages.length) {
2096
- log$4.warn("Stopping pagination due to store `response.messages` field missing or empty");
2084
+ const storeQueryResponse = StoreQueryResponse.decode(bytes);
2085
+ if (!storeQueryResponse.statusCode ||
2086
+ storeQueryResponse.statusCode >= 300) {
2087
+ const errorMessage = `Store query failed with status code: ${storeQueryResponse.statusCode}, description: ${storeQueryResponse.statusDesc}`;
2088
+ log$4.error(errorMessage);
2089
+ throw new Error(errorMessage);
2090
+ }
2091
+ if (!storeQueryResponse.messages || !storeQueryResponse.messages.length) {
2092
+ log$4.warn("Stopping pagination due to empty messages in response");
2097
2093
  break;
2098
2094
  }
2099
- log$4.error(`${response.messages.length} messages retrieved from store`);
2100
- yield response.messages.map((protoMsg) => {
2101
- const contentTopic = protoMsg.contentTopic;
2102
- if (typeof contentTopic !== "undefined") {
2095
+ log$4.info(`${storeQueryResponse.messages.length} messages retrieved from store`);
2096
+ const decodedMessages = storeQueryResponse.messages.map((protoMsg) => {
2097
+ if (!protoMsg.message) {
2098
+ return Promise.resolve(undefined);
2099
+ }
2100
+ const contentTopic = protoMsg.message.contentTopic;
2101
+ if (contentTopic) {
2103
2102
  const decoder = decoders.get(contentTopic);
2104
2103
  if (decoder) {
2105
- return decoder.fromProtoObj(queryOpts.pubsubTopic, toProtoMessage(protoMsg));
2104
+ return decoder.fromProtoObj(protoMsg.pubsubTopic || "", toProtoMessage(protoMsg.message));
2106
2105
  }
2107
2106
  }
2108
2107
  return Promise.resolve(undefined);
2109
2108
  });
2110
- const nextCursor = response.pagingInfo?.cursor;
2111
- if (typeof nextCursor === "undefined") {
2112
- // If the server does not return cursor then there is an issue,
2113
- // Need to abort, or we end up in an infinite loop
2114
- log$4.warn("Stopping pagination due to `response.pagingInfo.cursor` missing from store response");
2115
- break;
2109
+ yield decodedMessages;
2110
+ if (queryOpts.paginationForward) {
2111
+ currentCursor =
2112
+ storeQueryResponse.messages[storeQueryResponse.messages.length - 1]
2113
+ .messageHash;
2114
+ }
2115
+ else {
2116
+ currentCursor = storeQueryResponse.messages[0].messageHash;
2116
2117
  }
2117
- currentCursor = nextCursor;
2118
- const responsePageSize = response.pagingInfo?.pageSize;
2119
- const queryPageSize = historyRpcQuery.query?.pagingInfo?.pageSize;
2120
- if (
2121
- // Response page size smaller than query, meaning this is the last page
2122
- responsePageSize &&
2123
- queryPageSize &&
2124
- responsePageSize < queryPageSize) {
2118
+ if (storeQueryResponse.messages.length > MAX_PAGE_SIZE &&
2119
+ storeQueryResponse.messages.length <
2120
+ (queryOpts.paginationLimit || DEFAULT_PAGE_SIZE)) {
2125
2121
  break;
2126
2122
  }
2127
2123
  }
@@ -2130,7 +2126,6 @@ class StoreCore extends BaseProtocol {
2130
2126
 
2131
2127
  var index = /*#__PURE__*/Object.freeze({
2132
2128
  __proto__: null,
2133
- get PageDirection () { return PageDirection; },
2134
2129
  StoreCodec: StoreCodec,
2135
2130
  StoreCore: StoreCore
2136
2131
  });
@@ -3098,6 +3093,75 @@ class ConnectionManager extends TypedEventEmitter {
3098
3093
  }
3099
3094
  }
3100
3095
 
3096
+ class HealthManager {
3097
+ static instance;
3098
+ health;
3099
+ constructor() {
3100
+ this.health = {
3101
+ overallStatus: HealthStatus.Unhealthy,
3102
+ protocolStatuses: new Map()
3103
+ };
3104
+ }
3105
+ static getInstance() {
3106
+ if (!HealthManager.instance) {
3107
+ HealthManager.instance = new HealthManager();
3108
+ }
3109
+ return HealthManager.instance;
3110
+ }
3111
+ getHealthStatus() {
3112
+ return this.health.overallStatus;
3113
+ }
3114
+ getProtocolStatus(protocol) {
3115
+ return this.health.protocolStatuses.get(protocol);
3116
+ }
3117
+ updateProtocolHealth(multicodec, connectedPeers) {
3118
+ const protocol = this.getNameFromMulticodec(multicodec);
3119
+ let status = HealthStatus.Unhealthy;
3120
+ if (connectedPeers == 1) {
3121
+ status = HealthStatus.MinimallyHealthy;
3122
+ }
3123
+ else if (connectedPeers >= 2) {
3124
+ status = HealthStatus.SufficientlyHealthy;
3125
+ }
3126
+ this.health.protocolStatuses.set(protocol, {
3127
+ name: protocol,
3128
+ status: status,
3129
+ lastUpdate: new Date()
3130
+ });
3131
+ this.updateOverallHealth();
3132
+ }
3133
+ getNameFromMulticodec(multicodec) {
3134
+ let name;
3135
+ if (multicodec.includes("filter")) {
3136
+ name = Protocols.Filter;
3137
+ }
3138
+ else if (multicodec.includes("lightpush")) {
3139
+ name = Protocols.LightPush;
3140
+ }
3141
+ else if (multicodec.includes("store")) {
3142
+ name = Protocols.Store;
3143
+ }
3144
+ else {
3145
+ throw new Error(`Unknown protocol: ${multicodec}`);
3146
+ }
3147
+ return name;
3148
+ }
3149
+ updateOverallHealth() {
3150
+ const relevantProtocols = [Protocols.LightPush, Protocols.Filter];
3151
+ const statuses = relevantProtocols.map((p) => this.getProtocolStatus(p)?.status);
3152
+ if (statuses.some((status) => status === HealthStatus.Unhealthy)) {
3153
+ this.health.overallStatus = HealthStatus.Unhealthy;
3154
+ }
3155
+ else if (statuses.some((status) => status === HealthStatus.MinimallyHealthy)) {
3156
+ this.health.overallStatus = HealthStatus.MinimallyHealthy;
3157
+ }
3158
+ else {
3159
+ this.health.overallStatus = HealthStatus.SufficientlyHealthy;
3160
+ }
3161
+ }
3162
+ }
3163
+ const getHealthManager = () => HealthManager.getInstance();
3164
+
3101
3165
  const log = new Logger("metadata");
3102
3166
  const MetadataCodec = "/vac/waku/metadata/1.0.0";
3103
3167
  class Metadata extends BaseProtocol {
@@ -3209,4 +3273,4 @@ function wakuMetadata(shardInfo) {
3209
3273
  return (components) => new Metadata(shardInfo, components);
3210
3274
  }
3211
3275
 
3212
- export { ConnectionManager, FilterCodecs, FilterCore, KeepAliveManager, LightPushCodec, LightPushCore, MetadataCodec, PageDirection, StoreCore, createEncoder, index$3 as message, waitForRemotePeer, wakuMetadata, index$2 as waku_filter, index$1 as waku_light_push, index as waku_store };
3276
+ export { ConnectionManager, FilterCodecs, FilterCore, KeepAliveManager, LightPushCodec, LightPushCore, MetadataCodec, StoreCore, createEncoder, getHealthManager, index$3 as message, waitForRemotePeer, wakuMetadata, index$2 as waku_filter, index$1 as waku_light_push, index as waku_store };
@@ -1,2 +1,2 @@
1
- import '../index-DnW8ifxc.js';
2
- export { B as BaseProtocol } from '../base_protocol-BM6GbS_d.js';
1
+ import '../index-D-yd4hdM.js';
2
+ export { B as BaseProtocol } from '../base_protocol-0VPHaGce.js';
@@ -1,2 +1,2 @@
1
- export { D as DecodedMessage, k as Decoder, E as Encoder, V as Version, j as createDecoder, h as createEncoder, m as proto } from '../../version_0-WhJLc79v.js';
2
- import '../../index-DnW8ifxc.js';
1
+ export { D as DecodedMessage, j as Decoder, E as Encoder, V as Version, i as createDecoder, g as createEncoder, m as proto } from '../../version_0-Du1FfEvY.js';
2
+ import '../../index-D-yd4hdM.js';