peerjs.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env node
  2. import path from "node:path";
  3. import { version } from "../package.json";
  4. import fs from "node:fs";
  5. const optimistUsageLength = 98;
  6. import yargs from "yargs";
  7. import { hideBin } from "yargs/helpers";
  8. import { PeerServer } from "../src";
  9. import type { AddressInfo } from "node:net";
  10. import type { CorsOptions } from "cors";
  11. const y = yargs(hideBin(process.argv));
  12. const portEnvIsSet = !!process.env["PORT"];
  13. const opts = y
  14. .usage("Usage: $0")
  15. .wrap(Math.min(optimistUsageLength, y.terminalWidth()))
  16. .options({
  17. expire_timeout: {
  18. demandOption: false,
  19. alias: "t",
  20. describe: "timeout (milliseconds)",
  21. default: 5000,
  22. },
  23. concurrent_limit: {
  24. demandOption: false,
  25. alias: "c",
  26. describe: "concurrent limit",
  27. default: 5000,
  28. },
  29. alive_timeout: {
  30. demandOption: false,
  31. describe: "broken connection check timeout (milliseconds)",
  32. default: 60000,
  33. },
  34. key: {
  35. demandOption: false,
  36. alias: "k",
  37. describe: "connection key",
  38. default: "peerjs",
  39. },
  40. sslkey: {
  41. type: "string",
  42. demandOption: false,
  43. describe: "path to SSL key",
  44. },
  45. sslcert: {
  46. type: "string",
  47. demandOption: false,
  48. describe: "path to SSL certificate",
  49. },
  50. host: {
  51. type: "string",
  52. demandOption: false,
  53. alias: "H",
  54. describe: "host",
  55. },
  56. port: {
  57. type: "number",
  58. demandOption: !portEnvIsSet,
  59. alias: "p",
  60. describe: "port",
  61. },
  62. path: {
  63. type: "string",
  64. demandOption: false,
  65. describe: "custom path",
  66. default: process.env["PEERSERVER_PATH"] || "/",
  67. },
  68. allow_discovery: {
  69. type: "boolean",
  70. demandOption: false,
  71. describe: "allow discovery of peers",
  72. },
  73. proxied: {
  74. type: "boolean",
  75. demandOption: false,
  76. describe: "Set true if PeerServer stays behind a reverse proxy",
  77. default: false,
  78. },
  79. cors: {
  80. type: "string",
  81. array: true,
  82. describe: "Set the list of CORS origins",
  83. },
  84. })
  85. .boolean("allow_discovery")
  86. .parseSync();
  87. if (!opts.port) {
  88. opts.port = parseInt(process.env["PORT"] as string);
  89. }
  90. if (opts.cors) {
  91. opts["corsOptions"] = {
  92. origin: opts.cors,
  93. } satisfies CorsOptions;
  94. }
  95. process.on("uncaughtException", function (e) {
  96. console.error("Error: " + e);
  97. });
  98. if (opts.sslkey || opts.sslcert) {
  99. if (opts.sslkey && opts.sslcert) {
  100. opts["ssl"] = {
  101. key: fs.readFileSync(path.resolve(opts.sslkey)),
  102. cert: fs.readFileSync(path.resolve(opts.sslcert)),
  103. };
  104. } else {
  105. console.error(
  106. "Warning: PeerServer will not run because either " +
  107. "the key or the certificate has not been provided.",
  108. );
  109. process.exit(1);
  110. }
  111. }
  112. const userPath = opts.path;
  113. const server = PeerServer(opts, (server) => {
  114. const { address: host, port } = server.address() as AddressInfo;
  115. console.log(
  116. "Started PeerServer on %s, port: %s, path: %s (v. %s)",
  117. host,
  118. port,
  119. userPath || "/",
  120. version,
  121. );
  122. const shutdownApp = () => {
  123. server.close(() => {
  124. console.log("Http server closed.");
  125. process.exit(0);
  126. });
  127. };
  128. process.on("SIGINT", shutdownApp);
  129. process.on("SIGTERM", shutdownApp);
  130. });
  131. server.on("connection", (client) => {
  132. console.log(`Client connected: ${client.getId()}`);
  133. });
  134. server.on("disconnect", (client) => {
  135. console.log(`Client disconnected: ${client.getId()}`);
  136. });