Skip to content

Commit

Permalink
Refactor peer package for usage in watchers (#324)
Browse files Browse the repository at this point in the history
* Refactor protocol change handler

* Refactor protocol stream creation

* Refactor Peer constructor

* Refactor peer CLIs to a separate cli folder

* Move peer node setup to a separate file

* Add a getter method for primary relay node mutiaddr

* Close new relay peer connection if limit already reached
  • Loading branch information
prathamesh0 authored Feb 17, 2023
1 parent 91c1c35 commit a56ade9
Show file tree
Hide file tree
Showing 6 changed files with 654 additions and 598 deletions.
4 changes: 2 additions & 2 deletions packages/peer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"build": "tsc",
"lint": "eslint .",
"dev": "node dist/index.js",
"create-peer": "node dist/create-peer.js",
"relay-node": "node dist/relay.js"
"create-peer": "node dist/cli/create-peer.js",
"relay-node": "node dist/cli/relay.js"
},
"dependencies": {
"@cerc-io/libp2p": "0.42.2-laconic-0.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from 'path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs';

import { createPeerId } from './index.js';
import { createPeerId } from '../peer.js';

interface Arguments {
file: string;
Expand Down
103 changes: 103 additions & 0 deletions packages/peer/src/cli/relay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import fs from 'fs';
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;

interface Arguments {
host: string;
port: number;
announce?: string;
peerIdFile?: string;
relayPeers?: string;
maxDialRetry: number;
}

async function main (): Promise<void> {
const argv: Arguments = _getArgv();
let peerIdObj: PeerIdObj | undefined;
let relayPeersList: string[] = [];

if (argv.peerIdFile) {
const peerIdFilePath = path.resolve(argv.peerIdFile);
console.log(`Reading peer id from file ${peerIdFilePath}`);

const peerIdJson = fs.readFileSync(peerIdFilePath, 'utf-8');
peerIdObj = JSON.parse(peerIdJson);
} else {
console.log('Creating a new peer id');
}

if (argv.relayPeers) {
const relayPeersFilePath = path.resolve(argv.relayPeers);

if (!fs.existsSync(relayPeersFilePath)) {
console.log(`File at given path ${relayPeersFilePath} not found, exiting`);
process.exit();
}

console.log(`Reading relay peer multiaddr(s) from file ${relayPeersFilePath}`);
const relayPeersListObj = fs.readFileSync(relayPeersFilePath, 'utf-8');
relayPeersList = JSON.parse(relayPeersListObj);
}

const relayNodeInit: RelayNodeInit = {
host: argv.host,
port: argv.port,
announceDomain: argv.announce,
relayPeers: relayPeersList,
maxDialRetry: argv.maxDialRetry,
peerIdObj
};
await createRelayNode(relayNodeInit);
}

function _getArgv (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false
}).options({
host: {
type: 'string',
alias: 'h',
default: DEFAULT_HOST,
describe: 'Host to bind to'
},
port: {
type: 'number',
alias: 'p',
default: DEFAULT_PORT,
describe: 'Port to start listening on'
},
announce: {
type: 'string',
alias: 'a',
describe: 'Domain name to be used in the announce address'
},
peerIdFile: {
type: 'string',
alias: 'f',
describe: 'Relay Peer Id file path (json)'
},
relayPeers: {
type: 'string',
alias: 'r',
describe: 'Relay peer multiaddr(s) list file path (json)'
},
maxDialRetry: {
type: 'number',
describe: 'Maximum number of retries for dialling a relay peer',
default: DEFAULT_MAX_DIAL_RETRY
}
// https://github.com/yargs/yargs/blob/main/docs/typescript.md?plain=1#L83
}).parseSync();
}

main().catch(err => {
console.log(err);
});
Loading

0 comments on commit a56ade9

Please sign in to comment.