mirror of
https://github.com/zhom/donutbrowser.git
synced 2026-12-05 10:03:44 +01:00
43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
import http from "node:http";
|
|
|
|
/** Stands in for the cloud feedback route and keeps every request it gets. */
|
|
export async function feedbackCaptureServer() {
|
|
const received = [];
|
|
let status = 201;
|
|
const server = http.createServer((request, response) => {
|
|
let body = "";
|
|
request.setEncoding("utf8");
|
|
request.on("data", (chunk) => {
|
|
body += chunk;
|
|
});
|
|
request.on("end", () => {
|
|
received.push({
|
|
method: request.method,
|
|
url: request.url,
|
|
headers: request.headers,
|
|
body: body ? JSON.parse(body) : null,
|
|
});
|
|
response.writeHead(status, { "content-type": "application/json" });
|
|
response.end(
|
|
JSON.stringify(
|
|
status < 300
|
|
? {
|
|
id: `fb-${received.length}`,
|
|
createdAt: new Date().toISOString(),
|
|
}
|
|
: { statusCode: status, message: "Too Many Requests" },
|
|
),
|
|
);
|
|
});
|
|
});
|
|
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
return {
|
|
received,
|
|
url: `http://127.0.0.1:${server.address().port}/api/feedback`,
|
|
respondWith(next) {
|
|
status = next;
|
|
},
|
|
close: () => new Promise((resolve) => server.close(resolve)),
|
|
};
|
|
}
|