Skip to content

Commit 62e5a4e

Browse files
committed
Fix request consumption in server.js
Previously stream errors during `req` consumption would lead to a hanged connection and resource/memory leak. This also removes redundant double-promise from receiveBody. PR-URL: #174
1 parent 8ce90c9 commit 62e5a4e

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/server.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ const LONG_RESPONSE = 30000;
1212

1313
const clients = new Map();
1414

15-
const receiveBody = async req => new Promise(resolve => {
15+
const receiveBody = async req => {
1616
const buffers = [];
17-
req.on('data', chunk => {
17+
for await (const chunk of req) {
1818
buffers.push(chunk);
19-
}).on('end', () => {
20-
resolve(Buffer.concat(buffers).toString());
21-
});
22-
});
19+
}
20+
return Buffer.concat(buffers).toString();
21+
};
22+
2323

2424
const closeClients = () => {
2525
for (const [connection, client] of clients.entries()) {
@@ -56,6 +56,8 @@ const listener = (req, res) => {
5656
}
5757
receiveBody(req).then(data => {
5858
client.message(data);
59+
}, err => {
60+
client.error(500, err);
5961
});
6062
} else {
6163
if (url === '/' && !req.connection.encrypted) {

0 commit comments

Comments
 (0)