commit
dc4e45abc1
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,6 +8,7 @@ coverage
|
|||||||
*.pid
|
*.pid
|
||||||
*.gz
|
*.gz
|
||||||
|
|
||||||
|
.parcel-cache
|
||||||
dist
|
dist
|
||||||
pids
|
pids
|
||||||
logs
|
logs
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/mocharc",
|
"$schema": "https://json.schemastore.org/mocharc",
|
||||||
"require": "source-map-support/register",
|
"require": "source-map-support/register",
|
||||||
"require": "ts-node/register"
|
"require": "ts-node/register",
|
||||||
|
"spec": "test/**/*.ts"
|
||||||
}
|
}
|
77
bin/peerjs → bin/peerjs.ts
Executable file → Normal file
77
bin/peerjs → bin/peerjs.ts
Executable file → Normal file
@ -1,14 +1,13 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
// tslint:disable
|
|
||||||
|
|
||||||
const path = require("path");
|
import path from "node:path";
|
||||||
const pkg = require("../package.json");
|
import {version} from "../package.json";
|
||||||
const fs = require("fs");
|
import fs from "node:fs";
|
||||||
const optimistUsageLength = 98;
|
const optimistUsageLength = 98;
|
||||||
const yargs = require("yargs");
|
import yargs from "yargs";
|
||||||
const version = pkg.version;
|
import { PeerServer } from "../src";
|
||||||
const { PeerServer } = require("../dist/src");
|
import { AddressInfo } from "node:net";
|
||||||
const opts = yargs
|
const opts = yargs
|
||||||
.usage("Usage: $0")
|
.usage("Usage: $0")
|
||||||
.wrap(Math.min(optimistUsageLength, yargs.terminalWidth()))
|
.wrap(Math.min(optimistUsageLength, yargs.terminalWidth()))
|
||||||
.options({
|
.options({
|
||||||
@ -16,60 +15,66 @@ const opts = yargs
|
|||||||
demandOption: false,
|
demandOption: false,
|
||||||
alias: "t",
|
alias: "t",
|
||||||
describe: "timeout (milliseconds)",
|
describe: "timeout (milliseconds)",
|
||||||
default: 5000
|
default: 5000,
|
||||||
},
|
},
|
||||||
concurrent_limit: {
|
concurrent_limit: {
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
alias: "c",
|
alias: "c",
|
||||||
describe: "concurrent limit",
|
describe: "concurrent limit",
|
||||||
default: 5000
|
default: 5000,
|
||||||
},
|
},
|
||||||
alive_timeout: {
|
alive_timeout: {
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
describe: "broken connection check timeout (milliseconds)",
|
describe: "broken connection check timeout (milliseconds)",
|
||||||
default: 60000
|
default: 60000,
|
||||||
},
|
},
|
||||||
key: {
|
key: {
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
alias: "k",
|
alias: "k",
|
||||||
describe: "connection key",
|
describe: "connection key",
|
||||||
default: "peerjs"
|
default: "peerjs",
|
||||||
},
|
},
|
||||||
sslkey: {
|
sslkey: {
|
||||||
|
type: "string",
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
describe: "path to SSL key"
|
describe: "path to SSL key",
|
||||||
},
|
},
|
||||||
sslcert: {
|
sslcert: {
|
||||||
|
type: "string",
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
describe: "path to SSL certificate"
|
describe: "path to SSL certificate",
|
||||||
},
|
},
|
||||||
host: {
|
host: {
|
||||||
|
type: "string",
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
alias: "H",
|
alias: "H",
|
||||||
describe: "host"
|
describe: "host",
|
||||||
},
|
},
|
||||||
port: {
|
port: {
|
||||||
|
type: "number",
|
||||||
demandOption: true,
|
demandOption: true,
|
||||||
alias: "p",
|
alias: "p",
|
||||||
describe: "port"
|
describe: "port",
|
||||||
},
|
},
|
||||||
path: {
|
path: {
|
||||||
|
type: "string",
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
describe: "custom path",
|
describe: "custom path",
|
||||||
default: "/"
|
default: "/",
|
||||||
},
|
},
|
||||||
allow_discovery: {
|
allow_discovery: {
|
||||||
|
type: "boolean",
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
describe: "allow discovery of peers"
|
describe: "allow discovery of peers",
|
||||||
},
|
},
|
||||||
proxied: {
|
proxied: {
|
||||||
|
type: "boolean",
|
||||||
demandOption: false,
|
demandOption: false,
|
||||||
describe: "Set true if PeerServer stays behind a reverse proxy",
|
describe: "Set true if PeerServer stays behind a reverse proxy",
|
||||||
default: false
|
default: false,
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
.boolean("allow_discovery")
|
.boolean("allow_discovery").parseSync();
|
||||||
.argv;
|
|
||||||
|
|
||||||
process.on("uncaughtException", function (e) {
|
process.on("uncaughtException", function (e) {
|
||||||
console.error("Error: " + e);
|
console.error("Error: " + e);
|
||||||
@ -79,44 +84,48 @@ if (opts.sslkey || opts.sslcert) {
|
|||||||
if (opts.sslkey && opts.sslcert) {
|
if (opts.sslkey && opts.sslcert) {
|
||||||
opts.ssl = {
|
opts.ssl = {
|
||||||
key: fs.readFileSync(path.resolve(opts.sslkey)),
|
key: fs.readFileSync(path.resolve(opts.sslkey)),
|
||||||
cert: fs.readFileSync(path.resolve(opts.sslcert))
|
cert: fs.readFileSync(path.resolve(opts.sslcert)),
|
||||||
};
|
};
|
||||||
|
|
||||||
delete opts.sslkey;
|
delete opts.sslkey;
|
||||||
delete opts.sslcert;
|
delete opts.sslcert;
|
||||||
} else {
|
} else {
|
||||||
console.error("Warning: PeerServer will not run because either " +
|
console.error(
|
||||||
"the key or the certificate has not been provided.");
|
"Warning: PeerServer will not run because either " +
|
||||||
|
"the key or the certificate has not been provided."
|
||||||
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const userPath = opts.path;
|
const userPath = opts.path;
|
||||||
const server = PeerServer(opts, server => {
|
const server = PeerServer(opts, (server) => {
|
||||||
const host = server.address().address;
|
const { address: host, port } = server.address() as AddressInfo;
|
||||||
const port = server.address().port;
|
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"Started PeerServer on %s, port: %s, path: %s (v. %s)",
|
"Started PeerServer on %s, port: %s, path: %s (v. %s)",
|
||||||
host, port, userPath || "/", version
|
host,
|
||||||
|
port,
|
||||||
|
userPath || "/",
|
||||||
|
version
|
||||||
);
|
);
|
||||||
|
|
||||||
const shutdownApp = () => {
|
const shutdownApp = () => {
|
||||||
server.close(() => {
|
server.close(() => {
|
||||||
console.log('Http server closed.');
|
console.log("Http server closed.");
|
||||||
|
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
process.on('SIGINT', shutdownApp);
|
process.on("SIGINT", shutdownApp);
|
||||||
process.on('SIGTERM', shutdownApp);
|
process.on("SIGTERM", shutdownApp);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("connection", client => {
|
server.on("connection", (client) => {
|
||||||
console.log(`Client connected: ${client.getId()}`);
|
console.log(`Client connected: ${client.getId()}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("disconnect", client => {
|
server.on("disconnect", (client) => {
|
||||||
console.log(`Client disconnected: ${client.getId()}`);
|
console.log(`Client disconnected: ${client.getId()}`);
|
||||||
});
|
});
|
6129
package-lock.json
generated
6129
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
81
package.json
81
package.json
@ -1,54 +1,73 @@
|
|||||||
{
|
{
|
||||||
"name": "peer",
|
"name": "peer",
|
||||||
"version": "0.0.0-development",
|
"version": "0.0.0-development",
|
||||||
"description": "PeerJS server component",
|
|
||||||
"main": "dist/src/index.js",
|
|
||||||
"bin": {
|
|
||||||
"peerjs": "./bin/peerjs"
|
|
||||||
},
|
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"peerjs",
|
"peerjs",
|
||||||
"webrtc",
|
"webrtc",
|
||||||
"signaling"
|
"p2p",
|
||||||
|
"rtc"
|
||||||
],
|
],
|
||||||
"files": [
|
"description": "PeerJS server component",
|
||||||
"bin/",
|
"homepage": "https://peerjs.com",
|
||||||
"dist/",
|
|
||||||
"index.d.ts"
|
|
||||||
],
|
|
||||||
"homepage": "https://github.com/peers/peerjs-server#readme",
|
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/peers/peerjs-server/issues"
|
"url": "https://github.com/peers/peerjs-server/issues"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/peers/peerjs-server.git"
|
"url": "https://github.com/peers/peerjs-server"
|
||||||
},
|
},
|
||||||
"author": "Michelle Bu, Eric Zhang, Alex Sosnovskiy",
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"contributors": [],
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"module": "dist/module.js",
|
||||||
|
"source": "src/index.ts",
|
||||||
|
"binary": "dist/bin/peerjs.js",
|
||||||
|
"types": "dist/peer.d.ts",
|
||||||
|
"bin": {
|
||||||
|
"peerjs": "dist/bin/peerjs.js"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/peer"
|
||||||
|
},
|
||||||
|
"collective": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/peer"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist/"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"targets": {
|
||||||
|
"binary": {
|
||||||
|
"source": "bin/peerjs.ts"
|
||||||
|
},
|
||||||
|
"main": {},
|
||||||
|
"module": {}
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"preversion": "npm run clean && npm run build",
|
"build": "parcel build",
|
||||||
"build": "tsc",
|
"lint": "eslint --ext .js,.ts . && npm run check",
|
||||||
"clean": "rimraf ./dist",
|
"check": "tsc --noEmit",
|
||||||
"lint": "eslint --ext .js,.ts .",
|
"test": "npm run lint && mocha",
|
||||||
"tsc": "tsc",
|
"coverage": "nyc mocha",
|
||||||
"prebuild": "npm run lint",
|
"coverage:lcov": "nyc --reporter=lcov mocha",
|
||||||
"test": "npm run lint && mocha \"test/**/*\"",
|
"start": "dist/bin/peerjs.js --port ${PORT:=9000}",
|
||||||
"coverage": "nyc mocha \"test/**/*\"",
|
"dev": "nodemon --watch src -e ts --exec npm run start",
|
||||||
"coverage:lcov": "nyc --reporter=lcov mocha \"test/**/*\"",
|
|
||||||
"start": "bin/peerjs --port ${PORT:=9000}",
|
|
||||||
"dev:start": "npm-run-all build start",
|
|
||||||
"dev": "nodemon --watch src -e ts --exec npm run dev:start",
|
|
||||||
"semantic-release": "semantic-release"
|
"semantic-release": "semantic-release"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"ws": "^7.2.3",
|
"ws": "^7.2.3",
|
||||||
"yargs": "^15.3.1"
|
"yargs": "^17.6.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@istanbuljs/nyc-config-typescript": "^1.0.2",
|
"@istanbuljs/nyc-config-typescript": "^1.0.2",
|
||||||
|
"@parcel/packager-ts": "^2.8.2",
|
||||||
|
"@parcel/transformer-typescript-types": "^2.8.2",
|
||||||
"@semantic-release/changelog": "^6.0.1",
|
"@semantic-release/changelog": "^6.0.1",
|
||||||
"@semantic-release/git": "^10.0.1",
|
"@semantic-release/git": "^10.0.1",
|
||||||
"@types/chai": "^4.2.11",
|
"@types/chai": "^4.2.11",
|
||||||
@ -57,6 +76,7 @@
|
|||||||
"@types/mocha": "^7.0.2",
|
"@types/mocha": "^7.0.2",
|
||||||
"@types/node": "^14.18.33",
|
"@types/node": "^14.18.33",
|
||||||
"@types/ws": "^7.2.3",
|
"@types/ws": "^7.2.3",
|
||||||
|
"@types/yargs": "^17.0.19",
|
||||||
"@typescript-eslint/eslint-plugin": "^2.24.0",
|
"@typescript-eslint/eslint-plugin": "^2.24.0",
|
||||||
"@typescript-eslint/parser": "^2.24.0",
|
"@typescript-eslint/parser": "^2.24.0",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
@ -64,16 +84,11 @@
|
|||||||
"mocha": "^10.1.0",
|
"mocha": "^10.1.0",
|
||||||
"mock-socket": "^9.1.5",
|
"mock-socket": "^9.1.5",
|
||||||
"nodemon": "^2.0.20",
|
"nodemon": "^2.0.20",
|
||||||
"npm-run-all": "^4.1.5",
|
|
||||||
"nyc": "^15.1.0",
|
"nyc": "^15.1.0",
|
||||||
"rimraf": "^3.0.2",
|
"parcel": "^2.8.2",
|
||||||
"semantic-release": "^19.0.5",
|
"semantic-release": "^19.0.5",
|
||||||
"sinon": "^7.5.0",
|
|
||||||
"source-map-support": "^0.5.21",
|
"source-map-support": "^0.5.21",
|
||||||
"ts-node": "^8.10.2",
|
"ts-node": "^8.10.2",
|
||||||
"typescript": "^4.1.2"
|
"typescript": "^4.1.2"
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=14"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
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 { IRealm } from "../models/realm";
|
|
||||||
import PublicApi from "./v1/public";
|
import PublicApi from "./v1/public";
|
||||||
|
import type {IConfig} from "../config";
|
||||||
|
import type {IRealm} from "../models/realm";
|
||||||
|
|
||||||
export const Api = ({ config, realm }: {
|
export const Api = ({ config, realm }: {
|
||||||
config: IConfig;
|
config: IConfig;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { IConfig } from "../../../config";
|
import type {IConfig} from "../../../config";
|
||||||
import { IRealm } from "../../../models/realm";
|
import type {IRealm} from "../../../models/realm";
|
||||||
|
|
||||||
export default ({ config, realm }: {
|
export default ({ config, realm }: {
|
||||||
config: IConfig; realm: IRealm;
|
config: IConfig; realm: IRealm;
|
||||||
|
26
src/index.ts
26
src/index.ts
@ -1,16 +1,18 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import http from "http";
|
import http from "node:http";
|
||||||
import https from "https";
|
import https from "node:https";
|
||||||
import { Server } from "net";
|
import {Server as HttpServer} from "node:http";
|
||||||
|
import {Server as HttpsServer} from "node:https";
|
||||||
|
import type {Express} from 'express-serve-static-core';
|
||||||
|
|
||||||
import defaultConfig, { IConfig } from "./config";
|
import type {IConfig} from "./config";
|
||||||
import { createInstance } from "./instance";
|
import defaultConfig from "./config";
|
||||||
|
import type {PeerServerEvents} from "./instance";
|
||||||
|
import {createInstance} from "./instance";
|
||||||
|
|
||||||
type Optional<T> = {
|
export type {MessageType} from "./enums"
|
||||||
[P in keyof T]?: (T[P] | undefined);
|
|
||||||
};
|
|
||||||
|
|
||||||
function ExpressPeerServer(server: Server, options?: IConfig) {
|
function ExpressPeerServer(server: HttpsServer | HttpServer, options?: Partial<IConfig>) {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
const newOptions: IConfig = {
|
const newOptions: IConfig = {
|
||||||
@ -31,10 +33,10 @@ function ExpressPeerServer(server: Server, options?: IConfig) {
|
|||||||
createInstance({ app, server, options: newOptions });
|
createInstance({ app, server, options: newOptions });
|
||||||
});
|
});
|
||||||
|
|
||||||
return app;
|
return app as Express & PeerServerEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
function PeerServer(options: Optional<IConfig> = {}, callback?: (server: Server) => void) {
|
function PeerServer(options: Partial<IConfig> = {}, callback?: (server: HttpsServer | HttpServer) => void) {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
let newOptions: IConfig = {
|
let newOptions: IConfig = {
|
||||||
@ -45,7 +47,7 @@ function PeerServer(options: Optional<IConfig> = {}, callback?: (server: Server)
|
|||||||
const port = newOptions.port;
|
const port = newOptions.port;
|
||||||
const host = newOptions.host;
|
const host = newOptions.host;
|
||||||
|
|
||||||
let server: Server;
|
let server: HttpsServer | HttpServer;
|
||||||
|
|
||||||
const { ssl, ...restOptions } = newOptions;
|
const { ssl, ...restOptions } = newOptions;
|
||||||
if (ssl && Object.keys(ssl).length) {
|
if (ssl && Object.keys(ssl).length) {
|
||||||
|
@ -1,20 +1,30 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { Server } from "net";
|
import {Server as HttpServer} from "node:http";
|
||||||
import path from "path";
|
import {Server as HttpsServer} from "node:https";
|
||||||
import { IClient } from "./models/client";
|
import path from "node:path";
|
||||||
import { IMessage } from "./models/message";
|
import type {IRealm} from "./models/realm";
|
||||||
import { Realm } from "./models/realm";
|
import {Realm} from "./models/realm";
|
||||||
import { IRealm } from "./models/realm";
|
import {CheckBrokenConnections} from "./services/checkBrokenConnections";
|
||||||
import { CheckBrokenConnections } from "./services/checkBrokenConnections";
|
import type {IMessagesExpire} from "./services/messagesExpire";
|
||||||
import { IMessagesExpire, MessagesExpire } from "./services/messagesExpire";
|
import {MessagesExpire} from "./services/messagesExpire";
|
||||||
import { IWebSocketServer, WebSocketServer } from "./services/webSocketServer";
|
import type {IWebSocketServer} from "./services/webSocketServer";
|
||||||
import { MessageHandler } from "./messageHandler";
|
import {WebSocketServer} from "./services/webSocketServer";
|
||||||
import { Api } from "./api";
|
import {MessageHandler} from "./messageHandler";
|
||||||
import { IConfig } from "./config";
|
import {Api} from "./api";
|
||||||
|
import type {IClient} from "./models/client";
|
||||||
|
import type {IMessage} from "./models/message";
|
||||||
|
import type {IConfig} from "./config";
|
||||||
|
|
||||||
|
export interface PeerServerEvents {
|
||||||
|
on(event: 'connection', listener: (client: IClient) => void): this;
|
||||||
|
on(event: "message", listener: (client: IClient, message: IMessage) => void): this;
|
||||||
|
on(event: "disconnect", listener: (client: IClient) => void): this;
|
||||||
|
on(event: "error", listener: (client: Error) => void): this;
|
||||||
|
}
|
||||||
|
|
||||||
export const createInstance = ({ app, server, options }: {
|
export const createInstance = ({ app, server, options }: {
|
||||||
app: express.Application;
|
app: express.Application;
|
||||||
server: Server;
|
server: HttpServer | HttpsServer;
|
||||||
options: IConfig;
|
options: IConfig;
|
||||||
}): void => {
|
}): void => {
|
||||||
const config = options;
|
const config = options;
|
||||||
@ -72,4 +82,4 @@ export const createInstance = ({ app, server, options }: {
|
|||||||
|
|
||||||
messagesExpire.startMessagesExpiration();
|
messagesExpire.startMessagesExpiration();
|
||||||
checkBrokenConnections.start();
|
checkBrokenConnections.start();
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { IClient } from "../models/client";
|
import type {IClient} from "../models/client";
|
||||||
import { IMessage } from "../models/message";
|
import type {IMessage} from "../models/message";
|
||||||
|
|
||||||
export type Handler = (client: IClient | undefined, message: IMessage) => boolean;
|
export type Handler = (client: IClient | undefined, message: IMessage) => boolean;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { IClient } from "../../../models/client";
|
import type {IClient} from "../../../models/client";
|
||||||
|
|
||||||
export const HeartbeatHandler = (client: IClient | undefined): boolean => {
|
export const HeartbeatHandler = (client: IClient | undefined): boolean => {
|
||||||
if (client) {
|
if (client) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { MessageType } from "../../../enums";
|
import {MessageType} from "../../../enums";
|
||||||
import { IClient } from "../../../models/client";
|
import type {IClient} from "../../../models/client";
|
||||||
import { IMessage } from "../../../models/message";
|
import type {IMessage} from "../../../models/message";
|
||||||
import { IRealm } from "../../../models/realm";
|
import type {IRealm} from "../../../models/realm";
|
||||||
|
|
||||||
export const TransmissionHandler = ({ realm }: { realm: IRealm; }): (client: IClient | undefined, message: IMessage) => boolean => {
|
export const TransmissionHandler = ({ realm }: { realm: IRealm; }): (client: IClient | undefined, message: IMessage) => boolean => {
|
||||||
const handle = (client: IClient | undefined, message: IMessage) => {
|
const handle = (client: IClient | undefined, message: IMessage) => {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { MessageType } from "../enums";
|
import {MessageType} from "../enums";
|
||||||
import { IClient } from "../models/client";
|
import type {IClient} from "../models/client";
|
||||||
import { IMessage } from "../models/message";
|
import type {IMessage} from "../models/message";
|
||||||
import { Handler } from "./handler";
|
import type {Handler} from "./handler";
|
||||||
|
|
||||||
export interface IHandlersRegistry {
|
export interface IHandlersRegistry {
|
||||||
registerHandler(messageType: MessageType, handler: Handler): void;
|
registerHandler(messageType: MessageType, handler: Handler): void;
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
import { MessageType } from "../enums";
|
import {MessageType} from "../enums";
|
||||||
import { IClient } from "../models/client";
|
import {HeartbeatHandler, TransmissionHandler} from "./handlers";
|
||||||
import { IMessage } from "../models/message";
|
import type {IHandlersRegistry} from "./handlersRegistry";
|
||||||
import { IRealm } from "../models/realm";
|
import {HandlersRegistry} from "./handlersRegistry";
|
||||||
import { Handler } from "./handler";
|
import type {IClient} from "../models/client";
|
||||||
import { HeartbeatHandler, TransmissionHandler } from "./handlers";
|
import type {IMessage} from "../models/message";
|
||||||
import { IHandlersRegistry, HandlersRegistry } from "./handlersRegistry";
|
import type {IRealm} from "../models/realm";
|
||||||
|
import type {Handler} from "./handler";
|
||||||
|
|
||||||
export interface IMessageHandler {
|
export interface IMessageHandler {
|
||||||
handle(client: IClient | undefined, message: IMessage): boolean;
|
handle(client: IClient | undefined, message: IMessage): boolean;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { MessageType } from "../enums";
|
import type {MessageType} from "../enums";
|
||||||
|
|
||||||
export interface IMessage {
|
export interface IMessage {
|
||||||
readonly type: MessageType;
|
readonly type: MessageType;
|
||||||
readonly src: string;
|
readonly src: string;
|
||||||
readonly dst: string;
|
readonly dst: string;
|
||||||
readonly payload?: any;
|
readonly payload?: string;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { IMessage } from "./message";
|
import type {IMessage} from "./message";
|
||||||
|
|
||||||
export interface IMessageQueue {
|
export interface IMessageQueue {
|
||||||
getLastReadAt(): number;
|
getLastReadAt(): number;
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import { IClient } from "./client";
|
import type {IMessageQueue} from "./messageQueue";
|
||||||
import { IMessage } from "./message";
|
import {MessageQueue} from "./messageQueue";
|
||||||
import { IMessageQueue, MessageQueue } from "./messageQueue";
|
import {randomUUID} from "node:crypto";
|
||||||
import {randomUUID} from "crypto";
|
import type {IClient} from "./client";
|
||||||
|
import type {IMessage} from "./message";
|
||||||
|
|
||||||
export interface IRealm {
|
export interface IRealm {
|
||||||
getClientsIds(): string[];
|
getClientsIds(): string[];
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { IConfig } from "../../config";
|
import type {IConfig} from "../../config";
|
||||||
import { IClient } from "../../models/client";
|
import type {IClient} from "../../models/client";
|
||||||
import { IRealm } from "../../models/realm";
|
import type {IRealm} from "../../models/realm";
|
||||||
|
|
||||||
const DEFAULT_CHECK_INTERVAL = 300;
|
const DEFAULT_CHECK_INTERVAL = 300;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { IConfig } from "../../config";
|
import {MessageType} from "../../enums";
|
||||||
import { MessageType } from "../../enums";
|
import type {IConfig} from "../../config";
|
||||||
import { IMessageHandler } from "../../messageHandler";
|
import type {IMessageHandler} from "../../messageHandler";
|
||||||
import { IRealm } from "../../models/realm";
|
import type {IRealm} from "../../models/realm";
|
||||||
|
|
||||||
export interface IMessagesExpire {
|
export interface IMessagesExpire {
|
||||||
startMessagesExpiration(): void;
|
startMessagesExpiration(): void;
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
import EventEmitter from "events";
|
import {EventEmitter} from "node:events";
|
||||||
import { IncomingMessage } from "http";
|
import {IncomingMessage} from "node:http";
|
||||||
import url from "url";
|
import url from "node:url";
|
||||||
import WebSocketLib from "ws";
|
|
||||||
import { IConfig } from "../../config";
|
|
||||||
import { Errors, MessageType } from "../../enums";
|
|
||||||
import { Client, IClient } from "../../models/client";
|
|
||||||
import { IRealm } from "../../models/realm";
|
|
||||||
import type WebSocket from "ws";
|
import type WebSocket from "ws";
|
||||||
|
import * as WebSocketLib from "ws";
|
||||||
|
import {Errors, MessageType} from "../../enums";
|
||||||
|
import type {IClient} from "../../models/client";
|
||||||
|
import {Client} from "../../models/client";
|
||||||
|
import type {IConfig} from "../../config";
|
||||||
|
import type {IRealm} from "../../models/realm";
|
||||||
|
import {Server as HttpServer} from "node:http";
|
||||||
|
import {Server as HttpsServer} from "node:https";
|
||||||
|
|
||||||
export interface IWebSocketServer extends EventEmitter {
|
export interface IWebSocketServer extends EventEmitter {
|
||||||
readonly path: string;
|
readonly path: string;
|
||||||
@ -29,7 +32,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {
|
|||||||
private readonly config: CustomConfig;
|
private readonly config: CustomConfig;
|
||||||
public readonly socketServer: WebSocketLib.Server;
|
public readonly socketServer: WebSocketLib.Server;
|
||||||
|
|
||||||
constructor({ server, realm, config }: { server: any; realm: IRealm; config: CustomConfig; }) {
|
constructor({ server, realm, config }: { server: HttpServer | HttpsServer; realm: IRealm; config: CustomConfig; }) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.setMaxListeners(0);
|
this.setMaxListeners(0);
|
||||||
|
@ -35,7 +35,7 @@ describe('Check bin/peerjs', () => {
|
|||||||
rejecter = reject;
|
rejecter = reject;
|
||||||
});
|
});
|
||||||
|
|
||||||
const ls = spawn('node', [path.join(__dirname, '../', 'bin/peerjs'), '--port', PORT]);
|
const ls = spawn('node', [path.join(__dirname, '../', 'dist/bin/peerjs.js'), '--port', PORT]);
|
||||||
|
|
||||||
ls.stdout.on('data', async (data: string) => {
|
ls.stdout.on('data', async (data: string) => {
|
||||||
if (!data.includes('Started')) return;
|
if (!data.includes('Started')) return;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { Server, WebSocket } from 'mock-socket';
|
import { Server, WebSocket } from 'mock-socket';
|
||||||
|
import type {Server as HttpServer} from 'node:http';
|
||||||
import { Realm } from '../../../src/models/realm';
|
import { Realm } from '../../../src/models/realm';
|
||||||
import { WebSocketServer } from '../../../src/services/webSocketServer';
|
import { WebSocketServer } from '../../../src/services/webSocketServer';
|
||||||
import { Errors, MessageType } from '../../../src/enums';
|
import { Errors, MessageType } from '../../../src/enums';
|
||||||
@ -52,10 +53,10 @@ const checkSequence = async (c: WebSocket, msgs: { type: MessageType; error?: Er
|
|||||||
};
|
};
|
||||||
|
|
||||||
const createTestServer = ({ realm, config, url }: { realm: Realm; config: { path: string; key: string; concurrent_limit: number; }; url: string; }): Destroyable<WebSocketServer> => {
|
const createTestServer = ({ realm, config, url }: { realm: Realm; config: { path: string; key: string; concurrent_limit: number; }; url: string; }): Destroyable<WebSocketServer> => {
|
||||||
const server = new Server(url);
|
const server = new Server(url) as Server & HttpServer;
|
||||||
const webSocketServer: Destroyable<WebSocketServer> = new WebSocketServer({ server, realm, config });
|
const webSocketServer: Destroyable<WebSocketServer> = new WebSocketServer({ server, realm, config });
|
||||||
|
|
||||||
server.on('connection', (socket) => {
|
server.on('connection', (socket: WebSocket & { on?: (eventName: string, callback: () => void) => void; }) => {
|
||||||
const s = webSocketServer.socketServer;
|
const s = webSocketServer.socketServer;
|
||||||
s.emit('connection', socket, { url: socket.url });
|
s.emit('connection', socket, { url: socket.url });
|
||||||
|
|
||||||
@ -101,8 +102,8 @@ describe('WebSocketServer', () => {
|
|||||||
const realm = new Realm();
|
const realm = new Realm();
|
||||||
const config = { path: '/', key: 'testKey', concurrent_limit: 1 };
|
const config = { path: '/', key: 'testKey', concurrent_limit: 1 };
|
||||||
const config2 = { ...config, path: 'path' };
|
const config2 = { ...config, path: 'path' };
|
||||||
const server = new Server('path1');
|
const server = new Server('path1') as Server & HttpServer;
|
||||||
const server2 = new Server('path2');
|
const server2 = new Server('path2') as Server & HttpServer;
|
||||||
|
|
||||||
const webSocketServer = new WebSocketServer({ server, realm, config });
|
const webSocketServer = new WebSocketServer({ server, realm, config });
|
||||||
|
|
||||||
@ -155,11 +156,11 @@ describe('WebSocketServer', () => {
|
|||||||
ws.destroy = async (): Promise<void> => {
|
ws.destroy = async (): Promise<void> => {
|
||||||
ws.close();
|
ws.close();
|
||||||
|
|
||||||
wait(10);
|
wait(10);
|
||||||
|
|
||||||
webSocketServer.destroy?.();
|
webSocketServer.destroy?.();
|
||||||
|
|
||||||
wait(10);
|
wait(10);
|
||||||
|
|
||||||
ws.destroy = undefined;
|
ws.destroy = undefined;
|
||||||
};
|
};
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
"lib": [
|
"lib": [
|
||||||
"esnext"
|
"esnext"
|
||||||
],
|
],
|
||||||
|
"noEmit": true,
|
||||||
"target": "es2016",
|
"target": "es2016",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
@ -13,7 +14,7 @@
|
|||||||
"noUnusedLocals": true,
|
"noUnusedLocals": true,
|
||||||
"noUnusedParameters": true,
|
"noUnusedParameters": true,
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": false,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outDir": "dist"
|
"outDir": "dist"
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user