feat: define `Waku` interface

This commit is contained in:
fryorcraken.eth 2022-09-06 12:25:28 +10:00
parent aee054d9d6
commit f56f6e2e83
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
16 changed files with 76 additions and 16 deletions

View File

@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **breaking**: `DecryptionParams` may be passed when using `queryHistory` instead of just keys.
- Examples have been moved to https://github.com/waku-org/js-waku-examples.
- `Waku` is now defined as an interface with `WakuNode` an implementation of it.
### Fixed

View File

@ -13,6 +13,9 @@
"types": "./dist/lib/create_waku.d.ts",
"import": "./dist/lib/create_waku.js"
},
"./lib/interfaces": {
"types": "./dist/lib/interfaces.d.ts"
},
"./lib/peer_discovery_dns": {
"types": "./dist/lib/peer_discovery_dns/index.d.ts",
"import": "./dist/lib/peer_discovery_dns/index.js"

View File

@ -13,7 +13,7 @@ export * as utils from "./lib/utils";
export * as proto_message from "./proto/message";
export * as waku from "./lib/waku";
export { Waku, Protocols } from "./lib/waku";
export { WakuNode, Protocols } from "./lib/waku";
export * as waku_message from "./lib/waku_message";
export { WakuMessage } from "./lib/waku_message";

View File

@ -6,9 +6,10 @@ import { all as filterAll } from "@libp2p/websockets/filters";
import { createLibp2p, Libp2pOptions } from "libp2p";
import type { Libp2p } from "libp2p";
import type { Waku } from "./interfaces";
import { PeerDiscoveryStaticPeers } from "./peer_discovery_static_list";
import { getPredefinedBootstrapNodes } from "./predefined_bootstrap_nodes";
import { Waku, WakuOptions } from "./waku";
import { WakuNode, WakuOptions } from "./waku";
import { WakuFilter } from "./waku_filter";
import { WakuLightPush } from "./waku_light_push";
import { WakuRelay } from "./waku_relay";
@ -30,11 +31,11 @@ export interface CreateOptions {
*/
pubSubTopic?: string;
/**
* You can pass options to the `Libp2p` instance used by {@link index.waku.Waku} using the {@link CreateOptions.libp2p} property.
* You can pass options to the `Libp2p` instance used by {@link index.waku.WakuNode} 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 index.waku.Waku} to ensure it implements the Waku protocol.
* Notes that some values are overridden by {@link index.waku.WakuNode} to ensure it implements the Waku protocol.
*/
libp2p?: Partial<Libp2pOptions>;
/**
@ -65,7 +66,13 @@ export async function createWaku(
const wakuLightPush = new WakuLightPush(libp2p, options);
const wakuFilter = new WakuFilter(libp2p, options);
return new Waku(options ?? {}, libp2p, wakuStore, wakuLightPush, wakuFilter);
return new WakuNode(
options ?? {},
libp2p,
wakuStore,
wakuLightPush,
wakuFilter
);
}
export function defaultPeerDiscovery(): PeerDiscovery {

View File

@ -2,8 +2,9 @@ import { expect } from "chai";
import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils";
import { createWaku } from "../create_waku";
import type { Waku } from "../interfaces";
import { waitForRemotePeer } from "../wait_for_remote_peer";
import { Protocols, Waku } from "../waku";
import { Protocols } from "../waku";
import { ENR } from "./enr";

39
src/lib/interfaces.ts Normal file
View File

@ -0,0 +1,39 @@
import type { Stream } from "@libp2p/interface-connection";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { Multiaddr } from "@multiformats/multiaddr";
import type { Libp2p } from "libp2p";
import type { Protocols } from "./waku";
import type { WakuFilter } from "./waku_filter";
import type { WakuLightPush } from "./waku_light_push";
import type { DecryptionMethod } from "./waku_message";
import type { WakuRelay } from "./waku_relay";
import type { WakuStore } from "./waku_store";
export interface Waku {
libp2p: Libp2p;
relay: WakuRelay;
store: WakuStore;
filter: WakuFilter;
lightPush: WakuLightPush;
dial(peer: PeerId | Multiaddr, protocols?: Protocols[]): Promise<Stream>;
addPeerToAddressBook(
peerId: PeerId | string,
multiaddrs: Multiaddr[] | string[]
): void;
start(): Promise<void>;
stop(): Promise<void>;
isStarted(): boolean;
addDecryptionKey(
key: Uint8Array | string,
options?: { method?: DecryptionMethod; contentTopics?: string[] }
): void;
deleteDecryptionKey(key: Uint8Array | string): void;
}

View File

@ -4,8 +4,9 @@ import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../test_utils";
import { delay } from "../test_utils/delay";
import { createWaku } from "./create_waku";
import type { Waku } from "./interfaces";
import { waitForRemotePeer } from "./wait_for_remote_peer";
import { Protocols, Waku } from "./waku";
import { Protocols } from "./waku";
describe("Wait for remote peer", function () {
let waku: Waku;

View File

@ -5,7 +5,8 @@ import type { Libp2p } from "libp2p";
import { pEvent } from "p-event";
import { StoreCodecs } from "./constants";
import { Protocols, Waku } from "./waku";
import type { Waku } from "./interfaces";
import { Protocols } from "./waku";
import { FilterCodec } from "./waku_filter";
import { LightPushCodec } from "./waku_light_push";

View File

@ -10,9 +10,10 @@ import {
import { createWaku } from "./create_waku";
import { generateSymmetricKey } from "./crypto";
import type { Waku } from "./interfaces";
import { PeerDiscoveryStaticPeers } from "./peer_discovery_static_list";
import { waitForRemotePeer } from "./wait_for_remote_peer";
import { Protocols, Waku } from "./waku";
import { Protocols } from "./waku";
import { WakuMessage } from "./waku_message";
const TestContentTopic = "/test/1/waku/utf8";

View File

@ -2,7 +2,7 @@ import type { PeerId } from "@libp2p/interface-peer-id";
import { expect } from "chai";
import { createWaku } from "./create_waku";
import { Waku } from "./waku";
import type { Waku } from "./interfaces";
describe("Waku Dial", function () {
describe("Bootstrap [live data]", function () {

View File

@ -6,6 +6,7 @@ import { multiaddr } from "@multiformats/multiaddr";
import debug from "debug";
import type { Libp2p } from "libp2p";
import { Waku } from "./interfaces";
import { FilterCodec, WakuFilter } from "./waku_filter";
import { LightPushCodec, WakuLightPush } from "./waku_light_push";
import { DecryptionMethod, WakuMessage } from "./waku_message";
@ -43,7 +44,7 @@ export interface WakuOptions {
decryptionKeys?: Array<Uint8Array | string>;
}
export class Waku {
export class WakuNode implements Waku {
public libp2p: Libp2p;
public relay: WakuRelay;
public store: WakuStore;

View File

@ -4,8 +4,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 type { Waku } from "../interfaces";
import { waitForRemotePeer } from "../wait_for_remote_peer";
import { Protocols, Waku } from "../waku";
import { Protocols } from "../waku";
import { WakuMessage } from "../waku_message";
const log = debug("waku:test");

View File

@ -4,8 +4,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 type { Waku } from "../interfaces";
import { waitForRemotePeer } from "../wait_for_remote_peer";
import { Protocols, Waku } from "../waku";
import { Protocols } from "../waku";
import { WakuMessage } from "../waku_message";
const log = debug("waku:test:lightpush");

View File

@ -14,9 +14,10 @@ import {
generateSymmetricKey,
getPublicKey,
} from "../crypto";
import type { Waku } from "../interfaces";
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
import { waitForRemotePeer } from "../wait_for_remote_peer";
import { Protocols, Waku } from "../waku";
import { Protocols } from "../waku";
import { DecryptionMethod, WakuMessage } from "./index";

View File

@ -17,8 +17,9 @@ import {
generateSymmetricKey,
getPublicKey,
} from "../crypto";
import type { Waku } from "../interfaces";
import { waitForRemotePeer } from "../wait_for_remote_peer";
import { Protocols, Waku } from "../waku";
import { Protocols } from "../waku";
import { DecryptionMethod, WakuMessage } from "../waku_message";
const log = debug("waku:test");

View File

@ -13,8 +13,9 @@ import {
generateSymmetricKey,
getPublicKey,
} from "../crypto";
import type { Waku } from "../interfaces";
import { waitForRemotePeer } from "../wait_for_remote_peer";
import { Protocols, Waku } from "../waku";
import { Protocols } from "../waku";
import { DecryptionMethod, WakuMessage } from "../waku_message";
import { PageDirection } from "./history_rpc";