From 160fea0e7bae9091f9f2a386f48e98a51f8cb7a0 Mon Sep 17 00:00:00 2001 From: Franck R Date: Sun, 13 Feb 2022 19:04:50 +1100 Subject: [PATCH] `Waku.dial` accepts protocols expected from the peer. Defaults to Waku Relay only. (#516) --- CHANGELOG.md | 1 + examples/relay-reactjs-chat/src/App.js | 23 ++++++++-------- examples/store-reactjs-chat/src/App.js | 36 +++++++++++++------------- src/lib/discovery/dns.spec.ts | 6 ++++- src/lib/waku.spec.ts | 6 ++++- src/lib/waku.ts | 21 +++++++++++++-- src/test_utils/nim_waku.ts | 2 +- 7 files changed, 60 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 405eef7b26..c64adbb045 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Breaking**: Upgrade `libp2p` to `0.36.2` & `libp2p-gossipsub` to `0.13.0`. Some APIs are now async. - docs: Various improvements. - Ran `npm audit fix`. +- `Waku.dial` accepts protocols expected from the peer. Defaults to Waku Relay only. ## [0.16.0] - 2022-01-31 diff --git a/examples/relay-reactjs-chat/src/App.js b/examples/relay-reactjs-chat/src/App.js index 5de7fdf257..0d06aedbb8 100644 --- a/examples/relay-reactjs-chat/src/App.js +++ b/examples/relay-reactjs-chat/src/App.js @@ -1,7 +1,6 @@ -import { Waku } from 'js-waku'; -import * as React from 'react'; -import protons from 'protons'; -import { WakuMessage } from 'js-waku'; +import { Waku, WakuMessage } from "js-waku"; +import * as React from "react"; +import protons from "protons"; const ContentTopic = `/relay-reactjs-chat/1/chat/proto`; @@ -14,22 +13,22 @@ message SimpleChatMessage { function App() { const [waku, setWaku] = React.useState(undefined); - const [wakuStatus, setWakuStatus] = React.useState('None'); + const [wakuStatus, setWakuStatus] = React.useState("None"); // Using a counter just for the messages to be different const [sendCounter, setSendCounter] = React.useState(0); const [messages, setMessages] = React.useState([]); React.useEffect(() => { if (!!waku) return; - if (wakuStatus !== 'None') return; + if (wakuStatus !== "None") return; - setWakuStatus('Starting'); + setWakuStatus("Starting"); Waku.create({ bootstrap: { default: true } }).then((waku) => { setWaku(waku); - setWakuStatus('Connecting'); + setWakuStatus("Connecting"); waku.waitForRemotePeer().then(() => { - setWakuStatus('Ready'); + setWakuStatus("Ready"); }); }); }, [waku, wakuStatus]); @@ -64,10 +63,10 @@ function App() { const sendMessageOnClick = () => { // Check Waku is started and connected first. - if (wakuStatus !== 'Ready') return; + if (wakuStatus !== "Ready") return; sendMessage(`Here is message #${sendCounter}`, waku, new Date()).then(() => - console.log('Message sent') + console.log("Message sent") ); // For demonstration purposes. @@ -78,7 +77,7 @@ function App() {

{wakuStatus}

-
    diff --git a/examples/store-reactjs-chat/src/App.js b/examples/store-reactjs-chat/src/App.js index 3e05d62ed7..fbe0981cf5 100644 --- a/examples/store-reactjs-chat/src/App.js +++ b/examples/store-reactjs-chat/src/App.js @@ -1,8 +1,8 @@ -import { Waku } from 'js-waku'; -import * as React from 'react'; -import protons from 'protons'; +import { Waku } from "js-waku"; +import * as React from "react"; +import protons from "protons"; -const ContentTopic = '/toy-chat/2/huilong/proto'; +const ContentTopic = "/toy-chat/2/huilong/proto"; const proto = protons(` message ChatMessage { @@ -14,17 +14,17 @@ message ChatMessage { function App() { const [waku, setWaku] = React.useState(undefined); - const [wakuStatus, setWakuStatus] = React.useState('None'); + const [wakuStatus, setWakuStatus] = React.useState("None"); const [messages, setMessages] = React.useState([]); React.useEffect(() => { - if (wakuStatus !== 'None') return; + if (wakuStatus !== "None") return; - setWakuStatus('Starting'); + setWakuStatus("Starting"); Waku.create({ bootstrap: { default: true } }).then((waku) => { setWaku(waku); - setWakuStatus('Connecting'); + setWakuStatus("Connecting"); }); }, [waku, wakuStatus]); @@ -32,16 +32,16 @@ function App() { if (!waku) return; // We do not handle disconnection/re-connection in this example - if (wakuStatus === 'Connected') return; + if (wakuStatus === "Connected") return; waku.waitForRemotePeer().then(() => { // We are now connected to a store node - setWakuStatus('Connected'); + setWakuStatus("Connected"); }); }, [waku, wakuStatus]); React.useEffect(() => { - if (wakuStatus !== 'Connected') return; + if (wakuStatus !== "Connected") return; const processMessages = (retrievedMessages) => { const messages = retrievedMessages.map(decodeMessage).filter(Boolean); @@ -61,7 +61,7 @@ function App() { timeFilter: { startTime, endTime: new Date() }, }) .catch((e) => { - console.log('Failed to retrieve messages', e); + console.log("Failed to retrieve messages", e); }); }, [waku, wakuStatus]); @@ -92,7 +92,7 @@ function decodeMessage(wakuMessage) { const time = new Date(); time.setTime(timestamp); - const utf8Text = Buffer.from(text).toString('utf-8'); + const utf8Text = Buffer.from(text).toString("utf-8"); return { text: utf8Text, timestamp: time, nick }; } @@ -109,11 +109,11 @@ function Messages(props) { function formatDate(timestamp) { return timestamp.toLocaleString([], { - month: 'short', - day: 'numeric', - hour: 'numeric', - minute: '2-digit', - second: '2-digit', + month: "short", + day: "numeric", + hour: "numeric", + minute: "2-digit", + second: "2-digit", hour12: false, }); } diff --git a/src/lib/discovery/dns.spec.ts b/src/lib/discovery/dns.spec.ts index c5b73b45c7..e642135daf 100644 --- a/src/lib/discovery/dns.spec.ts +++ b/src/lib/discovery/dns.spec.ts @@ -8,6 +8,7 @@ declare global { __env__?: any; } } +declare let window: Window | undefined; const mockData = testData.dns; @@ -182,7 +183,10 @@ describe("DNS Node Discovery [live data]", function () { const maxQuantity = 3; before(function () { - if (process.env.CI || window.__env__.CI) { + if ( + process.env.CI || + (typeof window !== "undefined" && window?.__env__?.CI) + ) { this.skip(); } }); diff --git a/src/lib/waku.spec.ts b/src/lib/waku.spec.ts index 17bf558890..03a57d6eb6 100644 --- a/src/lib/waku.spec.ts +++ b/src/lib/waku.spec.ts @@ -8,6 +8,7 @@ declare global { __env__?: any; } } +declare let window: Window | undefined; describe("Waku Dial", function () { describe("Bootstrap [live data]", function () { @@ -18,7 +19,10 @@ describe("Waku Dial", function () { }); before(function () { - if (process.env.CI || window.__env__.CI) { + if ( + process.env.CI || + (typeof window !== "undefined" && window?.__env__?.CI) + ) { this.skip(); } }); diff --git a/src/lib/waku.ts b/src/lib/waku.ts index d4920c4d4a..42908da2b4 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -231,12 +231,29 @@ export class Waku { * Dials to the provided peer. * * @param peer The peer to dial + * @param protocols Waku protocols we expect from the peer; Default to Relay */ - async dial(peer: PeerId | Multiaddr | string): Promise<{ + async dial( + peer: PeerId | Multiaddr | string, + protocols?: Protocols[] + ): Promise<{ stream: MuxedStream; protocol: string; }> { - return this.libp2p.dialProtocol(peer, [StoreCodec].concat(RelayCodecs)); + const _protocols = protocols ?? [Protocols.Relay]; + + const codecs: string[] = []; + if (_protocols.includes(Protocols.Relay)) { + RelayCodecs.forEach((codec) => codecs.push(codec)); + } + if (_protocols.includes(Protocols.Store)) { + codecs.push(StoreCodec); + } + if (_protocols.includes(Protocols.LightPush)) { + codecs.push(LightPushCodec); + } + + return this.libp2p.dialProtocol(peer, codecs); } /** diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index 1c11236eab..5e8cc93294 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -118,7 +118,7 @@ export class NimWaku { ); const argsArray = argsToArray(mergedArgs); - dbg(`nim-waku args: ${argsArray}`); + dbg(`nim-waku args: ${argsArray.join(" ")}`); this.process = spawn(NIM_WAKU_BIN, argsArray, { cwd: NIM_WAKU_DIR, stdio: [