From fa9da61aba9af15eb1e4f7a44f05f9d46534f94b Mon Sep 17 00:00:00 2001 From: Runrioter Date: Thu, 24 Aug 2017 15:39:29 +0800 Subject: [PATCH 1/2] fix example bug and use syntactic sugar --- example.js | 4 ++-- index.js | 4 +--- package.json | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/example.js b/example.js index abf009d..ea47cce 100644 --- a/example.js +++ b/example.js @@ -1,7 +1,7 @@ var session = require('./'); -var koa = require('koa'); -var app = koa(); +var Koa = require('koa'); +var app = new Koa(); app.keys = ['some secret hurr']; diff --git a/index.js b/index.js index d7978c1..b095060 100644 --- a/index.js +++ b/index.js @@ -24,9 +24,7 @@ const _CONTEXT_SESSION = Symbol('context#_contextSession'); module.exports = function(opts, app) { // session(app[, opts]) if (opts && typeof opts.use === 'function') { - const tmp = app; - app = opts; - opts = tmp; + [ app, opts ] = [ opts, app ]; } // app required if (!app || typeof app.use !== 'function') { diff --git a/package.json b/package.json index c05e259..5091185 100644 --- a/package.json +++ b/package.json @@ -39,4 +39,4 @@ "test-travis": "npm run lint && NODE_ENV=test node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should test/*.test.js", "lint": "eslint lib test index.js" } -} \ No newline at end of file +} From b2e7c3ac8683342925ab38f4242279fbd16943a5 Mon Sep 17 00:00:00 2001 From: Runrioter Date: Tue, 19 Sep 2017 18:12:28 +0800 Subject: [PATCH 2/2] feat: make encode/decode support store-based session #88 --- index.js | 9 --------- lib/context.js | 26 ++++++++++++++++++++------ test/cookie.test.js | 8 ++++---- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index b095060..62aabce 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,6 @@ const debug = require('debug')('koa-session'); const ContextSession = require('./lib/context'); -const util = require('./lib/util'); const assert = require('assert'); const uid = require('uid-safe'); const is = require('is-type-of'); @@ -70,14 +69,6 @@ function formatOpts(opts) { debug('session options %j', opts); - // setup encoding/decoding - if (typeof opts.encode !== 'function') { - opts.encode = util.encode; - } - if (typeof opts.decode !== 'function') { - opts.decode = util.decode; - } - const store = opts.store; if (store) { assert(is.function(store.get), 'store.get must be function'); diff --git a/lib/context.js b/lib/context.js index f1dc702..8aed0ae 100644 --- a/lib/context.js +++ b/lib/context.js @@ -69,10 +69,20 @@ class ContextSession { const ctx = this.ctx; const opts = this.opts; - const externalKey = ctx.cookies.get(opts.key, opts); - debug('get external key from cookie %s', externalKey); + const cookie = ctx.cookies.get(opts.key, opts); + debug('get external key from cookie %s', cookie); - if (!externalKey) { + if (!cookie) { + // create a new `externalKey` + this.create(); + return; + } + let externalKey = cookie; + try { + if (typeof opts.decode === 'function') { + externalKey = opts.decode(externalKey); + } + } catch (err) { // create a new `externalKey` this.create(); return; @@ -109,7 +119,7 @@ class ContextSession { let json; debug('parse %s', cookie); try { - json = opts.decode(cookie); + json = typeof opts.decode === 'function' ? opts.decode(cookie) : util.decode(cookie); } catch (err) { // backwards compatibility: // create a new session if parsing fails. @@ -263,13 +273,17 @@ class ContextSession { changed, rolling: opts.rolling, }); - this.ctx.cookies.set(key, externalKey, opts); + let cookie = externalKey; + if (typeof opts.encode === 'function') { + cookie = opts.encode(externalKey); + } + this.ctx.cookies.set(key, cookie, opts); return; } // save to cookie debug('save %j to cookie', json); - json = opts.encode(json); + json = typeof opts.encode === 'function' ? opts.encode(json) : util.encode(json); debug('save %s', json); this.ctx.cookies.set(key, json, opts); diff --git a/test/cookie.test.js b/test/cookie.test.js index dc15fb9..ddc2462 100644 --- a/test/cookie.test.js +++ b/test/cookie.test.js @@ -761,10 +761,10 @@ describe('Koa Session Cookie', () => { before(() => { app = App({ rolling: true }); - app.use(function* () { - console.log(this.path); - if (this.path === '/set') this.session = { foo: 'bar' }; - this.body = this.session; + app.use(async function(ctx) { + console.log(ctx.path); + if (ctx.path === '/set') ctx.session = { foo: 'bar' }; + ctx.body = ctx.session; }); });