@@ -66,6 +66,7 @@ and [old][Old Architecture] RN architectures.
66
66
- [ Enabling Alias module]
67
67
- [ Enabling Rewrite module]
68
68
- [ Enabling WebDAV module]
69
+ - [ Connecting to an Active Server in the Native Layer]
69
70
- [ API Reference] ( #api-reference )
70
71
- [ Server] &mdash ; Represents a server instance.
71
72
- [ constructor()] &mdash ; Creates a new [ Server] instance.
@@ -89,7 +90,9 @@ and [old][Old Architecture] RN architectures.
89
90
- [ extractBundledAssets()] &mdash ; Extracts bundled assets into a regular folder
90
91
(Android-specific).
91
92
- [ getActiveServer()] &mdash ; Gets currently active, starting, or stopping
92
- server instance, if any.
93
+ server instance, if any, according to the TS layer data.
94
+ - [ getActiveServerId()] &mdash ; Gets ID of the currently active, starting, or
95
+ stopping server instance, if any, according to the Native layer data.
93
96
- [ resolveAssetsPath()] &mdash ; Resolves relative paths for bundled assets.
94
97
- [ ERROR_LOG_FILE] &mdash ; Location of the error log file.
95
98
- [ STATES] &mdash ; Enumerates possible states of [ Server] instance.
@@ -485,6 +488,50 @@ routes when you create [Server] instance, using `extraConfig` option.
485
488
` ,
486
489
` ` `
487
490
491
+ # ## Connecting to an Active Server in the Native Layer
492
+ [Connecting to an Active Server in the Native Layer]: # connecting-to-an-active-server-in-the-native-layer
493
+
494
+ When this library is used the regular way, the [Lighttpd] server in the native
495
+ layer is launched when the [.start()] method of a [Server] instance is triggered
496
+ on the JavaScript (TypeScript) side, and the native server is terminated when
497
+ the [.stop()] method is called on the JS side. In the JS layer we hold most of
498
+ the server-related information (` hostname` , ` port` , ` fileDir` , _etc._),
499
+ and take care of the high-level server control (_i.e._ the optional
500
+ pause / resume of the server when the app enters background / foreground).
501
+ If JS engine is restarted (or just related JS modules are reloaded) the regular
502
+ course of action is to explictly terminate the active server just before it,
503
+ and to re-create, and re-launch it afterwards. If it is not done, the [Lighttpd]
504
+ server will remain active in the native layer across the JS engine restart,
505
+ and it won' t be possible to launch a new server instance after the restart,
506
+ as the library only supports at most one active [Lighttpd] server, and it
507
+ throws an error if the server launch command arrives to the native layer while
508
+ [Lighttpd] server is already active.
509
+
510
+ However, in response to
511
+ [the ticket #95](https://github.com/birdofpreyru/react-native-static-server/issues/95)
512
+ we provide a way to reuse an active native server across JS engine restarts,
513
+ without restarting the server. To do so you:
514
+ - Use [getActiveServerId()] method to check whether the native server is active
515
+ (if so, this method resolves to a non-_null_ ID).
516
+ - Create a new [Server] instance passing into its [constructor()] that server ID
517
+ as the `id` option, and [STATES]`.ACTIVE` as the `state` option. These options
518
+ (usually omitted when creating a regular [Server] instance) ensure that
519
+ the created [Server] instance is able to communicate with the already running
520
+ native server, and to correctly handle subsequent [.stop()] and [.start()]
521
+ calls. Beside these, it is up-to-you to set all other options to the values
522
+ you need (_i.e._ setting `id`, and `state` just «connects»
523
+ the newly created [Server] instance to the active native server, but it
524
+ does not restore any other information about the server — you should
525
+ restore or redefine it the way you see fit).
526
+
527
+ Note, this way it is possible to create multiple [Server] instances connected
528
+ to the same active native server. As they have the same `id`, they all will
529
+ represent the same server, thus calling [.stop()] and [.start()] commands
530
+ on any of them will operate the same server, and update the states of all
531
+ these JS server instances, without triggering the error related to
532
+ the «at most one active server a time» (though, it has not been
533
+ carefully tested yet).
534
+
488
535
## API Reference
489
536
### Server
490
537
[Server]: #server
@@ -551,6 +598,12 @@ within `options` argument:
551
598
special ` hostname` values to ask the library to automatically select
552
599
appropriate non-local address._
553
600
601
+ - ` id` & mdash; ** number** & mdash; Optional. Allows to enforce a specific ID,
602
+ used to communicate with the server instance within the Native layer, thus
603
+ it allows to re-connect to an existing native server instance.
604
+ See & laquo; [Connecting to an Active Server in the Native Layer]& raquo;
605
+ for details. By default, an ` id` is selected by the library.
606
+
554
607
- ` nonLocal` & mdash; ** boolean** & mdash; Optional. By default, if ` hostname`
555
608
option was not provided, the server starts at the " ` 127.0.0.1` " (loopback)
556
609
address, and it is only accessible within the host app.
@@ -565,6 +618,15 @@ within `options` argument:
565
618
- ` port` & mdash; ** number** & mdash; Optional. The port at which to start the server.
566
619
If 0 (default) an available port will be automatically selected.
567
620
621
+ - ` state` & mdash; [STATES] & mdash; Optional. Allows to enforce the initial
622
+ server state value, which is necessary [when connecting to an existing
623
+ native server instance][Connecting to an Active Server in the Native Layer].
624
+ Note, it only influence the logic behind subsequent [.start()] and [.stop()]
625
+ calls, _i.e._ the constructor does not attempt to put the server in this
626
+ state, nor does it check the value is consistent with the active server,
627
+ if any, in the native layer. By default, the state is initialized
628
+ to ` STATES.INACTIVE` .
629
+
568
630
- ` stopInBackground` & mdash; ** boolean** & mdash; Optional.
569
631
570
632
By default, the server continues to work as usual when its host app enters
@@ -795,17 +857,43 @@ This is an Android-specific function; it does nothing on other platforms.
795
857
796
858
# ## getActiveServer()
797
859
[getActiveServer()]: # getactiveserver
798
- ```js
860
+ ` ` ` ts
799
861
import {getActiveServer} from ' @dr.pogodin/react-native-static-server' ;
800
862
801
- getActiveServer(): Server;
863
+ getActiveServer (): Server | undefined ;
802
864
` ` `
803
865
Returns currently active, starting, or stopping [Server] instance, if any exist
804
866
in the app. It does not return, however, any inactive server instance which has
805
867
been stopped automatically because of ` stopInBackground` option, when the app
806
868
entered background, and might be automatically started in future if the app
807
869
enters foreground again prior to an explicit [.stop()] call for that instance.
808
870
871
+ ** NOTE:** The result of this function is based on the TypeScript layer data
872
+ (that' s why it is synchronous), in contrast to the [getActiveServerId()]
873
+ function below, which calls into the Native layer, and returns ID of the active
874
+ server based on that.
875
+
876
+ ### getActiveServerId()
877
+ [getActiveServerId()]: #getactiveserverid
878
+ ```ts
879
+ import {getActiveServerId} from ' @dr.pogodin/react-native-static-server' ;
880
+
881
+ getActiveServerId(): Promise<number | null>;
882
+ ```
883
+ Returns ID of the currently active, starting, or stopping server instance,
884
+ if any exist in the Native layer.
885
+
886
+ This function is provided in response to
887
+ [the ticket #95](https://github.com/birdofpreyru/react-native-static-server/issues/95),
888
+ to allow «[Connecting to an Active Server in the Native Layer]».
889
+ The ID returned by this function can be passed in into [Server] instance
890
+ [constructor()] to create server instance communicating to the existing native
891
+ layer server.
892
+
893
+ **NOTE:** It is different from [getActiveServer()] function above, which
894
+ returns the acurrently active, starting, or stopping [Server] instance based on
895
+ TypeScript layer data.
896
+
809
897
### resolveAssetsPath()
810
898
[resolveAssetsPath()]: #resolveassetspath
811
899
```ts
0 commit comments