Simulate a network graph of connection streams.
$ npm install @dxos/network-generator
import { NetworkGenerator } from '@dxos/network-generator';
const generator = new NetworkGenerator({
async createPeer(id) {
// defines the peer object for the network. An "id" is required.
return { id, name: `peer${nodeId}` };
}
async createConnection(peerFrom, peerTo) {
// do something to connect peerFrom <--> peerTo
}
});
(async () => {
const network = await generator.balancedBinTree(5);
console.log(network.peers)
console.log(network.connections)
})();
Creates a network generator instance.
options
:createPeer: async (id: Buffer) => Object
: Defines how to create the peer object.createConnection: async (peerFrom, peerTo) => Stream
: Defines how to create a connection between peerFrom and peerTo. It can return an optional stream.
Creates a network using a balanced binary tree topology n levels
n: number
: Number of levels for the binary tree.
NetworkGenerator uses ngraph.generator, you can generate any topology supported by them topologies
Returns an array of peers.
Returns an array of connections.
Returns the ngraph instance.
Adds a new peer to the network.
Deletes a peer from the network.
Adds a new connection to the network.
Connection: { fromPeer, toPeer, stream }
Deletes a connection from the network.
Destroy the entire network.
PRs accepted.
GPL-3.0 © dxos