Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Commit 51d083f

Browse files
stafyniaksachapatrixr
authored andcommitted
dx: split functions in separate files
1 parent 44b890b commit 51d083f

32 files changed

+1298
-1054
lines changed

Diff for: .eslintrc.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
module.exports = {
22
parser: '@typescript-eslint/parser',
3-
extends: [
4-
'eslint:recommended',
5-
'google',
6-
'prettier',
7-
],
3+
extends: ['eslint:recommended', 'google', 'prettier'],
84
env: {
95
commonjs: true,
106
es6: true,
@@ -25,10 +21,8 @@ module.exports = {
2521
},
2622
rules: {
2723
indent: ['error', 2, { SwitchCase: 1 }],
28-
'no-console':
29-
process.env.NODE_ENV === 'production' ? 'error' : 'off',
30-
'no-debugger':
31-
process.env.NODE_ENV === 'production' ? 'error' : 'off',
24+
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
25+
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
3226
'object-curly-spacing': 'off',
3327
'valid-jsdoc': 'off',
3428
'require-jsdoc': 'off',

Diff for: .github/workflows/npm_publish.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: supercharge/[email protected]
2121
with:
2222
redis-version: ${{ matrix.redis-version }}
23-
- run: npm install
23+
- run: npm install
2424
- run: npm test
2525

2626
publish-npm:

Diff for: .github/workflows/npm_publish_beta.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: supercharge/[email protected]
2121
with:
2222
redis-version: ${{ matrix.redis-version }}
23-
- run: npm install
23+
- run: npm install
2424
- run: npm test
2525

2626
publish-npm:

Diff for: .github/workflows/test_middleware.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ jobs:
2020
uses: supercharge/[email protected]
2121
with:
2222
redis-version: ${{ matrix.redis-version }}
23-
- run: npm install
23+
- run: npm install
2424
- run: npm test

Diff for: .prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"trailingComma": "es5",
3+
"singleQuote": true
4+
}

Diff for: .vscode/launch.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
"version": "0.2.0",
66
"configurations": [
77
{
8-
"name": "Run mocha",
9-
"type": "node",
10-
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
11-
"stopOnEntry": false,
12-
"args": ["./tests/*.test.js", "--no-timeouts", "--exit"],
13-
"cwd": "${workspaceFolder}",
14-
"runtimeExecutable": null,
15-
"request": "launch"
8+
"name": "Run mocha",
9+
"type": "node",
10+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
11+
"stopOnEntry": false,
12+
"args": ["./tests/*.test.js", "--no-timeouts", "--exit"],
13+
"cwd": "${workspaceFolder}",
14+
"runtimeExecutable": null,
15+
"request": "launch"
1616
}
1717
]
18-
}
18+
}

Diff for: README.md

+42-41
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ yarn add strapi-middleware-cache@beta
3636
## Requirements
3737

3838
Since `2.0.1`:
39-
- strapi `3.4.0`
39+
40+
- strapi `3.4.0`
4041
- node `14`
4142

4243
_See `1.5.0` for strapi < `3.4.0`_
@@ -62,8 +63,8 @@ module.exports = ({ env }) => ({
6263
settings: {
6364
cache: {
6465
enabled: true,
65-
}
66-
}
66+
},
67+
},
6768
});
6869
```
6970

@@ -89,8 +90,8 @@ module.exports = ({ env }) => ({
8990
cache: {
9091
enabled: true,
9192
models: ['review'],
92-
}
93-
}
93+
},
94+
},
9495
});
9596
```
9697

@@ -149,9 +150,9 @@ module.exports = ({ env }) => ({
149150
{ host: '192.168.10.43', port: 26379 },
150151
],
151152
name: 'redis-primary',
152-
}
153-
}
154-
}
153+
},
154+
},
155+
},
155156
});
156157
```
157158

@@ -176,6 +177,7 @@ Each route can hold its own configuration object for more granular control. This
176177
by replacing the model strings in the list by object.
177178

178179
On which you can set:
180+
179181
- Its own custom `maxAge`
180182
- Headers to include in the cache-key (e.g the body may differ depending on the language requested)
181183

@@ -255,10 +257,10 @@ module.exports = ({ env }) => ({
255257
{
256258
model: 'footer',
257259
singleType: true,
258-
}
259-
]
260-
}
261-
}
260+
},
261+
],
262+
},
263+
},
262264
});
263265
```
264266

@@ -274,7 +276,6 @@ $ strapi develop
274276
[2021-02-26T07:03:18.987Z] debug [cache] GET /review recv maxAge=3600000
275277
```
276278

277-
278279
## Authentication
279280

280281
By default, cache is not looked up if `Authorization` or `Cookie` header are present.
@@ -295,10 +296,10 @@ module.exports = ({ env }) => ({
295296
model: 'footer',
296297
hitpass: false,
297298
singleType: true,
298-
}
299-
]
300-
}
301-
}
299+
},
300+
],
301+
},
302+
},
302303
});
303304
```
304305

@@ -325,17 +326,17 @@ module.exports = ({ env }) => ({
325326
cache: {
326327
enabled: true,
327328
populateContext: true,
328-
models: ['post']
329-
}
330-
}
329+
models: ['post'],
330+
},
331+
},
331332
});
332333

333334
// controller
334335

335336
module.exports = {
336337
async index(ctx) {
337-
ctx.middleware.cache // A direct access to the Cache API
338-
}
338+
ctx.middleware.cache; // A direct access to the Cache API
339+
},
339340
};
340341
```
341342

@@ -352,37 +353,37 @@ module.exports = ({ env }) => ({
352353
cache: {
353354
enabled: true,
354355
populateStrapiMiddleware: true,
355-
models: ['post']
356-
}
357-
}
356+
models: ['post'],
357+
},
358+
},
358359
});
359360

360361
// model
361362

362363
module.exports = {
363364
lifecycles: {
364365
async beforeUpdate(params, data) {
365-
strapi.middleware.cache // A direct access to the Cache API
366-
}
367-
}
366+
strapi.middleware.cache; // A direct access to the Cache API
367+
},
368+
},
368369
};
369370
```
370371

371372
**IMPORTANT**: We do not recommend using this unless truly necessary. It is disabled by default as it goes against the non-intrusive/transparent nature of this middleware.
372373

373374
## Cache API
374375

375-
```javascript
376+
```javascript
376377
const cache = {
377-
/**
378-
* @typedef {Object} CacheStore
379-
* @property {function(string): any} get
380-
* @property {function(string): any} peek
381-
* @property {function(string, any, number?): boolean} set
382-
* @property {function(string): void} del
383-
* @property {function(): any[]} keys
384-
* @property {function(): void} reset
385-
*/
378+
/**
379+
* @typedef {Object} CacheStore
380+
* @property {function(string): any} get
381+
* @property {function(string): any} peek
382+
* @property {function(string, any, number?): boolean} set
383+
* @property {function(string): void} del
384+
* @property {function(): any[]} keys
385+
* @property {function(): void} reset
386+
*/
386387
store,
387388

388389
/**
@@ -408,7 +409,7 @@ const cache = {
408409

409410
/**
410411
* Clear cache with uri parameters
411-
*
412+
*
412413
* @param {ModelCacheConfig} cacheConf
413414
* @param {{ [key: string]: string; }=} params
414415
*/
@@ -425,7 +426,7 @@ const cache = {
425426

426427
/**
427428
* Get related ModelCacheConfig with an uid
428-
*
429+
*
429430
* uid:
430431
* - application::sport.sport
431432
* - plugins::users-permissions.user
@@ -445,7 +446,7 @@ const cache = {
445446

446447
/**
447448
* Get regexs to match all ModelCacheConfig keys with given params
448-
*
449+
*
449450
* @param {ModelCacheConfig} cacheConf
450451
* @param {{ [key: string]: string; }=} params
451452
* @param {boolean=} wildcard

Diff for: lib/cache/index.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22

3-
const mapValues = require("lodash/mapValues");
3+
const mapValues = require('lodash/mapValues');
44

55
const rejectAfterTimeout = (ms) =>
66
new Promise((_, reject) => setTimeout(reject, ms));
@@ -46,24 +46,25 @@ module.exports = (strapi, options, logger) => {
4646
return await store.reset();
4747
},
4848
},
49-
(fn) => async (...args) => {
50-
try {
51-
if (!store.ready) {
52-
logger.warn("the store is not ready");
49+
(fn) =>
50+
async (...args) => {
51+
try {
52+
if (!store.ready) {
53+
logger.warn('the store is not ready');
54+
return null;
55+
}
56+
return await Promise.race([
57+
fn(...args),
58+
rejectAfterTimeout(options.cacheTimeout),
59+
]);
60+
} catch (e) {
61+
if (e) {
62+
logger.warn('there was an error with the store', e);
63+
}
64+
logger.warn('the cache request timed out');
65+
5366
return null;
5467
}
55-
return await Promise.race([
56-
fn(...args),
57-
rejectAfterTimeout(options.cacheTimeout),
58-
]);
59-
} catch (e) {
60-
if (e) {
61-
logger.warn("there was an error with the store", e);
62-
}
63-
logger.warn("the cache request timed out");
64-
65-
return null;
6668
}
67-
}
6869
);
6970
};

Diff for: lib/cache/stores/mem.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22

3-
const LRU = require("lru-cache");
3+
const LRU = require('lru-cache');
44

55
/**
66
*

Diff for: lib/cache/stores/redis.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22

3-
const Redis = require("ioredis");
4-
const lru = require("redis-lru");
3+
const Redis = require('ioredis');
4+
const lru = require('redis-lru');
55

66
/**
77
*
@@ -11,25 +11,25 @@ const lru = require("redis-lru");
1111
*/
1212
module.exports = (strapi, options, logger) => {
1313
const prefix = (key) => `strapi-middleware-cache:${key}`;
14-
const unprefix = (key) => key.replace(/^strapi-middleware-cache:/, "");
14+
const unprefix = (key) => key.replace(/^strapi-middleware-cache:/, '');
1515

1616
const client = new Redis(options.redisConfig);
1717
const cache = lru(client, options.max);
1818
let reconnected = false;
1919

20-
client.on("ready", () => {
21-
logger.debug("redis connection established");
20+
client.on('ready', () => {
21+
logger.debug('redis connection established');
2222

2323
if (reconnected) {
2424
cache.reset();
2525
}
2626
});
2727

28-
client.on("error", (error) => {
28+
client.on('error', (error) => {
2929
logger.error(`redis connection failed`, error);
3030
});
3131

32-
client.on("reconnecting", (e) => {
32+
client.on('reconnecting', (e) => {
3333
reconnected = true;
3434
});
3535

@@ -54,7 +54,7 @@ module.exports = (strapi, options, logger) => {
5454
return cache.reset();
5555
},
5656
get ready() {
57-
return client.status === "ready";
57+
return client.status === 'ready';
5858
},
5959
};
6060
};

0 commit comments

Comments
 (0)