From ace176ce3461906297138c83c6bed81b24503b60 Mon Sep 17 00:00:00 2001 From: "Dr. Sergey Pogodin" Date: Fri, 31 Mar 2023 00:19:19 +0200 Subject: [PATCH] [#21] Adds "hostname" to Server constructor's options --- README.md | 28 ++++++++++++++++++++++++---- src/index.tsx | 32 +++++++++++++++----------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 68ee23e1..f5e5ced3 100644 --- a/README.md +++ b/README.md @@ -362,10 +362,30 @@ within `options` argument: path; however, empty `fileDir` value is forbidden: if you really want to serve entire documents directory of the app, provide its absolute path explicitly. -- `nonLocal` — **boolean** — Optional. By default, the server is started on - `localhost` address, and it is only accessible within the app. With this flag - set **true** the server will be started on a local IP adress also accessible - from outside the app. +- `hostname` — **string** — Optional. Sets the address for server + to bind to. + - By default, if `nonLocal` option is **false**, `hostname` is set equal + "`localhost`" — the server binds to the localhost (127.0.0.1) + loopback address, and it is accessible only from within the host app. + - If `nonLocal` option is **true**, and `hostname` was not given, it is + initialized with empty string, and later assigned to a library-selected + non-local IP address, at the first launch of the server. + - If `hostname` value is provided, the server will bind to the given address, + and it will ignore `nonLocal` option. + + _NOTE: In future we'll deprecate `nonLocal` option, and instead will use + special `hostname` values to ask the library to automatically select + appropriate non-local address._ + +- `nonLocal` — **boolean** — Optional. By default, if `hostname` + option was not provided, the server starts at the "`localhost`" address, + and it is only accessible within the host app. With this flag set **true** + the server will be started on an IP adress also accessible from outside the app. + + _NOTE: When `hostname` option is set to a value different from "`localhost", + the `nonLocal` option is ignored. The plan is to deprecate `nonLocal` option + in future, in favour of special `hostname` values supporting the current + `nonLocal` functionality._ - `port` — **number** — Optional. The port at which to start the server. If 0 (default) an available port will be automatically selected. diff --git a/src/index.tsx b/src/index.tsx index 5d2001c5..37cf64b2 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -202,7 +202,9 @@ class StaticServer { _configPath?: string; _fileDir: string; _hostname = ''; - _nonLocal: boolean; + + /* DEPRECATED */ _nonLocal: boolean; + _origin: string = ''; _stopInBackground: boolean; _port: number; @@ -239,6 +241,7 @@ class StaticServer { return this._hostname; } + /** @deprecated */ get nonLocal() { return this._nonLocal; } @@ -265,33 +268,28 @@ class StaticServer { } /** - * Creates a new StaticServer instance. - * - * Because the legacy, the following alternative signatures are supported, - * and the overall constructor implementation is thus subpar. - * - * new StaticServer(port, root, opts); - * new StaticServer(port, opts); - * new StaticServer(port, root); - * new StaticServer(opts); - * new StaticServer(port); - * new StaticServer(); - * - * @param {object} options + * Creates a new Server instance. */ constructor({ fileDir, - nonLocal = false, + hostname = 'localhost', + + /* DEPRECATED */ nonLocal = false, + port = 0, stopInBackground = false, }: { fileDir: string; - nonLocal?: boolean; + hostname?: string; + + /* DEPRECATED */ nonLocal?: boolean; + port?: number; stopInBackground?: boolean; }) { this._nonLocal = nonLocal; - if (!nonLocal) this._hostname = 'localhost'; + this._hostname = nonLocal && hostname === 'localhost' ? '' : hostname; + this._port = port; this._stopInBackground = stopInBackground;