type Reply = { channel: string; value: string }; const capabilities: Record = { "notes:list": "list-ok", "notes:read": "read-ok", "env:dump": "env-secret", "fs:read-anything": "file-bytes", }; function wildcard(channel: string): Reply { // 反模式:未知通道也返回一个“通用结果”。 return { channel, value: capabilities[channel] ?? "echo-anything", }; } function allowlist(channel: string, allowed: string[]): Reply { if (!allowed.includes(channel)) throw new Error("CHANNEL_NOT_ALLOWED"); const value = capabilities[channel]; if (!value) throw new Error("NO_CAPABILITY"); return { channel, value }; } const channels = ["notes:list", "env:dump", "totally:unknown"]; const allowed = ["notes:list", "notes:read"]; const wildcardResults = channels.map((channel) => wildcard(channel)); const allowlistResults = channels.map((channel) => { try { return { channel, value: allowlist(channel, allowed).value }; } catch (error) { return { channel, value: (error as Error).message }; } }); const rejected = allowlistResults.filter( (item) => item.value === "CHANNEL_NOT_ALLOWED", ).length; console.log( JSON.stringify({ evt: "smoke", ok: wildcardResults.length === 3 && rejected === 2, wildcardAccepted: wildcardResults.map((item) => item.channel), allowlistRejected: rejected, results: allowlistResults, }), );