From da24c9fad01e3ddfcad4fe7b76fe1f3a339f641b Mon Sep 17 00:00:00 2001 From: Chris Sidebottom Date: Wed, 7 Dec 2016 19:41:10 +0000 Subject: [PATCH] Koa 2.0 Promise based middleware --- index.js | 86 +++++++++++++++---------------- package.json | 4 +- test/index.js | 138 +++++++++++++++++++++++++------------------------- 3 files changed, 113 insertions(+), 115 deletions(-) diff --git a/index.js b/index.js index c5e2dae..4778ec0 100644 --- a/index.js +++ b/index.js @@ -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(',')); + } + }); } } diff --git a/package.json b/package.json index 4688d21..bec4478 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" }, diff --git a/test/index.js b/test/index.js index 55bd09f..d2bb0f6 100644 --- a/test/index.js +++ b/test/index.js @@ -6,14 +6,14 @@ var fs = require('fs'); describe('cacheControl()', function () { describe('default configuration', function () { it('uses defaults if nothing defined on request', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ maxAge: 4 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -25,18 +25,18 @@ describe('cacheControl()', function () { describe('override default configuration', function () { it('allows middleware to override options in incoming requests', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ maxAge: 4 })); - app.use(function*(next) { - this.cacheControl = { + app.use(function (ctx, next) { + ctx.cacheControl = { maxAge: 60 }; - yield next; + return next(); }); request(app.listen()) @@ -46,24 +46,22 @@ describe('cacheControl()', function () { }) it('allows middleware to override options on outgoing requests', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ maxAge: 300 })); - app.use(function*(next) { - try { - yield next; - } catch (err) { - this.cacheControl = { + app.use(function (ctx, next) { + return next().catch(function (err) { + ctx.cacheControl = { noCache: true }; - } + }); }); - app.use(function*(next) { - this.throw(500); + app.use(function (ctx, next) { + ctx.throw(500); }); request(app.listen()) @@ -75,14 +73,14 @@ describe('cacheControl()', function () { describe('public is set', function () { it('adds public flag to cache-control header', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ public: true })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -94,14 +92,14 @@ describe('cacheControl()', function () { describe('private is set', function () { it('adds private flag to cache-control header', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ private: true })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -111,15 +109,15 @@ describe('cacheControl()', function () { }); it('discards public flag in cache-control header', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ private: true, public: true })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -131,14 +129,14 @@ describe('cacheControl()', function () { describe('maxAge is set', function () { it('sets cache-control max-age section', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ maxAge: 4 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -150,14 +148,14 @@ describe('cacheControl()', function () { describe('staleIfError is set', function () { it('sets cache-control header with stale-if-error', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ staleIfError: 320 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -169,14 +167,14 @@ describe('cacheControl()', function () { describe('staleWhileRevalidate is set', function () { it('sets cache-control header with stale-while-revalidate', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ staleWhileRevalidate: 320 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -188,14 +186,14 @@ describe('cacheControl()', function () { describe('mustRevalidate is set', function () { it('sets cache-control header with must-revalidate', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ mustRevalidate: true })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -205,7 +203,7 @@ describe('cacheControl()', function () { }); it('overthrows stale-while-revalidate and stale-if-error', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ mustRevalidate: true, @@ -213,8 +211,8 @@ describe('cacheControl()', function () { staleIfError: 404 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -226,14 +224,14 @@ describe('cacheControl()', function () { describe('when noCache is true', function () { it('adds no-cache to Cache-Control header', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ noCache: true })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -243,15 +241,15 @@ describe('cacheControl()', function () { }); it('sets maxAge to 0', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ noCache: true, maxAge: 60 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -261,15 +259,15 @@ describe('cacheControl()', function () { }); it('removes sMaxAge', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ noCache: true, sMaxAge: 60 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -279,7 +277,7 @@ describe('cacheControl()', function () { }); it('ignores stale settings', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ noCache: true, @@ -287,8 +285,8 @@ describe('cacheControl()', function () { staleWhileRevalidate: 10 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -300,14 +298,14 @@ describe('cacheControl()', function () { describe('when noStore is true', function () { it('sets Cache-Control no-store and no-cache', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ noStore: true })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -317,15 +315,15 @@ describe('cacheControl()', function () { }); it('sets maxAge to 0', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ noStore: true, maxAge: 50 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -337,14 +335,14 @@ describe('cacheControl()', function () { describe('when noTransform is set', function () { it('sets Cache-Control no-transform', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ noTransform: true })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -356,14 +354,14 @@ describe('cacheControl()', function () { describe('when proxyRevalidate', function () { it('sets Cache-Control proxy-revalidate', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ proxyRevalidate: true })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -376,14 +374,14 @@ describe('cacheControl()', function () { describe('when sMaxAge', function () { it('sets Cache-Control s-maxage property', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl({ sMaxAge: 10 })); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen()) @@ -395,12 +393,12 @@ describe('cacheControl()', function () { describe('when no cache properties set', function () { it('does not set a cache-control header', function (done) { - var app = koa(); + var app = new koa(); app.use(cacheControl()); - app.use(function*(next) { - yield next; + app.use(function (ctx, next) { + return next(); }); request(app.listen())