()
| 691 | } |
| 692 | |
| 693 | get server() { |
| 694 | if (this._server) { |
| 695 | return this._server; |
| 696 | } |
| 697 | |
| 698 | this.start = Date.now(); |
| 699 | |
| 700 | // Check for secure server requirements, otherwise use HTTP |
| 701 | let { key, cert } = this.options.https; |
| 702 | if(key && cert) { |
| 703 | const { createSecureServer } = require("http2"); |
| 704 | |
| 705 | let options = { |
| 706 | allowHTTP1: true, |
| 707 | |
| 708 | // Credentials |
| 709 | key: fs.readFileSync(key), |
| 710 | cert: fs.readFileSync(cert), |
| 711 | }; |
| 712 | this._server = createSecureServer(options, this.onRequestHandler.bind(this)); |
| 713 | this._serverProtocol = "https:"; |
| 714 | } else { |
| 715 | const { createServer } = require("http"); |
| 716 | |
| 717 | this._server = createServer(this.onRequestHandler.bind(this)); |
| 718 | this._serverProtocol = "http:"; |
| 719 | } |
| 720 | |
| 721 | this.portRetryCount = 0; |
| 722 | this._server.on("error", (err) => { |
| 723 | if (err.code == "EADDRINUSE") { |
| 724 | if (this.portRetryCount < this.options.portReassignmentRetryCount) { |
| 725 | this.portRetryCount++; |
| 726 | debug( |
| 727 | "Server already using port %o, trying the next port %o. Retry number %o of %o", |
| 728 | err.port, |
| 729 | err.port + 1, |
| 730 | this.portRetryCount, |
| 731 | this.options.portReassignmentRetryCount |
| 732 | ); |
| 733 | this._serverListen(err.port + 1); |
| 734 | } else { |
| 735 | throw new Error( |
| 736 | `Tried ${this.options.portReassignmentRetryCount} different ports but they were all in use. You can a different starter port using --port on the command line.` |
| 737 | ); |
| 738 | } |
| 739 | } else { |
| 740 | this._serverErrorHandler(err); |
| 741 | } |
| 742 | }); |
| 743 | |
| 744 | this._server.on("listening", (e) => { |
| 745 | this.setupReloadNotifier(); |
| 746 | |
| 747 | let logMessageCallback = typeof this.options.messageOnStart === "function" ? this.options.messageOnStart : () => false; |
| 748 | let hosts = this.getHosts(); |
| 749 | let message = logMessageCallback({ |
| 750 | hosts, |
nothing calls this directly
no test coverage detected