Skip to content

Commit

Permalink
fix: more accurate types
Browse files Browse the repository at this point in the history
Closes #182
  • Loading branch information
jonasgloning committed Jan 9, 2023
1 parent 2b73b5c commit 68f973a
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 83 deletions.
8 changes: 4 additions & 4 deletions bin/peerjs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

import path from "path";
import path from "node:path";
import {version} from "../package.json";
import fs from "fs";
import fs from "node:fs";
const optimistUsageLength = 98;
import yargs from "yargs";
import { PeerServer } from "../src";
import { AddressInfo } from "net";
import { AddressInfo } from "node:net";
const opts = yargs
.usage("Usage: $0")
.wrap(Math.min(optimistUsageLength, yargs.terminalWidth()))
Expand Down Expand Up @@ -74,7 +74,7 @@ const opts = yargs
default: false,
},
})
.boolean("allow_discovery").argv;
.boolean("allow_discovery").parseSync();

process.on("uncaughtException", function (e) {
console.error("Error: " + e);
Expand Down
4 changes: 2 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/api/v1/public/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
26 changes: 14 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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) {
Expand Down
38 changes: 24 additions & 14 deletions src/instance.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -72,4 +82,4 @@ export const createInstance = ({ app, server, options }: {

messagesExpire.startMessagesExpiration();
checkBrokenConnections.start();
};
};
4 changes: 2 additions & 2 deletions src/messageHandler/handler.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion src/messageHandler/handlers/heartbeat/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IClient } from "../../../models/client";
import type {IClient} from "../../../models/client";

export const HeartbeatHandler = (client: IClient | undefined): boolean => {
if (client) {
Expand Down
8 changes: 4 additions & 4 deletions src/messageHandler/handlers/transmission/index.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
8 changes: 4 additions & 4 deletions src/messageHandler/handlersRegistry.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
15 changes: 8 additions & 7 deletions src/messageHandler/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/models/message.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/models/messageQueue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IMessage } from "./message";
import type {IMessage} from "./message";

export interface IMessageQueue {
getLastReadAt(): number;
Expand Down
9 changes: 5 additions & 4 deletions src/models/realm.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand Down
6 changes: 3 additions & 3 deletions src/services/checkBrokenConnections/index.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
8 changes: 4 additions & 4 deletions src/services/messagesExpire/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
21 changes: 12 additions & 9 deletions src/services/webSocketServer/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions test/services/webSocketServer/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 });

Expand Down Expand Up @@ -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 });

Expand Down Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"skipLibCheck": false,
"sourceMap": true,
"outDir": "dist"
},
Expand Down

0 comments on commit 68f973a

Please sign in to comment.