import type { HookPoint } from "@umt/contracts"; import type { ServerContext } from "../modules/types.ts"; import type { HookContext, HookValue, InterceptPoint, RewritePoint } from "./points.ts"; import type { HookRejection } from "./registry.ts"; export type * from "./points.ts"; export { DEFAULT_HOOK_TIMEOUT_MS, type HookOptions, type HookRegistrar, HookRegistry, type HookRejection, } from "./registry.ts"; // 业务节点调用钩子的入口。ctx.hooks 由 bootstrap 装配;测试里手搭的上下文没有它,等同于没有注册。 /** 这个挂点有没有钩子(上下文构造代价大时先问一下) */ export function hasHook(ctx: Pick, point: HookPoint): boolean { return ctx.hooks?.has(point) ?? false; } /** 改写:没有钩子时原样返回同一个值;accept 判定单个钩子的结果能不能用(不能用的跳过) */ export async function rewriteHook

( ctx: Pick, point: P, value: HookValue

, context: HookContext

| (() => HookContext

), accept?: (v: HookValue

) => boolean, ): Promise> { const h = ctx.hooks; if (!h?.has(point)) return value; return h.runRewrite(point, value, typeof context === "function" ? context() : context, accept); } /** 拦截:返回第一个拒绝,没有钩子或都放行时返回 null */ export async function interceptHook

( ctx: Pick, point: P, context: HookContext

| (() => HookContext

), ): Promise { const h = ctx.hooks; if (!h?.has(point)) return null; return h.runIntercept(point, typeof context === "function" ? context() : context); }