mirror of
https://github.com/sergeyyarkov/asu_tools_service.git
synced 2026-10-11 18:39:15 +03:00
30 lines
677 B
JavaScript
30 lines
677 B
JavaScript
/**
|
|
* @param {import('node:http').IncomingMessage} req
|
|
* @returns {HTTPRequest}
|
|
*/
|
|
export function createRequest(req) {
|
|
return {
|
|
get req() {
|
|
return req;
|
|
},
|
|
|
|
async parseJson() {
|
|
return new Promise((resolve, reject) => {
|
|
/** @type {Buffer[]} */
|
|
const body = [];
|
|
|
|
this.req.on("data", (chunk) => body.push(chunk));
|
|
this.req.on("end", () => {
|
|
try {
|
|
const parsedJson = JSON.parse(Buffer.concat(body).toString());
|
|
resolve(parsedJson);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
this.req.on("error", (err) => reject(err));
|
|
});
|
|
}
|
|
};
|
|
}
|