diff --git a/.eslintignore b/.eslintignore index 26fb431..7773828 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1 @@ -src/ dist/ \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json index 0471021..d52bcf8 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -31,6 +31,7 @@ "requireLast": true } } - ] + ], + "@typescript-eslint/explicit-function-return-type": "off" } } \ No newline at end of file diff --git a/dist/src/index.js b/dist/src/index.js index a5a6f2c..64a4e12 100644 --- a/dist/src/index.js +++ b/dist/src/index.js @@ -1,4 +1,15 @@ "use strict"; +var __rest = (this && this.__rest) || function (s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; @@ -26,14 +37,14 @@ function ExpressPeerServer(server, options) { exports.ExpressPeerServer = ExpressPeerServer; function PeerServer(options = {}, callback) { const app = express_1.default(); - const newOptions = Object.assign(Object.assign({}, config_1.default), options); + let newOptions = Object.assign(Object.assign({}, config_1.default), options); const port = newOptions.port; const host = newOptions.host; let server; - if (newOptions.ssl && newOptions.ssl.key && newOptions.ssl.cert) { - server = https_1.default.createServer(options.ssl, app); - // @ts-ignore - delete newOptions.ssl; + const { ssl } = newOptions, restOptions = __rest(newOptions, ["ssl"]); + if (ssl && ssl.key && ssl.cert) { + server = https_1.default.createServer(ssl, app); + newOptions = restOptions; } else { server = http_1.default.createServer(app); diff --git a/dist/src/instance.js b/dist/src/instance.js index ca142e9..089f0fb 100644 --- a/dist/src/instance.js +++ b/dist/src/instance.js @@ -35,7 +35,7 @@ exports.createInstance = ({ app, server, options }) => { const messageQueue = realm.getMessageQueueById(client.getId()); if (messageQueue) { let message; - while (message = messageQueue.readMessage()) { + while ((message = messageQueue.readMessage())) { messageHandler.handle(client, message); } realm.clearMessageQueue(client.getId()); diff --git a/dist/src/models/realm.js b/dist/src/models/realm.js index 743b184..19d66fa 100644 --- a/dist/src/models/realm.js +++ b/dist/src/models/realm.js @@ -33,10 +33,11 @@ class Realm { return this.messageQueues.get(id); } addMessageToQueue(id, message) { + var _a; if (!this.getMessageQueueById(id)) { this.messageQueues.set(id, new messageQueue_1.MessageQueue()); } - this.getMessageQueueById(id).addMessage(message); + (_a = this.getMessageQueueById(id)) === null || _a === void 0 ? void 0 : _a.addMessage(message); } clearMessageQueue(id) { this.messageQueues.delete(id); diff --git a/dist/src/services/checkBrokenConnections/index.js b/dist/src/services/checkBrokenConnections/index.js index ac89a1f..f132361 100644 --- a/dist/src/services/checkBrokenConnections/index.js +++ b/dist/src/services/checkBrokenConnections/index.js @@ -32,6 +32,8 @@ class CheckBrokenConnections { const { alive_timeout: aliveTimeout } = this.config; for (const clientId of clientsIds) { const client = this.realm.getClientById(clientId); + if (!client) + continue; const timeSinceLastPing = now - client.getLastPing(); if (timeSinceLastPing < aliveTimeout) continue; diff --git a/dist/src/services/messagesExpire/index.js b/dist/src/services/messagesExpire/index.js index bc17dfb..d2b4245 100644 --- a/dist/src/services/messagesExpire/index.js +++ b/dist/src/services/messagesExpire/index.js @@ -32,6 +32,8 @@ class MessagesExpire { const seen = {}; for (const destinationClientId of destinationClientsIds) { const messageQueue = this.realm.getMessageQueueById(destinationClientId); + if (!messageQueue) + continue; const lastReadDiff = now - messageQueue.getLastReadAt(); if (lastReadDiff < maxDiff) continue; diff --git a/dist/src/services/webSocketServer/index.js b/dist/src/services/webSocketServer/index.js index fde0b03..632330a 100644 --- a/dist/src/services/webSocketServer/index.js +++ b/dist/src/services/webSocketServer/index.js @@ -22,7 +22,8 @@ class WebSocketServer extends events_1.default { this.socketServer.on("error", (error) => this._onSocketError(error)); } _onSocketConnection(socket, req) { - const { query = {} } = url_1.default.parse(req.url, true); + var _a; + const { query = {} } = url_1.default.parse((_a = req.url) !== null && _a !== void 0 ? _a : '', true); const { id, token, key } = query; if (!id || !token || !key) { return this._sendErrorAndClose(socket, enums_1.Errors.INVALID_WS_PARAMETERS); diff --git a/src/api/v1/calls/index.ts b/src/api/v1/calls/index.ts index ea42f8c..3535e99 100644 --- a/src/api/v1/calls/index.ts +++ b/src/api/v1/calls/index.ts @@ -3,7 +3,7 @@ import { IMessageHandler } from "../../../messageHandler"; import { IMessage } from "../../../models/message"; import { IRealm } from "../../../models/realm"; -export default ({ realm, messageHandler }: { realm: IRealm, messageHandler: IMessageHandler; }): express.Router => { +export default ({ realm, messageHandler }: { realm: IRealm; messageHandler: IMessageHandler; }): express.Router => { const app = express.Router(); const handle = (req: express.Request, res: express.Response, next: express.NextFunction): any => { diff --git a/src/api/v1/public/index.ts b/src/api/v1/public/index.ts index 728236d..ffd3a6f 100644 --- a/src/api/v1/public/index.ts +++ b/src/api/v1/public/index.ts @@ -3,7 +3,7 @@ import { IConfig } from "../../../config"; import { IRealm } from "../../../models/realm"; export default ({ config, realm }: { - config: IConfig, realm: IRealm + config: IConfig; realm: IRealm; }): express.Router => { const app = express.Router(); diff --git a/src/index.ts b/src/index.ts index 0739d88..a20c6a4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,7 +37,7 @@ function ExpressPeerServer(server: Server, options?: IConfig) { function PeerServer(options: Optional = {}, callback?: (server: Server) => void) { const app = express(); - const newOptions: IConfig = { + let newOptions: IConfig = { ...defaultConfig, ...options }; @@ -47,10 +47,12 @@ function PeerServer(options: Optional = {}, callback?: (server: Server) let server: Server; - if (newOptions.ssl && newOptions.ssl.key && newOptions.ssl.cert) { - server = https.createServer(options.ssl!, app); - // @ts-ignore - delete newOptions.ssl; + const { ssl, ...restOptions } = newOptions; + + if (ssl && ssl.key && ssl.cert) { + server = https.createServer(ssl, app); + + newOptions = restOptions; } else { server = http.createServer(app); } diff --git a/src/instance.ts b/src/instance.ts index 1cd032e..0b8f522 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -1,6 +1,6 @@ import express from "express"; import { Server } from "net"; -import path from 'path'; +import path from "path"; import { IClient } from "./models/client"; import { IMessage } from "./models/message"; import { Realm } from "./models/realm"; @@ -13,8 +13,8 @@ import { Api } from "./api"; import { IConfig } from "./config"; export const createInstance = ({ app, server, options }: { - app: express.Application, - server: Server, + app: express.Application; + server: Server; options: IConfig; }): void => { const config = options; @@ -48,7 +48,7 @@ export const createInstance = ({ app, server, options }: { if (messageQueue) { let message: IMessage | undefined; - while (message = messageQueue.readMessage()) { + while ((message = messageQueue.readMessage())) { messageHandler.handle(client, message); } realm.clearMessageQueue(client.getId()); diff --git a/src/models/client.ts b/src/models/client.ts index 4dee3ca..f4b12c6 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -13,7 +13,7 @@ export interface IClient { setLastPing(lastPing: number): void; - send(data: any): void; + send(data: T): void; } export class Client implements IClient { @@ -22,7 +22,7 @@ export class Client implements IClient { private socket: MyWebSocket | null = null; private lastPing: number = new Date().getTime(); - constructor({ id, token }: { id: string, token: string; }) { + constructor({ id, token }: { id: string; token: string; }) { this.id = id; this.token = token; } @@ -51,7 +51,7 @@ export class Client implements IClient { this.lastPing = lastPing; } - public send(data: any): void { + public send(data: T): void { this.socket?.send(JSON.stringify(data)); } } diff --git a/src/models/messageQueue.ts b/src/models/messageQueue.ts index da716bb..8653d3e 100644 --- a/src/models/messageQueue.ts +++ b/src/models/messageQueue.ts @@ -25,7 +25,7 @@ export class MessageQueue implements IMessageQueue { public readMessage(): IMessage | undefined { if (this.messages.length > 0) { this.lastReadAt = new Date().getTime(); - return this.messages.shift()!; + return this.messages.shift(); } return undefined; diff --git a/src/models/realm.ts b/src/models/realm.ts index 5b73fc2..b89688b 100644 --- a/src/models/realm.ts +++ b/src/models/realm.ts @@ -62,7 +62,7 @@ export class Realm implements IRealm { this.messageQueues.set(id, new MessageQueue()); } - this.getMessageQueueById(id)!.addMessage(message); + this.getMessageQueueById(id)?.addMessage(message); } public clearMessageQueue(id: string): void { @@ -70,7 +70,6 @@ export class Realm implements IRealm { } public generateClientId(generateClientId?: () => string): string { - const generateId = generateClientId ? generateClientId : uuidv4; let clientId = generateId(); diff --git a/src/services/checkBrokenConnections/index.ts b/src/services/checkBrokenConnections/index.ts index 9ef3c4e..f08d292 100644 --- a/src/services/checkBrokenConnections/index.ts +++ b/src/services/checkBrokenConnections/index.ts @@ -15,9 +15,9 @@ export class CheckBrokenConnections { private readonly onClose?: (client: IClient) => void; constructor({ realm, config, checkInterval = DEFAULT_CHECK_INTERVAL, onClose }: { - realm: IRealm, - config: CustomConfig, - checkInterval?: number, + realm: IRealm; + config: CustomConfig; + checkInterval?: number; onClose?: (client: IClient) => void; }) { this.realm = realm; @@ -54,7 +54,10 @@ export class CheckBrokenConnections { const { alive_timeout: aliveTimeout } = this.config; for (const clientId of clientsIds) { - const client = this.realm.getClientById(clientId)!; + const client = this.realm.getClientById(clientId); + + if (!client) continue; + const timeSinceLastPing = now - client.getLastPing(); if (timeSinceLastPing < aliveTimeout) continue; diff --git a/src/services/messagesExpire/index.ts b/src/services/messagesExpire/index.ts index 9ad36e4..6301269 100644 --- a/src/services/messagesExpire/index.ts +++ b/src/services/messagesExpire/index.ts @@ -58,7 +58,10 @@ export class MessagesExpire implements IMessagesExpire { const seen: Record = {}; for (const destinationClientId of destinationClientsIds) { - const messageQueue = this.realm.getMessageQueueById(destinationClientId)!; + const messageQueue = this.realm.getMessageQueueById(destinationClientId); + + if (!messageQueue) continue; + const lastReadDiff = now - messageQueue.getLastReadAt(); if (lastReadDiff < maxDiff) continue; diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index 3b237a1..d9f2c5e 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -29,7 +29,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { private readonly config: CustomConfig; public readonly socketServer: WebSocketLib.Server; - constructor({ server, realm, config }: { server: any, realm: IRealm, config: CustomConfig; }) { + constructor({ server, realm, config }: { server: any; realm: IRealm; config: CustomConfig; }) { super(); this.setMaxListeners(0); @@ -47,7 +47,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { } private _onSocketConnection(socket: MyWebSocket, req: IncomingMessage): void { - const { query = {} } = url.parse(req.url!, true); + const { query = {} } = url.parse(req.url ?? '', true); const { id, token, key }: IAuthParams = query;