Skip to content
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

Merged
merged 21 commits into from
Feb 27, 2017
Merged

Support external store #66

merged 21 commits into from
Feb 27, 2017

Conversation

dead-horse
Copy link
Member

@dead-horse dead-horse commented Feb 22, 2017

Feature

  • Support external store by pass options.store.
  • Throw when encode session error, consider a breaking change.
  • Clean cookie when decode session throw error, ensure next request won't throw again.

Fixes

  • Customize options.decode will check expired now.
  • Remove Semantics in README because it's not "guest" sessions any more.

Compatibility

  • All original test cases are passed.
  • Drop support for node < 4.
  • Internal implementations are changed, so some private API is changed.
    • Change private api session.save(), won't set cookie immediately now.
    • Remove private api session.changed().
    • Remove undocumented property context.sessionKey, can use opts.key instead.
    • Change undocumented property context.sessionOptions to getter.

Performance

QPS is reduced by <10% because of more generator function are used.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 93.956% when pulling bc9dc97 on external into 3bb2f0b on master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 93.956% when pulling bc9dc97 on external into 3bb2f0b on master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 93.956% when pulling bc9dc97 on external into 3bb2f0b on master.

@dead-horse
Copy link
Member Author

dead-horse commented Feb 22, 2017

maybe we can deprecate koa-generic-session after merge this, tow session modules with slightly different make user confused.

@coveralls
Copy link

coveralls commented Feb 22, 2017

Coverage Status

Coverage increased (+0.6%) to 94.972% when pulling 369616e on external into 3bb2f0b on master.

*/

constructor(ctx, obj) {
this._ctx = ctx;
Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. toJSON hacking

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fengmk2
Copy link
Member

fengmk2 commented Feb 22, 2017

Should release a major version.

@dead-horse
Copy link
Member Author

@fengmk2 add 490f31a

@coveralls
Copy link

coveralls commented Feb 22, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling 490f31a on external into 3bb2f0b on master.

@coveralls
Copy link

coveralls commented Feb 22, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling 4687a38 on external into 3bb2f0b on master.

index.js Outdated
} catch (err) {
throw err;
} finally {
commit(this, this._prevjson, this._sess, opts);
yield this.sess.commit();
Copy link
Member

@fengmk2 fengmk2 Feb 22, 2017

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

@fengmk2
Copy link
Member

fengmk2 commented Feb 22, 2017

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;
Copy link
Member Author

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.

Copy link
Member Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@coveralls
Copy link

coveralls commented Feb 22, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling ef64dcc on external into 3bb2f0b on master.

@coveralls
Copy link

coveralls commented Feb 22, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling ef64dcc on external into 3bb2f0b on master.

@jonathanong jonathanong self-assigned this Feb 22, 2017
@coveralls
Copy link

coveralls commented Feb 23, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling 46ee6a5 on external into 3bb2f0b on master.

index.js Outdated
var body = new Buffer(string, 'base64').toString('utf8');
var json = JSON.parse(body);
function extendContext(context, opts) {
context.__defineGetter__('sess', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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;
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it throw?

Copy link
Member

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?

Copy link
Member Author

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why just return session?

Copy link
Member Author

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();
Copy link
Member

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.

Copy link
Member Author

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.

  1. it is synchronize so it can be lazy loading,
  2. 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() {
Copy link
Member

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

Copy link
Member Author

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.

@coveralls
Copy link

coveralls commented Feb 23, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling ffb15e5 on external into 3bb2f0b on master.

@coveralls
Copy link

coveralls commented Feb 23, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling d1b2112 on external into 3bb2f0b on master.

@dead-horse dead-horse mentioned this pull request Feb 23, 2017
31 tasks
@coveralls
Copy link

coveralls commented Feb 24, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling 012283e on external into 3bb2f0b on master.

@dead-horse
Copy link
Member Author

ping @popomore @jonathanong

@coveralls
Copy link

coveralls commented Feb 24, 2017

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling e33923b on external into 3bb2f0b on master.

@dead-horse
Copy link
Member Author

@jonathanong we gonna merge this PR if there is no new question until next Monday.

@jonathanong
Copy link
Member

@dead-horse np

@coveralls
Copy link

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling f916f20 on external into 3bb2f0b on master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling f916f20 on external into 3bb2f0b on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+5.6%) to 100.0% when pulling f916f20 on external into 3bb2f0b on master.

@dead-horse
Copy link
Member Author

I'll merge this and release a new major version, and then rewrite it with async function for koa 2 support.

@dead-horse dead-horse merged commit 2f7010b into master Feb 27, 2017
@dead-horse dead-horse deleted the external branch February 27, 2017 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants