mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-05 00:55:30 +00:00
refactor: move createWaku
to own module
This commit is contained in:
parent
9b34897bab
commit
69aee6c1cc
@ -18,7 +18,7 @@ export { waitForRemotePeer } from "./lib/wait_for_remote_peer";
|
||||
export * as proto_message from "./proto/message";
|
||||
|
||||
export * as waku from "./lib/waku";
|
||||
export { createWaku, Waku, Protocols } from "./lib/waku";
|
||||
export { Waku, Protocols } from "./lib/waku";
|
||||
|
||||
export * as waku_message from "./lib/waku_message";
|
||||
export { WakuMessage } from "./lib/waku_message";
|
||||
@ -38,3 +38,4 @@ export { WakuRelay } from "./lib/waku_relay";
|
||||
|
||||
export * as waku_store from "./lib/waku_store";
|
||||
export { PageDirection, WakuStore, StoreCodecs } from "./lib/waku_store";
|
||||
export { createWaku } from "./lib/create_waku";
|
||||
|
80
src/lib/create_waku.ts
Normal file
80
src/lib/create_waku.ts
Normal file
@ -0,0 +1,80 @@
|
||||
import { Noise } from "@chainsafe/libp2p-noise";
|
||||
import { Mplex } from "@libp2p/mplex";
|
||||
import { WebSockets } from "@libp2p/websockets";
|
||||
import { all as filterAll } from "@libp2p/websockets/dist/src/filters";
|
||||
import { createLibp2p, Libp2pOptions } from "libp2p";
|
||||
|
||||
import { Bootstrap, BootstrapOptions } from "./discovery";
|
||||
import { Waku, WakuOptions } from "./waku";
|
||||
import { WakuFilter } from "./waku_filter";
|
||||
import { WakuLightPush } from "./waku_light_push";
|
||||
import { WakuRelay } from "./waku_relay";
|
||||
import { WakuStore } from "./waku_store";
|
||||
|
||||
export interface CreateOptions {
|
||||
/**
|
||||
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
|
||||
*
|
||||
* One and only one pubsub topic is used by Waku. This is used by:
|
||||
* - WakuRelay to receive, route and send messages,
|
||||
* - WakuLightPush to send messages,
|
||||
* - WakuStore to retrieve messages.
|
||||
*
|
||||
* The usage of the default pubsub topic is recommended.
|
||||
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
||||
*
|
||||
* @default {@link DefaultPubSubTopic}
|
||||
*/
|
||||
pubSubTopic?: string;
|
||||
/**
|
||||
* You can pass options to the `Libp2p` instance used by {@link Waku} using the {@link CreateOptions.libp2p} property.
|
||||
* This property is the same type than the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||
* apart that we made the `modules` property optional and partial,
|
||||
* allowing its omission and letting Waku set good defaults.
|
||||
* Notes that some values are overridden by {@link Waku} to ensure it implements the Waku protocol.
|
||||
*/
|
||||
libp2p?: Partial<Libp2pOptions>;
|
||||
/**
|
||||
* Byte array used as key for the noise protocol used for connection encryption
|
||||
* by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||
* This is only used for test purposes to not run out of entropy during CI runs.
|
||||
*/
|
||||
staticNoiseKey?: Uint8Array;
|
||||
/**
|
||||
* Use libp2p-bootstrap to discover and connect to new nodes.
|
||||
*
|
||||
* See [[BootstrapOptions]] for available parameters.
|
||||
*
|
||||
* Note: It overrides any other peerDiscovery modules that may have been set via
|
||||
* {@link CreateOptions.libp2p}.
|
||||
*/
|
||||
bootstrap?: BootstrapOptions;
|
||||
}
|
||||
|
||||
export async function createWaku(
|
||||
options?: CreateOptions & WakuOptions
|
||||
): Promise<Waku> {
|
||||
const peerDiscovery = [];
|
||||
if (options?.bootstrap) {
|
||||
peerDiscovery.push(new Bootstrap(options?.bootstrap));
|
||||
}
|
||||
|
||||
const libp2pOpts = Object.assign(
|
||||
{
|
||||
transports: [new WebSockets({ filter: filterAll })],
|
||||
streamMuxers: [new Mplex()],
|
||||
pubsub: new WakuRelay(options),
|
||||
connectionEncryption: [new Noise()],
|
||||
peerDiscovery: peerDiscovery,
|
||||
},
|
||||
options?.libp2p ?? {}
|
||||
);
|
||||
|
||||
const libp2p = await createLibp2p(libp2pOpts);
|
||||
|
||||
const wakuStore = new WakuStore(libp2p, options);
|
||||
const wakuLightPush = new WakuLightPush(libp2p, options);
|
||||
const wakuFilter = new WakuFilter(libp2p, options);
|
||||
|
||||
return new Waku(options ?? {}, libp2p, wakuStore, wakuLightPush, wakuFilter);
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils";
|
||||
import { createWaku } from "../create_waku";
|
||||
import { waitForRemotePeer } from "../wait_for_remote_peer";
|
||||
import { createWaku, Protocols, Waku } from "../waku";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
|
||||
import { ENR } from "./enr";
|
||||
|
||||
|
@ -3,8 +3,9 @@ import { expect } from "chai";
|
||||
import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../test_utils";
|
||||
import { delay } from "../test_utils/delay";
|
||||
|
||||
import { createWaku } from "./create_waku";
|
||||
import { waitForRemotePeer } from "./wait_for_remote_peer";
|
||||
import { createWaku, Protocols, Waku } from "./waku";
|
||||
import { Protocols, Waku } from "./waku";
|
||||
|
||||
describe("Wait for remote peer", function () {
|
||||
let waku: Waku;
|
||||
|
@ -8,9 +8,10 @@ import {
|
||||
Nwaku,
|
||||
} from "../test_utils/";
|
||||
|
||||
import { createWaku } from "./create_waku";
|
||||
import { generateSymmetricKey } from "./crypto";
|
||||
import { waitForRemotePeer } from "./wait_for_remote_peer";
|
||||
import { createWaku, Protocols, Waku } from "./waku";
|
||||
import { Protocols, Waku } from "./waku";
|
||||
import { WakuMessage } from "./waku_message";
|
||||
|
||||
const TestContentTopic = "/test/1/waku/utf8";
|
||||
|
@ -1,7 +1,8 @@
|
||||
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||
import { expect } from "chai";
|
||||
|
||||
import { createWaku, Waku } from "./waku";
|
||||
import { createWaku } from "./create_waku";
|
||||
import { Waku } from "./waku";
|
||||
|
||||
describe("Waku Dial", function () {
|
||||
describe("Bootstrap [live data]", function () {
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { Noise } from "@chainsafe/libp2p-noise";
|
||||
import type { Stream } from "@libp2p/interface-connection";
|
||||
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||
import { Mplex } from "@libp2p/mplex";
|
||||
import { peerIdFromString } from "@libp2p/peer-id";
|
||||
import { WebSockets } from "@libp2p/websockets";
|
||||
import { all as filterAll } from "@libp2p/websockets/filters";
|
||||
import { Multiaddr, multiaddr } from "@multiformats/multiaddr";
|
||||
import type { Multiaddr } from "@multiformats/multiaddr";
|
||||
import { multiaddr } from "@multiformats/multiaddr";
|
||||
import debug from "debug";
|
||||
import { createLibp2p, Libp2p, Libp2pOptions } from "libp2p";
|
||||
import type { Libp2p } from "libp2p";
|
||||
|
||||
import { Bootstrap, BootstrapOptions } from "./discovery";
|
||||
import { FilterCodec, WakuFilter } from "./waku_filter";
|
||||
import { LightPushCodec, WakuLightPush } from "./waku_light_push";
|
||||
import { DecryptionMethod, WakuMessage } from "./waku_message";
|
||||
@ -29,21 +25,7 @@ export enum Protocols {
|
||||
Filter = "filter",
|
||||
}
|
||||
|
||||
export interface CreateOptions {
|
||||
/**
|
||||
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
|
||||
*
|
||||
* One and only one pubsub topic is used by Waku. This is used by:
|
||||
* - WakuRelay to receive, route and send messages,
|
||||
* - WakuLightPush to send messages,
|
||||
* - WakuStore to retrieve messages.
|
||||
*
|
||||
* The usage of the default pubsub topic is recommended.
|
||||
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
||||
*
|
||||
* @default {@link DefaultPubSubTopic}
|
||||
*/
|
||||
pubSubTopic?: string;
|
||||
export interface WakuOptions {
|
||||
/**
|
||||
* Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
|
||||
* request to each peer after the set number of seconds. Set to 0 to disable.
|
||||
@ -58,58 +40,9 @@ export interface CreateOptions {
|
||||
* @default {@link DefaultRelayKeepAliveValueSecs}
|
||||
*/
|
||||
relayKeepAlive?: number;
|
||||
/**
|
||||
* You can pass options to the `Libp2p` instance used by {@link Waku} using the {@link CreateOptions.libp2p} property.
|
||||
* This property is the same type than the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||
* apart that we made the `modules` property optional and partial,
|
||||
* allowing its omission and letting Waku set good defaults.
|
||||
* Notes that some values are overridden by {@link Waku} to ensure it implements the Waku protocol.
|
||||
*/
|
||||
libp2p?: Partial<Libp2pOptions>;
|
||||
/**
|
||||
* Byte array used as key for the noise protocol used for connection encryption
|
||||
* by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||
* This is only used for test purposes to not run out of entropy during CI runs.
|
||||
*/
|
||||
staticNoiseKey?: Uint8Array;
|
||||
/**
|
||||
* Use libp2p-bootstrap to discover and connect to new nodes.
|
||||
*
|
||||
* See [[BootstrapOptions]] for available parameters.
|
||||
*
|
||||
* Note: It overrides any other peerDiscovery modules that may have been set via
|
||||
* {@link CreateOptions.libp2p}.
|
||||
*/
|
||||
bootstrap?: BootstrapOptions;
|
||||
decryptionKeys?: Array<Uint8Array | string>;
|
||||
}
|
||||
|
||||
export async function createWaku(options?: CreateOptions): Promise<Waku> {
|
||||
const peerDiscovery = [];
|
||||
if (options?.bootstrap) {
|
||||
peerDiscovery.push(new Bootstrap(options?.bootstrap));
|
||||
}
|
||||
|
||||
const libp2pOpts = Object.assign(
|
||||
{
|
||||
transports: [new WebSockets({ filter: filterAll })],
|
||||
streamMuxers: [new Mplex()],
|
||||
pubsub: new WakuRelay(options),
|
||||
connectionEncryption: [new Noise()],
|
||||
peerDiscovery: peerDiscovery,
|
||||
},
|
||||
options?.libp2p ?? {}
|
||||
);
|
||||
|
||||
const libp2p = await createLibp2p(libp2pOpts);
|
||||
|
||||
const wakuStore = new WakuStore(libp2p, options);
|
||||
const wakuLightPush = new WakuLightPush(libp2p, options);
|
||||
const wakuFilter = new WakuFilter(libp2p, options);
|
||||
|
||||
return new Waku(options ?? {}, libp2p, wakuStore, wakuLightPush, wakuFilter);
|
||||
}
|
||||
|
||||
export class Waku {
|
||||
public libp2p: Libp2p;
|
||||
public relay: WakuRelay;
|
||||
@ -125,7 +58,7 @@ export class Waku {
|
||||
};
|
||||
|
||||
constructor(
|
||||
options: CreateOptions,
|
||||
options: WakuOptions,
|
||||
libp2p: Libp2p,
|
||||
store: WakuStore,
|
||||
lightPush: WakuLightPush,
|
||||
|
@ -3,8 +3,9 @@ import debug from "debug";
|
||||
|
||||
import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils";
|
||||
import { delay } from "../../test_utils/delay";
|
||||
import { createWaku } from "../create_waku";
|
||||
import { waitForRemotePeer } from "../wait_for_remote_peer";
|
||||
import { createWaku, Protocols, Waku } from "../waku";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
import { WakuMessage } from "../waku_message";
|
||||
|
||||
const log = debug("waku:test");
|
||||
|
@ -3,8 +3,9 @@ import debug from "debug";
|
||||
|
||||
import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils";
|
||||
import { delay } from "../../test_utils/delay";
|
||||
import { createWaku } from "../create_waku";
|
||||
import { waitForRemotePeer } from "../wait_for_remote_peer";
|
||||
import { createWaku, Protocols, Waku } from "../waku";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
import { WakuMessage } from "../waku_message";
|
||||
|
||||
const dbg = debug("waku:test:lightpush");
|
||||
|
@ -8,6 +8,7 @@ import {
|
||||
WakuRelayMessage,
|
||||
} from "../../test_utils";
|
||||
import { delay } from "../../test_utils/delay";
|
||||
import { createWaku } from "../create_waku";
|
||||
import {
|
||||
generatePrivateKey,
|
||||
generateSymmetricKey,
|
||||
@ -15,7 +16,7 @@ import {
|
||||
} from "../crypto";
|
||||
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
|
||||
import { waitForRemotePeer } from "../wait_for_remote_peer";
|
||||
import { createWaku, Protocols, Waku } from "../waku";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
|
||||
import { DecryptionMethod, WakuMessage } from "./index";
|
||||
|
||||
|
@ -11,13 +11,14 @@ import {
|
||||
} from "../../test_utils";
|
||||
import { delay } from "../../test_utils/delay";
|
||||
import { DefaultPubSubTopic } from "../constants";
|
||||
import { createWaku } from "../create_waku";
|
||||
import {
|
||||
generatePrivateKey,
|
||||
generateSymmetricKey,
|
||||
getPublicKey,
|
||||
} from "../crypto";
|
||||
import { waitForRemotePeer } from "../wait_for_remote_peer";
|
||||
import { createWaku, Protocols, Waku } from "../waku";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
import { DecryptionMethod, WakuMessage } from "../waku_message";
|
||||
|
||||
const log = debug("waku:test");
|
||||
|
@ -12,13 +12,30 @@ import debug from "debug";
|
||||
|
||||
import { DefaultPubSubTopic } from "../constants";
|
||||
import { hexToBytes } from "../utils";
|
||||
import { CreateOptions } from "../waku";
|
||||
import { DecryptionMethod, WakuMessage } from "../waku_message";
|
||||
|
||||
import * as constants from "./constants";
|
||||
|
||||
const dbg = debug("waku:relay");
|
||||
|
||||
export interface CreateOptions {
|
||||
/**
|
||||
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
|
||||
*
|
||||
* One and only one pubsub topic is used by Waku. This is used by:
|
||||
* - WakuRelay to receive, route and send messages,
|
||||
* - WakuLightPush to send messages,
|
||||
* - WakuStore to retrieve messages.
|
||||
*
|
||||
* The usage of the default pubsub topic is recommended.
|
||||
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
||||
*
|
||||
* @default {@link DefaultPubSubTopic}
|
||||
*/
|
||||
pubSubTopic?: string;
|
||||
decryptionKeys?: Array<Uint8Array | string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the [Waku v2 Relay protocol]{@link https://rfc.vac.dev/spec/11/}.
|
||||
* Must be passed as a `pubsub` module to a {Libp2p} instance.
|
||||
|
@ -7,13 +7,14 @@ import {
|
||||
NOISE_KEY_2,
|
||||
Nwaku,
|
||||
} from "../../test_utils";
|
||||
import { createWaku } from "../create_waku";
|
||||
import {
|
||||
generatePrivateKey,
|
||||
generateSymmetricKey,
|
||||
getPublicKey,
|
||||
} from "../crypto";
|
||||
import { waitForRemotePeer } from "../wait_for_remote_peer";
|
||||
import { createWaku, Protocols, Waku } from "../waku";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
import { DecryptionMethod, WakuMessage } from "../waku_message";
|
||||
|
||||
import { PageDirection } from "./history_rpc";
|
||||
|
Loading…
x
Reference in New Issue
Block a user