-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor peer package for usage in watchers (#324)
* 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
1 parent
91c1c35
commit a56ade9
Showing
6 changed files
with
654 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.