Skip to content

Commit

Permalink
[#21] Adds "hostname" to Server constructor's options
Browse files Browse the repository at this point in the history
  • Loading branch information
birdofpreyru committed Mar 30, 2023
1 parent e223e33 commit ace176c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 15 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ class StaticServer {
_configPath?: string;
_fileDir: string;
_hostname = '';
_nonLocal: boolean;

/* DEPRECATED */ _nonLocal: boolean;

_origin: string = '';
_stopInBackground: boolean;
_port: number;
Expand Down Expand Up @@ -239,6 +241,7 @@ class StaticServer {
return this._hostname;
}

/** @deprecated */
get nonLocal() {
return this._nonLocal;
}
Expand All @@ -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;

Expand Down

0 comments on commit ace176c

Please sign in to comment.