Skip to content

Commit 70eb49d

Browse files
committed
Standardize code and fix linter issues
1 parent 8449c23 commit 70eb49d

23 files changed

+209
-226
lines changed

packages/testkit-backend/src/backend.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Channel from './channel'
2-
import Controller from './controller'
1+
import Channel from './channel' // eslint-disable-line no-unused-vars
2+
import Controller from './controller' // eslint-disable-line no-unused-vars
33

44
/**
55
* Binds Channel and Controller
@@ -39,5 +39,4 @@ export default class Backend {
3939
this._channel.stop()
4040
this._controller.stop()
4141
}
42-
4342
}

packages/testkit-backend/src/channel/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Channel from "./interface"
2-
import SocketChannel from "./socket"
3-
import WebSocketChannel from "./websocket"
1+
import Channel from './interface'
2+
import SocketChannel from './socket'
3+
import WebSocketChannel from './websocket'
44
/**
55
* Channels are the pieces of code responsible for communicating with testkit.
66
*

packages/testkit-backend/src/channel/interface.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EventEmitter } from "events"
1+
import { EventEmitter } from 'events'
22

33
/**
44
* Defines the interface used for receiving commands form teskit.
@@ -10,7 +10,6 @@ import { EventEmitter } from "events"
1010
* @event request This event is triggered when the channel receives a request
1111
*/
1212
export default class Channel extends EventEmitter {
13-
1413
start () {
1514
throw Error('Not implemented')
1615
}
@@ -24,7 +23,6 @@ export default class Channel extends EventEmitter {
2423
}
2524

2625
writeBackendError (contextId, error) {
27-
this.writeResponse(contextId, { name: 'BackendError', data: { msg: error } })
26+
this.writeResponse(contextId, { name: 'BackendError', data: { msg: error } })
2827
}
29-
3028
}

packages/testkit-backend/src/channel/socket.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function generateRandomId () {
1313
* This implementation is meant to be run in NodeJS, it doesn't support Browser.
1414
*/
1515
export default class SocketChannel extends Channel {
16-
constructor(port, newProtocol = stream => new Protocol(stream), newId = generateRandomId ) {
16+
constructor (port, newProtocol = stream => new Protocol(stream), newId = generateRandomId) {
1717
super()
1818
this._newProtocol = newProtocol
1919
this._server = null
@@ -34,9 +34,9 @@ export default class SocketChannel extends Channel {
3434
}
3535
}
3636

37-
_handleConnection(connection) {
37+
_handleConnection (connection) {
3838
console.log('Backend connected')
39-
39+
4040
const contextId = this._newId()
4141
const protocol = this._newProtocol(connection)
4242

@@ -46,10 +46,10 @@ export default class SocketChannel extends Channel {
4646
})
4747

4848
this.emit('contextOpen', { contextId })
49-
protocol.on('request', request => this.emit('request', { contextId, request }) )
49+
protocol.on('request', request => this.emit('request', { contextId, request }))
5050
protocol.on('error', e => this._writeBackendError(contextId, e))
51-
52-
connection.on('end', () => {
51+
52+
connection.on('end', () => {
5353
if (this._clients.has(contextId)) {
5454
this._clients.get(contextId).protocol.stop()
5555
}
@@ -69,7 +69,7 @@ export default class SocketChannel extends Channel {
6969
}
7070

7171
writeBackendError (contextId, error) {
72-
this.writeResponse(contextId, { name: 'BackendError', data: { msg: error } })
72+
this.writeResponse(contextId, { name: 'BackendError', data: { msg: error } })
7373
}
7474

7575
stop () {

packages/testkit-backend/src/channel/testkit-protocol.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class Protocol extends EventEmitter {
1111
this._readlineInterface = null
1212
}
1313

14-
start() {
14+
start () {
1515
if (!this._readlineInterface) {
1616
this._readlineInterface = readline.createInterface(this._stream, null)
1717
this._readlineInterface.on('line', this._processLine.bind(this))
@@ -56,14 +56,14 @@ export default class Protocol extends EventEmitter {
5656
}
5757
}
5858

59-
_emitError(e) {
59+
_emitError (e) {
6060
this.emit('error', e)
6161
}
6262

6363
serializeResponse (response) {
6464
console.log('> writing response', response)
6565
const responseStr = stringify(response)
66-
return ['#response begin', responseStr, '#response end'].join('\n') + '\n'
66+
return ['#response begin', responseStr, '#response end'].join('\n') + '\n'
6767
}
6868

6969
_emitRequest () {
@@ -72,5 +72,4 @@ export default class Protocol extends EventEmitter {
7272
console.log('> Got request ' + name, data)
7373
this.emit('request', { name, data })
7474
}
75-
7675
}

packages/testkit-backend/src/channel/websocket.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import Channel from "./interface"
1+
/* eslint-env browser */
2+
import Channel from './interface'
23

34
/**
45
* This communication channel is meant to connect to other instances of the `testkit-backend` for receiving its events.
56
*
67
* This channel is only supported in browsers since it depends on WebSocket client to be available globally.
78
*/
89
export default class WebSocketChannel extends Channel {
9-
10-
constructor(address) {
10+
constructor (address) {
1111
super()
1212
this._adddress = address
1313
this._ws = null
1414
}
1515

1616
start () {
17-
if(!this._ws) {
17+
if (!this._ws) {
1818
this._ws = new WebSocket(this._adddress)
1919
this._ws.onmessage = ({ data: message }) => {
2020
console.log(message)
2121
console.debug('[WebSocketChannel] Received messsage', message)
22-
const { messageType, contextId, data } = JSON.parse(message)
23-
22+
const { messageType, contextId, data } = JSON.parse(message)
23+
2424
switch (messageType) {
2525
case 'contextOpen':
2626
case 'contextClose':
@@ -39,7 +39,7 @@ export default class WebSocketChannel extends Channel {
3939
}
4040

4141
stop () {
42-
if(this._ws) {
42+
if (this._ws) {
4343
this._ws.close()
4444
this._ws = null
4545
}
@@ -58,5 +58,4 @@ export default class WebSocketChannel extends Channel {
5858
typeof value === 'bigint' ? `${value}n` : value
5959
)
6060
}
61-
6261
}

packages/testkit-backend/src/context.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ export default class Context {
9898
return Object.values(this._txs).filter(tx => tx.sessionId === sessionId)
9999
}
100100

101-
getShouldRunTestFunction() {
101+
getShouldRunTestFunction () {
102102
return this._shouldRunTest
103103
}
104104

105-
getFeatures() {
105+
getFeatures () {
106106
return this._getFeatures()
107107
}
108108

packages/testkit-backend/src/controller/interface.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { EventEmitter } from 'events'
77
* @event response Event triggered whith response to the request handled.
88
*/
99
export default class Controller extends EventEmitter {
10-
1110
start () {
1211

1312
}
@@ -24,7 +23,7 @@ export default class Controller extends EventEmitter {
2423
throw new Error('not implemented')
2524
}
2625

27-
async handle(contextId, request) {
26+
async handle (contextId, request) {
2827
throw new Error('not implemented')
2928
}
3029
}

packages/testkit-backend/src/controller/local.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import Controller from './interface'
33
import stringify from '../stringify'
44
import { isFrontendError } from '../request-handlers'
55

6-
76
/**
87
* Local controller handles the requests locally by redirecting them to the correct request handler/service.
98
*
109
* This controller is used when testing browser and locally.
1110
*/
1211
export default class LocalController extends Controller {
13-
14-
constructor(requestHandlers = {}, shouldRunTest = () => {}, getFeatures = () => []) {
12+
constructor (requestHandlers = {}, shouldRunTest = () => {}, getFeatures = () => []) {
1513
super()
1614
this._requestHandlers = requestHandlers
1715
this._shouldRunTest = shouldRunTest
@@ -41,7 +39,6 @@ export default class LocalController extends Controller {
4139
writeError: (e) => this._writeError(contextId, e),
4240
writeBackendError: (msg) => this._writeBackendError(contextId, msg)
4341
})
44-
4542
}
4643

4744
_writeResponse (contextId, response) {
@@ -57,7 +54,7 @@ export default class LocalController extends Controller {
5754
if (e.name) {
5855
if (isFrontendError(e)) {
5956
this._writeResponse(contextId, newResponse('FrontendError', {
60-
msg: 'Simulating the client code throwing some error.',
57+
msg: 'Simulating the client code throwing some error.'
6158
}))
6259
} else {
6360
const id = this._contexts.get(contextId).addError(e)
@@ -71,7 +68,6 @@ export default class LocalController extends Controller {
7168
}
7269
this._writeBackendError(contextId, e)
7370
}
74-
7571
}
7672

7773
function newResponse (name, data) {

packages/testkit-backend/src/controller/remote.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import Controller from "./interface"
2-
import { WebSocketServer } from "ws"
3-
import { createServer } from "http"
4-
import { Server } from "node-static"
1+
import Controller from './interface'
2+
import { WebSocketServer } from 'ws'
3+
import { createServer } from 'http'
4+
import { Server } from 'node-static'
55

66
/**
77
* RemoteController handles the requests by sending them a remote client.
@@ -12,7 +12,7 @@ import { Server } from "node-static"
1212
* This controller can only be used in Node since it depends on {@link createServer}, {@link WebSocketServer} and {@link Server}
1313
*/
1414
export default class RemoteController extends Controller {
15-
constructor(port) {
15+
constructor (port) {
1616
super()
1717
this._staticServer = new Server('./public')
1818
this._port = port
@@ -33,12 +33,11 @@ export default class RemoteController extends Controller {
3333
}
3434
if (!this._wss) {
3535
this._wss = new WebSocketServer({ server: this._http })
36-
this._wss.on('connection', safeRun( ws => this._handleClientConnection(ws)))
36+
this._wss.on('connection', safeRun(ws => this._handleClientConnection(ws)))
3737
this._wss.on('error', safeRun(error => {
3838
console.error('[RemoteController] Server error', error)
3939
}))
4040
}
41-
4241
}
4342

4443
stop () {
@@ -47,7 +46,7 @@ export default class RemoteController extends Controller {
4746
this._ws = null
4847
}
4948

50-
if(this._wss) {
49+
if (this._wss) {
5150
this._wss.close()
5251
this._wss = null
5352
}
@@ -78,7 +77,7 @@ export default class RemoteController extends Controller {
7877
console.log('[RemoteController] Registering client')
7978

8079
this._ws = ws
81-
this._ws.on('message', safeRun(buffer => {
80+
this._ws.on('message', safeRun(buffer => {
8281
const message = JSON.parse(buffer.toString())
8382
console.debug('[RemoteController] Received messsage', message)
8483
const { contextId, response } = message
@@ -105,7 +104,7 @@ export default class RemoteController extends Controller {
105104
data
106105
}
107106

108-
console.info(`[RemoteController] Sending message`, message)
107+
console.info('[RemoteController] Sending message', message)
109108
return this._ws.send(JSON.stringify(message))
110109
}
111110
console.error('[RemoteController] There is no client connected')
@@ -119,12 +118,10 @@ export default class RemoteController extends Controller {
119118
}
120119

121120
_writeBackendError (contextId, msg) {
122-
this._writeResponse(contextId, { name: 'BackendError', data: { msg: msg } })
121+
this._writeResponse(contextId, { name: 'BackendError', data: { msg: msg } })
123122
}
124-
125123
}
126124

127-
128125
function safeRun (func) {
129126
return function () {
130127
const args = [...arguments]

0 commit comments

Comments
 (0)