From d53d7c018a4e3eced4eb9222bd2461ed923dbc39 Mon Sep 17 00:00:00 2001 From: Selby Kendrick Date: Fri, 26 Feb 2016 10:13:45 -0600 Subject: [PATCH] update to ES7 async for Koa 2 --- .travis.yml | 5 +- History.md | 4 + Readme.md | 95 +++---- example.js | 38 ++- index.js | 75 +++--- package.json | 10 +- test/test.js | 707 ++++++++++++++++++++++++++------------------------- 7 files changed, 477 insertions(+), 457 deletions(-) diff --git a/.travis.yml b/.travis.yml index f7c965e..8ca8a1e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,5 @@ node_js: - - "0.11" - - "0.12" - - "iojs-1" - - "iojs-2" + - "5" language: node_js script: "npm run test-travis" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" diff --git a/History.md b/History.md index 4a60929..fc4c023 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +0.0.1 / 2016-02-26 +================== + + * fork 3.3.1 / 2015-07-08 ================== diff --git a/Readme.md b/Readme.md index 0ede205..6f743d3 100644 --- a/Readme.md +++ b/Readme.md @@ -1,37 +1,11 @@ -# koa-session - -[![NPM version][npm-image]][npm-url] -[![build status][travis-image]][travis-url] -[![Test coverage][coveralls-image]][coveralls-url] -[![Gittip][gittip-image]][gittip-url] -[![David deps][david-image]][david-url] -[![iojs version][iojs-image]][iojs-url] -[![node version][node-image]][node-url] -[![npm download][download-image]][download-url] - -[npm-image]: https://img.shields.io/npm/v/koa-session.svg?style=flat-square -[npm-url]: https://npmjs.org/package/koa-session -[travis-image]: https://img.shields.io/travis/koajs/session.svg?style=flat-square -[travis-url]: https://travis-ci.org/koajs/session -[coveralls-image]: https://img.shields.io/coveralls/koajs/session.svg?style=flat-square -[coveralls-url]: https://coveralls.io/r/koajs/session?branch=master -[gittip-image]: https://img.shields.io/gittip/fengmk2.svg?style=flat-square -[gittip-url]: https://www.gittip.com/fengmk2/ -[david-image]: https://img.shields.io/david/koajs/session.svg?style=flat-square -[david-url]: https://david-dm.org/koajs/session -[iojs-image]: https://img.shields.io/badge/io.js-%3E=_1.0-yellow.svg?style=flat-square -[iojs-url]: http://iojs.org/ -[node-image]: https://img.shields.io/badge/node.js-%3E=_0.12-green.svg?style=flat-square -[node-url]: http://nodejs.org/download/ -[download-image]: https://img.shields.io/npm/dm/koa-session.svg?style=flat-square -[download-url]: https://npmjs.org/package/koa-session - - Simple cookie-based session middleware for Koa. +# koa-session-async + + Simple cookie-based session middleware for Koa 2. Forked from For Koa 2, use [koa-session](https://github.com/koajs/session) to use ES6 and ES7 async/await keywords. ## Installation ```js -$ npm install koa-session +$ npm i --save koa-session-async ``` ## Example @@ -39,37 +13,46 @@ $ npm install koa-session View counter example: ```js -var session = require('koa-session'); -var koa = require('koa'); -var app = koa(); - -app.keys = ['some secret hurr']; +'using strict'; +/* + Import required dependencies + */ +const session = require('koa-session-async'); +const Koa = require('koa'); + +/* + Instantiate the koa app + */ +const app = new Koa(); + +/* + Setup secrets + */ +app.keys = ['secret']; + +/* + Attach middleware + */ app.use(session(app)); -app.use(function *(){ - // ignore favicon - if (this.path === '/favicon.ico') return; - - var n = this.session.views || 0; - this.session.views = ++n; - this.body = n + ' views'; -}) - +/* + Hopefully this works... + */ +app.use(async (ctx) => { + if ('/favicon.ico' === ctx.path){ + return; + } + let n = ctx.session.views || 0; + ctx.session.views = ++n; + ctx.body = n + ' views'; +}); + +/* + Turn it on + */ app.listen(3000); console.log('listening on port 3000'); ``` -For Koa 2, use [koa-convert](https://github.com/gyson/koa-convert) to convert the session middleware : - -```js -const koa = require('koa'); -const session = require('koa-session') -const convert = require('koa-convert'); - -const app = new koa(); -app.use(convert(session(app))); - -// codes -``` ## Semantics diff --git a/example.js b/example.js index abf009d..8d80e49 100644 --- a/example.js +++ b/example.js @@ -1,18 +1,40 @@ -var session = require('./'); -var koa = require('koa'); -var app = koa(); +'using strict'; +/* + Import required dependencies + */ +const session = require('./'); +const Koa = require('koa'); +/* + Instantiate the koa app + */ +const app = new Koa(); + +/* + Setup secrets + */ app.keys = ['some secret hurr']; +/* + Attach middleware + */ app.use(session(app)); -app.use(function* (next){ - if ('/favicon.ico' == this.path) return; - var n = this.session.views || 0; - this.session.views = ++n; - this.body = n + ' views'; +/* + Hopefully this works... + */ +app.use(async (ctx) => { + if ('/favicon.ico' === ctx.path){ + return; + } + let n = ctx.session.views || 0; + ctx.session.views = ++n; + ctx.body = n + ' views'; }); +/* + Turn it on + */ app.listen(3000); console.log('listening on port 3000'); diff --git a/index.js b/index.js index 0b4a6e9..6f5e3b1 100644 --- a/index.js +++ b/index.js @@ -18,16 +18,8 @@ var ONE_DAY = 24 * 60 * 60 * 1000; * @api public */ -module.exports = function(opts, app){ - // session(app[, opts]) - if (opts && typeof opts.use === 'function') { - var tmp = app; - app = opts; - opts = tmp; - } - +module.exports = function(opts){ opts = opts || {}; - // key opts.key = opts.key || 'koa:sess'; @@ -35,50 +27,47 @@ module.exports = function(opts, app){ if (!('maxAge' in opts)) opts.maxAge = opts.maxage; // defaults - if (null == opts.overwrite) opts.overwrite = true; - if (null == opts.httpOnly) opts.httpOnly = true; - if (null == opts.signed) opts.signed = true; + if (null === opts.overwrite) opts.overwrite = true; + if (null === opts.httpOnly) opts.httpOnly = true; + if (null === opts.signed) opts.signed = true; debug('session options %j', opts); - if (!app || typeof app.use !== 'function') { - throw new TypeError('app instance required: `session(opts, app)`'); - } - // setup encoding/decoding if (typeof opts.encode !== 'function') { - opts.encode = encode + opts.encode = encode; } if (typeof opts.decode !== 'function') { - opts.decode = decode + opts.decode = decode; } + return async (ctx, next) => { // to pass to Session() - app.context.sessionKey = opts.key; + ctx.sessionKey = opts.key; - app.context.__defineGetter__('session', function(){ - var sess = this._sess; +ctx.__defineGetter__('session', function(){ + let sess = ctx._sess; // already retrieved if (sess) return sess; // unset if (false === sess) return null; - var json = this.cookies.get(opts.key, opts); + var json = ctx.cookies.get(opts.key, opts); if (json) { debug('parse %s', json); try { // make sure sessionOptions exists - initSessionOptions(this, opts); + initSessionOptions(ctx, opts); var obj = opts.decode(json); - if (typeof opts.valid === 'function' && !opts.valid(this, obj)) { + if (typeof opts.valid === 'function' && !opts.valid(ctx, obj)) { // valid session value fail, ignore this session - sess = new Session(this); + sess = new Session(ctx); json = obj; debug('invalid %j', obj); } else { - sess = new Session(this, obj); + sess = new Session(ctx, obj); // make prev a different object from sess json = opts.decode(json); } @@ -90,34 +79,40 @@ module.exports = function(opts, app){ // but `JSON.parse(string)` will crash. debug('decode %j error: %s', json, err); if (!(err instanceof SyntaxError)) throw err; - sess = new Session(this); + sess = new Session(ctx); json = null; } } else { debug('new session'); - sess = new Session(this); + sess = new Session(ctx); } - this._sess = sess; - this._prevjson = json; + ctx._sess = sess; + ctx._prevjson = json; return sess; }); - app.context.__defineSetter__('session', function(val){ - if (null == val) return this._sess = false; - if ('object' == typeof val) return this._sess = new Session(this, val); + ctx.__defineSetter__('session', function(val){ + if (null === val) { + ctx._sess = false; + return ctx._sess; + } + if ('object' === typeof val) { + ctx._sess = new Session(ctx, val); + return ctx._sess; + } throw new Error('this.session can only be set as null or an object.'); }); - return function* (next){ + // make sessionOptions independent in each request - initSessionOptions(this, opts); + initSessionOptions(ctx, opts); try { - yield* next; + await next(); } catch (err) { throw err; } finally { - commit(this, this._prevjson, this._sess, opts); + commit(ctx, ctx._prevjson, ctx._sess, opts); } }; }; @@ -180,7 +175,7 @@ function Session(ctx, obj) { else { for (var k in obj) { // change session options - if ('_maxAge' == k) this._ctx.sessionOptions.maxAge = obj._maxAge; + if ('_maxAge' === k) this._ctx.sessionOptions.maxAge = obj._maxAge; else this[k] = obj[k]; } } @@ -199,8 +194,8 @@ Session.prototype.toJSON = function(){ var obj = {}; Object.keys(this).forEach(function(key){ - if ('isNew' == key) return; - if ('_' == key[0]) return; + if ('isNew' === key) return; + if ('_' === key[0]) return; obj[key] = self[key]; }); diff --git a/package.json b/package.json index b18769a..49a69cd 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { - "name": "koa-session", + "name": "koa-session-async", "description": "Koa cookie session middleware", - "repository": "koajs/session", - "version": "3.3.1", + "repository": "selbyk/session", + "version": "0.0.2", "keywords": [ "koa", "middleware", @@ -13,8 +13,8 @@ "index.js" ], "devDependencies": { - "istanbul-harmony": "0", - "koa": "1.1.2", + "istanbul-harmony": "^0.3.16", + "koa": "^2.0.0-alpha.3", "mocha": "2", "should": "8.0.2", "supertest": "1" diff --git a/test/test.js b/test/test.js index 4290409..a7ce18f 100644 --- a/test/test.js +++ b/test/test.js @@ -3,79 +3,81 @@ var request = require('supertest'); var should = require('should'); var session = require('..'); -describe('Koa Session', function(){ +describe('Koa Session', function() { var cookie; - describe('when options.signed = true', function(){ - describe('when app.keys are set', function(){ - it('should work', function(done){ + describe('when options.signed = true', function() { + describe('when app.keys are set', function() { + it('should work', function(done) { var app = koa(); app.keys = ['a', 'b']; app.use(session({}, app)); - app.use(function *(){ + app.use(function*() { this.session.message = 'hi'; this.body = this.session; }); request(app.listen()) - .get('/') - .expect(200, done); - }) - }) + .get('/') + .expect(200, done); + }); + }); - describe('when app.keys are not set', function(){ - it('should throw', function(done){ + describe('when app.keys are not set', function() { + it('should throw', function(done) { var app = koa(); app.use(session(app)); - app.use(function *(){ + app.use(function*() { this.session.message = 'hi'; this.body = this.session; }); request(app.listen()) - .get('/') - .expect(500, done); - }) - }) + .get('/') + .expect(500, done); + }); + }); - describe('when app not set', function(){ - it('should throw', function(){ + describe('when app not set', function() { + it('should throw', function() { var app = koa(); - (function(){ + (function() { app.use(session()); }).should.throw('app instance required: `session(opts, app)`'); - }) - }) - }) + }); + }); + }); - describe('when options.signed = false', function(){ - describe('when app.keys are not set', function(){ - it('should work', function(done){ + describe('when options.signed = false', function() { + describe('when app.keys are not set', function() { + it('should work', function(done) { var app = koa(); - app.use(session({ signed: false }, app)); + app.use(session({ + signed: false + }, app)); - app.use(function *(){ + app.use(function*() { this.session.message = 'hi'; this.body = this.session; }); request(app.listen()) - .get('/') - .expect(200, done); - }) - }) - }) + .get('/') + .expect(200, done); + }); + }); + }); - describe('when the session contains a ;', function(){ - it('should still work', function(done){ - var app = App(); + describe('when the session contains a ;', function() { + it('should still work', function(done) { + var app = new App(); - app.use(function *(){ + app.use(function*() { if (this.method === 'POST') { this.session.string = ';'; this.status = 204; @@ -87,271 +89,277 @@ describe('Koa Session', function(){ var server = app.listen(); request(server) - .post('/') - .expect(204, function(err, res){ - if (err) return done(err); - var cookie = res.headers['set-cookie']; - request(server) - .get('/') - .set('Cookie', cookie.join(';')) - .expect(';', done); - }) - }) - }) - - describe('new session', function(){ - describe('when not accessed', function(){ + .post('/') + .expect(204, function(err, res) { + if (err) return done(err); + var cookie = res.headers['set-cookie']; + request(server) + .get('/') + .set('Cookie', cookie.join(';')) + .expect(';', done); + }); + }); + }); + + describe('new session', function() { + describe('when not accessed', function() { it('should not Set-Cookie', function(done) { - var app = App(); + var app = new App(); - app.use(function *(){ + app.use(function*() { this.body = 'greetings'; - }) + }); request(app.listen()) - .get('/') - .expect(200, function(err, res){ - if (err) return done(err); - res.header.should.not.have.property('set-cookie'); - done(); - }) - }) - }) + .get('/') + .expect(200, function(err, res) { + if (err) return done(err); + res.header.should.not.have.property('set-cookie'); + done(); + }); + }); + }); - describe('when accessed and not populated', function(done){ + describe('when accessed and not populated', function(done) { it('should not Set-Cookie', function(done) { - var app = App(); + var app = new App(); - app.use(function *(){ - this.session; - this.body = 'greetings'; - }) + app.use(async (ctx) => { + console.log(ctx.session); + ctx.body = 'greetings'; + }); request(app.listen()) - .get('/') - .expect(200, function(err, res){ - if (err) return done(err); - res.header.should.not.have.property('set-cookie'); - done(); - }) - }) - }) + .get('/') + .expect(200, function(err, res) { + if (err) return done(err); + res.header.should.not.have.property('set-cookie'); + done(); + }); + }); + done(); + }); - describe('when populated', function(done){ - it('should Set-Cookie', function(done){ - var app = App(); + describe('when populated', function(done) { + it('should Set-Cookie', function(done) { + var app = new App(); - app.use(function *(){ + app.use(function*() { this.session.message = 'hello'; this.body = ''; - }) + }); request(app.listen()) - .get('/') - .expect('Set-Cookie', /koa:sess/) - .expect(200, function(err, res){ - if (err) return done(err); - cookie = res.header['set-cookie'].join(';'); - done(); - }) - }) + .get('/') + .expect('Set-Cookie', /koa:sess/) + .expect(200, function(err, res) { + if (err) return done(err); + cookie = res.header['set-cookie'].join(';'); + done(); + }); + }); - it('should not Set-Cookie', function(done){ - var app = App(); + it('should not Set-Cookie', function(done) { + var app = new App(); - app.use(function *(){ + app.use(function*() { this.body = this.session; - }) + }); request(app.listen()) - .get('/') - .expect(200, function(err, res){ - if (err) return done(err); - res.header.should.not.have.property('set-cookie'); - done(); - }) - }) - }) - }) - - describe('saved session', function(){ - describe('when not accessed', function(){ - it('should not Set-Cookie', function(done){ - var app = App(); - - app.use(function *(){ + .get('/') + .expect(200, function(err, res) { + if (err) return done(err); + res.header.should.not.have.property('set-cookie'); + done(); + }); + }); + done(); + }); + }); + + describe('saved session', function() { + describe('when not accessed', function() { + it('should not Set-Cookie', function(done) { + var app = new App(); + + app.use(function*() { this.body = 'aklsdjflasdjf'; - }) + }); request(app.listen()) - .get('/') - .set('Cookie', cookie) - .expect(200, function(err, res){ - if (err) return done(err); - res.header.should.not.have.property('set-cookie'); - done(); - }) - }) - }) + .get('/') + .set('Cookie', cookie) + .expect(200, function(err, res) { + if (err) return done(err); + res.header.should.not.have.property('set-cookie'); + done(); + }); + }); + }); - describe('when accessed but not changed', function(){ - it('should be the same session', function(done){ - var app = App(); + describe('when accessed but not changed', function() { + it('should be the same session', function(done) { + var app = new App(); - app.use(function *(){ + app.use(function*() { this.session.message.should.equal('hello'); this.body = 'aklsdjflasdjf'; - }) + }); request(app.listen()) - .get('/') - .set('Cookie', cookie) - .expect(200, done); - }) + .get('/') + .set('Cookie', cookie) + .expect(200, done); + }); - it('should not Set-Cookie', function(done){ - var app = App(); + it('should not Set-Cookie', function(done) { + var app = new App(); - app.use(function *(){ + app.use(function*() { this.session.message.should.equal('hello'); this.body = 'aklsdjflasdjf'; - }) + }); request(app.listen()) - .get('/') - .set('Cookie', cookie) - .expect(200, function(err, res){ - if (err) return done(err); - res.header.should.not.have.property('set-cookie'); - done(); - }) - }) - }) + .get('/') + .set('Cookie', cookie) + .expect(200, function(err, res) { + if (err) return done(err); + res.header.should.not.have.property('set-cookie'); + done(); + }); + }); + }); - describe('when accessed and changed', function(){ - it('should Set-Cookie', function(done){ - var app = App(); + describe('when accessed and changed', function() { + it('should Set-Cookie', function(done) { + var app = new App(); - app.use(function *(){ + app.use(function*() { this.session.money = '$$$'; this.body = 'aklsdjflasdjf'; - }) + }); request(app.listen()) - .get('/') - .set('Cookie', cookie) - .expect('Set-Cookie', /koa:sess/) - .expect(200, done); - }) - }) - }) + .get('/') + .set('Cookie', cookie) + .expect('Set-Cookie', /koa:sess/) + .expect(200, done); + }); + }); + }); - describe('when session is', function(){ - describe('null', function(){ - it('should expire the session', function(done){ - var app = App(); + describe('when session is', function() { + describe('null', function() { + it('should expire the session', function(done) { + var app = new App(); - app.use(function *(){ + app.use(function*() { this.session = null; this.body = 'asdf'; - }) + }); request(app.listen()) - .get('/') - .expect('Set-Cookie', /koa:sess/) - .expect(200, done); - }) - }) + .get('/') + .expect('Set-Cookie', /koa:sess/) + .expect(200, done); + }); + }); - describe('an empty object', function(){ - it('should not Set-Cookie', function(done){ - var app = App(); + describe('an empty object', function() { + it('should not Set-Cookie', function(done) { + var app = new App(); - app.use(function *(){ + app.use(function*() { this.session = {}; this.body = 'asdf'; - }) + }); request(app.listen()) - .get('/') - .expect(200, function(err, res){ - if (err) return done(err); - res.header.should.not.have.property('set-cookie'); - done(); - }); - }) - }) + .get('/') + .expect(200, function(err, res) { + if (err) return done(err); + res.header.should.not.have.property('set-cookie'); + done(); + }); + }); + }); - describe('an object', function(){ - it('should create a session', function(done){ - var app = App(); + describe('an object', function() { + it('should create a session', function(done) { + var app = new App(); - app.use(function *(){ - this.session = { message: 'hello' }; + app.use(function*() { + this.session = { + message: 'hello' + }; this.body = 'asdf'; - }) + }); request(app.listen()) - .get('/') - .expect('Set-Cookie', /koa:sess/) - .expect(200, done); - }) - }) + .get('/') + .expect('Set-Cookie', /koa:sess/) + .expect(200, done); + }); + }); - describe('anything else', function(){ - it('should throw', function(done){ - var app = App(); + describe('anything else', function() { + it('should throw', function(done) { + var app = new App(); - app.use(function *(){ - this.session = 'asdf' - }) + app.use(async (ctx) => { + ctx.session = 'asdf'; + }); request(app.listen()) - .get('/') - .expect(500, done); - }) - }) - }) + .get('/') + .expect(500, done); + }); + }); + }); - describe('when an error is thrown downstream and caught upstream', function(){ - it('should still save the session', function(done){ + describe('when an error is thrown downstream and caught upstream', function() { + it('should still save the session', function(done) { var app = koa(); app.keys = ['a', 'b']; - app.use(function *(next){ + app.use(async (ctx, next) => { try { - yield *next; + await next(); } catch (err) { - this.status = err.status; - this.body = err.message; + ctx.status = err.status; + ctx.body = err.message; } }); app.use(session(app)); - app.use(function *(next){ - this.session.name = 'funny'; - yield *next; + app.use(async (ctx, next) => { + ctx.session.name = 'funny'; + await next(); }); - app.use(function *(next){ - this.throw(401); + app.use(async (ctx) => { + ctx.status = 401; }); request(app.listen()) - .get('/') - .expect('Set-Cookie', /koa:sess/) - .expect(401, done); - }) - }) - - describe('when maxAge present', function () { - describe('and not expire', function () { - it('should not expire the session', function (done) { - var app = App({ maxAge: 100 }); - - app.use(function* () { + .get('/') + .expect('Set-Cookie', /koa:sess/) + .expect(401, done); + }); + }); + + describe('when maxAge present', function() { + describe('and not expire', function() { + it('should not expire the session', function(done) { + var app = new App({ + maxAge: 100 + }); + + app.use(function*() { if (this.method === 'POST') { this.session.message = 'hi'; this.body = 200; @@ -364,26 +372,28 @@ describe('Koa Session', function(){ var server = app.listen(); request(server) - .post('/') - .expect('Set-Cookie', /koa:sess/) - .end(function (err, res) { - if (err) return done(err); - var cookie = res.headers['set-cookie'].join(';'); + .post('/') + .expect('Set-Cookie', /koa:sess/) + .end(function(err, res) { + if (err) return done(err); + var cookie = res.headers['set-cookie'].join(';'); - request(server) - .get('/') - .set('cookie', cookie) - .expect('hi', done); - }) - }) - }) + request(server) + .get('/') + .set('cookie', cookie) + .expect('hi', done); + }); + }); + }); - describe('and expired', function () { - it('should expire the sess', function (done) { - var app = App({ maxAge: 100 }); + describe('and expired', function() { + it('should expire the sess', function(done) { + var app = new App({ + maxAge: 100 + }); - app.use(function* () { + app.use(function*() { if (this.method === 'POST') { this.session.message = 'hi'; this.status = 200; @@ -396,97 +406,99 @@ describe('Koa Session', function(){ var server = app.listen(); request(server) - .post('/') - .expect('Set-Cookie', /koa:sess/) - .end(function (err, res) { - if (err) return done(err); - var cookie = res.headers['set-cookie'].join(';'); + .post('/') + .expect('Set-Cookie', /koa:sess/) + .end(function(err, res) { + if (err) return done(err); + var cookie = res.headers['set-cookie'].join(';'); + + setTimeout(function() { + request(server) + .get('/') + .set('cookie', cookie) + .expect('', done); + }, 200); + }); + }); + }); + }); - setTimeout(function () { - request(server) - .get('/') - .set('cookie', cookie) - .expect('', done); - }, 200); - }) - }) - }) - }) - - describe('ctx.session.maxAge', function (){ - it('should return opt.maxAge', function(done){ - var app = App({maxAge: 100}); - - app.use(function *(){ + describe('ctx.session.maxAge', function() { + it('should return opt.maxAge', function(done) { + var app = new App({ + maxAge: 100 + }); + + app.use(function*() { this.body = this.session.maxAge; }); request(app.listen()) - .get('/') - .expect('100', done); - }) - }) + .get('/') + .expect('100', done); + }); + }); - describe('ctx.session.maxAge=', function () { - it('should set sessionOptions.maxAge', function(done){ - var app = App(); + describe('ctx.session.maxAge=', function() { + it('should set sessionOptions.maxAge', function(done) { + var app = new App(); - app.use(function* (){ + app.use(function*() { this.session.foo = 'bar'; this.session.maxAge = 100; this.body = this.session.foo; }); request(app.listen()) - .get('/') - .expect('Set-Cookie', /expires=/) - .expect(200, done); - }) - }) - - describe('when get session before enter session middleware', function(){ - it('should work', function(done){ + .get('/') + .expect('Set-Cookie', /expires=/) + .expect(200, done); + }); + }); + + describe('when get session before enter session middleware', function() { + it('should work', function(done) { var app = koa(); app.keys = ['a', 'b']; - app.use(function* (next) { + app.use(function*(next) { this.session.foo = 'hi'; yield next; }); app.use(session({}, app)); - app.use(function* (){ + app.use(function*() { this.body = this.session; }); request(app.callback()) - .get('/') - .expect(200, function (err, res) { - should.not.exist(err); - var cookies = res.headers['set-cookie'].join(';'); - cookies.should.containEql('koa:sess='); - - request(app.callback()) .get('/') - .set('Cookie', cookies) - .expect(200, done); - }); - }) - }) + .expect(200, function(err, res) { + should.not.exist(err); + var cookies = res.headers['set-cookie'].join(';'); + cookies.should.containEql('koa:sess='); - describe('when valid and beforeSave set', function(){ - it('should ignore session when uid changed', function(done){ + request(app.callback()) + .get('/') + .set('Cookie', cookies) + .expect(200, done); + }); + }); + }); + + describe('when valid and beforeSave set', function() { + it('should ignore session when uid changed', function(done) { var app = koa(); app.keys = ['a', 'b']; app.use(session({ - valid: function (ctx, sess) { + valid: function(ctx, sess) { return ctx.cookies.get('uid') === sess.uid; }, - beforeSave: function (ctx, sess) { + beforeSave: function(ctx, sess) { sess.uid = ctx.cookies.get('uid'); } }, app)); - app.use(function* () { + app.use(function*() { if (!this.session.foo) { this.session.foo = Date.now() + '|uid:' + this.cookies.get('uid'); } @@ -498,85 +510,92 @@ describe('Koa Session', function(){ }); request(app.callback()) - .get('/') - .set('Cookie', 'uid=123') - .expect(200, function (err, res) { - should.not.exist(err); - var data = res.body; - var cookies = res.headers['set-cookie'].join(';'); - cookies.should.containEql('koa:sess='); - - request(app.callback()) .get('/') - .set('Cookie', cookies + ';uid=123') - .expect(200) - .expect(data, function (err) { + .set('Cookie', 'uid=123') + .expect(200, function(err, res) { should.not.exist(err); + var data = res.body; + var cookies = res.headers['set-cookie'].join(';'); + cookies.should.containEql('koa:sess='); - // should ignore uid:123 session and create a new session for uid:456 request(app.callback()) - .get('/') - .set('Cookie', cookies + ';uid=456') - .expect(200, function (err, res) { - should.not.exist(err); - res.body.uid.should.equal('456'); - res.body.foo.should.not.equal(data.foo); - done(); - }); + .get('/') + .set('Cookie', cookies + ';uid=123') + .expect(200) + .expect(data, function(err) { + should.not.exist(err); + + // should ignore uid:123 session and create a new session for uid:456 + request(app.callback()) + .get('/') + .set('Cookie', cookies + ';uid=456') + .expect(200, function(err, res) { + should.not.exist(err); + res.body.uid.should.equal('456'); + res.body.foo.should.not.equal(data.foo); + done(); + }); + }); }); - }); - }) - }) + }); + }); - describe('when options.encode and options.decode are functions', function () { - describe('they are used to encode/decode stored cookie values', function () { - it('should work', function (done) { - var encodeCallCount = 0 - var decodeCallCount = 0 + describe('when options.encode and options.decode are functions', function() { + describe('they are used to encode/decode stored cookie values', function() { + it('should work', function(done) { + var encodeCallCount = 0; + var decodeCallCount = 0; function encode(data) { - ++encodeCallCount - return JSON.stringify({enveloped: data}) + ++encodeCallCount; + return JSON.stringify({ + enveloped: data + }); } + function decode(data) { - ++decodeCallCount - return JSON.parse(data).enveloped + ++decodeCallCount; + return JSON.parse(data).enveloped; } - var app = koa(); + var app = new koa(); app.keys = ['a', 'b']; app.use(session({ encode: encode, decode: decode }, app)); - app.use(function * () { - this.session.counter = (this.session.counter || 0) + 1 - this.body = this.session - return - }) + app.use(async (ctx) => { + ctx.session.counter = (ctx.session.counter || 0) + 1; + ctx.body = ctx.session; + return; + }); request(app.callback()) .get('/') - .expect(function () { encodeCallCount.should.above(0, 'encode was not called'); }) - .expect(200, function (err, res) { - should.not.exist(err) - res.body.counter.should.equal(1, 'expected body to be equal to session.counter') + .expect(function() { + encodeCallCount.should.above(0, 'encode was not called'); + }) + .expect(200, function(err, res) { + should.not.exist(err); + res.body.counter.should.equal(1, 'expected body to be equal to session.counter'); var cookies = res.headers['set-cookie'].join(';'); request(app.callback()) .get('/') .set('Cookie', cookies) - .expect(function () { decodeCallCount.should.be.above(1, 'decode was not called'); }) - .expect(200, function (err, res) { + .expect(function() { + decodeCallCount.should.be.above(1, 'decode was not called'); + }) + .expect(200, function(err, res) { should.not.exist(err); res.body.counter.should.equal(2); done(); - }) - }) - }) - }) - }) -}) + }); + }); + }); + }); + }); +}); function App(options) { var app = koa();