Skip to content

Commit 8828440

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent 1144f08 commit 8828440

File tree

9 files changed

+150
-168
lines changed

9 files changed

+150
-168
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 16
1714
steps:
1815
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
2017
with:
2118
node-version: ${{ matrix.node-version }}
2219
- run: npm install

example.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'use strict';
2-
const phpServer = require('.');
1+
import phpServer from './index.js';
32

3+
// TODO: Remove the async wrapper when ESLint 8 is out.
44
(async () => {
55
const server = await phpServer();
66
console.log(`PHP server running at ${server.url}`);

index.d.ts

Lines changed: 84 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,114 @@
1-
/// <reference types="node"/>
1+
export interface Options {
2+
/**
3+
The port on which you want to access the webserver.
24
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.
76
8-
Specify `0` to use a random port.
7+
@default 0
8+
*/
9+
readonly port?: number;
910

10-
@default 0
11-
*/
12-
readonly port?: number;
11+
/**
12+
The hostname the server will use.
1313
14-
/**
15-
The hostname the server will use.
14+
Use `'0.0.0.0'` if you want it to be accessible from the outside.
1615
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;
1819

19-
@default '127.0.0.1'
20-
*/
21-
readonly hostname?: string;
20+
/**
21+
The directory the server will serve from.
2222
23-
/**
24-
The directory the server will serve from.
23+
@default '.'
24+
*/
25+
readonly base?: string;
2526

26-
@default '.'
27-
*/
28-
readonly base?: string;
27+
/**
28+
Open the server URL in the browser.
2929
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.
3233
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;
3637

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>;
4042

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.
4545
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:
4847
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;
6360

64-
/**
65-
Path to the PHP binary.
61+
/**
62+
The path to the PHP binary.
6663
67-
@default 'php'
68-
*/
69-
readonly binary?: string;
64+
@default 'php'
65+
*/
66+
readonly binary?: string;
7067

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).
7370
74-
Default: The built-in `php.ini`
75-
*/
76-
readonly ini?: string;
71+
Default: The built-in `php.ini`
72+
*/
73+
readonly ini?: string;
7774

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+
}
8380

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;
105101
}
106102

107103
/**
108104
Start a [PHP server](https://php.net/manual/en/features.commandline.webserver.php)
109105
110106
@example
111107
```
112-
import phpServer = require('php-server');
108+
import phpServer from 'php-server';
113109
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}`);
118112
```
119113
*/
120-
declare function phpServer(options?: phpServer.Options): phpServer.Server;
121-
122-
export = phpServer;
114+
export default function phpServer(options?: Options): Promise<Server>;

index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
'use strict';
2-
const path = require('path');
3-
const {spawn} = require('child_process');
4-
const http = require('http');
5-
const open = require('open');
6-
const binVersionCheck = require('bin-version-check');
7-
const getPort = require('get-port');
1+
import process from 'node:process';
2+
import path from 'node:path';
3+
import {spawn} from 'node:child_process';
4+
import http from 'node:http';
5+
import open from 'open';
6+
import binVersionCheck from 'bin-version-check';
7+
import getPort from 'get-port';
88

99
const isServerRunning = (hostname, port, pathname) => new Promise((resolve, reject) => {
1010
const retryDelay = 50;
@@ -18,9 +18,9 @@ const isServerRunning = (hostname, port, pathname) => new Promise((resolve, reje
1818
method: 'HEAD',
1919
hostname,
2020
port,
21-
path: pathname
21+
path: pathname,
2222
}, response => {
23-
const statusCodeType = Number(response.statusCode.toString()[0]);
23+
const statusCodeType = Number.parseInt(response.statusCode.toString()[0], 10);
2424
if ([2, 3, 4].includes(statusCodeType)) {
2525
resolve();
2626
return;
@@ -46,7 +46,7 @@ const isServerRunning = (hostname, port, pathname) => new Promise((resolve, reje
4646
checkServer();
4747
});
4848

49-
module.exports = async options => {
49+
export default async function phpServer(options) {
5050
options = {
5151
port: 0,
5252
hostname: '127.0.0.1',
@@ -55,7 +55,7 @@ module.exports = async options => {
5555
env: {},
5656
binary: 'php',
5757
directives: {},
58-
...options
58+
...options,
5959
};
6060

6161
if (options.port === 0) {
@@ -90,8 +90,8 @@ module.exports = async options => {
9090
const subprocess = spawn(options.binary, spawnArguments, {
9191
env: {
9292
...process.env,
93-
...options.env
94-
}
93+
...options.env,
94+
},
9595
});
9696

9797
subprocess.ref();
@@ -119,6 +119,6 @@ module.exports = async options => {
119119
url,
120120
stop() {
121121
subprocess.kill();
122-
}
122+
},
123123
};
124-
};
124+
}

index.test-d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import {expectType} from 'tsd';
2-
import phpServer = require('.');
2+
import phpServer, {Server} from './index.js';
33

4-
expectType<phpServer.Server>(phpServer());
4+
expectType<Promise<Server>>(phpServer());
55

6-
expectType<phpServer.Server>(phpServer({
6+
expectType<Promise<Server>>(phpServer({
77
port: 8000,
88
hostname: '0.0.0.0',
99
base: 'foo',
1010
open: true,
1111
env: {
12-
FOOBAR: 'foobar'
12+
FOOBAR: 'foobar', // eslint-disable-line @typescript-eslint/naming-convention
1313
},
1414
router: 'index.php',
1515
binary: 'php5',
1616
ini: 'php.ini',
1717
directives: {
18-
error_log: 'foobar' // eslint-disable-line camelcase
18+
error_log: 'foobar', // eslint-disable-line @typescript-eslint/naming-convention
1919
},
2020
}));

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

package.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Start a PHP server",
55
"license": "MIT",
66
"repository": "sindresorhus/php-server",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11+
"url": "http://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd"
@@ -29,15 +32,15 @@
2932
"development"
3033
],
3134
"dependencies": {
32-
"bin-version-check": "^4.0.0",
33-
"get-port": "^5.0.0",
34-
"open": "^6.3.0"
35+
"bin-version-check": "^5.0.0",
36+
"get-port": "^6.0.0",
37+
"open": "^8.2.1"
3538
},
3639
"devDependencies": {
37-
"@types/node": "^12.0.1",
38-
"ava": "^1.4.1",
39-
"got": "^9.6.0",
40-
"tsd": "^0.7.3",
41-
"xo": "^0.24.0"
40+
"@types/node": "^16.10.2",
41+
"ava": "^3.15.0",
42+
"got": "^11.8.2",
43+
"tsd": "^0.17.0",
44+
"xo": "^0.45.0"
4245
}
4346
}

0 commit comments

Comments
 (0)