chore: remove `@waku/peer-exchange` dependency

As per its name, `@waku/core` aims to contain, and only contains, the
minimal set of core functionalities needed for a developer to use Waku
in their webapp.

Hence, `@waku/core` should avoid depending on other Waku packages. If a
developer wishes to use functionality from other packages, they should
explicitly import such packages.
This commit is contained in:
fryorcraken.eth 2023-01-25 17:20:33 +11:00
parent a5ff788eed
commit 372ff6454f
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 52 additions and 34 deletions

2
package-lock.json generated
View File

@ -26317,7 +26317,6 @@
"@waku/byte-utils": "*", "@waku/byte-utils": "*",
"@waku/interfaces": "*", "@waku/interfaces": "*",
"@waku/libp2p-utils": "*", "@waku/libp2p-utils": "*",
"@waku/peer-exchange": "*",
"@waku/proto": "*", "@waku/proto": "*",
"debug": "^4.3.4", "debug": "^4.3.4",
"it-all": "^1.0.6", "it-all": "^1.0.6",
@ -29834,7 +29833,6 @@
"@waku/byte-utils": "*", "@waku/byte-utils": "*",
"@waku/interfaces": "*", "@waku/interfaces": "*",
"@waku/libp2p-utils": "*", "@waku/libp2p-utils": "*",
"@waku/peer-exchange": "*",
"@waku/proto": "*", "@waku/proto": "*",
"app-root-path": "^3.0.0", "app-root-path": "^3.0.0",
"chai": "^4.3.4", "chai": "^4.3.4",

View File

@ -91,7 +91,6 @@
"@waku/byte-utils": "*", "@waku/byte-utils": "*",
"@waku/interfaces": "*", "@waku/interfaces": "*",
"@waku/libp2p-utils": "*", "@waku/libp2p-utils": "*",
"@waku/peer-exchange": "*",
"@waku/proto": "*", "@waku/proto": "*",
"debug": "^4.3.4", "debug": "^4.3.4",
"it-all": "^1.0.6", "it-all": "^1.0.6",

View File

@ -1,14 +1,9 @@
import { PeerProtocolsChangeData } from "@libp2p/interface-peer-store"; import { PeerProtocolsChangeData } from "@libp2p/interface-peer-store";
import type { IRelay, PointToPointProtocol, Waku } from "@waku/interfaces"; import type { IRelay, PointToPointProtocol, Waku } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces";
import { PeerExchangeCodec } from "@waku/peer-exchange";
import debug from "debug"; import debug from "debug";
import { pEvent } from "p-event"; import { pEvent } from "p-event";
import { FilterCodec } from "./filter/index.js";
import { LightPushCodec } from "./light_push/index.js";
import { StoreCodec } from "./store/index.js";
const log = debug("waku:wait-for-remote-peer"); const log = debug("waku:wait-for-remote-peer");
/** /**
@ -50,19 +45,19 @@ export async function waitForRemotePeer(
if (protocols.includes(Protocols.Store)) { if (protocols.includes(Protocols.Store)) {
if (!waku.store) if (!waku.store)
throw new Error("Cannot wait for Store peer: protocol not mounted"); throw new Error("Cannot wait for Store peer: protocol not mounted");
promises.push(waitForConnectedPeer(waku.store, [StoreCodec])); promises.push(waitForConnectedPeer(waku.store));
} }
if (protocols.includes(Protocols.LightPush)) { if (protocols.includes(Protocols.LightPush)) {
if (!waku.lightPush) if (!waku.lightPush)
throw new Error("Cannot wait for LightPush peer: protocol not mounted"); throw new Error("Cannot wait for LightPush peer: protocol not mounted");
promises.push(waitForConnectedPeer(waku.lightPush, [LightPushCodec])); promises.push(waitForConnectedPeer(waku.lightPush));
} }
if (protocols.includes(Protocols.Filter)) { if (protocols.includes(Protocols.Filter)) {
if (!waku.filter) if (!waku.filter)
throw new Error("Cannot wait for Filter peer: protocol not mounted"); throw new Error("Cannot wait for Filter peer: protocol not mounted");
promises.push(waitForConnectedPeer(waku.filter, [FilterCodec])); promises.push(waitForConnectedPeer(waku.filter));
} }
if (protocols.includes(Protocols.PeerExchange)) { if (protocols.includes(Protocols.PeerExchange)) {
@ -70,7 +65,7 @@ export async function waitForRemotePeer(
throw new Error( throw new Error(
"Cannot wait for Peer Exchange peer: protocol not mounted" "Cannot wait for Peer Exchange peer: protocol not mounted"
); );
promises.push(waitForConnectedPeer(waku.peerExchange, [PeerExchangeCodec])); promises.push(waitForConnectedPeer(waku.peerExchange));
} }
if (timeoutMs) { if (timeoutMs) {
@ -88,28 +83,25 @@ export async function waitForRemotePeer(
* Wait for a peer with the given protocol to be connected. * Wait for a peer with the given protocol to be connected.
*/ */
async function waitForConnectedPeer( async function waitForConnectedPeer(
waku: PointToPointProtocol, protocol: PointToPointProtocol
codecs: string[]
): Promise<void> { ): Promise<void> {
const peers = await waku.peers(); const codec = protocol.multicodec;
const peers = await protocol.peers();
if (peers.length) { if (peers.length) {
log(`${codecs} peer found: `, peers[0].id.toString()); log(`${codec} peer found: `, peers[0].id.toString());
return; return;
} }
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): void => { const cb = (evt: CustomEvent<PeerProtocolsChangeData>): void => {
for (const codec of codecs) {
if (evt.detail.protocols.includes(codec)) { if (evt.detail.protocols.includes(codec)) {
log("Resolving for", codec, evt.detail.protocols); log("Resolving for", codec, evt.detail.protocols);
waku.peerStore.removeEventListener("change:protocols", cb); protocol.peerStore.removeEventListener("change:protocols", cb);
resolve(); resolve();
break;
}
} }
}; };
waku.peerStore.addEventListener("change:protocols", cb); protocol.peerStore.addEventListener("change:protocols", cb);
}); });
} }

View File

@ -12,16 +12,15 @@ import type {
Waku, Waku,
} from "@waku/interfaces"; } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces";
import { PeerExchangeCodec } from "@waku/peer-exchange";
import debug from "debug"; import debug from "debug";
import type { Libp2p } from "libp2p"; import type { Libp2p } from "libp2p";
import { FilterCodec, FilterComponents } from "./filter/index.js"; import { FilterComponents } from "./filter/index.js";
import { LightPushCodec, LightPushComponents } from "./light_push/index.js"; import { LightPushComponents } from "./light_push/index.js";
import { createEncoder } from "./message/version_0.js"; import { createEncoder } from "./message/version_0.js";
import * as relayConstants from "./relay/constants.js"; import * as relayConstants from "./relay/constants.js";
import { RelayCodecs, RelayPingContentTopic } from "./relay/constants.js"; import { RelayPingContentTopic } from "./relay/constants.js";
import { StoreCodec, StoreComponents } from "./store/index.js"; import { StoreComponents } from "./store/index.js";
export const DefaultPingKeepAliveValueSecs = 0; export const DefaultPingKeepAliveValueSecs = 0;
export const DefaultRelayKeepAliveValueSecs = 5 * 60; export const DefaultRelayKeepAliveValueSecs = 5 * 60;
@ -165,20 +164,50 @@ export class WakuNode implements Waku {
const codecs: string[] = []; const codecs: string[] = [];
if (_protocols.includes(Protocols.Relay)) { if (_protocols.includes(Protocols.Relay)) {
RelayCodecs.forEach((codec) => codecs.push(codec)); if (this.relay) {
this.relay.multicodecs.forEach((codec) => codecs.push(codec));
} else {
log(
"Relay codec not included in dial codec: protocol not mounted locally"
);
}
} }
if (_protocols.includes(Protocols.Store)) { if (_protocols.includes(Protocols.Store)) {
codecs.push(StoreCodec); if (this.store) {
codecs.push(this.store.multicodec);
} else {
log(
"Store codec not included in dial codec: protocol not mounted locally"
);
}
} }
if (_protocols.includes(Protocols.LightPush)) { if (_protocols.includes(Protocols.LightPush)) {
codecs.push(LightPushCodec); if (this.lightPush) {
codecs.push(this.lightPush.multicodec);
} else {
log(
"Light Push codec not included in dial codec: protocol not mounted locally"
);
}
} }
if (_protocols.includes(Protocols.Filter)) { if (_protocols.includes(Protocols.Filter)) {
codecs.push(FilterCodec); if (this.filter) {
codecs.push(this.filter.multicodec);
} else {
log(
"Filter codec not included in dial codec: protocol not mounted locally"
);
}
} }
if (_protocols.includes(Protocols.PeerExchange)) { if (_protocols.includes(Protocols.PeerExchange)) {
codecs.push(PeerExchangeCodec); if (this.peerExchange) {
codecs.push(this.peerExchange.multicodec);
} else {
log(
"Peer Exchange codec not included in dial codec: protocol not mounted locally"
);
}
} }
log(`Dialing to ${peer.toString()} with protocols ${_protocols}`); log(`Dialing to ${peer.toString()} with protocols ${_protocols}`);