feat(slack): add HTTP receiver webhook mode (#1143) - thanks @jdrhyne
Co-authored-by: Jonathan Rhyne <jdrhyne@users.noreply.github.com>
This commit is contained in:
1
src/slack/http/index.ts
Normal file
1
src/slack/http/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./registry.js";
|
||||
87
src/slack/http/registry.test.ts
Normal file
87
src/slack/http/registry.test.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
handleSlackHttpRequest,
|
||||
normalizeSlackWebhookPath,
|
||||
registerSlackHttpHandler,
|
||||
} from "./registry.js";
|
||||
|
||||
describe("normalizeSlackWebhookPath", () => {
|
||||
it("returns the default path when input is empty", () => {
|
||||
expect(normalizeSlackWebhookPath()).toBe("/slack/events");
|
||||
expect(normalizeSlackWebhookPath(" ")).toBe("/slack/events");
|
||||
});
|
||||
|
||||
it("ensures a leading slash", () => {
|
||||
expect(normalizeSlackWebhookPath("slack/events")).toBe("/slack/events");
|
||||
expect(normalizeSlackWebhookPath("/hooks/slack")).toBe("/hooks/slack");
|
||||
});
|
||||
});
|
||||
|
||||
describe("registerSlackHttpHandler", () => {
|
||||
const unregisters: Array<() => void> = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const unregister of unregisters.splice(0)) unregister();
|
||||
});
|
||||
|
||||
it("routes requests to a registered handler", async () => {
|
||||
const handler = vi.fn();
|
||||
unregisters.push(
|
||||
registerSlackHttpHandler({
|
||||
path: "/slack/events",
|
||||
handler,
|
||||
}),
|
||||
);
|
||||
|
||||
const req = { url: "/slack/events?foo=bar" } as IncomingMessage;
|
||||
const res = {} as ServerResponse;
|
||||
|
||||
const handled = await handleSlackHttpRequest(req, res);
|
||||
|
||||
expect(handled).toBe(true);
|
||||
expect(handler).toHaveBeenCalledWith(req, res);
|
||||
});
|
||||
|
||||
it("returns false when no handler matches", async () => {
|
||||
const req = { url: "/slack/other" } as IncomingMessage;
|
||||
const res = {} as ServerResponse;
|
||||
|
||||
const handled = await handleSlackHttpRequest(req, res);
|
||||
|
||||
expect(handled).toBe(false);
|
||||
});
|
||||
|
||||
it("logs and ignores duplicate registrations", async () => {
|
||||
const handler = vi.fn();
|
||||
const log = vi.fn();
|
||||
unregisters.push(
|
||||
registerSlackHttpHandler({
|
||||
path: "/slack/events",
|
||||
handler,
|
||||
log,
|
||||
accountId: "primary",
|
||||
}),
|
||||
);
|
||||
unregisters.push(
|
||||
registerSlackHttpHandler({
|
||||
path: "/slack/events",
|
||||
handler: vi.fn(),
|
||||
log,
|
||||
accountId: "duplicate",
|
||||
}),
|
||||
);
|
||||
|
||||
const req = { url: "/slack/events" } as IncomingMessage;
|
||||
const res = {} as ServerResponse;
|
||||
|
||||
const handled = await handleSlackHttpRequest(req, res);
|
||||
|
||||
expect(handled).toBe(true);
|
||||
expect(handler).toHaveBeenCalledWith(req, res);
|
||||
expect(log).toHaveBeenCalledWith(
|
||||
'slack: webhook path /slack/events already registered for account "duplicate"',
|
||||
);
|
||||
});
|
||||
});
|
||||
45
src/slack/http/registry.ts
Normal file
45
src/slack/http/registry.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
|
||||
export type SlackHttpRequestHandler = (
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
) => Promise<void> | void;
|
||||
|
||||
type RegisterSlackHttpHandlerArgs = {
|
||||
path?: string | null;
|
||||
handler: SlackHttpRequestHandler;
|
||||
log?: (message: string) => void;
|
||||
accountId?: string;
|
||||
};
|
||||
|
||||
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)) {
|
||||
const suffix = params.accountId ? ` for account "${params.accountId}"` : "";
|
||||
params.log?.(`slack: webhook path ${normalizedPath} already registered${suffix}`);
|
||||
return () => {};
|
||||
}
|
||||
slackHttpRoutes.set(normalizedPath, params.handler);
|
||||
return () => {
|
||||
slackHttpRoutes.delete(normalizedPath);
|
||||
};
|
||||
}
|
||||
|
||||
export async function handleSlackHttpRequest(
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
): Promise<boolean> {
|
||||
const url = new URL(req.url ?? "/", "http://localhost");
|
||||
const handler = slackHttpRoutes.get(url.pathname);
|
||||
if (!handler) return false;
|
||||
await handler(req, res);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user