Add persistent HTTP server to background service daemon
This commit is contained in:
1 parent
13ed555bbb
commit
f183cc8294
7 files changed
+237
-50
No files matched your search
+13
-22
@@ -393,28 +393,19 @@ export function extractCorpId(
|
||||
}
|
||||
|
||||
export async function parseJsonBody(req: IncomingMessage): Promise<Record<string, unknown>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let raw = "";
|
||||
req.on("data", (chunk) => {
|
||||
raw += chunk;
|
||||
if (raw.length > 20 * 1024 * 1024) {
|
||||
// 20MB limit
|
||||
reject(new Error("Payload too large"));
|
||||
}
|
||||
});
|
||||
req.on("end", () => {
|
||||
if (!raw.trim()) {
|
||||
resolve({});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
resolve(JSON.parse(raw));
|
||||
} catch (err) {
|
||||
reject(new Error(`Invalid JSON body: ${err instanceof Error ? err.message : String(err)}`));
|
||||
}
|
||||
});
|
||||
req.on("error", reject);
|
||||
});
|
||||
let raw = "";
|
||||
for await (const chunk of req) {
|
||||
raw += chunk;
|
||||
if (raw.length > 20 * 1024 * 1024) {
|
||||
throw new Error("Payload too large");
|
||||
}
|
||||
}
|
||||
if (!raw.trim()) return {};
|
||||
try {
|
||||
return JSON.parse(raw);
|
||||
} catch (err) {
|
||||
throw new Error(`Invalid JSON body: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function sendJson(res: ServerResponse, statusCode: number, data: unknown) {
|
||||
|
||||
+5
-2
@@ -8,9 +8,12 @@ export type HttpServerOptions = {
|
||||
silent?: boolean;
|
||||
};
|
||||
|
||||
export const DEFAULT_HTTP_PORT = 8787;
|
||||
export const DEFAULT_HTTP_HOST = "127.0.0.1";
|
||||
|
||||
export async function startHttpServer(options: HttpServerOptions = {}): Promise<Server> {
|
||||
const port = options.port ?? (process.env.PORT ? Number.parseInt(process.env.PORT, 10) : 8787);
|
||||
const host = options.host ?? (process.env.HOST ? process.env.HOST : "127.0.0.1");
|
||||
const port = options.port ?? (process.env.PORT ? Number.parseInt(process.env.PORT, 10) : DEFAULT_HTTP_PORT);
|
||||
const host = options.host ?? (process.env.HOST ? process.env.HOST : DEFAULT_HTTP_HOST);
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user