UNPKG

1.89 kBPlain TextView Raw
1import * as IlpPacket from 'ilp-packet'
2import Config from '../services/config'
3import Accounts from '../services/accounts'
4import RouteBroadcaster from '../services/route-broadcaster'
5import RouteBuilder from '../services/route-builder'
6import IlpPrepareController from '../controllers/ilp-prepare'
7import { create as createLogger } from '../common/log'
8const log = createLogger('core-middleware')
9import reduct = require('reduct')
10const { InvalidPacketError } = IlpPacket.Errors
11
12export default class Core {
13 protected config: Config
14 protected accounts: Accounts
15 protected routeBroadcaster: RouteBroadcaster
16 protected routeBuilder: RouteBuilder
17 protected ilpPrepareController: IlpPrepareController
18
19 constructor (deps: reduct.Injector) {
20 this.config = deps(Config)
21 this.accounts = deps(Accounts)
22 this.routeBroadcaster = deps(RouteBroadcaster)
23 this.routeBuilder = deps(RouteBuilder)
24
25 this.ilpPrepareController = deps(IlpPrepareController)
26 }
27
28 async processData (data: Buffer, accountId: string, outbound: (data: Buffer, accountId: string) => Promise<Buffer>): Promise<Buffer> {
29 if (!this.accounts.getInfo(accountId)) {
30 log.warn('got data from unknown account id. accountId=%s', accountId)
31 throw new Error('got data from unknown account id. accountId=' + accountId)
32 }
33
34 if (!Buffer.isBuffer(data)) {
35 log.error('data handler was passed a non-buffer. typeof=%s data=%s', typeof data, data)
36 throw new Error('data handler was passed a non-buffer. typeof=' + typeof data)
37 }
38
39 switch (data[0]) {
40 case IlpPacket.Type.TYPE_ILP_PREPARE:
41 return this.ilpPrepareController.sendData(data, accountId, outbound)
42 default:
43 log.error('received invalid packet type. source=%s type=%s', accountId, data[0])
44 throw new InvalidPacketError('invalid packet type received. type=' + data[0])
45 }
46 }
47}