mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-09 09:14:06 +00:00
Add mobymask-v2-watcher with integrated relay and peer functionality (#327)
* Add an option to run relay node with watcher server * Add a v2 mobymask-watcher * Add an option to run peer node with watcher server * Ignore ts error when importing types from peer package * Rename mobymask v2 watcher * Parse mobymask libp2p messages * Refactor and add comments * Add a note in peer package about nodejs exports * Update copyright
This commit is contained in:
@@ -10,6 +10,10 @@ Package used for connecting between peers and send messages
|
||||
- [x] Use package in server
|
||||
- [x] Send messages between systems in different LANs using relay node
|
||||
|
||||
## Note
|
||||
|
||||
- Avoid any nodejs specific exports from this package as it is intented to be used in browser applications as well
|
||||
|
||||
## Known Issues
|
||||
|
||||
- `peer:disconnect` event is not fired when remote peer browser is closed
|
||||
|
||||
@@ -5,10 +5,7 @@ import path from 'path';
|
||||
|
||||
import { RelayNodeInit, createRelayNode } from '../relay.js';
|
||||
import { PeerIdObj } from '../peer.js';
|
||||
|
||||
const DEFAULT_HOST = '127.0.0.1';
|
||||
const DEFAULT_PORT = 9090;
|
||||
const DEFAULT_MAX_DIAL_RETRY = 5;
|
||||
import { RELAY_DEFAULT_HOST, RELAY_DEFAULT_PORT, RELAY_DEFAULT_MAX_DIAL_RETRY } from '../constants.js';
|
||||
|
||||
interface Arguments {
|
||||
host: string;
|
||||
@@ -58,20 +55,20 @@ async function main (): Promise<void> {
|
||||
await createRelayNode(relayNodeInit);
|
||||
}
|
||||
|
||||
function _getArgv (): any {
|
||||
function _getArgv (): Arguments {
|
||||
return yargs(hideBin(process.argv)).parserConfiguration({
|
||||
'parse-numbers': false
|
||||
}).options({
|
||||
host: {
|
||||
type: 'string',
|
||||
alias: 'h',
|
||||
default: DEFAULT_HOST,
|
||||
default: RELAY_DEFAULT_HOST,
|
||||
describe: 'Host to bind to'
|
||||
},
|
||||
port: {
|
||||
type: 'number',
|
||||
alias: 'p',
|
||||
default: DEFAULT_PORT,
|
||||
default: RELAY_DEFAULT_PORT,
|
||||
describe: 'Port to start listening on'
|
||||
},
|
||||
announce: {
|
||||
@@ -91,8 +88,8 @@ function _getArgv (): any {
|
||||
},
|
||||
maxDialRetry: {
|
||||
type: 'number',
|
||||
describe: 'Maximum number of retries for dialling a relay peer',
|
||||
default: DEFAULT_MAX_DIAL_RETRY
|
||||
describe: 'Maximum number of dial retries to be attempted to a relay peer',
|
||||
default: RELAY_DEFAULT_MAX_DIAL_RETRY
|
||||
}
|
||||
// https://github.com/yargs/yargs/blob/main/docs/typescript.md?plain=1#L83
|
||||
}).parseSync();
|
||||
|
||||
@@ -55,3 +55,14 @@ export const MIN_CONNECTIONS = 0;
|
||||
|
||||
// How long a dial is allowed to take before it's aborted
|
||||
export const DIAL_TIMEOUT = 10000; // 10 seconds
|
||||
|
||||
// Relay node defaults
|
||||
|
||||
// Default host to bind relay server to
|
||||
export const RELAY_DEFAULT_HOST = '127.0.0.1';
|
||||
|
||||
// Default port to start listening on
|
||||
export const RELAY_DEFAULT_PORT = 9090;
|
||||
|
||||
// Default max number of dial retries to a relay peer
|
||||
export const RELAY_DEFAULT_MAX_DIAL_RETRY = 5;
|
||||
|
||||
@@ -5,3 +5,4 @@
|
||||
export { Peer, PeerIdObj, createPeerId } from './peer.js';
|
||||
export { RelayNodeInit, createRelayNode } from './relay.js';
|
||||
export { getPseudonymForPeerId } from './utils/index.js';
|
||||
export { RELAY_DEFAULT_HOST, RELAY_DEFAULT_PORT, RELAY_DEFAULT_MAX_DIAL_RETRY } from './constants.js';
|
||||
|
||||
@@ -152,7 +152,7 @@ export class Peer {
|
||||
console.log('libp2p node created', this._node);
|
||||
this._peerHeartbeatChecker = new PeerHearbeatChecker(this._node);
|
||||
|
||||
// Dial to the HOP enabled relay node
|
||||
// Dial to the HOP enabled primary relay node
|
||||
await this._dialRelay();
|
||||
|
||||
// Listen for change in stored multiaddrs
|
||||
@@ -323,7 +323,7 @@ export class Peer {
|
||||
async _dialRelay (): Promise<void> {
|
||||
assert(this._node);
|
||||
const relayMultiaddr = this._relayNodeMultiaddr;
|
||||
console.log('Dialling relay node');
|
||||
console.log('Dialling primary relay node');
|
||||
|
||||
const connection = await dialWithRetry(
|
||||
this._node,
|
||||
@@ -473,7 +473,7 @@ export class Peer {
|
||||
this._peerHeartbeatChecker?.stop(disconnectedPeerId);
|
||||
|
||||
if (disconnectedPeerId.toString() === this._relayNodeMultiaddr?.getPeerId()) {
|
||||
// Reconnect to relay node if disconnected
|
||||
// Reconnect to primary relay node if disconnected
|
||||
await this._dialRelay();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user