lintageddon 4

This commit is contained in:
Josh Burman 2020-02-03 16:54:42 -05:00
parent 6ec98af769
commit e004a64e9b
11 changed files with 39 additions and 39 deletions

View File

@ -12,12 +12,12 @@ class ChannelManager {
constructor() { constructor() {
// create default channel... // create default channel...
let channel = new PublicChannel('default'); const channel = new PublicChannel('default');
this.channels.push(channel); this.channels.push(channel);
} }
createChannel(data: any) { createChannel(data: any) {
let channelExists: PublicChannel|PrivateChannel|CustomChannel|null = this.channelExists(data.channel); const channelExists: PublicChannel|PrivateChannel|CustomChannel|null = this.channelExists(data.channel);
let channel: PublicChannel|PrivateChannel|CustomChannel; let channel: PublicChannel|PrivateChannel|CustomChannel;
if (channelExists) { if (channelExists) {
@ -33,7 +33,7 @@ class ChannelManager {
} }
channelExists(channel_id: string) { channelExists(channel_id: string) {
for (let channel of this.channels) { for (const channel of this.channels) {
if (channel.id === channel_id) { if (channel.id === channel_id) {
return channel; return channel;
} }
@ -61,7 +61,7 @@ class ChannelManager {
if (data.channel_type === 'public') { if (data.channel_type === 'public') {
return new PublicChannel(data.channel); return new PublicChannel(data.channel);
} else if (data.channel_type == 'private') { } else if (data.channel_type === 'private') {
return new PrivateChannel(data.channel); return new PrivateChannel(data.channel);
} else { } else {
return new CustomChannel(data.channel, data.custom); return new CustomChannel(data.channel, data.custom);
@ -76,7 +76,7 @@ class ChannelManager {
removeClientFromChannel(client_id: number, channel_id: string) { removeClientFromChannel(client_id: number, channel_id: string) {
for (const channel of this.channels) { for (const channel of this.channels) {
if (channel.id == channel_id) { if (channel.id === channel_id) {
if (channel.removeClient(client_id)) { if (channel.removeClient(client_id)) {
logger.accessLog.info(`client removed from channel - channel: ${channel_id}, client: ${client_id}`); logger.accessLog.info(`client removed from channel - channel: ${channel_id}, client: ${client_id}`);
return true; return true;

View File

@ -17,7 +17,7 @@ class ChannelBase {
for (const to of this.clients) { for (const to of this.clients) {
if (this.messageTransactionPossible(from, to)) { if (this.messageTransactionPossible(from, to)) {
to.ws.send(JSON.stringify(message)); to.ws.send(JSON.stringify(message));
logger.accessLog.info(`sent to ${to.id}`, { data: { message: message }}); logger.accessLog.info(`sent to ${to.id}`, { data: {message}});
} else { } else {
logger.accessLog.info(`client is unable to send: ${to.id}`, { data: {message}}); logger.accessLog.info(`client is unable to send: ${to.id}`, { data: {message}});
} }
@ -52,7 +52,7 @@ class ChannelBase {
} }
removeClient(id: number) { removeClient(id: number) {
for (let client of this.clients) { for (const client of this.clients) {
if (client.id === id) { if (client.id === id) {
const index = this.clients.indexOf(client) const index = this.clients.indexOf(client)
this.clients.splice(index, 1); this.clients.splice(index, 1);

View File

@ -11,6 +11,6 @@ class PrivateChannel extends ChannelBase {
from.roles.includes('broadcaster') from.roles.includes('broadcaster')
) )
} }
}; }
export default PrivateChannel; export default PrivateChannel;

View File

@ -9,6 +9,6 @@ class PublicChannel extends ChannelBase {
to !== from to !== from
); );
} }
}; }
export default PublicChannel; export default PublicChannel;

View File

@ -29,7 +29,7 @@ class ClientManager {
clientsOfType(client_type: string) { clientsOfType(client_type: string) {
let result: PublicClient[]|PrivateClient[]|CustomClient[] = []; let result: PublicClient[]|PrivateClient[]|CustomClient[] = [];
for (let client of this.clients) { for (const client of this.clients) {
if (client.type() === client_type) { if (client.type() === client_type) {
result.push(client); result.push(client);
} }
@ -55,8 +55,8 @@ class ClientManager {
removeClient(id: number) { removeClient(id: number) {
let index: number = 0; let index: number = 0;
for (let client of this.clients) { for (const client of this.clients) {
if (client.id == id) { if (client.id === id) {
client.ws.close(); client.ws.close();
this.clients.splice(index, 1); this.clients.splice(index, 1);
logger.accessLog.info(`client disconnected: ${client.id}`); logger.accessLog.info(`client disconnected: ${client.id}`);
@ -72,11 +72,11 @@ class ClientManager {
logger.accessLog.info(`attempting to create client of type ${data.client_type}, client id: ${data.user_id}...`); 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) 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) return new PrivateClient(data, ws, channelManager, this);
} else { } else {
return new CustomClient(data, ws, channelManager, this) return new CustomClient(data, ws, channelManager, this);
} }
} catch (e) { } catch (e) {
logger.errorLog.info(e); logger.errorLog.info(e);

View File

@ -15,7 +15,7 @@ class ClientBase {
channel: PublicChannel|PrivateChannel|CustomChannel|null; channel: PublicChannel|PrivateChannel|CustomChannel|null;
clientManager: ClientManager; clientManager: ClientManager;
channelManager: ChannelManager; channelManager: ChannelManager;
roles: Array<string>; roles: String[];
messageListener: (data: any) => void; messageListener: (data: any) => void;
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) { constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
@ -25,12 +25,12 @@ class ClientBase {
this.channel = null; this.channel = null;
this.clientManager = clientManager; this.clientManager = clientManager;
this.channelManager = channelManager; this.channelManager = channelManager;
this.roles = ['receiver'] this.roles = ['receiver'];
this.messageListener = (data: any) => { this.messageListener = (message: any) => {
if (this.channel) { if (this.channel) {
logger.accessLog.info(`starting message transaction on channel ${this.channel.id}: `, {data}); logger.accessLog.info(`starting message transaction on channel ${this.channel.id}: `, {message});
data = messageManager.prepareMessage(data, this.channel, this); message = messageManager.prepareMessage(message, this.channel, this);
this.messageTransaction(data); this.messageTransaction(message);
} }
}; };
} }
@ -64,25 +64,25 @@ class ClientBase {
}); });
} }
messageTransaction(data: any) { messageTransaction(message: any) {
if (this.channel && !data.error) { if (this.channel && !message.error) {
switch (data.message_type) { switch (message.message_type) {
case 'broadcast': case 'broadcast':
this.channel.broadcastMessage(this, data); this.channel.broadcastMessage(this, message);
break; break;
case 'direct': case 'direct':
let to = this.clientManager.getClient(data.message.to) const to = this.clientManager.getClient(message.message.to);
to.directMessage(data); to.directMessage(message);
break; break;
case 'changeChannel': case 'changeChannel':
this.ws.removeListener('message', this.messageListener); this.ws.removeListener('message', this.messageListener);
this.channelManager.changeChannel(this, data); this.channelManager.changeChannel(this, message);
break; break;
} }
logger.accessLog.info(`message transaction complete on channel ${this.channel.id}: `, {message: data}); logger.accessLog.info(`message transaction complete on channel ${this.channel.id}: `, {message});
} else { } else {
logger.errorLog.info(`Validation failed or client is not part of a channel, please review schema`, {data: {message: data, error: data.error}}); logger.errorLog.info(`Validation failed or client is not part of a channel, please review schema`, {data: {message, error: message.error}});
} }
} }

View File

@ -3,8 +3,8 @@ import * as jwt from 'jsonwebtoken';
const app = require('../config/app'); const app = require('../config/app');
module.exports = { module.exports = {
confirmToken : (req: any, res: any) => { confirmToken: (req: any, res: any) => {
const token = req.body.token const token = req.body.token;
res.json({ res.json({
response: JSON.stringify(jwt.verify(token, app.secret, app.signOptions)) response: JSON.stringify(jwt.verify(token, app.secret, app.signOptions))
}); });

View File

@ -28,6 +28,6 @@ const accessLog = createLogger({
}); });
module.exports = { module.exports = {
errorLog: errorLog, errorLog,
accessLog: accessLog accessLog
}; };

View File

@ -3,8 +3,8 @@ import Validations from './services/validations';
module.exports = { module.exports = {
prepareMessage: (message: string) => { prepareMessage: (message: string) => {
let validations = new Validations(message) const validations = new Validations(message);
let parsed = JSON.parse(message); const parsed = JSON.parse(message);
const result = Joi.validate(parsed, validations.MessageConditions); const result = Joi.validate(parsed, validations.MessageConditions);
if (result.error) { if (result.error) {

View File

@ -10,7 +10,7 @@ const corsOptions = {
callback(new Error(`Not allowed by CORS. Origin: ${origin}`)); callback(new Error(`Not allowed by CORS. Origin: ${origin}`));
} }
} }
} };
const router = express.Router(); const router = express.Router();

View File

@ -58,8 +58,8 @@ function connectionManager() {
function validateJWT(request: any) { function validateJWT(request: any) {
try { try {
let query = url.parse(request.url, true).query; const query = url.parse(request.url, true).query;
let token = query.token || (app.environment === 'development' ? app.devToken : ''); const token = query.token || (app.environment === 'development' ? app.devToken : '');
return JSON.stringify(jwt.verify(token, app.secret, app.signOptions)); return JSON.stringify(jwt.verify(token, app.secret, app.signOptions));
} catch (e) { } catch (e) {
console.log(e); console.log(e);