Nfsv4TcpServer.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Nfsv4TcpServer = void 0;
  4. const tslib_1 = require("tslib");
  5. const net = tslib_1.__importStar(require("net"));
  6. const Nfsv4Connection_1 = require("./Nfsv4Connection");
  7. const PORT = Number(process.env.NFS_PORT) || Number(process.env.PORT) || 2049;
  8. const HOST = process.env.NFS_HOST
  9. ? String(process.env.NFS_HOST)
  10. : process.env.HOST
  11. ? String(process.env.HOST)
  12. : '127.0.0.1';
  13. class Nfsv4TcpServer {
  14. static start(opts) {
  15. const server = new Nfsv4TcpServer(opts);
  16. server.start().catch(console.error);
  17. }
  18. constructor(opts) {
  19. this.port = PORT;
  20. this.host = HOST;
  21. this.debug = false;
  22. this.port = opts.port ?? PORT;
  23. this.host = opts.host ?? HOST;
  24. this.debug = opts.debug ?? false;
  25. this.logger = opts.logger ?? console;
  26. const ops = opts.ops;
  27. const server = (this.server = new net.Server());
  28. server.on('connection', (socket) => {
  29. if (this.debug)
  30. this.logger.log('New connection from', socket.remoteAddress, 'port', socket.remotePort);
  31. new Nfsv4Connection_1.Nfsv4Connection({
  32. duplex: socket,
  33. ops,
  34. debug: this.debug,
  35. logger: this.logger,
  36. });
  37. });
  38. server.on('error', opts.onError ??
  39. ((err) => {
  40. if (this.debug)
  41. this.logger.error('Server error:', err.message);
  42. process.exit(1);
  43. }));
  44. if (opts.stopOnSigint ?? true) {
  45. this.sigintHandler = () => {
  46. if (this.debug)
  47. this.logger.log('\nShutting down NFSv4 server...');
  48. this.cleanup();
  49. process.exit(0);
  50. };
  51. process.on('SIGINT', this.sigintHandler);
  52. }
  53. }
  54. cleanup() {
  55. if (this.sigintHandler) {
  56. process.off('SIGINT', this.sigintHandler);
  57. this.sigintHandler = undefined;
  58. }
  59. this.server.close((err) => {
  60. if (this.debug && err)
  61. this.logger.error('Error closing server:', err);
  62. });
  63. }
  64. stop() {
  65. return new Promise((resolve) => {
  66. this.cleanup();
  67. this.server.close(() => {
  68. if (this.debug)
  69. this.logger.log('NFSv4 server closed');
  70. resolve();
  71. });
  72. });
  73. }
  74. start(port = this.port, host = this.host) {
  75. if (this.debug)
  76. this.logger.log(`Starting NFSv4 TCP server on ${host}:${port}...`);
  77. return new Promise((resolve, reject) => {
  78. const onError = (err) => reject(err);
  79. const server = this.server;
  80. server.on('error', onError);
  81. server.listen(port, host, () => {
  82. if (this.debug)
  83. this.logger.log(`NFSv4 TCP server listening on ${host}:${port}`);
  84. server.off('error', onError);
  85. resolve();
  86. });
  87. });
  88. }
  89. }
  90. exports.Nfsv4TcpServer = Nfsv4TcpServer;
  91. //# sourceMappingURL=Nfsv4TcpServer.js.map