Skip to content

Commit a27edba

Browse files
committed
Extracting channel module
1 parent 6c51f5d commit a27edba

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed

packages/testkit-backend/src/backend.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
1-
import SocketServer from './socket.server'
1+
import Channel from './channel'
22
import Controller from './controller'
33

4+
/**
5+
* Binds Channel and Controller
6+
*/
47
export default class Backend {
5-
constructor (port, newController = () => new Controller(), newSocketServer = port => new SocketServer(port)) {
6-
this._socketServer = newSocketServer(port)
8+
/**
9+
*
10+
* @param {function():Controller} newController The controller factory function
11+
* @param {function():Channel} newChannel The channel factory function
12+
*/
13+
constructor (newController, newChannel) {
14+
this._channel = newChannel()
715
this._controller = newController()
816

917
this._controller.on('response', ({ contextId, response }) => {
10-
this._socketServer.writeResponse(contextId, response)
18+
this._channel.writeResponse(contextId, response)
1119
})
1220

13-
this._socketServer.on('contextOpen', ({ contextId }) => this._controller.onContextOpen(contextId))
14-
this._socketServer.on('contextClose', ({ contextId }) => this._controller.onContextClose(contextId))
21+
this._channel.on('contextOpen', ({ contextId }) => this._controller.onContextOpen(contextId))
22+
this._channel.on('contextClose', ({ contextId }) => this._controller.onContextClose(contextId))
1523

16-
this._socketServer.on('request', ({ contextId, request }) => {
24+
this._channel.on('request', ({ contextId, request }) => {
1725
try {
1826
this._controller.handle(contextId, request)
1927
} catch (e) {
20-
this._socketServer.writeBackendError(contextId, e)
28+
this._channel.writeBackendError(contextId, e)
2129
}
2230
})
2331
}
2432

2533
start () {
2634
this._controller.start()
27-
this._socketServer.start()
35+
this._channel.start()
2836
}
2937

3038
stop () {
31-
this._socketServer.stop()
39+
this._channel.stop()
3240
this._controller.stop()
3341
}
3442

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { EventEmitter } from "events"
2+
3+
export default class Channel extends EventEmitter {
4+
5+
start () {
6+
throw Error('Not implemented')
7+
}
8+
9+
stop () {
10+
throw Error('Not implemented')
11+
}
12+
13+
writeResponse (contextId, response) {
14+
throw Error('Not implemented')
15+
}
16+
17+
writeBackendError (contextId, error) {
18+
this.writeResponse(contextId, { name: 'BackendError', data: { msg: error } })
19+
}
20+
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Channel from "./abstract"
2+
import SocketChannel from "./socket"
3+
4+
export default Channel
5+
export {
6+
SocketChannel
7+
}

packages/testkit-backend/src/socket.server.js renamed to packages/testkit-backend/src/channel/socket.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EventEmitter } from 'events'
22
import net from 'net'
33
import { randomBytes } from 'crypto'
4-
import Protocol from './protocol'
4+
import Protocol from './testkit-protocol'
55

66
function generateRandomId () {
77
return randomBytes(16).toString()
@@ -42,7 +42,7 @@ export default class SocketServer extends EventEmitter {
4242

4343
this.emit('contextOpen', { contextId })
4444
protocol.on('request', request => this.emit('request', { contextId, request }) )
45-
protocol.on('error', e => this.writeBackendError(contextId, e))
45+
protocol.on('error', e => this._writeBackendError(contextId, e))
4646

4747
connection.on('end', () => {
4848
if (this._clients.has(contextId)) {

packages/testkit-backend/src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import Backend from './backend'
2+
import { SocketChannel } from './channel'
23
import NodeController from './node.controller'
34

45
function main( ) {
5-
const backend = new Backend(process.env.BACKEND_PORT || 9876, () => new NodeController())
6+
const newChannel = () => new SocketChannel(process.env.BACKEND_PORT || 9876)
7+
const newController = () => new NodeController()
8+
const backend = new Backend(newController, newChannel)
69

710
backend.start()
811

@@ -16,4 +19,4 @@ function main( ) {
1619
process.on('uncaughtException', process.exit.bind(process));
1720
}
1821

19-
main()
22+
main()

0 commit comments

Comments
 (0)