peerjs.ts 2.9 KB

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