fix tests
This commit is contained in:
parent
dd0b60416e
commit
1daa092eea
18
dist/src/config/index.js
vendored
Normal file
18
dist/src/config/index.js
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
const defaultConfig = {
|
||||||
|
port: 9000,
|
||||||
|
expire_timeout: 5000,
|
||||||
|
alive_timeout: 60000,
|
||||||
|
key: "peerjs",
|
||||||
|
path: "/myapp",
|
||||||
|
concurrent_limit: 5000,
|
||||||
|
allow_discovery: false,
|
||||||
|
proxied: false,
|
||||||
|
cleanup_out_msgs: 1000,
|
||||||
|
ssl: {
|
||||||
|
key: "",
|
||||||
|
cert: ""
|
||||||
|
}
|
||||||
|
};
|
||||||
|
exports.default = defaultConfig;
|
4
dist/src/index.js
vendored
4
dist/src/index.js
vendored
@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||||||
const express_1 = __importDefault(require("express"));
|
const express_1 = __importDefault(require("express"));
|
||||||
const http_1 = __importDefault(require("http"));
|
const http_1 = __importDefault(require("http"));
|
||||||
const https_1 = __importDefault(require("https"));
|
const https_1 = __importDefault(require("https"));
|
||||||
const config_1 = __importDefault(require("../config"));
|
|
||||||
const api_1 = require("./api");
|
const api_1 = require("./api");
|
||||||
|
const config_1 = __importDefault(require("./config"));
|
||||||
const messageHandler_1 = require("./messageHandler");
|
const messageHandler_1 = require("./messageHandler");
|
||||||
const realm_1 = require("./models/realm");
|
const realm_1 = require("./models/realm");
|
||||||
const checkBrokenConnections_1 = require("./services/checkBrokenConnections");
|
const checkBrokenConnections_1 = require("./services/checkBrokenConnections");
|
||||||
@ -30,7 +30,7 @@ const init = ({ app, server, options }) => {
|
|||||||
const wss = new webSocketServer_1.WebSocketServer({
|
const wss = new webSocketServer_1.WebSocketServer({
|
||||||
server,
|
server,
|
||||||
realm,
|
realm,
|
||||||
config: Object.assign({}, config)
|
config
|
||||||
});
|
});
|
||||||
wss.on("connection", (client) => {
|
wss.on("connection", (client) => {
|
||||||
const messageQueue = realm.getMessageQueueById(client.getId());
|
const messageQueue = realm.getMessageQueueById(client.getId());
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
function default_1(client) {
|
exports.HeartbeatHandler = (client) => {
|
||||||
const nowTime = new Date().getTime();
|
const nowTime = new Date().getTime();
|
||||||
client.setLastPing(nowTime);
|
client.setLastPing(nowTime);
|
||||||
return true;
|
return true;
|
||||||
}
|
};
|
||||||
exports.default = default_1;
|
|
||||||
|
6
dist/src/messageHandler/handlers/index.js
vendored
Normal file
6
dist/src/messageHandler/handlers/index.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
var heartbeat_1 = require("./heartbeat");
|
||||||
|
exports.HeartbeatHandler = heartbeat_1.HeartbeatHandler;
|
||||||
|
var transmission_1 = require("./transmission");
|
||||||
|
exports.TransmissionHandler = transmission_1.TransmissionHandler;
|
@ -1,7 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const enums_1 = require("../../../enums");
|
const enums_1 = require("../../../enums");
|
||||||
function default_1({ realm }) {
|
exports.TransmissionHandler = ({ realm }) => {
|
||||||
const handle = (client, message) => {
|
const handle = (client, message) => {
|
||||||
const type = message.type;
|
const type = message.type;
|
||||||
const srcId = message.src;
|
const srcId = message.src;
|
||||||
@ -53,5 +53,4 @@ function default_1({ realm }) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
return handle;
|
return handle;
|
||||||
}
|
};
|
||||||
exports.default = default_1;
|
|
||||||
|
10
dist/src/messageHandler/index.js
vendored
10
dist/src/messageHandler/index.js
vendored
@ -1,17 +1,13 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
||||||
};
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const enums_1 = require("../enums");
|
const enums_1 = require("../enums");
|
||||||
const heartbeat_1 = __importDefault(require("./handlers/heartbeat"));
|
const handlers_1 = require("./handlers");
|
||||||
const transmission_1 = __importDefault(require("./handlers/transmission"));
|
|
||||||
const messageHandlers_1 = require("./messageHandlers");
|
const messageHandlers_1 = require("./messageHandlers");
|
||||||
class MessageHandler {
|
class MessageHandler {
|
||||||
constructor(realm) {
|
constructor(realm) {
|
||||||
this.messageHandlers = new messageHandlers_1.MessageHandlers();
|
this.messageHandlers = new messageHandlers_1.MessageHandlers();
|
||||||
const transmissionHandler = transmission_1.default({ realm });
|
const transmissionHandler = handlers_1.TransmissionHandler({ realm });
|
||||||
const heartbeatHandler = heartbeat_1.default;
|
const heartbeatHandler = handlers_1.HeartbeatHandler;
|
||||||
const handleTransmission = (client, message) => {
|
const handleTransmission = (client, message) => {
|
||||||
return transmissionHandler(client, {
|
return transmissionHandler(client, {
|
||||||
type: message.type,
|
type: message.type,
|
||||||
|
@ -27,7 +27,7 @@ class CheckBrokenConnections {
|
|||||||
checkConnections() {
|
checkConnections() {
|
||||||
const clientsIds = this.realm.getClientsIds();
|
const clientsIds = this.realm.getClientsIds();
|
||||||
const now = new Date().getTime();
|
const now = new Date().getTime();
|
||||||
const aliveTimeout = this.config.alive_timeout;
|
const { alive_timeout: aliveTimeout } = this.config;
|
||||||
for (const clientId of clientsIds) {
|
for (const clientId of clientsIds) {
|
||||||
const client = this.realm.getClientById(clientId);
|
const client = this.realm.getClientById(clientId);
|
||||||
const timeSinceLastPing = now - client.getLastPing();
|
const timeSinceLastPing = now - client.getLastPing();
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
"lint": "tslint -c tslint.json -p tsconfig.json --fix",
|
"lint": "tslint -c tslint.json -p tsconfig.json --fix",
|
||||||
"tsc": "tsc",
|
"tsc": "tsc",
|
||||||
"prebuild": "npm run lint",
|
"prebuild": "npm run lint",
|
||||||
"test": "npm run lint && mocha \"test/**/*.js\"",
|
"test": "npm run lint && npm run build && mocha \"test/**/*.js\"",
|
||||||
"start": "bin/peerjs --port ${PORT:=9000}",
|
"start": "bin/peerjs --port ${PORT:=9000}",
|
||||||
"dev:start": "npm-run-all build start",
|
"dev:start": "npm-run-all build start",
|
||||||
"dev": "nodemon --watch src -e ts --exec npm run dev:start"
|
"dev": "nodemon --watch src -e ts --exec npm run dev:start"
|
||||||
|
@ -2,7 +2,7 @@ import bodyParser from "body-parser";
|
|||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import publicContent from "../../app.json";
|
import publicContent from "../../app.json";
|
||||||
import { IConfig } from "../../config/";
|
import { IConfig } from "../config";
|
||||||
import { IMessageHandler } from "../messageHandler";
|
import { IMessageHandler } from "../messageHandler";
|
||||||
import { IRealm } from "../models/realm";
|
import { IRealm } from "../models/realm";
|
||||||
import { AuthMiddleware } from "./middleware/auth";
|
import { AuthMiddleware } from "./middleware/auth";
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { IConfig } from "../../../../config";
|
import { IConfig } from "../../../config";
|
||||||
import { Errors } from "../../../enums";
|
import { Errors } from "../../../enums";
|
||||||
import { IRealm } from "../../../models/realm";
|
import { IRealm } from "../../../models/realm";
|
||||||
import { IMiddleware } from "../middleware";
|
import { IMiddleware } from "../middleware";
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { IConfig } from "../../../../config";
|
import { IConfig } from "../../../config";
|
||||||
import { IRealm } from "../../../models/realm";
|
import { IRealm } from "../../../models/realm";
|
||||||
|
|
||||||
export default ({ config, realm }: {
|
export default ({ config, realm }: {
|
||||||
|
@ -8,7 +8,7 @@ export interface IConfig {
|
|||||||
readonly allow_discovery: boolean;
|
readonly allow_discovery: boolean;
|
||||||
readonly proxied: boolean | string;
|
readonly proxied: boolean | string;
|
||||||
readonly cleanup_out_msgs: number;
|
readonly cleanup_out_msgs: number;
|
||||||
readonly ssl: {
|
readonly ssl?: {
|
||||||
key: string;
|
key: string;
|
||||||
cert: string;
|
cert: string;
|
||||||
};
|
};
|
@ -5,8 +5,8 @@ import http from "http";
|
|||||||
import https from "https";
|
import https from "https";
|
||||||
|
|
||||||
import { Server } from "net";
|
import { Server } from "net";
|
||||||
import defaultConfig, { IConfig } from "../config";
|
|
||||||
import { Api } from "./api";
|
import { Api } from "./api";
|
||||||
|
import defaultConfig, { IConfig } from "./config";
|
||||||
import { MessageHandler } from "./messageHandler";
|
import { MessageHandler } from "./messageHandler";
|
||||||
import { IClient } from "./models/client";
|
import { IClient } from "./models/client";
|
||||||
import { IMessage } from "./models/message";
|
import { IMessage } from "./models/message";
|
||||||
@ -39,9 +39,7 @@ const init = ({ app, server, options }: {
|
|||||||
const wss: IWebSocketServer = new WebSocketServer({
|
const wss: IWebSocketServer = new WebSocketServer({
|
||||||
server,
|
server,
|
||||||
realm,
|
realm,
|
||||||
config: {
|
config
|
||||||
...config,
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
wss.on("connection", (client: IClient) => {
|
wss.on("connection", (client: IClient) => {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { IClient } from "../../../models/client";
|
import { IClient } from "../../../models/client";
|
||||||
|
|
||||||
export default function(client: IClient): boolean {
|
export const HeartbeatHandler = (client: IClient): boolean => {
|
||||||
const nowTime = new Date().getTime();
|
const nowTime = new Date().getTime();
|
||||||
client.setLastPing(nowTime);
|
client.setLastPing(nowTime);
|
||||||
return true;
|
return true;
|
||||||
}
|
};
|
||||||
|
2
src/messageHandler/handlers/index.ts
Normal file
2
src/messageHandler/handlers/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export { HeartbeatHandler } from "./heartbeat";
|
||||||
|
export { TransmissionHandler } from "./transmission";
|
@ -3,7 +3,7 @@ import { IClient } from "../../../models/client";
|
|||||||
import { IMessage } from "../../../models/message";
|
import { IMessage } from "../../../models/message";
|
||||||
import { IRealm } from "../../../models/realm";
|
import { IRealm } from "../../../models/realm";
|
||||||
|
|
||||||
export default function({ realm }: { realm: IRealm }): (client: IClient, message: IMessage) => boolean {
|
export const TransmissionHandler = ({ realm }: { realm: IRealm }): (client: IClient, message: IMessage) => boolean => {
|
||||||
const handle = (client: IClient, message: IMessage) => {
|
const handle = (client: IClient, message: IMessage) => {
|
||||||
const type = message.type;
|
const type = message.type;
|
||||||
const srcId = message.src;
|
const srcId = message.src;
|
||||||
@ -55,4 +55,4 @@ export default function({ realm }: { realm: IRealm }): (client: IClient, message
|
|||||||
};
|
};
|
||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
}
|
};
|
||||||
|
@ -3,8 +3,7 @@ import { IClient } from "../models/client";
|
|||||||
import { IMessage } from "../models/message";
|
import { IMessage } from "../models/message";
|
||||||
import { IRealm } from "../models/realm";
|
import { IRealm } from "../models/realm";
|
||||||
import { Handler } from "./handler";
|
import { Handler } from "./handler";
|
||||||
import HeartbeatHandler from "./handlers/heartbeat";
|
import { HeartbeatHandler, TransmissionHandler } from "./handlers";
|
||||||
import TransmissionHandler from "./handlers/transmission";
|
|
||||||
import { IMessageHandlers, MessageHandlers } from "./messageHandlers";
|
import { IMessageHandlers, MessageHandlers } from "./messageHandlers";
|
||||||
|
|
||||||
export interface IMessageHandler {
|
export interface IMessageHandler {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { IConfig } from "../../../config";
|
import { IConfig } from "../../config";
|
||||||
import { IClient } from "../../models/client";
|
import { IClient } from "../../models/client";
|
||||||
import { IRealm } from "../../models/realm";
|
import { IRealm } from "../../models/realm";
|
||||||
|
|
||||||
@ -37,17 +37,19 @@ export class CheckBrokenConnections {
|
|||||||
this.start();
|
this.start();
|
||||||
}, this.checkInterval);
|
}, this.checkInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
public stop(): void {
|
public stop(): void {
|
||||||
if (this.timeoutId) {
|
if (this.timeoutId) {
|
||||||
clearTimeout(this.timeoutId);
|
clearTimeout(this.timeoutId);
|
||||||
this.timeoutId = null;
|
this.timeoutId = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private checkConnections(): void {
|
private checkConnections(): void {
|
||||||
const clientsIds = this.realm.getClientsIds();
|
const clientsIds = this.realm.getClientsIds();
|
||||||
|
|
||||||
const now = new Date().getTime();
|
const now = new Date().getTime();
|
||||||
const aliveTimeout = this.config.alive_timeout;
|
const { alive_timeout: aliveTimeout } = this.config;
|
||||||
|
|
||||||
for (const clientId of clientsIds) {
|
for (const clientId of clientsIds) {
|
||||||
const client = this.realm.getClientById(clientId);
|
const client = this.realm.getClientById(clientId);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { IConfig } from "../../../config";
|
import { IConfig } from "../../config";
|
||||||
import { MessageType } from "../../enums";
|
import { MessageType } from "../../enums";
|
||||||
import { IMessageHandler } from "../../messageHandler";
|
import { IMessageHandler } from "../../messageHandler";
|
||||||
import { IRealm } from "../../models/realm";
|
import { IRealm } from "../../models/realm";
|
||||||
|
@ -2,7 +2,7 @@ import EventEmitter from "events";
|
|||||||
import { IncomingMessage } from "http";
|
import { IncomingMessage } from "http";
|
||||||
import url from "url";
|
import url from "url";
|
||||||
import WebSocketLib from "ws";
|
import WebSocketLib from "ws";
|
||||||
import { IConfig } from "../../../config";
|
import { IConfig } from "../../config";
|
||||||
import { Errors, MessageType } from "../../enums";
|
import { Errors, MessageType } from "../../enums";
|
||||||
import { Client, IClient } from "../../models/client";
|
import { Client, IClient } from "../../models/client";
|
||||||
import { IRealm } from "../../models/realm";
|
import { IRealm } from "../../models/realm";
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { expect } from 'chai';
|
const { expect } = require('chai');
|
||||||
const Client = require('../../../../src/models/client');
|
const { Client } = require('../../../../dist/src/models/client');
|
||||||
const heartbeatHandler = require('../../../../src/messageHandler/handlers/heartbeat');
|
const { HeartbeatHandler } = require('../../../../dist/src/messageHandler/handlers');
|
||||||
|
|
||||||
describe('Heartbeat handler', () => {
|
describe('Heartbeat handler', () => {
|
||||||
it('should update last ping time', () => {
|
it('should update last ping time', () => {
|
||||||
@ -9,7 +9,7 @@ describe('Heartbeat handler', () => {
|
|||||||
|
|
||||||
const nowTime = new Date().getTime();
|
const nowTime = new Date().getTime();
|
||||||
|
|
||||||
heartbeatHandler(client);
|
HeartbeatHandler(client);
|
||||||
|
|
||||||
expect(client.getLastPing()).to.be.closeTo(nowTime, 2);
|
expect(client.getLastPing()).to.be.closeTo(nowTime, 2);
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
const { expect } = require('chai');
|
const { expect } = require('chai');
|
||||||
const Realm = require('../../src/models/realm');
|
const { Realm } = require('../../dist/src/models/realm');
|
||||||
const Client = require('../../src/models/client');
|
const { Client } = require('../../dist/src/models/client');
|
||||||
|
|
||||||
describe('Realm', () => {
|
describe('Realm', () => {
|
||||||
describe('#generateClientId', () => {
|
describe('#generateClientId', () => {
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
const { expect } = require('chai');
|
const { expect } = require('chai');
|
||||||
const Client = require('../../../src/models/client');
|
const { Client } = require('../../../dist/src/models/client');
|
||||||
const Realm = require('../../../src/models/realm');
|
const { Realm } = require('../../../dist/src/models/realm');
|
||||||
const checkBrokenConnectionsBuilder = require('../../../src/services/checkBrokenConnections');
|
const { CheckBrokenConnections } = require('../../../dist/src/services/checkBrokenConnections');
|
||||||
|
|
||||||
describe('checkBrokenConnections service', () => {
|
describe('checkBrokenConnections service', () => {
|
||||||
it('should remove client after 2 checks', (done) => {
|
it('should remove client after 2 checks', (done) => {
|
||||||
const realm = new Realm();
|
const realm = new Realm();
|
||||||
const doubleCheckTime = 55;//~ equals to checkBrokenConnections.CHECK_INTERVAL * 2
|
const doubleCheckTime = 55;//~ equals to checkBrokenConnections.checkInterval * 2
|
||||||
const checkBrokenConnections = checkBrokenConnectionsBuilder({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
|
const checkBrokenConnections = new CheckBrokenConnections({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
|
||||||
const client = new Client({ id: 'id', token: '' });
|
const client = new Client({ id: 'id', token: '' });
|
||||||
realm.setClient(client, 'id');
|
realm.setClient(client, 'id');
|
||||||
|
|
||||||
@ -17,13 +17,13 @@ describe('checkBrokenConnections service', () => {
|
|||||||
expect(realm.getClientById('id')).to.be.undefined;
|
expect(realm.getClientById('id')).to.be.undefined;
|
||||||
checkBrokenConnections.stop();
|
checkBrokenConnections.stop();
|
||||||
done();
|
done();
|
||||||
}, checkBrokenConnections.CHECK_INTERVAL * 2 + 3);
|
}, checkBrokenConnections.checkInterval * 2 + 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should remove client after 1 ping', (done) => {
|
it('should remove client after 1 ping', (done) => {
|
||||||
const realm = new Realm();
|
const realm = new Realm();
|
||||||
const doubleCheckTime = 55;//~ equals to checkBrokenConnections.CHECK_INTERVAL * 2
|
const doubleCheckTime = 55;//~ equals to checkBrokenConnections.checkInterval * 2
|
||||||
const checkBrokenConnections = checkBrokenConnectionsBuilder({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
|
const checkBrokenConnections = new CheckBrokenConnections({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
|
||||||
const client = new Client({ id: 'id', token: '' });
|
const client = new Client({ id: 'id', token: '' });
|
||||||
realm.setClient(client, 'id');
|
realm.setClient(client, 'id');
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ describe('checkBrokenConnections service', () => {
|
|||||||
expect(realm.getClientById('id')).to.be.undefined;
|
expect(realm.getClientById('id')).to.be.undefined;
|
||||||
checkBrokenConnections.stop();
|
checkBrokenConnections.stop();
|
||||||
done();
|
done();
|
||||||
}, checkBrokenConnections.CHECK_INTERVAL * 2 + 10);
|
}, checkBrokenConnections.checkInterval * 2 + 10);
|
||||||
}, checkBrokenConnections.CHECK_INTERVAL);
|
}, checkBrokenConnections.checkInterval);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -21,11 +21,10 @@
|
|||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"./src/**/*",
|
"./src/**/*",
|
||||||
"./config/**/*",
|
|
||||||
],
|
],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"./test/",
|
"./test/",
|
||||||
"./node_modules/",
|
"./node_modules/",
|
||||||
"./bin/"
|
"./bin/",
|
||||||
]
|
]
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user