feat: ESM support

feat: automatic .d.ts generation

chore(deps): yargs@17
This commit is contained in:
Jonas Gloning 2023-01-08 00:24:02 +01:00
parent 59d4f6671b
commit 2b73b5c97d
No known key found for this signature in database
GPG Key ID: 684639B5E59E7614
6 changed files with 4718 additions and 423 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ coverage
*.pid *.pid
*.gz *.gz
.parcel-cache
dist dist
pids pids
logs logs

77
bin/peerjs → bin/peerjs.ts Executable file → Normal file
View File

@ -1,14 +1,13 @@
#!/usr/bin/env node #!/usr/bin/env node
// tslint:disable
const path = require("path"); import path from "path";
const pkg = require("../package.json"); import {version} from "../package.json";
const fs = require("fs"); import fs from "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 "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").argv;
.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()}`);
}); });

5021
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -18,9 +18,13 @@
}, },
"license": "MIT", "license": "MIT",
"contributors": [], "contributors": [],
"main": "dist/src/index.js", "main": "dist/index.js",
"module": "dist/module.js",
"source": "src/index.ts",
"binary": "dist/bin/peerjs.js",
"types": "dist/peer.d.ts",
"bin": { "bin": {
"peerjs": "./bin/peerjs" "peerjs": "dist/bin/peerjs.js"
}, },
"funding": { "funding": {
"type": "opencollective", "type": "opencollective",
@ -31,36 +35,39 @@
"url": "https://opencollective.com/peer" "url": "https://opencollective.com/peer"
}, },
"files": [ "files": [
"bin/", "dist/"
"dist/",
"index.d.ts"
], ],
"engines": { "engines": {
"node": ">=14" "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 .",
"tsc": "tsc",
"prebuild": "npm run lint",
"test": "npm run lint && mocha \"test/**/*\"", "test": "npm run lint && mocha \"test/**/*\"",
"coverage": "nyc mocha \"test/**/*\"", "coverage": "nyc mocha \"test/**/*\"",
"coverage:lcov": "nyc --reporter=lcov mocha \"test/**/*\"", "coverage:lcov": "nyc --reporter=lcov mocha \"test/**/*\"",
"start": "bin/peerjs --port ${PORT:=9000}", "start": "dist/bin/peerjs.js --port ${PORT:=9000}",
"dev:start": "npm-run-all build start", "dev": "nodemon --watch src -e ts --exec npm run 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",
@ -69,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",
@ -78,6 +86,7 @@
"nodemon": "^2.0.20", "nodemon": "^2.0.20",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"parcel": "^2.8.2",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"semantic-release": "^19.0.5", "semantic-release": "^19.0.5",
"sinon": "^7.5.0", "sinon": "^7.5.0",

View File

@ -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;

View File

@ -3,6 +3,7 @@
"lib": [ "lib": [
"esnext" "esnext"
], ],
"noEmit": true,
"target": "es2016", "target": "es2016",
"module": "commonjs", "module": "commonjs",
"strict": true, "strict": true,