Skip to content

Commit 828c33c

Browse files
committed
Refactor buffer concatenation
PR-URL: #159
1 parent c9c6406 commit 828c33c

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

lib/server.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ const METHOD_OFFSET = '/api/'.length;
1313

1414
const clients = new Map();
1515

16-
const receiveArgs = async req => new Promise(resolve => {
17-
const body = [];
16+
const receiveBody = async req => new Promise(resolve => {
17+
const buffers = [];
1818
req.on('data', chunk => {
19-
body.push(chunk);
20-
}).on('end', async () => {
21-
const data = body.join('');
22-
const args = JSON.parse(data);
23-
resolve(args);
19+
buffers.push(chunk);
20+
}).on('end', () => {
21+
resolve(Buffer.concat(buffers).toString());
2422
});
2523
});
2624

@@ -58,8 +56,9 @@ const listener = (req, res) => {
5856
client.error(403, new Error(`Forbidden: ${url}`));
5957
return;
6058
}
61-
receiveArgs(req).then(args => {
59+
receiveBody(req).then(body => {
6260
const method = url.substring(METHOD_OFFSET);
61+
const args = JSON.parse(body);
6362
client.rpc(method, args);
6463
});
6564
} else {

0 commit comments

Comments
 (0)