mirror of https://github.com/waku-org/js-waku.git
chore: throw if more than one network config is passed (#2056)
* chore: throw if more than one network config is passed * up * update init * up message * improve project structure * address nit
This commit is contained in:
parent
169a09d552
commit
2b02f829c2
|
@ -1,9 +1,8 @@
|
||||||
import { type Libp2pComponents, type LightNode } from "@waku/interfaces";
|
import { type LightNode } from "@waku/interfaces";
|
||||||
|
|
||||||
import { createLibp2pAndUpdateOptions } from "../utils/libp2p.js";
|
|
||||||
import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "../waku.js";
|
import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "../waku.js";
|
||||||
|
|
||||||
export { Libp2pComponents };
|
import { createLibp2pAndUpdateOptions } from "./libp2p.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a Waku node that uses Waku Light Push, Filter and Store to send and
|
* Create a Waku node that uses Waku Light Push, Filter and Store to send and
|
|
@ -0,0 +1,2 @@
|
||||||
|
export { createLightNode } from "./create.js";
|
||||||
|
export { defaultLibp2p } from "./libp2p.js";
|
|
@ -92,6 +92,46 @@ export async function defaultLibp2p(
|
||||||
export async function createLibp2pAndUpdateOptions(
|
export async function createLibp2pAndUpdateOptions(
|
||||||
options: CreateWakuNodeOptions
|
options: CreateWakuNodeOptions
|
||||||
): Promise<Libp2p> {
|
): Promise<Libp2p> {
|
||||||
|
const shardInfo = configureNetworkOptions(options);
|
||||||
|
|
||||||
|
const libp2pOptions = options?.libp2p ?? {};
|
||||||
|
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
|
||||||
|
|
||||||
|
if (options?.defaultBootstrap) {
|
||||||
|
peerDiscovery.push(...defaultPeerDiscoveries(options.pubsubTopics!));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options?.bootstrapPeers) {
|
||||||
|
peerDiscovery.push(bootstrap({ list: options.bootstrapPeers }));
|
||||||
|
}
|
||||||
|
|
||||||
|
libp2pOptions.peerDiscovery = peerDiscovery;
|
||||||
|
|
||||||
|
const libp2p = await defaultLibp2p(
|
||||||
|
shardInfo,
|
||||||
|
wakuGossipSub(options),
|
||||||
|
libp2pOptions,
|
||||||
|
options?.userAgent
|
||||||
|
);
|
||||||
|
|
||||||
|
return libp2p;
|
||||||
|
}
|
||||||
|
|
||||||
|
function configureNetworkOptions(
|
||||||
|
options: CreateWakuNodeOptions
|
||||||
|
): ShardInfo | undefined {
|
||||||
|
const flags = [
|
||||||
|
options.contentTopics,
|
||||||
|
options.pubsubTopics,
|
||||||
|
options.shardInfo
|
||||||
|
].filter((v) => !!v);
|
||||||
|
|
||||||
|
if (flags.length > 1) {
|
||||||
|
throw Error(
|
||||||
|
"Too many network configurations provided. Pass only one of: pubsubTopic, contentTopics or shardInfo."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
logWhichShardInfoIsUsed(options);
|
logWhichShardInfoIsUsed(options);
|
||||||
|
|
||||||
if (options.contentTopics) {
|
if (options.contentTopics) {
|
||||||
|
@ -105,27 +145,7 @@ export async function createLibp2pAndUpdateOptions(
|
||||||
options.pubsubTopics = shardInfo?.pubsubTopics ??
|
options.pubsubTopics = shardInfo?.pubsubTopics ??
|
||||||
options.pubsubTopics ?? [DefaultPubsubTopic];
|
options.pubsubTopics ?? [DefaultPubsubTopic];
|
||||||
|
|
||||||
const libp2pOptions = options?.libp2p ?? {};
|
return shardInfo?.shardInfo;
|
||||||
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
|
|
||||||
|
|
||||||
if (options?.defaultBootstrap) {
|
|
||||||
peerDiscovery.push(...defaultPeerDiscoveries(options.pubsubTopics));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options?.bootstrapPeers) {
|
|
||||||
peerDiscovery.push(bootstrap({ list: options.bootstrapPeers }));
|
|
||||||
}
|
|
||||||
|
|
||||||
libp2pOptions.peerDiscovery = peerDiscovery;
|
|
||||||
|
|
||||||
const libp2p = await defaultLibp2p(
|
|
||||||
shardInfo?.shardInfo,
|
|
||||||
wakuGossipSub(options),
|
|
||||||
libp2pOptions,
|
|
||||||
options?.userAgent
|
|
||||||
);
|
|
||||||
|
|
||||||
return libp2p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function logWhichShardInfoIsUsed(options: CreateWakuNodeOptions): void {
|
function logWhichShardInfoIsUsed(options: CreateWakuNodeOptions): void {
|
|
@ -7,11 +7,10 @@ export {
|
||||||
|
|
||||||
export { utf8ToBytes, bytesToUtf8 } from "@waku/utils/bytes";
|
export { utf8ToBytes, bytesToUtf8 } from "@waku/utils/bytes";
|
||||||
|
|
||||||
export { defaultLibp2p } from "./utils/libp2p.js";
|
|
||||||
export * from "./utils/content_topic.js";
|
export * from "./utils/content_topic.js";
|
||||||
export * from "./waku.js";
|
export * from "./waku.js";
|
||||||
|
|
||||||
export { createLightNode } from "./light-node/index.js";
|
export { createLightNode, defaultLibp2p } from "./create/index.js";
|
||||||
export { wakuLightPush } from "./protocols/light_push.js";
|
export { wakuLightPush } from "./protocols/light_push.js";
|
||||||
export { wakuFilter } from "./protocols/filter.js";
|
export { wakuFilter } from "./protocols/filter.js";
|
||||||
export { wakuStore } from "./protocols/store.js";
|
export { wakuStore } from "./protocols/store.js";
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { type FullNode, type RelayNode } from "@waku/interfaces";
|
import { type FullNode, type RelayNode } from "@waku/interfaces";
|
||||||
import { RelayCreateOptions } from "@waku/relay";
|
import { RelayCreateOptions } from "@waku/relay";
|
||||||
|
|
||||||
import { createLibp2pAndUpdateOptions } from "../utils/libp2p.js";
|
import { createLibp2pAndUpdateOptions } from "../create/libp2p.js";
|
||||||
import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "../waku.js";
|
import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "../waku.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -12,7 +12,7 @@ import {
|
||||||
shardInfoToPubsubTopics
|
shardInfoToPubsubTopics
|
||||||
} from "@waku/utils";
|
} from "@waku/utils";
|
||||||
|
|
||||||
import { createLightNode } from "../light-node/index.js";
|
import { createLightNode } from "../create/index.js";
|
||||||
|
|
||||||
interface CreateTopicOptions {
|
interface CreateTopicOptions {
|
||||||
waku?: LightNode;
|
waku?: LightNode;
|
||||||
|
|
|
@ -88,19 +88,23 @@ export async function runMultipleNodes(
|
||||||
withoutFilter
|
withoutFilter
|
||||||
);
|
);
|
||||||
|
|
||||||
const waku_options: ProtocolCreateOptions = {
|
const wakuOptions: ProtocolCreateOptions = {
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
libp2p: {
|
libp2p: {
|
||||||
addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] }
|
addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] }
|
||||||
},
|
}
|
||||||
pubsubTopics,
|
|
||||||
shardInfo
|
|
||||||
};
|
};
|
||||||
|
|
||||||
log.info("Starting js waku node with :", JSON.stringify(waku_options));
|
if (shardInfo) {
|
||||||
|
wakuOptions.shardInfo = shardInfo;
|
||||||
|
} else {
|
||||||
|
wakuOptions.pubsubTopics = pubsubTopics;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Starting js waku node with :", JSON.stringify(wakuOptions));
|
||||||
let waku: LightNode | undefined;
|
let waku: LightNode | undefined;
|
||||||
try {
|
try {
|
||||||
waku = await createLightNode(waku_options);
|
waku = await createLightNode(wakuOptions);
|
||||||
await waku.start();
|
await waku.start();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error("jswaku node failed to start:", error);
|
log.error("jswaku node failed to start:", error);
|
||||||
|
|
Loading…
Reference in New Issue