route-cache
Advanced tools
Comparing version 0.5.0 to 0.6.1
10
index.js
@@ -69,5 +69,5 @@ 'use strict' | ||
if (value.isJson) { | ||
res.json(value.body) | ||
res.status(value.status).json(value.body) | ||
} else { | ||
res.send(value.body) | ||
res.status(value.status).send(value.body) | ||
} | ||
@@ -106,3 +106,3 @@ return true | ||
const body = data instanceof Buffer ? data.toString() : data | ||
if (res.statusCode < 400) cacheStore.set(key, { body: body, isJson: isJson }, ttl) | ||
if (res.statusCode < 400) cacheStore.set(key, { status: res.statusCode, body: body, isJson: isJson }, ttl) | ||
@@ -186,5 +186,5 @@ // send this response to everyone in the queue | ||
if (value.isJson) { | ||
res.json(value.body) | ||
res.status(value.status || 200).json(value.body) | ||
} else { | ||
res.send(value.body) | ||
res.status(value.status || 200).send(value.body) | ||
} | ||
@@ -191,0 +191,0 @@ }) |
{ | ||
"name": "route-cache", | ||
"version": "0.5.0", | ||
"version": "0.6.1", | ||
"description": "express middleware for caching your routes", | ||
@@ -36,3 +36,4 @@ "main": "index.js", | ||
"ignore": [ | ||
"test/**" | ||
"test/**", | ||
"loadtests/**" | ||
] | ||
@@ -39,0 +40,0 @@ }, |
# Route-Cache | ||
Blazing fast :bullettrain_side: Express middleware for route caching with a given TTL (in seconds) | ||
[![Build Status](https://travis-ci.com/bradoyler/route-cache.svg?branch=master)](https://travis-ci.com/bradoyler/route-cache) | ||
[![Build Status](https://github.com/bradoyler/route-cache/actions/workflows/node.js.yml/badge.svg)](https://github.com/bradoyler/route-cache/actions/workflows/node.js.yml) | ||
[![NPM Version][npm-image]][npm-url] | ||
@@ -113,1 +113,2 @@ [![Downloads][downloads-image]][npm-url] | ||
[npm-url]: https://npmjs.org/package/route-cache | ||
[gha-workflow]: https://github.com/bradoyler/route-cache/actions/workflows/node.js.yml |
'use strict'; | ||
var request = require('supertest'), | ||
routeCache = require('../index').config({max:99}), | ||
express = require('express'); | ||
express = require('express'), | ||
assert = require('assert'); | ||
var hitIndex = 0; | ||
var noContentIndex = 0; | ||
@@ -34,2 +36,12 @@ var app = express(); | ||
app.get('/send-test-204', function (req, res) { | ||
noContentIndex++; | ||
res.status(204).send(); | ||
}); | ||
app.get('/test-api-204', function (req, res) { | ||
noContentIndex++; | ||
res.status(204).json(); | ||
}); | ||
it('1st res.send', function (done) { | ||
@@ -95,3 +107,37 @@ agent | ||
it('1st send-test-204', function (done) { | ||
agent | ||
.get('/send-test-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}) | ||
it('2st send-test-204', function (done) { | ||
agent | ||
.get('/send-test-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}) | ||
it('1st test-api-204', function (done) { | ||
agent | ||
.get('/test-api-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 2) | ||
done() | ||
}) | ||
}) | ||
it('2st test-api-204', function (done) { | ||
agent | ||
.get('/test-api-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 2) | ||
done() | ||
}) | ||
}) | ||
}); |
@@ -6,4 +6,6 @@ /* globals decribe */ | ||
const express = require('express') | ||
const assert = require('assert'); | ||
let testindex = 0 | ||
let noContentIndex = 0 | ||
let paramTestindex = 0 | ||
@@ -28,2 +30,7 @@ let testindexRemove = 0 | ||
app.get('/204', routeCache.cacheSeconds(10), function (req, res) { | ||
noContentIndex++ | ||
res.status(204).send() | ||
}) | ||
app.get('/500', routeCache.cacheSeconds(10), function (req, res) { | ||
@@ -54,2 +61,7 @@ res.status(500).send('Internal server error: ' + Math.random()) | ||
app.get('/204-api', routeCache.cacheSeconds(3600), function (req, res) { | ||
noContentIndex++ | ||
res.status(204).json() | ||
}) | ||
const agent = request.agent(app) | ||
@@ -60,2 +72,3 @@ | ||
.get('/hello') | ||
.expect(200) | ||
.expect('Hello 1', done) | ||
@@ -67,2 +80,3 @@ }) | ||
.get('/hello') | ||
.expect(200) | ||
.expect('Hello 1', done) | ||
@@ -74,2 +88,3 @@ }) | ||
.get('/hello/1') | ||
.expect(200) | ||
.expect('Hello/1', done) | ||
@@ -81,5 +96,24 @@ }) | ||
.get('/hello/2') | ||
.expect(200) | ||
.expect('Hello param:2', done) | ||
}) | ||
it('1st 204', function (done) { | ||
agent | ||
.get('/204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}) | ||
it('2st 204', function (done) { | ||
agent | ||
.get('/204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}) | ||
it('1st Redirect to hello', function (done) { | ||
@@ -129,2 +163,3 @@ agent | ||
.get('/hello') | ||
.expect(200) | ||
.expect('Hello 2', done) | ||
@@ -137,2 +172,3 @@ }, 1200) | ||
.get('/hello?a=1') | ||
.expect(200) | ||
.expect('Hello 3', done) | ||
@@ -157,2 +193,3 @@ }) | ||
.get('/hello-remove') | ||
.expect(200) | ||
.expect('Hello remove 1').end(function (req, res) { | ||
@@ -162,2 +199,3 @@ setTimeout(function () { | ||
.get('/hello-remove') | ||
.expect(200) | ||
.expect('Hello remove 1').end(function (req, res) { | ||
@@ -168,2 +206,3 @@ routeCache.removeCache('/hello-remove') | ||
.get('/hello-remove') | ||
.expect(200) | ||
.expect('Hello remove 2', done) | ||
@@ -178,2 +217,3 @@ }) | ||
.get('/hello-api') | ||
.expect(200) | ||
.expect('Content-Type', /json/).end(function (req, res) { | ||
@@ -183,2 +223,3 @@ setTimeout(function () { | ||
.get('/hello-api') | ||
.expect(200) | ||
.expect('Content-Type', /json/, done) | ||
@@ -188,2 +229,20 @@ }, 200) | ||
}) | ||
it('1st 204-api', function (done) { | ||
agent | ||
.get('/204-api') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 2) | ||
done() | ||
}) | ||
}) | ||
it('2st 204-api', function (done) { | ||
agent | ||
.get('/204-api') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 2) | ||
done() | ||
}) | ||
}) | ||
}) |
'use strict' | ||
var request = require('supertest'), | ||
routeCache = require('../index'), | ||
express = require('express') | ||
express = require('express'), | ||
assert = require('assert') | ||
var paramTestIndex = 0 | ||
var noContentIndex = 0 | ||
@@ -16,2 +18,7 @@ describe('Params / Querystrings', function () { | ||
app.get('/params-204', routeCache.cacheSeconds(10, '/params-test-204'), function (req, res) { | ||
noContentIndex++ | ||
res.status(204).send() | ||
}) | ||
var agent = request.agent(app) | ||
@@ -39,2 +46,29 @@ | ||
}) | ||
it('204 without params', function (done) { | ||
agent | ||
.get('/params-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}) | ||
it('204 with a=1', function (done) { | ||
agent | ||
.get('/params-204?a=1') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}) | ||
it('204 with a=2', function (done) { | ||
agent | ||
.get('/params-204?a=2') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}) | ||
}) |
109
test/ttls.js
@@ -5,3 +5,4 @@ // mocha -R list test/no-cache | ||
routeCache = require('../index'), | ||
express = require('express'); | ||
express = require('express'), | ||
assert = require('assert'); | ||
@@ -15,2 +16,3 @@ var app = express(); | ||
var hitIndex = 0; | ||
var noContentIndex = 0; | ||
@@ -22,2 +24,7 @@ app.get('/cache-disabled', routeCache.cacheSeconds(-1), function (req, res) { | ||
app.get('/cache-disabled-204', routeCache.cacheSeconds(-1), function (req, res) { | ||
noContentIndex++; | ||
res.status(204).send(); | ||
}); | ||
it('Should get Hit #1', function (done) { | ||
@@ -40,2 +47,23 @@ agent | ||
it('Should noContentIndex is 1', function (done) { | ||
agent | ||
.get('/cache-disabled-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}); | ||
it('Should noContentIndex is 2 (after nextTick)', function (done) { | ||
process.nextTick(function () { | ||
agent | ||
.get('/cache-disabled-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 2) | ||
done() | ||
}) | ||
}); | ||
}); | ||
}) | ||
@@ -45,2 +73,3 @@ | ||
var hitIndex = 0; | ||
var noContentIndex = 0; | ||
@@ -52,2 +81,7 @@ app.get('/cache-zero', routeCache.cacheSeconds(0), function (req, res) { | ||
app.get('/cache-zero-204', routeCache.cacheSeconds(0), function (req, res) { | ||
noContentIndex++; | ||
res.status(204).send(); | ||
}); | ||
it('Should get Hit #1', function (done) { | ||
@@ -80,7 +114,40 @@ agent | ||
it('Should noContentIndex is 1', function (done) { | ||
agent | ||
.get('/cache-zero-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}); | ||
it('Should noContentIndex is 1 (after nextTick)', function (done) { | ||
process.nextTick(function () { | ||
agent | ||
.get('/cache-zero-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}); | ||
}); | ||
it('Should noContentIndex is 2 (after 200ms delay)', function (done) { | ||
setTimeout(function () { | ||
agent | ||
.get('/cache-zero-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 2) | ||
done() | ||
}) | ||
}, 200); | ||
}); | ||
}) | ||
context('1 second:', function () { | ||
var hitIndex = 0; | ||
var noContentIndex = 0; | ||
@@ -92,2 +159,7 @@ app.get('/cache-1s', routeCache.cacheSeconds(1), function (req, res) { | ||
app.get('/cache-1s-204', routeCache.cacheSeconds(1), function (req, res) { | ||
noContentIndex++; | ||
res.status(204).send(); | ||
}); | ||
it('Should get Hit #1', function (done) { | ||
@@ -120,4 +192,37 @@ agent | ||
it('Should noContentIndex is 1', function (done) { | ||
agent | ||
.get('/cache-1s-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}); | ||
it('Should noContentIndex is 1 (after 200ms delay)', function (done) { | ||
setTimeout(function () { | ||
agent | ||
.get('/cache-1s-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 1) | ||
done() | ||
}) | ||
}, 300); | ||
}); | ||
it('Should noContentIndex is 2 (after 1200ms delay)', function (done) { | ||
setTimeout(function () { | ||
agent | ||
.get('/cache-1s-204') | ||
.expect(204, () => { | ||
assert.equal(noContentIndex, 2) | ||
done() | ||
}) | ||
}, 1300); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45521
1286