mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-21 05:32:53 +00:00
refactor(slack): lazy-load webhook handler
This commit is contained in:
1
extensions/slack/src/http/handler.runtime.ts
Normal file
1
extensions/slack/src/http/handler.runtime.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { handleSlackHttpRequest } from "./registry.js";
|
||||
7
extensions/slack/src/http/paths.ts
Normal file
7
extensions/slack/src/http/paths.ts
Normal 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}`;
|
||||
}
|
||||
@@ -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),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user