diff --git a/src/channelManager.ts b/src/channelManager.ts index 3ef9b32..b7dc29c 100644 --- a/src/channelManager.ts +++ b/src/channelManager.ts @@ -43,7 +43,7 @@ class ChannelManager { } addClientToChannel(client: PublicClient|PrivateClient|CustomClient, channel_id: string) { - let channel: PrivateChannel|PrivateChannel|CustomChannel|null = this.channelExists(channel_id); + const channel: PrivateChannel|PrivateChannel|CustomChannel|null = this.channelExists(channel_id); if (channel) { channel.addClient(client); @@ -75,7 +75,7 @@ class ChannelManager { } removeClientFromChannel(client_id: number, channel_id: string) { - for (let channel of this.channels) { + for (const channel of this.channels) { if (channel.id == channel_id) { if (channel.removeClient(client_id)) { logger.accessLog.info(`client removed from channel - channel: ${channel_id}, client: ${client_id}`); @@ -111,6 +111,6 @@ class ChannelManager { } } } -}; +} export default ChannelManager; diff --git a/src/channels/channelBase.ts b/src/channels/channelBase.ts index b73c227..2eec4e4 100644 --- a/src/channels/channelBase.ts +++ b/src/channels/channelBase.ts @@ -14,7 +14,7 @@ class ChannelBase { } broadcastMessage(from: PublicClient|PrivateClient|CustomClient, message: object) { - for (let to of this.clients) { + for (const to of this.clients) { if (this.messageTransactionPossible(from, to)) { to.ws.send(JSON.stringify(message)); logger.accessLog.info(`sent to ${to.id}`, { data: { message: message }}); @@ -23,7 +23,7 @@ class ChannelBase { } } - return {'status': 'success', 'message': `message broadcast complete`}; + return {status: 'success', message: `message broadcast complete`}; } messageTransactionPossible(_from: PublicClient|PrivateClient|CustomClient, _to: PublicClient|PrivateClient|CustomClient) { @@ -33,11 +33,11 @@ class ChannelBase { addClient(client: PublicClient|PrivateClient|CustomClient) { if (this.clientExists(client.id)) { logger.errorLog.info('Client already exits in channel', {channelId: this.id, clientId: client.id}); - return {'status': 'notice', 'message': 'client already exists in channel'}; + return {status: 'notice', message: 'client already exists in channel'}; } else { this.clients.push(client); logger.accessLog.info('Added client to channel', {channelId: this.id, clientId: client.id}); - return {'status': 'success', 'message': 'client added'}; + return {status: 'success', message: 'client added'}; } } diff --git a/src/channels/types/customChannel.ts b/src/channels/types/customChannel.ts index 9c768a1..82b6b47 100644 --- a/src/channels/types/customChannel.ts +++ b/src/channels/types/customChannel.ts @@ -7,12 +7,12 @@ class CustomChannel extends ChannelBase { constructor(id: string, custom: any) { super(id); - this.custom = custom + this.custom = custom; } messageTransactionPossible(from: CustomClient, to: CustomClient) { - return eval(this.custom.broadcastConditions) + return eval(this.custom.broadcastConditions); } -}; +} export default CustomChannel; diff --git a/src/clientManager.ts b/src/clientManager.ts index 592ec53..8d75775 100644 --- a/src/clientManager.ts +++ b/src/clientManager.ts @@ -4,7 +4,7 @@ import PrivateClient from './clients/types/privateClient'; import CustomClient from './clients/types/customClient'; import ChannelManager from './channelManager'; -var logger = require('./logger'); +const logger = require('./logger'); class ClientManager { clients: any[] = []; @@ -15,7 +15,7 @@ class ClientManager { addClient(data: any, channelManager: ChannelManager, ws: WebSocket) { if (data.client_type && !this.clientExists(data.user_id)) { - let client = this.getClientType(data, channelManager, ws); + const client = this.getClientType(data, channelManager, ws); this.clients.push(client); logger.accessLog.info(`client added to client manager: ${data.user_id}`); return client; @@ -30,7 +30,7 @@ class ClientManager { let result: PublicClient[]|PrivateClient[]|CustomClient[] = []; for (let client of this.clients) { - if (client.type() == client_type) { + if (client.type() === client_type) { result.push(client); } } @@ -40,7 +40,7 @@ class ClientManager { clientExists(id: number) { for (let client of this.clients) { - if (client.id == id) { + if (client.id === id) { return client; } } @@ -71,9 +71,9 @@ class ClientManager { try { logger.accessLog.info(`attempting to create client of type ${data.client_type}, client id: ${data.user_id}...`); - if (data.client_type == 'public') { + if (data.client_type === 'public') { return new PublicClient(data, ws, channelManager, this) - } else if (data.channel_type == 'private') { + } else if (data.channel_type === 'private') { return new PrivateClient(data, ws, channelManager, this) } else { return new CustomClient(data, ws, channelManager, this) diff --git a/src/clients/clientBase.ts b/src/clients/clientBase.ts index 2cbe5bd..78edf0e 100644 --- a/src/clients/clientBase.ts +++ b/src/clients/clientBase.ts @@ -28,7 +28,7 @@ class ClientBase { this.roles = ['receiver'] this.messageListener = (data: any) => { if (this.channel) { - logger.accessLog.info(`starting message transaction on channel ${this.channel.id}: `, {data: data}); + logger.accessLog.info(`starting message transaction on channel ${this.channel.id}: `, {data}); data = messageManager.prepareMessage(data, this.channel, this); this.messageTransaction(data); } diff --git a/src/clients/types/customClient.ts b/src/clients/types/customClient.ts index 2732b70..73d24d2 100644 --- a/src/clients/types/customClient.ts +++ b/src/clients/types/customClient.ts @@ -2,17 +2,15 @@ import ClientBase from '../clientBase'; import ClientManager from '../../clientManager'; import ChannelManager from '../../channelManager'; import * as WebSocket from 'ws'; -import * as Joi from 'joi'; -var logger = require('../../logger'); +const logger = require('../../logger'); class CustomClient extends ClientBase { constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) { super(data, ws, channelManager, clientManager); - this.roles = data.user_roles + this.roles = data.user_roles; logger.accessLog.info('Custom Client Created', {data: data}); } -}; +} export default CustomClient; - diff --git a/src/clients/types/privateClient.ts b/src/clients/types/privateClient.ts index 469fea4..59e79c3 100644 --- a/src/clients/types/privateClient.ts +++ b/src/clients/types/privateClient.ts @@ -3,7 +3,7 @@ import ClientBase from '../clientBase'; import ClientManager from '../../clientManager'; import ChannelManager from '../../channelManager'; -var logger = require('../../logger'); +const logger = require('../../logger'); class PrivateClient extends ClientBase { constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) { @@ -14,4 +14,3 @@ class PrivateClient extends ClientBase { }; export default PrivateClient; - diff --git a/src/clients/types/publicClient.ts b/src/clients/types/publicClient.ts index bc2c38e..620daf3 100644 --- a/src/clients/types/publicClient.ts +++ b/src/clients/types/publicClient.ts @@ -3,4 +3,3 @@ import ClientBase from '../clientBase'; class PublicClient extends ClientBase {}; export default PublicClient; - diff --git a/src/config/app.ts b/src/config/app.ts index 1fa186d..3f1b33e 100644 --- a/src/config/app.ts +++ b/src/config/app.ts @@ -13,7 +13,7 @@ module.exports = { issuer: 'Yardstick Software', subject: 'Braid JWT', audience: 'internal', - algorithm: ["HS256"] + algorithm: ['HS256'] }, messageTypes : ['broadcast', 'direct', 'changeChannel'] -} +}; diff --git a/src/helpers/clientHelpers.ts b/src/helpers/clientHelpers.ts index c86d8a2..ae3f48e 100644 --- a/src/helpers/clientHelpers.ts +++ b/src/helpers/clientHelpers.ts @@ -1,5 +1,5 @@ module.exports = { - clientExists: function (id: number) { + clientExists: function(id: number) { for (const client of this.clients) { if (client.id === id) { return client; @@ -8,4 +8,4 @@ module.exports = { return null; } -} +}; diff --git a/src/logger.ts b/src/logger.ts index ea88a0e..2387437 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,8 +1,8 @@ -var {winston, transports, createLogger, format} = require('winston'); -var path = require('path'); +const {winston, transports, createLogger, format} = require('winston'); +const path = require('path'); // Set this to whatever, by default the path of the script. -var logPath = './logs/'; +const logPath = './logs/'; const tsFormat = () => (new Date().toISOString()); const logFormat = format.combine(format.timestamp(), format.json()); @@ -15,7 +15,7 @@ function loggerTransports(logName: string, logLevel: string) { level: logLevel }) ] -}; +} const errorLog = createLogger({ format: logFormat, diff --git a/src/messageManager.ts b/src/messageManager.ts index 6566fa2..72ff678 100644 --- a/src/messageManager.ts +++ b/src/messageManager.ts @@ -4,13 +4,13 @@ import Validations from './services/validations'; module.exports = { prepareMessage: (message: string) => { let validations = new Validations(message) - let parsed = JSON.parse(message) + let parsed = JSON.parse(message); const result = Joi.validate(parsed, validations.MessageConditions); if (result.error) { - return result + return result; } else { - return parsed + return parsed; } } -} +}; diff --git a/src/routes.ts b/src/routes.ts index e7bfadf..c53c893 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -1,8 +1,8 @@ import * as cors from 'cors'; import * as express from 'express'; -var app = require('./config/app'); -var corsOptions = { +const app = require('./config/app'); +const corsOptions = { origin: (origin: string, callback: Function) => { if (app.whitelist.indexOf(origin) !== -1) { callback(null, true); @@ -12,14 +12,14 @@ var corsOptions = { } } -var router = express.Router(); +const router = express.Router(); //application -var appController = require('./controllers/appController'); +const appController = require('./controllers/appController'); router.route(['/', '/home']).get(appController.home) //auth -var authController = require('./controllers/authController'); +const authController = require('./controllers/authController'); router.route('/auth/user').post(cors(), authController.confirmToken); module.exports = router; diff --git a/src/server.ts b/src/server.ts index b68f66a..118c3b4 100755 --- a/src/server.ts +++ b/src/server.ts @@ -4,7 +4,7 @@ import * as jwt from 'jsonwebtoken'; import * as url from 'url'; // internal imports -var app = require('./config/app'); +const app = require('./config/app'); const logger = require('./logger'); import ClientManager from './clientManager'; @@ -13,14 +13,14 @@ import PublicClient from './clients/types/publicClient'; import PrivateClient from './clients/types/privateClient'; import CustomClient from './clients/types/customClient'; -var wss = new WebSocket.Server({ maxPayload:250000, port: app.port }); +const wss = new WebSocket.Server({ maxPayload:250000, port: app.port }); -let clientManager = new ClientManager(); -let channelManager = new ChannelManager(); +const clientManager = new ClientManager(); +const channelManager = new ChannelManager(); function connectionManager() { wss.on('connection', (ws: WebSocket, request: any, args: string) => { - let result = JSON.parse(validateJWT(request)); + const result = JSON.parse(validateJWT(request)); if (result.error) { ws.send(`Unable to validate JWT, please try again or contact support...`); @@ -59,7 +59,7 @@ function connectionManager() { function validateJWT(request: any) { try { let query = url.parse(request.url, true).query; - let token = query.token || (app.environment == 'development' ? app.devToken : ''); + let token = query.token || (app.environment === 'development' ? app.devToken : ''); return JSON.stringify(jwt.verify(token, app.secret, app.signOptions)); } catch (e) { console.log(e); diff --git a/src/services/validations.ts b/src/services/validations.ts index e709784..ea8253c 100644 --- a/src/services/validations.ts +++ b/src/services/validations.ts @@ -1,9 +1,6 @@ -import PublicChannel from '../channels/types/publicChannel'; -import PrivateChannel from '../channels/types/privateChannel'; -import CustomChannel from '../channels/types/customChannel'; import * as Joi from 'joi' -var app = require('../config/app'); +const app = require('../config/app'); class Validations { MessageConditions = { @@ -14,13 +11,13 @@ class Validations { user_id: Joi.number().integer(), message: Joi.alternatives().try(Joi.string(), Joi.object()), custom: Joi.object() - } + }; constructor(message: any) { if (message.channel_type === 'custom') { - let conditions = message.conditions; + const conditions = message.conditions; } } } -export default Validations; \ No newline at end of file +export default Validations;