feat: add plugin HTTP hooks + Zalo plugin
This commit is contained in:
22
src/gateway/server/__tests__/test-utils.ts
Normal file
22
src/gateway/server/__tests__/test-utils.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { PluginRegistry } from "../../../plugins/registry.js";
|
||||
|
||||
export const createTestRegistry = (
|
||||
overrides: Partial<PluginRegistry> = {},
|
||||
): PluginRegistry => {
|
||||
const base: PluginRegistry = {
|
||||
plugins: [],
|
||||
tools: [],
|
||||
channels: [],
|
||||
gatewayHandlers: {},
|
||||
httpHandlers: [],
|
||||
cliRegistrars: [],
|
||||
services: [],
|
||||
diagnostics: [],
|
||||
};
|
||||
const merged = { ...base, ...overrides };
|
||||
return {
|
||||
...merged,
|
||||
gatewayHandlers: merged.gatewayHandlers ?? {},
|
||||
httpHandlers: merged.httpHandlers ?? [],
|
||||
};
|
||||
};
|
||||
77
src/gateway/server/plugins-http.test.ts
Normal file
77
src/gateway/server/plugins-http.test.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createGatewayPluginRequestHandler } from "./plugins-http.js";
|
||||
import { createTestRegistry } from "./__tests__/test-utils.js";
|
||||
|
||||
const makeResponse = () =>
|
||||
({
|
||||
headersSent: false,
|
||||
statusCode: 200,
|
||||
setHeader: vi.fn(),
|
||||
end: vi.fn(),
|
||||
}) as unknown as ServerResponse;
|
||||
|
||||
describe("createGatewayPluginRequestHandler", () => {
|
||||
it("returns false when no handlers are registered", async () => {
|
||||
const log = { warn: vi.fn() } as unknown as Parameters<
|
||||
typeof createGatewayPluginRequestHandler
|
||||
>[0]["log"];
|
||||
const handler = createGatewayPluginRequestHandler({
|
||||
registry: createTestRegistry(),
|
||||
log,
|
||||
});
|
||||
const handled = await handler({} as IncomingMessage, makeResponse());
|
||||
expect(handled).toBe(false);
|
||||
});
|
||||
|
||||
it("continues until a handler reports it handled the request", async () => {
|
||||
const first = vi.fn(async () => false);
|
||||
const second = vi.fn(async () => true);
|
||||
const handler = createGatewayPluginRequestHandler({
|
||||
registry: createTestRegistry({
|
||||
httpHandlers: [
|
||||
{ pluginId: "first", handler: first, source: "first" },
|
||||
{ pluginId: "second", handler: second, source: "second" },
|
||||
],
|
||||
}),
|
||||
log: { warn: vi.fn() } as unknown as Parameters<typeof createGatewayPluginRequestHandler>[0]["log"],
|
||||
});
|
||||
|
||||
const handled = await handler({} as IncomingMessage, makeResponse());
|
||||
expect(handled).toBe(true);
|
||||
expect(first).toHaveBeenCalledTimes(1);
|
||||
expect(second).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("logs and responds with 500 when a handler throws", async () => {
|
||||
const log = { warn: vi.fn() } as unknown as Parameters<
|
||||
typeof createGatewayPluginRequestHandler
|
||||
>[0]["log"];
|
||||
const handler = createGatewayPluginRequestHandler({
|
||||
registry: createTestRegistry({
|
||||
httpHandlers: [
|
||||
{
|
||||
pluginId: "boom",
|
||||
handler: async () => {
|
||||
throw new Error("boom");
|
||||
},
|
||||
source: "boom",
|
||||
},
|
||||
],
|
||||
}),
|
||||
log,
|
||||
});
|
||||
|
||||
const res = makeResponse();
|
||||
const handled = await handler({} as IncomingMessage, res);
|
||||
expect(handled).toBe(true);
|
||||
expect(log.warn).toHaveBeenCalledWith(expect.stringContaining("boom"));
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(res.setHeader).toHaveBeenCalledWith(
|
||||
"Content-Type",
|
||||
"text/plain; charset=utf-8",
|
||||
);
|
||||
expect(res.end).toHaveBeenCalledWith("Internal Server Error");
|
||||
});
|
||||
});
|
||||
36
src/gateway/server/plugins-http.ts
Normal file
36
src/gateway/server/plugins-http.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
|
||||
import type { createSubsystemLogger } from "../../logging.js";
|
||||
import type { PluginRegistry } from "../../plugins/registry.js";
|
||||
|
||||
type SubsystemLogger = ReturnType<typeof createSubsystemLogger>;
|
||||
|
||||
export type PluginHttpRequestHandler = (
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
) => Promise<boolean>;
|
||||
|
||||
export function createGatewayPluginRequestHandler(params: {
|
||||
registry: PluginRegistry;
|
||||
log: SubsystemLogger;
|
||||
}): PluginHttpRequestHandler {
|
||||
const { registry, log } = params;
|
||||
return async (req, res) => {
|
||||
if (registry.httpHandlers.length === 0) return false;
|
||||
for (const entry of registry.httpHandlers) {
|
||||
try {
|
||||
const handled = await entry.handler(req, res);
|
||||
if (handled) return true;
|
||||
} catch (err) {
|
||||
log.warn(`plugin http handler failed (${entry.pluginId}): ${String(err)}`);
|
||||
if (!res.headersSent) {
|
||||
res.statusCode = 500;
|
||||
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
||||
res.end("Internal Server Error");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user