Skip to content

Commit c800fd0

Browse files
committed
fix: ensure only HEAD and GET requests are considered for caching
1 parent 1fb0c45 commit c800fd0

17 files changed

+94
-7655
lines changed

.commitlintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional']
3+
};

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.*.js

.gitattributes

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* text=auto
1+
* text=auto eol=lf

.github/workflows/ci.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: CI
2+
on:
3+
- push
4+
- pull_request
5+
jobs:
6+
build:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
os:
11+
- ubuntu-latest
12+
node_version:
13+
- 18
14+
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Setup node
18+
uses: actions/setup-node@v3
19+
with:
20+
node-version: ${{ matrix.node_version }}
21+
- name: Install dependencies
22+
run: npm install
23+
- name: Run tests
24+
run: npm run test
25+
- name: Run benchmarks
26+
run: npm run benchmarks

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@
44
node_modules
55
coverage
66
.nyc_output
7+
locales/
8+
package-lock.json
9+
yarn.lock
10+
11+
Thumbs.db
12+
tmp/
13+
temp/
14+
*.lcov
15+
.env

.lintstagedrc.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module.exports = {
2-
"*.md,!test/**/*.md": [
3-
filenames => filenames.map(filename => `remark ${filename} -qfo`)
4-
],
2+
"*.md": filenames => filenames.map(filename => `remark ${filename} -qfo`),
53
'package.json': 'fixpack',
64
'*.js': 'xo --fix'
75
};

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
singleQuote: true,
3+
bracketSpacing: true,
4+
trailingComma: 'none'
5+
};

.remarkrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: ['preset-github']
3+
};

.travis.yml

-11
This file was deleted.

.xo-config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
prettier: true,
3+
space: true,
4+
extends: ['xo-lass']
5+
};

README.md

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# [**@ladjs/koa-cache-responses**](https://github.com/ladjs/koa-cache-responses)
22

3-
[![build status](https://img.shields.io/travis/com/ladjs/koa-cache-responses.svg)](https://travis-ci.com/ladjs/koa-cache-responses)
4-
[![code coverage](https://img.shields.io/codecov/c/github/ladjs/koa-cache-responses.svg)](https://codecov.io/gh/ladjs/koa-cache-responses)
3+
[![build status](https://github.com/ladjs/koa-cache-responses/actions/workflows/ci.yml/badge.svg)](https://github.com/ladjs/koa-cache-responses/actions/workflows/ci.yml)
54
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
65
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
76
[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
87
[![license](https://img.shields.io/github/license/ladjs/koa-cache-responses.svg)](LICENSE)
9-
[![npm downloads](https://img.shields.io/npm/dt/@ladjs/koa-cache-responses.svg)](https://npm.im/@ladjs/koa-cache-responses)
8+
[![npm downloads](https://img.shields.io/npm/dt/koa-cash.svg)](https://npm.im/koa-cash)
109

1110
> Caching middleware for Koa using `koa-cash` and route pattern-based matching with `path-to-regexp`. Made for [Lad](https://lad.js.org).
1211
@@ -27,12 +26,6 @@
2726
npm install @ladjs/koa-cache-responses
2827
```
2928

30-
[yarn][]:
31-
32-
```sh
33-
yarn add @ladjs/koa-cache-responses
34-
```
35-
3629

3730
## Usage
3831

@@ -51,8 +44,6 @@ See [@ladjs/web](https://github.com/ladjs/web) integration.
5144
[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)
5245

5346

54-
##
47+
##
5548

5649
[npm]: https://www.npmjs.com/
57-
58-
[yarn]: https://yarnpkg.com/

ava.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
files: ['test/*.js', 'test/**/*.js']
3+
};

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CacheResponses {
1010
// <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control>
1111
// <https://web.dev/uses-long-cache-ttl/?utm_source=lighthouse&utm_medium=unknown>
1212
cacheControl: ['public', `max-age=${ms('1y') / 1000}`],
13+
methods: ['HEAD', 'GET'],
1314
...config
1415
};
1516

@@ -18,7 +19,7 @@ class CacheResponses {
1819
}
1920

2021
async middleware(ctx, next) {
21-
if (ctx.method !== 'GET') return next();
22+
if (!this.config.methods.includes(ctx.method)) return next();
2223

2324
if (!_.isFunction(ctx.cashed))
2425
throw new Error(

package.json

+25-57
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,43 @@
33
"description": "Caching middleware for Koa using koa-cash and route pattern-based matching with path-to-regexp. Made for Lad.",
44
"version": "0.0.3",
55
"author": "Nick Baugh <[email protected]> (http://niftylettuce.com/)",
6-
"ava": {
7-
"verbose": true
8-
},
96
"bugs": {
107
"url": "https://github.com/ladjs/koa-cache-responses/issues",
118
"email": "[email protected]"
129
},
13-
"commitlint": {
14-
"extends": [
15-
"@commitlint/config-conventional"
16-
]
17-
},
1810
"contributors": [
1911
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
2012
],
2113
"dependencies": {
22-
"lodash": "^4.17.15",
23-
"ms": "^2.1.2",
14+
"lodash": "^4.17.21",
15+
"ms": "^2.1.3",
2416
"path-to-regexp": "^6.1.0"
2517
},
2618
"devDependencies": {
27-
"@commitlint/cli": "latest",
28-
"@commitlint/config-conventional": "latest",
29-
"@ladjs/redis": "^1.0.4",
30-
"ava": "latest",
31-
"codecov": "latest",
32-
"cross-env": "latest",
33-
"eslint": "6.x",
34-
"eslint-config-xo-lass": "latest",
35-
"fast-safe-stringify": "^2.0.7",
36-
"fixpack": "latest",
37-
"husky": "latest",
38-
"koa": "^2.12.1",
39-
"koa-cash": "^4.0.3",
40-
"lint-staged": "latest",
41-
"node-cache": "^5.1.1",
42-
"nyc": "latest",
43-
"remark-cli": "latest",
44-
"remark-preset-github": "latest",
19+
"@commitlint/cli": "^19.7.1",
20+
"@commitlint/config-conventional": "^19.7.1",
21+
"@ladjs/redis": "^1.1.1",
22+
"ava": "^5.2.0",
23+
"cross-env": "^7.0.3",
24+
"eslint": "^8.55.0",
25+
"eslint-config-xo-lass": "^2.0.1",
26+
"fast-safe-stringify": "^2.1.1",
27+
"fixpack": "^4.0.0",
28+
"husky": "^9.1.7",
29+
"koa": "^2.15.4",
30+
"koa-cash": "^4.1.1",
31+
"lint-staged": "^15.4.3",
32+
"node-cache": "^5.1.2",
33+
"nyc": "^17.1.0",
34+
"remark-cli": "11.0.0",
35+
"remark-preset-github": "^4.0.4",
4536
"supertest": "^4.0.2",
46-
"xo": "0.25"
37+
"xo": "0.56.0"
4738
},
4839
"engines": {
49-
"node": ">=8.3"
40+
"node": ">=18"
5041
},
5142
"homepage": "https://github.com/ladjs/koa-cache-responses",
52-
"husky": {
53-
"hooks": {
54-
"pre-commit": "lint-staged && npm test",
55-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
56-
}
57-
},
5843
"keywords": [
5944
"alternative",
6045
"asset",
@@ -101,36 +86,19 @@
10186
],
10287
"license": "MIT",
10388
"main": "index.js",
104-
"prettier": {
105-
"singleQuote": true,
106-
"bracketSpacing": true,
107-
"trailingComma": "none"
108-
},
10989
"publishConfig": {
11090
"access": "public"
11191
},
112-
"remarkConfig": {
113-
"plugins": [
114-
"preset-github"
115-
]
116-
},
11792
"repository": {
11893
"type": "git",
11994
"url": "https://github.com/ladjs/koa-cache-responses"
12095
},
12196
"scripts": {
12297
"ava": "cross-env NODE_ENV=test ava",
123-
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
124-
"lint": "xo && remark . -qfo",
98+
"lint": "xo --fix && remark . -qfo && fixpack",
12599
"nyc": "cross-env NODE_ENV=test nyc ava",
126-
"test": "yarn run lint && yarn run ava",
127-
"test-coverage": "yarn run lint && yarn run nyc"
128-
},
129-
"xo": {
130-
"prettier": true,
131-
"space": true,
132-
"extends": [
133-
"xo-lass"
134-
]
100+
"prepare": "husky install",
101+
"pretest": "npm run lint",
102+
"test": "npm run nyc"
135103
}
136104
}

test/test.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ const koaCash = require('koa-cash');
66
const supertest = require('supertest');
77
const test = require('ava');
88
const safeStringify = require('fast-safe-stringify');
9-
109
const CacheResponses = require('..');
1110

12-
test('should expose methods and default options', t => {
11+
test('should expose methods and default options', (t) => {
1312
const cacheResponses = new CacheResponses();
1413
t.deepEqual(cacheResponses.config.pathToRegexp, {
1514
sensitive: true,
@@ -20,7 +19,7 @@ test('should expose methods and default options', t => {
2019
t.true(_.isFunction(cacheResponses.paired));
2120
});
2221

23-
test('memory cache', async t => {
22+
test('memory cache', async (t) => {
2423
const app = new Koa();
2524
const myCache = new NodeCache();
2625
app.use(
@@ -45,7 +44,7 @@ test('memory cache', async t => {
4544
let cachedTime;
4645
app.use((ctx, next) => {
4746
const time = Date.now();
48-
if (!cachedTime) cachedTime = time;
47+
cachedTime ||= time;
4948
ctx.body = time;
5049
return next();
5150
});
@@ -61,7 +60,7 @@ test('memory cache', async t => {
6160
t.is(JSON.parse(myCache.get('/')).body, cachedTime.toString());
6261
});
6362

64-
test('redis cache', async t => {
63+
test('redis cache', async (t) => {
6564
const app = new Koa();
6665
const redis = new Redis({
6766
lazyConnect: true,
@@ -76,7 +75,7 @@ test('redis cache', async t => {
7675
let value;
7776
try {
7877
value = await redis.get(key);
79-
if (value) value = JSON.parse(value);
78+
value &&= JSON.parse(value);
8079
} catch (err) {
8180
console.error(err);
8281
}
@@ -98,7 +97,7 @@ test('redis cache', async t => {
9897
let cachedTime;
9998
app.use((ctx, next) => {
10099
const time = Date.now();
101-
if (!cachedTime) cachedTime = time;
100+
cachedTime ||= time;
102101
ctx.body = time;
103102
return next();
104103
});

0 commit comments

Comments
 (0)