Skip to content

Commit

Permalink
Reduce dependencies and use more ES6ness
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Sidebottom committed Dec 7, 2016
1 parent da24c9f commit 8bec6ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
17 changes: 7 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
var _ = require('lodash');
var util = require('util');

module.exports = function (defaults) {
return function cacheControl(ctx, next) {
return next().then(function () {
var options = _.defaults(ctx.cacheControl || {}, defaults);
var options = ctx.cacheControl || defaults || {};
var cacheControl = [];

if (options.private) {
Expand Down Expand Up @@ -36,20 +33,20 @@ module.exports = function (defaults) {
cacheControl.push('must-revalidate');
} else if (!options.noCache) {
if (options.staleIfError) {
cacheControl.push(util.format('stale-if-error=%d', options.staleIfError));
cacheControl.push(`stale-if-error=${options.staleIfError}`);
}

if (options.staleWhileRevalidate) {
cacheControl.push(util.format('stale-while-revalidate=%d', options.staleWhileRevalidate));
cacheControl.push(`stale-while-revalidate=${options.staleWhileRevalidate}`);
}
}

if (_.isNumber(options.maxAge)) {
cacheControl.push(util.format('max-age=%d', options.maxAge));
if (Number.isInteger(options.maxAge)) {
cacheControl.push(`max-age=${options.maxAge}`);
}

if (_.isNumber(options.sMaxAge)) {
cacheControl.push(util.format('s-maxage=%d', options.sMaxAge));
if (Number.isInteger(options.sMaxAge)) {
cacheControl.push(`s-maxage=${options.sMaxAge}`);
}

if (cacheControl.length) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@
"supertest": "^2.0.0"
},
"dependencies": {
"lodash": "^4.12.0"
}
}
50 changes: 25 additions & 25 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var request = require('supertest');
var koa = require('koa');
var cacheControl = require('..');
var fs = require('fs');
const request = require('supertest');
const koa = require('koa');
const cacheControl = require('..');
const fs = require('fs');

describe('cacheControl()', function () {
describe('default configuration', function () {
it('uses defaults if nothing defined on request', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
maxAge: 4
Expand All @@ -25,7 +25,7 @@ describe('cacheControl()', function () {

describe('override default configuration', function () {
it('allows middleware to override options in incoming requests', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
maxAge: 4
Expand All @@ -46,7 +46,7 @@ describe('cacheControl()', function () {
})

it('allows middleware to override options on outgoing requests', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
maxAge: 300
Expand All @@ -73,7 +73,7 @@ describe('cacheControl()', function () {

describe('public is set', function () {
it('adds public flag to cache-control header', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
public: true
Expand All @@ -92,7 +92,7 @@ describe('cacheControl()', function () {

describe('private is set', function () {
it('adds private flag to cache-control header', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
private: true
Expand All @@ -109,7 +109,7 @@ describe('cacheControl()', function () {
});

it('discards public flag in cache-control header', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
private: true,
Expand All @@ -129,7 +129,7 @@ describe('cacheControl()', function () {

describe('maxAge is set', function () {
it('sets cache-control max-age section', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
maxAge: 4
Expand All @@ -148,7 +148,7 @@ describe('cacheControl()', function () {

describe('staleIfError is set', function () {
it('sets cache-control header with stale-if-error', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
staleIfError: 320
Expand All @@ -167,7 +167,7 @@ describe('cacheControl()', function () {

describe('staleWhileRevalidate is set', function () {
it('sets cache-control header with stale-while-revalidate', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
staleWhileRevalidate: 320
Expand All @@ -186,7 +186,7 @@ describe('cacheControl()', function () {

describe('mustRevalidate is set', function () {
it('sets cache-control header with must-revalidate', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
mustRevalidate: true
Expand All @@ -203,7 +203,7 @@ describe('cacheControl()', function () {
});

it('overthrows stale-while-revalidate and stale-if-error', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
mustRevalidate: true,
Expand All @@ -224,7 +224,7 @@ describe('cacheControl()', function () {

describe('when noCache is true', function () {
it('adds no-cache to Cache-Control header', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
noCache: true
Expand All @@ -241,7 +241,7 @@ describe('cacheControl()', function () {
});

it('sets maxAge to 0', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
noCache: true,
Expand All @@ -259,7 +259,7 @@ describe('cacheControl()', function () {
});

it('removes sMaxAge', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
noCache: true,
Expand All @@ -277,7 +277,7 @@ describe('cacheControl()', function () {
});

it('ignores stale settings', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
noCache: true,
Expand All @@ -298,7 +298,7 @@ describe('cacheControl()', function () {

describe('when noStore is true', function () {
it('sets Cache-Control no-store and no-cache', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
noStore: true
Expand All @@ -315,7 +315,7 @@ describe('cacheControl()', function () {
});

it('sets maxAge to 0', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
noStore: true,
Expand All @@ -335,7 +335,7 @@ describe('cacheControl()', function () {

describe('when noTransform is set', function () {
it('sets Cache-Control no-transform', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
noTransform: true
Expand All @@ -354,7 +354,7 @@ describe('cacheControl()', function () {

describe('when proxyRevalidate', function () {
it('sets Cache-Control proxy-revalidate', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
proxyRevalidate: true
Expand All @@ -374,7 +374,7 @@ describe('cacheControl()', function () {

describe('when sMaxAge', function () {
it('sets Cache-Control s-maxage property', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl({
sMaxAge: 10
Expand All @@ -393,7 +393,7 @@ describe('cacheControl()', function () {

describe('when no cache properties set', function () {
it('does not set a cache-control header', function (done) {
var app = new koa();
const app = new koa();

app.use(cacheControl());

Expand Down

0 comments on commit 8bec6ea

Please sign in to comment.