Skip to content

Commit f95a9e1

Browse files
committed
Fix server hanging
Fixes #6
1 parent 1c5c853 commit f95a9e1

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ export type Options = {
8080

8181
export type Server = {
8282
/**
83-
The [`subprocess.stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout).
83+
The [`subprocess.stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout).
8484
*/
85-
readonly stdout: NodeJS.WritableStream;
85+
readonly stdout: NodeJS.ReadableStream;
8686

8787
/**
8888
The [`subprocess.stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).
8989
*/
90-
readonly stderr: NodeJS.WritableStream;
90+
readonly stderr: NodeJS.ReadableStream;
9191

9292
/**
9393
The URL to the server.

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ export default async function phpServer(options) {
112112
},
113113
});
114114

115+
// Drain stdout/stderr to prevent the child process from blocking if nothing reads them.
116+
// Users can still attach listeners on these streams as needed.
117+
// See https://github.com/sindresorhus/php-server/issues/6
118+
if (subprocess.stdout) {
119+
subprocess.stdout.resume();
120+
}
121+
122+
if (subprocess.stderr) {
123+
subprocess.stderr.resume();
124+
}
125+
115126
subprocess.ref();
116127

117128
process.on('exit', () => {

test/stdio-flowing.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {test} from 'node:test';
2+
import assert from 'node:assert/strict';
3+
import phpServer from '../index.js';
4+
5+
test('stdout/stderr are flowing to avoid blocking', async () => {
6+
const server = await phpServer({base: 'test/fixtures/200'});
7+
try {
8+
assert.equal(server.stdout?.readableFlowing, true);
9+
assert.equal(server.stderr?.readableFlowing, true);
10+
} finally {
11+
server.stop();
12+
}
13+
});

0 commit comments

Comments
 (0)