UI for Zipcoin Blue

network.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const os = require("os");
  5. exports.ERROR_NETWORK_ADDRESS_NOT_AVAIL = 'NETWORK_ADDRESS_NOT_AVAIL';
  6. function getSuitableNetworkInterfaces() {
  7. const networkInterfaces = os.networkInterfaces();
  8. const devices = [];
  9. for (let deviceName of Object.keys(networkInterfaces)) {
  10. const networkInterface = networkInterfaces[deviceName];
  11. for (let networkAddress of networkInterface) {
  12. if (!networkAddress.internal && networkAddress.family === 'IPv4') {
  13. devices.push(Object.assign({ deviceName }, networkAddress));
  14. }
  15. }
  16. }
  17. return devices;
  18. }
  19. exports.getSuitableNetworkInterfaces = getSuitableNetworkInterfaces;
  20. function findClosestOpenPort(port, host) {
  21. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  22. function t(portToCheck) {
  23. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  24. if (yield isPortAvailable(portToCheck, host)) {
  25. return portToCheck;
  26. }
  27. return t(portToCheck + 1);
  28. });
  29. }
  30. return t(port);
  31. });
  32. }
  33. exports.findClosestOpenPort = findClosestOpenPort;
  34. function isPortAvailable(port, host) {
  35. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  36. const net = yield Promise.resolve().then(() => require('net'));
  37. return new Promise((resolve, reject) => {
  38. const tester = net.createServer()
  39. .once('error', (err) => {
  40. if (err.code === 'EADDRNOTAVAIL') {
  41. reject(exports.ERROR_NETWORK_ADDRESS_NOT_AVAIL);
  42. }
  43. else if (err.code === 'EADDRINUSE') {
  44. resolve(false); // host/port in use
  45. }
  46. else {
  47. reject(err);
  48. }
  49. })
  50. .once('listening', () => {
  51. tester.once('close', () => {
  52. resolve(true); // found available host/port
  53. })
  54. .close();
  55. })
  56. .on('error', (err) => {
  57. reject(err);
  58. })
  59. .listen(port, host);
  60. });
  61. });
  62. }
  63. exports.isPortAvailable = isPortAvailable;