MCPcopy Index your code
hub / github.com/directus/directus / getCacheKey

Function getCacheKey

api/src/utils/get-cache-key.ts:16–58  ·  view source on GitHub ↗
(req: Request)

Source from the content-addressed store, hash-verified

14const FLOW_TRIGGER_PATTERN = /^\/flows\/trigger\/([0-9a-f-]+)/i;
15
16export async function getCacheKey(req: Request) {
17 const path = url.parse(req.originalUrl).pathname;
18 const isGraphQl = path?.startsWith('/graphql');
19
20 let flowTriggerQuery: Record<string, any> | undefined = undefined;
21
22 if (path) {
23 const flowMatch = path.match(FLOW_TRIGGER_PATTERN);
24
25 // Flow trigger endpoints accept arbitrary query params that affect their response,
26 // but these are not captured in sanitizedQuery.
27
28 if (flowMatch) {
29 const flow = getFlowManager().getFlow(flowMatch[1]!);
30 const cacheQueryParams: string[] = toArray(flow?.options?.['cacheQueryParams'] ?? []);
31 const picked = pick(req.query, cacheQueryParams);
32
33 if (!isEmpty(picked)) flowTriggerQuery = picked;
34 }
35 }
36
37 let includeIp = false;
38
39 if (req.accountability && req.accountability.ip) {
40 // Check if the IP influences the result of the request, that can be the case if some policies have an ip_access
41 // filter and the request IP matches any of those filters
42 const ipFilters = await fetchPoliciesIpAccess(req.accountability, getDatabase());
43 includeIp = ipFilters.length > 0 && ipFilters.some((networks) => ipInNetworks(req.accountability!.ip!, networks));
44 }
45
46 const info = {
47 version,
48 user: req.accountability?.user || null,
49 path,
50 query: isGraphQl ? getGraphqlQueryAndVariables(req) : req.sanitizedQuery,
51 ...(flowTriggerQuery && { rawQuery: flowTriggerQuery }),
52 ...(req.accountability?.share && { share: req.accountability.share }),
53 ...(includeIp && { ip: req.accountability!.ip }),
54 };
55
56 const key = hash(info);
57 return key;
58}

Callers 3

cache.tsFile · 0.85
respond.tsFile · 0.85

Calls 8

toArrayFunction · 0.90
ipInNetworksFunction · 0.90
getFlowManagerFunction · 0.85
isEmptyFunction · 0.85
getDatabaseFunction · 0.85
getFlowMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected