UNPKG

mokka

Version:

Mokka Consensus Algorithm implementation in Javascript

54 lines 1.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const body_parser_1 = __importDefault(require("body-parser")); const express_1 = __importDefault(require("express")); const url_1 = require("url"); const main_1 = require("../consensus/main"); class RPCMokka extends main_1.Mokka { constructor() { super(...arguments); this.app = (0, express_1.default)(); } initialize() { this.app.use(body_parser_1.default.json()); this.app.post('/', (req, res) => { const packet = Buffer.from(req.body.data, 'hex'); this.emitPacket(packet); res.send({ ok: 1 }); }); const url = new url_1.URL(this.address); this.app.listen(url.port, () => { this.logger.info(`rpc started on port ${url.port}`); }); } /** * The message to write. * * @param {string} address The peer address * @param {Object} packet The packet to write to the connection. * @api private */ async write(address, packet) { await axios_1.default.post(address, { data: packet.toString('hex') }, { timeout: this.heartbeat }).catch((e) => { this.logger.trace(`received error from ${address}: ${e}`); }); } async disconnect() { await super.disconnect(); this.app.close(); } async connect() { this.initialize(); await super.connect(); } } exports.default = RPCMokka; //# sourceMappingURL=RPC.js.map