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