Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ const METHOD_OFFSET = '/api/'.length;

const clients = new Map();

const receiveArgs = async req => new Promise(resolve => {
const body = [];
const receiveBody = async req => new Promise(resolve => {
const buffers = [];
req.on('data', chunk => {
body.push(chunk);
}).on('end', async () => {
const data = body.join('');
const args = JSON.parse(data);
resolve(args);
buffers.push(chunk);
}).on('end', () => {
resolve(Buffer.concat(buffers).toString());
});
});

Expand Down Expand Up @@ -58,8 +56,9 @@ const listener = (req, res) => {
client.error(403, new Error(`Forbidden: ${url}`));
return;
}
receiveArgs(req).then(args => {
receiveBody(req).then(body => {
const method = url.substring(METHOD_OFFSET);
const args = JSON.parse(body);
client.rpc(method, args);
});
} else {
Expand Down