Skip to content

Commit

Permalink
fix: import from ESM only environments
Browse files Browse the repository at this point in the history
refactor: strictest typescript preset
  • Loading branch information
jonasgloning committed Feb 14, 2023
1 parent 669b200 commit 993dee9
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 9,298 deletions.
16 changes: 8 additions & 8 deletions bin/peerjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import {version} from "../package.json";
import fs from "node:fs";
const optimistUsageLength = 98;
import yargs from "yargs";
import { PeerServer } from "../src";
import { AddressInfo } from "node:net";
const opts = yargs
import { PeerServer} from "../src";
import type { AddressInfo } from "node:net";

const y = yargs();

const opts = y
.usage("Usage: $0")
.wrap(Math.min(optimistUsageLength, yargs.terminalWidth()))
.wrap(Math.min(optimistUsageLength, y.terminalWidth()))
.options({
expire_timeout: {
demandOption: false,
Expand Down Expand Up @@ -82,13 +85,10 @@ process.on("uncaughtException", function (e) {

if (opts.sslkey || opts.sslcert) {
if (opts.sslkey && opts.sslcert) {
opts.ssl = {
opts["ssl"] = {
key: fs.readFileSync(path.resolve(opts.sslkey)),
cert: fs.readFileSync(path.resolve(opts.sslcert)),
};

delete opts.sslkey;
delete opts.sslcert;
} else {
console.error(
"Warning: PeerServer will not run because either " +
Expand Down
9,358 changes: 101 additions & 9,257 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@
},
"license": "MIT",
"contributors": [],
"main": "dist/index.js",
"module": "dist/module.js",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/peer.d.ts",
"default": "./dist/module.mjs"
},
"require":{
"types": "./dist/peer.d.ts",
"default": "./dist/index.cjs"
}
}
},
"main": "dist/index.cjs",
"module": "dist/module.mjs",
"source": "src/index.ts",
"binary": "dist/bin/peerjs.js",
"types": "dist/peer.d.ts",
Expand Down Expand Up @@ -73,6 +86,7 @@
"@parcel/transformer-typescript-types": "^2.8.2",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@tsconfig/node16-strictest-esm": "^1.0.3",
"@types/chai": "^4.2.11",
"@types/cors": "^2.8.6",
"@types/mocha": "^10.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/api/v1/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default ({ config, realm }: {
return res.send(clientsIds);
}

res.sendStatus(401);
return res.sendStatus(401);
});

return app;
Expand Down
4 changes: 2 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Server, ServerOptions} from 'ws';
import type {WebSocketServer, ServerOptions} from 'ws';

export interface IConfig {
readonly host: string;
Expand All @@ -16,7 +16,7 @@ export interface IConfig {
cert: string;
};
readonly generateClientId?: () => string;
readonly createWebSocketServer?: (options: ServerOptions) => Server;
readonly createWebSocketServer?: (options: ServerOptions) => WebSocketServer;
}

const defaultConfig: IConfig = {
Expand Down
6 changes: 3 additions & 3 deletions src/instance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from "express";
import {Server as HttpServer} from "node:http";
import {Server as HttpsServer} from "node:https";
import type express from "express";
import type {Server as HttpServer} from "node:http";
import type {Server as HttpsServer} from "node:https";
import path from "node:path";
import type {IRealm} from "./models/realm";
import {Realm} from "./models/realm";
Expand Down
2 changes: 1 addition & 1 deletion src/messageHandler/handlersRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MessageType} from "../enums";
import type {MessageType} from "../enums";
import type {IClient} from "../models/client";
import type {IMessage} from "../models/message";
import type {Handler} from "./handler";
Expand Down
2 changes: 1 addition & 1 deletion src/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export interface IMessage {
readonly type: MessageType;
readonly src: string;
readonly dst: string;
readonly payload?: string;
readonly payload?: string |undefined;
}
2 changes: 1 addition & 1 deletion src/services/messagesExpire/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class MessagesExpire implements IMessagesExpire {
this.messageHandler.handle(undefined, {
type: MessageType.EXPIRE,
src: message.dst,
dst: message.src
dst: message.src,
});

seen[seenKey] = true;
Expand Down
14 changes: 7 additions & 7 deletions src/services/webSocketServer/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {EventEmitter} from "node:events";
import {IncomingMessage} from "node:http";
import type {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";
import {WebSocketServer as Server} from "ws";
import type {Server as HttpServer} from "node:http";
import type {Server as HttpsServer} from "node:https";

export interface IWebSocketServer extends EventEmitter {
readonly path: string;
Expand All @@ -30,7 +30,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {
public readonly path: string;
private readonly realm: IRealm;
private readonly config: CustomConfig;
public readonly socketServer: WebSocketLib.Server;
public readonly socketServer: Server;

constructor({ server, realm, config }: { server: HttpServer | HttpsServer; realm: IRealm; config: CustomConfig; }) {
super();
Expand All @@ -43,15 +43,15 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {
const path = this.config.path;
this.path = `${path}${path.endsWith('/') ? "" : "/"}${WS_PATH}`;

const options: WebSocketLib.ServerOptions = {
const options: WebSocket.ServerOptions = {
path: this.path,
server,
};

this.socketServer = (
config.createWebSocketServer ?
config.createWebSocketServer(options) :
new WebSocketLib.Server(options)
new Server(options)
);

this.socketServer.on("connection", (socket, req) => this._onSocketConnection(socket, req));
Expand Down
20 changes: 5 additions & 15 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
{
"extends": "@tsconfig/node16-strictest-esm/tsconfig.json",
"compilerOptions": {
"lib": [
"esnext"
],
"noEmit": true,
"target": "es2016",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"downlevelIteration": true,
"moduleResolution": "node",
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noEmit": true,
"resolveJsonModule": true,
"skipLibCheck": false,
"sourceMap": true,
"outDir": "dist"
"exactOptionalPropertyTypes": false
},
"include": [
"./src/**/*",
"./src/**/*"
],
"exclude": [
"test",
"bin",
"bin"
]
}

0 comments on commit 993dee9

Please sign in to comment.