1 2
import * as IlpPacket from 'ilp-packet'
2 2
import Config from '../services/config'
3 2
import Accounts from '../services/accounts'
4 2
import RouteBroadcaster from '../services/route-broadcaster'
5 2
import RouteBuilder from '../services/route-builder'
6 2
import IlpPrepareController from '../controllers/ilp-prepare'
7 2
import { create as createLogger } from '../common/log'
8 2
const log = createLogger('core-middleware')
9
import reduct = require('reduct')
10 2
const { InvalidPacketError } = IlpPacket.Errors
11

12 2
export 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 2
    this.config = deps(Config)
21 2
    this.accounts = deps(Accounts)
22 2
    this.routeBroadcaster = deps(RouteBroadcaster)
23 2
    this.routeBuilder = deps(RouteBuilder)
24

25 2
    this.ilpPrepareController = deps(IlpPrepareController)
26
  }
27

28
  async processData (data: Buffer, accountId: string, outbound: (data: Buffer, accountId: string) => Promise<Buffer>): Promise<Buffer> {
29 2
    if (!this.accounts.getInfo(accountId)) {
30 0
      log.warn('got data from unknown account id. accountId=%s', accountId)
31 0
      throw new Error('got data from unknown account id. accountId=' + accountId)
32
    }
33

34 2
    if (!Buffer.isBuffer(data)) {
35 0
      log.error('data handler was passed a non-buffer. typeof=%s data=%s', typeof data, data)
36 0
      throw new Error('data handler was passed a non-buffer. typeof=' + typeof data)
37
    }
38

39 2
    switch (data[0]) {
40 2
      case IlpPacket.Type.TYPE_ILP_PREPARE:
41 2
        return this.ilpPrepareController.sendData(data, accountId, outbound)
42
      default:
43 0
        log.error('received invalid packet type. source=%s type=%s', accountId, data[0])
44 0
        throw new InvalidPacketError('invalid packet type received. type=' + data[0])
45
    }
46
  }
47
}

Read our documentation on viewing source code .

Loading