UNPKG

@rsksmart/rif-communications-pubsub-bootnode

Version:
71 lines (70 loc) 3.37 kB
#!/usr/bin/env node "use strict"; /* eslint no-console: 0 */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = __importDefault(require("config")); const rif_communications_pubsub_1 = require("@rsksmart/rif-communications-pubsub"); const peer_id_1 = __importDefault(require("peer-id")); const chalk_1 = __importDefault(require("chalk")); const util_1 = require("util"); function isValidPeerId(peerId) { return (peerId.isValid() && Boolean(peerId.toB58String()) && Boolean(peerId.privKey) && Boolean(peerId.pubKey)); } function formatMessage(msg) { const prefix = ' '; const topics = `${prefix} Topics: ${prefix} - ${msg.topicIDs.join(`\n${prefix} - `)}`; const data = util_1.inspect(msg.data, undefined, 3, true).split('\n').map(line => `${prefix} ${line}`).join('\n'); return `${prefix}${chalk_1.default.blue(`From: ${msg.from}`)} ${chalk_1.default.gray(topics)} ${data} `; } function main() { return __awaiter(this, void 0, void 0, function* () { const libp2pConfig = config_1.default.get('libp2p'); let libp2p; if (config_1.default.has('peerId')) { const cnfId = config_1.default.get('peerId'); const peerId = yield peer_id_1.default.createFromJSON(cnfId); if (!isValidPeerId(peerId)) { throw new Error('Supplied PeerId is not valid!'); } libp2p = yield rif_communications_pubsub_1.createLibP2P(Object.assign(Object.assign({}, libp2pConfig), { peerId })); } else { libp2p = yield rif_communications_pubsub_1.createLibP2P(libp2pConfig); } console.log('Node started, listening on addresses:'); libp2p.multiaddrs.forEach((addr) => { console.log(`${addr.toString()}/p2p/${libp2p.peerId.toB58String()}`); }); const rooms = config_1.default.get('rooms'); console.log('\nListening on topics: '); rooms.forEach((roomName) => { const room = new rif_communications_pubsub_1.Room(libp2p, roomName); console.log(` - ${roomName}`); room.on('peer:joined', (peer) => console.log(`${roomName}: ${chalk_1.default.green(`peer ${peer} joined`)}`)); room.on('peer:left', (peer) => console.log(`${roomName}: ${chalk_1.default.red(`peer ${peer} left`)}`)); room.on('message', (message) => console.log(`${roomName}: message\n`, formatMessage(message))); }); console.log('\n'); }); } exports.default = main; main();