|
1 | | -/// <reference types="node"/> |
| 1 | +export interface Options { |
| 2 | + /** |
| 3 | + The port on which you want to access the webserver. |
2 | 4 |
|
3 | | -declare namespace phpServer { |
4 | | - interface Options { |
5 | | - /** |
6 | | - The port on which you want to access the webserver. |
| 5 | + Specify `0` to use a random port. |
7 | 6 |
|
8 | | - Specify `0` to use a random port. |
| 7 | + @default 0 |
| 8 | + */ |
| 9 | + readonly port?: number; |
9 | 10 |
|
10 | | - @default 0 |
11 | | - */ |
12 | | - readonly port?: number; |
| 11 | + /** |
| 12 | + The hostname the server will use. |
13 | 13 |
|
14 | | - /** |
15 | | - The hostname the server will use. |
| 14 | + Use `'0.0.0.0'` if you want it to be accessible from the outside. |
16 | 15 |
|
17 | | - Use `'0.0.0.0'` if you want it to be accessible from the outside. |
| 16 | + @default '127.0.0.1' |
| 17 | + */ |
| 18 | + readonly hostname?: string; |
18 | 19 |
|
19 | | - @default '127.0.0.1' |
20 | | - */ |
21 | | - readonly hostname?: string; |
| 20 | + /** |
| 21 | + The directory the server will serve from. |
22 | 22 |
|
23 | | - /** |
24 | | - The directory the server will serve from. |
| 23 | + @default '.' |
| 24 | + */ |
| 25 | + readonly base?: string; |
25 | 26 |
|
26 | | - @default '.' |
27 | | - */ |
28 | | - readonly base?: string; |
| 27 | + /** |
| 28 | + Open the server URL in the browser. |
29 | 29 |
|
30 | | - /** |
31 | | - Open the server URL in the browser. |
| 30 | + Can be one of the following: |
| 31 | + - `true`: Opens the default server URL (`http://${hostname}${port}`). |
| 32 | + - A relative URL: Opens that URL in the browser. Useful when testing pages that are not the default. |
32 | 33 |
|
33 | | - Can be one of the following: |
34 | | - - `true`: Opens the default server URL (`http://${hostname}${port}`). |
35 | | - - A relative URL: Opens that URL in the browser. Useful when testing pages that are not the default. |
| 34 | + @default false |
| 35 | + */ |
| 36 | + readonly open?: boolean | string; |
36 | 37 |
|
37 | | - @default false |
38 | | - */ |
39 | | - readonly open?: boolean | string; |
| 38 | + /** |
| 39 | + Set environment variables for the PHP process. |
| 40 | + */ |
| 41 | + readonly env?: Record<string, string>; |
40 | 42 |
|
41 | | - /** |
42 | | - Set environment variables for the PHP process. |
43 | | - */ |
44 | | - readonly env?: {[key: string]: string}; |
| 43 | + /** |
| 44 | + Optionally specify the path to a [router script](https://php.net/manual/en/features.commandline.webserver.php#example-412) that is run at the start of each HTTP request. If this script returns `false`, the requested resource is returned as-is. Otherwise, the script's output is returned to the browser. |
45 | 45 |
|
46 | | - /** |
47 | | - Optionally specify the path to a [router script](https://php.net/manual/en/features.commandline.webserver.php#example-412) that is run at the start of each HTTP request. If this script returns `false`, the requested resource is returned as-is. Otherwise, the script's output is returned to the browser. |
| 46 | + Example router script: |
48 | 47 |
|
49 | | - Example router script: |
50 | | -
|
51 | | - ```php |
52 | | - <?php |
53 | | - // router.php |
54 | | - if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) { |
55 | | - return false; // Serve the requested resource as-is |
56 | | - } else { |
57 | | - echo "<p>Thanks for using php-server :)</p>"; |
58 | | - } |
59 | | - ?> |
60 | | - ``` |
61 | | - */ |
62 | | - readonly router?: string; |
| 48 | + ```php |
| 49 | + <?php |
| 50 | + // router.php |
| 51 | + if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) { |
| 52 | + return false; // Serve the requested resource as-is |
| 53 | + } else { |
| 54 | + echo "<p>Thanks for using php-server :)</p>"; |
| 55 | + } |
| 56 | + ?> |
| 57 | + ``` |
| 58 | + */ |
| 59 | + readonly router?: string; |
63 | 60 |
|
64 | | - /** |
65 | | - Path to the PHP binary. |
| 61 | + /** |
| 62 | + The path to the PHP binary. |
66 | 63 |
|
67 | | - @default 'php' |
68 | | - */ |
69 | | - readonly binary?: string; |
| 64 | + @default 'php' |
| 65 | + */ |
| 66 | + readonly binary?: string; |
70 | 67 |
|
71 | | - /** |
72 | | - Path to a custom [`php.ini` config file](https://php.net/manual/en/ini.php). |
| 68 | + /** |
| 69 | + A path to a custom [`php.ini` config file](https://php.net/manual/en/ini.php). |
73 | 70 |
|
74 | | - Default: The built-in `php.ini` |
75 | | - */ |
76 | | - readonly ini?: string; |
| 71 | + Default: The built-in `php.ini` |
| 72 | + */ |
| 73 | + readonly ini?: string; |
77 | 74 |
|
78 | | - /** |
79 | | - Add custom [INI directives](https://php.net/manual/en/ini.list.php). |
80 | | - */ |
81 | | - readonly directives?: {[key: string]: string}; |
82 | | - } |
| 75 | + /** |
| 76 | + Add custom [INI directives](https://php.net/manual/en/ini.list.php). |
| 77 | + */ |
| 78 | + readonly directives?: Record<string, string>; |
| 79 | +} |
83 | 80 |
|
84 | | - interface Server { |
85 | | - /** |
86 | | - The [`subprocess.stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout). |
87 | | - */ |
88 | | - readonly stdout: NodeJS.WritableStream; |
89 | | - |
90 | | - /** |
91 | | - The [`subprocess.stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr). |
92 | | - */ |
93 | | - readonly stderr: NodeJS.WritableStream; |
94 | | - |
95 | | - /** |
96 | | - URL to the server. |
97 | | - */ |
98 | | - readonly url: string; |
99 | | - |
100 | | - /** |
101 | | - Stop the server. |
102 | | - */ |
103 | | - stop(): void; |
104 | | - } |
| 81 | +export interface Server { |
| 82 | + /** |
| 83 | + The [`subprocess.stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout). |
| 84 | + */ |
| 85 | + readonly stdout: NodeJS.WritableStream; |
| 86 | + |
| 87 | + /** |
| 88 | + The [`subprocess.stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr). |
| 89 | + */ |
| 90 | + readonly stderr: NodeJS.WritableStream; |
| 91 | + |
| 92 | + /** |
| 93 | + The URL to the server. |
| 94 | + */ |
| 95 | + readonly url: string; |
| 96 | + |
| 97 | + /** |
| 98 | + A method, which when called, stops the server. |
| 99 | + */ |
| 100 | + stop(): void; |
105 | 101 | } |
106 | 102 |
|
107 | 103 | /** |
108 | 104 | Start a [PHP server](https://php.net/manual/en/features.commandline.webserver.php) |
109 | 105 |
|
110 | 106 | @example |
111 | 107 | ``` |
112 | | -import phpServer = require('php-server'); |
| 108 | +import phpServer from 'php-server'; |
113 | 109 |
|
114 | | -(async () => { |
115 | | - const server = await phpServer(); |
116 | | - console.log(`PHP server running at ${server.url}`) |
117 | | -})(); |
| 110 | +const server = await phpServer(); |
| 111 | +console.log(`PHP server running at ${server.url}`); |
118 | 112 | ``` |
119 | 113 | */ |
120 | | -declare function phpServer(options?: phpServer.Options): phpServer.Server; |
121 | | - |
122 | | -export = phpServer; |
| 114 | +export default function phpServer(options?: Options): Promise<Server>; |
0 commit comments