mirror of https://github.com/waku-org/js-waku.git
feat: lift contentTopics and make shardInfo mandatory for createLight… (#1959)
* feat: lift contentTopics and make shardInfo mandatory for createLightNode * add default shard info and wanring * fix * fix lint * fix * fix types * remove breaking changes
This commit is contained in:
parent
fb34b7262a
commit
5b03709dfe
|
@ -61,10 +61,16 @@ export type ProtocolCreateOptions = {
|
|||
* - WakuRelay to receive, route and send messages,
|
||||
* - WakuLightPush to send messages,
|
||||
* - WakuStore to retrieve messages.
|
||||
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
||||
* See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md) for details.
|
||||
*
|
||||
*/
|
||||
shardInfo?: Partial<ShardingParams>;
|
||||
/**
|
||||
* Content topics are used to determine pubsubTopics
|
||||
* If not provided pubsubTopics will be determined based on shardInfo
|
||||
* See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md) for details.
|
||||
*/
|
||||
contentTopics?: string[];
|
||||
/**
|
||||
* You can pass options to the `Libp2p` instance used by {@link @waku/sdk!WakuNode} using the `libp2p` property.
|
||||
* This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||
|
|
|
@ -11,7 +11,7 @@ export { defaultLibp2p } from "./utils/libp2p.js";
|
|||
export * from "./utils/content_topic.js";
|
||||
export * from "./waku.js";
|
||||
|
||||
export { createLightNode, createNode } from "./light-node/index.js";
|
||||
export { createLightNode } from "./light-node/index.js";
|
||||
export { wakuLightPush } from "./protocols/light_push.js";
|
||||
export { wakuFilter } from "./protocols/filter.js";
|
||||
export { wakuStore } from "./protocols/store.js";
|
||||
|
|
|
@ -8,31 +8,6 @@ import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "../waku.js";
|
|||
|
||||
export { Libp2pComponents };
|
||||
|
||||
/**
|
||||
* Create a Waku node configured to use autosharding or static sharding.
|
||||
*/
|
||||
export async function createNode(
|
||||
options: CreateWakuNodeOptions = { pubsubTopics: [] }
|
||||
): Promise<LightNode> {
|
||||
if (!options.shardInfo) {
|
||||
throw new Error("Shard info must be set");
|
||||
}
|
||||
|
||||
const libp2p = await createLibp2pAndUpdateOptions(options);
|
||||
|
||||
const store = wakuStore(options);
|
||||
const lightPush = wakuLightPush(options);
|
||||
const filter = wakuFilter(options);
|
||||
|
||||
return new WakuNode(
|
||||
options as WakuOptions,
|
||||
libp2p,
|
||||
store,
|
||||
lightPush,
|
||||
filter
|
||||
) as LightNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Waku node that uses Waku Light Push, Filter and Store to send and
|
||||
* receive messages, enabling low resource consumption.
|
||||
|
|
|
@ -16,7 +16,7 @@ import {
|
|||
type ShardInfo
|
||||
} from "@waku/interfaces";
|
||||
import { wakuGossipSub } from "@waku/relay";
|
||||
import { ensureShardingConfigured } from "@waku/utils";
|
||||
import { ensureShardingConfigured, Logger } from "@waku/utils";
|
||||
import { createLibp2p } from "libp2p";
|
||||
|
||||
import {
|
||||
|
@ -35,6 +35,8 @@ type MetadataService = {
|
|||
metadata?: (components: Libp2pComponents) => IMetadata;
|
||||
};
|
||||
|
||||
const logger = new Logger("sdk:create");
|
||||
|
||||
export async function defaultLibp2p(
|
||||
shardInfo?: ShardInfo,
|
||||
wakuGossipSub?: PubsubService["pubsub"],
|
||||
|
@ -88,6 +90,12 @@ export async function defaultLibp2p(
|
|||
export async function createLibp2pAndUpdateOptions(
|
||||
options: CreateWakuNodeOptions
|
||||
): Promise<Libp2p> {
|
||||
logWhichShardInfoIsUsed(options);
|
||||
|
||||
if (options.contentTopics) {
|
||||
options.shardInfo = { contentTopics: options.contentTopics };
|
||||
}
|
||||
|
||||
const shardInfo = options.shardInfo
|
||||
? ensureShardingConfigured(options.shardInfo)
|
||||
: undefined;
|
||||
|
@ -117,3 +125,22 @@ export async function createLibp2pAndUpdateOptions(
|
|||
|
||||
return libp2p;
|
||||
}
|
||||
|
||||
function logWhichShardInfoIsUsed(options: CreateWakuNodeOptions): void {
|
||||
if (options.pubsubTopics) {
|
||||
logger.info("Using pubsubTopics array to bootstrap the node.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.contentTopics) {
|
||||
logger.info(
|
||||
"Using contentTopics and default cluster ID (1) to bootstrap the node."
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.shardInfo) {
|
||||
logger.info("Using shardInfo parameters to bootstrap the node.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue