Waku.dial accepts protocols expected from the peer. Defaults to Waku Relay only. (#516)

This commit is contained in:
Franck R 2022-02-13 19:04:50 +11:00 committed by GitHub
parent 0f678750df
commit 160fea0e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 35 deletions

View File

@ -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

View File

@ -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() {
<div className="App">
<header className="App-header">
<p>{wakuStatus}</p>
<button onClick={sendMessageOnClick} disabled={wakuStatus !== 'Ready'}>
<button onClick={sendMessageOnClick} disabled={wakuStatus !== "Ready"}>
Send Message
</button>
<ul>

View File

@ -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,
});
}

View File

@ -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();
}
});

View File

@ -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();
}
});

View File

@ -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);
}
/**

View File

@ -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: [