()
| 7 | |
| 8 | class Worker extends SCWorker { |
| 9 | run() { |
| 10 | const httpServer = this.httpServer; |
| 11 | const scServer = this.scServer; |
| 12 | const options = this.options; |
| 13 | const store = createStore(options); |
| 14 | |
| 15 | httpServer.on('request', app); |
| 16 | |
| 17 | app.use(routes(options, store, scServer)); |
| 18 | |
| 19 | scServer.addMiddleware(scServer.MIDDLEWARE_EMIT, function (req, next) { |
| 20 | const channel = req.event; |
| 21 | const data = req.data; |
| 22 | if ( |
| 23 | channel.substr(0, 3) === 'sc-' || |
| 24 | channel === 'respond' || |
| 25 | channel === 'log' |
| 26 | ) { |
| 27 | scServer.exchange.publish(channel, data); |
| 28 | } else if (channel === 'log-noid') { |
| 29 | scServer.exchange.publish('log', { id: req.socket.id, data: data }); |
| 30 | } |
| 31 | next(); |
| 32 | }); |
| 33 | |
| 34 | scServer.addMiddleware(scServer.MIDDLEWARE_SUBSCRIBE, function (req, next) { |
| 35 | next(); |
| 36 | if (req.channel === 'report') { |
| 37 | store |
| 38 | .list() |
| 39 | .then(function (data) { |
| 40 | req.socket.emit(req.channel!, { type: 'list', data: data }); |
| 41 | }) |
| 42 | .catch(function (error) { |
| 43 | console.error(error); // eslint-disable-line no-console |
| 44 | }); |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | scServer.on('connection', function (socket) { |
| 49 | let channelToWatch: string, channelToEmit: string; |
| 50 | socket.on('login', function (this: Worker, credentials, respond) { |
| 51 | if (credentials === 'master') { |
| 52 | channelToWatch = 'respond'; |
| 53 | channelToEmit = 'log'; |
| 54 | } else { |
| 55 | channelToWatch = 'log'; |
| 56 | channelToEmit = 'respond'; |
| 57 | } |
| 58 | this.exchange.subscribe('sc-' + socket.id).watch(function (msg) { |
| 59 | socket.emit(channelToWatch, msg); |
| 60 | }); |
| 61 | respond(null, channelToWatch); |
| 62 | }); |
| 63 | socket.on('getReport', function (id: string, respond) { |
| 64 | store |
| 65 | .get(id) |
| 66 | .then(function (data) { |
no test coverage detected