-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
58 lines (48 loc) · 1.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* eslint-disable import/no-extraneous-dependencies */
import axios from 'axios';
import bodyParser from 'body-parser';
import express from 'express';
import { URL } from 'url';
import ABGP from 'abgp-js';
import PacketModel from 'abgp-js/dist/consensus/models/PacketModel';
class RPCABGP extends ABGP {
private app = express();
public initialize() {
this.app.use(bodyParser.json());
this.app.post('/', async (req, res) => {
const packet = Buffer.from(req.body.data, 'hex');
const decoded = this.messageApi.decodePacket(packet.toString());
// eslint-disable-next-line @typescript-eslint/no-floating-promises
const reply = await this.requestProcessorService.process(decoded);
res.send(Buffer.from(JSON.stringify(reply)).toString('hex'));
});
const url = new 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
*/
public async call(address: string, packet: PacketModel): Promise<PacketModel> {
const reply = await axios.post(address, {
data: Buffer.from(JSON.stringify(packet)).toString('hex')
}, {
timeout: this.gossipInterval.max
});
return this.messageApi.decodePacket(Buffer.from(reply.data, 'hex').toString());
}
public async disconnect(): Promise<void> {
await super.disconnect();
this.app.close();
}
public async connect(): Promise<void> {
this.initialize();
await super.connect();
}
}
export default RPCABGP;