mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-08 16:54:08 +00:00
Pass initialization options when starting a peer (#328)
* Pass initialization options when starting a peer * Update config used for relay and peer nodes in watcher * Rename types * Refactor mobymask libp2p message parsing * Enable laconic debug logs in server command
This commit is contained in:
@@ -20,7 +20,7 @@ async function main (): Promise<void> {
|
||||
// https://adamcoster.com/blog/commonjs-and-esm-importexport-compatibility-examples#importing-esm-into-commonjs-cjs
|
||||
const { Peer } = await import('@cerc-io/peer');
|
||||
const peer = new Peer(argv.relayNode, true);
|
||||
await peer.init();
|
||||
await peer.init({});
|
||||
|
||||
peer.subscribeMessage((peerId: PeerId, message: string) => {
|
||||
console.log(`> ${peerId.toString()} > ${message}`);
|
||||
|
||||
@@ -28,8 +28,12 @@ import {
|
||||
P2PConfig
|
||||
} from '@cerc-io/util';
|
||||
import { TypeSource } from '@graphql-tools/utils';
|
||||
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
|
||||
import { RelayNodeInit, PeerIdObj } from '@cerc-io/peer';
|
||||
import {
|
||||
RelayNodeInitConfig,
|
||||
PeerInitConfig,
|
||||
PeerIdObj
|
||||
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
|
||||
} from '@cerc-io/peer';
|
||||
|
||||
import { BaseCmd } from './base';
|
||||
import { readPeerId } from './utils/index';
|
||||
@@ -145,7 +149,13 @@ export class ServerCmd {
|
||||
parseLibp2pMessage?: (peerId: string, data: any) => void
|
||||
): Promise<void> {
|
||||
const { createRelayNode, Peer } = await import('@cerc-io/peer');
|
||||
const { RELAY_DEFAULT_HOST, RELAY_DEFAULT_PORT, RELAY_DEFAULT_MAX_DIAL_RETRY } = await import('@cerc-io/peer');
|
||||
const {
|
||||
RELAY_DEFAULT_HOST,
|
||||
RELAY_DEFAULT_PORT,
|
||||
RELAY_DEFAULT_MAX_DIAL_RETRY,
|
||||
RELAY_REDIAL_INTERVAL,
|
||||
PING_INTERVAL
|
||||
} = await import('@cerc-io/peer');
|
||||
|
||||
// Run the relay node if enabled
|
||||
if (p2pConfig.enableRelay) {
|
||||
@@ -157,11 +167,13 @@ export class ServerCmd {
|
||||
peerIdObj = readPeerId(relayConfig.peerIdFile);
|
||||
}
|
||||
|
||||
const relayNodeInit: RelayNodeInit = {
|
||||
const relayNodeInit: RelayNodeInitConfig = {
|
||||
host: relayConfig.host ?? RELAY_DEFAULT_HOST,
|
||||
port: relayConfig.port ?? RELAY_DEFAULT_PORT,
|
||||
announceDomain: relayConfig.announce,
|
||||
relayPeers: relayConfig.relayPeers ?? [],
|
||||
pingInterval: relayConfig.pingInterval ?? PING_INTERVAL,
|
||||
redialInterval: relayConfig.redialInterval ?? RELAY_REDIAL_INTERVAL,
|
||||
maxDialRetry: relayConfig.maxDialRetry ?? RELAY_DEFAULT_MAX_DIAL_RETRY,
|
||||
peerIdObj
|
||||
};
|
||||
@@ -170,10 +182,22 @@ export class ServerCmd {
|
||||
|
||||
// Run a peer node if enabled
|
||||
if (p2pConfig.enablePeer) {
|
||||
const peer = new Peer(p2pConfig.relayMultiaddr, true);
|
||||
await peer.init();
|
||||
const peerConfig = p2pConfig.peer;
|
||||
assert(peerConfig, 'Peer config not set');
|
||||
|
||||
peer.subscribeTopic(p2pConfig.pubSubTopic, (peerId, data) => {
|
||||
const peer = new Peer(peerConfig.relayMultiaddr, true);
|
||||
|
||||
const peerNodeInit: PeerInitConfig = {
|
||||
pingInterval: peerConfig.pingInterval,
|
||||
pingTimeout: peerConfig.pingTimeout,
|
||||
maxRelayConnections: peerConfig.maxRelayConnections,
|
||||
relayRedialInterval: peerConfig.relayRedialInterval,
|
||||
maxConnections: peerConfig.maxConnections,
|
||||
dialTimeout: peerConfig.dialTimeout
|
||||
};
|
||||
await peer.init(peerNodeInit);
|
||||
|
||||
peer.subscribeTopic(peerConfig.pubSubTopic, (peerId, data) => {
|
||||
if (parseLibp2pMessage) {
|
||||
parseLibp2pMessage(peerId.toString(), data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user