Skip to content

Commit

Permalink
refactor: add eslint, do a first linting pass
Browse files Browse the repository at this point in the history
@pedrobmarin`s suggestion

The heavier, more error prone stuff (mainly the Promise usage shakedown) will be done in a subsequent commit
  • Loading branch information
prlanzarin committed Nov 9, 2021
1 parent 5fd8ef9 commit 1d476bd
Show file tree
Hide file tree
Showing 67 changed files with 1,004 additions and 663 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
env:
node: true
commonjs: true
es2021: true
extends: eslint:recommended
parserOptions:
ecmaVersion: 13
rules:
no-console: "error"
83 changes: 35 additions & 48 deletions lib/base/BaseManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ module.exports = class BaseManager {
_flushIceQueue (session, queue) {
if (queue) {
let candidate;
while(candidate = queue.pop()) {
while((candidate = queue.pop())) {
session.onIceCandidate(candidate);
}
}
Expand All @@ -149,63 +149,50 @@ module.exports = class BaseManager {
}

_stopSession (sessionId) {
return new Promise(async (resolve, reject) => {
try {
if (this._sessions == null || sessionId == null) {
return resolve();
}
try {
if (this._sessions == null || sessionId == null) {
return Promise.resolve();
}

let session = this._sessions[sessionId];
if (session) {
Logger.info(this._logPrefix, `Stopping session ${sessionId}`);
if (typeof session.stop === 'function') {
await session.stop();
}
delete this._sessions[sessionId];
this._logAvailableSessions();
return resolve();
const session = this._sessions[sessionId];
if (session) {
Logger.debug(this._logPrefix, `Stopping session ${sessionId}`);
delete this._sessions[sessionId];

if (typeof session.stop === 'function') {
return session.stop().catch((error) => {
Logger.error(this._logPrefix, 'CRITICAL: stop session failure', {
errorMessage: error.message, error,
});
});
}

return resolve();
} catch (error) {
return reject(error);
}
});

return Promise.resolve();
} catch (error) {
Logger.error(this._logPrefix, 'CRITICAL: stop session failure', {
errorMessage: error.message, error,
});
}
}

stopAll() {
return new Promise(async (resolve, reject) => {
try {
Logger.info(this._logPrefix, 'Stopping everything!');
if (this._sessions == null) {
return resolve;
}
try {
Logger.info(this._logPrefix, 'Stopping everything!');

let sessionIds = Object.keys(this._sessions);
let stopProcedures = [];
const sessionIds = Object.keys(this._sessions);
const stopProcedures = [];

for (let i = 0; i < sessionIds.length; i++) {
stopProcedures.push(this._stopSession(sessionIds[i]));
}
resolve(Promise.all(stopProcedures));
for (let i = 0; i < sessionIds.length; i++) {
stopProcedures.push(this._stopSession(sessionIds[i]));
}
catch (err) {
Logger.error(error);
resolve();
}
});
}

_logAvailableSessions () {
if(this._sessions) {
let sessionMainKeys = Object.keys(this._sessions);
let logInfo = this._logPrefix + 'There are ' + sessionMainKeys.length + ' sessions available =>\n';
for (var k in this._sessions) {
if(this._sessions[k]) {
logInfo += '(Session[' + k +']' + ' of type ' + this._sessions[k].constructor.name + ');\n';
}
}
Logger.debug(logInfo);
return Promise.all(stopProcedures);
} catch (error) {
Logger.error(this._logPrefix, 'CRITICAL: stop all sessions failure', {
errorMessage: error.message, error,
});
throw error;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/base/BaseProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = class BaseProcess {
}

_failOver () {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
setTimeout(resolve, 5000);
});
}
Expand Down
20 changes: 11 additions & 9 deletions lib/base/BaseProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const Messaging = require('../bbb/messages/Messaging');
const Logger = require('../utils/Logger');
const EventEmitter = require('events').EventEmitter;
const errors = require('../base/errors');
const config = require('config');

const LOG_PREFIX = '[base-provider]';

module.exports = class BaseProvider extends EventEmitter {
constructor (bbbGW) {
Expand Down Expand Up @@ -91,19 +92,19 @@ module.exports = class BaseProvider extends EventEmitter {
if (global.CM_ROUTER && typeof global.CM_ROUTER.emit === 'function') {
global.CM_ROUTER.emit(C.REDIS_MESSAGE, message);
} else {
Logger.error(this._logPrefix, "Can't send outbound request, router not found",
{ request: message, ipc: process.env.SFU_IPC_MODE, target });
Logger.error(LOG_PREFIX, "Can't send outbound request, router not found",
{ sfuApp: this.sfuApp, request: message, ipc: process.env.SFU_IPC_MODE, target });
}
break;
default:
Logger.error(this._logPrefix, "Can't send outbound request, invalid IPC mode",
{ request: message, ipc: process.env.SFU_IPC_MODE, target });
Logger.error(LOG_PREFIX, "Can't send outbound request, invalid IPC mode",
{ sfuApp: this.sfuApp, request: message, ipc: process.env.SFU_IPC_MODE, target });
return;
}
}

probeForRecordingStatus (meetingId, userId) {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
const onRecordingStatusReply = (payload) => {
if (payload.requestedBy === userId) {
Logger.info("RecordingStatusReply for userId", payload.requestedBy, "is", payload.recorded);
Expand All @@ -120,7 +121,6 @@ module.exports = class BaseProvider extends EventEmitter {

flushCandidatesQueue (broker, queue, mediaId = null) {
if (mediaId) {
Logger.trace("Flushing", queue.length, "candidates queue for", mediaId);
queue.forEach(async (candidate) => {
try {
await broker.addIceCandidate(mediaId, candidate);
Expand All @@ -140,8 +140,10 @@ module.exports = class BaseProvider extends EventEmitter {
return `${this.getRecordingBasePath(adapter)}/${subPath}/${room}/${recordingName}-${timestamp}.${format}`
}

handleMCSCoreDisconnection (error) {
Logger.error(`[${this.sfuApp}] Provider received a MCS core disconnection event, fire a MEDIA_SERVER_OFFLINE`);
handleMCSCoreDisconnection () {
Logger.error(LOG_PREFIX, 'Provider received a mcs-core disconnection event', {
sfuApp: this.sfuApp,
});
this.emit(C.MEDIA_SERVER_OFFLINE);
}
};
16 changes: 5 additions & 11 deletions lib/base/MCSAPIWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ module.exports = class MCSAPIWrapper extends EventEmitter {
return instance;
}

async start (address, port, secure) {
async start (address, port) {
this.addr = 'ws://' + address + ':' + port + '/mcs';
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
try {
Logger.info("[sfu-mcs-api] Connecting to MCS at", this.addr);
this._mcs = new MCS(this.addr);
Expand Down Expand Up @@ -186,7 +186,7 @@ module.exports = class MCSAPIWrapper extends EventEmitter {
return ;
}
catch (error) {
throw (this._handleError(error, 'connect', { source, sink, type }));
throw (this._handleError(error, 'connect', { source, sinks, type }));
}
}

Expand All @@ -212,10 +212,8 @@ module.exports = class MCSAPIWrapper extends EventEmitter {

async addIceCandidate (mediaId, candidate) {
try {
const ack = await this._mcs.addIceCandidate(mediaId, candidate);
return ;
}
catch (error) {
await this._mcs.addIceCandidate(mediaId, candidate);
} catch (error) {
throw (this._handleError(error, 'addIceCandidate', { mediaId, candidate }));
}
}
Expand Down Expand Up @@ -277,10 +275,6 @@ module.exports = class MCSAPIWrapper extends EventEmitter {
}
}

setStrategy (strategy) {
// TODO
}

waitForConnection () {
const onConnected = () => {
return new Promise((resolve) => {
Expand Down
2 changes: 0 additions & 2 deletions lib/bbb/messages/Constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

const config = require('config');
/**
* @classdesc
* Message constants for the communication with BigBlueButton
Expand Down Expand Up @@ -108,7 +107,6 @@ const config = require('config');
CONFERENCE_NAME: "voiceConf",
SCREENSHARE_CONF: "screenshareConf",
STREAM_URL: "stream",
TIMESTAMP: "timestamp",
VIDEO_WIDTH: "vidWidth",
VIDEO_HEIGHT: "vidHeight",
HAS_AUDIO: "hasAudio",
Expand Down
2 changes: 0 additions & 2 deletions lib/bbb/messages/Messaging.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const Constants = require('./Constants.js');

const ScreenshareRTMPBroadcastStartedEventMessage2x =
require('./screenshare/ScreenshareRTMPBroadcastStartedEventMessage2x.js');
const ScreenshareRTMPBroadcastStoppedEventMessage2x =
Expand Down
31 changes: 0 additions & 31 deletions lib/bbb/messages/OutMessage.js

This file was deleted.

4 changes: 1 addition & 3 deletions lib/bbb/messages/OutMessage2x.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @constructor
*/

const config = require('config');

module.exports = class OutMessage2x {
static assembleCoreHeader = (fields) => {
const headers = {};
Expand Down Expand Up @@ -44,7 +42,7 @@ module.exports = class OutMessage2x {
* @type {Object}
*/
this.core.body = null;
};
}

/**
* Generates the JSON representation of the message
Expand Down
2 changes: 1 addition & 1 deletion lib/bbb/messages/audio/GetGlobalAudioPermissionReqMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ module.exports = class GetGlobalAudioPermissionReqMsg extends OutMessage2x {
this.core.body[Constants.VOICE_CONF_2x] = voiceConf;
this.core.body[Constants.USER_ID_2x] = userId;
this.core.body[Constants.SFU_SESSION_ID] = sfuSessionId;
};
}
}
2 changes: 1 addition & 1 deletion lib/bbb/messages/audio/UserConnectedToGlobalAudio2x.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ module.exports = class UserConnectedToGlobalAudio2x extends OutMessage2x {
this.core.body = {};
this.core.body[Constants.USER_ID_2x] = userId;
this.core.body[Constants.NAME] = name;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ module.exports = class UserDisconnectedFromGlobalAudio2x extends OutMessage2x {
this.core.body = {};
this.core.body[Constants.USER_ID_2x] = userId;
this.core.body[Constants.NAME] = name;
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ module.exports = class RecordingStatusRequestMessage2x extends OutMessage2x {

this.core.body = {};
this.core.body[Constants.REQUESTED_BY] = userId;
};
}
}
3 changes: 1 addition & 2 deletions lib/bbb/messages/recording/WebRTCShareEvent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const OutMessage2x = require('../OutMessage2x');
const C = require('../Constants.js');

module.exports = class WebRTCShareEvent {
Expand All @@ -10,7 +9,7 @@ module.exports = class WebRTCShareEvent {
this.payload[C.TIMESTAMP] = timestampHR;
this.payload[C.TIMESTAMP_UTC] = timestampUTC;
this.payload[C.FILENAME] = filename;
};
}

/**
* Generates the JSON representation of the message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ module.exports = class GetScreenBroadcastPermissionReqMsg extends OutMessage2x {
this.core.body[C.VOICE_CONF_2x] = voiceConf;
this.core.body[C.USER_ID_2x] = userId;
this.core.body[C.SFU_SESSION_ID] = sfuSessionId;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ module.exports = class GetScreenSubscribePermissionReqMsg extends OutMessage2x {
this.core.body[C.USER_ID_2x] = userId;
this.core.body[C.STREAM_ID] = streamId;
this.core.body[C.SFU_SESSION_ID] = sfuSessionId;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ module.exports = class ScreenshareRTMPBroadcastStartedEventMessage2x extends Out
if (typeof hasAudio !== 'undefined') {
this.core.body[C.HAS_AUDIO] = hasAudio;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ module.exports = class ScreenshareRTMPBroadcastStoppedEventMessage2x extends Out
this.core.body[C.VIDEO_WIDTH] = vw;
this.core.body[C.VIDEO_HEIGHT] = vh;
this.core.body[C.TIMESTAMP] = timestamp;
};
}
}
2 changes: 1 addition & 1 deletion lib/bbb/messages/video/GetCamBroadcastPermissionReqMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ module.exports = class GetCamBroadcastPermissionReqMsg extends OutMessage2x {
this.core.body[C.MEETING_ID_2x] = meetingId;
this.core.body[C.USER_ID_2x] = userId;
this.core.body[C.SFU_SESSION_ID] = sfuSessionId;
};
}
}
2 changes: 1 addition & 1 deletion lib/bbb/messages/video/GetCamSubscribePermissionReqMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ module.exports = class GetCamSubscribePermissionReqMsg extends OutMessage2x {
this.core.body[C.USER_ID_2x] = userId;
this.core.body[C.STREAM_ID] = streamId;
this.core.body[C.SFU_SESSION_ID] = sfuSessionId;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ module.exports = class UserCamBroadcastStoppedEventMessage2x extends OutMessage2

this.core.body = {};
this.core.body[C.STREAM_URL] = stream;
};
}
}
2 changes: 1 addition & 1 deletion lib/bbb/pubsub/RedisWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = class RedisWrapper extends EventEmitter {
//TODO
});

this.redisCli.on("psubscribe", (channel, count) => {
this.redisCli.on("psubscribe", (channel) => {
Logger.debug(LOG_PREFIX, `Redis client subscribed to channel ${channel}`);
});

Expand Down
Loading

0 comments on commit 1d476bd

Please sign in to comment.