Adopt more typescript-y syntax, do not lint generated files

This commit is contained in:
Franck Royer 2021-03-10 14:30:31 +11:00
parent 5e64708111
commit 8e91ca6d01
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 33 additions and 38 deletions

View File

@ -3,7 +3,7 @@
"parser": "@typescript-eslint/parser", "parser": "@typescript-eslint/parser",
"parserOptions": { "project": "./tsconfig.json" }, "parserOptions": { "project": "./tsconfig.json" },
"env": { "es6": true }, "env": { "es6": true },
"ignorePatterns": ["node_modules", "build", "coverage"], "ignorePatterns": ["node_modules", "build", "coverage", "gen"],
"plugins": ["import", "eslint-comments", "functional"], "plugins": ["import", "eslint-comments", "functional"],
"extends": [ "extends": [
"eslint:recommended", "eslint:recommended",

View File

@ -4,48 +4,42 @@ import test from 'ava';
import { createNode } from './node'; import { createNode } from './node';
function delay(ms: number) function delay(ms: number) {
{ return new Promise((resolve) => setTimeout(resolve, ms));
return new Promise(resolve => setTimeout(resolve, ms));
} }
test('Can publish message', async (t) => { test('Can publish message', async (t) => {
const topic = 'news';
const topic = 'news' const [node1, node2] = await Promise.all([createNode(), createNode()]);
const [node1, node2] = await Promise.all([
createNode(),
createNode()
])
// Add node's 2 data to the PeerStore // Add node's 2 data to the PeerStore
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs) node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs);
await node1.dial(node2.peerId) await node1.dial(node2.peerId);
let node1Received = ""; let node1Received = '';
node1.pubsub.on(topic, (msg) => { node1.pubsub.on(topic, (msg) => {
node1Received = new TextDecoder().decode(msg.data); node1Received = new TextDecoder().decode(msg.data);
console.log(`node1 received: ${node1Received}`) console.log(`node1 received: ${node1Received}`);
});
}) await node1.pubsub.subscribe(topic);
await node1.pubsub.subscribe(topic)
// Will not receive own published messages by default // Will not receive own published messages by default
node2.pubsub.on(topic, (msg) => { node2.pubsub.on(topic, (msg) => {
console.log(`node2 received: ${new TextDecoder().decode(msg.data)}`) console.log(`node2 received: ${new TextDecoder().decode(msg.data)}`);
}) });
await node2.pubsub.subscribe(topic) await node2.pubsub.subscribe(topic);
const message = 'Bird bird bird, bird is the word!'; const message = 'Bird bird bird, bird is the word!';
await delay(1000); await delay(1000);
await node2.pubsub.publish(topic, new TextEncoder().encode(message)); await node2.pubsub.publish(topic, new TextEncoder().encode(message));
await delay(1000); await delay(1000);
t.deepEqual(node1Received, message) t.deepEqual(node1Received, message);
}); });

View File

@ -4,20 +4,21 @@ import Mplex from 'libp2p-mplex';
import { NOISE } from 'libp2p-noise'; import { NOISE } from 'libp2p-noise';
import TCP from 'libp2p-tcp'; import TCP from 'libp2p-tcp';
export const createNode = async () => { export async function createNode() {
const node = await Libp2p.create({ const node = await Libp2p.create({
addresses: { addresses: {
listen: ['/ip4/0.0.0.0/tcp/0'] listen: ['/ip4/0.0.0.0/tcp/0'],
}, },
modules: { modules: {
transport: [TCP], transport: [TCP],
streamMuxer: [Mplex], streamMuxer: [Mplex],
connEncryption: [NOISE], connEncryption: [NOISE],
// @ts-ignore: Type needs update // eslint-disable-next-line @typescript-eslint/ban-ts-comment
pubsub: Gossipsub // @ts-ignore: Type needs update
} pubsub: Gossipsub,
}) },
});
await node.start() await node.start();
return node return node;
} }