Skip to content

Commit

Permalink
Koa 2.0 Promise based middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Sidebottom committed Dec 7, 2016
1 parent 7ee478b commit da24c9f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 115 deletions.
86 changes: 43 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,59 @@ var _ = require('lodash');
var util = require('util');

module.exports = function (defaults) {
return function* cacheControl(next) {
yield* next;

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

if (options.private) {
cacheControl.push('private');
} else if (options.public) {
cacheControl.push('public');
}

if (options.private) {
cacheControl.push('private');
} else if (options.public) {
cacheControl.push('public');
}
if (options.noStore) {
options.noCache = true;
cacheControl.push('no-store');
}

if (options.noStore) {
options.noCache = true;
cacheControl.push('no-store');
}
if (options.noCache) {
options.maxAge = 0;
delete options.sMaxAge;
cacheControl.push('no-cache');
}

if (options.noCache) {
options.maxAge = 0;
delete options.sMaxAge;
cacheControl.push('no-cache');
}
if (options.noTransform) {
cacheControl.push('no-transform');
}

if (options.noTransform) {
cacheControl.push('no-transform');
}
if (options.proxyRevalidate) {
cacheControl.push('proxy-revalidate');
}

if (options.proxyRevalidate) {
cacheControl.push('proxy-revalidate');
}
if (options.mustRevalidate) {
cacheControl.push('must-revalidate');
} else if (!options.noCache) {
if (options.staleIfError) {
cacheControl.push(util.format('stale-if-error=%d', options.staleIfError));
}

if (options.mustRevalidate) {
cacheControl.push('must-revalidate');
} else if (!options.noCache) {
if (options.staleIfError) {
cacheControl.push(util.format('stale-if-error=%d', options.staleIfError));
if (options.staleWhileRevalidate) {
cacheControl.push(util.format('stale-while-revalidate=%d', options.staleWhileRevalidate));
}
}

if (options.staleWhileRevalidate) {
cacheControl.push(util.format('stale-while-revalidate=%d', options.staleWhileRevalidate));
if (_.isNumber(options.maxAge)) {
cacheControl.push(util.format('max-age=%d', options.maxAge));
}
}

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

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

if (cacheControl.length) {
this.set('Cache-Control', cacheControl.join(','));
}
if (cacheControl.length) {
ctx.set('Cache-Control', cacheControl.join(','));
}
});
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-cache-control",
"version": "1.0.0",
"version": "2.0.0-alpha.1",
"description": "Middleware for meddling with Cache-Control headers",
"main": "index.js",
"scripts": {
Expand All @@ -26,7 +26,7 @@
"devDependencies": {
"eslint": "^3.3.0",
"istanbul": "^0.4.3",
"koa": "^1.0.0",
"koa": "^2.0.0-alpha.7",
"mocha": "^3.0.2",
"supertest": "^2.0.0"
},
Expand Down
Loading

0 comments on commit da24c9f

Please sign in to comment.