mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 17:30:26 +00:00
feat: Add Line plugin (#1630)
* feat: add LINE plugin (#1630) (thanks @plum-dawg) * feat: complete LINE plugin (#1630) (thanks @plum-dawg) * chore: drop line plugin node_modules (#1630) (thanks @plum-dawg) * test: mock /context report in commands test (#1630) (thanks @plum-dawg) * test: limit macOS CI workers to avoid OOM (#1630) (thanks @plum-dawg) * test: reduce macOS CI vitest workers (#1630) (thanks @plum-dawg) --------- Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
53
src/plugins/http-registry.ts
Normal file
53
src/plugins/http-registry.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
|
||||
import type { PluginHttpRouteRegistration, PluginRegistry } from "./registry.js";
|
||||
import { requireActivePluginRegistry } from "./runtime.js";
|
||||
import { normalizePluginHttpPath } from "./http-path.js";
|
||||
|
||||
export type PluginHttpRouteHandler = (
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
) => Promise<void> | void;
|
||||
|
||||
export function registerPluginHttpRoute(params: {
|
||||
path?: string | null;
|
||||
fallbackPath?: string | null;
|
||||
handler: PluginHttpRouteHandler;
|
||||
pluginId?: string;
|
||||
source?: string;
|
||||
accountId?: string;
|
||||
log?: (message: string) => void;
|
||||
registry?: PluginRegistry;
|
||||
}): () => void {
|
||||
const registry = params.registry ?? requireActivePluginRegistry();
|
||||
const routes = registry.httpRoutes ?? [];
|
||||
registry.httpRoutes = routes;
|
||||
|
||||
const normalizedPath = normalizePluginHttpPath(params.path, params.fallbackPath);
|
||||
const suffix = params.accountId ? ` for account "${params.accountId}"` : "";
|
||||
if (!normalizedPath) {
|
||||
params.log?.(`plugin: webhook path missing${suffix}`);
|
||||
return () => {};
|
||||
}
|
||||
|
||||
if (routes.some((entry) => entry.path === normalizedPath)) {
|
||||
const pluginHint = params.pluginId ? ` (${params.pluginId})` : "";
|
||||
params.log?.(`plugin: webhook path ${normalizedPath} already registered${suffix}${pluginHint}`);
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const entry: PluginHttpRouteRegistration = {
|
||||
path: normalizedPath,
|
||||
handler: params.handler,
|
||||
pluginId: params.pluginId,
|
||||
source: params.source,
|
||||
};
|
||||
routes.push(entry);
|
||||
|
||||
return () => {
|
||||
const index = routes.indexOf(entry);
|
||||
if (index >= 0) {
|
||||
routes.splice(index, 1);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user