This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
344 lines (274 loc) · 10.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
const createCache = require('./cache');
const Router = require('koa-router');
const crypto = require('crypto');
const _ = require('lodash');
const pluralize = require('pluralize');
const semver = require('semver');
const PLUGIN_NAME = 'cache';
const cacheKeyPrefix = ({ model, id = null}) => {
return id ? `/${model}/${id}` : `/${model}`;
}
/**
* Generates a cache key for the current request
*
* @param {object} ctx
* @returns
*/
const generateCacheKey = (model, ctx, headers) => {
const { params, query = {} } = ctx;
const { id } = params;
const prefix = cacheKeyPrefix({ model, id })
const querySuffix = Object.keys(query)
.sort()
.map((k) => `${k}=${JSON.stringify(query[k])}`)
.join(",");
const headersSuffix = headers
.sort()
.filter((k) => ctx.request.header[k.toLowerCase()] !== undefined)
.map((k) => `${k}=${JSON.stringify(ctx.request.header[k.toLowerCase()])}`)
.join(",");
const suffix = `${querySuffix}${headersSuffix}`;
const cacheKey = `${prefix}?${suffix}`;
return { prefix, suffix, cacheKey };
};
/**
* Creates the caching middleware for strapi
*
* @param {Strapi} strapi
*/
const Cache = (strapi) => {
const options = _.get(strapi, `config.middleware.settings.${PLUGIN_NAME}`, {});
const type = _.get(options, 'type', 'mem');
const allowLogs = _.get(options, 'logs', true);
const withKoaContext = _.get(options, 'populateContext', false);
const withStrapiMiddleware = _.get(options, 'populateStrapiMiddleware', false);
const enableEtagSupport = _.get(options, 'enableEtagSupport', false);
const clearRelatedCache = _.get(options, 'clearRelatedCache', false);
const defaultModelOptions = { singleType: false, headers: [] };
const info = (msg) => allowLogs && strapi.log.debug(`[Cache] ${msg}`);
const toCache = _.chain(options)
.get('models', [])
.map(model => _.isString(model) ? { model } : model)
.map(cfg => ({ ...defaultModelOptions, ...cfg }))
.map(cfg => ({
...cfg,
model: cfg.singleType ? cfg.model : pluralize(cfg.model)
}))
.value();
return {
cache: null,
initialize() {
info('Mounting LRU cache middleware');
info(`Storage engine: ${type}`);
const router = new Router();
const cache = createCache(strapi, type, options);
this.cache = cache;
// --- Helpers
/**
*
* @param {string} model The model used to find related caches to bust
* @return {array} Array of related models
*/
const getRelatedModels = (model) => {
const models = Object.assign(strapi.models, strapi.plugins['users-permissions'].models);
const components = strapi.components;
const modelKey = _.findKey(models, { 'collectionName': model }) || model;
const modelObj = models[modelKey];
const attributes = _.get(modelObj, 'allAttributes', {});
const relatedComponents = [];
const relatedModels = {};
// first, look for direct relations
for (const key in attributes) {
const attr = attributes[key];
if (!attr.via) continue;
if (attr.collection) {
relatedModels[attr.collection] = models[attr.collection];
} else if (attr.model && attr.model !== 'file') {
relatedModels[attr.model] = models[attr.model];
}
}
// second, look for relations to current model in components
for (const compKey in components) {
const compObj = components[compKey];
const attributes = _.get(compObj, 'allAttributes', {});
// look for the current model
for (const key in attributes) {
const attr = attributes[key];
for (const key in attr) {
const field = attr[key];
if (key !== 'model') continue;
if (field === modelKey) {
relatedComponents.push(compKey);
}
}
}
// look one level deeper
for (const key in attributes) {
const attr = attributes[key];
for (const key in attr) {
const field = attr[key];
if (key !== 'component' && key !== 'components') continue;
if (!relatedModels.hasOwnProperty(modelKey) &&
(relatedComponents.includes(field) ||
(Array.isArray(field) && field.filter(x => relatedComponents.includes(x)).length))) {
relatedComponents.push(compKey);
}
}
}
}
// finally locate all the models that have the related components in their models
for (const modelKey in models) {
const modelObj = models[modelKey];
const attributes = _.get(modelObj, 'allAttributes', {});
for (const key in attributes) {
const attr = attributes[key];
for (const key in attr) {
const field = attr[key];
if (key !== 'component' && key !== 'components') continue;
if (!relatedModels.hasOwnProperty(modelKey) &&
(relatedComponents.includes(field) ||
(Array.isArray(field) && field.filter(x => relatedComponents.includes(x)).length))) {
relatedModels[modelKey] = models[modelKey];
}
}
}
}
return relatedModels;
}
/**
*
* @param {Object} params
* @param {string} params.model The model to bust the cache of
* @param {string} [params.id] The (optional) ID we want to bust the cache for
*/
const clearCache = async ({ model, id = null }) => {
const keys = await cache.keys() || [];
const rexps = [];
if (!id) {
rexps.push(new RegExp(`^/${model}`)); // Bust anything in relation to that model
} else {
rexps.push(new RegExp(`^/${model}/${id}`)); // Bust the record itself
rexps.push(new RegExp(`^/${model}\\?`)); // Bust the collections, but not other individual records
}
if (clearRelatedCache) {
const relatedModels = getRelatedModels(model);
// prep related models to have their whole cache busted
_.each(relatedModels, (relatedModel, key) => {
const relatedModelSlug = relatedModel.kind === 'collectionType'
? relatedModel.collectionName
: key;
rexps.push(new RegExp(`^/${relatedModelSlug}`)); // Bust all entries from models that have relations with that model
});
}
const shouldBust = key => _.find(rexps, r => r.test(key));
const bust = key => cache.del(key);
await Promise.all(
_.filter(keys, shouldBust).map(bust)
);
}
// --- Populate Koa Context with cache entry point
if (withKoaContext) {
strapi.app.use((ctx, next) => {
_.set(ctx, 'middleware.cache', {
bust: clearCache,
store: cache
})
return next();
});
}
// --- Populate Strapi Middleware with cache entry point
if (withStrapiMiddleware) {
_.set(strapi, 'middleware.cache', {
bust: clearCache,
store: cache
})
}
// --- Standard REST endpoints
/**
* Creates a Koa middleware that busts the cache of a given model
*
* @param {string} model
*/
const bust = (model) => async (ctx, next) => {
const { params } = ctx;
const { id } = params;
await next();
if (!_.inRange(ctx.status, 200, 300)) return;
await clearCache({ model, id });
}
_.each(toCache, (cacheConf) => {
const { model, maxAge = options.maxAge, headers = options.headers } = cacheConf;
const endpoint = `/${model}/:id*`;
info(`Caching route ${endpoint} [maxAge=${maxAge}]`);
router.delete(endpoint, bust(model));
router.post(endpoint, bust(model));
router.put(endpoint, bust(model));
router.get(endpoint, async (ctx, next) => {
const { cacheKey } = generateCacheKey(model, ctx, headers);
const cacheEntry = await cache.get(cacheKey);
if (enableEtagSupport) {
const ifNoneMatch = ctx.request.headers['if-none-match'];
const etagEntry = await cache.get(`${cacheKey}_etag`);
const etagMatch = ifNoneMatch === etagEntry;
if (!etagMatch) {
ctx.set('ETag', etagEntry)
} else {
ctx.status = 304;
return;
}
}
if (cacheEntry) {
ctx.status = 200;
ctx.body = cacheEntry;
return;
}
await next();
if (ctx.body && ctx.status == 200) {
await cache.set(cacheKey, ctx.body, maxAge);
if (enableEtagSupport) {
const etag = crypto.createHash('md5').update(JSON.stringify(ctx.body)).digest('hex');
ctx.set('ETag', `"${etag}"`)
await cache.set(`${cacheKey}_etag`, `"${etag}"`, maxAge);
}
}
});
});
// --- Admin REST endpoints
/**
* Busts the cache on receiving a modification from the admin panel
*
* @param {Koa.BaseContext} ctx
* @param {Function} next
*/
const bustAdmin = async (ctx, next) => {
const model = _.chain(ctx).get('params.scope').split('.').last().value();
const cacheCfg = _.find(toCache, (cfg) => {
return cfg.model === (cfg.singleType ? model : pluralize(model));
});
const isCached = !!cacheCfg;
if (isCached) {
await bust(cacheCfg.model)(ctx, next)
} else {
await next();
}
};
if (semver.lt(process.version, '3.4.0')) {
router.post('/content-manager/explorer/:scope', bustAdmin);
router.post('/content-manager/explorer/:scope/publish/:id*', bustAdmin);
router.post('/content-manager/explorer/:scope/unpublish/:id*', bustAdmin);
router.put('/content-manager/explorer/:scope/:id*', bustAdmin);
router.delete('/content-manager/explorer/:scope/:id*', bustAdmin);
} else {
['collection-types', 'single-types'].forEach(type => {
router.post(`/content-manager/${type}/:scope`, bustAdmin);
router.post(`/content-manager/${type}/:scope/publish/:id*`, bustAdmin);
router.post(`/content-manager/${type}/:scope/unpublish/:id*`, bustAdmin);
router.put(`/content-manager/${type}/:scope/:id*`, bustAdmin);
router.delete(`/content-manager/${type}/:scope/:id*`, bustAdmin);
});
}
strapi.app.use(router.routes());
},
};
};
module.exports = Cache;