refactor(slack): lazy-load webhook handler

This commit is contained in:
Vincent Koc
2026-04-04 02:25:34 +09:00
parent 7ad72281f7
commit 230c61885d
4 changed files with 21 additions and 10 deletions

View File

@@ -0,0 +1 @@
export { handleSlackHttpRequest } from "./registry.js";

View File

@@ -0,0 +1,7 @@
export function normalizeSlackWebhookPath(path?: string | null): string {
const trimmed = path?.trim();
if (!trimmed) {
return "/slack/events";
}
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
}

View File

@@ -1,7 +1,14 @@
import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/account-id";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/core";
import { listSlackAccountIds, resolveSlackAccount } from "../accounts.js";
import { handleSlackHttpRequest, normalizeSlackWebhookPath } from "./registry.js";
import { normalizeSlackWebhookPath } from "./paths.js";
let slackHttpHandlerRuntimePromise: Promise<typeof import("./handler.runtime.js")> | null = null;
async function loadSlackHttpHandlerRuntime() {
slackHttpHandlerRuntimePromise ??= import("./handler.runtime.js");
return await slackHttpHandlerRuntimePromise;
}
export function registerSlackPluginHttpRoutes(api: OpenClawPluginApi): void {
const accountIds = new Set<string>([DEFAULT_ACCOUNT_ID, ...listSlackAccountIds(api.config)]);
@@ -17,7 +24,8 @@ export function registerSlackPluginHttpRoutes(api: OpenClawPluginApi): void {
api.registerHttpRoute({
path,
auth: "plugin",
handler: async (req, res) => await handleSlackHttpRequest(req, res),
handler: async (req, res) =>
await (await loadSlackHttpHandlerRuntime()).handleSlackHttpRequest(req, res),
});
}
}

View File

@@ -1,4 +1,7 @@
import type { IncomingMessage, ServerResponse } from "node:http";
import { normalizeSlackWebhookPath } from "./paths.js";
export { normalizeSlackWebhookPath } from "./paths.js";
export type SlackHttpRequestHandler = (
req: IncomingMessage,
@@ -14,14 +17,6 @@ type RegisterSlackHttpHandlerArgs = {
const slackHttpRoutes = new Map<string, SlackHttpRequestHandler>();
export function normalizeSlackWebhookPath(path?: string | null): string {
const trimmed = path?.trim();
if (!trimmed) {
return "/slack/events";
}
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
}
export function registerSlackHttpHandler(params: RegisterSlackHttpHandlerArgs): () => void {
const normalizedPath = normalizeSlackWebhookPath(params.path);
if (slackHttpRoutes.has(normalizedPath)) {