Skip to content

Commit

Permalink
feat: allow init multi session middleware (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu authored and dead-horse committed Dec 18, 2018
1 parent 89c048a commit 5f12f70
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions index.js
Expand Up @@ -120,6 +120,9 @@ function formatOpts(opts) {
*/

function extendContext(context, opts) {
if (context.hasOwnProperty(CONTEXT_SESSION)) {
return;
}
Object.defineProperties(context, {
[CONTEXT_SESSION]: {
get() {
Expand Down
4 changes: 2 additions & 2 deletions lib/util.js
Expand Up @@ -13,7 +13,7 @@ module.exports = {
*/

decode(string) {
const body = new Buffer(string, 'base64').toString('utf8');
const body = Buffer.from(string, 'base64').toString('utf8');
const json = JSON.parse(body);
return json;
},
Expand All @@ -28,7 +28,7 @@ module.exports = {

encode(body) {
body = JSON.stringify(body);
return new Buffer(body).toString('base64');
return Buffer.from(body).toString('base64');
},

hash(sess) {
Expand Down
13 changes: 13 additions & 0 deletions test/cookie.test.js
@@ -1,5 +1,6 @@
'use strict';

const assert = require('assert');
const Koa = require('koa');
const request = require('supertest');
const should = require('should');
Expand Down Expand Up @@ -795,6 +796,18 @@ describe('Koa Session Cookie', () => {
});
});
});

describe('init multi session middleware', () => {
it('should work', () => {
const app = new Koa();

app.keys = [ 'a', 'b' ];
const s1 = session({}, app);
const s2 = session({}, app);
assert(s1);
assert(s2);
});
});
});

function App(options) {
Expand Down

0 comments on commit 5f12f70

Please sign in to comment.