UNPKG

3.42 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const IlpPacket = require("ilp-packet");
4class StatsMiddleware {
5 constructor(opts, { stats, getInfo }) {
6 this.stats = stats;
7 this.getInfo = getInfo;
8 }
9 async applyToPipelines(pipelines, accountId) {
10 const accountInfo = this.getInfo(accountId);
11 if (!accountInfo) {
12 throw new Error('could not load info for account. accountId=' + accountId);
13 }
14 const account = { accountId, accountInfo };
15 pipelines.incomingData.insertLast({
16 name: 'stats',
17 method: async (data, next) => {
18 try {
19 const result = await next(data);
20 if (result[0] === IlpPacket.Type.TYPE_ILP_FULFILL) {
21 this.stats.incomingDataPackets.increment(account, { result: 'fulfilled' });
22 }
23 else {
24 this.stats.incomingDataPackets.increment(account, { result: 'rejected' });
25 }
26 return result;
27 }
28 catch (err) {
29 this.stats.incomingDataPackets.increment(account, { result: 'failed' });
30 throw err;
31 }
32 }
33 });
34 pipelines.incomingMoney.insertLast({
35 name: 'stats',
36 method: async (amount, next) => {
37 try {
38 const result = await next(amount);
39 this.stats.incomingMoney.setValue(account, { result: 'succeeded' }, +amount);
40 return result;
41 }
42 catch (err) {
43 this.stats.incomingMoney.setValue(account, { result: 'failed' }, +amount);
44 throw err;
45 }
46 }
47 });
48 pipelines.outgoingData.insertLast({
49 name: 'stats',
50 method: async (data, next) => {
51 try {
52 const result = await next(data);
53 if (result[0] === IlpPacket.Type.TYPE_ILP_FULFILL) {
54 this.stats.outgoingDataPackets.increment(account, { result: 'fulfilled' });
55 }
56 else {
57 const rejectPacket = IlpPacket.deserializeIlpReject(result);
58 const { code } = rejectPacket;
59 this.stats.outgoingDataPackets.increment(account, { result: 'rejected', code });
60 }
61 return result;
62 }
63 catch (err) {
64 this.stats.outgoingDataPackets.increment(account, { result: 'failed' });
65 throw err;
66 }
67 }
68 });
69 pipelines.outgoingMoney.insertLast({
70 name: 'stats',
71 method: async (amount, next) => {
72 try {
73 const result = await next(amount);
74 this.stats.outgoingMoney.setValue(account, { result: 'succeeded' }, +amount);
75 return result;
76 }
77 catch (err) {
78 this.stats.outgoingMoney.setValue(account, { result: 'failed' }, +amount);
79 throw err;
80 }
81 }
82 });
83 }
84}
85exports.default = StatsMiddleware;
86//# sourceMappingURL=stats.js.map
\No newline at end of file