From 53af8994bd12a3a13245b920c6018377450dd99d Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Tue, 6 Dec 2022 12:36:29 +1100 Subject: [PATCH] feat: added `I` prefix to protocols interfaces This will enable the remove of `Waku` prefix on all protocol implementations, which is redundant due to the context. --- packages/core/src/lib/wait_for_remote_peer.ts | 4 +- packages/core/src/lib/waku.ts | 30 +++++++------- packages/core/src/lib/waku_filter/index.ts | 6 +-- .../core/src/lib/waku_light_push/index.ts | 6 +-- packages/core/src/lib/waku_relay/index.ts | 6 +-- packages/core/src/lib/waku_store/index.ts | 6 +-- packages/create/src/index.ts | 9 ++++- packages/interfaces/src/filter.ts | 2 +- packages/interfaces/src/light_push.ts | 2 +- packages/interfaces/src/peer_exchange.ts | 2 +- packages/interfaces/src/relay.ts | 2 +- packages/interfaces/src/store.ts | 2 +- packages/interfaces/src/waku.ts | 40 +++++++++---------- .../peer-exchange/src/waku_peer_exchange.ts | 4 +- 14 files changed, 63 insertions(+), 58 deletions(-) diff --git a/packages/core/src/lib/wait_for_remote_peer.ts b/packages/core/src/lib/wait_for_remote_peer.ts index 785e0dd370..85c312c12e 100644 --- a/packages/core/src/lib/wait_for_remote_peer.ts +++ b/packages/core/src/lib/wait_for_remote_peer.ts @@ -1,5 +1,5 @@ import { PeerProtocolsChangeData } from "@libp2p/interface-peer-store"; -import type { PointToPointProtocol, Relay, Waku } from "@waku/interfaces"; +import type { IRelay, PointToPointProtocol, Waku } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { PeerExchangeCodec } from "@waku/peer-exchange"; import debug from "debug"; @@ -117,7 +117,7 @@ async function waitForConnectedPeer( * Wait for a peer with the given protocol to be connected and in the gossipsub * mesh. */ -async function waitForGossipSubPeerInMesh(waku: Relay): Promise { +async function waitForGossipSubPeerInMesh(waku: IRelay): Promise { let peers = waku.getMeshPeers(); while (peers.length == 0) { diff --git a/packages/core/src/lib/waku.ts b/packages/core/src/lib/waku.ts index cac155d88a..ba6969f3fe 100644 --- a/packages/core/src/lib/waku.ts +++ b/packages/core/src/lib/waku.ts @@ -3,12 +3,12 @@ import type { PeerId } from "@libp2p/interface-peer-id"; import type { PubSub } from "@libp2p/interface-pubsub"; import type { Multiaddr } from "@multiformats/multiaddr"; import type { - Filter, - LightPush, - PeerExchange, + IFilter, + ILightPush, + IPeerExchange, + IRelay, + IStore, PeerExchangeComponents, - Relay, - Store, Waku, } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; @@ -56,11 +56,11 @@ export interface WakuOptions { export class WakuNode implements Waku { public libp2p: Libp2p; - public relay?: Relay; - public store?: Store; - public filter?: Filter; - public lightPush?: LightPush; - public peerExchange?: PeerExchange; + public relay?: IRelay; + public store?: IStore; + public filter?: IFilter; + public lightPush?: ILightPush; + public peerExchange?: IPeerExchange; private pingKeepAliveTimers: { [peer: string]: ReturnType; @@ -72,10 +72,10 @@ export class WakuNode implements Waku { constructor( options: WakuOptions, libp2p: Libp2p, - store?: (components: StoreComponents) => Store, - lightPush?: (components: LightPushComponents) => LightPush, - filter?: (components: FilterComponents) => Filter, - peerExchange?: (components: PeerExchangeComponents) => PeerExchange + store?: (components: StoreComponents) => IStore, + lightPush?: (components: LightPushComponents) => ILightPush, + filter?: (components: FilterComponents) => IFilter, + peerExchange?: (components: PeerExchangeComponents) => IPeerExchange ) { this.libp2p = libp2p; @@ -274,7 +274,7 @@ export class WakuNode implements Waku { } } -function isRelay(pubsub: PubSub): pubsub is Relay { +function isRelay(pubsub: PubSub): pubsub is IRelay { if (pubsub) { try { return pubsub.multicodecs.includes( diff --git a/packages/core/src/lib/waku_filter/index.ts b/packages/core/src/lib/waku_filter/index.ts index c8b2ab2b51..d7e3dae673 100644 --- a/packages/core/src/lib/waku_filter/index.ts +++ b/packages/core/src/lib/waku_filter/index.ts @@ -7,9 +7,9 @@ import type { IncomingStreamData } from "@libp2p/interface-registrar"; import type { Registrar } from "@libp2p/interface-registrar"; import type { Callback, - Filter, IDecodedMessage, IDecoder, + IFilter, IMessage, ProtocolOptions, } from "@waku/interfaces"; @@ -64,7 +64,7 @@ export type UnsubscribeFunction = () => Promise; * - https://github.com/status-im/go-waku/issues/245 * - https://github.com/status-im/nwaku/issues/948 */ -class WakuFilter implements Filter { +class WakuFilter implements IFilter { pubSubTopic: string; private subscriptions: Map>; private decoders: Map< @@ -308,6 +308,6 @@ class WakuFilter implements Filter { export function wakuFilter( init: Partial = {} -): (components: FilterComponents) => Filter { +): (components: FilterComponents) => IFilter { return (components: FilterComponents) => new WakuFilter(components, init); } diff --git a/packages/core/src/lib/waku_light_push/index.ts b/packages/core/src/lib/waku_light_push/index.ts index 54c8db3afb..9e934ccbfd 100644 --- a/packages/core/src/lib/waku_light_push/index.ts +++ b/packages/core/src/lib/waku_light_push/index.ts @@ -4,8 +4,8 @@ import type { Peer } from "@libp2p/interface-peer-store"; import type { PeerStore } from "@libp2p/interface-peer-store"; import type { IEncoder, + ILightPush, IMessage, - LightPush, ProtocolOptions, SendResult, } from "@waku/interfaces"; @@ -51,7 +51,7 @@ export interface CreateOptions { /** * Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/). */ -class WakuLightPush implements LightPush { +class WakuLightPush implements ILightPush { pubSubTopic: string; constructor(public components: LightPushComponents, options?: CreateOptions) { @@ -151,7 +151,7 @@ class WakuLightPush implements LightPush { export function wakuLightPush( init: Partial = {} -): (components: LightPushComponents) => LightPush { +): (components: LightPushComponents) => ILightPush { return (components: LightPushComponents) => new WakuLightPush(components, init); } diff --git a/packages/core/src/lib/waku_relay/index.ts b/packages/core/src/lib/waku_relay/index.ts index d3a4861ffe..9492e09bec 100644 --- a/packages/core/src/lib/waku_relay/index.ts +++ b/packages/core/src/lib/waku_relay/index.ts @@ -11,7 +11,7 @@ import type { IDecoder, IEncoder, IMessage, - Relay, + IRelay, SendResult, } from "@waku/interfaces"; import { IDecodedMessage } from "@waku/interfaces"; @@ -53,7 +53,7 @@ export type CreateOptions = { * * @implements {require('libp2p-interfaces/src/pubsub')} */ -class WakuRelay extends GossipSub implements Relay { +class WakuRelay extends GossipSub implements IRelay { pubSubTopic: string; defaultDecoder: IDecoder; public static multicodec: string = constants.RelayCodecs[0]; @@ -189,6 +189,6 @@ WakuRelay.multicodec = constants.RelayCodecs[constants.RelayCodecs.length - 1]; export function wakuRelay( init: Partial = {} -): (components: GossipSubComponents) => Relay { +): (components: GossipSubComponents) => IRelay { return (components: GossipSubComponents) => new WakuRelay(components, init); } diff --git a/packages/core/src/lib/waku_store/index.ts b/packages/core/src/lib/waku_store/index.ts index 78c890164d..7bf80fecc8 100644 --- a/packages/core/src/lib/waku_store/index.ts +++ b/packages/core/src/lib/waku_store/index.ts @@ -9,7 +9,7 @@ import { IDecodedMessage, IDecoder, Index, - Store, + IStore, } from "@waku/interfaces"; import { getPeersForProtocol, @@ -104,7 +104,7 @@ export interface QueryOptions { * * The Waku Store protocol can be used to retrieved historical messages. */ -class WakuStore implements Store { +class WakuStore implements IStore { pubSubTopic: string; constructor(public components: StoreComponents, options?: CreateOptions) { @@ -432,6 +432,6 @@ export async function createCursor( export function wakuStore( init: Partial = {} -): (components: StoreComponents) => Store { +): (components: StoreComponents) => IStore { return (components: StoreComponents) => new WakuStore(components, init); } diff --git a/packages/create/src/index.ts b/packages/create/src/index.ts index ae1af85f44..756b1bbbf3 100644 --- a/packages/create/src/index.ts +++ b/packages/create/src/index.ts @@ -15,7 +15,12 @@ import { } from "@waku/core"; import { DefaultUserAgent } from "@waku/core"; import { getPredefinedBootstrapNodes } from "@waku/core/lib/predefined_bootstrap_nodes"; -import type { Relay, WakuFull, WakuLight, WakuPrivacy } from "@waku/interfaces"; +import type { + IRelay, + WakuFull, + WakuLight, + WakuPrivacy, +} from "@waku/interfaces"; import { wakuPeerExchange } from "@waku/peer-exchange"; import type { Libp2p } from "libp2p"; import { createLibp2p, Libp2pOptions } from "libp2p"; @@ -172,7 +177,7 @@ export function defaultPeerDiscovery(): ( } export async function defaultLibp2p( - wakuRelay?: (components: Libp2pComponents) => Relay, + wakuRelay?: (components: Libp2pComponents) => IRelay, options?: Partial, userAgent?: string ): Promise { diff --git a/packages/interfaces/src/filter.ts b/packages/interfaces/src/filter.ts index 74c8e9c4fd..526b493421 100644 --- a/packages/interfaces/src/filter.ts +++ b/packages/interfaces/src/filter.ts @@ -5,7 +5,7 @@ import type { ProtocolOptions, } from "./protocols.js"; -export interface Filter extends PointToPointProtocol { +export interface IFilter extends PointToPointProtocol { subscribe: ( decoders: IDecoder[], callback: Callback, diff --git a/packages/interfaces/src/light_push.ts b/packages/interfaces/src/light_push.ts index 2afb674ae9..ad217b60de 100644 --- a/packages/interfaces/src/light_push.ts +++ b/packages/interfaces/src/light_push.ts @@ -5,7 +5,7 @@ import type { SendResult, } from "./protocols.js"; -export interface LightPush extends PointToPointProtocol { +export interface ILightPush extends PointToPointProtocol { push: ( encoder: IEncoder, message: IMessage, diff --git a/packages/interfaces/src/peer_exchange.ts b/packages/interfaces/src/peer_exchange.ts index 05359e25da..c74415c4d9 100644 --- a/packages/interfaces/src/peer_exchange.ts +++ b/packages/interfaces/src/peer_exchange.ts @@ -5,7 +5,7 @@ import { ENR } from "@waku/enr"; import { PointToPointProtocol } from "./protocols.js"; -export interface PeerExchange extends PointToPointProtocol { +export interface IPeerExchange extends PointToPointProtocol { query( params: PeerExchangeQueryParams, callback: (response: PeerExchangeResponse) => Promise | void diff --git a/packages/interfaces/src/relay.ts b/packages/interfaces/src/relay.ts index 76d1a76958..f88e4ca676 100644 --- a/packages/interfaces/src/relay.ts +++ b/packages/interfaces/src/relay.ts @@ -8,7 +8,7 @@ import type { } from "./message.js"; import type { Callback, SendResult } from "./protocols.js"; -export interface Relay extends GossipSub { +export interface IRelay extends GossipSub { send: (encoder: IEncoder, message: IMessage) => Promise; addObserver: ( decoder: IDecoder, diff --git a/packages/interfaces/src/store.ts b/packages/interfaces/src/store.ts index b7d7b355ce..77dd9467df 100644 --- a/packages/interfaces/src/store.ts +++ b/packages/interfaces/src/store.ts @@ -50,7 +50,7 @@ export type StoreQueryOptions = { cursor?: Cursor; } & ProtocolOptions; -export interface Store extends PointToPointProtocol { +export interface IStore extends PointToPointProtocol { queryOrderedCallback: ( decoders: IDecoder[], callback: (message: T) => Promise | boolean | void, diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index 19e0e429ff..21f5007438 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -3,20 +3,20 @@ import type { PeerId } from "@libp2p/interface-peer-id"; import type { Multiaddr } from "@multiformats/multiaddr"; import type { Libp2p } from "libp2p"; -import type { Filter } from "./filter.js"; -import type { LightPush } from "./light_push.js"; -import type { PeerExchange } from "./peer_exchange.js"; +import type { IFilter } from "./filter.js"; +import type { ILightPush } from "./light_push.js"; +import type { IPeerExchange } from "./peer_exchange.js"; import { Protocols } from "./protocols.js"; -import type { Relay } from "./relay.js"; -import type { Store } from "./store.js"; +import type { IRelay } from "./relay.js"; +import type { IStore } from "./store.js"; export interface Waku { libp2p: Libp2p; - relay?: Relay; - store?: Store; - filter?: Filter; - lightPush?: LightPush; - peerExchange?: PeerExchange; + relay?: IRelay; + store?: IStore; + filter?: IFilter; + lightPush?: ILightPush; + peerExchange?: IPeerExchange; dial(peer: PeerId | Multiaddr, protocols?: Protocols[]): Promise; @@ -29,14 +29,14 @@ export interface Waku { export interface WakuLight extends Waku { relay: undefined; - store: Store; - filter: Filter; - lightPush: LightPush; - peerExchange: PeerExchange; + store: IStore; + filter: IFilter; + lightPush: ILightPush; + peerExchange: IPeerExchange; } export interface WakuPrivacy extends Waku { - relay: Relay; + relay: IRelay; store: undefined; filter: undefined; lightPush: undefined; @@ -44,9 +44,9 @@ export interface WakuPrivacy extends Waku { } export interface WakuFull extends Waku { - relay: Relay; - store: Store; - filter: Filter; - lightPush: LightPush; - peerExchange: PeerExchange; + relay: IRelay; + store: IStore; + filter: IFilter; + lightPush: ILightPush; + peerExchange: IPeerExchange; } diff --git a/packages/peer-exchange/src/waku_peer_exchange.ts b/packages/peer-exchange/src/waku_peer_exchange.ts index c4adf2ecb7..99379bfc9c 100644 --- a/packages/peer-exchange/src/waku_peer_exchange.ts +++ b/packages/peer-exchange/src/waku_peer_exchange.ts @@ -4,7 +4,7 @@ import type { Peer, PeerStore } from "@libp2p/interface-peer-store"; import type { IncomingStreamData } from "@libp2p/interface-registrar"; import { ENR } from "@waku/enr"; import type { - PeerExchange, + IPeerExchange, PeerExchangeComponents, PeerExchangeQueryParams, PeerExchangeResponse, @@ -26,7 +26,7 @@ export const PeerExchangeCodec = "/vac/waku/peer-exchange/2.0.0-alpha1"; const log = debug("waku:peer-exchange"); -export class WakuPeerExchange implements PeerExchange { +export class WakuPeerExchange implements IPeerExchange { private callback: | ((response: PeerExchangeResponse) => Promise) | undefined;