Skip to content

Commit

Permalink
fix: ensure only HEAD and GET requests are considered for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Feb 13, 2025
1 parent 1fb0c45 commit c800fd0
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 7,655 deletions.
3 changes: 3 additions & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional']
};
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.*.js
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI
on:
- push
- pull_request
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
node_version:
- 18
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test
- name: Run benchmarks
run: npm run benchmarks
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@
node_modules
coverage
.nyc_output
locales/
package-lock.json
yarn.lock

Thumbs.db
tmp/
temp/
*.lcov
.env
4 changes: 1 addition & 3 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module.exports = {
"*.md,!test/**/*.md": [
filenames => filenames.map(filename => `remark ${filename} -qfo`)
],
"*.md": filenames => filenames.map(filename => `remark ${filename} -qfo`),
'package.json': 'fixpack',
'*.js': 'xo --fix'
};
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 5 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
singleQuote: true,
bracketSpacing: true,
trailingComma: 'none'
};
3 changes: 3 additions & 0 deletions .remarkrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: ['preset-github']
};
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

5 changes: 5 additions & 0 deletions .xo-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
prettier: true,
space: true,
extends: ['xo-lass']
};
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# [**@ladjs/koa-cache-responses**](https://github.com/ladjs/koa-cache-responses)

[![build status](https://img.shields.io/travis/com/ladjs/koa-cache-responses.svg)](https://travis-ci.com/ladjs/koa-cache-responses)
[![code coverage](https://img.shields.io/codecov/c/github/ladjs/koa-cache-responses.svg)](https://codecov.io/gh/ladjs/koa-cache-responses)
[![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)
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
[![license](https://img.shields.io/github/license/ladjs/koa-cache-responses.svg)](LICENSE)
[![npm downloads](https://img.shields.io/npm/dt/@ladjs/koa-cache-responses.svg)](https://npm.im/@ladjs/koa-cache-responses)
[![npm downloads](https://img.shields.io/npm/dt/koa-cash.svg)](https://npm.im/koa-cash)

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

[yarn][]:

```sh
yarn add @ladjs/koa-cache-responses
```


## Usage

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


##
##

[npm]: https://www.npmjs.com/

[yarn]: https://yarnpkg.com/
3 changes: 3 additions & 0 deletions ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
files: ['test/*.js', 'test/**/*.js']
};
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CacheResponses {
// <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control>
// <https://web.dev/uses-long-cache-ttl/?utm_source=lighthouse&utm_medium=unknown>
cacheControl: ['public', `max-age=${ms('1y') / 1000}`],
methods: ['HEAD', 'GET'],
...config
};

Expand All @@ -18,7 +19,7 @@ class CacheResponses {
}

async middleware(ctx, next) {
if (ctx.method !== 'GET') return next();
if (!this.config.methods.includes(ctx.method)) return next();

if (!_.isFunction(ctx.cashed))
throw new Error(
Expand Down
82 changes: 25 additions & 57 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,43 @@
"description": "Caching middleware for Koa using koa-cash and route pattern-based matching with path-to-regexp. Made for Lad.",
"version": "0.0.3",
"author": "Nick Baugh <[email protected]> (http://niftylettuce.com/)",
"ava": {
"verbose": true
},
"bugs": {
"url": "https://github.com/ladjs/koa-cache-responses/issues",
"email": "[email protected]"
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"contributors": [
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
],
"dependencies": {
"lodash": "^4.17.15",
"ms": "^2.1.2",
"lodash": "^4.17.21",
"ms": "^2.1.3",
"path-to-regexp": "^6.1.0"
},
"devDependencies": {
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@ladjs/redis": "^1.0.4",
"ava": "latest",
"codecov": "latest",
"cross-env": "latest",
"eslint": "6.x",
"eslint-config-xo-lass": "latest",
"fast-safe-stringify": "^2.0.7",
"fixpack": "latest",
"husky": "latest",
"koa": "^2.12.1",
"koa-cash": "^4.0.3",
"lint-staged": "latest",
"node-cache": "^5.1.1",
"nyc": "latest",
"remark-cli": "latest",
"remark-preset-github": "latest",
"@commitlint/cli": "^19.7.1",
"@commitlint/config-conventional": "^19.7.1",
"@ladjs/redis": "^1.1.1",
"ava": "^5.2.0",
"cross-env": "^7.0.3",
"eslint": "^8.55.0",
"eslint-config-xo-lass": "^2.0.1",
"fast-safe-stringify": "^2.1.1",
"fixpack": "^4.0.0",
"husky": "^9.1.7",
"koa": "^2.15.4",
"koa-cash": "^4.1.1",
"lint-staged": "^15.4.3",
"node-cache": "^5.1.2",
"nyc": "^17.1.0",
"remark-cli": "11.0.0",
"remark-preset-github": "^4.0.4",
"supertest": "^4.0.2",
"xo": "0.25"
"xo": "0.56.0"
},
"engines": {
"node": ">=8.3"
"node": ">=18"
},
"homepage": "https://github.com/ladjs/koa-cache-responses",
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm test",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"keywords": [
"alternative",
"asset",
Expand Down Expand Up @@ -101,36 +86,19 @@
],
"license": "MIT",
"main": "index.js",
"prettier": {
"singleQuote": true,
"bracketSpacing": true,
"trailingComma": "none"
},
"publishConfig": {
"access": "public"
},
"remarkConfig": {
"plugins": [
"preset-github"
]
},
"repository": {
"type": "git",
"url": "https://github.com/ladjs/koa-cache-responses"
},
"scripts": {
"ava": "cross-env NODE_ENV=test ava",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"lint": "xo && remark . -qfo",
"lint": "xo --fix && remark . -qfo && fixpack",
"nyc": "cross-env NODE_ENV=test nyc ava",
"test": "yarn run lint && yarn run ava",
"test-coverage": "yarn run lint && yarn run nyc"
},
"xo": {
"prettier": true,
"space": true,
"extends": [
"xo-lass"
]
"prepare": "husky install",
"pretest": "npm run lint",
"test": "npm run nyc"
}
}
13 changes: 6 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const koaCash = require('koa-cash');
const supertest = require('supertest');
const test = require('ava');
const safeStringify = require('fast-safe-stringify');

const CacheResponses = require('..');

test('should expose methods and default options', t => {
test('should expose methods and default options', (t) => {
const cacheResponses = new CacheResponses();
t.deepEqual(cacheResponses.config.pathToRegexp, {
sensitive: true,
Expand All @@ -20,7 +19,7 @@ test('should expose methods and default options', t => {
t.true(_.isFunction(cacheResponses.paired));
});

test('memory cache', async t => {
test('memory cache', async (t) => {
const app = new Koa();
const myCache = new NodeCache();
app.use(
Expand All @@ -45,7 +44,7 @@ test('memory cache', async t => {
let cachedTime;
app.use((ctx, next) => {
const time = Date.now();
if (!cachedTime) cachedTime = time;
cachedTime ||= time;
ctx.body = time;
return next();
});
Expand All @@ -61,7 +60,7 @@ test('memory cache', async t => {
t.is(JSON.parse(myCache.get('/')).body, cachedTime.toString());
});

test('redis cache', async t => {
test('redis cache', async (t) => {
const app = new Koa();
const redis = new Redis({
lazyConnect: true,
Expand All @@ -76,7 +75,7 @@ test('redis cache', async t => {
let value;
try {
value = await redis.get(key);
if (value) value = JSON.parse(value);
value &&= JSON.parse(value);
} catch (err) {
console.error(err);
}
Expand All @@ -98,7 +97,7 @@ test('redis cache', async t => {
let cachedTime;
app.use((ctx, next) => {
const time = Date.now();
if (!cachedTime) cachedTime = time;
cachedTime ||= time;
ctx.body = time;
return next();
});
Expand Down
Loading

0 comments on commit c800fd0

Please sign in to comment.