mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 22:54:33 +00:00
perf: cache allowlist and account-id normalization
This commit is contained in:
@@ -16,6 +16,17 @@ export type AllowlistMatch<TSource extends string = AllowlistMatchSource> = {
|
||||
matchSource?: TSource;
|
||||
};
|
||||
|
||||
type CachedAllowListSet = {
|
||||
size: number;
|
||||
set: Set<string>;
|
||||
};
|
||||
|
||||
const ALLOWLIST_SET_CACHE = new WeakMap<string[], CachedAllowListSet>();
|
||||
const SIMPLE_ALLOWLIST_CACHE = new WeakMap<
|
||||
Array<string | number>,
|
||||
{ normalized: string[]; size: number; wildcard: boolean; set: Set<string> }
|
||||
>();
|
||||
|
||||
export function formatAllowlistMatchMeta(
|
||||
match?: { matchKey?: string; matchSource?: string } | null,
|
||||
): string {
|
||||
@@ -26,11 +37,12 @@ export function resolveAllowlistMatchByCandidates<TSource extends string>(params
|
||||
allowList: string[];
|
||||
candidates: Array<{ value?: string; source: TSource }>;
|
||||
}): AllowlistMatch<TSource> {
|
||||
const allowSet = resolveAllowListSet(params.allowList);
|
||||
for (const candidate of params.candidates) {
|
||||
if (!candidate.value) {
|
||||
continue;
|
||||
}
|
||||
if (params.allowList.includes(candidate.value)) {
|
||||
if (allowSet.has(candidate.value)) {
|
||||
return {
|
||||
allowed: true,
|
||||
matchKey: candidate.value,
|
||||
@@ -47,26 +59,57 @@ export function resolveAllowlistMatchSimple(params: {
|
||||
senderName?: string | null;
|
||||
allowNameMatching?: boolean;
|
||||
}): AllowlistMatch<"wildcard" | "id" | "name"> {
|
||||
const allowFrom = params.allowFrom
|
||||
.map((entry) => String(entry).trim().toLowerCase())
|
||||
.filter(Boolean);
|
||||
const allowFrom = resolveSimpleAllowFrom(params.allowFrom);
|
||||
|
||||
if (allowFrom.length === 0) {
|
||||
if (allowFrom.size === 0) {
|
||||
return { allowed: false };
|
||||
}
|
||||
if (allowFrom.includes("*")) {
|
||||
if (allowFrom.wildcard) {
|
||||
return { allowed: true, matchKey: "*", matchSource: "wildcard" };
|
||||
}
|
||||
|
||||
const senderId = params.senderId.toLowerCase();
|
||||
if (allowFrom.includes(senderId)) {
|
||||
if (allowFrom.set.has(senderId)) {
|
||||
return { allowed: true, matchKey: senderId, matchSource: "id" };
|
||||
}
|
||||
|
||||
const senderName = params.senderName?.toLowerCase();
|
||||
if (params.allowNameMatching === true && senderName && allowFrom.includes(senderName)) {
|
||||
if (params.allowNameMatching === true && senderName && allowFrom.set.has(senderName)) {
|
||||
return { allowed: true, matchKey: senderName, matchSource: "name" };
|
||||
}
|
||||
|
||||
return { allowed: false };
|
||||
}
|
||||
|
||||
function resolveAllowListSet(allowList: string[]): Set<string> {
|
||||
const cached = ALLOWLIST_SET_CACHE.get(allowList);
|
||||
if (cached && cached.size === allowList.length) {
|
||||
return cached.set;
|
||||
}
|
||||
const set = new Set(allowList);
|
||||
ALLOWLIST_SET_CACHE.set(allowList, { size: allowList.length, set });
|
||||
return set;
|
||||
}
|
||||
|
||||
function resolveSimpleAllowFrom(allowFrom: Array<string | number>): {
|
||||
normalized: string[];
|
||||
size: number;
|
||||
wildcard: boolean;
|
||||
set: Set<string>;
|
||||
} {
|
||||
const cached = SIMPLE_ALLOWLIST_CACHE.get(allowFrom);
|
||||
if (cached && cached.size === allowFrom.length) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const normalized = allowFrom.map((entry) => String(entry).trim().toLowerCase()).filter(Boolean);
|
||||
const set = new Set(normalized);
|
||||
const built = {
|
||||
normalized,
|
||||
size: allowFrom.length,
|
||||
wildcard: set.has("*"),
|
||||
set,
|
||||
};
|
||||
SIMPLE_ALLOWLIST_CACHE.set(allowFrom, built);
|
||||
return built;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user