-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support external store #66
Conversation
2 similar comments
maybe we can deprecate koa-generic-session after merge this, tow session modules with slightly different make user confused. |
*/ | ||
|
||
constructor(ctx, obj) { | ||
this._ctx = ctx; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContextSession named ctx
but here is _ctx
, what's the different on naming ctx property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. toJSON
hacking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Should release a major version. |
index.js
Outdated
} catch (err) { | ||
throw err; | ||
} finally { | ||
commit(this, this._prevjson, this._sess, opts); | ||
yield this.sess.commit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commit
will throw error and who will catch this error? Is it send error response twice when commit error
and yield next
error happen in the same time?
--- EDIT ---
It look ok on context.onerror even error throw twice https://github.com/koajs/koa/blob/master/lib/context.js#L113
Need one more reviewer @popomore |
lib/context.js
Outdated
// when `string` is not base64-encoded. | ||
// but `JSON.parse(string)` will crash. | ||
debug('decode %j error: %s', cookie, err); | ||
if (!(err instanceof SyntaxError)) throw err; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should clean this cookie to ensure next request won't thrown again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but this error should rarely happend
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't help user to clean up, they won't never get the normal response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
index.js
Outdated
var body = new Buffer(string, 'base64').toString('utf8'); | ||
var json = JSON.parse(body); | ||
function extendContext(context, opts) { | ||
context.__defineGetter__('sess', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use Object.defineProperty, this API is deprecated https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or can use assign?
Object.assign(context, {
get sess() {},
})
index.js
Outdated
var json = JSON.parse(body); | ||
function extendContext(context, opts) { | ||
context.__defineGetter__('sess', function() { | ||
if (this._sess) return this._sess; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use Symbol now?
lib/context.js
Outdated
return; | ||
} | ||
|
||
const json = yield this.store.get(externalKey, ctx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it throw?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ctx always be the first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could throw here, it is strong dependent on external store if options.store
present.
I'll remove ctx
here, it is useless. Keep the signature the same as koa-generic-session
's store.
|
||
// cookie session store | ||
if (!this.store) this.initFromCookie(); | ||
return this.session; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why just return session?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
session
is undefined here, this.session
will be assigned after this.initFromCookie()
index.js
Outdated
// make sessionOptions independent in each request | ||
initSessionOptions(this, opts); | ||
return function* session(next) { | ||
if (this.sess.store) yield this.sess.initFromExternal(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use cookie session as one of the store, so this needn't call external.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cookie store is a little different from other external stores.
- it is synchronize so it can be lazy loading,
- it use the same cookie field to store the value.
If we treat cookie session as a store, we still need to write some tricky code.
* @api private | ||
*/ | ||
|
||
initFromCookie() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initFromCookie will call when first get, and initFromExternal run in middleware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lazy loading is good for those route don't touch session when using cookie storage.
ping @popomore @jonathanong |
@jonathanong we gonna merge this PR if there is no new question until next Monday. |
@dead-horse np |
2 similar comments
I'll merge this and release a new major version, and then rewrite it with async function for koa 2 support. |
Feature
options.store
.Fixes
options.decode
will check expired now."guest" sessions
any more.Compatibility
session.save()
, won't set cookie immediately now.session.changed()
.context.sessionKey
, can useopts.key
instead.context.sessionOptions
to getter.Performance
QPS is reduced by <10% because of more generator function are used.