Skip to content

Commit c9b871a

Browse files
authored
Merge pull request #2 from interledgerjs/feat/factories
Use provider pattern
2 parents e9c265d + a6ef536 commit c9b871a

File tree

15 files changed

+1507
-3990
lines changed

15 files changed

+1507
-3990
lines changed

package-lock.json

Lines changed: 1358 additions & 3742 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,33 @@
4444
"bignumber.js": "^8.0.1",
4545
"debug": "^3.1.0",
4646
"ilp-logger": "^1.1.2",
47-
"ilp-packet": "^3.0.5",
47+
"ilp-packet": "^3.0.6",
4848
"ilp-store": "^1.0.1",
49-
"ilp-transport-grpc": "github:interledgerjs/ilp-transport-grpc",
5049
"prom-client": "^11.2.0"
5150
},
5251
"devDependencies": {
53-
"@types/chai": "^4.0.10",
54-
"@types/debug": "0.0.30",
55-
"@types/long": "^3.0.32",
56-
"@types/mocha": "^2.2.45",
52+
"@types/chai": "^4.1.7",
53+
"@types/debug": "^0.0.31",
54+
"@types/long": "^4.0.0",
55+
"@types/mocha": "^5.2.5",
5756
"@types/mock-require": "^2.0.0",
58-
"@types/node": "^8.5.2",
59-
"@types/sinon": "^4.1.2",
60-
"chai": "^4.1.2",
61-
"codecov": "^3.0.0",
62-
"gh-pages": "^1.1.0",
63-
"ilp-protocol-ildcp": "^1.0.0",
64-
"json-schema-to-typescript": "^6.0.1",
65-
"mocha": "^4.1.0",
66-
"mocha-typescript": "^1.1.12",
67-
"mock-require": "^2.0.2",
68-
"nyc": "^11.4.1",
69-
"sinon": "^4.1.3",
57+
"@types/node": "^10.12.10",
58+
"@types/sinon": "^5.0.7",
59+
"chai": "^4.2.0",
60+
"codecov": "^3.1.0",
61+
"gh-pages": "^2.0.1",
62+
"ilp-protocol-ildcp": "^2.0.0",
63+
"json-schema-to-typescript": "^6.0.2",
64+
"mocha": "^5.2.0",
65+
"mocha-typescript": "^1.1.17",
66+
"mock-require": "^3.0.2",
67+
"nyc": "^13.1.0",
68+
"sinon": "^7.1.1",
7069
"source-map-support": "^0.5.9",
71-
"ts-node": "^4.1.0",
72-
"tslint": "^5.8.0",
73-
"tslint-config-standard": "^7.0.0",
74-
"typescript": "^2.6.2"
70+
"ts-node": "^7.0.1",
71+
"tslint": "^5.11.0",
72+
"tslint-config-standard": "^8.0.1",
73+
"typescript": "^3.1.6"
7574
},
7675
"nyc": {
7776
"check-coverage": true,

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import PluginAccountService from './implementations/plugin'
2-
import { AccountInfo } from 'ilp-transport-grpc/dist/lib/account'
32
import { PluginInstance } from './types/plugin'
43
import { AccountService } from './types/account-service'
4+
import { AccountInfo } from './types/accounts'
55
import { default as createLogger } from 'ilp-logger'
66
const log = createLogger('app')
77

src/implementations/base.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import createLogger from 'ilp-logger'
33
import MiddlewareManager from '../services/middleware-manager'
44
import { AccountService } from '../types/account-service'
55
import { MoneyHandler } from '../types/plugin'
6-
import { IlpPrepare, IlpPacketHander, IlpReply } from 'ilp-packet'
7-
import { UnreachableError } from 'ilp-packet/dist/src/errors'
6+
import { IlpPrepare, IlpPacketHander, IlpReply, Errors } from 'ilp-packet'
7+
import { EventEmitter } from 'events'
8+
const { UnreachableError } = Errors
89
const log = createLogger('plugin-account-service')
910

10-
export class AccountServiceBase implements AccountService {
11+
export class AccountServiceBase extends EventEmitter implements AccountService {
1112

1213
protected _id: string
1314
protected _info: AccountInfo
14-
protected _connectHandler?: () => void
15-
protected _disconnectHandler?: () => void
1615
protected _outgoingIlpPacketHandler?: IlpPacketHander
1716
protected _outgoingMoneyHandler?: MoneyHandler
1817
protected _incomingIlpPacketHandler: IlpPacketHander
@@ -21,6 +20,7 @@ export class AccountServiceBase implements AccountService {
2120
private _started: boolean = false
2221

2322
constructor (accountId: string, accountInfo: AccountInfo, middlewares: string[]) {
23+
super()
2424
this._id = accountId
2525
this._info = accountInfo
2626
if (middlewares.length > 0) {
@@ -39,7 +39,7 @@ export class AccountServiceBase implements AccountService {
3939
return this._id
4040
}
4141

42-
getInfo () {
42+
public get info () {
4343
return this._info
4444
}
4545

@@ -99,34 +99,6 @@ export class AccountServiceBase implements AccountService {
9999
throw new Error('isConnected must be implemented.')
100100
}
101101

102-
registerConnectHandler (handler: () => void) {
103-
if (this._connectHandler) {
104-
log.error('Connect handler already exists for account: ' + this._id)
105-
throw new Error('Connect handler already exists for account: ' + this._id)
106-
}
107-
this._connectHandler = handler
108-
}
109-
110-
deregisterConnectHandler () {
111-
if (this._connectHandler) {
112-
this._connectHandler = undefined
113-
}
114-
}
115-
116-
registerDisconnectHandler (handler: () => void) {
117-
if (this._disconnectHandler) {
118-
log.error('Disconnect handler already exists for account: ' + this._id)
119-
throw new Error('Disconnect handler already exists for account: ' + this._id)
120-
}
121-
this._disconnectHandler = handler
122-
}
123-
124-
deregisterDisconnectHandler () {
125-
if (this._disconnectHandler) {
126-
this._disconnectHandler = undefined
127-
}
128-
}
129-
130102
registerIlpPacketHandler (handler: IlpPacketHander) {
131103
if (this._started) {
132104
log.error('Can\'t register handler after sertvice has started.')

src/implementations/grpc-plugin-proxy.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/implementations/grpc.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/implementations/plugin.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export default class PluginAccountService extends AccountServiceBase implements
1313
constructor (accountId: string, accountInfo: AccountInfo, plugin: PluginInstance, middlewares: string[]) {
1414
super(accountId, accountInfo, middlewares)
1515
this._plugin = plugin
16-
this._plugin.on('connect', this._pluginConnect.bind(this))
17-
this._plugin.on('disconnect', this._pluginDisconnect.bind(this))
16+
this._plugin.on('connect', this.emit.bind(this, 'connect'))
17+
this._plugin.on('disconnect', this.emit.bind(this, 'disconnect'))
1818

1919
this._outgoingIlpPacketHandler = async (packet) => {
2020
return deserializeIlpPacket(await this._plugin.sendData(serializeIlpPrepare(packet))).data as IlpReply
@@ -53,14 +53,6 @@ export default class PluginAccountService extends AccountServiceBase implements
5353
return this._plugin.isConnected()
5454
}
5555

56-
protected async _pluginConnect () {
57-
if (this._connectHandler) return this._connectHandler()
58-
}
59-
60-
protected async _pluginDisconnect () {
61-
if (this._disconnectHandler) return this._disconnectHandler()
62-
}
63-
6456
public getPlugin () {
6557
return this._plugin
6658
}

src/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#!/usr/bin/env node
22

3+
export { AccountInfo, AccountEntry } from './types/accounts'
34
export { AccountService } from './types/account-service'
45
export { AccountServiceBase } from './implementations/base'
6+
export {
7+
AccountServiceProvider,
8+
AccountServiceProviderServices,
9+
AccountServiceProviderDefinition } from './types/account-service-provider'
510
export { default as PluginAccountService } from './implementations/plugin'
11+
export { default as PluginAccountServiceProvider } from './providers/plugin'
12+
613
import { default as createLogger } from 'ilp-logger'
714
import { default as createStore } from 'ilp-store'
8-
import GrpcAccountService from './implementations/grpc'
9-
import { createConnection } from 'ilp-transport-grpc'
1015
import { AccountInfo } from './types/accounts'
1116
import createApp from './app'
17+
import { AccountService } from './types/account-service'
1218
const log = createLogger('ilp-account-service')
1319
require('source-map-support').install()
1420

@@ -17,8 +23,8 @@ if (!module.parent) {
1723
const accountId = process.env.ACCOUNT_ID
1824
const accountInfo = JSON.parse(process.env.ACCOUNT_INFO || '{}') as AccountInfo
1925
const opts = Object.assign({}, accountInfo.options)
20-
const Plugin = require(accountInfo.plugin || 'ilp-plugin-btp')
21-
const plugin = new Plugin(opts, {
26+
const pluginModule = accountInfo.plugin ? String(accountInfo.plugin) : 'ilp-plugin-btp'
27+
const plugin = new (require(pluginModule))(opts, {
2228
log: createLogger('ilp-account-service[plugin]'),
2329
store: createStore()
2430
})
@@ -32,8 +38,11 @@ if (!module.parent) {
3238

3339
const run = async () => {
3440

35-
const client = await createConnection(connectorUrl, { accountId, accountInfo })
36-
const uplink = new GrpcAccountService(accountId, accountInfo, client)
41+
// TODO - Provide a better default. We can build but this won't work
42+
// const client = await createConnection(connectorUrl, { accountId, accountInfo })
43+
// const uplink = new GrpcAccountService(accountId, accountInfo, client)
44+
45+
const uplink = {} as AccountService
3746
const middlewares = ['error-handler', 'rate-limit', 'throughput', 'balance', 'expire']
3847
const app = createApp(accountId, accountInfo, plugin, uplink, middlewares)
3948

@@ -61,5 +70,5 @@ if (!module.parent) {
6170
}
6271
})
6372
}
64-
run()
73+
run().catch(e => console.error)
6574
}

src/lib/utils.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { createHash, randomBytes, createHmac } from 'crypto'
22
import { resolve } from 'path'
33
import { MiddlewareMethod } from '../types/middleware'
44

5-
export const sha256 = (preimage: Buffer) => {
6-
return createHash('sha256').update(preimage).digest()
7-
}
8-
95
export function moduleExists (path: string) {
106
try {
117
require.resolve(path)
@@ -81,17 +77,3 @@ export function composeMiddleware<T, U> (
8177
}
8278
}
8379
}
84-
85-
export function uuid () {
86-
const random = randomBytes(16)
87-
random[6] = (random[6] & 0x0f) | 0x40
88-
random[8] = (random[8] & 0x3f) | 0x80
89-
return random.toString('hex')
90-
.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5')
91-
}
92-
93-
export function hmac (secret: Buffer, message: string | Buffer) {
94-
const hmac = createHmac('sha256', secret)
95-
hmac.update(message, 'utf8')
96-
return hmac.digest()
97-
}

0 commit comments

Comments
 (0)