Spaces:
Running
Running
File size: 1,445 Bytes
fb4d8fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import type { IncomingMessage, ServerResponse } from "node:http";
export type LineHttpRequestHandler = (
req: IncomingMessage,
res: ServerResponse,
) => Promise<void> | void;
type RegisterLineHttpHandlerArgs = {
path?: string | null;
handler: LineHttpRequestHandler;
log?: (message: string) => void;
accountId?: string;
};
const lineHttpRoutes = new Map<string, LineHttpRequestHandler>();
export function normalizeLineWebhookPath(path?: string | null): string {
const trimmed = path?.trim();
if (!trimmed) {
return "/line/webhook";
}
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
}
export function registerLineHttpHandler(params: RegisterLineHttpHandlerArgs): () => void {
const normalizedPath = normalizeLineWebhookPath(params.path);
if (lineHttpRoutes.has(normalizedPath)) {
const suffix = params.accountId ? ` for account "${params.accountId}"` : "";
params.log?.(`line: webhook path ${normalizedPath} already registered${suffix}`);
return () => {};
}
lineHttpRoutes.set(normalizedPath, params.handler);
return () => {
lineHttpRoutes.delete(normalizedPath);
};
}
export async function handleLineHttpRequest(
req: IncomingMessage,
res: ServerResponse,
): Promise<boolean> {
const url = new URL(req.url ?? "/", "http://localhost");
const handler = lineHttpRoutes.get(url.pathname);
if (!handler) {
return false;
}
await handler(req, res);
return true;
}
|